XWPF Themes: allow public access of theme and add helpers for theme fonts (#986)

* allow public access of theme and add helpers for theme fonts

* nit:naming

* add javadoc and tests

* Fix formatting of getMajorFontForScript method

* Refactor font retrieval methods for null checks

* Update XWPFTheme.java

---------

Co-authored-by: PJ Fanning <pjfanning@users.noreply.github.com>
This commit is contained in:
Etienne Gautier 2026-01-09 23:09:12 +11:00 committed by GitHub
parent c94e795f35
commit aead782eb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 77 additions and 4 deletions

View File

@ -27,7 +27,9 @@ import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBaseStyles; import org.openxmlformats.schemas.drawingml.x2006.main.CTBaseStyles;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColor; import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme; import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme;
import org.openxmlformats.schemas.drawingml.x2006.main.CTFontCollection;
import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeStyleSheet; import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeStyleSheet;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSupplementalFont;
import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument; import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
@ -67,6 +69,14 @@ public class XWPFTheme extends POIXMLDocumentPart {
_theme = theme.getXmlObject(); _theme = theme.getXmlObject();
} }
/**
* @return the underlying CTOfficeStyleSheet instance.
* @since 6.0.0
*/
public CTOfficeStyleSheet getCTOfficeStyleSheet() {
return _theme;
}
/** /**
* *
* @return name of this theme, e.g. "Office Theme" * @return name of this theme, e.g. "Office Theme"
@ -137,8 +147,10 @@ public class XWPFTheme extends POIXMLDocumentPart {
* *
*/ */
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public String getMajorFont(){ public String getMajorFont() {
return _theme.getThemeElements().getFontScheme().getMajorFont().getLatin().getTypeface(); CTFontCollection majorFonts = getMajorFonts();
return majorFonts == null || majorFonts.getLatin() == null ?
null : majorFonts.getLatin().getTypeface();
} }
/** /**
@ -147,8 +159,62 @@ public class XWPFTheme extends POIXMLDocumentPart {
* *
*/ */
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public String getMinorFont(){ public String getMinorFont() {
return _theme.getThemeElements().getFontScheme().getMinorFont().getLatin().getTypeface(); CTFontCollection minorFonts = getMinorFonts();
return minorFonts == null || minorFonts.getLatin() == null ?
null : minorFonts.getLatin().getTypeface();
}
/**
* @param script a 4-letter script code, e.g. "Latn", "Jpan"
* @return typeface of the major font for the given script
* @since 6.0.0
*/
public String getMajorFontForScript(String script) {
CTFontCollection majorFonts = getMajorFonts();
return majorFonts == null ? null : getFontTypeface(majorFonts, script);
}
/**
* @param script a 4-letter script code, e.g. "Latn", "Jpan"
* @return typeface of the minor font for the given script
* @since 6.0.0
*/
public String getMinorFontForScript(String script) {
CTFontCollection minorFonts = getMinorFonts();
return minorFonts == null ? null : getFontTypeface(minorFonts, script);
}
private CTFontCollection getMajorFonts() {
if (_theme == null
|| _theme.getThemeElements() == null
|| _theme.getThemeElements().getFontScheme() == null
|| _theme.getThemeElements().getFontScheme().getMajorFont() == null) {
return null;
}
return _theme.getThemeElements().getFontScheme().getMajorFont();
}
private CTFontCollection getMinorFonts() {
if (_theme == null
|| _theme.getThemeElements() == null
|| _theme.getThemeElements().getFontScheme() == null
|| _theme.getThemeElements().getFontScheme().getMinorFont() == null) {
return null;
}
return _theme.getThemeElements().getFontScheme().getMinorFont();
}
private static String getFontTypeface(CTFontCollection fontCollection, String script) {
CTSupplementalFont[] fonts = fontCollection.getFontArray();
if (fonts != null) {
for (CTSupplementalFont font : fonts) {
if (font.getScript() != null && font.getScript().equals(script)) {
return font.getTypeface();
}
}
}
return null;
} }
/** /**

View File

@ -21,11 +21,13 @@ import org.apache.poi.xslf.usermodel.XSLFColor;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColor; import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTOfficeStyleSheet;
import java.awt.*; import java.awt.*;
import java.io.IOException; import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public final class TestXWPFTheme { public final class TestXWPFTheme {
@ -36,6 +38,11 @@ public final class TestXWPFTheme {
assertEquals("Office Theme", theme.getName()); assertEquals("Office Theme", theme.getName());
assertEquals("Cambria", theme.getMajorFont()); assertEquals("Cambria", theme.getMajorFont());
assertEquals("Calibri", theme.getMinorFont()); assertEquals("Calibri", theme.getMinorFont());
assertEquals("Angsana New", theme.getMajorFontForScript("Thai"));
assertEquals("Cordia New", theme.getMinorFontForScript("Thai"));
CTOfficeStyleSheet styleSheet = theme.getCTOfficeStyleSheet();
assertNotNull(styleSheet);
assertEquals("Office", styleSheet.getThemeElements().getFontScheme().getName());
CTColor accent1 = theme.getCTColor("accent1"); CTColor accent1 = theme.getCTColor("accent1");
XSLFColor color = new XSLFColor(accent1, null, null, null); XSLFColor color = new XSLFColor(accent1, null, null, null);
assertEquals(new Color(79, 129, 189), color.getColor()); assertEquals(new Color(79, 129, 189), color.getColor());