-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
382 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.xerdnu.blastedimage; | ||
|
||
import androidx.annotation.NonNull; | ||
import com.bumptech.glide.load.Options; | ||
import com.bumptech.glide.load.ResourceDecoder; | ||
import com.bumptech.glide.load.engine.Resource; | ||
import com.bumptech.glide.load.resource.SimpleResource; | ||
import com.caverock.androidsvg.SVG; | ||
import com.caverock.androidsvg.SVGParseException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class SvgDecoder implements ResourceDecoder<InputStream, SVG> { | ||
@Override | ||
public boolean handles(@NonNull InputStream source, @NonNull Options options) { | ||
return true; // Assume all InputStreams are SVG | ||
} | ||
|
||
@Override | ||
public Resource<SVG> decode(@NonNull InputStream source, int width, int height, @NonNull Options options) | ||
throws IOException { | ||
try { | ||
SVG svg = SVG.getFromInputStream(source); | ||
return new SimpleResource<>(svg); | ||
} catch (SVGParseException e) { | ||
throw new IOException("Failed to parse SVG", e); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
android/src/main/java/com/reactlibrary/SvgDrawableResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.xerdnu.blastedimage; | ||
|
||
import android.graphics.drawable.PictureDrawable; | ||
import androidx.annotation.NonNull; | ||
import com.bumptech.glide.load.engine.Resource; | ||
|
||
public class SvgDrawableResource implements Resource<PictureDrawable> { | ||
private final PictureDrawable drawable; | ||
|
||
public SvgDrawableResource(PictureDrawable drawable) { | ||
if (drawable == null) { | ||
throw new NullPointerException("PictureDrawable must not be null"); | ||
} | ||
this.drawable = drawable; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Class<PictureDrawable> getResourceClass() { | ||
return PictureDrawable.class; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public PictureDrawable get() { | ||
return drawable; | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public void recycle() { | ||
// Do nothing here | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
android/src/main/java/com/reactlibrary/SvgDrawableTranscoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.xerdnu.blastedimage; | ||
|
||
import android.graphics.Picture; | ||
import android.graphics.drawable.PictureDrawable; | ||
|
||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.bumptech.glide.load.Options; | ||
import com.bumptech.glide.load.engine.Resource; | ||
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder; | ||
import com.caverock.androidsvg.SVG; | ||
|
||
public class SvgDrawableTranscoder implements ResourceTranscoder<SVG, PictureDrawable> { | ||
@Nullable | ||
@Override | ||
public Resource<PictureDrawable> transcode( | ||
@NonNull Resource<SVG> toTranscode, | ||
@NonNull Options options | ||
) { | ||
SVG svg = toTranscode.get(); | ||
Picture picture = svg.renderToPicture(); | ||
PictureDrawable drawable = new PictureDrawable(picture); | ||
return new SvgDrawableResource(drawable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.xerdnu.blastedimage; | ||
|
||
import android.content.Context; | ||
import android.graphics.drawable.PictureDrawable; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.bumptech.glide.Glide; | ||
import com.bumptech.glide.Registry; | ||
import com.bumptech.glide.annotation.GlideModule; | ||
import com.bumptech.glide.module.AppGlideModule; | ||
import com.caverock.androidsvg.SVG; | ||
|
||
import java.io.InputStream; | ||
|
||
@GlideModule | ||
public class SvgModule extends AppGlideModule { | ||
@Override | ||
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) { | ||
Log.d("SvgModule", "Registering SVG support in Glide"); | ||
registry | ||
.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder()) | ||
.append(InputStream.class, SVG.class, new SvgDecoder()); | ||
} | ||
|
||
@Override | ||
public boolean isManifestParsingEnabled() { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.