Skip to content

Commit

Permalink
Prepare for new Batik version
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldk committed Aug 23, 2023
1 parent 608b372 commit aab2d36
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public class SVGImageReader extends ImageReaderBase {
/**
* Creates an {@code SVGImageReader}.
*
* @param pProvider the provider
* @param provider the provider
*/
public SVGImageReader(final ImageReaderSpi pProvider) {
super(pProvider);
public SVGImageReader(final ImageReaderSpi provider) {
super(provider);
}

protected void resetMembers() {
Expand All @@ -108,20 +108,20 @@ public void dispose() {
}

@Override
public void setInput(Object pInput, boolean seekForwardOnly, boolean ignoreMetadata) {
super.setInput(pInput, seekForwardOnly, ignoreMetadata);
public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) {
super.setInput(input, seekForwardOnly, ignoreMetadata);

if (imageInput != null) {
TranscoderInput input = new TranscoderInput(IIOUtil.createStreamAdapter(imageInput));
rasterizer.setInput(input);
TranscoderInput transcoderInput = new TranscoderInput(IIOUtil.createStreamAdapter(imageInput));
rasterizer.setInput(transcoderInput);
}
}

public BufferedImage read(int pIndex, ImageReadParam pParam) throws IOException {
checkBounds(pIndex);
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
checkBounds(imageIndex);

if (pParam instanceof SVGReadParam) {
SVGReadParam svgParam = (SVGReadParam) pParam;
if (param instanceof SVGReadParam) {
SVGReadParam svgParam = (SVGReadParam) param;

// set the external-resource-resolution preference
allowExternalResources = svgParam.isAllowExternalResources();
Expand All @@ -139,17 +139,17 @@ public BufferedImage read(int pIndex, ImageReadParam pParam) throws IOException
}

Dimension size = null;
if (pParam != null) {
size = pParam.getSourceRenderSize();
if (param != null) {
size = param.getSourceRenderSize();
}
if (size == null) {
size = new Dimension(getWidth(pIndex), getHeight(pIndex));
size = new Dimension(getWidth(imageIndex), getHeight(imageIndex));
}

BufferedImage destination = getDestination(pParam, getImageTypes(pIndex), size.width, size.height);
BufferedImage destination = getDestination(param, getImageTypes(imageIndex), size.width, size.height);

// Read in the image, using the Batik Transcoder
processImageStarted(pIndex);
processImageStarted(imageIndex);

BufferedImage image = rasterizer.getImage();

Expand All @@ -173,18 +173,18 @@ private static Throwable unwrapException(TranscoderException ex) {
return ex.getException() != null ? ex.getException() : ex;
}

private TranscodingHints paramsToHints(SVGReadParam pParam) throws IOException {
private TranscodingHints paramsToHints(SVGReadParam param) throws IOException {
TranscodingHints hints = new TranscodingHints();
// Note: We must allow generic ImageReadParams, so converting to
// TanscodingHints should be done outside the SVGReadParam class.

// Set dimensions
Dimension size = pParam.getSourceRenderSize();
Dimension size = param.getSourceRenderSize();
Rectangle viewBox = rasterizer.getViewBox();
if (size == null) {
// SVG is not a pixel based format, but we'll scale it, according to
// the subsampling for compatibility
size = getSourceRenderSizeFromSubsamping(pParam, viewBox.getSize());
size = getSourceRenderSizeFromSubsamping(param, viewBox.getSize());
}

if (size != null) {
Expand All @@ -193,7 +193,7 @@ private TranscodingHints paramsToHints(SVGReadParam pParam) throws IOException {
}

// Set area of interest
Rectangle region = pParam.getSourceRegion();
Rectangle region = param.getSourceRegion();
if (region != null) {
hints.put(ImageTranscoder.KEY_AOI, region);

Expand All @@ -217,18 +217,18 @@ else if (size != null) {
}

// Background color
Paint bg = pParam.getBackgroundColor();
Paint bg = param.getBackgroundColor();
if (bg != null) {
hints.put(ImageTranscoder.KEY_BACKGROUND_COLOR, bg);
}

return hints;
}

private Dimension getSourceRenderSizeFromSubsamping(ImageReadParam pParam, Dimension pOrigSize) {
if (pParam.getSourceXSubsampling() > 1 || pParam.getSourceYSubsampling() > 1) {
return new Dimension((int) (pOrigSize.width / (float) pParam.getSourceXSubsampling()),
(int) (pOrigSize.height / (float) pParam.getSourceYSubsampling()));
private Dimension getSourceRenderSizeFromSubsamping(ImageReadParam param, Dimension origSize) {
if (param.getSourceXSubsampling() > 1 || param.getSourceYSubsampling() > 1) {
return new Dimension((int) (origSize.width / (float) param.getSourceXSubsampling()),
(int) (origSize.height / (float) param.getSourceYSubsampling()));
}
return null;
}
Expand All @@ -237,14 +237,14 @@ public SVGReadParam getDefaultReadParam() {
return new SVGReadParam();
}

public int getWidth(int pIndex) throws IOException {
checkBounds(pIndex);
public int getWidth(int imageIndex) throws IOException {
checkBounds(imageIndex);

return rasterizer.getDefaultWidth();
}

public int getHeight(int pIndex) throws IOException {
checkBounds(pIndex);
public int getHeight(int imageIndex) throws IOException {
checkBounds(imageIndex);
return rasterizer.getDefaultHeight();
}

Expand Down Expand Up @@ -601,6 +601,7 @@ private synchronized void init() throws IIOException {
initialized = true;

try {
super.addTranscodingHint(SVGAbstractTranscoder.KEY_ALLOW_EXTERNAL_RESOURCES, allowExternalResources);
super.transcode(transcoderInput, null);
}
catch (TranscoderException e) {
Expand Down Expand Up @@ -633,8 +634,8 @@ Rectangle getViewBox() throws IOException {
return viewBox.getBounds();
}

void setInput(final TranscoderInput pInput) {
transcoderInput = pInput;
void setInput(final TranscoderInput input) {
transcoderInput = input;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ public SVGImageReaderSpi() {
super(new SVGProviderInfo());
}

public boolean canDecodeInput(final Object pSource) throws IOException {
return pSource instanceof ImageInputStream && canDecode((ImageInputStream) pSource);
public boolean canDecodeInput(final Object source) throws IOException {
return source instanceof ImageInputStream && canDecode((ImageInputStream) source);
}

@SuppressWarnings("StatementWithEmptyBody")
private static boolean canDecode(final ImageInputStream pInput) throws IOException {
private static boolean canDecode(final ImageInputStream input) throws IOException {
// NOTE: This test is quite quick as it does not involve any parsing,
// however it may not recognize all kinds of SVG documents.
try {
pInput.mark();
input.mark();

// TODO: This is not ok for UTF-16 and other wide encodings
// TODO: Use an XML (encoding) aware Reader instance instead
// Need to figure out pretty fast if this is XML or not
int b;
while (Character.isWhitespace((char) (b = pInput.read()))) {
while (Character.isWhitespace((char) (b = input.read()))) {
// Skip over leading WS
}

Expand All @@ -95,30 +95,30 @@ private static boolean canDecode(final ImageInputStream pInput) throws IOExcepti

byte[] buffer = new byte[4];
while (true) {
pInput.readFully(buffer);
input.readFully(buffer);

if (buffer[0] == '?') {
// This is the XML declaration or a processing instruction
while (!((pInput.readByte() & 0xFF) == '?' && pInput.read() == '>')) {
while (!((input.readByte() & 0xFF) == '?' && input.read() == '>')) {
// Skip until end of XML declaration or processing instruction or EOF
}
}
else if (buffer[0] == '!') {
if (buffer[1] == '-' && buffer[2] == '-') {
// This is a comment
while (!((pInput.readByte() & 0xFF) == '-' && pInput.read() == '-' && pInput.read() == '>')) {
while (!((input.readByte() & 0xFF) == '-' && input.read() == '-' && input.read() == '>')) {
// Skip until end of comment or EOF
}
}
else if (buffer[1] == 'D' && buffer[2] == 'O' && buffer[3] == 'C'
&& pInput.read() == 'T' && pInput.read() == 'Y'
&& pInput.read() == 'P' && pInput.read() == 'E') {
&& input.read() == 'T' && input.read() == 'Y'
&& input.read() == 'P' && input.read() == 'E') {
// This is the DOCTYPE declaration
while (Character.isWhitespace((char) (b = pInput.read()))) {
while (Character.isWhitespace((char) (b = input.read()))) {
// Skip over WS
}

if (b == 's' && pInput.read() == 'v' && pInput.read() == 'g') {
if (b == 's' && input.read() == 'v' && input.read() == 'g') {
// It's SVG, identified by DOCTYPE
return true;
}
Expand All @@ -142,7 +142,7 @@ else if (buffer[1] == 'D' && buffer[2] == 'O' && buffer[3] == 'C'
return false;
}

while ((pInput.readByte() & 0xFF) != '<') {
while ((input.readByte() & 0xFF) != '<') {
// Skip over, until next begin tag or EOF
}
}
Expand All @@ -153,7 +153,7 @@ else if (buffer[1] == 'D' && buffer[2] == 'O' && buffer[3] == 'C'
}
finally {
//noinspection ThrowFromFinallyBlock
pInput.reset();
input.reset();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ public Paint getBackgroundColor() {
return background;
}

public void setBackgroundColor(Paint pColor) {
background = pColor;
public void setBackgroundColor(Paint color) {
background = color;
}

public String getBaseURI() {
return baseURI;
}

public void setBaseURI(String pBaseURI) {
baseURI = pBaseURI;
public void setBaseURI(String baseURI) {
this.baseURI = baseURI;
}

public void setAllowExternalResources(boolean allow) {
Expand Down

0 comments on commit aab2d36

Please sign in to comment.