From 2e2062d99ae126c14217f4eaa8c3bc0008297bc3 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sat, 15 Nov 2008 12:54:09 +0000 Subject: [PATCH] applied patch #46212 by Gisella Bronzetti: initial implementation of XWPFRun object git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@714244 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/TextAlignment.java | 78 +++ .../poi/xwpf/usermodel/UnderlinePatterns.java | 163 ++++++ .../poi/xwpf/usermodel/VerticalAlign.java | 75 +++ .../apache/poi/xwpf/usermodel/XWPFRun.java | 478 ++++++++++++++---- .../poi/xwpf/usermodel/TestXWPFRun.java | 135 +++++ 5 files changed, 824 insertions(+), 105 deletions(-) create mode 100755 src/ooxml/java/org/apache/poi/xwpf/usermodel/TextAlignment.java create mode 100755 src/ooxml/java/org/apache/poi/xwpf/usermodel/UnderlinePatterns.java create mode 100755 src/ooxml/java/org/apache/poi/xwpf/usermodel/VerticalAlign.java create mode 100755 src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/TextAlignment.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TextAlignment.java new file mode 100755 index 000000000..4bf1f86d1 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TextAlignment.java @@ -0,0 +1,78 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import java.util.HashMap; +import java.util.Map; + +/** + * Specifies all types of vertical alignment which are available to be applied to of all text + * on each line displayed within a paragraph. + * + * @author Gisella Bronzetti + */ +public enum TextAlignment { + /** + * Specifies that all text in the parent object shall be + * aligned to the top of each character when displayed + */ + TOP(1), + /** + * Specifies that all text in the parent object shall be + * aligned to the center of each character when displayed. + */ + CENTER(2), + /** + * Specifies that all text in the parent object shall be + * aligned to the baseline of each character when displayed. + */ + BASELINE(3), + /** + * Specifies that all text in the parent object shall be + * aligned to the bottom of each character when displayed. + */ + BOTTOM(4), + /** + * Specifies that all text in the parent object shall be + * aligned automatically when displayed. + */ + AUTO(5); + + private final int value; + + private TextAlignment(int val){ + value = val; + } + + public int getValue(){ + return value; + } + + private static Map imap = new HashMap(); + static{ + for (TextAlignment p : values()) { + imap.put(p.getValue(), p); + } + } + + public static TextAlignment valueOf(int type){ + TextAlignment align = imap.get(type); + if(align == null) throw new IllegalArgumentException("Unknown text alignment: " + type); + return align; + } + +} diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/UnderlinePatterns.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/UnderlinePatterns.java new file mode 100755 index 000000000..a2d3682a4 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/UnderlinePatterns.java @@ -0,0 +1,163 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import java.util.HashMap; +import java.util.Map; + +/** + * Specifies the types of patterns which may be used to create the underline + * applied beneath the text in a run. + * + * @author Gisella Bronzetti + */ +public enum UnderlinePatterns { + + /** + * Specifies an underline consisting of a single line beneath all characters + * in this run. + */ + SINGLE(1), + + /** + * Specifies an underline consisting of a single line beneath all non-space + * characters in the run. There shall be no underline beneath any space + * character (breaking or non-breaking). + */ + WORDS(2), + + /** + * Specifies an underline consisting of two lines beneath all characters in + * this run + */ + DOUBLE(3), + + /** + * Specifies an underline consisting of a single thick line beneath all + * characters in this run. + */ + THICK(4), + + /** + * Specifies an underline consisting of a series of dot characters beneath + * all characters in this run. + */ + DOTTED(5), + + /** + * Specifies an underline consisting of a series of thick dot characters + * beneath all characters in this run. + */ + DOTTED_HEAVY(6), + + /** + * Specifies an underline consisting of a dashed line beneath all characters + * in this run. + */ + DASH(7), + + /** + * Specifies an underline consisting of a series of thick dashes beneath all + * characters in this run. + */ + DASHED_HEAVY(8), + + /** + * Specifies an underline consisting of long dashed characters beneath all + * characters in this run. + */ + DASH_LONG(9), + + /** + * Specifies an underline consisting of thick long dashed characters beneath + * all characters in this run. + */ + DASH_LONG_HEAVY(10), + + /** + * Specifies an underline consisting of a series of dash, dot characters + * beneath all characters in this run. + */ + DOT_DASH(11), + + /** + * Specifies an underline consisting of a series of thick dash, dot + * characters beneath all characters in this run. + */ + DASH_DOT_HEAVY(12), + + /** + * Specifies an underline consisting of a series of dash, dot, dot + * characters beneath all characters in this run. + */ + DOT_DOT_DASH(13), + + /** + * Specifies an underline consisting of a series of thick dash, dot, dot + * characters beneath all characters in this run. + */ + DASH_DOT_DOT_HEAVY(14), + + /** + * Specifies an underline consisting of a single wavy line beneath all + * characters in this run. + */ + WAVE(15), + + /** + * Specifies an underline consisting of a single thick wavy line beneath all + * characters in this run. + */ + WAVY_HEAVY(16), + + /** + * Specifies an underline consisting of a pair of wavy lines beneath all + * characters in this run. + */ + WAVY_DOUBLE(17), + + /** + * Specifies no underline beneath this run. + */ + NONE(18); + + private final int value; + + private UnderlinePatterns(int val) { + value = val; + } + + public int getValue() { + return value; + } + + private static Map imap = new HashMap(); + static { + for (UnderlinePatterns p : values()) { + imap.put(p.getValue(), p); + } + } + + public static UnderlinePatterns valueOf(int type) { + UnderlinePatterns align = imap.get(type); + if (align == null) + throw new IllegalArgumentException("Unknown underline pattern: " + + type); + return align; + } + +} diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/VerticalAlign.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/VerticalAlign.java new file mode 100755 index 000000000..691d856c1 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/VerticalAlign.java @@ -0,0 +1,75 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import java.util.HashMap; +import java.util.Map; + +/** + * Specifies possible values for the alignment of the contents of this run in + * relation to the default appearance of the run's text. This allows the text to + * be repositioned as subscript or superscript without altering the font size of + * the run properties. + * + * @author Gisella Bronzetti + */ +public enum VerticalAlign { + + /** + * Specifies that the text in the parent run shall be located at the + * baseline and presented in the same size as surrounding text. + */ + BASELINE(1), + /** + * Specifies that this text should be subscript. This setting shall lower + * the text in this run below the baseline and change it to a smaller size, + * if a smaller size is available. + */ + SUPERSCRIPT(2), + /** + * Specifies that this text should be superscript. This setting shall raise + * the text in this run above the baseline and change it to a smaller size, + * if a smaller size is available. + */ + SUBSCRIPT(3); + + private final int value; + + private VerticalAlign(int val) { + value = val; + } + + public int getValue() { + return value; + } + + private static Map imap = new HashMap(); + static { + for (VerticalAlign p : values()) { + imap.put(p.getValue(), p); + } + } + + public static VerticalAlign valueOf(int type) { + VerticalAlign align = imap.get(type); + if (align == null) + throw new IllegalArgumentException("Unknown vertical alignment: " + + type); + return align; + } + +} diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index 570f6faa1..7af82bdbf 100755 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -1,105 +1,373 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -==================================================================== */ -package org.apache.poi.xwpf.usermodel; - -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; - -/** - * XWPFRun object defines a region of text with a common set of properties - * - * @author Yegor Kozlov - */ -public class XWPFRun { - private CTR run; - private XWPFParagraph paragraph; - - /** - * - * @param r the CTR bean which holds the run attributes - * @param p the parent paragraph - */ - protected XWPFRun(CTR r, XWPFParagraph p){ - this.run = r; - this.paragraph = p; - } - - public CTR getCTR(){ - return run; - } - - public XWPFParagraph getParagraph(){ - return paragraph; - } - - /** - * Whether the bold property shall be applied to all non-complex script characters in the - * contents of this run when displayed in a document - * - * @return true if the bold property is applied - */ - public boolean isBold(){ - CTRPr pr = run.getRPr(); - return pr != null && pr.isSetB(); - } - - /** - * Whether the bold property shall be applied to all non-complex script characters in the - * contents of this run when displayed in a document - * - *

- * This formatting property is a toggle property, which specifies that its behavior differs between its use within a - * style definition and its use as direct formatting. When used as part of a style definition, setting this property - * shall toggle the current state of that property as specified up to this point in the hierarchy (i.e. applied to not - * applied, and vice versa). Setting it to false (or an equivalent) shall result in the current - * setting remaining unchanged. However, when used as direct formatting, setting this property to true or false - * shall set the absolute state of the resulting property. - *

- *

- * If this element is not present, the default value is to leave the formatting applied at previous level in the style - * hierarchy. If this element is never applied in the style hierarchy, then bold shall not be applied to non-complex - * script characters. - *

- * - * @param value true if the bold property is applied to this run - */ - public void setBold(boolean value){ - CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); - pr.addNewB().setVal(value ? STOnOff.TRUE : STOnOff.FALSE); - } - - /** - * Return the string content of this text run - * - * @return the text of this text run or null if not set - */ - public String getText(){ - return run.sizeOfTArray() == 0 ? null : run.getTArray(0).getStringValue(); - } - - /** - * Sets the text of this text run - * - * @param value the literal text which shall be displayed in the document - */ - public void setText(String value){ - CTText t = run.sizeOfTArray() == 0 ? run.addNewT() : run.getTArray(0); - t.setStringValue(value); - } -} +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import java.math.BigInteger; + +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSignedHpsMeasure; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalAlignRun; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; + +/** + * XWPFRun object defines a region of text with a common set of properties + * + * @author Yegor Kozlov + */ +public class XWPFRun { + private CTR run; + private XWPFParagraph paragraph; + + /** + * @param r the CTR bean which holds the run attributes + * @param p the parent paragraph + */ + protected XWPFRun(CTR r, XWPFParagraph p) { + this.run = r; + this.paragraph = p; + } + + public CTR getCTR() { + return run; + } + + public XWPFParagraph getParagraph() { + return paragraph; + } + + /** + * Whether the bold property shall be applied to all non-complex script + * characters in the contents of this run when displayed in a document + * + * @return true if the bold property is applied + */ + public boolean isBold() { + CTRPr pr = run.getRPr(); + return pr != null && pr.isSetB(); + } + + /** + * Whether the bold property shall be applied to all non-complex script + * characters in the contents of this run when displayed in a document + *

+ *

+ * This formatting property is a toggle property, which specifies that its + * behavior differs between its use within a style definition and its use as + * direct formatting. When used as part of a style definition, setting this + * property shall toggle the current state of that property as specified up + * to this point in the hierarchy (i.e. applied to not applied, and vice + * versa). Setting it to false (or an equivalent) shall + * result in the current setting remaining unchanged. However, when used as + * direct formatting, setting this property to true or false shall set the + * absolute state of the resulting property. + *

+ *

+ * If this element is not present, the default value is to leave the + * formatting applied at previous level in the style hierarchy. If this + * element is never applied in the style hierarchy, then bold shall not be + * applied to non-complex script characters. + *

+ * + * @param value true if the bold property is applied to + * this run + */ + public void setBold(boolean value) { + CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); + CTOnOff bold = pr.isSetB() ? pr.getB() : pr.addNewB(); + bold.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + } + + /** + * Return the string content of this text run + * + * @return the text of this text run or null if not set + */ + public String getText() { + return run.sizeOfTArray() == 0 ? null : run.getTArray(0) + .getStringValue(); + } + + /** + * Sets the text of this text run + * + * @param value the literal text which shall be displayed in the document + */ + public void setText(String value) { + CTText t = run.sizeOfTArray() == 0 ? run.addNewT() : run.getTArray(0); + t.setStringValue(value); + } + + /** + * Whether the italic property should be applied to all non-complex script + * characters in the contents of this run when displayed in a document. + * + * @return true if the italic property is applied + */ + public boolean isItalic() { + CTRPr pr = run.getRPr(); + return pr != null && pr.isSetI(); + } + + /** + * Whether the bold property shall be applied to all non-complex script + * characters in the contents of this run when displayed in a document + *

+ *

+ * This formatting property is a toggle property, which specifies that its + * behavior differs between its use within a style definition and its use as + * direct formatting. When used as part of a style definition, setting this + * property shall toggle the current state of that property as specified up + * to this point in the hierarchy (i.e. applied to not applied, and vice + * versa). Setting it to false (or an equivalent) shall + * result in the current setting remaining unchanged. However, when used as + * direct formatting, setting this property to true or false shall set the + * absolute state of the resulting property. + *

+ *

+ * If this element is not present, the default value is to leave the + * formatting applied at previous level in the style hierarchy. If this + * element is never applied in the style hierarchy, then bold shall not be + * applied to non-complex script characters. + *

+ * + * @param value true if the italic property is applied to + * this run + */ + public void setItalic(boolean value) { + CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); + CTOnOff italic = pr.isSetI() ? pr.getI() : pr.addNewI(); + italic.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + } + + /** + * Specifies that the contents of this run should be displayed along with an + * underline appearing directly below the character heigh + * + * @return the Underline pattern applyed to this run + * @see UnderlinePatterns + */ + public UnderlinePatterns getUnderline() { + CTRPr pr = run.getRPr(); + return (pr != null && pr.isSetU()) ? UnderlinePatterns.valueOf(pr + .getU().getVal().intValue()) : null; + } + + /** + * Specifies that the contents of this run should be displayed along with an + * underline appearing directly below the character heigh + *

+ *

+ * If this element is not present, the default value is to leave the + * formatting applied at previous level in the style hierarchy. If this + * element is never applied in the style hierarchy, then an underline shall + * not be applied to the contents of this run. + *

+ * + * @param value - + * underline type + * @see UnderlinePatterns : all possible patterns that could be applied + */ + public void setUnderline(UnderlinePatterns value) { + CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); + CTUnderline underline = pr.isSetU() ? pr.getU() : pr.addNewU(); + underline.setVal(STUnderline.Enum.forInt(value.getValue())); + } + + /** + * Specifies that the contents of this run shall be displayed with a single + * horizontal line through the center of the line. + * + * @return true if the strike property is applied + */ + public boolean isStrike() { + CTRPr pr = run.getRPr(); + return pr != null && pr.isSetStrike(); + } + + /** + * Specifies that the contents of this run shall be displayed with a single + * horizontal line through the center of the line. + *

+ * This formatting property is a toggle property, which specifies that its + * behavior differs between its use within a style definition and its use as + * direct formatting. When used as part of a style definition, setting this + * property shall toggle the current state of that property as specified up + * to this point in the hierarchy (i.e. applied to not applied, and vice + * versa). Setting it to false (or an equivalent) shall result in the + * current setting remaining unchanged. However, when used as direct + * formatting, setting this property to true or false shall set the absolute + * state of the resulting property. + *

+ *

+ * If this element is not present, the default value is to leave the + * formatting applied at previous level in the style hierarchy. If this + * element is never applied in the style hierarchy, then strikethrough shall + * not be applied to the contents of this run. + *

+ * + * @param value true if the strike property is applied to + * this run + */ + public void setStrike(boolean value) { + CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); + CTOnOff strike = pr.isSetStrike() ? pr.getStrike() : pr.addNewStrike(); + strike.setVal(value ? STOnOff.TRUE : STOnOff.FALSE); + } + + /** + * Specifies the alignment which shall be applied to the contents of this + * run in relation to the default appearance of the run's text. + * This allows the text to be repositioned as subscript or superscript without + * altering the font size of the run properties. + * + * @return VerticalAlign + * @see VerticalAlign all possible value that could be applyed to this run + */ + public VerticalAlign getSubscript() { + CTRPr pr = run.getRPr(); + return (pr != null && pr.isSetVertAlign()) ? VerticalAlign.valueOf(pr + .getVertAlign().getVal().intValue()) : null; + } + + /** + * Specifies the alignment which shall be applied to the contents of this + * run in relation to the default appearance of the run's text. This allows + * the text to be repositioned as subscript or superscript without altering + * the font size of the run properties. + *

+ * If this element is not present, the default value is to leave the + * formatting applied at previous level in the style hierarchy. If this + * element is never applied in the style hierarchy, then the text shall not + * be subscript or superscript relative to the default baseline location for + * the contents of this run. + *

+ * + * @param valign + * @see VerticalAlign + */ + public void setSubscript(VerticalAlign valign) { + CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); + CTVerticalAlignRun ctValign = pr.isSetVertAlign() ? pr.getVertAlign() : pr.addNewVertAlign(); + ctValign.setVal(STVerticalAlignRun.Enum.forInt(valign.getValue())); + } + + /** + * 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 + * + * @return a string representing the font family + */ + public String getFontFamily() { + CTRPr pr = run.getRPr(); + return (pr != null && pr.isSetRFonts()) ? pr.getRFonts().getAscii() + : null; + } + + /** + * 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 + * + * @param fontFamily + */ + public void setFontFamily(String fontFamily) { + CTRPr pr = run.getRPr(); + CTFonts fonts = pr.isSetRFonts() ? pr.getRFonts() : pr.addNewRFonts(); + fonts.setAscii(fontFamily); + } + + /** + * Specifies the font size which shall be applied to all non complex script + * characters in the contents of this run when displayed. + * + * @return value representing the font size + */ + public BigInteger getFontSize() { + CTRPr pr = run.getRPr(); + return (pr != null && pr.isSetSz()) ? pr.getSz().getVal() : null; + } + + /** + * Specifies the font size which shall be applied to all non complex script + * characters in the contents of this run when displayed. + *

+ * If this element is not present, the default value is to leave the value + * applied at previous level in the style hierarchy. If this element is + * never applied in the style hierarchy, then any appropriate font size may + * be used for non complex script characters. + *

+ * + * @param size + */ + public void setFontSize(BigInteger size) { + CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); + CTHpsMeasure ctSize = pr.isSetSz() ? pr.getSz() : pr.addNewSz(); + ctSize.setVal(size); + } + + /** + * This element specifies the amount by which text shall be raised or + * lowered for this run in relation to the default baseline of the + * surrounding non-positioned text. This allows the text to be repositioned + * without altering the font size of the contents. + * + * @return a big integer representing the amount of text shall be "moved" + */ + public BigInteger getTextPosition() { + CTRPr pr = run.getRPr(); + return (pr != null && pr.isSetPosition()) ? pr.getPosition().getVal() + : null; + } + + /** + * This element specifies the amount by which text shall be raised or + * lowered for this run in relation to the default baseline of the + * surrounding non-positioned text. This allows the text to be repositioned + * without altering the font size of the contents. + *

+ * If the val attribute is positive, then the parent run shall be raised + * above the baseline of the surrounding text by the specified number of + * half-points. If the val attribute is negative, then the parent run shall + * be lowered below the baseline of the surrounding text by the specified + * number of half-points. + *

+ *

+ * If this element is not present, the default value is to leave the + * formatting applied at previous level in the style hierarchy. If this + * element is never applied in the style hierarchy, then the text shall not + * be raised or lowered relative to the default baseline location for the + * contents of this run. + *

+ * + * @param val + */ + public void setTextPosition(BigInteger val) { + CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); + CTSignedHpsMeasure position = pr.isSetPosition() ? pr.getPosition() : pr.addNewPosition(); + position.setVal(val); + } + +} diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java new file mode 100755 index 000000000..abe3a613c --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -0,0 +1,135 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xwpf.usermodel; + +import java.math.BigInteger; + +import junit.framework.TestCase; + +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; + +/** + * Tests for XWPF Run + */ +public class TestXWPFRun extends TestCase { + + public CTR ctRun; + public XWPFParagraph p; + + protected void setUp() { + XWPFDocument doc = new XWPFDocument(); + p = doc.createParagraph(); + + this.ctRun = CTR.Factory.newInstance(); + + } + + public void testSetGetBold() { + CTRPr rpr = ctRun.addNewRPr(); + rpr.addNewB().setVal(STOnOff.TRUE); + + XWPFRun run = new XWPFRun(ctRun, p); + assertEquals(true, run.isBold()); + + run.setBold(false); + assertEquals(STOnOff.FALSE, rpr.getB().getVal()); + } + + public void testSetGetItalic() { + CTRPr rpr = ctRun.addNewRPr(); + rpr.addNewI().setVal(STOnOff.TRUE); + + XWPFRun run = new XWPFRun(ctRun, p); + assertEquals(true, run.isItalic()); + + run.setItalic(false); + assertEquals(STOnOff.FALSE, rpr.getI().getVal()); + } + + public void testSetGetStrike() { + CTRPr rpr = ctRun.addNewRPr(); + rpr.addNewStrike().setVal(STOnOff.TRUE); + + XWPFRun run = new XWPFRun(ctRun, p); + assertEquals(true, run.isStrike()); + + run.setStrike(false); + assertEquals(STOnOff.FALSE, rpr.getStrike().getVal()); + } + + public void testSetGetUnderline() { + CTRPr rpr = ctRun.addNewRPr(); + rpr.addNewU().setVal(STUnderline.DASH); + + XWPFRun run = new XWPFRun(ctRun, p); + assertEquals(UnderlinePatterns.DASH.getValue(), run.getUnderline() + .getValue()); + + run.setUnderline(UnderlinePatterns.NONE); + assertEquals(STUnderline.NONE.intValue(), rpr.getU().getVal() + .intValue()); + } + + public void testSetGetVAlign() { + CTRPr rpr = ctRun.addNewRPr(); + rpr.addNewVertAlign().setVal(STVerticalAlignRun.SUBSCRIPT); + + XWPFRun run = new XWPFRun(ctRun, p); + assertEquals(VerticalAlign.SUBSCRIPT, run.getSubscript()); + + run.setSubscript(VerticalAlign.BASELINE); + assertEquals(STVerticalAlignRun.BASELINE, rpr.getVertAlign().getVal()); + } + + public void testSetGetFontFamily() { + CTRPr rpr = ctRun.addNewRPr(); + rpr.addNewRFonts().setAscii("Times New Roman"); + + XWPFRun run = new XWPFRun(ctRun, p); + assertEquals("Times New Roman", run.getFontFamily()); + + run.setFontFamily("Verdana"); + assertEquals("Verdana", rpr.getRFonts().getAscii()); + } + + public void testSetGetFontSize() { + CTRPr rpr = ctRun.addNewRPr(); + rpr.addNewSz().setVal(new BigInteger("4000")); + + XWPFRun run = new XWPFRun(ctRun, p); + assertEquals(4000, run.getFontSize().longValue()); + + run.setFontSize(new BigInteger("2400")); + assertEquals(2400, rpr.getSz().getVal().longValue()); + } + + public void testSetGetTextForegroundBackground() { + CTRPr rpr = ctRun.addNewRPr(); + rpr.addNewPosition().setVal(new BigInteger("4000")); + + XWPFRun run = new XWPFRun(ctRun, p); + assertEquals(4000, run.getTextPosition().longValue()); + + run.setTextPosition(new BigInteger("2400")); + assertEquals(2400, rpr.getPosition().getVal().longValue()); + } + +}