diff --git a/images/src/org/intellij/images/util/ImageInfoReader.java b/images/src/org/intellij/images/util/ImageInfoReader.java index 4ba6e8c12d3d..14aacd85e2a3 100644 --- a/images/src/org/intellij/images/util/ImageInfoReader.java +++ b/images/src/org/intellij/images/util/ImageInfoReader.java @@ -16,6 +16,7 @@ package org.intellij.images.util; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.util.ImageLoader; import com.intellij.util.SVGLoader; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -24,7 +25,6 @@ import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.ImageTypeSpecifier; import javax.imageio.stream.ImageInputStream; -import java.awt.geom.Dimension2D; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.Arrays; @@ -72,7 +72,7 @@ public class ImageInfoReader { private static Info getSvgSize(byte[] data) { try { - Dimension2D size = SVGLoader.getDocumentSize(null, new ByteArrayInputStream(data), 1.0f); + ImageLoader.Dimension2DDouble size = SVGLoader.getDocumentSize(null, new ByteArrayInputStream(data), 1.0f); return new Info((int)Math.round(size.getWidth()), (int)Math.round(size.getHeight()), 32, true); } catch (Throwable e) { diff --git a/platform/platform-tests/testSrc/com/intellij/util/ui/SvgIconSizeTest.java b/platform/platform-tests/testSrc/com/intellij/util/ui/SvgIconSizeTest.java index 13b38a9fc6d0..a72523c6d46d 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/ui/SvgIconSizeTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/ui/SvgIconSizeTest.java @@ -5,13 +5,13 @@ import com.intellij.testFramework.PlatformTestUtil; import com.intellij.ui.RestoreScaleRule; import com.intellij.ui.scale.JBUIScale; import com.intellij.ui.scale.ScaleContext; +import com.intellij.util.ImageLoader; import com.intellij.util.SVGLoader; import org.junit.ClassRule; import org.junit.Test; import org.junit.rules.ExternalResource; import java.awt.*; -import java.awt.geom.Dimension2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -67,7 +67,7 @@ public class SvgIconSizeTest { * Test SVGLoader.getDocumentSize for SVG starting with - diff --git a/platform/util/ui/src/com/intellij/util/ImageLoader.java b/platform/util/ui/src/com/intellij/util/ImageLoader.java index ce9edb3c54c7..eed81840bd58 100644 --- a/platform/util/ui/src/com/intellij/util/ImageLoader.java +++ b/platform/util/ui/src/com/intellij/util/ImageLoader.java @@ -15,7 +15,6 @@ import com.intellij.util.containers.ContainerUtil; import com.intellij.util.ui.EmptyIcon; import com.intellij.util.ui.ImageUtil; import com.intellij.util.ui.StartupUiUtil; -import org.apache.xmlgraphics.java2d.Dimension2DDouble; import org.imgscalr.Scalr; import org.intellij.lang.annotations.MagicConstant; import org.jetbrains.annotations.NonNls; @@ -23,7 +22,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.*; -import java.awt.geom.Dimension2D; import java.awt.image.BufferedImageOp; import java.awt.image.ImageFilter; import java.io.*; @@ -54,7 +52,7 @@ public final class ImageLoader implements Serializable { } public static final long CACHED_IMAGE_MAX_SIZE = (long)(SystemProperties.getFloatProperty("ide.cached.image.max.size", 1.5f) * 1024 * 1024); - private static final ConcurrentMap> ourCache = ContainerUtil.createConcurrentSoftValueMap(); + private static final ConcurrentMap> ourCache = ContainerUtil.createConcurrentSoftValueMap(); public static void clearCache() { ourCache.clear(); @@ -71,6 +69,34 @@ public final class ImageLoader implements Serializable { Image load(@Nullable LoadFunction delegate, @NotNull ImageDesc.Type type) throws IOException; } + public static class Dimension2DDouble { + private double myWidth; + private double myHeight; + + public Dimension2DDouble(double width, double height) { + myWidth = width; + myHeight = height; + } + + public void setSize(Dimension2DDouble size) { + myWidth = size.myWidth; + myHeight = size.myHeight; + } + + public void setSize(double width, double height) { + myWidth = width; + myHeight = height; + } + + public double getWidth() { + return myWidth; + } + + public double getHeight() { + return myHeight; + } + } + public static final class ImageDesc { public enum Type {IMG, SVG} @@ -80,7 +106,7 @@ public final class ImageLoader implements Serializable { final boolean original; // path is not altered // The original user space size of the image. In case of SVG it's the size specified in the SVG doc. // Otherwise it's the size of the original image divided by the image's scale (defined by the extension @2x). - final @NotNull Dimension2D origUsrSize; + final @NotNull Dimension2DDouble origUsrSize; public ImageDesc(@NotNull String path, double scale, @NotNull Type type) { this(path, scale, type, false); @@ -126,7 +152,7 @@ public final class ImageLoader implements Serializable { if (stream == null) { if (useCache) { cacheKey = path + (type == SVG ? "_@" + scale + "x" : ""); - Pair pair = ourCache.get(cacheKey); + Pair pair = ourCache.get(cacheKey); if (pair != null) { origUsrSize.setSize(pair.second); return pair.first; diff --git a/platform/util/ui/src/com/intellij/util/SVGLoader.java b/platform/util/ui/src/com/intellij/util/SVGLoader.java index e38803b45f7c..13d5737ddb94 100644 --- a/platform/util/ui/src/com/intellij/util/SVGLoader.java +++ b/platform/util/ui/src/com/intellij/util/SVGLoader.java @@ -14,7 +14,6 @@ import org.apache.batik.bridge.BridgeContext; import org.apache.batik.bridge.GVTBuilder; import org.apache.batik.transcoder.TranscoderException; import org.apache.batik.transcoder.TranscoderInput; -import org.apache.xmlgraphics.java2d.Dimension2DDouble; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.w3c.dom.Document; @@ -49,7 +48,7 @@ public final class SVGLoader { return load(url, stream, scale, null); } - static Image load(@Nullable URL url, @NotNull InputStream stream, double scale, @Nullable Dimension2D docSize /*OUT*/) throws IOException { + static Image load(@Nullable URL url, @NotNull InputStream stream, double scale, @Nullable ImageLoader.Dimension2DDouble docSize /*OUT*/) throws IOException { try { MyTranscoder transcoder = MyTranscoder.createImage(scale, createTranscodeInput(url, stream)); if (docSize != null) { @@ -106,7 +105,7 @@ public final class SVGLoader { return loadHiDPI(url, stream, (ScaleContext)ctx); } - public static Dimension2D getDocumentSize(@Nullable URL url, @NotNull InputStream stream, double scale) throws IOException { + public static ImageLoader.Dimension2DDouble getDocumentSize(@Nullable URL url, @NotNull InputStream stream, double scale) throws IOException { // In order to get the size we parse the whole document and build a tree ("GVT"), what might be too expensive. // So, to optimize we extract the svg header (possibly prepended with header) and parse only it. // Assumes 8-bit encoding of the input stream (no one in theirs right mind would use wide characters for SVG anyway). @@ -127,11 +126,11 @@ public final class SVGLoader { return getDocumentSize(scale, createTranscodeInput(url, new ByteArrayInputStream(buffer.getInternalBuffer(), 0, buffer.size()))); } } - return new Dimension2DDouble(ICON_DEFAULT_SIZE * scale, ICON_DEFAULT_SIZE * scale); + return new ImageLoader.Dimension2DDouble(ICON_DEFAULT_SIZE * scale, ICON_DEFAULT_SIZE * scale); } public static double getMaxZoomFactor(@Nullable URL url, @NotNull InputStream stream, @NotNull ScaleContext ctx) throws IOException { - Dimension2D size = getDocumentSize(ctx.getScale(DerivedScaleType.PIX_SCALE), createTranscodeInput(url, stream)); + ImageLoader.Dimension2DDouble size = getDocumentSize(ctx.getScale(DerivedScaleType.PIX_SCALE), createTranscodeInput(url, stream)); double iconMaxSize = MyTranscoder.getIconMaxSize(); return Math.min(iconMaxSize / size.getWidth(), iconMaxSize / size.getHeight()); } @@ -169,13 +168,12 @@ public final class SVGLoader { IconLoader.clearCache(); } - private static Dimension2D getDocumentSize(double scale, @NotNull TranscoderInput input) { + private static ImageLoader.Dimension2DDouble getDocumentSize(double scale, @NotNull TranscoderInput input) { SVGOMDocument document = (SVGOMDocument)input.getDocument(); BridgeContext ctx = new MyTranscoder(scale).createBridgeContext(document); new GVTBuilder().build(ctx, document); Dimension2D size = ctx.getDocumentSize(); - size.setSize(size.getWidth() * scale, size.getHeight() * scale); - return size; + return new ImageLoader.Dimension2DDouble(size.getWidth() * scale, size.getHeight() * scale); } public interface SvgColorPatcher {