| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.renderer.output.impl; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.image.BufferedImage; |
| 26 |
import java.awt.image.ImageObserver; |
| 27 |
|
| 28 |
import org.jetbrains.annotations.NotNull; |
| 29 |
import org.jetbrains.annotations.Nullable; |
| 30 |
|
| 31 |
import com.github.weisj.jsvg.logging.Logger; |
| 32 |
import com.github.weisj.jsvg.logging.Logger.Level; |
| 33 |
import com.github.weisj.jsvg.logging.impl.LogFactory; |
| 34 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 35 |
import com.github.weisj.jsvg.util.ImageUtil; |
| 36 |
|
| 37 |
|
| 38 |
public final class GraphicsUtil { |
| 39 |
private static final Logger LOGGER = LogFactory.createLogger(GraphicsUtil.class); |
| 40 |
|
| 41 |
private GraphicsUtil() {} |
| 42 |
|
| 43 |
public static void safelySetPaint(@NotNull Output output, @NotNull Graphics2D g, @NotNull Paint paint) { |
| 44 |
g.setPaint(exchangePaint(output, g.getPaint(), paint, true)); |
| 45 |
} |
| 46 |
|
| 47 |
public static void cleanupPaint(@NotNull Output output, @NotNull Paint paint) { |
| 48 |
if (paint instanceof WrappingPaint) { |
| 49 |
cleanupPaint(output, ((WrappingPaint) paint).paint()); |
| 50 |
} |
| 51 |
if (paint instanceof DisposablePaint) { |
| 52 |
((DisposablePaint) paint).cleanupIfNeeded(output); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public static void preparePaint(@NotNull Paint paint) { |
| 57 |
if (paint instanceof WrappingPaint) { |
| 58 |
preparePaint(((WrappingPaint) paint).paint()); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public static @NotNull Paint exchangePaint(@NotNull Output output, @NotNull Paint current, @NotNull Paint paint, |
| 63 |
boolean doCleanUp) { |
| 64 |
if (paint instanceof WrappingPaint) { |
| 65 |
WrappingPaint wrappingPaint = (WrappingPaint) paint; |
| 66 |
wrappingPaint.safelySetPaint(output, current, doCleanUp); |
| 67 |
return paint; |
| 68 |
} |
| 69 |
if (current instanceof WrappingPaint) { |
| 70 |
WrappingPaint wrappingPaint = (WrappingPaint) current; |
| 71 |
wrappingPaint.safelySetPaint(output, paint, doCleanUp); |
| 72 |
return current; |
| 73 |
} |
| 74 |
if (doCleanUp) { |
| 75 |
preparePaint(paint); |
| 76 |
cleanupPaint(output, current); |
| 77 |
} |
| 78 |
return paint; |
| 79 |
} |
| 80 |
|
| 81 |
public static @NotNull Graphics2D createGraphics(@NotNull BufferedImage image) { |
| 82 |
Graphics2D g = image.createGraphics(); |
| 83 |
g.clipRect(0, 0, image.getWidth(), image.getHeight()); |
| 84 |
return g; |
| 85 |
} |
| 86 |
|
| 87 |
public static @NotNull Composite deriveComposite(@NotNull Graphics2D g, float opacity) { |
| 88 |
Composite composite = g.getComposite(); |
| 89 |
if (composite instanceof AlphaComposite) { |
| 90 |
AlphaComposite ac = (AlphaComposite) composite; |
| 91 |
return AlphaComposite.getInstance(ac.getRule(), ac.getAlpha() * opacity); |
| 92 |
} else if (composite != null) { |
| 93 |
LOGGER.log(Level.WARNING, |
| 94 |
() -> String.format("Composite %s will be overridden by opacity %s", composite, opacity)); |
| 95 |
} |
| 96 |
return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity); |
| 97 |
} |
| 98 |
|
| 99 |
public static void safelyDrawImage(@NotNull Output output, @NotNull Graphics2D g, @NotNull Image image, |
| 100 |
@Nullable ImageObserver observer) { |
| 101 |
Paint p = g.getPaint(); |
| 102 |
if (p instanceof WrappingPaint) { |
| 103 |
WrappingPaint wrappingPaint = (WrappingPaint) p; |
| 104 |
Paint inner = wrappingPaint.innerPaint(); |
| 105 |
|
| 106 |
Rectangle r = new Rectangle(0, 0, image.getWidth(observer), image.getHeight(observer)); |
| 107 |
BufferedImage img = image instanceof BufferedImage |
| 108 |
? (BufferedImage) image |
| 109 |
: ImageUtil.toBufferedImage(image); |
| 110 |
TexturePaint texturePaint = new TexturePaint(img, r); |
| 111 |
|
| 112 |
wrappingPaint.setPaint(exchangePaint(output, wrappingPaint.paint(), texturePaint, false)); |
| 113 |
g.fill(r); |
| 114 |
wrappingPaint.setPaint(exchangePaint(output, texturePaint, inner, false)); |
| 115 |
} else { |
| 116 |
g.drawImage(image, 0, 0, observer); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public interface WrappingPaint { |
| 121 |
void setPaint(@NotNull Paint paint); |
| 122 |
|
| 123 |
@NotNull |
| 124 |
Paint paint(); |
| 125 |
|
| 126 |
default @NotNull Paint innerPaint() { |
| 127 |
Paint p = paint(); |
| 128 |
if (p instanceof WrappingPaint) { |
| 129 |
return ((WrappingPaint) p).innerPaint(); |
| 130 |
} |
| 131 |
return p; |
| 132 |
} |
| 133 |
|
| 134 |
default void safelySetPaint(@NotNull Output output, @NotNull Paint paint, boolean updateRefCounts) { |
| 135 |
setPaint(exchangePaint(output, paint(), paint, updateRefCounts)); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public interface DisposablePaint { |
| 140 |
|
| 141 |
void cleanupIfNeeded(@NotNull Output output); |
| 142 |
} |
| 143 |
} |