| •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; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.geom.AffineTransform; |
| 26 |
import java.awt.geom.Point2D; |
| 27 |
|
| 28 |
import org.jetbrains.annotations.NotNull; |
| 29 |
import org.jetbrains.annotations.Nullable; |
| 30 |
|
| 31 |
import com.github.weisj.jsvg.attributes.FillRule; |
| 32 |
import com.github.weisj.jsvg.attributes.PaintOrder; |
| 33 |
import com.github.weisj.jsvg.attributes.font.FontResolver; |
| 34 |
import com.github.weisj.jsvg.attributes.font.MeasurableFontSpec; |
| 35 |
import com.github.weisj.jsvg.attributes.font.SVGFont; |
| 36 |
import com.github.weisj.jsvg.attributes.stroke.StrokeResolver; |
| 37 |
import com.github.weisj.jsvg.geometry.size.Length; |
| 38 |
import com.github.weisj.jsvg.nodes.SVG; |
| 39 |
import com.github.weisj.jsvg.nodes.prototype.Mutator; |
| 40 |
import com.github.weisj.jsvg.paint.SVGPaint; |
| 41 |
import com.github.weisj.jsvg.paint.impl.PredefinedPaints; |
| 42 |
import com.github.weisj.jsvg.renderer.impl.PaintResolver; |
| 43 |
import com.github.weisj.jsvg.renderer.impl.context.*; |
| 44 |
import com.github.weisj.jsvg.renderer.impl.context.PaintContext; |
| 45 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 46 |
import com.github.weisj.jsvg.view.ViewBox; |
| 47 |
|
| 48 |
public final class RenderContext { |
| 49 |
|
| 50 |
private final @NotNull PlatformSupport platformSupport; |
| 51 |
private final @NotNull MeasureContext measureContext; |
| 52 |
private final @NotNull PaintContext paintContext; |
| 53 |
|
| 54 |
private final @NotNull FontRenderContext fontRenderContext; |
| 55 |
private final @NotNull MeasurableFontSpec fontSpec; |
| 56 |
|
| 57 |
|
| 58 |
private final @Nullable ContextElementAttributes contextElementAttributes; |
| 59 |
|
| 60 |
private final @NotNull AffineTransform rootTransform; |
| 61 |
private final @NotNull AffineTransform userSpaceTransform; |
| 62 |
|
| 63 |
static { |
| 64 |
RenderContextAccessor.setInstance(new RenderContextAccessor.Accessor() { |
| 65 |
@Override |
| 66 |
public @NotNull RenderContext createInitial(@Nullable SVGPaint currentColor, |
| 67 |
@NotNull PlatformSupport awtSupport, |
| 68 |
@NotNull MeasureContext measureContext) { |
| 69 |
return RenderContext.createInitial(currentColor, awtSupport, measureContext); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public @NotNull RenderContext deriveForSurface(@NotNull RenderContext context) { |
| 74 |
return context.deriveForSurface(); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public @NotNull RenderContext deriveForChildGraphics(@NotNull RenderContext context) { |
| 79 |
return context.deriveForChildGraphics(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public @NotNull RenderContext deriveForNode( |
| 84 |
@NotNull RenderContext context, |
| 85 |
@Nullable Mutator<PaintContext> paintContextMutator, |
| 86 |
@Nullable Mutator<MeasurableFontSpec> attributeFontSpec, |
| 87 |
@Nullable FontRenderContext frc, |
| 88 |
@Nullable ContextElementAttributes contextAttributes, |
| 89 |
@NotNull Object node) { |
| 90 |
return context.deriveForNode(paintContextMutator, attributeFontSpec, frc, contextAttributes, node); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public @NotNull RenderContext setupInnerViewRenderContext(@NotNull ViewBox viewBox, |
| 95 |
@NotNull RenderContext context, boolean inheritAttributes) { |
| 96 |
if (inheritAttributes) { |
| 97 |
return context.derive(null, null, viewBox, null, null, |
| 98 |
RenderContext.EstablishRootMeasure.NO); |
| 99 |
} else { |
| 100 |
MeasureContext newMeasure = context.measureContext().derive(viewBox, |
| 101 |
Length.UNSPECIFIED_RAW, Length.UNSPECIFIED_RAW); |
| 102 |
return new RenderContext( |
| 103 |
context.platformSupport(), |
| 104 |
new AffineTransform(), |
| 105 |
new AffineTransform(), |
| 106 |
PaintContext.createDefault(PredefinedPaints.DEFAULT_PAINT), |
| 107 |
newMeasure, |
| 108 |
FontRenderContext.createDefault(), |
| 109 |
MeasurableFontSpec.createDefault(), |
| 110 |
context.contextElementAttributes()); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
@Override |
| 116 |
public @NotNull StrokeContext strokeContext(@NotNull RenderContext context) { |
| 117 |
return context.strokeContext(); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public @NotNull FontRenderContext fontRenderContext(@NotNull RenderContext context) { |
| 122 |
return context.fontRenderContext(); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public @NotNull FillRule fillRule(@NotNull RenderContext context) { |
| 127 |
return context.fillRule(); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public @NotNull PaintOrder paintOrder(@NotNull RenderContext context) { |
| 132 |
return context.paintOrder(); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public @NotNull SVGFont font(@NotNull RenderContext context) { |
| 137 |
return context.font(); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public @Nullable SVGPaint currentColor(@NotNull RenderContext context) { |
| 142 |
return context.paintContext.color; |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public void setRootTransform(@NotNull RenderContext context, @NotNull AffineTransform rootTransform) { |
| 147 |
context.setRootTransform(rootTransform); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public void setRootTransform(@NotNull RenderContext context, @NotNull AffineTransform rootTransform, |
| 152 |
@NotNull AffineTransform userSpaceTransform) { |
| 153 |
context.setRootTransform(rootTransform, userSpaceTransform); |
| 154 |
} |
| 155 |
}); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
private static @NotNull RenderContext createInitial(@Nullable SVGPaint currentColor, |
| 160 |
@NotNull PlatformSupport awtSupport, |
| 161 |
@NotNull MeasureContext measureContext) { |
| 162 |
SVGPaint color = currentColor != null ? currentColor : PredefinedPaints.DEFAULT_PAINT; |
| 163 |
return new RenderContext(awtSupport, |
| 164 |
new AffineTransform(), |
| 165 |
new AffineTransform(), |
| 166 |
PaintContext.createDefault(color), |
| 167 |
measureContext, |
| 168 |
FontRenderContext.createDefault(), |
| 169 |
MeasurableFontSpec.createDefault(), |
| 170 |
null); |
| 171 |
} |
| 172 |
|
| 173 |
private RenderContext(@NotNull PlatformSupport platformSupport, |
| 174 |
@NotNull AffineTransform rootTransform, |
| 175 |
@NotNull AffineTransform userSpaceTransform, |
| 176 |
@NotNull com.github.weisj.jsvg.renderer.impl.context.PaintContext paintContext, |
| 177 |
@NotNull MeasureContext measureContext, |
| 178 |
@NotNull FontRenderContext fontRenderContext, |
| 179 |
@NotNull MeasurableFontSpec fontSpec, |
| 180 |
@Nullable ContextElementAttributes contextElementAttributes) { |
| 181 |
this.platformSupport = platformSupport; |
| 182 |
this.rootTransform = rootTransform; |
| 183 |
this.userSpaceTransform = userSpaceTransform; |
| 184 |
this.paintContext = paintContext; |
| 185 |
this.measureContext = measureContext; |
| 186 |
this.fontRenderContext = fontRenderContext; |
| 187 |
this.fontSpec = fontSpec; |
| 188 |
this.contextElementAttributes = contextElementAttributes; |
| 189 |
} |
| 190 |
|
| 191 |
private enum EstablishRootMeasure { |
| 192 |
YES, |
| 193 |
NO |
| 194 |
} |
| 195 |
|
| 196 |
@NotNull |
| 197 |
private RenderContext derive( |
| 198 |
@Nullable Mutator<PaintContext> context, |
| 199 |
@Nullable Mutator<MeasurableFontSpec> attributeFontSpec, |
| 200 |
@Nullable ViewBox viewBox, @Nullable FontRenderContext frc, |
| 201 |
@Nullable ContextElementAttributes contextAttributes, |
| 202 |
EstablishRootMeasure establishRootMeasure) { |
| 203 |
return deriveImpl(context, attributeFontSpec, viewBox, frc, contextAttributes, null, |
| 204 |
establishRootMeasure); |
| 205 |
} |
| 206 |
|
| 207 |
@NotNull |
| 208 |
private RenderContext deriveImpl( |
| 209 |
@Nullable Mutator<com.github.weisj.jsvg.renderer.impl.context.PaintContext> context, |
| 210 |
@Nullable Mutator<MeasurableFontSpec> attributeFontSpec, |
| 211 |
@Nullable ViewBox viewBox, @Nullable FontRenderContext frc, |
| 212 |
@Nullable ContextElementAttributes contextAttributes, |
| 213 |
@Nullable AffineTransform rootTransform, |
| 214 |
EstablishRootMeasure establishRootMeasure) { |
| 215 |
if (context == null && viewBox == null && attributeFontSpec == null && frc == null) return this; |
| 216 |
PaintContext newPaintContext = paintContext; |
| 217 |
MeasurableFontSpec newFontSpec = fontSpec; |
| 218 |
|
| 219 |
if (context != null) newPaintContext = context.mutate(paintContext); |
| 220 |
if (attributeFontSpec != null) newFontSpec = attributeFontSpec.mutate(newFontSpec); |
| 221 |
|
| 222 |
ContextElementAttributes newContextAttributes = contextElementAttributes; |
| 223 |
if (contextAttributes != null) newContextAttributes = contextAttributes; |
| 224 |
|
| 225 |
float em = newFontSpec.emSize(measureContext); |
| 226 |
float ex = SVGFont.exFromEm(em); |
| 227 |
MeasureContext newMeasureContext = measureContext.derive(viewBox, em, ex); |
| 228 |
|
| 229 |
if (establishRootMeasure == EstablishRootMeasure.YES) { |
| 230 |
newMeasureContext = newMeasureContext.deriveRoot(em); |
| 231 |
} |
| 232 |
|
| 233 |
FontRenderContext effectiveFrc = fontRenderContext.derive(frc); |
| 234 |
AffineTransform newRootTransform = rootTransform != null ? rootTransform : this.rootTransform; |
| 235 |
|
| 236 |
return new RenderContext(platformSupport, newRootTransform, new AffineTransform(userSpaceTransform), |
| 237 |
newPaintContext, newMeasureContext, effectiveFrc, newFontSpec, newContextAttributes); |
| 238 |
} |
| 239 |
|
| 240 |
private @NotNull RenderContext deriveForChildGraphics() { |
| 241 |
|
| 242 |
return derive(t -> t, null, null, null, null, EstablishRootMeasure.NO); |
| 243 |
} |
| 244 |
|
| 245 |
private @NotNull RenderContext deriveForSurface() { |
| 246 |
return deriveImpl(t -> t, null, null, null, null, |
| 247 |
new AffineTransform(rootTransform), EstablishRootMeasure.NO); |
| 248 |
} |
| 249 |
|
| 250 |
private @NotNull RenderContext deriveForNode( |
| 251 |
@Nullable Mutator<PaintContext> context, |
| 252 |
@Nullable Mutator<MeasurableFontSpec> attributeFontSpec, |
| 253 |
@Nullable FontRenderContext frc, |
| 254 |
@Nullable ContextElementAttributes contextAttributes, |
| 255 |
@NotNull Object node) { |
| 256 |
EstablishRootMeasure establishRootMeasure = node instanceof SVG && ((SVG) node).isTopLevel() |
| 257 |
? EstablishRootMeasure.YES |
| 258 |
: EstablishRootMeasure.NO; |
| 259 |
return derive(context, attributeFontSpec, null, frc, contextAttributes, |
| 260 |
establishRootMeasure); |
| 261 |
} |
| 262 |
|
| 263 |
public @NotNull AffineTransform rootTransform() { |
| 264 |
return rootTransform; |
| 265 |
} |
| 266 |
|
| 267 |
public @NotNull AffineTransform userSpaceTransform() { |
| 268 |
return userSpaceTransform; |
| 269 |
} |
| 270 |
|
| 271 |
public void translate(@NotNull Output output, @NotNull Point2D dp) { |
| 272 |
translate(output, dp.getX(), dp.getY()); |
| 273 |
} |
| 274 |
|
| 275 |
public void translate(@NotNull Output output, double dx, double dy) { |
| 276 |
output.translate(dx, dy); |
| 277 |
userSpaceTransform.translate(dx, dy); |
| 278 |
} |
| 279 |
|
| 280 |
public void scale(@NotNull Output output, double sx, double sy) { |
| 281 |
output.scale(sx, sy); |
| 282 |
userSpaceTransform.scale(sx, sy); |
| 283 |
} |
| 284 |
|
| 285 |
public void rotate(@NotNull Output output, double angle) { |
| 286 |
output.rotate(angle); |
| 287 |
userSpaceTransform.rotate(angle); |
| 288 |
} |
| 289 |
|
| 290 |
public void transform(@NotNull Output output, @NotNull AffineTransform at) { |
| 291 |
output.applyTransform(at); |
| 292 |
userSpaceTransform.concatenate(at); |
| 293 |
} |
| 294 |
|
| 295 |
public @NotNull PlatformSupport platformSupport() { |
| 296 |
return platformSupport; |
| 297 |
} |
| 298 |
|
| 299 |
public @NotNull MeasureContext measureContext() { |
| 300 |
return measureContext; |
| 301 |
} |
| 302 |
|
| 303 |
public @NotNull SVGPaint strokePaint() { |
| 304 |
return PaintResolver.resolvePaint(paintContext.strokePaint, paintContext, contextElementAttributes); |
| 305 |
} |
| 306 |
|
| 307 |
public @NotNull SVGPaint fillPaint() { |
| 308 |
return PaintResolver.resolvePaint(paintContext.fillPaint, paintContext, contextElementAttributes); |
| 309 |
} |
| 310 |
|
| 311 |
public float rawOpacity() { |
| 312 |
return paintContext.opacity.get(measureContext); |
| 313 |
} |
| 314 |
|
| 315 |
public float fillOpacity() { |
| 316 |
assert paintContext.fillOpacity != null; |
| 317 |
return paintContext.fillOpacity.get(measureContext) * paintContext.opacity.get(measureContext); |
| 318 |
} |
| 319 |
|
| 320 |
public float strokeOpacity() { |
| 321 |
assert paintContext.strokeOpacity != null; |
| 322 |
return paintContext.strokeOpacity.get(measureContext) * paintContext.opacity.get(measureContext); |
| 323 |
} |
| 324 |
|
| 325 |
public @NotNull Stroke stroke(float pathLengthFactor) { |
| 326 |
return StrokeResolver.resolve(pathLengthFactor, measureContext, strokeContext()); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
private @NotNull StrokeContext strokeContext() { |
| 332 |
|
| 333 |
|
| 334 |
assert paintContext.strokeContext != null; |
| 335 |
return paintContext.strokeContext; |
| 336 |
} |
| 337 |
|
| 338 |
private @NotNull FontRenderContext fontRenderContext() { |
| 339 |
return fontRenderContext; |
| 340 |
} |
| 341 |
|
| 342 |
private @Nullable ContextElementAttributes contextElementAttributes() { |
| 343 |
return contextElementAttributes; |
| 344 |
} |
| 345 |
|
| 346 |
private @NotNull FillRule fillRule() { |
| 347 |
FillRule fillRule = paintContext.fillRule; |
| 348 |
return fillRule != null ? fillRule : FillRule.Nonzero; |
| 349 |
} |
| 350 |
|
| 351 |
private @NotNull PaintOrder paintOrder() { |
| 352 |
PaintOrder paintOrder = paintContext.paintOrder; |
| 353 |
return paintOrder != null ? paintOrder : PaintOrder.NORMAL; |
| 354 |
} |
| 355 |
|
| 356 |
private @NotNull SVGFont font() { |
| 357 |
return FontResolver.resolve(this.fontSpec, this.measureContext, platformSupport.fontFamily()); |
| 358 |
} |
| 359 |
|
| 360 |
private void setRootTransform(@NotNull AffineTransform rootTransform) { |
| 361 |
this.rootTransform.setTransform(rootTransform); |
| 362 |
this.userSpaceTransform.setToIdentity(); |
| 363 |
} |
| 364 |
|
| 365 |
private void setRootTransform(@NotNull AffineTransform rootTransform, @NotNull AffineTransform userSpaceTransform) { |
| 366 |
this.rootTransform.setTransform(rootTransform); |
| 367 |
this.userSpaceTransform.setTransform(userSpaceTransform); |
| 368 |
} |
| 369 |
|
| 370 |
@Override |
| 371 |
public String toString() { |
| 372 |
return "RenderContext{" + |
| 373 |
"platformSupport=" + platformSupport + |
| 374 |
", measureContext=" + measureContext + |
| 375 |
", paintContext=" + paintContext + |
| 376 |
", fontRenderContext=" + fontRenderContext + |
| 377 |
", fontSpec=" + fontSpec + |
| 378 |
", contextElementAttributes=" + contextElementAttributes + |
| 379 |
", rootTransform=" + rootTransform + |
| 380 |
", userSpaceTransform=" + userSpaceTransform + |
| 381 |
'}'; |
| 382 |
} |
| 383 |
} |