/** * Combine pitch and family to native id * * @see <a href="https://msdn.microsoft.com/en-us/library/dd145037.aspx">LOGFONT structure</a> * * @param pitch The pitch-value, cannot be null * @param family The family-value, cannot be null * * @return The resulting combined byte-value with pitch and family encoded into one byte */ public static byte getNativeId(FontPitch pitch, FontFamily family) { return (byte)(pitch.getNativeId() | (family.getFlag() << 4)); }
/** * Get FontFamily from combined native id * * @param pitchAndFamily The PitchFamily to decode. * * @return The resulting FontFamily */ public static FontFamily valueOfPitchFamily(byte pitchAndFamily) { return valueOf(pitchAndFamily >>> 4); } }
public static FontPitch valueOf(int flag) { for (FontPitch fp : values()) { if (fp.nativeId == flag) return fp; } return null; }
void copyFrom(FontInfo fontInfo) { CTTextFont tf = getXmlObject(true); if (tf == null) { return; } setTypeface(fontInfo.getTypeface()); setCharset(fontInfo.getCharset()); FontPitch pitch = fontInfo.getPitch(); FontFamily family = fontInfo.getFamily(); if (pitch == null && family == null) { if (tf.isSetPitchFamily()) { tf.unsetPitchFamily(); } } else { setPitch(pitch); setFamily(family); } }
@Override public byte getPitchAndFamily(){ FontGroup fg = FontGroup.getFontGroupFirst(getRawText()); XSLFFontInfo fontInfo = new XSLFFontInfo(fg); FontPitch pitch = fontInfo.getPitch(); if (pitch == null) { pitch = FontPitch.VARIABLE; } FontFamily family = fontInfo.getFamily(); if (family == null) { family = FontFamily.FF_SWISS; } return FontPitch.getNativeId(pitch, family); }
@Override public void setPitch(FontPitch pitch) { CTTextFont tf = getXmlObject(true); if (tf == null || (pitch == null && !tf.isSetPitchFamily())) { return; } FontFamily family = (tf.isSetPitchFamily()) ? FontFamily.valueOfPitchFamily(tf.getPitchFamily()) : FontFamily.FF_SWISS; byte pitchFamily = FontPitch.getNativeId(pitch != null ? pitch : FontPitch.VARIABLE, family); tf.setPitchFamily(pitchFamily); }
@Override public void setFamily(FontFamily family) { CTTextFont tf = getXmlObject(true); if (tf == null || (family == null && !tf.isSetPitchFamily())) { return; } FontPitch pitch = (tf.isSetPitchFamily()) ? FontPitch.valueOfPitchFamily(tf.getPitchFamily()) : FontPitch.VARIABLE; byte pitchFamily = FontPitch.getNativeId(pitch, family != null ? family : FontFamily.FF_SWISS); tf.setPitchFamily(pitchFamily); }
@Override public Font createAWTFont(Graphics2D graphics, FontInfo fontInfo, double fontSize, boolean bold, boolean italic) { int style = (bold ? Font.BOLD : 0) | (italic ? Font.ITALIC : 0); Font font = new Font(fontInfo.getTypeface(), style, 12); if (Font.DIALOG.equals(font.getFamily())) { // SansSerif is a better choice than Dialog font = new Font(Font.SANS_SERIF, style, 12); } return font.deriveFont((float)fontSize); }
private XSLFFontInfo(FontGroup fontGroup) { this.fontGroup = (fontGroup != null) ? fontGroup : FontGroup.getFontGroupFirst(getRawText()); }
public static FontFamily valueOf(int nativeId) { for (FontFamily ff : values()) { if (ff.nativeId == nativeId) { return ff; } } return null; }
/** * Get FontPitch from native id * * @param pitchAndFamily The combined byte value for pitch and family * * @return The resulting FontPitch enumeration value */ public static FontPitch valueOfPitchFamily(byte pitchAndFamily) { return valueOf(pitchAndFamily & 0x3); } }
public static FontGroup getFontGroupFirst(String runText) { return (runText == null || runText.isEmpty()) ? LATIN : lookup(runText.codePointAt(0)); }
@Override public FontFamily getFamily() { CTTextFont tf = getXmlObject(false); return (tf != null && tf.isSetPitchFamily()) ? FontFamily.valueOfPitchFamily(tf.getPitchFamily()) : null; }
@Override public FontCharset getCharset() { CTTextFont tf = getXmlObject(false); return (tf != null && tf.isSetCharset()) ? FontCharset.valueOf(tf.getCharset()&0xFF) : null; }
@Override public FontPitch getPitch() { CTTextFont tf = getXmlObject(false); return (tf != null && tf.isSetPitchFamily()) ? FontPitch.valueOfPitchFamily(tf.getPitchFamily()) : null; }
@Override public void setCharset(FontCharset charset) { CTTextFont tf = getXmlObject(true); if (tf == null) { return; } if (charset != null) { tf.setCharset((byte)charset.getNativeId()); } else { if (tf.isSetCharset()) { tf.unsetCharset(); } } }
public String mapFontCharset(Graphics2D graphics, FontInfo fontInfo, String text) { // TODO: find a real charset mapping solution instead of hard coding for Wingdings String attStr = text; if (fontInfo != null && "Wingdings".equalsIgnoreCase(fontInfo.getTypeface())) { // wingdings doesn't contain high-surrogates, so chars are ok boolean changed = false; char chrs[] = attStr.toCharArray(); for (int i=0; i<chrs.length; i++) { // only change valid chars if ((0x20 <= chrs[i] && chrs[i] <= 0x7f) || (0xa0 <= chrs[i] && chrs[i] <= 0xff)) { chrs[i] |= 0xf000; changed = true; } } if (changed) { attStr = new String(chrs); } } return attStr; }
@Override public void setFontFamily(String typeface) { FontGroup fg = FontGroup.getFontGroupFirst(getRawText()); new XSLFFontInfo(fg).setTypeface(typeface); }
private FontInfo getFontWithFallback(Graphics2D graphics, DrawableHint hint, FontInfo fontInfo) { @SuppressWarnings("unchecked") Map<String,String> fontMap = (Map<String,String>)graphics.getRenderingHint(hint); if (fontMap == null) { return fontInfo; } String f = (fontInfo != null) ? fontInfo.getTypeface() : null; String mappedTypeface = null; if (fontMap.containsKey(f)) { mappedTypeface = fontMap.get(f); } else if (fontMap.containsKey("*")) { mappedTypeface = fontMap.get("*"); } return (mappedTypeface != null) ? new DrawFontInfo(mappedTypeface) : fontInfo; } }
@Override public String getFontFamily() { FontGroup fg = FontGroup.getFontGroupFirst(getRawText()); return new XSLFFontInfo(fg).getTypeface(); }