FindBugs fixes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1823892 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2018-02-11 20:35:11 +00:00
parent 0e3e8c795a
commit 5f71c80131
4 changed files with 200 additions and 195 deletions

View File

@ -381,17 +381,18 @@ public class DrawTextParagraph implements Drawable {
return getRenderableText(tr); return getRenderableText(tr);
} }
String getRenderableText(TextRun tr) { private String getRenderableText(final TextRun tr) {
String txt = tr.getRawText(); final String txtSpace = tr.getRawText().replace("\t", tab2space(tr)).replace('\u000b', '\n');
txt = txt.replace("\t", tab2space(tr)).replace("\u000b", "\n"); final Locale loc = LocaleUtil.getUserLocale();
switch (tr.getTextCap()) { switch (tr.getTextCap()) {
case ALL: txt = txt.toUpperCase(LocaleUtil.getUserLocale()); break; case ALL:
case SMALL: txt = txt.toLowerCase(LocaleUtil.getUserLocale()); break; return txtSpace.toUpperCase(loc);
case NONE: break; case SMALL:
return txtSpace.toLowerCase(loc);
default:
return txtSpace;
} }
return txt;
} }
/** /**

View File

@ -17,6 +17,7 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import java.awt.Color; import java.awt.Color;
import java.util.Locale;
import org.apache.poi.common.usermodel.fonts.FontCharset; import org.apache.poi.common.usermodel.fonts.FontCharset;
import org.apache.poi.common.usermodel.fonts.FontFamily; import org.apache.poi.common.usermodel.fonts.FontFamily;
@ -60,64 +61,64 @@ 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) { /* package */ String getRenderableText(final String txt){
// TODO: finish support for tabs // TODO: finish support for tabs
txt = txt.replace("\t", " "); final String txtSpace = txt.replace("\t", " ");
final Locale loc = LocaleUtil.getUserLocale();
switch (getTextCap()) { switch (getTextCap()) {
case ALL: case ALL:
txt = txt.toUpperCase(LocaleUtil.getUserLocale()); return txtSpace.toUpperCase(loc);
break;
case SMALL: case SMALL:
txt = txt.toLowerCase(LocaleUtil.getUserLocale()); return txtSpace.toLowerCase(loc);
break; default:
return txtSpace;
} }
return txt;
} }
@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 +129,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 +143,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 +154,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 +176,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 +189,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,43 +200,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;
@ -252,16 +253,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));
} }
} }
@ -299,7 +300,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();
@ -320,10 +321,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;
} }
@ -336,9 +337,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;
@ -351,16 +352,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);
} }
/** /**
@ -369,7 +370,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.);
} }
@ -379,15 +380,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;
@ -404,9 +405,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]);
@ -420,15 +421,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;
@ -441,15 +442,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;
@ -467,10 +468,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;
@ -490,21 +491,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) {
@ -515,12 +516,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;
@ -531,7 +532,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;
@ -543,7 +544,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();
@ -557,7 +558,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();
@ -569,22 +570,22 @@ 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);
} }
@ -596,22 +597,22 @@ public class XSLFTextRun implements TextRun {
} }
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);
} }
} }
@ -620,7 +621,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;
} }
@ -674,48 +675,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();
@ -736,8 +737,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);
} }
@ -755,8 +756,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);
} }
@ -766,9 +767,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;
@ -779,7 +780,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) {
@ -789,31 +790,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) {
@ -826,7 +827,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);
@ -838,7 +839,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();
} }

View File

@ -32,7 +32,6 @@ import java.awt.geom.Rectangle2D;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;

View File

@ -19,30 +19,29 @@ package org.apache.poi.hslf.blip;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.zip.DeflaterOutputStream; import java.util.zip.DeflaterOutputStream;
import org.apache.poi.hslf.usermodel.HSLFPictureData; import org.apache.poi.hslf.usermodel.HSLFPictureData;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianInputStream;
import org.apache.poi.util.LittleEndianOutputStream;
import org.apache.poi.util.Units; import org.apache.poi.util.Units;
/** /**
* Represents a metafile picture which can be one of the following types: EMF, WMF, or PICT. * Represents a metafile picture which can be one of the following types: EMF, WMF, or PICT.
* A metafile is stored compressed using the ZIP deflate/inflate algorithm. * A metafile is stored compressed using the ZIP deflate/inflate algorithm.
*
* @author Yegor Kozlov
*/ */
public abstract class Metafile extends HSLFPictureData { public abstract class Metafile extends HSLFPictureData {
/** /**
* A structure which represents a 34-byte header preceding the compressed metafile data * A structure which represents a 34-byte header preceding the compressed metafile data
*
* @author Yegor Kozlov
*/ */
public static class Header{ public static class Header{
private static final int RECORD_LENGTH = 34;
/** /**
* size of the original file * size of the original file
*/ */
@ -74,43 +73,48 @@ public abstract class Metafile extends HSLFPictureData {
private int filter = 254; private int filter = 254;
public void read(byte[] data, int offset){ public void read(byte[] data, int offset){
int pos = offset; @SuppressWarnings("resource")
wmfsize = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE; LittleEndianInputStream leis = new LittleEndianInputStream(
new ByteArrayInputStream(data, offset, RECORD_LENGTH));
int left = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
int top = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE; wmfsize = leis.readInt();
int right = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
int bottom = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
int left = leis.readInt();
int top = leis.readInt();
int right = leis.readInt();
int bottom = leis.readInt();
bounds.setBounds(left, top, right-left, bottom-top); bounds.setBounds(left, top, right-left, bottom-top);
int width = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
int height = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
int width = leis.readInt();
int height = leis.readInt();
size.setSize(width, height); size.setSize(width, height);
zipsize = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE; zipsize = leis.readInt();
compression = leis.readUByte();
compression = LittleEndian.getUByte(data, pos); pos++; filter = leis.readUByte();
filter = LittleEndian.getUByte(data, pos); pos++;
} }
public void write(OutputStream out) throws IOException { public void write(OutputStream out) throws IOException {
byte[] header = new byte[34]; @SuppressWarnings("resource")
int pos = 0; LittleEndianOutputStream leos = new LittleEndianOutputStream(out);
LittleEndian.putInt(header, pos, wmfsize); pos += LittleEndian.INT_SIZE; //hmf
//hmf
LittleEndian.putInt(header, pos, bounds.x); pos += LittleEndian.INT_SIZE; //left leos.writeInt(wmfsize);
LittleEndian.putInt(header, pos, bounds.y); pos += LittleEndian.INT_SIZE; //top //left
LittleEndian.putInt(header, pos, bounds.x + bounds.width); pos += LittleEndian.INT_SIZE; //right leos.writeInt(bounds.x);
LittleEndian.putInt(header, pos, bounds.y + bounds.height); pos += LittleEndian.INT_SIZE; //bottom //top
LittleEndian.putInt(header, pos, size.width); pos += LittleEndian.INT_SIZE; //inch leos.writeInt(bounds.y);
LittleEndian.putInt(header, pos, size.height); pos += LittleEndian.INT_SIZE; //inch //right
LittleEndian.putInt(header, pos, zipsize); pos += LittleEndian.INT_SIZE; //inch leos.writeInt(bounds.x + bounds.width);
//bottom
header[pos] = 0; pos ++; leos.writeInt(bounds.y + bounds.height);
header[pos] = (byte)filter; pos ++; //inch
leos.writeInt(size.width);
out.write(header); //inch
leos.writeInt(size.height);
leos.writeInt(zipsize);
leos.writeByte(compression);
leos.writeByte(filter);
} }
public int getSize(){ public int getSize(){