diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 8793b5e98..94691ec8f 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49908 - support for processing of symbols in HWPF 50022 - support for retrieving pictures from HSSF workbooks 50020 - Avoid IllegalStateException when creating Data validation in sheet with macro 50033 - Improved rounding in MOD diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java index 2b6d41bb3..076b0029a 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/CharacterRun.java @@ -18,6 +18,7 @@ package org.apache.poi.hwpf.usermodel; import org.apache.poi.hwpf.model.CHPX; +import org.apache.poi.hwpf.model.Ffn; import org.apache.poi.hwpf.model.StyleSheet; import org.apache.poi.hwpf.sprm.SprmBuffer; @@ -557,5 +558,50 @@ public final class CharacterRun return cp; } + + /** + * Returns true, if the CharacterRun is a special character run containing a symbol, otherwise false. + * + *

In case of a symbol, the {@link #text()} method always returns a single character 0x0028, but word actually stores + * the character in a different field. Use {@link #getSymbolCharacter()} to get that character and {@link #getSymbolFont()} + * to determine its font. + */ + public boolean isSymbol() + { + return isSpecialCharacter() && text().equals("\u0028"); + } + + /** + * Returns the symbol character, if this is a symbol character run. + * + * @see #isSymbol() + * @throws IllegalStateException If this is not a symbol character run: call {@link #isSymbol()} first. + */ + public char getSymbolCharacter() + { + if (isSymbol()) { + return (char)_props.getXchSym(); + } else + throw new IllegalStateException("Not a symbol CharacterRun"); + } + + /** + * Returns the symbol font, if this is a symbol character run. Might return null, if the font index is not found in the font table. + * + * @see #isSymbol() + * @throws IllegalStateException If this is not a symbol character run: call {@link #isSymbol()} first. + */ + public Ffn getSymbolFont() + { + if (isSymbol()) { + Ffn[] fontNames = _doc.getFontTable().getFontNames(); + + if (fontNames.length <= _props.getFtcSym()) + return null; + + return fontNames[_props.getFtcSym()]; + } else + throw new IllegalStateException("Not a symbol CharacterRun"); + } } diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeSymbols.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeSymbols.java new file mode 100755 index 000000000..bc1d9979a --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestRangeSymbols.java @@ -0,0 +1,48 @@ +/* ==================================================================== + 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.hwpf.usermodel; + +import java.io.IOException; + +import junit.framework.TestCase; + +import org.apache.poi.hwpf.HWPFDocument; +import org.apache.poi.hwpf.HWPFTestDataSamples; + +/** + * API for processing of symbols, see Bugzilla 49908 + */ +public final class TestRangeSymbols extends TestCase { + + public void test() throws IOException { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug49908.doc"); + + Range range = doc.getRange(); + + assertTrue(range.numCharacterRuns() >= 2); + CharacterRun chr = range.getCharacterRun(0); + assertEquals(false, chr.isSymbol()); + + chr = range.getCharacterRun(1); + assertEquals(true, chr.isSymbol()); + assertEquals("\u0028", chr.text()); + assertEquals("Wingdings", chr.getSymbolFont().getMainFontName()); + assertEquals(0xf028, chr.getSymbolCharacter()); + } + +} \ No newline at end of file diff --git a/test-data/document/Bug49908.doc b/test-data/document/Bug49908.doc new file mode 100755 index 000000000..6a89732e7 Binary files /dev/null and b/test-data/document/Bug49908.doc differ