Skip to content

Commit

Permalink
Copy androidsvg to project
Browse files Browse the repository at this point in the history
  • Loading branch information
ismartcoding committed Jul 2, 2024
1 parent 50c0eed commit 22b6221
Show file tree
Hide file tree
Showing 29 changed files with 16,784 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
import com.bumptech.glide.load.resource.bitmap.Downsampler
import com.bumptech.glide.request.RequestOptions
import com.caverock.androidsvg.SVG
import com.ismartcoding.lib.androidsvg.SVG
import com.ismartcoding.lib.extensions.compress
import com.ismartcoding.lib.extensions.getMediaContentUri
import com.ismartcoding.lib.extensions.isAudioFast
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ismartcoding.plain.helpers

import com.caverock.androidsvg.SVG
import com.ismartcoding.lib.androidsvg.SVG
import java.io.File
import androidx.compose.ui.unit.IntSize
import com.ismartcoding.lib.logcat.LogCat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.unit.IntSize
import coil3.compose.AsyncImage
import com.caverock.androidsvg.SVG
import com.ismartcoding.lib.androidsvg.SVG
import com.ismartcoding.plain.enums.DarkTheme
import com.ismartcoding.plain.preference.LocalDarkTheme
import com.ismartcoding.plain.ui.base.svg.parseDynamicColor
Expand Down
4 changes: 1 addition & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[versions]
androidGifDrawableVersion="1.2.23"
androidsvgAarVersion="1.4"
androidGifDrawableVersion="1.2.28"
appcompatVersion="1.7.0"
bcprovJdk15onVersion="1.70"
coil="3.0.0-alpha06"
Expand Down Expand Up @@ -57,7 +56,6 @@ pagingVersion = "3.3.0"

[libraries]
android-gif-drawable = { module = "pl.droidsonroids.gif:android-gif-drawable", version.ref = "androidGifDrawableVersion" }
androidsvg-aar = { module = "com.caverock:androidsvg-aar", version.ref = "androidsvgAarVersion" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompatVersion" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtxVersion" }
androidx-fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "fragmentKtxVersion" }
Expand Down
1 change: 0 additions & 1 deletion lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ dependencies {
api(libs.markwon.tables)
api(libs.markwon.latex)
api(libs.markwon.linkify)
api(libs.androidsvg.aar)
api(libs.okhttp)
implementation(libs.android.gif.drawable)

Expand Down
33 changes: 33 additions & 0 deletions lib/src/main/java/com/ismartcoding/lib/androidsvg/CSS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.ismartcoding.lib.androidsvg;

import com.ismartcoding.lib.androidsvg.utils.CSSBase;

/**
* This is a container for pre-parsed CSS that can be used to avoid parsing raw CSS string on each
* render. It can be passed to RenderOptions like this:
* <pre class="code-block">
* {@code
* CSS css = CSS.getFromString("...some complex and long css here that takes time to parse...")
* RenderOption renderOptions = RenderOptions.create();
* renderOptions.css(css) // And now you can reuse the already parsed css
* svg1.renderToCanvas(canvas, renderOptions);
* svg2.renderToCanvas(canvas, renderOptions);
* svg3.renderToCanvas(canvas, renderOptions);
* }
* </pre>
*/
public class CSS extends CSSBase {
private CSS(String css)
{
super(css);
}

/**
* @param css css string to parse
* @return pre-parsed CSS
*/
public static CSS getFromString(String css)
{
return new CSS(css);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
package com.ismartcoding.lib.androidsvg;

import com.ismartcoding.lib.androidsvg.utils.TextScanner;

import java.util.HashMap;
import java.util.Map;

public class PreserveAspectRatio
{
private final Alignment alignment;
private final Scale scale;

private static final Map<String, Alignment> aspectRatioKeywords = new HashMap<>(10);


/**
* Draw document at its natural position and scale.
*/
@SuppressWarnings("unused")
public static final PreserveAspectRatio UNSCALED = new PreserveAspectRatio(null, null);

/**
* Stretch horizontally and vertically to fill the viewport.
* <p>
* Equivalent to <code>preserveAspectRatio="none"</code> in an SVG.
*/
@SuppressWarnings("WeakerAccess")
public static final PreserveAspectRatio STRETCH = new PreserveAspectRatio(Alignment.none, null);

/**
* Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport.
* <p>
* The document will be centred in the viewport and may have blank strips at either the top and
* bottom of the viewport or at the sides.
* <p>
* Equivalent to <code>preserveAspectRatio="xMidYMid meet"</code> in an SVG.
*/
@SuppressWarnings("WeakerAccess")
public static final PreserveAspectRatio LETTERBOX = new PreserveAspectRatio(Alignment.xMidYMid, Scale.meet);

/**
* Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport.
* <p>
* The document will be positioned at the top of tall and narrow viewports, and at the left of short
* and wide viewports.
* <p>
* Equivalent to <code>preserveAspectRatio="xMinYMin meet"</code> in an SVG.
*/
@SuppressWarnings("unused")
public static final PreserveAspectRatio START = new PreserveAspectRatio(Alignment.xMinYMin, Scale.meet);

/**
* Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport.
* <p>
* The document will be positioned at the bottom of tall and narrow viewports, and at the right of short
* and wide viewports.
* <p>
* Equivalent to <code>preserveAspectRatio="xMaxYMax meet"</code> in an SVG.
*/
@SuppressWarnings("unused")
public static final PreserveAspectRatio END = new PreserveAspectRatio(Alignment.xMaxYMax, Scale.meet);

/**
* Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport.
* <p>
* The document will be positioned at the top of tall and narrow viewports, and at the centre of
* short and wide viewports.
* <p>
* Equivalent to <code>preserveAspectRatio="xMidYMin meet"</code> in an SVG.
*/
@SuppressWarnings("unused")
public static final PreserveAspectRatio TOP = new PreserveAspectRatio(Alignment.xMidYMin, Scale.meet);

/**
* Keep the document's aspect ratio, but scale it so that it fits neatly inside the viewport.
* <p>
* The document will be positioned at the bottom of tall and narrow viewports, and at the centre of
* short and wide viewports.
* <p>
* Equivalent to <code>preserveAspectRatio="xMidYMax meet"</code> in an SVG.
*/
@SuppressWarnings("unused")
public static final PreserveAspectRatio BOTTOM = new PreserveAspectRatio(Alignment.xMidYMax, Scale.meet);

/**
* Keep the document's aspect ratio, but scale it so that it fills the entire viewport.
* This may result in some of the document falling outside the viewport.
* <p>
* The document will be positioned so that the centre of the document will always be visible,
* but the edges of the document may not.
* <p>
* Equivalent to <code>preserveAspectRatio="xMidYMid slice"</code> in an SVG.
*/
@SuppressWarnings("unused")
public static final PreserveAspectRatio FULLSCREEN = new PreserveAspectRatio(Alignment.xMidYMid, Scale.slice);

/**
* Keep the document's aspect ratio, but scale it so that it fills the entire viewport.
* This may result in some of the document falling outside the viewport.
* <p>
* The document will be positioned so that the top left of the document will always be visible,
* but the right hand or bottom edge may not.
* <p>
* Equivalent to <code>preserveAspectRatio="xMinYMin slice"</code> in an SVG.
*/
@SuppressWarnings("unused")
public static final PreserveAspectRatio FULLSCREEN_START = new PreserveAspectRatio(Alignment.xMinYMin, Scale.slice);



/**
* Determines how the document is to me positioned relative to the viewport (normally the canvas).
* <p>
* For the value {@code none}, the document is stretched to fit the viewport dimensions. For all
* other values, the aspect ratio of the document is kept the same but the document is scaled to
* fit the viewport.
* @since 1.2.0
*/
public enum Alignment
{
/** 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. */
none,
/** Document is positioned at the top left of the viewport. */
xMinYMin,
/** Document is positioned at the centre top of the viewport. */
xMidYMin,
/** Document is positioned at the top right of the viewport. */
xMaxYMin,
/** Document is positioned at the middle left of the viewport. */
xMinYMid,
/** Document is centred in the viewport both vertically and horizontally. */
xMidYMid,
/** Document is positioned at the middle right of the viewport. */
xMaxYMid,
/** Document is positioned at the bottom left of the viewport. */
xMinYMax,
/** Document is positioned at the bottom centre of the viewport. */
xMidYMax,
/** Document is positioned at the bottom right of the viewport. */
xMaxYMax
}


/**
* Determine whether the scaled document fills the viewport entirely or is scaled to
* fill the viewport without overflowing.
* @since 1.2.0
*/
public enum Scale
{
/**
* The document is scaled so that it is as large as possible without overflowing the viewport.
* There may be blank areas on one or more sides of the document.
*/
meet,
/**
* The document is scaled so that entirely fills the viewport. That means that some of the
* document may fall outside the viewport and will not be rendered.
*/
slice
}


static {
aspectRatioKeywords.put("none", PreserveAspectRatio.Alignment.none);
aspectRatioKeywords.put("xMinYMin", PreserveAspectRatio.Alignment.xMinYMin);
aspectRatioKeywords.put("xMidYMin", PreserveAspectRatio.Alignment.xMidYMin);
aspectRatioKeywords.put("xMaxYMin", PreserveAspectRatio.Alignment.xMaxYMin);
aspectRatioKeywords.put("xMinYMid", PreserveAspectRatio.Alignment.xMinYMid);
aspectRatioKeywords.put("xMidYMid", PreserveAspectRatio.Alignment.xMidYMid);
aspectRatioKeywords.put("xMaxYMid", PreserveAspectRatio.Alignment.xMaxYMid);
aspectRatioKeywords.put("xMinYMax", PreserveAspectRatio.Alignment.xMinYMax);
aspectRatioKeywords.put("xMidYMax", PreserveAspectRatio.Alignment.xMidYMax);
aspectRatioKeywords.put("xMaxYMax", PreserveAspectRatio.Alignment.xMaxYMax);
}


/*
* Private constructor
*/
PreserveAspectRatio(Alignment alignment, Scale scale)
{
this.alignment = alignment;
this.scale = scale;
}


/**
* Parse the given SVG <code>preserveAspectRation</code> attribute value and return an equivalent
* instance of this class.
* @param value a string in the same format as an SVG {@code preserveAspectRatio} attribute
* @return a instance of this class
*/
public static PreserveAspectRatio of(String value)
{
try {
return parsePreserveAspectRatio(value);
} catch (SVGParseException e) {
throw new IllegalArgumentException(e.getMessage());
}
}


/**
* Returns the alignment value of this instance.
* @return the alignment
*/
@SuppressWarnings("WeakerAccess")
public Alignment getAlignment()
{
return alignment;
}


/**
* Returns the scale value of this instance.
* @return the scale
*/
@SuppressWarnings("WeakerAccess")
public Scale getScale()
{
return scale;
}


@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PreserveAspectRatio other = (PreserveAspectRatio) obj;
return (alignment == other.alignment && scale == other.scale);
}


@Override
public String toString()
{
return alignment + " " + scale;
}




private static PreserveAspectRatio parsePreserveAspectRatio(String val) throws SVGParseException
{
TextScanner scan = new TextScanner(val);
scan.skipWhitespace();

String word = scan.nextToken();
if ("defer".equals(word)) { // Ignore defer keyword
scan.skipWhitespace();
word = scan.nextToken();
}

PreserveAspectRatio.Alignment align = aspectRatioKeywords.get(word);
PreserveAspectRatio.Scale scale = null;

scan.skipWhitespace();

if (!scan.empty()) {
String meetOrSlice = scan.nextToken();
switch (meetOrSlice) {
case "meet":
scale = PreserveAspectRatio.Scale.meet; break;
case "slice":
scale = PreserveAspectRatio.Scale.slice; break;
default:
throw new SVGParseException("Invalid preserveAspectRatio definition: " + val);
}
}
return new PreserveAspectRatio(align, scale);
}

}
Loading

0 comments on commit 22b6221

Please sign in to comment.