Skip to content

Commit

Permalink
Merge pull request #1057 from xiaokaRepo/add-svg-to-png-scale-config
Browse files Browse the repository at this point in the history
新增svg转png缩放比例配置
  • Loading branch information
Sayi authored Jan 24, 2024
2 parents 6dbec33 + 1e01af4 commit 844672e
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 62 deletions.
14 changes: 10 additions & 4 deletions poi-tl/src/main/java/com/deepoove/poi/data/Pictures.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*/
package com.deepoove.poi.data;

import java.awt.image.BufferedImage;
import java.io.InputStream;

import com.deepoove.poi.data.style.PictureStyle;
import com.deepoove.poi.data.style.PictureStyle.PictureAlign;
import com.deepoove.poi.util.BufferedImageUtils;
import com.deepoove.poi.util.ByteUtils;
import com.deepoove.poi.util.UnitUtils;
import com.deepoove.poi.xwpf.WidthScalePattern;

import java.awt.image.BufferedImage;
import java.io.InputStream;

/**
* Factory method to build {@link PictureRenderData} instances.
*
*
* @author Sayi
*/
public class Pictures {
Expand Down Expand Up @@ -97,6 +97,12 @@ public PictureBuilder size(int width, int height) {
return this;
}

public PictureBuilder svgScale(int svgScale) {
PictureStyle style = getPictureStyle();
style.setSvgScale(svgScale);
return this;
}

private PictureStyle getPictureStyle() {
PictureStyle style = data.getPictureStyle();
if (null == style) {
Expand Down
17 changes: 14 additions & 3 deletions poi-tl/src/main/java/com/deepoove/poi/data/style/PictureStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
*/
package com.deepoove.poi.data.style;

import java.io.Serializable;

import com.deepoove.poi.xwpf.WidthScalePattern;

import java.io.Serializable;

/**
* @author Sayi
*
*/
public class PictureStyle implements Serializable {

Expand All @@ -42,6 +41,11 @@ public static enum PictureAlign {
RIGHT;
}

/**
* This is used for setting the scaling factor for SVG to PNG conversion
*/
private int svgScale = 1;

public PictureAlign getAlign() {
return align;
}
Expand Down Expand Up @@ -74,4 +78,11 @@ public void setScalePattern(WidthScalePattern scalePattern) {
this.scalePattern = scalePattern;
}

public int getSvgScale() {
return svgScale;
}

public void setSvgScale(int svgScale) {
this.svgScale = svgScale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@
*/
package com.deepoove.poi.policy;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import com.deepoove.poi.converter.ObjectToPictureRenderDataConverter;
import com.deepoove.poi.converter.ToRenderDataConverter;
import com.deepoove.poi.data.PictureRenderData;
Expand All @@ -39,10 +29,19 @@
import com.deepoove.poi.xwpf.BodyContainer;
import com.deepoove.poi.xwpf.BodyContainerFactory;
import com.deepoove.poi.xwpf.WidthScalePattern;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
* picture render
*
*
* @author Sayi
*/
public class PictureRenderPolicy extends AbstractRenderPolicy<PictureRenderData> {
Expand Down Expand Up @@ -94,9 +93,10 @@ public static void renderPicture(XWPFRun run, PictureRenderData picture) throws
if (null == style) style = new PictureStyle();
int width = style.getWidth();
int height = style.getHeight();
int svgScale = style.getSvgScale();

if (pictureType == PictureType.SVG) {
imageBytes = SVGConvertor.toPng(imageBytes, (float) width, (float) height);
imageBytes = SVGConvertor.toPng(imageBytes, (float) width, (float) height, svgScale);
pictureType = PictureType.PNG;
}
if (!isSetSize(style)) {
Expand All @@ -106,7 +106,7 @@ public static void renderPicture(XWPFRun run, PictureRenderData picture) throws
if (style.getScalePattern() == WidthScalePattern.FIT) {
BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);
int pageWidth = UnitUtils
.twips2Pixel(bodyContainer.elementPageWidth((IBodyElement) run.getParent()));
.twips2Pixel(bodyContainer.elementPageWidth((IBodyElement) run.getParent()));
if (width > pageWidth) {
double ratio = pageWidth / (double) width;
width = pageWidth;
Expand All @@ -120,13 +120,13 @@ public static void renderPicture(XWPFRun run, PictureRenderData picture) throws
((XWPFParagraph) run.getParent()).setAlignment(ParagraphAlignment.valueOf(align.ordinal() + 1));
}
run.addPicture(stream, pictureType.type(), "Generated", Units.pixelToEMU(width),
Units.pixelToEMU(height));
Units.pixelToEMU(height));
}
}

private static boolean isSetSize(PictureStyle style) {
return (style.getWidth() != 0 || style.getHeight() != 0)
&& style.getScalePattern() == WidthScalePattern.NONE;
&& style.getScalePattern() == WidthScalePattern.NONE;
}
}
}
30 changes: 14 additions & 16 deletions poi-tl/src/main/java/com/deepoove/poi/util/SVGConvertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,22 @@
*/
package com.deepoove.poi.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.batik.transcoder.ErrorHandler;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import com.deepoove.poi.exception.RenderException;
import org.apache.batik.transcoder.*;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.deepoove.poi.exception.RenderException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class SVGConvertor {

private static Logger logger = LoggerFactory.getLogger(SVGConvertor.class);

public static byte[] toPng(byte[] svgs, float width, float maxHeight) throws TranscoderException, IOException {
public static byte[] toPng(byte[] svgs, float width, float maxHeight,
int svgScale) throws TranscoderException, IOException {
Transcoder t = new PNGTranscoder();
t.setErrorHandler(new ErrorHandler() {

Expand All @@ -53,18 +49,20 @@ public void error(TranscoderException ex) throws TranscoderException {
throw ex;
}
});
if (0 != width) t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, width);
if (0 != maxHeight) t.addTranscodingHint(PNGTranscoder.KEY_MAX_HEIGHT, maxHeight);
try (ByteArrayInputStream instream = new ByteArrayInputStream(svgs);
ByteArrayOutputStream ostream = new ByteArrayOutputStream()) {
if (0 != width)
t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, width * svgScale);
if (0 != maxHeight)
t.addTranscodingHint(PNGTranscoder.KEY_MAX_HEIGHT, maxHeight * svgScale);
try (ByteArrayInputStream instream = new ByteArrayInputStream(svgs); ByteArrayOutputStream ostream =
new ByteArrayOutputStream()) {
TranscoderInput input = new TranscoderInput(instream);
TranscoderOutput output = new TranscoderOutput(ostream);
t.transcode(input, output);
ostream.flush();
return ostream.toByteArray();
} catch (Exception e) {
throw new RenderException("Unable transcode from svg to png, possibly some svg attribute is not supported.",
e);
e);
}
}

Expand Down

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions poi-tl/src/test/resources/picture/svg_scale.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

0 comments on commit 844672e

Please sign in to comment.