import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
+import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
+import org.openxmlformats.schemas.drawingml.x2006.picture.CTPictureNonVisual;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
-import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
-import org.openxmlformats.schemas.drawingml.x2006.picture.CTPictureNonVisual;
/**
* XWPFRun object defines a region of text with a common set of properties
private IRunBody parent;
private List<XWPFPicture> pictures;
+ /**
+ * @see <a href="http://msdn.microsoft.com/en-us/library/ff533743(v=office.12).aspx">[MS-OI29500] Run Fonts</a>
+ */
+ public static enum FontCharRange {
+ ascii /* char 0-127 */,
+ cs /* complex symbol */,
+ eastAsia /* east asia */,
+ hAnsi /* high ansi */
+ };
+
/**
* @param r the CTR bean which holds the run attributes
* @param p the parent paragraph
}
/**
- * Specifies the fonts which shall be used to display the text contents of
+ * Gets the fonts which shall be used to display the text contents of
* this run. Specifies a font which shall be used to format all characters
* in the ASCII range (0 - 127) within the parent run
*
* @return a string representing the font family
*/
public String getFontFamily() {
- CTRPr pr = run.getRPr();
- return (pr != null && pr.isSetRFonts()) ? pr.getRFonts().getAscii()
- : null;
+ return getFontFamily(null);
}
+ /**
+ * Gets the font family for the specified font char range.
+ * If fcr is null, the font char range "ascii" is used
+ *
+ * @param fcr the font char range, defaults to "ansi"
+ * @return a string representing the font famil
+ */
+ public String getFontFamily(FontCharRange fcr) {
+ CTRPr pr = run.getRPr();
+ if (pr == null || !pr.isSetRFonts()) return null;
+
+ CTFonts fonts = pr.getRFonts();
+ switch (fcr == null ? FontCharRange.ascii : fcr) {
+ default:
+ case ascii:
+ return fonts.getAscii();
+ case cs:
+ return fonts.getCs();
+ case eastAsia:
+ return fonts.getEastAsia();
+ case hAnsi:
+ return fonts.getHAnsi();
+ }
+ }
+
+
/**
* Specifies the fonts which shall be used to display the text contents of
* this run. Specifies a font which shall be used to format all characters
- * in the ASCII range (0 - 127) within the parent run
+ * in the ASCII range (0 - 127) within the parent run.
+ *
+ * Also sets the other font ranges, if they haven't been set before
*
* @param fontFamily
+ *
+ * @see FontCharRange
*/
public void setFontFamily(String fontFamily) {
+ setFontFamily(fontFamily, null);
+ }
+
+ /**
+ * Specifies the fonts which shall be used to display the text contents of
+ * this run. The default handling for fcr == null is to overwrite the
+ * ascii font char range with the given font family and also set all not
+ * specified font ranges
+ *
+ * @param fontFamily
+ * @param fcr FontCharRange or null for default handling
+ */
+ public void setFontFamily(String fontFamily, FontCharRange fcr) {
CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr();
CTFonts fonts = pr.isSetRFonts() ? pr.getRFonts() : pr.addNewRFonts();
- fonts.setAscii(fontFamily);
+
+ if (fcr == null) {
+ fonts.setAscii(fontFamily);
+ if (!fonts.isSetHAnsi()) {
+ fonts.setHAnsi(fontFamily);
+ }
+ if (!fonts.isSetCs()) {
+ fonts.setCs(fontFamily);
+ }
+ if (!fonts.isSetEastAsia()) {
+ fonts.setEastAsia(fontFamily);
+ }
+ } else {
+ switch (fcr) {
+ case ascii:
+ fonts.setAscii(fontFamily);
+ break;
+ case cs:
+ fonts.setCs(fontFamily);
+ break;
+ case eastAsia:
+ fonts.setEastAsia(fontFamily);
+ break;
+ case hAnsi:
+ fonts.setHAnsi(fontFamily);
+ break;
+ }
+ }
}
/**
--- /dev/null
+/* ====================================================================\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
+package org.apache.poi.xwpf.usermodel;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+\r
+import org.apache.poi.xwpf.usermodel.XWPFRun.FontCharRange;\r
+import org.junit.Test;\r
+\r
+public class TestXWPFBugs {\r
+ @Test\r
+ public void bug55802() throws Exception {\r
+ String blabla =\r
+ "Bir, iki, \u00fc\u00e7, d\u00f6rt, be\u015f,\n"+\r
+ "\nalt\u0131, yedi, sekiz, dokuz, on.\n"+\r
+ "\nK\u0131rm\u0131z\u0131 don,\n"+\r
+ "\ngel bizim bah\u00e7eye kon,\n"+\r
+ "\nsar\u0131 limon";\r
+ XWPFDocument doc = new XWPFDocument();\r
+ XWPFRun run = doc.createParagraph().createRun();\r
+ \r
+ for (String str : blabla.split("\n")) {\r
+ run.setText(str);\r
+ run.addBreak();\r
+ }\r
+\r
+ run.setFontFamily("Times New Roman");\r
+ run.setFontSize(20);\r
+ assertEquals(run.getFontFamily(), "Times New Roman");\r
+ assertEquals(run.getFontFamily(FontCharRange.cs), "Times New Roman");\r
+ assertEquals(run.getFontFamily(FontCharRange.eastAsia), "Times New Roman");\r
+ assertEquals(run.getFontFamily(FontCharRange.hAnsi), "Times New Roman");\r
+ run.setFontFamily("Arial", FontCharRange.hAnsi);\r
+ assertEquals(run.getFontFamily(FontCharRange.hAnsi), "Arial");\r
+ }\r
+\r
+}\r