From ba25ff3684d54f1ca9f7f2febacc6c69f6543f92 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sat, 12 Oct 2019 17:55:50 +0000 Subject: [PATCH] #63818 - Allow multiple charsets for same font typeface git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1868358 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hpsf/ClassIDPredefined.java | 4 +- .../poi/hslf/record/FontCollection.java | 28 +++++++++-- .../poi/hslf/usermodel/HSLFSlideMaster.java | 50 ++++++++++++------- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/src/java/org/apache/poi/hpsf/ClassIDPredefined.java b/src/java/org/apache/poi/hpsf/ClassIDPredefined.java index bf85c24589..5e81639d41 100644 --- a/src/java/org/apache/poi/hpsf/ClassIDPredefined.java +++ b/src/java/org/apache/poi/hpsf/ClassIDPredefined.java @@ -84,7 +84,9 @@ public enum ClassIDPredefined { /** AcroExch.Document */ PDF ("{B801CA65-A1FC-11D0-85AD-444553540000}", ".pdf", "application/pdf"), /** Plain Text Persistent Handler **/ - TXT_ONLY ("{5e941d80-bf96-11cd-b579-08002b30bfeb}", ".txt", "text/plain") + TXT_ONLY ("{5e941d80-bf96-11cd-b579-08002b30bfeb}", ".txt", "text/plain"), + /** Microsoft Paint **/ + PAINT ("{0003000A-0000-0000-C000-000000000046}", null, null) ; private static final Map LOOKUP = new HashMap<>(); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java b/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java index db2ffcf6d9..c712753800 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java @@ -24,8 +24,10 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.function.Predicate; import java.util.function.Supplier; +import org.apache.poi.common.usermodel.fonts.FontCharset; import org.apache.poi.common.usermodel.fonts.FontHeader; import org.apache.poi.common.usermodel.fonts.FontInfo; import org.apache.poi.common.usermodel.fonts.FontPitch; @@ -41,7 +43,7 @@ import org.apache.poi.util.POILogger; @SuppressWarnings("WeakerAccess") public final class FontCollection extends RecordContainer { - private final Map fonts = new LinkedHashMap<>(); + private final Map fonts = new LinkedHashMap<>(); private byte[] _header; /* package */ FontCollection(byte[] source, int start, int len) { @@ -53,7 +55,7 @@ public final class FontCollection extends RecordContainer { for (Record r : _children){ if(r instanceof FontEntityAtom) { HSLFFontInfo fi = new HSLFFontInfo((FontEntityAtom) r); - fonts.put(fi.getTypeface(), fi); + fonts.put(fi.getIndex(), fi); } else if (r instanceof FontEmbeddedData) { FontEmbeddedData fed = (FontEmbeddedData)r; FontHeader fontHeader = fed.getFontHeader(); @@ -93,14 +95,14 @@ public final class FontCollection extends RecordContainer { * @return the register HSLFFontInfo object */ public HSLFFontInfo addFont(FontInfo fontInfo) { - HSLFFontInfo fi = getFontInfo(fontInfo.getTypeface()); + HSLFFontInfo fi = getFontInfo(fontInfo.getTypeface(), fontInfo.getCharset()); if (fi != null) { return fi; } fi = new HSLFFontInfo(fontInfo); fi.setIndex(fonts.size()); - fonts.put(fi.getTypeface(), fi); + fonts.put(fi.getIndex(), fi); FontEntityAtom fnt = fi.createRecord(); @@ -172,7 +174,23 @@ public final class FontCollection extends RecordContainer { * @return the HSLFFontInfo for the given name or {@code null} if not found */ public HSLFFontInfo getFontInfo(String typeface) { - return fonts.get(typeface); + return getFontInfo(typeface, null); + } + + /** + * Lookup a FontInfo object by its typeface + * + * @param typeface the full font name + * @param charset the charset + * + * @return the HSLFFontInfo for the given name or {@code null} if not found + */ + public HSLFFontInfo getFontInfo(String typeface, FontCharset charset) { + return fonts.values().stream().filter(findFont(typeface, charset)).findFirst().orElse(null); + } + + private static Predicate findFont(String typeface, FontCharset charset) { + return (fi) -> typeface.equals(fi.getTypeface()) && (charset == null || charset.equals(fi.getCharset())); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideMaster.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideMaster.java index f33f641fcc..2f258e6eb6 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideMaster.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideMaster.java @@ -89,38 +89,50 @@ public final class HSLFSlideMaster extends HSLFMasterSheet { */ @Override public TextPropCollection getPropCollection(final int txtype, final int level, final String name, final boolean isCharacter) { - if (txtype < _txmaster.length) { - final TxMasterStyleAtom t = _txmaster[txtype]; - final List styles = isCharacter ? t.getCharacterStyles() : t.getParagraphStyles(); - // TODO: what is the reaction for readOnly=false and styles.isEmpty()? - final int minLevel = Math.min(level, styles.size()-1); - if ("*".equals(name)) { - return styles.get(minLevel); - } - - for (int i=minLevel; i >= 0; i--) { - final TextPropCollection col = styles.get(i); - final TextProp tp = col.findByName(name); - if (tp != null) { - return col; - } - } + TextPropCollection tpc = getPropHelper(txtype, level, name, isCharacter); + if (tpc != null) { + return tpc; } - switch (TextPlaceholder.fromNativeId(txtype)) { + TextPlaceholder tp = TextPlaceholder.fromNativeId(txtype); + switch (tp == null ? TextPlaceholder.BODY : tp) { case BODY: case CENTER_BODY: case HALF_BODY: case QUARTER_BODY: - return getPropCollection(TextPlaceholder.BODY.nativeId, level, name, isCharacter); + return getPropHelper(TextPlaceholder.BODY.nativeId, level, name, isCharacter); case TITLE: case CENTER_TITLE: - return getPropCollection(TextPlaceholder.TITLE.nativeId, level, name, isCharacter); + return getPropHelper(TextPlaceholder.TITLE.nativeId, level, name, isCharacter); default: return null; } } + private TextPropCollection getPropHelper(final int txtype, final int level, final String name, final boolean isCharacter) { + if (txtype >= _txmaster.length) { + return null; + } + final TxMasterStyleAtom t = _txmaster[txtype]; + final List styles = isCharacter ? t.getCharacterStyles() : t.getParagraphStyles(); + // TODO: what is the reaction for readOnly=false and styles.isEmpty()? + final int minLevel = Math.min(level, styles.size()-1); + if ("*".equals(name)) { + return styles.get(minLevel); + } + + for (int i=minLevel; i >= 0; i--) { + final TextPropCollection col = styles.get(i); + final TextProp tp = col.findByName(name); + if (tp != null) { + return col; + } + } + + return null; + } + + /** * Assign SlideShow for this slide master. */ -- 2.39.5