[bug-62038] handle null font size in XSLFTextRun copy

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1822253 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-01-26 07:51:57 +00:00
parent a5d2574538
commit 7e434aa155

View File

@ -60,44 +60,44 @@ public class XSLFTextRun implements TextRun {
private final XmlObject _r; private final XmlObject _r;
private final XSLFTextParagraph _p; private final XSLFTextParagraph _p;
protected XSLFTextRun(XmlObject r, XSLFTextParagraph p){ protected XSLFTextRun(XmlObject r, XSLFTextParagraph p) {
_r = r; _r = r;
_p = p; _p = p;
if (!(r instanceof CTRegularTextRun || r instanceof CTTextLineBreak || r instanceof CTTextField)) { if (!(r instanceof CTRegularTextRun || r instanceof CTTextLineBreak || r instanceof CTTextField)) {
throw new OpenXML4JRuntimeException("unsupported text run of type "+r.getClass()); throw new OpenXML4JRuntimeException("unsupported text run of type " + r.getClass());
} }
} }
XSLFTextParagraph getParentParagraph(){ XSLFTextParagraph getParentParagraph() {
return _p; return _p;
} }
@Override @Override
public String getRawText(){ public String getRawText() {
if (_r instanceof CTTextField) { if (_r instanceof CTTextField) {
return ((CTTextField)_r).getT(); return ((CTTextField) _r).getT();
} else if (_r instanceof CTTextLineBreak) { } else if (_r instanceof CTTextLineBreak) {
return "\n"; return "\n";
} }
return ((CTRegularTextRun)_r).getT(); return ((CTRegularTextRun) _r).getT();
} }
String getRenderableText(){ String getRenderableText() {
if (_r instanceof CTTextField) { if (_r instanceof CTTextField) {
CTTextField tf = (CTTextField)_r; CTTextField tf = (CTTextField) _r;
XSLFSheet sheet = _p.getParentShape().getSheet(); XSLFSheet sheet = _p.getParentShape().getSheet();
if ("slidenum".equals(tf.getType()) && sheet instanceof XSLFSlide) { if ("slidenum".equals(tf.getType()) && sheet instanceof XSLFSlide) {
return Integer.toString(((XSLFSlide)sheet).getSlideNumber()); return Integer.toString(((XSLFSlide) sheet).getSlideNumber());
} }
return tf.getT(); return tf.getT();
} else if (_r instanceof CTTextLineBreak) { } else if (_r instanceof CTTextLineBreak) {
return "\n"; return "\n";
} }
return getRenderableText(((CTRegularTextRun)_r).getT()); return getRenderableText(((CTRegularTextRun) _r).getT());
} }
String getRenderableText(String txt){ String getRenderableText(String txt) {
// TODO: finish support for tabs // TODO: finish support for tabs
txt.replace("\t", " "); txt.replace("\t", " ");
@ -113,11 +113,11 @@ public class XSLFTextRun implements TextRun {
} }
@Override @Override
public void setText(String text){ public void setText(String text) {
if (_r instanceof CTTextField) { if (_r instanceof CTTextField) {
((CTTextField)_r).setT(text); ((CTTextField) _r).setT(text);
} else if (!(_r instanceof CTTextLineBreak)) { } else if (!(_r instanceof CTTextLineBreak)) {
((CTRegularTextRun)_r).setT(text); ((CTRegularTextRun) _r).setT(text);
} }
} }
@ -128,7 +128,7 @@ public class XSLFTextRun implements TextRun {
* *
* @return the xmlbeans object * @return the xmlbeans object
*/ */
public XmlObject getXmlObject(){ public XmlObject getXmlObject() {
return _r; return _r;
} }
@ -142,7 +142,7 @@ public class XSLFTextRun implements TextRun {
if (!(color instanceof SolidPaint)) { if (!(color instanceof SolidPaint)) {
throw new IllegalArgumentException("Currently only SolidPaint is supported!"); throw new IllegalArgumentException("Currently only SolidPaint is supported!");
} }
SolidPaint sp = (SolidPaint)color; SolidPaint sp = (SolidPaint) color;
Color c = DrawPaint.applyColorTransform(sp.getSolidColor()); Color c = DrawPaint.applyColorTransform(sp.getSolidColor());
CTTextCharacterProperties rPr = getRPr(true); CTTextCharacterProperties rPr = getRPr(true);
@ -153,11 +153,11 @@ public class XSLFTextRun implements TextRun {
} }
@Override @Override
public PaintStyle getFontColor(){ public PaintStyle getFontColor() {
final boolean hasPlaceholder = getParentParagraph().getParentShape().getPlaceholder() != null; final boolean hasPlaceholder = getParentParagraph().getParentShape().getPlaceholder() != null;
CharacterPropertyFetcher<PaintStyle> fetcher = new CharacterPropertyFetcher<PaintStyle>(_p.getIndentLevel()){ CharacterPropertyFetcher<PaintStyle> fetcher = new CharacterPropertyFetcher<PaintStyle>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if (props == null) { if (props == null) {
return false; return false;
} }
@ -175,7 +175,7 @@ public class XSLFTextRun implements TextRun {
XSLFTheme theme = sheet.getTheme(); XSLFTheme theme = sheet.getTheme();
PaintStyle ps = XSLFShape.selectPaint(fp, phClr, pp, theme, hasPlaceholder); PaintStyle ps = XSLFShape.selectPaint(fp, phClr, pp, theme, hasPlaceholder);
if (ps != null) { if (ps != null) {
setValue(ps); setValue(ps);
return true; return true;
} }
@ -188,9 +188,9 @@ public class XSLFTextRun implements TextRun {
} }
@Override @Override
public void setFontSize(Double fontSize){ public void setFontSize(Double fontSize) {
CTTextCharacterProperties rPr = getRPr(true); CTTextCharacterProperties rPr = getRPr(true);
if(fontSize == null) { if (fontSize == null) {
if (rPr.isSetSz()) { if (rPr.isSetSz()) {
rPr.unsetSz(); rPr.unsetSz();
} }
@ -199,44 +199,43 @@ public class XSLFTextRun implements TextRun {
throw new IllegalArgumentException("Minimum font size is 1pt but was " + fontSize); throw new IllegalArgumentException("Minimum font size is 1pt but was " + fontSize);
} }
rPr.setSz((int)(100*fontSize)); rPr.setSz((int) (100 * fontSize));
} }
} }
@Override @Override
public Double getFontSize(){ public Double getFontSize() {
double scale = 1; double scale = 1;
CTTextNormalAutofit afit = getParentParagraph().getParentShape().getTextBodyPr().getNormAutofit(); CTTextNormalAutofit afit = getParentParagraph().getParentShape().getTextBodyPr().getNormAutofit();
if(afit != null) { if (afit != null) {
scale = (double)afit.getFontScale() / 100000; scale = (double) afit.getFontScale() / 100000;
} }
CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getIndentLevel()){ CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if (props != null && props.isSetSz()) { if (props != null && props.isSetSz()) {
setValue(props.getSz()*0.01); setValue(props.getSz() * 0.01);
return true; return true;
} }
return false; return false;
} }
}; };
fetchCharacterProperty(fetcher); fetchCharacterProperty(fetcher);
return fetcher.getValue() == null ? null : fetcher.getValue()*scale; return fetcher.getValue() == null ? null : fetcher.getValue() * scale;
} }
/** /**
*
* @return the spacing between characters within a text run, * @return the spacing between characters within a text run,
* If this attribute is omitted than a value of 0 or no adjustment is assumed. * If this attribute is omitted than a value of 0 or no adjustment is assumed.
*/ */
public double getCharacterSpacing(){ public double getCharacterSpacing() {
CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getIndentLevel()){ CharacterPropertyFetcher<Double> fetcher = new CharacterPropertyFetcher<Double>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if (props != null && props.isSetSpc()) { if (props != null && props.isSetSpc()) {
setValue(props.getSpc()*0.01); setValue(props.getSpc() * 0.01);
return true; return true;
} }
return false; return false;
@ -253,16 +252,16 @@ public class XSLFTextRun implements TextRun {
* negative values to condense. * negative values to condense.
* </p> * </p>
* *
* @param spc character spacing in points. * @param spc character spacing in points.
*/ */
public void setCharacterSpacing(double spc){ public void setCharacterSpacing(double spc) {
CTTextCharacterProperties rPr = getRPr(true); CTTextCharacterProperties rPr = getRPr(true);
if(spc == 0.0) { if (spc == 0.0) {
if(rPr.isSetSpc()) { if (rPr.isSetSpc()) {
rPr.unsetSpc(); rPr.unsetSpc();
} }
} else { } else {
rPr.setSpc((int)(100*spc)); rPr.setSpc((int) (100 * spc));
} }
} }
@ -300,7 +299,7 @@ public class XSLFTextRun implements TextRun {
} }
@Override @Override
public byte getPitchAndFamily(){ public byte getPitchAndFamily() {
FontGroup fg = FontGroup.getFontGroupFirst(getRawText()); FontGroup fg = FontGroup.getFontGroupFirst(getRawText());
XSLFFontInfo fontInfo = new XSLFFontInfo(fg); XSLFFontInfo fontInfo = new XSLFFontInfo(fg);
FontPitch pitch = fontInfo.getPitch(); FontPitch pitch = fontInfo.getPitch();
@ -321,10 +320,10 @@ public class XSLFTextRun implements TextRun {
@Override @Override
public boolean isStrikethrough() { public boolean isStrikethrough() {
CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){ CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if(props != null && props.isSetStrike()) { if (props != null && props.isSetStrike()) {
setValue(props.getStrike() != STTextStrikeType.NO_STRIKE); setValue(props.getStrike() != STTextStrikeType.NO_STRIKE);
return true; return true;
} }
@ -337,9 +336,9 @@ public class XSLFTextRun implements TextRun {
@Override @Override
public boolean isSuperscript() { public boolean isSuperscript() {
CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){ CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if (props != null && props.isSetBaseline()) { if (props != null && props.isSetBaseline()) {
setValue(props.getBaseline() > 0); setValue(props.getBaseline() > 0);
return true; return true;
@ -352,16 +351,16 @@ public class XSLFTextRun implements TextRun {
} }
/** /**
* Set the baseline for both the superscript and subscript fonts. * Set the baseline for both the superscript and subscript fonts.
* <p> * <p>
* The size is specified using a percentage. * The size is specified using a percentage.
* Positive values indicate superscript, negative values indicate subscript. * Positive values indicate superscript, negative values indicate subscript.
* </p> * </p>
* *
* @param baselineOffset * @param baselineOffset
*/ */
public void setBaselineOffset(double baselineOffset){ public void setBaselineOffset(double baselineOffset) {
getRPr(true).setBaseline((int) baselineOffset * 1000); getRPr(true).setBaseline((int) baselineOffset * 1000);
} }
/** /**
@ -370,7 +369,7 @@ public class XSLFTextRun implements TextRun {
* *
* @see #setBaselineOffset(double) * @see #setBaselineOffset(double)
*/ */
public void setSuperscript(boolean flag){ public void setSuperscript(boolean flag) {
setBaselineOffset(flag ? 30. : 0.); setBaselineOffset(flag ? 30. : 0.);
} }
@ -380,15 +379,15 @@ public class XSLFTextRun implements TextRun {
* *
* @see #setBaselineOffset(double) * @see #setBaselineOffset(double)
*/ */
public void setSubscript(boolean flag){ public void setSubscript(boolean flag) {
setBaselineOffset(flag ? -25.0 : 0.); setBaselineOffset(flag ? -25.0 : 0.);
} }
@Override @Override
public boolean isSubscript() { public boolean isSubscript() {
CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){ CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if (props != null && props.isSetBaseline()) { if (props != null && props.isSetBaseline()) {
setValue(props.getBaseline() < 0); setValue(props.getBaseline() < 0);
return true; return true;
@ -405,9 +404,9 @@ public class XSLFTextRun implements TextRun {
*/ */
@Override @Override
public TextCap getTextCap() { public TextCap getTextCap() {
CharacterPropertyFetcher<TextCap> fetcher = new CharacterPropertyFetcher<TextCap>(_p.getIndentLevel()){ CharacterPropertyFetcher<TextCap> fetcher = new CharacterPropertyFetcher<TextCap>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if (props != null && props.isSetCap()) { if (props != null && props.isSetCap()) {
int idx = props.getCap().intValue() - 1; int idx = props.getCap().intValue() - 1;
setValue(TextCap.values()[idx]); setValue(TextCap.values()[idx]);
@ -421,15 +420,15 @@ public class XSLFTextRun implements TextRun {
} }
@Override @Override
public void setBold(boolean bold){ public void setBold(boolean bold) {
getRPr(true).setB(bold); getRPr(true).setB(bold);
} }
@Override @Override
public boolean isBold(){ public boolean isBold() {
CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){ CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if (props != null && props.isSetB()) { if (props != null && props.isSetB()) {
setValue(props.getB()); setValue(props.getB());
return true; return true;
@ -442,15 +441,15 @@ public class XSLFTextRun implements TextRun {
} }
@Override @Override
public void setItalic(boolean italic){ public void setItalic(boolean italic) {
getRPr(true).setI(italic); getRPr(true).setI(italic);
} }
@Override @Override
public boolean isItalic(){ public boolean isItalic() {
CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){ CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if (props != null && props.isSetI()) { if (props != null && props.isSetI()) {
setValue(props.getI()); setValue(props.getI());
return true; return true;
@ -468,10 +467,10 @@ public class XSLFTextRun implements TextRun {
} }
@Override @Override
public boolean isUnderlined(){ public boolean isUnderlined() {
CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()){ CharacterPropertyFetcher<Boolean> fetcher = new CharacterPropertyFetcher<Boolean>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
if (props != null && props.isSetU()) { if (props != null && props.isSetU()) {
setValue(props.getU() != STTextUnderlineType.NONE); setValue(props.getU() != STTextUnderlineType.NONE);
return true; return true;
@ -491,21 +490,21 @@ public class XSLFTextRun implements TextRun {
*/ */
protected CTTextCharacterProperties getRPr(boolean create) { protected CTTextCharacterProperties getRPr(boolean create) {
if (_r instanceof CTTextField) { if (_r instanceof CTTextField) {
CTTextField tf = (CTTextField)_r; CTTextField tf = (CTTextField) _r;
if (tf.isSetRPr()) { if (tf.isSetRPr()) {
return tf.getRPr(); return tf.getRPr();
} else if (create) { } else if (create) {
return tf.addNewRPr(); return tf.addNewRPr();
} }
} else if (_r instanceof CTTextLineBreak) { } else if (_r instanceof CTTextLineBreak) {
CTTextLineBreak tlb = (CTTextLineBreak)_r; CTTextLineBreak tlb = (CTTextLineBreak) _r;
if (tlb.isSetRPr()) { if (tlb.isSetRPr()) {
return tlb.getRPr(); return tlb.getRPr();
} else if (create) { } else if (create) {
return tlb.addNewRPr(); return tlb.addNewRPr();
} }
} else { } else {
CTRegularTextRun tr = (CTRegularTextRun)_r; CTRegularTextRun tr = (CTRegularTextRun) _r;
if (tr.isSetRPr()) { if (tr.isSetRPr()) {
return tr.getRPr(); return tr.getRPr();
} else if (create) { } else if (create) {
@ -516,12 +515,12 @@ public class XSLFTextRun implements TextRun {
} }
@Override @Override
public String toString(){ public String toString() {
return "[" + getClass() + "]" + getRawText(); return "[" + getClass() + "]" + getRawText();
} }
@Override @Override
public XSLFHyperlink createHyperlink(){ public XSLFHyperlink createHyperlink() {
XSLFHyperlink hl = getHyperlink(); XSLFHyperlink hl = getHyperlink();
if (hl != null) { if (hl != null) {
return hl; return hl;
@ -532,7 +531,7 @@ public class XSLFTextRun implements TextRun {
} }
@Override @Override
public XSLFHyperlink getHyperlink(){ public XSLFHyperlink getHyperlink() {
CTTextCharacterProperties rPr = getRPr(false); CTTextCharacterProperties rPr = getRPr(false);
if (rPr == null) { if (rPr == null) {
return null; return null;
@ -544,7 +543,7 @@ public class XSLFTextRun implements TextRun {
return new XSLFHyperlink(hl, _p.getParentShape().getSheet()); return new XSLFHyperlink(hl, _p.getParentShape().getSheet());
} }
private boolean fetchCharacterProperty(CharacterPropertyFetcher<?> fetcher){ private boolean fetchCharacterProperty(CharacterPropertyFetcher<?> fetcher) {
XSLFTextShape shape = _p.getParentShape(); XSLFTextShape shape = _p.getParentShape();
XSLFSheet sheet = shape.getSheet(); XSLFSheet sheet = shape.getSheet();
@ -558,7 +557,7 @@ public class XSLFTextRun implements TextRun {
} }
CTPlaceholder ph = shape.getCTPlaceholder(); CTPlaceholder ph = shape.getCTPlaceholder();
if (ph == null){ if (ph == null) {
// if it is a plain text box then take defaults from presentation.xml // if it is a plain text box then take defaults from presentation.xml
@SuppressWarnings("resource") @SuppressWarnings("resource")
XMLSlideShow ppt = sheet.getSlideShow(); XMLSlideShow ppt = sheet.getSlideShow();
@ -570,47 +569,47 @@ public class XSLFTextRun implements TextRun {
} }
// TODO: determine master shape // TODO: determine master shape
CTTextParagraphProperties defaultProps = _p.getDefaultMasterStyle(); CTTextParagraphProperties defaultProps = _p.getDefaultMasterStyle();
if(defaultProps != null && fetcher.fetch(defaultProps)) { if (defaultProps != null && fetcher.fetch(defaultProps)) {
return true; return true;
} }
return false; return false;
} }
void copy(XSLFTextRun r){ void copy(XSLFTextRun r) {
String srcFontFamily = r.getFontFamily(); String srcFontFamily = r.getFontFamily();
if(srcFontFamily != null && !srcFontFamily.equals(getFontFamily())){ if (srcFontFamily != null && !srcFontFamily.equals(getFontFamily())) {
setFontFamily(srcFontFamily); setFontFamily(srcFontFamily);
} }
PaintStyle srcFontColor = r.getFontColor(); PaintStyle srcFontColor = r.getFontColor();
if(srcFontColor != null && !srcFontColor.equals(getFontColor())){ if (srcFontColor != null && !srcFontColor.equals(getFontColor())) {
setFontColor(srcFontColor); setFontColor(srcFontColor);
} }
double srcFontSize = r.getFontSize(); Double srcFontSize = r.getFontSize();
if(srcFontSize != getFontSize()){ if (srcFontSize != getFontSize()) {
setFontSize(srcFontSize); setFontSize(srcFontSize);
} }
boolean bold = r.isBold(); boolean bold = r.isBold();
if(bold != isBold()) { if (bold != isBold()) {
setBold(bold); setBold(bold);
} }
boolean italic = r.isItalic(); boolean italic = r.isItalic();
if(italic != isItalic()) { if (italic != isItalic()) {
setItalic(italic); setItalic(italic);
} }
boolean underline = r.isUnderlined(); boolean underline = r.isUnderlined();
if(underline != isUnderlined()) { if (underline != isUnderlined()) {
setUnderlined(underline); setUnderlined(underline);
} }
boolean strike = r.isStrikethrough(); boolean strike = r.isStrikethrough();
if(strike != isStrikethrough()) { if (strike != isStrikethrough()) {
setStrikethrough(strike); setStrikethrough(strike);
} }
} }
@ -619,7 +618,7 @@ public class XSLFTextRun implements TextRun {
@Override @Override
public FieldType getFieldType() { public FieldType getFieldType() {
if (_r instanceof CTTextField) { if (_r instanceof CTTextField) {
CTTextField tf = (CTTextField)_r; CTTextField tf = (CTTextField) _r;
if ("slidenum".equals(tf.getType())) { if ("slidenum".equals(tf.getType())) {
return FieldType.SLIDE_NUMBER; return FieldType.SLIDE_NUMBER;
} }
@ -673,48 +672,48 @@ public class XSLFTextRun implements TextRun {
getXmlObject(true).setTypeface(typeface); getXmlObject(true).setTypeface(typeface);
return; return;
} }
CTTextCharacterProperties props = getRPr(false); CTTextCharacterProperties props = getRPr(false);
if (props == null) { if (props == null) {
return; return;
} }
FontGroup fg = FontGroup.getFontGroupFirst(getRawText()); FontGroup fg = FontGroup.getFontGroupFirst(getRawText());
switch (fg) { switch (fg) {
default: default:
case LATIN: case LATIN:
if (props.isSetLatin()) { if (props.isSetLatin()) {
props.unsetLatin(); props.unsetLatin();
} }
break; break;
case EAST_ASIAN: case EAST_ASIAN:
if (props.isSetEa()) { if (props.isSetEa()) {
props.unsetEa(); props.unsetEa();
} }
break; break;
case COMPLEX_SCRIPT: case COMPLEX_SCRIPT:
if (props.isSetCs()) { if (props.isSetCs()) {
props.unsetCs(); props.unsetCs();
} }
break; break;
case SYMBOL: case SYMBOL:
if (props.isSetSym()) { if (props.isSetSym()) {
props.unsetSym(); props.unsetSym();
} }
break; break;
} }
} }
@Override @Override
public FontCharset getCharset() { public FontCharset getCharset() {
CTTextFont tf = getXmlObject(false); CTTextFont tf = getXmlObject(false);
return (tf != null && tf.isSetCharset()) ? FontCharset.valueOf(tf.getCharset()&0xFF) : null; return (tf != null && tf.isSetCharset()) ? FontCharset.valueOf(tf.getCharset() & 0xFF) : null;
} }
@Override @Override
public void setCharset(FontCharset charset) { public void setCharset(FontCharset charset) {
CTTextFont tf = getXmlObject(true); CTTextFont tf = getXmlObject(true);
if (charset != null) { if (charset != null) {
tf.setCharset((byte)charset.getNativeId()); tf.setCharset((byte) charset.getNativeId());
} else { } else {
if (tf.isSetCharset()) { if (tf.isSetCharset()) {
tf.unsetCharset(); tf.unsetCharset();
@ -735,8 +734,8 @@ public class XSLFTextRun implements TextRun {
return; return;
} }
FontPitch pitch = (tf.isSetPitchFamily()) FontPitch pitch = (tf.isSetPitchFamily())
? FontPitch.valueOfPitchFamily(tf.getPitchFamily()) ? FontPitch.valueOfPitchFamily(tf.getPitchFamily())
: FontPitch.VARIABLE; : FontPitch.VARIABLE;
byte pitchFamily = FontPitch.getNativeId(pitch, family != null ? family : FontFamily.FF_SWISS); byte pitchFamily = FontPitch.getNativeId(pitch, family != null ? family : FontFamily.FF_SWISS);
tf.setPitchFamily(pitchFamily); tf.setPitchFamily(pitchFamily);
} }
@ -754,8 +753,8 @@ public class XSLFTextRun implements TextRun {
return; return;
} }
FontFamily family = (tf.isSetPitchFamily()) FontFamily family = (tf.isSetPitchFamily())
? FontFamily.valueOfPitchFamily(tf.getPitchFamily()) ? FontFamily.valueOfPitchFamily(tf.getPitchFamily())
: FontFamily.FF_SWISS; : FontFamily.FF_SWISS;
byte pitchFamily = FontPitch.getNativeId(pitch != null ? pitch : FontPitch.VARIABLE, family); byte pitchFamily = FontPitch.getNativeId(pitch != null ? pitch : FontPitch.VARIABLE, family);
tf.setPitchFamily(pitchFamily); tf.setPitchFamily(pitchFamily);
} }
@ -765,9 +764,9 @@ public class XSLFTextRun implements TextRun {
return getCTTextFont(getRPr(true), true); return getCTTextFont(getRPr(true), true);
} }
CharacterPropertyFetcher<CTTextFont> visitor = new CharacterPropertyFetcher<CTTextFont>(_p.getIndentLevel()){ CharacterPropertyFetcher<CTTextFont> visitor = new CharacterPropertyFetcher<CTTextFont>(_p.getIndentLevel()) {
@Override @Override
public boolean fetch(CTTextCharacterProperties props){ public boolean fetch(CTTextCharacterProperties props) {
CTTextFont font = getCTTextFont(props, false); CTTextFont font = getCTTextFont(props, false);
if (font == null) { if (font == null) {
return false; return false;
@ -778,7 +777,7 @@ public class XSLFTextRun implements TextRun {
}; };
fetchCharacterProperty(visitor); fetchCharacterProperty(visitor);
return visitor.getValue(); return visitor.getValue();
} }
private CTTextFont getCTTextFont(CTTextCharacterProperties props, boolean create) { private CTTextFont getCTTextFont(CTTextCharacterProperties props, boolean create) {
@ -788,31 +787,31 @@ public class XSLFTextRun implements TextRun {
CTTextFont font; CTTextFont font;
switch (fontGroup) { switch (fontGroup) {
default: default:
case LATIN: case LATIN:
font = props.getLatin(); font = props.getLatin();
if (font == null && create) { if (font == null && create) {
font = props.addNewLatin(); font = props.addNewLatin();
} }
break; break;
case EAST_ASIAN: case EAST_ASIAN:
font = props.getEa(); font = props.getEa();
if (font == null && create) { if (font == null && create) {
font = props.addNewEa(); font = props.addNewEa();
} }
break; break;
case COMPLEX_SCRIPT: case COMPLEX_SCRIPT:
font = props.getCs(); font = props.getCs();
if (font == null && create) { if (font == null && create) {
font = props.addNewCs(); font = props.addNewCs();
} }
break; break;
case SYMBOL: case SYMBOL:
font = props.getSym(); font = props.getSym();
if (font == null && create) { if (font == null && create) {
font = props.addNewSym(); font = props.addNewSym();
} }
break; break;
} }
if (font == null) { if (font == null) {
@ -825,7 +824,7 @@ public class XSLFTextRun implements TextRun {
final XSLFTheme theme = _p.getParentShape().getSheet().getTheme(); final XSLFTheme theme = _p.getParentShape().getSheet().getTheme();
CTFontScheme fontTheme = theme.getXmlObject().getThemeElements().getFontScheme(); CTFontScheme fontTheme = theme.getXmlObject().getThemeElements().getFontScheme();
CTFontCollection coll = typeface.startsWith("+mj-") CTFontCollection coll = typeface.startsWith("+mj-")
? fontTheme.getMajorFont() : fontTheme.getMinorFont(); ? fontTheme.getMajorFont() : fontTheme.getMinorFont();
// TODO: handle LCID codes // TODO: handle LCID codes
// see https://blogs.msdn.microsoft.com/officeinteroperability/2013/04/22/office-open-xml-themes-schemes-and-fonts/ // see https://blogs.msdn.microsoft.com/officeinteroperability/2013/04/22/office-open-xml-themes-schemes-and-fonts/
String fgStr = typeface.substring(4); String fgStr = typeface.substring(4);
@ -837,7 +836,7 @@ public class XSLFTextRun implements TextRun {
font = coll.getLatin(); font = coll.getLatin();
} }
// SYMBOL is missing // SYMBOL is missing
if (font == null || !font.isSetTypeface() || "".equals(font.getTypeface())) { if (font == null || !font.isSetTypeface() || "".equals(font.getTypeface())) {
font = coll.getLatin(); font = coll.getLatin();
} }