This is an automated email from the ASF dual-hosted git repository. centic pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/poi.git
commit 1910fff53edf7eb5da010ff642a38392cce9704c Author: Dominik Stadler <[email protected]> AuthorDate: Fri Jan 16 08:56:42 2026 +0100 Add a file-handler for .wmf files These can be embedded in slideshows and POI supports some handling of them. Also Tika uses this code to extract some textual information from such images. --- .../org/apache/poi/stress/FileHandlerKnown.java | 1 + .../org/apache/poi/stress/HWMFFileHandler.java | 93 +++++++++++++++++++++ test-data/spreadsheet/stress.xls | Bin 75776 -> 76288 bytes 3 files changed, 94 insertions(+) diff --git a/poi-integration/src/test/java/org/apache/poi/stress/FileHandlerKnown.java b/poi-integration/src/test/java/org/apache/poi/stress/FileHandlerKnown.java index c3f1e11137..dc2af11ba9 100644 --- a/poi-integration/src/test/java/org/apache/poi/stress/FileHandlerKnown.java +++ b/poi-integration/src/test/java/org/apache/poi/stress/FileHandlerKnown.java @@ -29,6 +29,7 @@ public enum FileHandlerKnown { HSLF, HSMF, HSSF, + HWMF, HWPF, OPC, POIFS, diff --git a/poi-integration/src/test/java/org/apache/poi/stress/HWMFFileHandler.java b/poi-integration/src/test/java/org/apache/poi/stress/HWMFFileHandler.java new file mode 100644 index 0000000000..3429844bfb --- /dev/null +++ b/poi-integration/src/test/java/org/apache/poi/stress/HWMFFileHandler.java @@ -0,0 +1,93 @@ +/* ==================================================================== + 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.stress; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + +import java.awt.image.BufferedImage; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.nio.charset.Charset; + +import org.apache.poi.hwmf.record.HwmfFont; +import org.apache.poi.hwmf.record.HwmfRecord; +import org.apache.poi.hwmf.record.HwmfRecordType; +import org.apache.poi.hwmf.record.HwmfText; +import org.apache.poi.hwmf.usermodel.HwmfPicture; +import org.apache.poi.util.LocaleUtil; +import org.junit.jupiter.api.Test; + +public class HWMFFileHandler implements FileHandler { + + @Override + public void handleExtracting(File file) throws Exception { + try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) { + HwmfPicture picture = new HwmfPicture(stream); + Charset charset = LocaleUtil.CHARSET_1252; + + // mimic a bit what e.g. Tika does to extract some information from .wmf files + for (HwmfRecord record : picture.getRecords()) { + if (record.getWmfRecordType().equals(HwmfRecordType.createFontIndirect)) { + HwmfFont font = ((HwmfText.WmfCreateFontIndirect) record).getFont(); + charset = (font.getCharset() == null || font.getCharset().getCharset() == null) ? + LocaleUtil.CHARSET_1252 : font.getCharset().getCharset(); + } + + if (record.getWmfRecordType().equals(HwmfRecordType.extTextOut)) { + assertInstanceOf(HwmfText.WmfExtTextOut.class, record); + HwmfText.WmfExtTextOut textOut = (HwmfText.WmfExtTextOut) record; + textOut.getText(charset); + } else if (record.getWmfRecordType().equals(HwmfRecordType.textOut)) { + assertInstanceOf(HwmfText.WmfTextOut.class, record); + HwmfText.WmfTextOut textOut = (HwmfText.WmfTextOut) record; + textOut.getText(charset); + } + } + } + } + + @Override + public void handleAdditional(File file) throws Exception { + // no additional checks for now + } + + @Override + public void handleFile(InputStream stream, String path) throws Exception { + HwmfPicture picture = new HwmfPicture(stream); + + for (HwmfRecord record : picture.getRecords()) { + record.getWmfRecordType(); + record.getGenericRecordType(); + } + + BufferedImage dest = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); + picture.draw(dest.createGraphics()); + } + + @Test + void test() throws Exception { + String file = "test-data/slideshow/santa.wmf"; + + try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) { + handleFile(stream, file); + } + + handleExtracting(new File(file)); + } +} diff --git a/test-data/spreadsheet/stress.xls b/test-data/spreadsheet/stress.xls index 17d8e2afca..a9d24e8244 100644 Binary files a/test-data/spreadsheet/stress.xls and b/test-data/spreadsheet/stress.xls differ --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
