|
| 1 | +package com.ismartcoding.lib.androidsvg; |
| 2 | + |
| 3 | +import com.ismartcoding.lib.androidsvg.utils.TextScanner; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +public class PreserveAspectRatio |
| 9 | +{ |
| 10 | + private final Alignment alignment; |
| 11 | + private final Scale scale; |
| 12 | + |
| 13 | + private static final Map<String, Alignment> aspectRatioKeywords = new HashMap<>(10); |
| 14 | + |
| 15 | + |
| 16 | + /** |
| 17 | + * Draw document at its natural position and scale. |
| 18 | + */ |
| 19 | + @SuppressWarnings("unused") |
| 20 | + public static final PreserveAspectRatio UNSCALED = new PreserveAspectRatio(null, null); |
| 21 | + |
| 22 | + /** |
| 23 | + * Stretch horizontally and vertically to fill the viewport. |
| 24 | + * <p> |
| 25 | + * Equivalent to <code>preserveAspectRatio="none"</code> in an SVG. |
| 26 | + */ |
| 27 | + @SuppressWarnings("WeakerAccess") |
| 28 | + public static final PreserveAspectRatio STRETCH = new PreserveAspectRatio(Alignment.none, null); |
| 29 | + |
| 30 | + /** |
| 31 | + * Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport. |
| 32 | + * <p> |
| 33 | + * The document will be centred in the viewport and may have blank strips at either the top and |
| 34 | + * bottom of the viewport or at the sides. |
| 35 | + * <p> |
| 36 | + * Equivalent to <code>preserveAspectRatio="xMidYMid meet"</code> in an SVG. |
| 37 | + */ |
| 38 | + @SuppressWarnings("WeakerAccess") |
| 39 | + public static final PreserveAspectRatio LETTERBOX = new PreserveAspectRatio(Alignment.xMidYMid, Scale.meet); |
| 40 | + |
| 41 | + /** |
| 42 | + * Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport. |
| 43 | + * <p> |
| 44 | + * The document will be positioned at the top of tall and narrow viewports, and at the left of short |
| 45 | + * and wide viewports. |
| 46 | + * <p> |
| 47 | + * Equivalent to <code>preserveAspectRatio="xMinYMin meet"</code> in an SVG. |
| 48 | + */ |
| 49 | + @SuppressWarnings("unused") |
| 50 | + public static final PreserveAspectRatio START = new PreserveAspectRatio(Alignment.xMinYMin, Scale.meet); |
| 51 | + |
| 52 | + /** |
| 53 | + * Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport. |
| 54 | + * <p> |
| 55 | + * The document will be positioned at the bottom of tall and narrow viewports, and at the right of short |
| 56 | + * and wide viewports. |
| 57 | + * <p> |
| 58 | + * Equivalent to <code>preserveAspectRatio="xMaxYMax meet"</code> in an SVG. |
| 59 | + */ |
| 60 | + @SuppressWarnings("unused") |
| 61 | + public static final PreserveAspectRatio END = new PreserveAspectRatio(Alignment.xMaxYMax, Scale.meet); |
| 62 | + |
| 63 | + /** |
| 64 | + * Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport. |
| 65 | + * <p> |
| 66 | + * The document will be positioned at the top of tall and narrow viewports, and at the centre of |
| 67 | + * short and wide viewports. |
| 68 | + * <p> |
| 69 | + * Equivalent to <code>preserveAspectRatio="xMidYMin meet"</code> in an SVG. |
| 70 | + */ |
| 71 | + @SuppressWarnings("unused") |
| 72 | + public static final PreserveAspectRatio TOP = new PreserveAspectRatio(Alignment.xMidYMin, Scale.meet); |
| 73 | + |
| 74 | + /** |
| 75 | + * Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport. |
| 76 | + * <p> |
| 77 | + * The document will be positioned at the bottom of tall and narrow viewports, and at the centre of |
| 78 | + * short and wide viewports. |
| 79 | + * <p> |
| 80 | + * Equivalent to <code>preserveAspectRatio="xMidYMax meet"</code> in an SVG. |
| 81 | + */ |
| 82 | + @SuppressWarnings("unused") |
| 83 | + public static final PreserveAspectRatio BOTTOM = new PreserveAspectRatio(Alignment.xMidYMax, Scale.meet); |
| 84 | + |
| 85 | + /** |
| 86 | + * Keep the document's aspect ratio, but scale it so that it fills the entire viewport. |
| 87 | + * This may result in some of the document falling outside the viewport. |
| 88 | + * <p> |
| 89 | + * The document will be positioned so that the centre of the document will always be visible, |
| 90 | + * but the edges of the document may not. |
| 91 | + * <p> |
| 92 | + * Equivalent to <code>preserveAspectRatio="xMidYMid slice"</code> in an SVG. |
| 93 | + */ |
| 94 | + @SuppressWarnings("unused") |
| 95 | + public static final PreserveAspectRatio FULLSCREEN = new PreserveAspectRatio(Alignment.xMidYMid, Scale.slice); |
| 96 | + |
| 97 | + /** |
| 98 | + * Keep the document's aspect ratio, but scale it so that it fills the entire viewport. |
| 99 | + * This may result in some of the document falling outside the viewport. |
| 100 | + * <p> |
| 101 | + * The document will be positioned so that the top left of the document will always be visible, |
| 102 | + * but the right hand or bottom edge may not. |
| 103 | + * <p> |
| 104 | + * Equivalent to <code>preserveAspectRatio="xMinYMin slice"</code> in an SVG. |
| 105 | + */ |
| 106 | + @SuppressWarnings("unused") |
| 107 | + public static final PreserveAspectRatio FULLSCREEN_START = new PreserveAspectRatio(Alignment.xMinYMin, Scale.slice); |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + /** |
| 112 | + * Determines how the document is to me positioned relative to the viewport (normally the canvas). |
| 113 | + * <p> |
| 114 | + * For the value {@code none}, the document is stretched to fit the viewport dimensions. For all |
| 115 | + * other values, the aspect ratio of the document is kept the same but the document is scaled to |
| 116 | + * fit the viewport. |
| 117 | + * @since 1.2.0 |
| 118 | + */ |
| 119 | + public enum Alignment |
| 120 | + { |
| 121 | + /** Document is stretched to fit both the width and height of the viewport. When using this Alignment value, the value of Scale is not used and will be ignored. */ |
| 122 | + none, |
| 123 | + /** Document is positioned at the top left of the viewport. */ |
| 124 | + xMinYMin, |
| 125 | + /** Document is positioned at the centre top of the viewport. */ |
| 126 | + xMidYMin, |
| 127 | + /** Document is positioned at the top right of the viewport. */ |
| 128 | + xMaxYMin, |
| 129 | + /** Document is positioned at the middle left of the viewport. */ |
| 130 | + xMinYMid, |
| 131 | + /** Document is centred in the viewport both vertically and horizontally. */ |
| 132 | + xMidYMid, |
| 133 | + /** Document is positioned at the middle right of the viewport. */ |
| 134 | + xMaxYMid, |
| 135 | + /** Document is positioned at the bottom left of the viewport. */ |
| 136 | + xMinYMax, |
| 137 | + /** Document is positioned at the bottom centre of the viewport. */ |
| 138 | + xMidYMax, |
| 139 | + /** Document is positioned at the bottom right of the viewport. */ |
| 140 | + xMaxYMax |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + /** |
| 145 | + * Determine whether the scaled document fills the viewport entirely or is scaled to |
| 146 | + * fill the viewport without overflowing. |
| 147 | + * @since 1.2.0 |
| 148 | + */ |
| 149 | + public enum Scale |
| 150 | + { |
| 151 | + /** |
| 152 | + * The document is scaled so that it is as large as possible without overflowing the viewport. |
| 153 | + * There may be blank areas on one or more sides of the document. |
| 154 | + */ |
| 155 | + meet, |
| 156 | + /** |
| 157 | + * The document is scaled so that entirely fills the viewport. That means that some of the |
| 158 | + * document may fall outside the viewport and will not be rendered. |
| 159 | + */ |
| 160 | + slice |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + static { |
| 165 | + aspectRatioKeywords.put("none", PreserveAspectRatio.Alignment.none); |
| 166 | + aspectRatioKeywords.put("xMinYMin", PreserveAspectRatio.Alignment.xMinYMin); |
| 167 | + aspectRatioKeywords.put("xMidYMin", PreserveAspectRatio.Alignment.xMidYMin); |
| 168 | + aspectRatioKeywords.put("xMaxYMin", PreserveAspectRatio.Alignment.xMaxYMin); |
| 169 | + aspectRatioKeywords.put("xMinYMid", PreserveAspectRatio.Alignment.xMinYMid); |
| 170 | + aspectRatioKeywords.put("xMidYMid", PreserveAspectRatio.Alignment.xMidYMid); |
| 171 | + aspectRatioKeywords.put("xMaxYMid", PreserveAspectRatio.Alignment.xMaxYMid); |
| 172 | + aspectRatioKeywords.put("xMinYMax", PreserveAspectRatio.Alignment.xMinYMax); |
| 173 | + aspectRatioKeywords.put("xMidYMax", PreserveAspectRatio.Alignment.xMidYMax); |
| 174 | + aspectRatioKeywords.put("xMaxYMax", PreserveAspectRatio.Alignment.xMaxYMax); |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + /* |
| 179 | + * Private constructor |
| 180 | + */ |
| 181 | + PreserveAspectRatio(Alignment alignment, Scale scale) |
| 182 | + { |
| 183 | + this.alignment = alignment; |
| 184 | + this.scale = scale; |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + /** |
| 189 | + * Parse the given SVG <code>preserveAspectRation</code> attribute value and return an equivalent |
| 190 | + * instance of this class. |
| 191 | + * @param value a string in the same format as an SVG {@code preserveAspectRatio} attribute |
| 192 | + * @return a instance of this class |
| 193 | + */ |
| 194 | + public static PreserveAspectRatio of(String value) |
| 195 | + { |
| 196 | + try { |
| 197 | + return parsePreserveAspectRatio(value); |
| 198 | + } catch (SVGParseException e) { |
| 199 | + throw new IllegalArgumentException(e.getMessage()); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + |
| 204 | + /** |
| 205 | + * Returns the alignment value of this instance. |
| 206 | + * @return the alignment |
| 207 | + */ |
| 208 | + @SuppressWarnings("WeakerAccess") |
| 209 | + public Alignment getAlignment() |
| 210 | + { |
| 211 | + return alignment; |
| 212 | + } |
| 213 | + |
| 214 | + |
| 215 | + /** |
| 216 | + * Returns the scale value of this instance. |
| 217 | + * @return the scale |
| 218 | + */ |
| 219 | + @SuppressWarnings("WeakerAccess") |
| 220 | + public Scale getScale() |
| 221 | + { |
| 222 | + return scale; |
| 223 | + } |
| 224 | + |
| 225 | + |
| 226 | + @Override |
| 227 | + public boolean equals(Object obj) |
| 228 | + { |
| 229 | + if (this == obj) |
| 230 | + return true; |
| 231 | + if (obj == null) |
| 232 | + return false; |
| 233 | + if (getClass() != obj.getClass()) |
| 234 | + return false; |
| 235 | + PreserveAspectRatio other = (PreserveAspectRatio) obj; |
| 236 | + return (alignment == other.alignment && scale == other.scale); |
| 237 | + } |
| 238 | + |
| 239 | + |
| 240 | + @Override |
| 241 | + public String toString() |
| 242 | + { |
| 243 | + return alignment + " " + scale; |
| 244 | + } |
| 245 | + |
| 246 | + |
| 247 | + |
| 248 | + |
| 249 | + private static PreserveAspectRatio parsePreserveAspectRatio(String val) throws SVGParseException |
| 250 | + { |
| 251 | + TextScanner scan = new TextScanner(val); |
| 252 | + scan.skipWhitespace(); |
| 253 | + |
| 254 | + String word = scan.nextToken(); |
| 255 | + if ("defer".equals(word)) { // Ignore defer keyword |
| 256 | + scan.skipWhitespace(); |
| 257 | + word = scan.nextToken(); |
| 258 | + } |
| 259 | + |
| 260 | + PreserveAspectRatio.Alignment align = aspectRatioKeywords.get(word); |
| 261 | + PreserveAspectRatio.Scale scale = null; |
| 262 | + |
| 263 | + scan.skipWhitespace(); |
| 264 | + |
| 265 | + if (!scan.empty()) { |
| 266 | + String meetOrSlice = scan.nextToken(); |
| 267 | + switch (meetOrSlice) { |
| 268 | + case "meet": |
| 269 | + scale = PreserveAspectRatio.Scale.meet; break; |
| 270 | + case "slice": |
| 271 | + scale = PreserveAspectRatio.Scale.slice; break; |
| 272 | + default: |
| 273 | + throw new SVGParseException("Invalid preserveAspectRatio definition: " + val); |
| 274 | + } |
| 275 | + } |
| 276 | + return new PreserveAspectRatio(align, scale); |
| 277 | + } |
| 278 | + |
| 279 | +} |
0 commit comments