Repository navigation
Description
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.fontSizeCsis 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 thecsslot 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 onefontFamilyand 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
`engine-gaps`: שני הפערים שנשארו בלשונית „מתקדם” דווחו למעלה
caio-pizzol commentedon Sep 10, 2026
@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.
Nathaniel-260 commentedon Nov 7, 2026
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
Normalstyle is the most common shape Hebrew Word writes when an author picks a face in the Hebrew box only: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. InresolveFontFamily, an inheritedasciiThemeresolves at the theme step, socsis 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:resolveFontSize(props.fontSize).fontSizeCsis still read nowhere.resolveFontFamily(line 145) still walks the whole Latin track (asciiat line 167, then the theme) beforecs/cstheme(line 182).2457e27addedfontScript?: '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, fromw: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.xmlso 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: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:usesComplexScriptStack(props)isprops.rtl === true || props.cs === true(§17.3.2.7, §17.3.2.30).fontSizeCs ?? fontSize.cs→csthemeslots come first on that stack, with the Latin track kept as the fallback.usesComplexScriptTogglesfrom fix(style-engine): resolve bold and italic from the complex-script toggles #3958 if that lands first. The two can share one predicate.bun testinstyle-engine: the whole package is green, including 6 new cases. With the predicate forced tofalse, 4 of the new cases fail. The other 2 are the control and the fallback, which must pass either way.vp fmt --checkis clean.Patch:
run-attrs.ts+normalize.test.tsOne follow-up this patch does not cover
cstheme="minorBidi"resolves throughthemeFontFromScheme(…, '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 byw:themeFontLang/@w:bidiinsettings.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.