From 4cd11462daabf78b9708abf4897646780ccb4314 Mon Sep 17 00:00:00 2001 From: Emux Date: Wed, 7 Dec 2016 19:58:23 +0200 Subject: [PATCH] SVG resources scaling, closes #214 --- docs/Changelog.md | 1 + resources/rendertheme.xsd | 20 ++- .../oscim/android/canvas/AndroidGraphics.java | 8 +- .../android/canvas/AndroidSvgBitmap.java | 12 +- .../src/org/oscim/awt/AwtGraphics.java | 8 +- .../src/org/oscim/awt/AwtSvgBitmap.java | 10 +- .../org/oscim/ios/backend/IosGraphics.java | 10 +- .../org/oscim/ios/backend/IosSvgBitmap.java | 8 +- .../org/oscim/gdx/client/GwtGdxGraphics.java | 6 +- vtm/src/org/oscim/backend/CanvasAdapter.java | 18 ++- vtm/src/org/oscim/theme/XmlThemeBuilder.java | 137 ++++++++++------ vtm/src/org/oscim/theme/styles/AreaStyle.java | 41 +++++ .../org/oscim/theme/styles/CircleStyle.java | 2 +- vtm/src/org/oscim/theme/styles/LineStyle.java | 150 +++++++++++------- .../org/oscim/theme/styles/SymbolStyle.java | 44 +++++ vtm/src/org/oscim/theme/styles/TextStyle.java | 42 +++++ 16 files changed, 380 insertions(+), 137 deletions(-) diff --git a/docs/Changelog.md b/docs/Changelog.md index e3ddaad2..0fff6a04 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -7,6 +7,7 @@ - Polygon label position enhancements [#80](https://github.com/mapsforge/vtm/issues/80) - vtm-web modules update [#51](https://github.com/mapsforge/vtm/issues/51) - Mapbox vector tiles [#57](https://github.com/mapsforge/vtm/issues/57) +- SVG resources scaling [#214](https://github.com/mapsforge/vtm/issues/214) - Circle map style [#122](https://github.com/mapsforge/vtm/issues/122) - PathLayer (vtm) fix disappearing segments [#108](https://github.com/mapsforge/vtm/issues/108) - House numbers (nodes) fix visibility [#168](https://github.com/mapsforge/vtm/issues/168) diff --git a/resources/rendertheme.xsd b/resources/rendertheme.xsd index 1d5fb800..d0a931b3 100644 --- a/resources/rendertheme.xsd +++ b/resources/rendertheme.xsd @@ -124,6 +124,9 @@ + + + @@ -147,8 +150,11 @@ - + + + + @@ -170,6 +176,9 @@ + + + @@ -194,6 +203,9 @@ + + + --> @@ -201,6 +213,9 @@ + + + @@ -229,6 +244,9 @@ + + + diff --git a/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java b/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java index 642adf98..5cc117ed 100644 --- a/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java +++ b/vtm-android/src/org/oscim/android/canvas/AndroidGraphics.java @@ -56,9 +56,9 @@ public final class AndroidGraphics extends CanvasAdapter { } @Override - public Bitmap decodeSvgBitmapImpl(InputStream inputStream) { + public Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent) { try { - return new AndroidSvgBitmap(inputStream); + return new AndroidSvgBitmap(inputStream, width, height, percent); } catch (IOException e) { e.printStackTrace(); return null; @@ -66,9 +66,9 @@ public final class AndroidGraphics extends CanvasAdapter { } @Override - public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src) { + public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src, int width, int height, int percent) { try { - return createBitmap(relativePathPrefix, src); + return createBitmap(relativePathPrefix, src, width, height, percent); } catch (IOException e) { e.printStackTrace(); } diff --git a/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java b/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java index f159cd03..55294308 100644 --- a/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java +++ b/vtm-android/src/org/oscim/android/canvas/AndroidSvgBitmap.java @@ -1,5 +1,7 @@ /* - * Copyright 2016 devemux86 + * Copyright 2010, 2011, 2012 mapsforge.org + * Copyright 2013-2014 Ludwig M Brinckmann + * Copyright 2014-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 @@ -74,13 +76,13 @@ public class AndroidSvgBitmap extends AndroidBitmap { } } - private static android.graphics.Bitmap getResourceBitmapImpl(InputStream inputStream) throws IOException { + private static android.graphics.Bitmap getResourceBitmapImpl(InputStream inputStream, int width, int height, int percent) throws IOException { synchronized (SVG.getVersion()) { - return getResourceBitmap(inputStream, CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI, DEFAULT_SIZE, 0, 0, 100); + return getResourceBitmap(inputStream, CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI, DEFAULT_SIZE, width, height, percent); } } - public AndroidSvgBitmap(InputStream inputStream) throws IOException { - super(getResourceBitmapImpl(inputStream)); + public AndroidSvgBitmap(InputStream inputStream, int width, int height, int percent) throws IOException { + super(getResourceBitmapImpl(inputStream, width, height, percent)); } } diff --git a/vtm-desktop/src/org/oscim/awt/AwtGraphics.java b/vtm-desktop/src/org/oscim/awt/AwtGraphics.java index 0cfa7ee4..1c0c3e1c 100644 --- a/vtm-desktop/src/org/oscim/awt/AwtGraphics.java +++ b/vtm-desktop/src/org/oscim/awt/AwtGraphics.java @@ -103,9 +103,9 @@ public class AwtGraphics extends CanvasAdapter { } @Override - public Bitmap decodeSvgBitmapImpl(InputStream inputStream) { + public Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent) { try { - return new AwtSvgBitmap(inputStream); + return new AwtSvgBitmap(inputStream, width, height, percent); } catch (IOException e) { e.printStackTrace(); return null; @@ -113,9 +113,9 @@ public class AwtGraphics extends CanvasAdapter { } @Override - public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src) { + public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src, int width, int height, int percent) { try { - return createBitmap(relativePathPrefix, src); + return createBitmap(relativePathPrefix, src, width, height, percent); } catch (IOException e) { e.printStackTrace(); } diff --git a/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java b/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java index 6435d5e7..42908146 100644 --- a/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java +++ b/vtm-desktop/src/org/oscim/awt/AwtSvgBitmap.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 devemux86 + * Copyright 2015-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 @@ -77,13 +77,13 @@ public class AwtSvgBitmap extends AwtBitmap { } } - private static BufferedImage getResourceBitmapImpl(InputStream inputStream) throws IOException { + private static BufferedImage getResourceBitmapImpl(InputStream inputStream, int width, int height, int percent) throws IOException { synchronized (SVGCache.getSVGUniverse()) { - return getResourceBitmap(inputStream, CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI, DEFAULT_SIZE, 0, 0, 100); + return getResourceBitmap(inputStream, CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI, DEFAULT_SIZE, width, height, percent); } } - public AwtSvgBitmap(InputStream inputStream) throws IOException { - super(getResourceBitmapImpl(inputStream)); + public AwtSvgBitmap(InputStream inputStream, int width, int height, int percent) throws IOException { + super(getResourceBitmapImpl(inputStream, width, height, percent)); } } diff --git a/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java b/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java index 6f9cec9d..3afaafc6 100644 --- a/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java +++ b/vtm-ios/src/org/oscim/ios/backend/IosGraphics.java @@ -30,7 +30,7 @@ import java.io.InputStream; */ public class IosGraphics extends CanvasAdapter { - static final Logger log = LoggerFactory.getLogger(IosGraphics.class); + private static final Logger log = LoggerFactory.getLogger(IosGraphics.class); public static void init() { CanvasAdapter.init(new IosGraphics()); @@ -62,9 +62,9 @@ public class IosGraphics extends CanvasAdapter { } @Override - protected Bitmap decodeSvgBitmapImpl(InputStream inputStream) { + protected Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent) { try { - return new IosSvgBitmap(inputStream); + return new IosSvgBitmap(inputStream, width, height, percent); } catch (IOException e) { log.error("decodeSvgBitmapImpl", e); return null; @@ -72,9 +72,9 @@ public class IosGraphics extends CanvasAdapter { } @Override - protected Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src) { + protected Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src, int width, int height, int percent) { try { - return createBitmap(relativePathPrefix, src); + return createBitmap(relativePathPrefix, src, width, height, percent); } catch (IOException e) { log.error("loadBitmapAssetImpl", e); return null; diff --git a/vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java b/vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java index a4dc713a..19e78a43 100644 --- a/vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java +++ b/vtm-ios/src/org/oscim/ios/backend/IosSvgBitmap.java @@ -89,11 +89,11 @@ public class IosSvgBitmap extends IosBitmap { return renderer.asImageWithSize(new CGSize(bitmapWidth, bitmapHeight), 1); } - private static UIImage getResourceBitmapImpl(InputStream inputStream) { - return getResourceBitmap(inputStream, CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI, DEFAULT_SIZE, 0, 0, 100); + private static UIImage getResourceBitmapImpl(InputStream inputStream, int width, int height, int percent) { + return getResourceBitmap(inputStream, CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI, DEFAULT_SIZE, width, height, percent); } - public IosSvgBitmap(InputStream inputStream) throws IOException { - super(getResourceBitmapImpl(inputStream)); + public IosSvgBitmap(InputStream inputStream, int width, int height, int percent) throws IOException { + super(getResourceBitmapImpl(inputStream, width, height, percent)); } } diff --git a/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java b/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java index 05ca5d3d..d62d0534 100644 --- a/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java +++ b/vtm-web/src/org/oscim/gdx/client/GwtGdxGraphics.java @@ -48,19 +48,19 @@ public class GwtGdxGraphics extends CanvasAdapter { } @Override - public Bitmap decodeBitmapImpl(InputStream in) { + public Bitmap decodeBitmapImpl(InputStream inputStream) { // TODO return null; } @Override - public Bitmap decodeSvgBitmapImpl(InputStream in) { + public Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent) { // TODO return null; } @Override - public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src) { + public Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src, int width, int height, int percent) { String pathName = (relativePathPrefix == null || relativePathPrefix.length() == 0 ? "" : relativePathPrefix + File.separatorChar) + src; return new GwtBitmap(pathName); } diff --git a/vtm/src/org/oscim/backend/CanvasAdapter.java b/vtm/src/org/oscim/backend/CanvasAdapter.java index 27bf221e..30eae5b2 100644 --- a/vtm/src/org/oscim/backend/CanvasAdapter.java +++ b/vtm/src/org/oscim/backend/CanvasAdapter.java @@ -117,10 +117,10 @@ public abstract class CanvasAdapter { * @param inputStream the input stream * @return the SVG bitmap */ - protected abstract Bitmap decodeSvgBitmapImpl(InputStream inputStream); + protected abstract Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent); - public static Bitmap decodeSvgBitmap(InputStream inputStream) { - return g.decodeSvgBitmapImpl(inputStream); + public static Bitmap decodeSvgBitmap(InputStream inputStream, int width, int height, int percent) { + return g.decodeSvgBitmapImpl(inputStream, width, height, percent); } /** @@ -130,13 +130,17 @@ public abstract class CanvasAdapter { * @param src the resource * @return the bitmap */ - protected abstract Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src); + protected abstract Bitmap loadBitmapAssetImpl(String relativePathPrefix, String src, int width, int height, int percent); public static Bitmap getBitmapAsset(String relativePathPrefix, String src) { - return g.loadBitmapAssetImpl(relativePathPrefix, src); + return getBitmapAsset(relativePathPrefix, src, 0, 0, 100); } - protected static Bitmap createBitmap(String relativePathPrefix, String src) throws IOException { + public static Bitmap getBitmapAsset(String relativePathPrefix, String src, int width, int height, int percent) { + return g.loadBitmapAssetImpl(relativePathPrefix, src, width, height, percent); + } + + protected static Bitmap createBitmap(String relativePathPrefix, String src, int width, int height, int percent) throws IOException { if (src == null || src.length() == 0) { // no image source defined return null; @@ -163,7 +167,7 @@ public abstract class CanvasAdapter { Bitmap bitmap; if (src.toLowerCase(Locale.ENGLISH).endsWith(".svg")) - bitmap = decodeSvgBitmap(inputStream); + bitmap = decodeSvgBitmap(inputStream, width, height, percent); else bitmap = decodeBitmap(inputStream); inputStream.close(); diff --git a/vtm/src/org/oscim/theme/XmlThemeBuilder.java b/vtm/src/org/oscim/theme/XmlThemeBuilder.java index 1c4f7190..1cc31d23 100644 --- a/vtm/src/org/oscim/theme/XmlThemeBuilder.java +++ b/vtm/src/org/oscim/theme/XmlThemeBuilder.java @@ -39,11 +39,13 @@ import org.oscim.theme.rule.RuleBuilder; import org.oscim.theme.styles.AreaStyle; import org.oscim.theme.styles.AreaStyle.AreaBuilder; import org.oscim.theme.styles.CircleStyle; +import org.oscim.theme.styles.CircleStyle.CircleBuilder; import org.oscim.theme.styles.ExtrusionStyle; import org.oscim.theme.styles.LineStyle; import org.oscim.theme.styles.LineStyle.LineBuilder; import org.oscim.theme.styles.RenderStyle; import org.oscim.theme.styles.SymbolStyle; +import org.oscim.theme.styles.SymbolStyle.SymbolBuilder; import org.oscim.theme.styles.TextStyle; import org.oscim.theme.styles.TextStyle.TextBuilder; import org.slf4j.Logger; @@ -120,9 +122,11 @@ public class XmlThemeBuilder extends DefaultHandler { private final HashMap> mTextStyles = new HashMap<>(10); - private final TextBuilder mTextBuilder = TextStyle.builder(); private final AreaBuilder mAreaBuilder = AreaStyle.builder(); + private final CircleBuilder mCircleBuilder = CircleStyle.builder(); private final LineBuilder mLineBuilder = LineStyle.builder(); + private final SymbolBuilder mSymbolBuilder = SymbolStyle.builder(); + private final TextBuilder mTextBuilder = TextStyle.builder(); private RuleBuilder mCurrentRule; private TextureAtlas mTextureAtlas; @@ -455,6 +459,7 @@ public class XmlThemeBuilder extends DefaultHandler { LineBuilder b = mLineBuilder.set(line); b.isOutline(isOutline); b.level(level); + String src = null; for (int i = 0; i < attributes.getLength(); i++) { String name = attributes.getLocalName(i); @@ -466,11 +471,10 @@ public class XmlThemeBuilder extends DefaultHandler { else if ("cat".equals(name)) b.cat(value); - else if ("src".equals(name)) { - b.texture = loadTexture(value); - /*if (b.texture != null) - b.texture.mipmap = true;*/ - } else if ("use".equals(name)) + else if ("src".equals(name)) + src = value; + + else if ("use".equals(name)) ;// ignore else if ("outline".equals(name)) @@ -520,10 +524,23 @@ public class XmlThemeBuilder extends DefaultHandler { else if ("dasharray".equals(name)) ; // TBD + else if ("symbol-width".equals(name)) + b.symbolWidth = (int) (Integer.parseInt(value) * mScale); + + else if ("symbol-height".equals(name)) + b.symbolHeight = (int) (Integer.parseInt(value) * mScale); + + else if ("symbol-percent".equals(name)) + b.symbolPercent = Integer.parseInt(value); + else logUnknownAttribute(elementName, name, value, i); } + b.texture = loadTexture(src, b.symbolWidth, b.symbolHeight, b.symbolPercent); + /*if (b.texture != null) + b.texture.mipmap = true;*/ + return b.build(); } @@ -558,6 +575,7 @@ public class XmlThemeBuilder extends DefaultHandler { int level) { AreaBuilder b = mAreaBuilder.set(area); b.level(level); + String src = null; for (int i = 0; i < attributes.getLength(); i++) { String name = attributes.getLocalName(i); @@ -573,7 +591,7 @@ public class XmlThemeBuilder extends DefaultHandler { ;// ignore else if ("src".equals(name)) - b.texture = loadTexture(value); + src = value; else if ("fill".equals(name)) b.color(value); @@ -598,19 +616,30 @@ public class XmlThemeBuilder extends DefaultHandler { else if ("mesh".equals(name)) b.mesh(Boolean.parseBoolean(value)); + else if ("symbol-width".equals(name)) + b.symbolWidth = (int) (Integer.parseInt(value) * mScale); + + else if ("symbol-height".equals(name)) + b.symbolHeight = (int) (Integer.parseInt(value) * mScale); + + else if ("symbol-percent".equals(name)) + b.symbolPercent = Integer.parseInt(value); + else logUnknownAttribute(elementName, name, value, i); } + b.texture = loadTexture(src, b.symbolWidth, b.symbolHeight, b.symbolPercent); + return b.build(); } - private TextureItem loadTexture(String src) { - if (src == null) + private TextureItem loadTexture(String src, int width, int height, int percent) { + if (src == null || src.length() == 0) return null; try { - Bitmap bitmap = CanvasAdapter.getBitmapAsset(mTheme.getRelativePathPrefix(), src); + Bitmap bitmap = CanvasAdapter.getBitmapAsset(mTheme.getRelativePathPrefix(), src, width, height, percent); if (bitmap != null) { log.debug("loading {}", src); return new TextureItem(bitmap, true); @@ -827,6 +856,7 @@ public class XmlThemeBuilder extends DefaultHandler { b.caption = caption; } else b = mTextBuilder.from(style); + String symbol = null; for (int i = 0; i < attributes.getLength(); i++) { String name = attributes.getLocalName(i); @@ -872,18 +902,21 @@ public class XmlThemeBuilder extends DefaultHandler { // NB: minus.. b.dy = -Float.parseFloat(value) * mScale; - else if ("symbol".equals(name)) { - String lowValue = value.toLowerCase(Locale.ENGLISH); - if (lowValue.endsWith(".png") || lowValue.endsWith(".svg")) { - try { - b.bitmap = CanvasAdapter.getBitmapAsset(mTheme.getRelativePathPrefix(), value); - } catch (Exception e) { - log.debug(e.getMessage()); - } - } else - b.texture = getAtlasRegion(value); - } else if ("use".equals(name)) + else if ("symbol".equals(name)) + symbol = value; + + else if ("use".equals(name)) ;/* ignore */ + + else if ("symbol-width".equals(name)) + b.symbolWidth = (int) (Integer.parseInt(value) * mScale); + + else if ("symbol-height".equals(name)) + b.symbolHeight = (int) (Integer.parseInt(value) * mScale); + + else if ("symbol-percent".equals(name)) + b.symbolPercent = Integer.parseInt(value); + else logUnknownAttribute(elementName, name, value, i); } @@ -892,6 +925,18 @@ public class XmlThemeBuilder extends DefaultHandler { validateNonNegative("size", b.fontSize); validateNonNegative("stroke-width", b.strokeWidth); + if (symbol != null && symbol.length() > 0) { + String lowValue = symbol.toLowerCase(Locale.ENGLISH); + if (lowValue.endsWith(".png") || lowValue.endsWith(".svg")) { + try { + b.bitmap = CanvasAdapter.getBitmapAsset(mTheme.getRelativePathPrefix(), symbol, b.symbolWidth, b.symbolHeight, b.symbolPercent); + } catch (Exception e) { + log.debug(e.getMessage()); + } + } else + b.texture = getAtlasRegion(symbol); + } + return b; } @@ -900,52 +945,47 @@ public class XmlThemeBuilder extends DefaultHandler { * @return a new Circle with the given rendering attributes. */ private CircleStyle createCircle(String elementName, Attributes attributes, int level) { - String cat = null; - float radius = 0; - boolean scaleRadius = false; - int fill = Color.TRANSPARENT; - int stroke = Color.TRANSPARENT; - float strokeWidth = 0; + CircleBuilder b = mCircleBuilder.reset(); + b.level(level); for (int i = 0; i < attributes.getLength(); i++) { String name = attributes.getLocalName(i); String value = attributes.getValue(i); if ("r".equals(name) || "radius".equals(name)) - radius = Float.parseFloat(value) * mScale2; + b.radius(Float.parseFloat(value) * mScale2); else if ("cat".equals(name)) - cat = value; + b.cat(value); else if ("scale-radius".equals(name)) - scaleRadius = Boolean.parseBoolean(value); + b.scaleRadius(Boolean.parseBoolean(value)); else if ("fill".equals(name)) - fill = Color.parseColor(value); + b.color(Color.parseColor(value)); else if ("stroke".equals(name)) - stroke = Color.parseColor(value); + b.strokeColor(Color.parseColor(value)); else if ("stroke-width".equals(name)) - strokeWidth = Float.parseFloat(value) * mScale2; + b.strokeWidth(Float.parseFloat(value) * mScale2); else logUnknownAttribute(elementName, name, value, i); } - validateExists("radius", radius, elementName); - validateNonNegative("radius", radius); - validateNonNegative("stroke-width", strokeWidth); + validateExists("radius", b.radius, elementName); + validateNonNegative("radius", b.radius); + validateNonNegative("stroke-width", b.strokeWidth); - return new CircleStyle(radius, scaleRadius, fill, stroke, strokeWidth, level) - .setCat(cat); + return b.build(); } /** * @return a new Symbol with the given rendering attributes. */ private SymbolStyle createSymbol(String elementName, Attributes attributes) { - String cat = null; + SymbolBuilder b = mSymbolBuilder.reset(); String src = null; for (int i = 0; i < attributes.getLength(); i++) { @@ -956,7 +996,16 @@ public class XmlThemeBuilder extends DefaultHandler { src = value; else if ("cat".equals(name)) - cat = value; + b.cat(value); + + else if ("symbol-width".equals(name)) + b.symbolWidth = (int) (Integer.parseInt(value) * mScale); + + else if ("symbol-height".equals(name)) + b.symbolHeight = (int) (Integer.parseInt(value) * mScale); + + else if ("symbol-percent".equals(name)) + b.symbolPercent = Integer.parseInt(value); else logUnknownAttribute(elementName, name, value, i); @@ -967,17 +1016,15 @@ public class XmlThemeBuilder extends DefaultHandler { String lowSrc = src.toLowerCase(Locale.ENGLISH); if (lowSrc.endsWith(".png") || lowSrc.endsWith(".svg")) { try { - Bitmap bitmap = CanvasAdapter.getBitmapAsset(mTheme.getRelativePathPrefix(), src); + Bitmap bitmap = CanvasAdapter.getBitmapAsset(mTheme.getRelativePathPrefix(), src, b.symbolWidth, b.symbolHeight, b.symbolPercent); if (bitmap != null) - return new SymbolStyle(bitmap) - .setCat(cat); + return b.bitmap(bitmap).build(); } catch (Exception e) { log.debug(e.getMessage()); } return null; } - return new SymbolStyle(getAtlasRegion(src)) - .setCat(cat); + return b.texture(getAtlasRegion(src)).build(); } private ExtrusionStyle createExtrusion(String elementName, Attributes attributes, int level) { diff --git a/vtm/src/org/oscim/theme/styles/AreaStyle.java b/vtm/src/org/oscim/theme/styles/AreaStyle.java index 0edc9a09..014a9714 100644 --- a/vtm/src/org/oscim/theme/styles/AreaStyle.java +++ b/vtm/src/org/oscim/theme/styles/AreaStyle.java @@ -1,4 +1,5 @@ /* + * Copyright 2010, 2011, 2012 mapsforge.org * Copyright 2014 Hannes Janetzek * Copyright 2016 devemux86 * @@ -77,6 +78,10 @@ public class AreaStyle extends RenderStyle { */ public final boolean mesh; + public final int symbolWidth; + public final int symbolHeight; + public final int symbolPercent; + public AreaStyle(int color) { this(0, color); } @@ -92,6 +97,10 @@ public class AreaStyle extends RenderStyle { this.strokeColor = color; this.strokeWidth = 1; this.mesh = false; + + this.symbolWidth = 0; + this.symbolHeight = 0; + this.symbolPercent = 100; } public AreaStyle(AreaBuilder b) { @@ -105,6 +114,10 @@ public class AreaStyle extends RenderStyle { this.strokeColor = b.strokeColor; this.strokeWidth = b.strokeWidth; this.mesh = b.mesh; + + this.symbolWidth = b.symbolWidth; + this.symbolHeight = b.symbolHeight; + this.symbolPercent = b.symbolPercent; } @Override @@ -163,6 +176,10 @@ public class AreaStyle extends RenderStyle { public TextureItem texture; + public int symbolWidth; + public int symbolHeight; + public int symbolPercent; + public AreaBuilder() { } @@ -181,6 +198,10 @@ public class AreaStyle extends RenderStyle { this.strokeWidth = area.strokeWidth; this.mesh = area.mesh; + this.symbolWidth = area.symbolWidth; + this.symbolHeight = area.symbolHeight; + this.symbolPercent = area.symbolPercent; + return self(); } @@ -214,6 +235,21 @@ public class AreaStyle extends RenderStyle { return self(); } + public T symbolWidth(int symbolWidth) { + this.symbolWidth = symbolWidth; + return self(); + } + + public T symbolHeight(int symbolHeight) { + this.symbolHeight = symbolHeight; + return self(); + } + + public T symbolPercent(int symbolPercent) { + this.symbolPercent = symbolPercent; + return self(); + } + public T reset() { fillColor = Color.WHITE; strokeColor = Color.BLACK; @@ -224,6 +260,11 @@ public class AreaStyle extends RenderStyle { style = null; texture = null; mesh = false; + + symbolWidth = 0; + symbolHeight = 0; + symbolPercent = 100; + return self(); } diff --git a/vtm/src/org/oscim/theme/styles/CircleStyle.java b/vtm/src/org/oscim/theme/styles/CircleStyle.java index 265375bc..7e1d7468 100644 --- a/vtm/src/org/oscim/theme/styles/CircleStyle.java +++ b/vtm/src/org/oscim/theme/styles/CircleStyle.java @@ -26,7 +26,7 @@ import org.oscim.backend.canvas.Color; public final class CircleStyle extends RenderStyle { public final int fillColor; - public final int level; + private final int level; public final float radius; public final boolean scaleRadius; public final int strokeColor; diff --git a/vtm/src/org/oscim/theme/styles/LineStyle.java b/vtm/src/org/oscim/theme/styles/LineStyle.java index 9bbb93a3..92c8a849 100644 --- a/vtm/src/org/oscim/theme/styles/LineStyle.java +++ b/vtm/src/org/oscim/theme/styles/LineStyle.java @@ -26,7 +26,7 @@ import static org.oscim.backend.canvas.Color.parseColor; public final class LineStyle extends RenderStyle { - final int level; + private final int level; public final String style; public final float width; public final int color; @@ -43,21 +43,20 @@ public final class LineStyle extends RenderStyle { public final boolean randomOffset; - private LineStyle(LineBuilder builder) { - this.level = builder.level; - this.style = builder.style; - this.width = builder.strokeWidth; - this.color = builder.fillColor; - this.cap = builder.cap; - this.outline = builder.outline; - this.fixed = builder.fixed; - this.fadeScale = builder.fadeScale; - this.blur = builder.blur; - this.stipple = builder.stipple; - this.stippleColor = builder.stippleColor; - this.stippleWidth = builder.stippleWidth; - this.texture = builder.texture; - this.randomOffset = builder.randomOffset; + public final int symbolWidth; + public final int symbolHeight; + public final int symbolPercent; + + public LineStyle(int stroke, float width) { + this(0, "", stroke, width, Cap.BUTT, true, 0, 0, 0, -1, 0, false, null, true); + } + + public LineStyle(int level, int stroke, float width) { + this(level, "", stroke, width, Cap.BUTT, true, 0, 0, 0, -1, 0, false, null, true); + } + + public LineStyle(int stroke, float width, Cap cap) { + this(0, "", stroke, width, cap, true, 0, 0, 0, -1, 0, false, null, true); } public LineStyle(int level, String style, int color, float width, @@ -84,23 +83,31 @@ public final class LineStyle extends RenderStyle { this.fadeScale = fadeScale; this.randomOffset = randomOffset; + + this.symbolWidth = 0; + this.symbolHeight = 0; + this.symbolPercent = 100; } - public LineStyle(int stroke, float width) { - this(0, "", stroke, width, Cap.BUTT, true, 0, 0, 0, -1, 0, false, null, true); - } + private LineStyle(LineBuilder b) { + this.level = b.level; + this.style = b.style; + this.width = b.strokeWidth; + this.color = b.fillColor; + this.cap = b.cap; + this.outline = b.outline; + this.fixed = b.fixed; + this.fadeScale = b.fadeScale; + this.blur = b.blur; + this.stipple = b.stipple; + this.stippleColor = b.stippleColor; + this.stippleWidth = b.stippleWidth; + this.texture = b.texture; + this.randomOffset = b.randomOffset; - public LineStyle(int level, int stroke, float width) { - this(level, "", stroke, width, Cap.BUTT, true, 0, 0, 0, -1, 0, false, null, true); - } - - public LineStyle(int stroke, float width, Cap cap) { - this(0, "", stroke, width, cap, true, 0, 0, 0, -1, 0, false, null, true); - } - - @Override - public void renderWay(Callback cb) { - cb.renderWay(this, level); + this.symbolWidth = b.symbolWidth; + this.symbolHeight = b.symbolHeight; + this.symbolPercent = b.symbolPercent; } @Override @@ -108,6 +115,11 @@ public final class LineStyle extends RenderStyle { return (LineStyle) mCurrent; } + @Override + public void renderWay(Callback cb) { + cb.renderWay(this, level); + } + public static class LineBuilder> extends StyleBuilder { public Cap cap; @@ -123,9 +135,17 @@ public final class LineStyle extends RenderStyle { public boolean randomOffset; + public int symbolWidth; + public int symbolHeight; + public int symbolPercent; + + public LineBuilder() { + } + public T set(LineStyle line) { if (line == null) return reset(); + this.level = line.level; this.style = line.style; this.strokeWidth = line.width; @@ -140,26 +160,10 @@ public final class LineStyle extends RenderStyle { this.stippleWidth = line.stippleWidth; this.texture = line.texture; this.randomOffset = line.randomOffset; - return self(); - } - public T reset() { - level = -1; - style = null; - fillColor = Color.BLACK; - cap = Cap.ROUND; - strokeWidth = 1; - fixed = false; - - fadeScale = -1; - blur = 0; - - stipple = 0; - stippleWidth = 1; - stippleColor = Color.BLACK; - texture = null; - - randomOffset = true; + this.symbolWidth = line.symbolWidth; + this.symbolHeight = line.symbolHeight; + this.symbolPercent = line.symbolPercent; return self(); } @@ -199,10 +203,6 @@ public final class LineStyle extends RenderStyle { return self(); } - public LineStyle build() { - return new LineStyle(this); - } - public T cap(Cap cap) { this.cap = cap; return self(); @@ -222,6 +222,50 @@ public final class LineStyle extends RenderStyle { this.randomOffset = randomOffset; return self(); } + + public T symbolWidth(int symbolWidth) { + this.symbolWidth = symbolWidth; + return self(); + } + + public T symbolHeight(int symbolHeight) { + this.symbolHeight = symbolHeight; + return self(); + } + + public T symbolPercent(int symbolPercent) { + this.symbolPercent = symbolPercent; + return self(); + } + + public T reset() { + level = -1; + style = null; + fillColor = Color.BLACK; + cap = Cap.ROUND; + strokeWidth = 1; + fixed = false; + + fadeScale = -1; + blur = 0; + + stipple = 0; + stippleWidth = 1; + stippleColor = Color.BLACK; + texture = null; + + randomOffset = true; + + symbolWidth = 0; + symbolHeight = 0; + symbolPercent = 100; + + return self(); + } + + public LineStyle build() { + return new LineStyle(this); + } } @SuppressWarnings("rawtypes") diff --git a/vtm/src/org/oscim/theme/styles/SymbolStyle.java b/vtm/src/org/oscim/theme/styles/SymbolStyle.java index e1d77315..2b2b0f1d 100644 --- a/vtm/src/org/oscim/theme/styles/SymbolStyle.java +++ b/vtm/src/org/oscim/theme/styles/SymbolStyle.java @@ -29,19 +29,35 @@ public final class SymbolStyle extends RenderStyle { public final Bitmap bitmap; public final TextureRegion texture; + public final int symbolWidth; + public final int symbolHeight; + public final int symbolPercent; + public SymbolStyle(Bitmap bitmap) { this.bitmap = bitmap; this.texture = null; + + this.symbolWidth = 0; + this.symbolHeight = 0; + this.symbolPercent = 100; } public SymbolStyle(TextureRegion texture) { this.bitmap = null; this.texture = texture; + + this.symbolWidth = 0; + this.symbolHeight = 0; + this.symbolPercent = 100; } public SymbolStyle(SymbolBuilder b) { this.bitmap = b.bitmap; this.texture = b.texture; + + this.symbolWidth = b.symbolWidth; + this.symbolHeight = b.symbolHeight; + this.symbolPercent = b.symbolPercent; } @Override @@ -70,6 +86,10 @@ public final class SymbolStyle extends RenderStyle { public Bitmap bitmap; public TextureRegion texture; + public int symbolWidth; + public int symbolHeight; + public int symbolPercent; + public SymbolBuilder() { } @@ -80,6 +100,10 @@ public final class SymbolStyle extends RenderStyle { this.bitmap = symbol.bitmap; this.texture = symbol.texture; + this.symbolWidth = symbol.symbolWidth; + this.symbolHeight = symbol.symbolHeight; + this.symbolPercent = symbol.symbolPercent; + return self(); } @@ -93,9 +117,29 @@ public final class SymbolStyle extends RenderStyle { return self(); } + public T symbolWidth(int symbolWidth) { + this.symbolWidth = symbolWidth; + return self(); + } + + public T symbolHeight(int symbolHeight) { + this.symbolHeight = symbolHeight; + return self(); + } + + public T symbolPercent(int symbolPercent) { + this.symbolPercent = symbolPercent; + return self(); + } + public T reset() { bitmap = null; texture = null; + + symbolWidth = 0; + symbolHeight = 0; + symbolPercent = 100; + return self(); } diff --git a/vtm/src/org/oscim/theme/styles/TextStyle.java b/vtm/src/org/oscim/theme/styles/TextStyle.java index c2b79cdf..92a107fe 100644 --- a/vtm/src/org/oscim/theme/styles/TextStyle.java +++ b/vtm/src/org/oscim/theme/styles/TextStyle.java @@ -43,6 +43,10 @@ public final class TextStyle extends RenderStyle { public FontFamily fontFamily; public FontStyle fontStyle; + public int symbolWidth; + public int symbolHeight; + public int symbolPercent; + public T reset() { fontFamily = FontFamily.DEFAULT; fontStyle = FontStyle.NORMAL; @@ -58,6 +62,11 @@ public final class TextStyle extends RenderStyle { strokeColor = Color.BLACK; strokeWidth = 0; dy = 0; + + symbolWidth = 0; + symbolHeight = 0; + symbolPercent = 100; + return self(); } @@ -126,6 +135,21 @@ public final class TextStyle extends RenderStyle { return self(); } + public T symbolWidth(int symbolWidth) { + this.symbolWidth = symbolWidth; + return self(); + } + + public T symbolHeight(int symbolHeight) { + this.symbolHeight = symbolHeight; + return self(); + } + + public T symbolPercent(int symbolPercent) { + this.symbolPercent = symbolPercent; + return self(); + } + public T from(TextBuilder other) { fontFamily = other.fontFamily; fontStyle = other.fontStyle; @@ -141,6 +165,11 @@ public final class TextStyle extends RenderStyle { strokeColor = other.strokeColor; strokeWidth = other.strokeWidth; dy = other.dy; + + symbolWidth = other.symbolWidth; + symbolHeight = other.symbolHeight; + symbolPercent = other.symbolPercent; + return self(); } @@ -159,6 +188,11 @@ public final class TextStyle extends RenderStyle { this.strokeColor = style.stroke.getColor(); this.strokeWidth = 2; this.fontSize = style.fontSize; + + this.symbolWidth = style.symbolWidth; + this.symbolHeight = style.symbolHeight; + this.symbolPercent = style.symbolPercent; + return self(); } } @@ -192,6 +226,10 @@ public final class TextStyle extends RenderStyle { stroke = null; this.fontSize = tb.fontSize; + + this.symbolWidth = tb.symbolWidth; + this.symbolHeight = tb.symbolHeight; + this.symbolPercent = tb.symbolPercent; } public final String style; @@ -212,6 +250,10 @@ public final class TextStyle extends RenderStyle { public final Bitmap bitmap; public final TextureRegion texture; + public final int symbolWidth; + public final int symbolHeight; + public final int symbolPercent; + @Override public void dispose() { if (bitmap != null)