Skip to content

Hebrew and Arabic runs take the Latin font and size: w:szCs and w:rFonts/@cs are never read #3994

Open

Description

@Nathaniel-260

What happened?

First, thank you — the engagement on #3959 and #3983 has been real, and the code around
this area is a pleasure to read: RunScriptContext in direction-context.ts states the
ECMA complex-script rule more clearly than the spec does. This issue is the half of that
rule that is still open.

Per ECMA-376 Annex I, a run resolving through the complex-script stack takes four
properties from the CS variants: bold from w:bCs, italic from w:iCs, size from
w:szCs, and font from w:rFonts/@cs
. #3959 (and PR #3958) covers the first two. The
other two are the ones that decide what the text looks like at all, and nothing reads
them — so a Hebrew or Arabic run is laid out and painted with the font and the size
belonging to the other script.

Where it stops, in the open tree (main @ 9ae9f03)

packages/layout-engine/style-engine/src/normalize/run-attrs.ts:

  • Size. Line 80 is const fontSize = resolveFontSize(props.fontSize); — the Latin
    w:sz, and only it. RunProperties.fontSizeCs is declared at
    style-engine/src/ooxml/types.ts:322 ("Complex script font size in half-points") and is
    read nowhere in the package.
  • Font. resolveFontFamily (line 139) walks
    ascii → hAnsi → asciiTheme/hAnsiTheme → eastAsia → eastAsiaTheme → cs → cstheme → val,
    so the cs slot at line 165 is reached only when the entire Latin track resolved to
    nothing. The function's own comment says as much — the ascii/hAnsi track is "used by
    Latin text — the only thing v2 paints generically right now".
  • And there is nowhere for a per-script value to travel. TextRunStyleAttrs
    (style-engine/src/normalize/types.ts:24-25) carries one fontFamily and one
    fontSize, so the stack has to be chosen inside the normalizer — exactly where fix(style-engine): resolve bold and italic from the complex-script toggles #3958
    now chooses it for bold and italic.

The contract already documents this as unfinished work, in RunScriptContext
(packages/layout-engine/contracts/src/direction-context.ts:150-160):

Per ECMA Annex I, when w:rtl is set or w:cs is set, the run's formatting comes from the
complex-script variants (bCs, iCs, szCs, rFonts/@cs). Otherwise it comes from the Latin
variants (b, i, sz, rFonts/@Ascii).

This context is preservation-only in Wave 1a. Wave 1b implements the stack-selection
logic (resolveRunScriptContext returns whether to render with the CS or Latin stack).

resolveRunScriptContext appears in that comment and nowhere else in the repository.

Why this bites harder than the bold leg

The two size slots usually hold different values in a real Hebrew document, because
Word keeps two independent size boxes in its font dialog and the Hebrew one is the one the
author sets. The heading style I quoted in #3959 has precisely that shape:

<w:rPr>
  <w:rFonts w:ascii="FrankRuehl DP" w:hAnsi="FrankRuehl DP" w:cs="FrankRuehl DP"/>
  <w:bCs/>
  <w:szCs w:val="28"/>          <!-- no w:sz at all -->
</w:rPr>

heading 2 there carries a CS size and no Latin size. With the Latin track as the only
source, the heading takes its size from whatever the cascade hands the Latin slot —
docDefaults — and comes out at body size. Bold is the second thing you notice about that
document; the size is the first.

The font slots diverge just as often, in the other direction: Word's Hebrew UI writes the
Hebrew face into w:cs and frequently leaves an unrelated Latin face in
w:ascii/w:hAnsi. The Hebrew glyphs are then shaped in a font that was never chosen for
them — and where that face has no Hebrew coverage, in whatever the browser substitutes.

This is not paint-only. run-attrs feeds the size that layout measures, so line breaking
follows the wrong value too.

Steps to reproduce

cs-stack.docx from this gist — synthetic, generated from scratch, no dependencies,
Node 18+:

https://gist.github.com/Nathaniel-260/a95fb34889cf3077726f22c3d1109952

node superdoc-run-property-repro.mjs . --case cs-stack

It puts 8pt Courier New in the Latin slots against 28pt Arial in the CS slots, so which
slot won is visible at a glance rather than something you have to measure. Six paragraphs,
each captioned in Latin with what Word does:

# run properties Word with the Latin track as the only source
1 sz=16 szCs=56, rFonts ascii="Courier New" cs="Arial", w:rtl Arial, 28pt Courier New, 8pt
2 sz=16 szCs=56, w:rtl, no rFonts 28pt 8pt
3 rFonts ascii="Courier New" cs="Arial", one size in both slots, w:rtl Arial Courier New
4 same as 1, selected by w:cs instead of w:rtl (§17.3.2.7) Arial, 28pt Courier New, 8pt
5 same as 1, carried by a paragraph style rather than direct formatting Arial, 28pt Courier New, 8pt
6 control — Latin run, same sz/szCs pair, neither w:rtl nor w:cs Courier New, 8pt Courier New, 8pt

Row 6 is the regression guard. Absence of both signals is not false: the stack then
follows the Unicode script of the run text, which this layer never sees, so a fix must
leave that line exactly as it is — the same reasoning usesComplexScriptToggles spells out
in #3958.

From Word instead of the gist: type a Hebrew line, set the complex-script size box in
Font to something far from the Latin size box, save, open.

SuperDoc version

2.13.0 (@superdoc/docx-engine 0.12.0). Also main @ 9ae9f03 (2.14.0-next.2).

Browser

Chrome

Additional context

Where this sits against the open work: #3959 / PR #3958 fix the bold and italic legs of the
same stack, and the usesComplexScriptToggles(props) helper that PR adds is the natural
place to hang these two — props.fontSizeCs ?? props.fontSize for the size, and a CS-first
slot order for the font, each keeping the Latin value as fallback for the reason documented
there (Word decides the stack per character, and plenty of documents carry only the Latin
variant on an RTL run).

Happy to send that as a PR — on top of #3958, or standalone if you would rather land the
two changes separately. Just say which.

Apologies for the number of issues from me lately. I am building a Hebrew editor on
SuperDoc and this stack is the floor of it, so I keep landing on it. I am a student on an
old two-core laptop and each round of verification takes a while, so if you tell me which
of these is most useful to you I will do that one first.

Activity

  1. ghost added
    status: triage
    on Sep 9, 2026
  2. ghost added
    status: investigating
    on Sep 10, 2026
  3. ghost removed
    status: triage
    on Sep 10, 2026
  4. caio-pizzol commented on Sep 10, 2026

    @caio-pizzol
    Contributor

    @Nathaniel-260, we confirmed this using your synthetic document on current main and SuperDoc 2.13.0. Both direct formatting and paragraph styles use the Latin font and size where the complex-script values should apply; the Latin control renders correctly. We’ve added your reproduction to our existing font/size issue, which is separate from the bold/italic work in #3958. Thanks for the clear test cases—they made both failures easy to isolate.

  5. self-assigned this
    on Sep 11, 2026
  6. ghost added
    status: queued
    on Sep 11, 2026
  7. ghost removed
    status: investigating
    on Sep 11, 2026
  8. added theissue type on Sep 11, 2026
  9. Nathaniel-260 commented on Nov 7, 2026

    @Nathaniel-260
    ContributorAuthor

    Thank you again to the amazing team, and to @caio-pizzol for confirming this so quickly. A follow-up with a second real-world shape of the bug, an updated pointer into the code, and a ready patch.

    A second shape: the Latin slot is a theme font

    A user of our Hebrew editor reported this week, on 2.29.0, that a Word file opens in the wrong font. The document's Normal style is the most common shape Hebrew Word writes when an author picks a face in the Hebrew box only:

    <w:style w:type="paragraph" w:default="1" w:styleId="Normal">
      <w:rPr>
        <w:rFonts w:asciiTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi" w:cs="<a font the user installed>"/>
      </w:rPr>
    </w:style>

    In Word's font dialog the Latin box reads "+Body" and the Hebrew box reads the installed font, and Word renders every Hebrew run (w:rtl) in that installed font. The user found the workaround on their own: setting the Latin box to the same face makes it render correctly. That is the original report from a different angle. In resolveFontFamily, an inherited asciiTheme resolves at the theme step, so cs is never reached, even though the Latin value here was never chosen by anyone.

    Where it sits on current main (2457e27)

    packages/layout-engine/style-engine/src/normalize/run-attrs.ts:

    • Size: line 86 is still resolveFontSize(props.fontSize). fontSizeCs is still read nowhere.
    • Font: resolveFontFamily (line 145) still walks the whole Latin track (ascii at line 167, then the theme) before cs/cstheme (line 182).
    • The path to the fix is already in the tree. 2457e27 added fontScript?: 'eastAsia' (line 26) together with an early branch at line 152 that reads the East Asian slot before the Latin track. The complex-script stack is the same shape with a simpler selector. Annex I selects it per run, from w:rtl/w:cs, so it needs no text segmentation.

    Why this should live in the normalizer, not downstream

    We tried a downstream workaround first: on open, rewrite styles.xml so the Latin slot carries the complex-script values, and restore the original on save. The project maintainer reviewed it and rejected it, and the reasons are inherent to working outside the engine:

    • Latin text in the same style now paints in the Hebrew face.
    • A value the user re-selects explicitly cannot be told apart from an untouched injected one.
    • Direct run formatting cannot be rewritten without changing the file.

    Only the normalizer knows which stack a run is on, so this is the one place where the fix has no side effects.

    Proposed fix

    Additive. One file plus tests, against 2457e27:

    bun test in style-engine: the whole package is green, including 6 new cases. With the predicate forced to false, 4 of the new cases fail. The other 2 are the control and the fallback, which must pass either way. vp fmt --check is clean.

    Patch: run-attrs.ts + normalize.test.ts
    diff --git a/packages/layout-engine/style-engine/src/normalize/normalize.test.ts b/packages/layout-engine/style-engine/src/normalize/normalize.test.ts
    index 405d81e..643e7af 100644
    --- a/packages/layout-engine/style-engine/src/normalize/normalize.test.ts
    +++ b/packages/layout-engine/style-engine/src/normalize/normalize.test.ts
    @@ -261,6 +261,55 @@ describe('East Asian font slot selection', () => {
       });
     });
     
    +describe('complex-script font and size (w:rtl / w:cs)', () => {
    +  const sizeOf = (halfPoints: number) => normalizeRunAttrsFromOoxml({ fontSize: halfPoints }).fontSize;
    +  const split = {
    +    fontFamily: { ascii: 'Courier New', hAnsi: 'Courier New', cs: 'Arial' },
    +    fontSize: 16,
    +    fontSizeCs: 56,
    +  };
    +
    +  it('reads rFonts/@cs and szCs on a run that declares w:rtl', () => {
    +    expect(normalizeRunAttrsFromOoxml({ ...split, rtl: true })).toMatchObject({
    +      fontFamily: 'Arial, sans-serif',
    +      fontSize: sizeOf(56),
    +    });
    +  });
    +
    +  it('reads the same members on a run that declares w:cs', () => {
    +    expect(normalizeRunAttrsFromOoxml({ ...split, cs: true })).toMatchObject({
    +      fontFamily: 'Arial, sans-serif',
    +      fontSize: sizeOf(56),
    +    });
    +  });
    +
    +  it('leaves a run that declares neither on the Latin members', () => {
    +    expect(normalizeRunAttrsFromOoxml(split)).toMatchObject({
    +      fontFamily: 'Courier New, monospace',
    +      fontSize: sizeOf(16),
    +    });
    +  });
    +
    +  it('prefers a named cs face over an inherited Latin theme font', () => {
    +    const themeFontScheme = { minor: { latin: 'Calibri', cs: 'Arial' }, major: { latin: 'Cambria' } };
    +    const props = { fontFamily: { asciiTheme: 'minorHAnsi', hAnsiTheme: 'minorHAnsi', cs: 'Tahoma' }, rtl: true };
    +    expect(normalizeRunAttrsFromOoxml(props, { themeFontScheme }).fontFamily).toBe('Tahoma, sans-serif');
    +  });
    +
    +  it('resolves cstheme against the theme cs face', () => {
    +    const themeFontScheme = { minor: { latin: 'Calibri', cs: 'Arial' }, major: { latin: 'Cambria' } };
    +    const props = { fontFamily: { asciiTheme: 'minorHAnsi', cstheme: 'minorBidi' }, rtl: true };
    +    expect(normalizeRunAttrsFromOoxml(props, { themeFontScheme }).fontFamily).toBe('Arial, sans-serif');
    +  });
    +
    +  it('falls back to the Latin members when the complex-script ones are absent', () => {
    +    expect(normalizeRunAttrsFromOoxml({ fontFamily: { ascii: 'Arial' }, fontSize: 24, rtl: true })).toMatchObject({
    +      fontFamily: 'Arial, sans-serif',
    +      fontSize: sizeOf(24),
    +    });
    +  });
    +});
    +
     describe('font-script context across text leaves', () => {
       it('keeps mark-only leaves with their preceding font track and preserves empty leaves', () => {
         expect(splitEastAsianFontSegments('\u{E0100}', 'eastAsia')).toEqual([
    diff --git a/packages/layout-engine/style-engine/src/normalize/run-attrs.ts b/packages/layout-engine/style-engine/src/normalize/run-attrs.ts
    index dd5e9e4..09dea7e 100644
    --- a/packages/layout-engine/style-engine/src/normalize/run-attrs.ts
    +++ b/packages/layout-engine/style-engine/src/normalize/run-attrs.ts
    @@ -77,13 +77,16 @@ export function normalizeRunAttrsFromOoxml(
       }
       if (props.smallCaps != null) out.smallCaps = props.smallCaps === true;
     
    -  const fontFamily = resolveFontFamily(props, context.themeFontScheme, context.fontScript);
    +  const complexScript = usesComplexScriptStack(props);
    +  const fontFamily = resolveFontFamily(props, context.themeFontScheme, context.fontScript, complexScript);
       const fontKey = fontFamily?.trim().replace(/^["']|["']$/g, '');
       const metadata = fontKey ? (context.fontTable?.[fontKey] ?? context.fontTable?.[fontKey.toLowerCase()]) : undefined;
       const cssFontFamily = toCssFontFamily(fontFamily, { wordFamily: metadata?.family, pitch: metadata?.pitch });
       if (cssFontFamily) out.fontFamily = cssFontFamily;
     
    -  const fontSize = resolveFontSize(props.fontSize);
    +  // The Latin size stays as the fallback: plenty of documents carry only w:sz
    +  // on an RTL run, and Word then sizes it from the same inherited value.
    +  const fontSize = resolveFontSize(complexScript ? (props.fontSizeCs ?? props.fontSize) : props.fontSize);
       if (fontSize != null) out.fontSize = fontSize;
     
       const color = resolveColor(props, context.themeColors);
    @@ -146,6 +149,7 @@ function resolveFontFamily(
       props: RunProperties,
       fontScheme?: WordThemeFontScheme,
       fontScript?: 'eastAsia',
    +  complexScript = false,
     ): string | undefined {
       const ff = props.fontFamily;
       if (!ff) return undefined;
    @@ -156,6 +160,16 @@ function resolveFontFamily(
           if (family) return family;
         }
       }
    +  // A run on the complex-script stack reads w:rFonts/@cs (or @cstheme) first.
    +  // Without this, any Latin value — including an inherited asciiTheme such as
    +  // the +Body font — wins over the Hebrew/Arabic face the author chose.
    +  if (complexScript) {
    +    if (ff.cs) return ff.cs;
    +    if (fontScheme && ff.cstheme) {
    +      const family = themeFontFromScheme(ff.cstheme, fontScheme, 'cs');
    +      if (family) return family;
    +    }
    +  }
       // Per ECMA-376 §17.3.2.26, each script slot is independent; for the
       // ascii/hAnsi track the resolution order is:
       //   ascii → hAnsi → asciiTheme(major/minor) → hAnsiTheme(major/minor)
    @@ -187,6 +201,16 @@ function resolveFontFamily(
       return undefined;
     }
     
    +/**
    + * ECMA-376 Annex I, §17.3.2.7 and §17.3.2.30: a run that declares w:cs or w:rtl
    + * takes its font and size from the complex-script members (rFonts/@cs, szCs).
    + * Absence of both is not "Latin" — the stack then follows the Unicode script of
    + * each character, which this layer does not see — so such runs are left as is.
    + */
    +function usesComplexScriptStack(props: RunProperties): boolean {
    +  return props.rtl === true || props.cs === true;
    +}
    +
     function themeFontFromScheme(
       themeName: string,
       scheme: WordThemeFontScheme,

    One follow-up this patch does not cover

    cstheme="minorBidi" resolves through themeFontFromScheme(…, 'cs'), which reads only <a:cs> (parse-theme.ts:87). The stock Office theme leaves <a:cs typeface=""/> empty and puts the Hebrew and Arabic faces in <a:font script="Hebr" …/> / <a:font script="Arab" …/>. Word picks among those by w:themeFontLang/@w:bidi in settings.xml. So for documents whose Hebrew face comes from the theme, the patch falls back to the Latin track, exactly as today. That needs the script fonts parsed into the scheme. I'm happy to write it up separately if it is useful.

    Glad to adjust the patch however suits you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status: queued

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions