@formulaxjs/kity-runtime 0.2.0 → 0.3.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,9 +796,11 @@ 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";
799
800
  var FontInstallerModule = class {
800
801
  static create(kity, FontManager, fontConfig, checkerTemplate) {
801
802
  let nodeList = [];
803
+ let checkerNode = null;
802
804
  return kity.createClass("FontInstaller", {
803
805
  constructor: function(doc, resource) {
804
806
  const normalized = typeof resource === "string" ? { path: resource } : resource ?? {};
@@ -808,36 +810,30 @@ var FontInstallerModule = class {
808
810
  },
809
811
  mount: function(callback) {
810
812
  const fontList = FontManager.getFontList();
811
- let count = 0;
812
813
  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
- preloadFont(this.doc, fontInfo).then(() => {
817
- applyFonts(this.doc, fontInfo);
818
- }).catch(() => void 0).finally(() => {
819
- count -= 1;
820
- if (count === 0) {
821
- complete(this.doc, callback);
822
- }
823
- });
816
+ if (fontInfo.meta.fontFamily === MAIN_FONT_FAMILY) {
817
+ checkerNode = createFontCheckerNode(this.doc);
818
+ return;
819
+ }
820
+ applyFonts(this.doc, fontInfo);
824
821
  });
822
+ complete(this.doc, callback);
825
823
  },
826
824
  createFontStyle: function(fontInfo) {
825
+ const styleId = createFontStyleId(fontInfo);
826
+ if (this.doc.getElementById(styleId)) {
827
+ return;
828
+ }
827
829
  const stylesheet = this.doc.createElement("style");
828
830
  const tpl = '@font-face{\nfont-family: "${fontFamily}";\nsrc: url("${src}");\n}';
829
831
  stylesheet.setAttribute("type", "text/css");
832
+ stylesheet.id = styleId;
830
833
  stylesheet.innerHTML = tpl.replace("${fontFamily}", fontInfo.meta.fontFamily).replace("${src}", fontInfo.meta.src);
831
834
  this.doc.head.appendChild(stylesheet);
832
835
  }
833
836
  });
834
- function preloadFont(doc, fontInfo) {
835
- const view = doc.defaultView ?? window;
836
- if (view.fetch) {
837
- return view.fetch(fontInfo.meta.src, { method: "GET" }).then(() => void 0);
838
- }
839
- return Promise.resolve();
840
- }
841
837
  function resolveFontSource(fonts, resourceBase, originalSrc) {
842
838
  const directMatch = fonts[originalSrc];
843
839
  if (directMatch) {
@@ -850,34 +846,29 @@ var FontInstallerModule = class {
850
846
  }
851
847
  return resourceBase + originalSrc;
852
848
  }
853
- function waitForFontsReady(doc, fontList) {
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) {
854
858
  const view = doc.defaultView ?? window;
855
859
  if (view.document.fonts) {
856
- const fontLoadPromises = fontList.map((fontInfo) => {
857
- return view.document.fonts.load(`50px "${fontInfo.meta.fontFamily}"`);
858
- });
859
- return Promise.all(fontLoadPromises).then(() => view.document.fonts.ready).then(() => {
860
- return new Promise((resolve) => {
861
- requestAnimationFrame(() => {
862
- requestAnimationFrame(() => {
863
- resolve();
864
- });
865
- });
866
- });
867
- });
860
+ return view.document.fonts.ready.then(() => void 0);
868
861
  }
869
862
  return Promise.resolve();
870
863
  }
871
864
  function complete(doc, callback) {
872
865
  const view = doc.defaultView ?? window;
873
- const fontList = FontManager.getFontList();
874
- const fontArray = Object.values(fontList);
875
- waitForFontsReady(doc, fontArray).then(() => {
876
- view.setTimeout(() => {
877
- initFontSystemInfo(doc);
878
- removeTmpNode();
879
- callback();
880
- }, 100);
866
+ waitForFontsReady(doc).then(() => {
867
+ return waitForNextFrames(view, 2);
868
+ }).then(() => {
869
+ initFontSystemInfo(doc);
870
+ removeTmpNode();
871
+ callback();
881
872
  }).catch(() => {
882
873
  view.setTimeout(() => {
883
874
  initFontSystemInfo(doc);
@@ -898,12 +889,37 @@ var FontInstallerModule = class {
898
889
  doc.body.appendChild(node);
899
890
  nodeList.push(node);
900
891
  }
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
+ }
901
920
  function initFontSystemInfo(doc) {
902
- const tmpNode = doc.createElement("div");
903
- tmpNode.style.cssText = "position: absolute; top: 0; left: -100000px;";
904
- tmpNode.innerHTML = checkerTemplate.join("");
905
- doc.body.appendChild(tmpNode);
906
- const rectBox = tmpNode.getElementsByTagName("text")[0].getBBox();
921
+ const activeCheckerNode = checkerNode ?? createFontCheckerNode(doc);
922
+ const rectBox = activeCheckerNode.getElementsByTagName("text")[0].getBBox();
907
923
  fontConfig.spaceHeight = rectBox.height;
908
924
  fontConfig.topSpace = -rectBox.y - fontConfig.baseline;
909
925
  fontConfig.bottomSpace = fontConfig.spaceHeight - fontConfig.topSpace - fontConfig.baseHeight;
@@ -912,7 +928,8 @@ var FontInstallerModule = class {
912
928
  fontConfig.meanlinePosition = (fontConfig.topSpace + fontConfig.meanline) / fontConfig.spaceHeight;
913
929
  fontConfig.ascenderPosition = fontConfig.topSpace / fontConfig.spaceHeight;
914
930
  fontConfig.descenderPosition = (fontConfig.topSpace + fontConfig.baseHeight) / fontConfig.spaceHeight;
915
- doc.body.removeChild(tmpNode);
931
+ activeCheckerNode.parentNode?.removeChild(activeCheckerNode);
932
+ checkerNode = null;
916
933
  }
917
934
  function removeTmpNode() {
918
935
  kity.Utils.each(nodeList, (node) => {
@@ -5471,4 +5488,4 @@ export {
5471
5488
  install_default as default,
5472
5489
  installLegacyKityFormulaRuntime
5473
5490
  };
5474
- //# sourceMappingURL=install-ARHGHFNJ.js.map
5491
+ //# sourceMappingURL=install-TIZBWEFU.js.map