Skip to content

Commit 22b6221

Browse files
committed
Copy androidsvg to project
1 parent 50c0eed commit 22b6221

29 files changed

+16784
-11
lines changed

app/src/main/java/com/ismartcoding/plain/extensions/File.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy
1414
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
1515
import com.bumptech.glide.load.resource.bitmap.Downsampler
1616
import com.bumptech.glide.request.RequestOptions
17-
import com.caverock.androidsvg.SVG
17+
import com.ismartcoding.lib.androidsvg.SVG
1818
import com.ismartcoding.lib.extensions.compress
1919
import com.ismartcoding.lib.extensions.getMediaContentUri
2020
import com.ismartcoding.lib.extensions.isAudioFast

app/src/main/java/com/ismartcoding/plain/helpers/SvgHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.ismartcoding.plain.helpers
22

3-
import com.caverock.androidsvg.SVG
3+
import com.ismartcoding.lib.androidsvg.SVG
44
import java.io.File
55
import androidx.compose.ui.unit.IntSize
66
import com.ismartcoding.lib.logcat.LogCat

app/src/main/java/com/ismartcoding/plain/ui/base/DynamicSVGImage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import androidx.compose.ui.Modifier
1414
import androidx.compose.ui.layout.onGloballyPositioned
1515
import androidx.compose.ui.unit.IntSize
1616
import coil3.compose.AsyncImage
17-
import com.caverock.androidsvg.SVG
17+
import com.ismartcoding.lib.androidsvg.SVG
1818
import com.ismartcoding.plain.enums.DarkTheme
1919
import com.ismartcoding.plain.preference.LocalDarkTheme
2020
import com.ismartcoding.plain.ui.base.svg.parseDynamicColor

gradle/libs.versions.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[versions]
2-
androidGifDrawableVersion="1.2.23"
3-
androidsvgAarVersion="1.4"
2+
androidGifDrawableVersion="1.2.28"
43
appcompatVersion="1.7.0"
54
bcprovJdk15onVersion="1.70"
65
coil="3.0.0-alpha06"
@@ -57,7 +56,6 @@ pagingVersion = "3.3.0"
5756

5857
[libraries]
5958
android-gif-drawable = { module = "pl.droidsonroids.gif:android-gif-drawable", version.ref = "androidGifDrawableVersion" }
60-
androidsvg-aar = { module = "com.caverock:androidsvg-aar", version.ref = "androidsvgAarVersion" }
6159
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompatVersion" }
6260
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtxVersion" }
6361
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "fragmentKtxVersion" }

lib/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ dependencies {
9292
api(libs.markwon.tables)
9393
api(libs.markwon.latex)
9494
api(libs.markwon.linkify)
95-
api(libs.androidsvg.aar)
9695
api(libs.okhttp)
9796
implementation(libs.android.gif.drawable)
9897

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.ismartcoding.lib.androidsvg;
2+
3+
import com.ismartcoding.lib.androidsvg.utils.CSSBase;
4+
5+
/**
6+
* This is a container for pre-parsed CSS that can be used to avoid parsing raw CSS string on each
7+
* render. It can be passed to RenderOptions like this:
8+
* <pre class="code-block">
9+
* {@code
10+
* CSS css = CSS.getFromString("...some complex and long css here that takes time to parse...")
11+
* RenderOption renderOptions = RenderOptions.create();
12+
* renderOptions.css(css) // And now you can reuse the already parsed css
13+
* svg1.renderToCanvas(canvas, renderOptions);
14+
* svg2.renderToCanvas(canvas, renderOptions);
15+
* svg3.renderToCanvas(canvas, renderOptions);
16+
* }
17+
* </pre>
18+
*/
19+
public class CSS extends CSSBase {
20+
private CSS(String css)
21+
{
22+
super(css);
23+
}
24+
25+
/**
26+
* @param css css string to parse
27+
* @return pre-parsed CSS
28+
*/
29+
public static CSS getFromString(String css)
30+
{
31+
return new CSS(css);
32+
}
33+
}
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
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

Comments
 (0)