Example usage for org.apache.poi.hwmf.record HwmfFont getCharset

List of usage examples for org.apache.poi.hwmf.record HwmfFont getCharset

Introduction

In this page you can find the example usage for org.apache.poi.hwmf.record HwmfFont getCharset.

Prototype

@Override
    public FontCharset getCharset() 

Source Link

Usage

From source file:org.apache.tika.parser.microsoft.WMFParser.java

License:Apache License

@Override
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();// w w  w .  j  a v  a 2 s .  com
    try {
        HwmfPicture picture = new HwmfPicture(stream);
        //TODO: make x/y info public in POI so that we can use it here
        //to determine when to keep two text parts on the same line
        for (HwmfRecord record : picture.getRecords()) {
            Charset charset = LocaleUtil.CHARSET_1252;
            //this is pure hackery for specifying the font
            //TODO: do what Graphics does by maintaining the stack, etc.!
            //This fix should be done within POI
            if (record.getRecordType().equals(HwmfRecordType.createFontIndirect)) {
                HwmfFont font = ((HwmfText.WmfCreateFontIndirect) record).getFont();
                charset = (font.getCharSet().getCharset() == null) ? LocaleUtil.CHARSET_1252
                        : font.getCharSet().getCharset();
            }
            if (record.getRecordType().equals(HwmfRecordType.extTextOut)) {
                HwmfText.WmfExtTextOut textOut = (HwmfText.WmfExtTextOut) record;
                xhtml.startElement("p");
                xhtml.characters(textOut.getText(charset));
                xhtml.endElement("p");
            } else if (record.getRecordType().equals(HwmfRecordType.textOut)) {
                HwmfText.WmfTextOut textOut = (HwmfText.WmfTextOut) record;
                xhtml.startElement("p");
                xhtml.characters(textOut.getText(charset));
                xhtml.endElement("p");
            }
        }
    } catch (RecordFormatException e) { //POI's hwmfparser can throw these for "parse exceptions"
        throw new TikaException(e.getMessage());
    } catch (RuntimeException e) { //convert Runtime to RecordFormatExceptions
        throw new TikaException(e.getMessage());
    } catch (AssertionError e) { //POI's hwmfparser can throw these for parse exceptions
        throw new TikaException(e.getMessage());
    }
    xhtml.endDocument();
}