* @return full height of this text run which is sum of ascent, descent and leading\r
*/\r
public float getHeight(){\r
- return _layout.getAscent() + _layout.getDescent() + _layout.getLeading();\r
+ double h = Math.ceil(_layout.getAscent()) + Math.ceil(_layout.getDescent()) + _layout.getLeading();\r
+ return (float)h;\r
}\r
\r
/**\r
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ====================================================================\r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;\r
+import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;\r
+\r
+/**\r
+ * @author Yegor Kozlov\r
+ */\r
+class XSLFLineBreak extends XSLFTextRun {\r
+ private final CTTextCharacterProperties _brProps;\r
+\r
+ XSLFLineBreak(CTRegularTextRun r, XSLFTextParagraph p, CTTextCharacterProperties brProps){\r
+ super(r, p);\r
+ _brProps = brProps;\r
+ }\r
+\r
+ @Override\r
+ protected CTTextCharacterProperties getRPr(){\r
+ return _brProps;\r
+ }\r
+\r
+ public void setText(String text){\r
+ throw new IllegalStateException("You cannot change text of a line break, it is always '\\n'");\r
+ }\r
+\r
+}\r
return _runs.iterator();\r
}\r
\r
+ /**\r
+ * Add a new run of text\r
+ *\r
+ * @return a new run of text\r
+ */\r
public XSLFTextRun addNewTextRun(){\r
CTRegularTextRun r = _p.addNewR();\r
CTTextCharacterProperties rPr = r.addNewRPr();\r
return run;\r
}\r
\r
- public void addLineBreak(){\r
- _p.addNewBr();\r
+ /**\r
+ * Insert a line break\r
+ *\r
+ * @return text run representing this line break ('\n')\r
+ */\r
+ public XSLFTextRun addLineBreak(){\r
+ CTTextLineBreak br = _p.addNewBr();\r
+ CTTextCharacterProperties brProps = br.addNewRPr();\r
+ if(_runs.size() > 0){\r
+ // by default line break has the font size of the last text run\r
+ CTTextCharacterProperties prevRun = _runs.get(_runs.size() - 1).getRPr();\r
+ brProps.set(prevRun);\r
+ }\r
+ CTRegularTextRun r = CTRegularTextRun.Factory.newInstance();\r
+ r.setRPr(brProps);\r
+ r.setT("\n");\r
+ XSLFTextRun run = new XSLFLineBreak(r, this, brProps);\r
+ _runs.add(run);\r
+ return run;\r
}\r
\r
/**\r
}\r
\r
public void setFontColor(Color color){\r
- CTTextCharacterProperties rPr = getRpR();\r
+ CTTextCharacterProperties rPr = getRPr();\r
CTSolidColorFillProperties fill = rPr.isSetSolidFill() ? rPr.getSolidFill() : rPr.addNewSolidFill();\r
CTSRgbColor clr = fill.isSetSrgbClr() ? fill.getSrgbClr() : fill.addNewSrgbClr();\r
clr.setVal(new byte[]{(byte)color.getRed(), (byte)color.getGreen(), (byte)color.getBlue()});\r
* The value of <code>-1</code> unsets the Sz attribyte from the underlying xml bean\r
*/\r
public void setFontSize(double fontSize){\r
- CTTextCharacterProperties rPr = getRpR();\r
+ CTTextCharacterProperties rPr = getRPr();\r
if(fontSize == -1.0) {\r
if(rPr.isSetSz()) rPr.unsetSz();\r
} else {\r
* @param spc character spacing in points.\r
*/\r
public void setCharacterSpacing(double spc){\r
- CTTextCharacterProperties rPr = getRpR();\r
+ CTTextCharacterProperties rPr = getRPr();\r
if(spc == 0.0) {\r
if(rPr.isSetSpc()) rPr.unsetSpc();\r
} else {\r
}\r
\r
public void setFontFamily(String typeface, byte charset, byte pictAndFamily, boolean isSymbol){\r
- CTTextCharacterProperties rPr = getRpR();\r
+ CTTextCharacterProperties rPr = getRPr();\r
\r
if(typeface == null){\r
if(rPr.isSetLatin()) rPr.unsetLatin();\r
*\r
* @param strike whether a run of text will be formatted as strikethrough text.\r
*/\r
- public void setStrikethrough(boolean strike){\r
- getRpR().setStrike(strike ? STTextStrikeType.SNG_STRIKE : STTextStrikeType.NO_STRIKE);\r
+ public void setStrikethrough(boolean strike) {\r
+ getRPr().setStrike(strike ? STTextStrikeType.SNG_STRIKE : STTextStrikeType.NO_STRIKE);\r
}\r
\r
/**\r
* @param bold whether this run of text will be formatted as bold text\r
*/\r
public void setBold(boolean bold){\r
- getRpR().setB(bold);\r
+ getRPr().setB(bold);\r
}\r
\r
/**\r
* @param italic whether this run of text is formatted as italic text\r
*/\r
public void setItalic(boolean italic){\r
- getRpR().setI(italic);\r
+ getRPr().setI(italic);\r
}\r
\r
/**\r
/**\r
* @param underline whether this run of text is formatted as underlined text\r
*/\r
- public void setUnderline(boolean underline){\r
- getRpR().setU(underline ? STTextUnderlineType.SNG : STTextUnderlineType.NONE);\r
+ public void setUnderline(boolean underline) {\r
+ getRPr().setU(underline ? STTextUnderlineType.SNG : STTextUnderlineType.NONE);\r
}\r
\r
/**\r
return fetcher.getValue() == null ? false : fetcher.getValue();\r
}\r
\r
- protected CTTextCharacterProperties getRpR(){\r
+ protected CTTextCharacterProperties getRPr(){\r
return _r.isSetRPr() ? _r.getRPr() : _r.addNewRPr();\r
}\r
\r
private boolean fetchCharacterProperty(CharacterPropertyFetcher fetcher){\r
boolean ok = false;\r
\r
- if(_r.isSetRPr()) ok = fetcher.fetch(_r.getRPr());\r
+ if(_r.isSetRPr()) ok = fetcher.fetch(getRPr());\r
\r
if(!ok) {\r
XSLFTextShape shape = _p.getParentShape();\r
"Theme Master first level\n" +
"And the 2nd level\n" +
"Our 3rd level goes here\n" +
- "And onto the 4th, such fun….\n" +
+ "And onto the 4th, such fun....\n" +
"Finally is the Fifth level\n";
// Check the whole text
p.setBullet(false);\r
assertFalse(p.isBullet());\r
}\r
+\r
+ public void testLineBreak(){\r
+ XMLSlideShow ppt = new XMLSlideShow();\r
+ XSLFSlide slide = ppt.createSlide();\r
+ XSLFTextShape sh = slide.createAutoShape();\r
+\r
+ XSLFTextParagraph p = sh.addNewTextParagraph();\r
+ XSLFTextRun r1 = p.addNewTextRun();\r
+ r1.setText("Hello,");\r
+ XSLFTextRun r2 = p.addLineBreak();\r
+ assertEquals("\n", r2.getText());\r
+ r2.setFontSize(10.0);\r
+ assertEquals(10.0, r2.getFontSize());\r
+ XSLFTextRun r3 = p.addNewTextRun();\r
+ r3.setText("World!");\r
+ r3.setFontSize(20.0);\r
+ XSLFTextRun r4 = p.addLineBreak();\r
+ assertEquals(20.0, r4.getFontSize());\r
+\r
+ assertEquals("Hello,\nWorld!\n",sh.getText());\r
+\r
+ try {\r
+ r2.setText("aaa");\r
+ fail("Expected IllegalStateException");\r
+ } catch (IllegalStateException e){\r
+ assertEquals("You cannot change text of a line break, it is always '\\n'", e.getMessage());\r
+ }\r
+ }\r
}\r
package org.apache.poi.xslf.usermodel;\r
\r
import junit.framework.TestCase;\r
-import org.apache.poi.util.POILogFactory;\r
-import org.apache.poi.util.POILogger;\r
-import org.apache.poi.xslf.XSLFTestDataSamples;\r
\r
import java.awt.*;\r
-import java.awt.geom.Rectangle2D;\r
-import java.awt.image.BufferedImage;\r
-import java.util.List;\r
\r
/**\r
* @author Yegor Kozlov\r
XSLFTextShape sh = slide.createAutoShape();\r
\r
XSLFTextRun r = sh.addNewTextParagraph().addNewTextRun();\r
- assertEquals("en-US", r.getRpR().getLang());\r
+ assertEquals("en-US", r.getRPr().getLang());\r
\r
assertEquals(0., r.getCharacterSpacing());\r
r.setCharacterSpacing(3);\r
assertEquals(-3., r.getCharacterSpacing());\r
r.setCharacterSpacing(0);\r
assertEquals(0., r.getCharacterSpacing());\r
- assertFalse(r.getRpR().isSetSpc());\r
+ assertFalse(r.getRPr().isSetSpc());\r
\r
assertEquals(Color.black, r.getFontColor());\r
r.setFontColor(Color.red);\r