Mapsforge themes compatibility (#388) #100

This commit is contained in:
Longri
2017-08-23 12:22:52 +02:00
committed by Emux
parent f6d2db4c6f
commit b695d43fee
18 changed files with 1565 additions and 20 deletions

View File

@@ -43,10 +43,15 @@ uniform sampler2D tex;
uniform float u_mode;
void
main(){
if (u_mode == 1.0) {
vec4 c=texture2D(tex,vec2(abs(mod(v_st.s+1.0,2.0)),(v_st.t+1.0)*0.5));
if (u_mode >= 1.0) {
float step= 2.0;
if (u_mode == 3.0){// dashed texture
step =1.0;
}
vec4 c=texture2D(tex,vec2(abs(mod(v_st.s+1.0,step)),(v_st.t+1.0)*0.5));
float fuzz=fwidth(c.a);
gl_FragColor=(c * u_color) *smoothstep(0.5-fuzz,0.5+fuzz,c.a);
gl_FragColor=(c * u_color) * smoothstep(0.5-fuzz,0.5+fuzz,c.a);
}
else {
/* distance on perpendicular to the line */

View File

@@ -58,4 +58,6 @@ public interface Canvas {
int getHeight();
int getWidth();
void fillRectangle(int x, int y, int width, int height, int color);
}

View File

@@ -359,7 +359,7 @@ public final class LineTexBucket extends LineBucket {
LineTexBucket lb = (LineTexBucket) b;
LineStyle line = lb.line.current();
gl.uniform1f(shader.uMode, line.texture != null ? 1 : 0);
gl.uniform1f(shader.uMode, line.dashTexture? 3 : line.texture != null ? 1 : 0);
if (line.texture != null)
line.texture.bind();

View File

@@ -18,11 +18,21 @@
*/
package org.oscim.theme;
import org.oscim.backend.CanvasAdapter;
import org.oscim.theme.IRenderTheme.ThemeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
public class ThemeLoader {
private static final Logger log = LoggerFactory.getLogger(ThemeLoader.class);
public static boolean USE_ATLAS;
public static boolean POT_TEXTURES;
@@ -46,8 +56,21 @@ public class ThemeLoader {
return load(theme, null);
}
public static IRenderTheme load(ThemeFile theme, ThemeCallback themeCallback) throws ThemeException {
IRenderTheme t = USE_ATLAS ? XmlAtlasThemeBuilder.read(theme, themeCallback) : XmlThemeBuilder.read(theme, themeCallback);
IRenderTheme t = null;
try {
if(ThemeUtils.isMapsforgeTheme(theme.getRenderThemeAsStream())){
t = USE_ATLAS ? XmlMapsforgeAtlasThemeBuilder.read(theme, themeCallback) : XmlMapsforgeThemeBuilder.read(theme, themeCallback);
}else{
t = USE_ATLAS ? XmlAtlasThemeBuilder.read(theme, themeCallback) : XmlThemeBuilder.read(theme, themeCallback);
}
} catch (IOException | ParserConfigurationException | SAXException e) {
e.printStackTrace();
}
if (t != null)
t.scaleTextSize(CanvasAdapter.textScale + (CanvasAdapter.dpi / CanvasAdapter.DEFAULT_DPI - 1));
return t;

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
* 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.theme;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
/**
* Created by Longri on 30.08.2017.
*/
public class ThemeUtils {
public static class SAXTerminatorException extends SAXException {
public SAXTerminatorException() {
super();
}
}
/**
* Return true, if the given InputStream a Mapsforge render theme!
*
* @param stream
* @return TRUE or FALSE
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public static boolean isMapsforgeTheme(InputStream stream) throws IOException, SAXException, ParserConfigurationException {
final AtomicBoolean isMapsforgeTheme = new AtomicBoolean(false);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
xmlReader.setContentHandler(new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.equals("rendertheme")) {
isMapsforgeTheme.set(uri.equals("http://mapsforge.org/renderTheme"));
//we have all info's, break parsing
throw new SAXTerminatorException();
}
}
});
try {
xmlReader.parse(new InputSource(stream));
} catch (SAXTerminatorException e) {
// do nothing
}
stream.close();
return isMapsforgeTheme.get();
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2017 Longri
* Copyright 2017 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.theme;
import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.Platform;
import org.oscim.backend.XMLReaderAdapter;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.renderer.atlas.TextureAtlas;
import org.oscim.renderer.atlas.TextureRegion;
import org.oscim.theme.IRenderTheme.ThemeException;
import org.oscim.theme.rule.Rule;
import org.oscim.theme.styles.RenderStyle;
import org.oscim.theme.styles.SymbolStyle;
import org.oscim.theme.styles.SymbolStyle.SymbolBuilder;
import org.oscim.utils.TextureAtlasUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class XmlMapsforgeAtlasThemeBuilder extends XmlMapsforgeThemeBuilder {
/**
* @param theme an input theme containing valid render theme XML data.
* @return a new RenderTheme which is created by parsing the XML data from the input theme.
* @throws ThemeException if an error occurs while parsing the render theme XML.
*/
public static IRenderTheme read(ThemeFile theme) throws ThemeException {
return read(theme, null);
}
/**
* @param theme an input theme containing valid render theme XML data.
* @param themeCallback the theme callback.
* @return a new RenderTheme which is created by parsing the XML data from the input theme.
* @throws ThemeException if an error occurs while parsing the render theme XML.
*/
public static IRenderTheme read(ThemeFile theme, ThemeCallback themeCallback) throws ThemeException {
Map<Object, TextureRegion> outputMap = new HashMap<>();
List<TextureAtlas> atlasList = new ArrayList<>();
XmlMapsforgeAtlasThemeBuilder renderThemeHandler = new XmlMapsforgeAtlasThemeBuilder(theme, themeCallback, outputMap, atlasList);
try {
new XMLReaderAdapter().parse(renderThemeHandler, theme.getRenderThemeAsStream());
} catch (Exception e) {
throw new ThemeException(e.getMessage());
}
TextureAtlasUtils.createTextureRegions(renderThemeHandler.bitmapMap, outputMap, atlasList,
true, CanvasAdapter.platform == Platform.IOS);
return replaceThemeSymbols(renderThemeHandler.mRenderTheme, outputMap);
}
private static IRenderTheme replaceThemeSymbols(RenderTheme renderTheme, Map<Object, TextureRegion> regionMap) {
SymbolBuilder<?> symbolBuilder = SymbolStyle.builder();
for (Rule rule : renderTheme.getRules()) {
replaceRuleSymbols(rule, regionMap, symbolBuilder);
}
return renderTheme;
}
private static void replaceRuleSymbols(Rule rule, Map<Object, TextureRegion> regionMap, SymbolBuilder<?> symbolBuilder) {
for (int i = 0, n = rule.styles.length; i < n; i++) {
RenderStyle style = rule.styles[i];
if (style instanceof SymbolStyle) {
int hash = ((SymbolStyle) style).hash;
TextureRegion region = regionMap.get(hash);
if (region != null) {
SymbolBuilder<?> b = symbolBuilder.reset();
rule.styles[i] = b.texture(region).build();
}
}
}
for (Rule subRule : rule.subRules) {
replaceRuleSymbols(subRule, regionMap, symbolBuilder);
}
}
private final Map<Object, TextureRegion> regionMap;
private final List<TextureAtlas> atlasList;
private final Map<Object, Bitmap> bitmapMap = new HashMap<>();
public XmlMapsforgeAtlasThemeBuilder(ThemeFile theme,
Map<Object, TextureRegion> regionMap, List<TextureAtlas> atlasList) {
this(theme, null, regionMap, atlasList);
}
public XmlMapsforgeAtlasThemeBuilder(ThemeFile theme, ThemeCallback themeCallback,
Map<Object, TextureRegion> regionMap, List<TextureAtlas> atlasList) {
super(theme, themeCallback);
this.regionMap = regionMap;
this.atlasList = atlasList;
}
@Override
RenderTheme createTheme(Rule[] rules) {
return new AtlasRenderTheme(mMapBackground, mTextScale, rules, mLevels, regionMap, atlasList);
}
@Override
SymbolStyle buildSymbol(SymbolBuilder<?> b, String src, Bitmap bitmap) {
// we need to hash with the width/height included as the same symbol could be required
// in a different size and must be cached with a size-specific hash
String absoluteName = CanvasAdapter.getAbsoluteFile(mTheme.getRelativePathPrefix(), src).getAbsolutePath();
int hash = new StringBuilder().append(absoluteName).append(b.symbolWidth).append(b.symbolHeight).append(b.symbolPercent).toString().hashCode();
bitmapMap.put(hash, bitmap);
return b.hash(hash).build();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -2,6 +2,7 @@
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2013 Hannes Janetzek
* Copyright 2016-2017 devemux86
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@@ -47,6 +48,7 @@ public final class LineStyle extends RenderStyle<LineStyle> {
public final int symbolWidth;
public final int symbolHeight;
public final int symbolPercent;
public boolean dashTexture;
public LineStyle(int stroke, float width) {
this(0, "", stroke, width, Cap.BUTT, true, 0, 0, 0, -1, 0, false, null, true);
@@ -112,6 +114,7 @@ public final class LineStyle extends RenderStyle<LineStyle> {
this.symbolWidth = b.symbolWidth;
this.symbolHeight = b.symbolHeight;
this.symbolPercent = b.symbolPercent;
this.dashTexture = b.strokeDasharray != null;
}
@Override
@@ -143,6 +146,7 @@ public final class LineStyle extends RenderStyle<LineStyle> {
public int symbolWidth;
public int symbolHeight;
public int symbolPercent;
public float[] strokeDasharray;
public LineBuilder() {
}
@@ -273,7 +277,7 @@ public final class LineStyle extends RenderStyle<LineStyle> {
symbolWidth = 0;
symbolHeight = 0;
symbolPercent = 100;
strokeDasharray = null;
return self();
}