| •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.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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
None(AlignType.Min, AlignType.Min), |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
xMinYMin(AlignType.Min, AlignType.Min), |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
xMidYMin(AlignType.Mid, AlignType.Min), |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
xMaxYMin(AlignType.Max, AlignType.Min), |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
xMinYMid(AlignType.Min, AlignType.Mid), |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@Default |
| 100 |
xMidYMid(AlignType.Mid, AlignType.Mid), |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
xMaxYMid(AlignType.Max, AlignType.Mid), |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
xMinYMax(AlignType.Min, AlignType.Max), |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
xMidYMax(AlignType.Mid, AlignType.Max), |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
@Default |
| 153 |
Meet, |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 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 |
|
| 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 |
} |