support for processing of symbols in HWPF, see Bugzilla 49908

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1005443 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2010-10-07 13:40:58 +00:00
parent adefc926c1
commit b53e0ba59c
4 changed files with 95 additions and 0 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.7-beta4" date="2010-??-??">
<action dev="poi-developers" type="fix">49908 - support for processing of symbols in HWPF</action>
<action dev="poi-developers" type="fix">50022 - support for retrieving pictures from HSSF workbooks</action>
<action dev="poi-developers" type="fix">50020 - Avoid IllegalStateException when creating Data validation in sheet with macro</action>
<action dev="poi-developers" type="fix">50033 - Improved rounding in MOD</action>

View File

@ -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.
*
* <p>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");
}
}

View File

@ -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());
}
}

BIN
test-data/document/Bug49908.doc Executable file

Binary file not shown.