| •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; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.geom.AffineTransform; |
| 26 |
import java.awt.geom.Area; |
| 27 |
import java.awt.geom.Path2D; |
| 28 |
|
| 29 |
import javax.swing.*; |
| 30 |
|
| 31 |
import org.jetbrains.annotations.NotNull; |
| 32 |
import org.jetbrains.annotations.Nullable; |
| 33 |
|
| 34 |
import com.github.weisj.jsvg.attributes.PreserveAspectRatio; |
| 35 |
import com.github.weisj.jsvg.attributes.font.SVGFont; |
| 36 |
import com.github.weisj.jsvg.nodes.SVG; |
| 37 |
import com.github.weisj.jsvg.paint.SVGPaint; |
| 38 |
import com.github.weisj.jsvg.parser.impl.DocumentConstructorAccessor; |
| 39 |
import com.github.weisj.jsvg.renderer.*; |
| 40 |
import com.github.weisj.jsvg.renderer.MeasureContext; |
| 41 |
import com.github.weisj.jsvg.renderer.NullPlatformSupport; |
| 42 |
import com.github.weisj.jsvg.renderer.PlatformSupport; |
| 43 |
import com.github.weisj.jsvg.renderer.animation.Animation; |
| 44 |
import com.github.weisj.jsvg.renderer.animation.AnimationState; |
| 45 |
import com.github.weisj.jsvg.renderer.awt.AwtComponentPlatformSupport; |
| 46 |
import com.github.weisj.jsvg.renderer.impl.*; |
| 47 |
import com.github.weisj.jsvg.renderer.impl.context.RenderContextAccessor; |
| 48 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 49 |
import com.github.weisj.jsvg.renderer.output.impl.CurrentColorProvider; |
| 50 |
import com.github.weisj.jsvg.renderer.output.impl.ShapeOutput; |
| 51 |
import com.github.weisj.jsvg.view.FloatSize; |
| 52 |
import com.github.weisj.jsvg.view.ViewBox; |
| 53 |
|
| 54 |
public final class SVGDocument { |
| 55 |
private static final boolean DEBUG = false; |
| 56 |
private final @NotNull SVG root; |
| 57 |
private final @NotNull FloatSize size; |
| 58 |
|
| 59 |
static { |
| 60 |
DocumentConstructorAccessor.setDocumentConstructor(SVGDocument::new); |
| 61 |
} |
| 62 |
|
| 63 |
private SVGDocument(@NotNull SVG root) { |
| 64 |
this.root = root; |
| 65 |
this.size = sizeForViewport(null); |
| 66 |
} |
| 67 |
|
| 68 |
public @NotNull FloatSize size() { |
| 69 |
return size; |
| 70 |
} |
| 71 |
|
| 72 |
public @NotNull FloatSize sizeForViewport(@Nullable ViewBox viewport) { |
| 73 |
float em = SVGFont.defaultFontSize(); |
| 74 |
return root.sizeForTopLevel(viewport, em, SVGFont.exFromEm(em)); |
| 75 |
} |
| 76 |
|
| 77 |
public @NotNull ViewBox viewBox() { |
| 78 |
return root.staticViewBox(size()); |
| 79 |
} |
| 80 |
|
| 81 |
public @NotNull Shape computeShape() { |
| 82 |
return computeShape(null); |
| 83 |
} |
| 84 |
|
| 85 |
public @NotNull Shape computeShape(@Nullable ViewBox viewBox) { |
| 86 |
Area accumulator = new Area(new Path2D.Float()); |
| 87 |
renderWithPlatform(NullPlatformSupport.INSTANCE, new ShapeOutput(accumulator), viewBox); |
| 88 |
return accumulator; |
| 89 |
} |
| 90 |
|
| 91 |
public boolean isAnimated() { |
| 92 |
return animation().duration() > 0; |
| 93 |
} |
| 94 |
|
| 95 |
public @NotNull Animation animation() { |
| 96 |
return root.animationPeriod(); |
| 97 |
} |
| 98 |
|
| 99 |
public void render(@Nullable JComponent component, @NotNull Graphics2D g) { |
| 100 |
render(component, g, null); |
| 101 |
} |
| 102 |
|
| 103 |
public void render(@Nullable Component component, @NotNull Graphics2D graphics2D, @Nullable ViewBox bounds) { |
| 104 |
PlatformSupport platformSupport = component != null |
| 105 |
? new AwtComponentPlatformSupport(component) |
| 106 |
: NullPlatformSupport.INSTANCE; |
| 107 |
renderWithPlatform(platformSupport, graphics2D, bounds); |
| 108 |
} |
| 109 |
|
| 110 |
private float computePlatformFontSize(@NotNull PlatformSupport platformSupport, @NotNull Output output) { |
| 111 |
return output.contextFontSize().orElseGet(platformSupport::fontSize); |
| 112 |
} |
| 113 |
|
| 114 |
public void renderWithPlatform(@NotNull PlatformSupport platformSupport, @NotNull Graphics2D graphics2D, |
| 115 |
@Nullable ViewBox bounds) { |
| 116 |
Output output = Output.createForGraphics(graphics2D); |
| 117 |
renderWithPlatform(platformSupport, output, bounds); |
| 118 |
output.dispose(); |
| 119 |
} |
| 120 |
|
| 121 |
public void renderWithPlatform(@NotNull PlatformSupport platformSupport, @NotNull Output output, |
| 122 |
@Nullable ViewBox viewportBounds) { |
| 123 |
renderWithPlatform(platformSupport, output, viewportBounds, null); |
| 124 |
} |
| 125 |
|
| 126 |
public void renderWithPlatform(@NotNull PlatformSupport platformSupport, @NotNull Output output, |
| 127 |
@Nullable ViewBox viewportBounds, @Nullable AnimationState animationState) { |
| 128 |
RenderContext context = prepareRenderContext(platformSupport, output, viewportBounds, animationState); |
| 129 |
|
| 130 |
ViewBox rootVieBox = new ViewBox(root.size(context)); |
| 131 |
|
| 132 |
if (viewportBounds == null) viewportBounds = rootVieBox; |
| 133 |
|
| 134 |
if (DEBUG) { |
| 135 |
final ViewBox finalBounds = viewportBounds; |
| 136 |
output.debugPaint(g -> { |
| 137 |
g.setColor(Color.RED); |
| 138 |
g.draw(finalBounds); |
| 139 |
}); |
| 140 |
} |
| 141 |
|
| 142 |
AffineTransform rootTransform = PreserveAspectRatio.forDisplay() |
| 143 |
.computeViewportTransform(viewportBounds.size(), rootVieBox); |
| 144 |
|
| 145 |
RenderContext innerContext = NodeRenderer.setupInnerViewRenderContext(rootVieBox, context, true); |
| 146 |
|
| 147 |
output.applyClip(viewportBounds); |
| 148 |
|
| 149 |
innerContext.translate(output, viewportBounds.location()); |
| 150 |
innerContext.transform(output, rootTransform); |
| 151 |
|
| 152 |
|
| 153 |
RenderContextAccessor.Accessor accessor = RenderContextAccessor.instance(); |
| 154 |
accessor.setRootTransform(innerContext, output.transform()); |
| 155 |
|
| 156 |
NodeRenderer.renderRootSVG(root, context, output); |
| 157 |
} |
| 158 |
|
| 159 |
private @NotNull RenderContext prepareRenderContext( |
| 160 |
@NotNull PlatformSupport platformSupport, |
| 161 |
@NotNull Output output, |
| 162 |
@Nullable ViewBox viewportBounds, |
| 163 |
@Nullable AnimationState animationState) { |
| 164 |
float defaultEm = computePlatformFontSize(platformSupport, output); |
| 165 |
float defaultEx = SVGFont.exFromEm(defaultEm); |
| 166 |
AnimationState animState = animationState != null ? animationState : AnimationState.NO_ANIMATION; |
| 167 |
MeasureContext initialMeasure = viewportBounds != null |
| 168 |
? MeasureContext.createInitial(viewportBounds.size(), defaultEm, defaultEx, animState) |
| 169 |
: MeasureContext.createInitial(root.sizeForTopLevel(null, defaultEm, defaultEx), |
| 170 |
defaultEm, defaultEx, animState); |
| 171 |
SVGPaint currentColor = null; |
| 172 |
if (output instanceof CurrentColorProvider) { |
| 173 |
currentColor = ((CurrentColorProvider) output).currentColor(); |
| 174 |
} |
| 175 |
return RenderContextAccessor.instance().createInitial(currentColor, platformSupport, initialMeasure); |
| 176 |
} |
| 177 |
|
| 178 |
} |