@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.
@@ -7079,6 +7079,22 @@ function decodeWordSymbol(font, code) {
7079
7079
  return { text, normalized: text !== original };
7080
7080
  }
7081
7081
 
7082
+ 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;
7083
+ const precedingFont = /[\p{Mark}\u200c\u200d\u2060]/u;
7084
+ function fontScriptRuns(text, previousEastAsian = false) {
7085
+ const runs = [];
7086
+ for (const character of text) {
7087
+ const previous = runs[runs.length - 1];
7088
+ const eastAsian = precedingFont.test(character)
7089
+ ? previous?.eastAsian ?? previousEastAsian : eastAsianCharacter.test(character);
7090
+ if (previous?.eastAsian === eastAsian)
7091
+ previous.text += character;
7092
+ else
7093
+ runs.push({ text: character, eastAsian });
7094
+ }
7095
+ return runs;
7096
+ }
7097
+
7082
7098
  class CommentLayer {
7083
7099
  constructor(className, createLayer, layoutContent) {
7084
7100
  this.className = className;
@@ -8854,6 +8870,7 @@ class HtmlRenderer {
8854
8870
  this.currentTabs = [];
8855
8871
  this.anchorLayouts = new Map();
8856
8872
  this.currentParagraph = null;
8873
+ this.currentRunFont = null;
8857
8874
  this.currentParagraphTabIndex = 0;
8858
8875
  this.currentParagraphTabCount = 0;
8859
8876
  this.commentMap = {};
@@ -9910,7 +9927,7 @@ class HtmlRenderer {
9910
9927
  this.setCssStyle(result, "--docx-latin-font-family", latin);
9911
9928
  this.setCssStyle(result, "font-family", `var(--docx-latin-font-family), ${family}`);
9912
9929
  }
9913
- applyRunFontSlots(elem, style, result) {
9930
+ resolveRunFontSlots(elem, style) {
9914
9931
  const paragraph = elem.type == DomType.Paragraph ? elem : this.currentParagraph;
9915
9932
  const paragraphStyle = this.findStyle(paragraph?.styleName) ?? this.defaultParagraphStyle;
9916
9933
  const levels = [elem.cssStyle, ...this.styleValueLevels(style, "span"),
@@ -9936,18 +9953,29 @@ class HtmlRenderer {
9936
9953
  && !/[\p{Script=Latin}\p{Script=Cyrillic}\p{Script=Han}]/u.test(text))
9937
9954
  || isAdditionalComplexScriptRun(text)));
9938
9955
  const highAnsi = /[\p{Script=Cyrillic}\p{Script=Greek}]/u.test(text);
9956
+ const scriptRuns = fontScriptRuns(text);
9957
+ const eastAsianOnly = scriptRuns.length && scriptRuns.every(run => run.eastAsian);
9939
9958
  const slots = complex ? ["complex", "ascii", "hansi", "eastasia"]
9940
- : highAnsi ? ["hansi", "ascii", "eastasia", "complex"] : ["ascii", "hansi", "eastasia", "complex"];
9941
- const fonts = slots.map(slot => {
9959
+ : eastAsianOnly ? ["eastasia", "ascii", "hansi", "complex"]
9960
+ : highAnsi ? ["hansi", "ascii", "eastasia", "complex"] : ["ascii", "hansi", "eastasia", "complex"];
9961
+ const resolveFonts = (slots) => slots.map(slot => {
9942
9962
  const font = value(`--docx-font-${slot}`);
9943
9963
  return slot == "eastasia" ? themedFont(font, language("$lang-eastAsia"))
9944
9964
  : slot == "complex" ? themedFont(font, language("$lang-bidi")) : font;
9945
9965
  }).filter(Boolean);
9946
- if (fonts.length)
9947
- this.setCssStyle(result, "font-family", [...new Set(fonts)].join(', '));
9948
- if (complex && value("--docx-complex-font-size")) {
9949
- this.setCssStyle(result, "font-size", value("--docx-complex-font-size"));
9950
- this.setCssStyle(result, "min-height", value("--docx-complex-font-size"));
9966
+ const fontFamily = [...new Set(resolveFonts(slots))].join(', ');
9967
+ const eastAsianFontFamily = !complex && value("--docx-font-eastasia")
9968
+ ? [...new Set(resolveFonts(["eastasia", ...slots.filter(slot => slot !== "eastasia")]))].join(', ') : null;
9969
+ return { fontFamily, eastAsianFontFamily,
9970
+ splitEastAsia: !!eastAsianFontFamily && eastAsianFontFamily !== fontFamily && scriptRuns.some(run => run.eastAsian),
9971
+ complexFontSize: complex ? value("--docx-complex-font-size") : null, previousEastAsian: false };
9972
+ }
9973
+ applyRunFontSlots(elem, style, result, font = this.resolveRunFontSlots(elem, style)) {
9974
+ if (font.fontFamily)
9975
+ this.setCssStyle(result, "font-family", font.fontFamily);
9976
+ if (font.complexFontSize) {
9977
+ this.setCssStyle(result, "font-size", font.complexFontSize);
9978
+ this.setCssStyle(result, "min-height", font.complexFontSize);
9951
9979
  }
9952
9980
  }
9953
9981
  isInsideTableCell(elem) {
@@ -14361,7 +14389,14 @@ section.${c}>footer { z-index: 1; }
14361
14389
  return false;
14362
14390
  }
14363
14391
  renderText(elem) {
14364
- return this.h(this.applyCjkLineBreakGuards(elem.text));
14392
+ const text = this.applyCjkLineBreakGuards(elem.text);
14393
+ if (!this.currentRunFont?.splitEastAsia)
14394
+ return this.h(text);
14395
+ const fragments = fontScriptRuns(text, this.currentRunFont.previousEastAsian);
14396
+ this.currentRunFont.previousEastAsian = fragments[fragments.length - 1]?.eastAsian ?? this.currentRunFont.previousEastAsian;
14397
+ return this.h({ tagName: "#fragment", children: fragments.map(run => run.eastAsian
14398
+ ? this.h({ tagName: "span", className: `${this.className}-font-eastasia`, style: { fontFamily: this.currentRunFont.eastAsianFontFamily }, children: [run.text] })
14399
+ : this.h(run.text)) });
14365
14400
  }
14366
14401
  applyCjkLineBreakGuards(text) {
14367
14402
  if (!text || !this.document.settingsPart?.settings?.kinsoku)
@@ -14543,7 +14578,17 @@ section.${c}>footer { z-index: 1; }
14543
14578
  : elem.children;
14544
14579
  if (elem.fieldRun && (!runChildren || runChildren.length == 0))
14545
14580
  return null;
14546
- let children = this.renderElements(runChildren);
14581
+ const previousFont = this.currentRunFont;
14582
+ const style = this.findStyle(elem.styleName);
14583
+ const font = this.resolveRunFontSlots(elem, style);
14584
+ this.currentRunFont = font;
14585
+ let children;
14586
+ try {
14587
+ children = this.renderElements(runChildren);
14588
+ }
14589
+ finally {
14590
+ this.currentRunFont = previousFont;
14591
+ }
14547
14592
  if (elem.verticalAlign) {
14548
14593
  children = [this.h({ tagName: elem.verticalAlign, children })];
14549
14594
  }
@@ -14560,9 +14605,8 @@ section.${c}>footer { z-index: 1; }
14560
14605
  return tab;
14561
14606
  }
14562
14607
  const result = this.toHTML(elem, ns.html, "span", children);
14563
- const style = this.findStyle(elem.styleName);
14564
14608
  this.applyTextBoxFontInheritance(elem, style, result);
14565
- this.applyRunFontSlots(elem, style, result);
14609
+ this.applyRunFontSlots(elem, style, result, font);
14566
14610
  const snapToGrid = elem.cssStyle?.["--docx-snap-to-grid"] ?? this.styleValue(style, "span", "--docx-snap-to-grid");
14567
14611
  if (/^(0|false|off)$/i.test(snapToGrid ?? ""))
14568
14612
  result.style.letterSpacing = "normal";