@formulaxjs/kity-runtime 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -796,11 +796,9 @@ var ExpressionModule = class {
796
796
  var createExpressionClass = ExpressionModule.create;
797
797
 
798
798
  // src/vendor/kity-formula/font-installer.ts
799
- var MAIN_FONT_FAMILY = "KF AMS MAIN";
800
799
  var FontInstallerModule = class {
801
800
  static create(kity, FontManager, fontConfig, checkerTemplate) {
802
801
  let nodeList = [];
803
- let checkerNode = null;
804
802
  return kity.createClass("FontInstaller", {
805
803
  constructor: function(doc, resource) {
806
804
  const normalized = typeof resource === "string" ? { path: resource } : resource ?? {};
@@ -810,26 +808,22 @@ var FontInstallerModule = class {
810
808
  },
811
809
  mount: function(callback) {
812
810
  const fontList = FontManager.getFontList();
811
+ let count = 0;
813
812
  kity.Utils.each(fontList, (fontInfo) => {
813
+ count += 1;
814
814
  fontInfo.meta.src = resolveFontSource(this.fonts, this.resource, fontInfo.meta.src);
815
815
  this.createFontStyle(fontInfo);
816
- if (fontInfo.meta.fontFamily === MAIN_FONT_FAMILY) {
817
- checkerNode = createFontCheckerNode(this.doc);
818
- return;
819
- }
820
816
  applyFonts(this.doc, fontInfo);
817
+ count -= 1;
818
+ if (count === 0) {
819
+ complete(this.doc, callback);
820
+ }
821
821
  });
822
- complete(this.doc, callback);
823
822
  },
824
823
  createFontStyle: function(fontInfo) {
825
- const styleId = createFontStyleId(fontInfo);
826
- if (this.doc.getElementById(styleId)) {
827
- return;
828
- }
829
824
  const stylesheet = this.doc.createElement("style");
830
825
  const tpl = '@font-face{\nfont-family: "${fontFamily}";\nsrc: url("${src}");\n}';
831
826
  stylesheet.setAttribute("type", "text/css");
832
- stylesheet.id = styleId;
833
827
  stylesheet.innerHTML = tpl.replace("${fontFamily}", fontInfo.meta.fontFamily).replace("${src}", fontInfo.meta.src);
834
828
  this.doc.head.appendChild(stylesheet);
835
829
  }
@@ -846,29 +840,34 @@ var FontInstallerModule = class {
846
840
  }
847
841
  return resourceBase + originalSrc;
848
842
  }
849
- function createFontStyleId(fontInfo) {
850
- const raw = `${fontInfo.meta.fontFamily}::${fontInfo.meta.src}`;
851
- let hash = 0;
852
- for (let index = 0; index < raw.length; index += 1) {
853
- hash = (hash << 5) - hash + raw.charCodeAt(index) | 0;
854
- }
855
- return `formulax-kity-font-${Math.abs(hash).toString(36)}`;
856
- }
857
- function waitForFontsReady(doc) {
843
+ function waitForFontsReady(doc, fontList) {
858
844
  const view = doc.defaultView ?? window;
859
845
  if (view.document.fonts) {
860
- return view.document.fonts.ready.then(() => void 0);
846
+ const fontLoadPromises = fontList.map((fontInfo) => {
847
+ return view.document.fonts.load(`50px "${fontInfo.meta.fontFamily}"`);
848
+ });
849
+ return Promise.all(fontLoadPromises).then(() => view.document.fonts.ready).then(() => {
850
+ return new Promise((resolve) => {
851
+ requestAnimationFrame(() => {
852
+ requestAnimationFrame(() => {
853
+ resolve();
854
+ });
855
+ });
856
+ });
857
+ });
861
858
  }
862
859
  return Promise.resolve();
863
860
  }
864
861
  function complete(doc, callback) {
865
862
  const view = doc.defaultView ?? window;
866
- waitForFontsReady(doc).then(() => {
867
- return waitForNextFrames(view, 2);
868
- }).then(() => {
869
- initFontSystemInfo(doc);
870
- removeTmpNode();
871
- callback();
863
+ const fontList = FontManager.getFontList();
864
+ const fontArray = Object.values(fontList);
865
+ waitForFontsReady(doc, fontArray).then(() => {
866
+ view.setTimeout(() => {
867
+ initFontSystemInfo(doc);
868
+ removeTmpNode();
869
+ callback();
870
+ }, 100);
872
871
  }).catch(() => {
873
872
  view.setTimeout(() => {
874
873
  initFontSystemInfo(doc);
@@ -889,37 +888,12 @@ var FontInstallerModule = class {
889
888
  doc.body.appendChild(node);
890
889
  nodeList.push(node);
891
890
  }
892
- function createFontCheckerNode(doc) {
893
- if (checkerNode) {
894
- return checkerNode;
895
- }
896
- const node = doc.createElement("div");
897
- node.style.cssText = "position: absolute; top: 0; left: -100000px;";
898
- node.innerHTML = checkerTemplate.join("");
899
- doc.body.appendChild(node);
900
- checkerNode = node;
901
- return node;
902
- }
903
- function waitForNextFrames(view, frameCount) {
904
- if (typeof view.requestAnimationFrame !== "function") {
905
- return Promise.resolve();
906
- }
907
- return new Promise((resolve) => {
908
- const step = (remaining) => {
909
- if (remaining <= 0) {
910
- resolve();
911
- return;
912
- }
913
- view.requestAnimationFrame(() => {
914
- step(remaining - 1);
915
- });
916
- };
917
- step(frameCount);
918
- });
919
- }
920
891
  function initFontSystemInfo(doc) {
921
- const activeCheckerNode = checkerNode ?? createFontCheckerNode(doc);
922
- const rectBox = activeCheckerNode.getElementsByTagName("text")[0].getBBox();
892
+ const tmpNode = doc.createElement("div");
893
+ tmpNode.style.cssText = "position: absolute; top: 0; left: -100000px;";
894
+ tmpNode.innerHTML = checkerTemplate.join("");
895
+ doc.body.appendChild(tmpNode);
896
+ const rectBox = tmpNode.getElementsByTagName("text")[0].getBBox();
923
897
  fontConfig.spaceHeight = rectBox.height;
924
898
  fontConfig.topSpace = -rectBox.y - fontConfig.baseline;
925
899
  fontConfig.bottomSpace = fontConfig.spaceHeight - fontConfig.topSpace - fontConfig.baseHeight;
@@ -928,8 +902,7 @@ var FontInstallerModule = class {
928
902
  fontConfig.meanlinePosition = (fontConfig.topSpace + fontConfig.meanline) / fontConfig.spaceHeight;
929
903
  fontConfig.ascenderPosition = fontConfig.topSpace / fontConfig.spaceHeight;
930
904
  fontConfig.descenderPosition = (fontConfig.topSpace + fontConfig.baseHeight) / fontConfig.spaceHeight;
931
- activeCheckerNode.parentNode?.removeChild(activeCheckerNode);
932
- checkerNode = null;
905
+ doc.body.removeChild(tmpNode);
933
906
  }
934
907
  function removeTmpNode() {
935
908
  kity.Utils.each(nodeList, (node) => {
@@ -5488,4 +5461,4 @@ export {
5488
5461
  install_default as default,
5489
5462
  installLegacyKityFormulaRuntime
5490
5463
  };
5491
- //# sourceMappingURL=install-TIZBWEFU.js.map
5464
+ //# sourceMappingURL=install-WDCVVIMM.js.map