@file-viewer/docx 0.3.41 → 0.3.42

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.
@@ -7083,6 +7083,22 @@
7083
7083
  return { text, normalized: text !== original };
7084
7084
  }
7085
7085
 
7086
+ const eastAsianCharacter = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Bopomofo}\p{Script=Yi}\u2e80-\u2fff\u3000-\u303f\u31c0-\u31ef\u3200-\u33ff\ufe10-\ufe1f\ufe30-\ufe6f\uff00-\uffef]/u;
7087
+ const precedingFont = /[\p{Mark}\u200c\u200d\u2060]/u;
7088
+ function fontScriptRuns(text, previousEastAsian = false) {
7089
+ const runs = [];
7090
+ for (const character of text) {
7091
+ const previous = runs[runs.length - 1];
7092
+ const eastAsian = precedingFont.test(character)
7093
+ ? previous?.eastAsian ?? previousEastAsian : eastAsianCharacter.test(character);
7094
+ if (previous?.eastAsian === eastAsian)
7095
+ previous.text += character;
7096
+ else
7097
+ runs.push({ text: character, eastAsian });
7098
+ }
7099
+ return runs;
7100
+ }
7101
+
7086
7102
  class CommentLayer {
7087
7103
  constructor(className, createLayer, layoutContent) {
7088
7104
  this.className = className;
@@ -8858,6 +8874,7 @@
8858
8874
  this.currentTabs = [];
8859
8875
  this.anchorLayouts = new Map();
8860
8876
  this.currentParagraph = null;
8877
+ this.currentRunFont = null;
8861
8878
  this.currentParagraphTabIndex = 0;
8862
8879
  this.currentParagraphTabCount = 0;
8863
8880
  this.commentMap = {};
@@ -9914,7 +9931,7 @@
9914
9931
  this.setCssStyle(result, "--docx-latin-font-family", latin);
9915
9932
  this.setCssStyle(result, "font-family", `var(--docx-latin-font-family), ${family}`);
9916
9933
  }
9917
- applyRunFontSlots(elem, style, result) {
9934
+ resolveRunFontSlots(elem, style) {
9918
9935
  const paragraph = elem.type == DomType.Paragraph ? elem : this.currentParagraph;
9919
9936
  const paragraphStyle = this.findStyle(paragraph?.styleName) ?? this.defaultParagraphStyle;
9920
9937
  const levels = [elem.cssStyle, ...this.styleValueLevels(style, "span"),
@@ -9940,18 +9957,29 @@
9940
9957
  && !/[\p{Script=Latin}\p{Script=Cyrillic}\p{Script=Han}]/u.test(text))
9941
9958
  || isAdditionalComplexScriptRun(text)));
9942
9959
  const highAnsi = /[\p{Script=Cyrillic}\p{Script=Greek}]/u.test(text);
9960
+ const scriptRuns = fontScriptRuns(text);
9961
+ const eastAsianOnly = scriptRuns.length && scriptRuns.every(run => run.eastAsian);
9943
9962
  const slots = complex ? ["complex", "ascii", "hansi", "eastasia"]
9944
- : highAnsi ? ["hansi", "ascii", "eastasia", "complex"] : ["ascii", "hansi", "eastasia", "complex"];
9945
- const fonts = slots.map(slot => {
9963
+ : eastAsianOnly ? ["eastasia", "ascii", "hansi", "complex"]
9964
+ : highAnsi ? ["hansi", "ascii", "eastasia", "complex"] : ["ascii", "hansi", "eastasia", "complex"];
9965
+ const resolveFonts = (slots) => slots.map(slot => {
9946
9966
  const font = value(`--docx-font-${slot}`);
9947
9967
  return slot == "eastasia" ? themedFont(font, language("$lang-eastAsia"))
9948
9968
  : slot == "complex" ? themedFont(font, language("$lang-bidi")) : font;
9949
9969
  }).filter(Boolean);
9950
- if (fonts.length)
9951
- this.setCssStyle(result, "font-family", [...new Set(fonts)].join(', '));
9952
- if (complex && value("--docx-complex-font-size")) {
9953
- this.setCssStyle(result, "font-size", value("--docx-complex-font-size"));
9954
- this.setCssStyle(result, "min-height", value("--docx-complex-font-size"));
9970
+ const fontFamily = [...new Set(resolveFonts(slots))].join(', ');
9971
+ const eastAsianFontFamily = !complex && value("--docx-font-eastasia")
9972
+ ? [...new Set(resolveFonts(["eastasia", ...slots.filter(slot => slot !== "eastasia")]))].join(', ') : null;
9973
+ return { fontFamily, eastAsianFontFamily,
9974
+ splitEastAsia: !!eastAsianFontFamily && eastAsianFontFamily !== fontFamily && scriptRuns.some(run => run.eastAsian),
9975
+ complexFontSize: complex ? value("--docx-complex-font-size") : null, previousEastAsian: false };
9976
+ }
9977
+ applyRunFontSlots(elem, style, result, font = this.resolveRunFontSlots(elem, style)) {
9978
+ if (font.fontFamily)
9979
+ this.setCssStyle(result, "font-family", font.fontFamily);
9980
+ if (font.complexFontSize) {
9981
+ this.setCssStyle(result, "font-size", font.complexFontSize);
9982
+ this.setCssStyle(result, "min-height", font.complexFontSize);
9955
9983
  }
9956
9984
  }
9957
9985
  isInsideTableCell(elem) {
@@ -14365,7 +14393,14 @@ section.${c}>footer { z-index: 1; }
14365
14393
  return false;
14366
14394
  }
14367
14395
  renderText(elem) {
14368
- return this.h(this.applyCjkLineBreakGuards(elem.text));
14396
+ const text = this.applyCjkLineBreakGuards(elem.text);
14397
+ if (!this.currentRunFont?.splitEastAsia)
14398
+ return this.h(text);
14399
+ const fragments = fontScriptRuns(text, this.currentRunFont.previousEastAsian);
14400
+ this.currentRunFont.previousEastAsian = fragments[fragments.length - 1]?.eastAsian ?? this.currentRunFont.previousEastAsian;
14401
+ return this.h({ tagName: "#fragment", children: fragments.map(run => run.eastAsian
14402
+ ? this.h({ tagName: "span", className: `${this.className}-font-eastasia`, style: { fontFamily: this.currentRunFont.eastAsianFontFamily }, children: [run.text] })
14403
+ : this.h(run.text)) });
14369
14404
  }
14370
14405
  applyCjkLineBreakGuards(text) {
14371
14406
  if (!text || !this.document.settingsPart?.settings?.kinsoku)
@@ -14547,7 +14582,17 @@ section.${c}>footer { z-index: 1; }
14547
14582
  : elem.children;
14548
14583
  if (elem.fieldRun && (!runChildren || runChildren.length == 0))
14549
14584
  return null;
14550
- let children = this.renderElements(runChildren);
14585
+ const previousFont = this.currentRunFont;
14586
+ const style = this.findStyle(elem.styleName);
14587
+ const font = this.resolveRunFontSlots(elem, style);
14588
+ this.currentRunFont = font;
14589
+ let children;
14590
+ try {
14591
+ children = this.renderElements(runChildren);
14592
+ }
14593
+ finally {
14594
+ this.currentRunFont = previousFont;
14595
+ }
14551
14596
  if (elem.verticalAlign) {
14552
14597
  children = [this.h({ tagName: elem.verticalAlign, children })];
14553
14598
  }
@@ -14564,9 +14609,8 @@ section.${c}>footer { z-index: 1; }
14564
14609
  return tab;
14565
14610
  }
14566
14611
  const result = this.toHTML(elem, ns.html, "span", children);
14567
- const style = this.findStyle(elem.styleName);
14568
14612
  this.applyTextBoxFontInheritance(elem, style, result);
14569
- this.applyRunFontSlots(elem, style, result);
14613
+ this.applyRunFontSlots(elem, style, result, font);
14570
14614
  const snapToGrid = elem.cssStyle?.["--docx-snap-to-grid"] ?? this.styleValue(style, "span", "--docx-snap-to-grid");
14571
14615
  if (/^(0|false|off)$/i.test(snapToGrid ?? ""))
14572
14616
  result.style.letterSpacing = "normal";