diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2020-08-30 11:18:21 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2020-08-30 11:18:21 +0000 |
commit | b00ca445b2eee110df17fba5f8afd64260c16174 (patch) | |
tree | 049f6ecfd3b8dd02bc15f53b04679b7210c8a295 /src/ooxml | |
parent | f89528addc51729a438eb5732129b87c2d87b4b9 (diff) | |
download | poi-b00ca445b2eee110df17fba5f8afd64260c16174.tar.gz poi-b00ca445b2eee110df17fba5f8afd64260c16174.zip |
#64693 - POI HwmfGraphics cannot read the embedded document title
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1881322 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml')
6 files changed, 69 insertions, 13 deletions
diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java index 1c3890a159..b57e2da602 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTextRun.java @@ -738,7 +738,9 @@ public class XSLFTextRun implements TextRun { // SYMBOL is missing if (font == null || !font.isSetTypeface() || "".equals(font.getTypeface())) { - font = coll.getLatin(); + // don't fallback to latin but bubble up in the style hierarchy (slide -> layout -> master -> theme) + return null; +// font = coll.getLatin(); } } diff --git a/src/ooxml/java/org/apache/poi/xslf/util/EMFHandler.java b/src/ooxml/java/org/apache/poi/xslf/util/EMFHandler.java index 9d7e620bb4..761c819612 100644 --- a/src/ooxml/java/org/apache/poi/xslf/util/EMFHandler.java +++ b/src/ooxml/java/org/apache/poi/xslf/util/EMFHandler.java @@ -25,6 +25,7 @@ import java.awt.geom.Rectangle2D; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.Collections; import org.apache.poi.common.usermodel.GenericRecord; @@ -111,4 +112,9 @@ class EMFHandler extends MFProxy { ? ((EmbeddedExtractor) imgr).getEmbeddings() : Collections.emptyList(); } + + @Override + void setDefaultCharset(Charset charset) { + imgr.setDefaultCharset(charset); + } } diff --git a/src/ooxml/java/org/apache/poi/xslf/util/MFProxy.java b/src/ooxml/java/org/apache/poi/xslf/util/MFProxy.java index 435fdb3e78..5a02c63695 100644 --- a/src/ooxml/java/org/apache/poi/xslf/util/MFProxy.java +++ b/src/ooxml/java/org/apache/poi/xslf/util/MFProxy.java @@ -25,6 +25,7 @@ import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.Collections; import java.util.Set; @@ -64,4 +65,6 @@ abstract class MFProxy implements Closeable { abstract GenericRecord getRoot(); abstract Iterable<EmbeddedPart> getEmbeddings(int slideNo); + + abstract void setDefaultCharset(Charset charset); } diff --git a/src/ooxml/java/org/apache/poi/xslf/util/PPTHandler.java b/src/ooxml/java/org/apache/poi/xslf/util/PPTHandler.java index debb34f005..79d676ddc0 100644 --- a/src/ooxml/java/org/apache/poi/xslf/util/PPTHandler.java +++ b/src/ooxml/java/org/apache/poi/xslf/util/PPTHandler.java @@ -27,6 +27,7 @@ import java.awt.geom.Dimension2D; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.Set; import java.util.Spliterator; import java.util.Spliterators; @@ -49,12 +50,14 @@ import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShowFactory; import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; +import org.apache.poi.util.LocaleUtil; /** Handler for ppt and pptx files */ @Internal class PPTHandler extends MFProxy { private SlideShow<?,?> ppt; private Slide<?,?> slide; + private Charset defaultCharset = LocaleUtil.CHARSET_1252; @Override public void parse(File file) throws IOException { @@ -67,6 +70,9 @@ class PPTHandler extends MFProxy { throw e; } } + if (ppt == null) { + throw new IOException("Unknown file format or missing poi-scratchpad.jar / poi-ooxml.jar"); + } slide = ppt.getSlides().get(0); } @@ -81,6 +87,9 @@ class PPTHandler extends MFProxy { throw e; } } + if (ppt == null) { + throw new IOException("Unknown file format or missing poi-scratchpad.jar / poi-ooxml.jar"); + } slide = ppt.getSlides().get(0); } @@ -162,8 +171,8 @@ class PPTHandler extends MFProxy { ; } - private static EmbeddedPart fromObjectShape(Shape s) { - final ObjectShape os = (ObjectShape)s; + private static EmbeddedPart fromObjectShape(Shape<?,?> s) { + final ObjectShape<?,?> os = (ObjectShape<?,?>)s; final ObjectData od = os.getObjectData(); EmbeddedPart embed = new EmbeddedPart(); embed.setName(od.getFileName()); @@ -177,4 +186,8 @@ class PPTHandler extends MFProxy { }); return embed; } + + @Override + void setDefaultCharset(Charset charset) { + } } diff --git a/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java b/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java index e3bbf62001..384255c57a 100644 --- a/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java +++ b/src/ooxml/java/org/apache/poi/xslf/util/PPTX2PNG.java @@ -27,15 +27,18 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.Locale; import java.util.Set; import java.util.regex.Pattern; import org.apache.poi.common.usermodel.GenericRecord; import org.apache.poi.poifs.filesystem.FileMagic; +import org.apache.poi.sl.draw.Drawable; import org.apache.poi.sl.draw.EmbeddedExtractor.EmbeddedPart; import org.apache.poi.util.Dimension2DDouble; import org.apache.poi.util.GenericRecordJsonWriter; +import org.apache.poi.util.LocaleUtil; import org.apache.poi.xslf.util.OutputFormat.BitmapFormat; import org.apache.poi.xslf.util.OutputFormat.SVGFormat; @@ -71,7 +74,8 @@ public final class PPTX2PNG { " -inputType <type> default input file type (OLE2,WMF,EMF), default is OLE2 = Powerpoint\n" + " some files (usually wmf) don't have a header, i.e. an identifiable file magic\n" + " -textAsShapes text elements are saved as shapes in SVG, necessary for variable spacing\n" + - " often found in math formulas"; + " often found in math formulas\n" + + " -charset sets the default charset to be used, defaults to Windows-1252"; System.out.println(msg); // no System.exit here, as we also run in junit tests! @@ -99,6 +103,7 @@ public final class PPTX2PNG { private boolean extractEmbedded = false; private FileMagic defaultFileType = FileMagic.OLE2; private boolean textAsShapes = false; + private Charset charset = LocaleUtil.CHARSET_1252; private PPTX2PNG() { } @@ -176,6 +181,15 @@ public final class PPTX2PNG { case "-extractEmbedded": extractEmbedded = true; break; + case "-charset": + if (opt != null) { + charset = Charset.forName(opt); + i++; + } else { + charset = LocaleUtil.CHARSET_1252; + } + break; + default: file = new File(args[i]); break; @@ -264,6 +278,7 @@ public final class PPTX2PNG { graphics.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); + graphics.setRenderingHint(Drawable.DEFAULT_CHARSET, getDefaultCharset()); graphics.scale(scale / lenSide, scale / lenSide); @@ -315,7 +330,7 @@ public final class PPTX2PNG { } private void dumpRecords(MFProxy proxy) throws IOException { - if (dumpfile == null) { + if (dumpfile == null || "null".equals(dumpfile.getPath())) { return; } GenericRecord gr = proxy.getRoot(); @@ -387,6 +402,7 @@ public final class PPTX2PNG { proxy.setQuite(quiet); proxy.parse(file); } + proxy.setDefaultCharset(charset); return proxy; } @@ -400,6 +416,10 @@ public final class PPTX2PNG { return INPUT_PATTERN.matcher(inname).replaceAll(outpat); } + private Charset getDefaultCharset() { + return charset; + } + static class NoScratchpadException extends IOException { NoScratchpadException() { } diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java index 96d30601e5..df23f14f28 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestPPTX2PNG.java @@ -19,10 +19,13 @@ package org.apache.poi.xslf.usermodel; +import static java.util.Arrays.asList; import static org.junit.Assume.assumeFalse; import java.io.File; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -39,13 +42,14 @@ import org.junit.runners.Parameterized.Parameters; /** * Test class for testing PPTX2PNG utility which renders .ppt and .pptx slideshows */ +@SuppressWarnings("ConstantConditions") @RunWith(Parameterized.class) public class TestPPTX2PNG { private static boolean xslfOnly; private static final POIDataSamples samples = POIDataSamples.getSlideShowInstance(); private static final File basedir = null; private static final String files = - "53446.ppt, alterman_security.ppt, alterman_security.pptx, KEY02.pptx, themes.pptx, " + + "bug64693.pptx, 53446.ppt, alterman_security.ppt, alterman_security.pptx, KEY02.pptx, themes.pptx, " + "backgrounds.pptx, layouts.pptx, sample.pptx, shapes.pptx, 54880_chinese.ppt, keyframes.pptx," + "customGeo.pptx, customGeo.ppt, wrench.emf, santa.wmf, missing-moveto.ppt"; @@ -62,7 +66,6 @@ public class TestPPTX2PNG { @Parameter public String pptFile; - @SuppressWarnings("ConstantConditions") @Parameters(name="{0}") public static Collection<String> data() { Function<String, Stream<String>> fun = (basedir == null) ? Stream::of : @@ -75,7 +78,9 @@ public class TestPPTX2PNG { public void render() throws Exception { assumeFalse("ignore HSLF (.ppt) / HEMF (.emf) / HWMF (.wmf) files in no-scratchpad run", xslfOnly && pptFile.matches(".*\\.(ppt|emf|wmf)$")); - String[] args = { + // bug64693.pptx + + final List<String> args = new ArrayList<>(asList( "-format", "null", // png,gif,jpg,svg or null for test "-slide", "-1", // -1 for all "-outdir", new File("build/tmp/").getCanonicalPath(), @@ -84,10 +89,17 @@ public class TestPPTX2PNG { "-dump", "null", "-quiet", "-fixside", "long", - "-scale", "800", - // "-scale", "1.333333333", - (basedir == null ? samples.getFile(pptFile) : new File(basedir, pptFile)).getAbsolutePath() - }; - PPTX2PNG.main(args); + "-scale", "800" + )); + + if ("bug64693.pptx".equals(pptFile)) { + args.addAll(asList( + "-charset", "GBK" + )); + } + + args.add((basedir == null ? samples.getFile(pptFile) : new File(basedir, pptFile)).getAbsolutePath()); + + PPTX2PNG.main(args.toArray(new String[0])); } } |