Debian Codegraph @

src/com/github/weisj/jsvg/attributes/VectorEffect.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2022-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.attributes;
23
24 import java.awt.*;
25 import java.awt.geom.AffineTransform;
26 import java.util.EnumSet;
27 import java.util.Set;
28
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31
32 import com.github.weisj.jsvg.geometry.util.GeometryUtil;
33 import com.github.weisj.jsvg.parser.impl.AttributeNode;
34 import com.github.weisj.jsvg.renderer.RenderContext;
35 import com.github.weisj.jsvg.renderer.output.Output;
36 import com.github.weisj.jsvg.util.ShapeUtil;
37
38
39 public enum VectorEffect implements HasMatchName {
40 None(0),
41 NonScalingStroke("non-scaling-stroke", 0),
42 NonScalingSize("non-scaling-size", Flags.NON_SCALING_SIZE),
43 NonRotation("non-rotation", Flags.NON_ROTATING),
44 FixedPosition("fixed-position", Flags.FIXED_POSITION);
45
46
47 private final @NotNull String matchName;
48 private final int flag;
49
50 VectorEffect(int flag) {
51 this.matchName = name();
52 this.flag = flag;
53 }
54
55 VectorEffect(@NotNull String matchName, int flag) {
56 this.matchName = matchName;
57 this.flag = flag;
58 }
59
60 public static @NotNull Set<VectorEffect> parse(@NotNull AttributeNode attributeNode) {
61 @NotNull String[] vectorEffectsRaw = attributeNode.getStringList("vector-effect");
62 EnumSet<VectorEffect> vectorEffects = EnumSet.noneOf(VectorEffect.class);
63 for (String effect : vectorEffectsRaw) {
64 vectorEffects.add(attributeNode.parser().parseEnum(effect, VectorEffect.None));
65 }
66 return vectorEffects;
67 }
68
69 @Override
70 public @NotNull String matchName() {
71 return matchName;
72 }
73
74 private static int flags(@NotNull Set<VectorEffect> effects) {
75 int flag = 0;
76 for (VectorEffect effect : effects) {
77 flag |= effect.flag;
78 }
79 return flag;
80 }
81
82 public static void applyEffects(@NotNull Set<VectorEffect> effects, @NotNull Output output,
83 @NotNull RenderContext context, @Nullable AffineTransform elementTransform) {
84 int flags = flags(effects);
85 if (flags == 0) return;
86
87 AffineTransform shapeTransform = new AffineTransform(context.userSpaceTransform());
88
89 double x0 = elementTransform != null ? elementTransform.getTranslateX() : 0;
90 double y0 = elementTransform != null ? elementTransform.getTranslateY() : 0;
91
92 updateTransformForFlags(flags, shapeTransform, x0, y0);
93
94 output.setTransform(context.rootTransform());
95 output.applyTransform(shapeTransform);
96 }
97
98 // https://svgwg.org/svg2-draft/painting.html#PaintingVectorEffects
99 public static @NotNull Shape applyNonScalingStroke(@NotNull Output output, @NotNull RenderContext context,
100 @NotNull Stroke stroke, @NotNull Shape shape) {
101 // For the stroke not to be scaled we have to pre-multiply the shape by the transform and then paint
102 // in the non-transformed coordinate system.
103 Shape strokedShape = ShapeUtil.transformShape(shape, context.userSpaceTransform());
104 strokedShape = stroke.createStrokedShape(strokedShape);
105 return ShapeUtil.transformShape(strokedShape, GeometryUtil.createInverse(context.userSpaceTransform()));
106 }
107
108 private static void updateTransformForFlags(int flags, @NotNull AffineTransform transform, double x0, double y0) {
109 switch (flags) {
110 case Flags.NON_SCALING_SIZE: {
111 double detRoot = Math.sqrt(Math.abs(transform.getDeterminant()));
112 if (detRoot == 0) return;
113 double detRootInv = 1 / detRoot;
114 transform.setTransform(
115 transform.getScaleX() * detRootInv, transform.getShearY() * detRootInv,
116 transform.getShearX() * detRootInv, transform.getScaleY() * detRootInv,
117 transform.getTranslateX(),
118 transform.getTranslateY());
119 break;
120 }
121 case Flags.NON_ROTATING: {
122 double detRoot = Math.sqrt(Math.abs(transform.getDeterminant()));
123 transform.setTransform(
124 detRoot, 0, 0, detRoot,
125 transform.getTranslateX(),
126 transform.getTranslateY());
127 break;
128 }
129 case Flags.NON_SCALING_SIZE | Flags.NON_ROTATING: {
130 transform.setTransform(
131 1, 0, 0, 1,
132 transform.getTranslateX(),
133 transform.getTranslateY());
134 break;
135 }
136 case Flags.FIXED_POSITION: {
137 transform.setTransform(
138 transform.getScaleX(), transform.getShearY(),
139 transform.getShearX(), transform.getScaleY(),
140 x0,
141 y0);
142 break;
143 }
144 case Flags.FIXED_POSITION | Flags.NON_SCALING_SIZE: {
145 double detRoot = Math.sqrt(Math.abs(transform.getDeterminant()));
146 if (detRoot == 0) return;
147 double detRootInv = 1 / detRoot;
148 transform.setTransform(
149 transform.getScaleX() * detRootInv, transform.getShearY() * detRootInv,
150 transform.getShearX() * detRootInv, transform.getScaleY() * detRootInv,
151 x0,
152 y0);
153 break;
154 }
155 case Flags.FIXED_POSITION | Flags.NON_ROTATING: {
156 double detRoot = Math.sqrt(Math.abs(transform.getDeterminant()));
157 transform.setTransform(
158 detRoot, 0, 0, detRoot,
159 x0,
160 y0);
161 break;
162 }
163 case Flags.FIXED_POSITION | Flags.NON_ROTATING | Flags.NON_SCALING_SIZE: {
164 transform.setTransform(
165 1, 0, 0, 1,
166 x0,
167 y0);
168 break;
169 }
170 default:
171 break;
172 }
173 }
174
175 private static final class Flags {
176 private static final int NON_SCALING_SIZE = 1;
177 private static final int NON_ROTATING = 1 << 1;
178 private static final int FIXED_POSITION = 1 << 2;
179 }
180 }