Commit b43a127b authored by Anton Tarasov's avatar Anton Tarasov
Browse files

Add another SVGLoader.loadHiDPI method

parent 55e4238c
Showing with 29 additions and 9 deletions
+29 -9
......@@ -20,6 +20,8 @@ import com.intellij.util.Urls;
import com.intellij.util.io.HttpRequests;
import com.intellij.util.io.URLUtil;
import com.intellij.util.ui.JBImageIcon;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.JBUI.ScaleContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
......@@ -286,8 +288,9 @@ public class PluginLogo {
@Nullable
private static PluginLogoIconProvider loadFileIcon(@NotNull ThrowableComputable<InputStream, IOException> provider) {
try {
Icon logo40 = new JBImageIcon(SVGLoader.load(null, provider.compute(), 40, 40));
Icon logo80 = new JBImageIcon(SVGLoader.load(null, provider.compute(), 80, 80));
ScaleContext ctx = ScaleContext.create();
Icon logo40 = new JBImageIcon(SVGLoader.loadHiDPI(null, provider.compute(), ctx, 40, 40));
Icon logo80 = new JBImageIcon(SVGLoader.loadHiDPI(null, provider.compute(), ctx, 80, 80));
return new PluginLogoIcon(logo40, Objects.requireNonNull(IconLoader.getDisabledIcon(logo40)),
logo80, Objects.requireNonNull(IconLoader.getDisabledIcon(logo80)));
......
......@@ -31,6 +31,7 @@ import com.intellij.util.*;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.ImageUtil;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.JBUI.ScaleContext;
import com.intellij.util.ui.SwingHelper;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
......@@ -110,7 +111,7 @@ public class AppUIUtil {
URL url = AppUIUtil.class.getResource(svgIconUrl);
try {
return
SVGLoader.load(url, AppUIUtil.class.getResourceAsStream(svgIconUrl), JBUI.pixScale(window) * size, JBUI.pixScale(window) * size);
SVGLoader.load(url, AppUIUtil.class.getResourceAsStream(svgIconUrl), ScaleContext.create(window), size, size);
}
catch (IOException e) {
LOG.info("Cannot load svg application icon from " + svgIconUrl, e);
......
......@@ -15,6 +15,7 @@ import java.io.File;
import java.io.IOException;
import java.net.URL;
import static com.intellij.util.ui.JBUI.ScaleType.PIX_SCALE;
import static com.intellij.util.ui.JBUI.ScaleType.SYS_SCALE;
import static com.intellij.util.ui.TestScaleHelper.loadImage;
import static com.intellij.util.ui.TestScaleHelper.overrideJreHiDPIEnabled;
......@@ -42,11 +43,13 @@ public class SvgIconSizeTest {
* Test overridden size.
*/
URL url = new File(getSvgIconPath("20x10")).toURI().toURL();
Image image = SVGLoader.load(url, url.openStream(), 25, 15);
ScaleContext ctx = ScaleContext.create(SYS_SCALE.of(2));
double pixScale = ctx.getScale(PIX_SCALE);
Image image = SVGLoader.load(url, url.openStream(), ctx, 25, 15);
assertNotNull(image);
image = ImageUtil.toBufferedImage(image);
assertEquals("wrong image width", 25, image.getWidth(null));
assertEquals("wrong image height", 15, image.getHeight(null));
assertEquals("wrong image width", pixScale * 25, (double)image.getWidth(null));
assertEquals("wrong image height", pixScale * 15, (double)image.getHeight(null));
}
private static void test(ScaleContext ctx) {
......
......@@ -122,17 +122,30 @@ public class SVGLoader {
}
/**
* Loads an image with the specified {@code width} and {@code height}. Size specified in svg file is ignored.
* Loads an image with the specified {@code width} and {@code height} (in user space). Size specified in svg file is ignored.
*/
public static Image load(@Nullable URL url, @NotNull InputStream stream, double width, double height) throws IOException {
public static Image load(@Nullable URL url, @NotNull InputStream stream, @NotNull ScaleContext ctx, double width, double height) throws IOException {
try {
return new SVGLoader(url, stream, width, height, 1).createImage();
double s = ctx.getScale(PIX_SCALE);
return new SVGLoader(url, stream, width * s, height * s, 1).createImage();
}
catch (TranscoderException ex) {
throw new IOException(ex);
}
}
/**
* Loads a HiDPI-aware image with the specified {@code width} and {@code height} (in user space). Size specified in svg file is ignored.
*/
public static <T extends BufferedImage> T loadHiDPI(@Nullable URL url, @NotNull InputStream stream , ScaleContext ctx, double width, double height) throws IOException {
BufferedImage image = (BufferedImage)load(url, stream, ctx, width, height);
//noinspection unchecked
return (T)ImageUtil.ensureHiDPI(image, ctx);
}
/**
* Loads a HiDPI-aware image of the size specified in the svg file.
*/
public static <T extends BufferedImage> T loadHiDPI(@Nullable URL url, @NotNull InputStream stream , ScaleContext ctx) throws IOException {
BufferedImage image = (BufferedImage)load(url, stream, ctx.getScale(PIX_SCALE));
//noinspection unchecked
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment