From fed2cd05e42aede4c8596cb25fc77486d49105bf Mon Sep 17 00:00:00 2001 From: Emux <devemux86@gmail.com> Date: Thu, 14 Jul 2016 21:18:04 +0300 Subject: [PATCH] Render themes: SVG resources, closes #60 --- vtm-android-gdx/build.gradle | 1 + .../oscim/android/canvas/AndroidGraphics.java | 10 +++ .../android/canvas/AndroidSvgBitmap.java | 59 ++++++++++++++++++ vtm-android/build.gradle | 1 + .../oscim/android/canvas/AndroidGraphics.java | 10 +++ .../android/canvas/AndroidSvgBitmap.java | 59 ++++++++++++++++++ vtm-desktop/build.gradle | 1 + .../src/org/oscim/awt/AwtGraphics.java | 10 +++ .../src/org/oscim/awt/AwtSvgBitmap.java | 62 +++++++++++++++++++ .../org/oscim/ios/backend/IosGraphics.java | 6 ++ .../org/oscim/gdx/client/GwtGdxGraphics.java | 5 ++ vtm/src/org/oscim/backend/CanvasAdapter.java | 19 +++++- vtm/src/org/oscim/theme/XmlThemeBuilder.java | 3 +- 13 files changed, 244 insertions(+), 2 deletions(-) create mode 100644 vtm-android-gdx/src/org/oscim/android/canvas/AndroidSvgBitmap.java create mode 100644 vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java create mode 100644 vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java diff --git a/vtm-android-gdx/build.gradle b/vtm-android-gdx/build.gradle index 735a8ae5..5eb1a45b 100644 --- a/vtm-android-gdx/build.gradle +++ b/vtm-android-gdx/build.gradle @@ -5,6 +5,7 @@ dependencies { compile project(':vtm-gdx') compile project(':vtm-themes') compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" + compile 'com.caverock:androidsvg:1.2.2-beta-1' compile 'com.noveogroup.android:android-logger:1.3.6' } diff --git a/vtm-android-gdx/src/org/oscim/android/canvas/AndroidGraphics.java b/vtm-android-gdx/src/org/oscim/android/canvas/AndroidGraphics.java index 34d229e2..5165e7a8 100644 --- a/vtm-android-gdx/src/org/oscim/android/canvas/AndroidGraphics.java +++ b/vtm-android-gdx/src/org/oscim/android/canvas/AndroidGraphics.java @@ -55,6 +55,16 @@ public final class AndroidGraphics extends CanvasAdapter { return new AndroidBitmap(inputStream); } + @Override + public Bitmap decodeSvgBitmapImpl(InputStream inputStream) { + try { + return new AndroidSvgBitmap(inputStream); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + @Override public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src) { try { diff --git a/vtm-android-gdx/src/org/oscim/android/canvas/AndroidSvgBitmap.java b/vtm-android-gdx/src/org/oscim/android/canvas/AndroidSvgBitmap.java new file mode 100644 index 00000000..cb510e90 --- /dev/null +++ b/vtm-android-gdx/src/org/oscim/android/canvas/AndroidSvgBitmap.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016 devemux86 + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.oscim.android.canvas; + +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Picture; +import android.graphics.RectF; + +import com.caverock.androidsvg.SVG; + +import org.oscim.backend.CanvasAdapter; + +import java.io.IOException; +import java.io.InputStream; + +public class AndroidSvgBitmap extends AndroidBitmap { + private static final float DEFAULT_SIZE = 400f; + + private static android.graphics.Bitmap getResourceBitmap(InputStream inputStream) throws IOException { + synchronized (SVG.getVersion()) { + try { + SVG svg = SVG.getFromInputStream(inputStream); + Picture picture = svg.renderToPicture(); + + float scaleFactor = CanvasAdapter.dpi / 160; + double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / DEFAULT_SIZE); + + float bitmapWidth = (float) (picture.getWidth() * scale); + float bitmapHeight = (float) (picture.getHeight() * scale); + + android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bitmapWidth), + (int) Math.ceil(bitmapHeight), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + canvas.drawPicture(picture, new RectF(0, 0, bitmapWidth, bitmapHeight)); + + return bitmap; + } catch (Exception e) { + throw new IOException(e); + } + } + } + + AndroidSvgBitmap(InputStream inputStream) throws IOException { + super(getResourceBitmap(inputStream)); + } +} diff --git a/vtm-android/build.gradle b/vtm-android/build.gradle index 1bdc01d8..8cdec00d 100644 --- a/vtm-android/build.gradle +++ b/vtm-android/build.gradle @@ -3,6 +3,7 @@ apply plugin: 'com.github.dcendents.android-maven' dependencies { compile project(':vtm') + compile 'com.caverock:androidsvg:1.2.2-beta-1' } android { diff --git a/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java b/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java index 34d229e2..5165e7a8 100644 --- a/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java +++ b/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java @@ -55,6 +55,16 @@ public final class AndroidGraphics extends CanvasAdapter { return new AndroidBitmap(inputStream); } + @Override + public Bitmap decodeSvgBitmapImpl(InputStream inputStream) { + try { + return new AndroidSvgBitmap(inputStream); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + @Override public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src) { try { diff --git a/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java b/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java new file mode 100644 index 00000000..cb510e90 --- /dev/null +++ b/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java @@ -0,0 +1,59 @@ +/* + * Copyright 2016 devemux86 + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.oscim.android.canvas; + +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Picture; +import android.graphics.RectF; + +import com.caverock.androidsvg.SVG; + +import org.oscim.backend.CanvasAdapter; + +import java.io.IOException; +import java.io.InputStream; + +public class AndroidSvgBitmap extends AndroidBitmap { + private static final float DEFAULT_SIZE = 400f; + + private static android.graphics.Bitmap getResourceBitmap(InputStream inputStream) throws IOException { + synchronized (SVG.getVersion()) { + try { + SVG svg = SVG.getFromInputStream(inputStream); + Picture picture = svg.renderToPicture(); + + float scaleFactor = CanvasAdapter.dpi / 160; + double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / DEFAULT_SIZE); + + float bitmapWidth = (float) (picture.getWidth() * scale); + float bitmapHeight = (float) (picture.getHeight() * scale); + + android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bitmapWidth), + (int) Math.ceil(bitmapHeight), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + canvas.drawPicture(picture, new RectF(0, 0, bitmapWidth, bitmapHeight)); + + return bitmap; + } catch (Exception e) { + throw new IOException(e); + } + } + } + + AndroidSvgBitmap(InputStream inputStream) throws IOException { + super(getResourceBitmap(inputStream)); + } +} diff --git a/vtm-desktop/build.gradle b/vtm-desktop/build.gradle index 76171c02..dbfa4e9d 100644 --- a/vtm-desktop/build.gradle +++ b/vtm-desktop/build.gradle @@ -8,6 +8,7 @@ dependencies { compile files('natives') compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" compile "com.badlogicgames.gdx:gdx-backend-jglfw:$gdxVersion" + compile 'com.kitfox.svg:svg-salamander:1.0' compile 'org.slf4j:slf4j-simple:1.7.21' } diff --git a/vtm-desktop/src/org/oscim/awt/AwtGraphics.java b/vtm-desktop/src/org/oscim/awt/AwtGraphics.java index ba15e3fd..a7cef279 100644 --- a/vtm-desktop/src/org/oscim/awt/AwtGraphics.java +++ b/vtm-desktop/src/org/oscim/awt/AwtGraphics.java @@ -100,6 +100,16 @@ public class AwtGraphics extends CanvasAdapter { } } + @Override + public Bitmap decodeSvgBitmapImpl(InputStream inputStream) { + try { + return new AwtSvgBitmap(inputStream); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + @Override public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src) { try { diff --git a/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java b/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java new file mode 100644 index 00000000..5d9a6d4f --- /dev/null +++ b/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java @@ -0,0 +1,62 @@ +/* + * Copyright 2016 devemux86 + * + * This program is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * this program. If not, see <http://www.gnu.org/licenses/>. + */ +package org.oscim.awt; + +import com.kitfox.svg.SVGCache; +import com.kitfox.svg.SVGDiagram; +import com.kitfox.svg.app.beans.SVGIcon; + +import org.oscim.backend.CanvasAdapter; + +import java.awt.Dimension; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; + +public class AwtSvgBitmap extends AwtBitmap { + private static final float DEFAULT_SIZE = 400f; + + private static BufferedImage getResourceBitmap(InputStream inputStream) throws IOException { + synchronized (SVGCache.getSVGUniverse()) { + try { + URI uri = SVGCache.getSVGUniverse().loadSVG(inputStream, Integer.toString(inputStream.hashCode())); + SVGDiagram diagram = SVGCache.getSVGUniverse().getDiagram(uri); + + float scaleFactor = CanvasAdapter.dpi / 240; + double scale = scaleFactor / Math.sqrt((diagram.getHeight() * diagram.getWidth()) / DEFAULT_SIZE); + + float bitmapWidth = (float) (diagram.getWidth() * scale); + float bitmapHeight = (float) (diagram.getHeight() * scale); + + SVGIcon icon = new SVGIcon(); + icon.setAntiAlias(true); + icon.setPreferredSize(new Dimension((int) bitmapWidth, (int) bitmapHeight)); + icon.setScaleToFit(true); + icon.setSvgURI(uri); + BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); + icon.paintIcon(null, bufferedImage.createGraphics(), 0, 0); + + return bufferedImage; + } catch (Exception e) { + throw new IOException(e); + } + } + } + + AwtSvgBitmap(InputStream inputStream) throws IOException { + super(getResourceBitmap(inputStream)); + } +} diff --git a/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java b/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java index 6b1f2fdc..57e38dac 100644 --- a/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java +++ b/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java @@ -62,6 +62,12 @@ public class IosGraphics extends CanvasAdapter { } } + @Override + protected Bitmap decodeSvgBitmapImpl(InputStream inputStream) { + // TODO + return null; + } + @Override protected Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src) { try { diff --git a/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java b/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java index 92d2e2ae..fb91935f 100644 --- a/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java +++ b/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java @@ -53,6 +53,11 @@ public class GwtGdxGraphics extends CanvasAdapter { return null; } + @Override + public Bitmap decodeSvgBitmapImpl(InputStream in) { + return null; + } + @Override public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src) { String pathName = (relativePathPrefix == null || relativePathPrefix.length() == 0 ? "" : relativePathPrefix + File.separatorChar) + src; diff --git a/vtm/src/org/oscim/backend/CanvasAdapter.java b/vtm/src/org/oscim/backend/CanvasAdapter.java index e37f1aed..2c1f14bd 100644 --- a/vtm/src/org/oscim/backend/CanvasAdapter.java +++ b/vtm/src/org/oscim/backend/CanvasAdapter.java @@ -27,6 +27,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Locale; /** * The Class CanvasAdapter. @@ -100,6 +101,18 @@ public abstract class CanvasAdapter { return g.decodeBitmapImpl(inputStream); } + /** + * Create SVG {@link Bitmap} from InputStream. + * + * @param inputStream the input stream + * @return the SVG bitmap + */ + protected abstract Bitmap decodeSvgBitmapImpl(InputStream inputStream); + + public static Bitmap decodeSvgBitmap(InputStream inputStream) { + return g.decodeSvgBitmapImpl(inputStream); + } + /** * Create {@link Bitmap} from bundled assets. * @@ -138,7 +151,11 @@ public abstract class CanvasAdapter { return null; } - Bitmap bitmap = decodeBitmap(inputStream); + Bitmap bitmap; + if (src.toLowerCase(Locale.ENGLISH).endsWith(".svg")) + bitmap = decodeSvgBitmap(inputStream); + else + bitmap = decodeBitmap(inputStream); inputStream.close(); return bitmap; } diff --git a/vtm/src/org/oscim/theme/XmlThemeBuilder.java b/vtm/src/org/oscim/theme/XmlThemeBuilder.java index f76570be..cdee055d 100644 --- a/vtm/src/org/oscim/theme/XmlThemeBuilder.java +++ b/vtm/src/org/oscim/theme/XmlThemeBuilder.java @@ -837,7 +837,8 @@ public class XmlThemeBuilder extends DefaultHandler { validateExists("src", src, elementName); - if (src.toLowerCase(Locale.ENGLISH).endsWith(".png")) { + String lowSrc = src.toLowerCase(Locale.ENGLISH); + if (lowSrc.endsWith(".png") || lowSrc.endsWith(".svg")) { try { Bitmap bitmap = CanvasAdapter.getBitmapAsset(mRelativePathPrefix, src); if (bitmap != null)