Skip to content

Conversation

@Etienne-Gautier
Copy link

@Etienne-Gautier Etienne-Gautier commented Dec 31, 2025

Context

I'd like to better support DOCX theme fonts, but currently they are inaccessible.

For anyone interested, a run can define fonts either

  • directly w:ascii="Times New Roman"
  • indirectly via a theme for example w:asciiTheme="majorBidi"

Currently the XWPFTheme doesn't expose major/minor fonts to resolve theme fonts, so I am fixing that in this PR.

Note: I spent a bit of time figuring out how the theme fonts are resolved and although it isn't implemented in this PR I thought I'd write it down somewhere. So for anyone interested, a run might look like this:

    <w:r>
      <w:rPr>
        <w:rFonts w:asciiTheme="majorBidi" w:hAnsiTheme="majorBidi" w:cstheme="majorBidi"/>
      </w:rPr>
      <w:t>This text uses Times New Roman via themes</w:t>
    </w:r>

With settings containing

  <w:themeFontLang w:val="en-AU" w:bidi="ar-SA"/>

And a Theme like this:

    <a:fontScheme name="Office">
      <a:majorFont>
        <a:latin typeface="Aptos Display" panose="02110004020202020204"/>
        <a:ea typeface=""/>
        <a:cs typeface="Segoe UI"/>
        <a:font script="Jpan" typeface="游ゴシック Light"/>
        <a:font script="Arab" typeface="Times New Roman"/>
    </a:majorFont>
    <a:minorFont>
        <a:latin typeface="Aptos" panose="02110004020202020204"/>
        <a:ea typeface=""/>
        <a:cs typeface=""/>
        <a:font script="Jpan" typeface="游明朝"/>
        <a:font script="Arab" typeface="Arial"/>
    </a:minorFont>

This theme font can be interpreted by combining info from the document settings (themeFontLang) and themes. There is an explanation of that in the spec at 17.15.1.88 themeFontLang (Theme Font Languages). However that explanation doesn't really tell you how to map the locale code (for example ar-SA) from themeFontLang to a script (for example Arab).

I found that:

  • POI's LocaleID.lookupByLanguageTag(localeName) can read the locale.
  • Then the logic from Libreoffice here can easily be ported from java to map a localeID.getLcid() to a script. We can then lookup the major/minor theme font with that script
_theme = theme.getXmlObject();
}

public CTOfficeStyleSheet getCTOfficeStyleSheet() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add javadoc with at least @since 6.0.0 on new public methods?

return _theme;
}

public String getMajorFontForScript(String script){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any chance of adding unit test coverage?

@Etienne-Gautier
Copy link
Author

@pjfanning I've added some javadoc and basic tests. Thanks for the quick review

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

Labels

None yet

2 participants