Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-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.geom.AffineTransform;
25 import java.util.Objects;
26
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.github.weisj.jsvg.parser.impl.AttributeParser;
31 import com.github.weisj.jsvg.parser.impl.SeparatorMode;
32 import com.github.weisj.jsvg.view.FloatSize;
33 import com.github.weisj.jsvg.view.ViewBox;
34
35
36 public final class PreserveAspectRatio {
37
38 private enum AlignType {
39 Min {
40 @Override
41 double align(double size1, double size2) {
42 return 0;
43 }
44 },
45 Mid {
46 @Override
47 double align(double size1, double size2) {
48 return (size1 - size2) / 2;
49 }
50 },
51 Max {
52 @Override
53 double align(double size1, double size2) {
54 return size1 - size2;
55 }
56 };
57
58 abstract double align(double size1, double size2);
59 }
60
61 public enum Align {
62
63 /**
64 * Do not force uniform scaling.
65 * Scale the graphic content of the given element non-uniformly if necessary such that the
66 * element's bounding box exactly matches the viewport rectangle.
67 * Note that if [align] is none, then the optional [meetOrSlice] value is ignored.
68 */
69 None(AlignType.Min, AlignType.Min),
70 /**
71 * Force uniform scaling.
72 * Align the [min-x] of the element's viewBox with the smallest X value of the viewport.
73 * Align the [min-y] of the element's viewBox with the smallest Y value of the viewport.
74 */
75 xMinYMin(AlignType.Min, AlignType.Min),
76 /**
77 * Force uniform scaling.
78 * Align the midpoint X value of the element's viewBox with the midpoint X value of the viewport.
79 * Align the [min-y] of the element's viewBox with the smallest Y value of the viewport.
80 */
81 xMidYMin(AlignType.Mid, AlignType.Min),
82 /**
83 * Force uniform scaling.
84 * Align the [min-x]+[width] of the element's viewBox with the maximum X value of the viewport.
85 * Align the [min-y] of the element's viewBox with the smallest Y value of the viewport.
86 */
87 xMaxYMin(AlignType.Max, AlignType.Min),
88 /**
89 * Force uniform scaling.
90 * Align the [min-x] of the element's viewBox with the smallest X value of the viewport.
91 * Align the midpoint Y value of the element's viewBox with the midpoint Y value of the viewport.
92 */
93 xMinYMid(AlignType.Min, AlignType.Mid),
94 /**
95 * Force uniform scaling.
96 * Align the midpoint X value of the element's viewBox with the midpoint X value of the viewport.
97 * Align the midpoint Y value of the element's viewBox with the midpoint Y value of the viewport.
98 */
99 @Default
100 xMidYMid(AlignType.Mid, AlignType.Mid),
101 /**
102 * Force uniform scaling.
103 * Align the [min-x]+[width] of the element's viewBox with the maximum X value of the viewport.
104 * Align the midpoint Y value of the element's viewBox with the midpoint Y value of the viewport.
105 */
106 xMaxYMid(AlignType.Max, AlignType.Mid),
107 /**
108 * Force uniform scaling.
109 * Align the [min-x] of the element's viewBox with the smallest X value of the viewport.
110 * Align the [min-y]+[height] of the element's viewBox with the maximum Y value of the viewport.
111 */
112 xMinYMax(AlignType.Min, AlignType.Max),
113 /**
114 * Force uniform scaling.
115 * Align the midpoint X value of the element's viewBox with the midpoint X value of the viewport.
116 * Align the [min-y]+[height] of the element's viewBox with the maximum Y value of the viewport.
117 */
118 xMidYMax(AlignType.Mid, AlignType.Max),
119 /**
120 * Force uniform scaling.
121 * Align the [min-x]+[width] of the element's viewBox with the maximum X value of the viewport.
122 * Align the [min-y]+[height] of the element's viewBox with the maximum Y value of the viewport.
123 */
124 xMaxYMax(AlignType.Max, AlignType.Max);
125
126 private final @NotNull AlignType xAlign;
127 private final @NotNull AlignType yAlign;
128
129 Align(@NotNull AlignType xAlign, @NotNull AlignType yAlign) {
130 this.xAlign = xAlign;
131 this.yAlign = yAlign;
132 }
133
134 @Override
135 public String toString() {
136 return name() + "{" + xAlign + ", " + yAlign + "}";
137 }
138 }
139
140 public enum MeetOrSlice {
141 /**
142 * Scale the graphic such that:
143 * <p>
144 * - aspect ratio is preserved
145 * - the entire viewBox is visible within the viewport
146 * - the viewBox is scaled up as much as possible, while still meeting the other criteria
147 * <p>
148 * In this case, if the aspect ratio of the graphic does not match the viewport,
149 * some viewport will extend beyond the bounds of the viewBox
150 * (i.e., the area into which the viewBox will draw will be smaller than the viewport).
151 */
152 @Default
153 Meet,
154 /**
155 * Scale the graphic such that:
156 * <p>
157 * - aspect ratio is preserved
158 * - the entire viewport is covered by the viewBox
159 * - the viewBox is scaled down as much as possible, while still meeting the other criteria
160 * <p>
161 * In this case, if the aspect ratio of the viewBox does not match the viewport, some
162 * viewBox will extend beyond the bounds of the viewport
163 * (i.e., the area into which the viewBox will draw is larger than the viewport).
164 */
165 Slice
166 }
167
168 public final @NotNull Align align;
169 public final @NotNull MeetOrSlice meetOrSlice;
170
171 private PreserveAspectRatio(@NotNull Align align, @NotNull MeetOrSlice meetOrSlice) {
172 this.align = align;
173 this.meetOrSlice = meetOrSlice;
174 }
175
176 public static @NotNull PreserveAspectRatio none() {
177 return new PreserveAspectRatio(Align.None, MeetOrSlice.Meet);
178 }
179
180 public static @NotNull PreserveAspectRatio forDisplay() {
181 return new PreserveAspectRatio(Align.xMidYMid, MeetOrSlice.Meet);
182 }
183
184 public static @NotNull PreserveAspectRatio parse(@Nullable String preserveAspectRation,
185 @NotNull AttributeParser parser) {
186 return parse(preserveAspectRation, null, parser);
187 }
188
189 public static @NotNull PreserveAspectRatio parse(@Nullable String preserveAspectRation,
190 @Nullable PreserveAspectRatio fallback, @NotNull AttributeParser parser) {
191 Align align = Align.xMidYMid;
192 MeetOrSlice meetOrSlice = MeetOrSlice.Meet;
193 if (preserveAspectRation == null) {
194 return fallback != null ? fallback : new PreserveAspectRatio(align, meetOrSlice);
195 }
196 String[] components =
197 parser.parseStringList(preserveAspectRation, SeparatorMode.COMMA_AND_WHITESPACE);
198 if (components.length < 1 || components.length > 2)
199 throw new IllegalArgumentException("Too many arguments specified: " + preserveAspectRation);
200 align = parser.parseEnum(components[0], align);
201 if (components.length > 1)
202 meetOrSlice = parser.parseEnum(components[1], meetOrSlice);
203 return new PreserveAspectRatio(align, meetOrSlice);
204 }
205
206 @Override
207 public boolean equals(Object o) {
208 if (this == o) return true;
209 if (!(o instanceof PreserveAspectRatio)) return false;
210 PreserveAspectRatio that = (PreserveAspectRatio) o;
211 return align == that.align && meetOrSlice == that.meetOrSlice;
212 }
213
214 @Override
215 public int hashCode() {
216 return Objects.hash(align, meetOrSlice);
217 }
218
219 // https://www.w3.org/TR/SVG2/coords.html#ComputingAViewportsTransform
220 public @NotNull AffineTransform computeViewportTransform(@NotNull FloatSize size, @NotNull ViewBox viewBox) {
221 AffineTransform viewTransform = new AffineTransform();
222 if (align == Align.None) {
223 viewTransform.scale(size.width / viewBox.width, size.height / viewBox.height);
224 } else {
225 double xScale = size.width / viewBox.width;
226 double yScale = size.height / viewBox.height;
227
228 switch (meetOrSlice) {
229 case Meet:
230 xScale = yScale = Math.min(xScale, yScale);
231 break;
232 case Slice:
233 xScale = yScale = Math.max(xScale, yScale);
234 break;
235 default:
236 throw new IllegalStateException();
237 }
238
239 viewTransform.translate(
240 align.xAlign.align(size.width, viewBox.width * xScale),
241 align.yAlign.align(size.height, viewBox.height * yScale));
242 viewTransform.scale(xScale, yScale);
243 }
244 viewTransform.translate(-viewBox.x, -viewBox.y);
245 return viewTransform;
246 }
247
248 @Override
249 public String toString() {
250 return "PreserveAspectRatio{" +
251 "align=" + align +
252 ", meetOrSlice=" + meetOrSlice +
253 '}';
254 }
255 }