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

@ -61,6 +61,9 @@
<activity
android:name=".MapsforgeMapActivity$MapFilePicker"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".MapsforgeMapActivity$ThemeFilePicker"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".MapsforgeStyleActivity"
android:configChanges="keyboardHidden|orientation|screenSize" />

View File

@ -20,6 +20,9 @@
<item
android:id="@+id/theme_newtron"
android:title="@string/theme_newtron" />
<item
android:id="@+id/theme_load"
android:title="@string/theme_load" />
</group>
<item

View File

@ -17,5 +17,6 @@
<string name="style_1">Show nature</string>
<string name="style_2">Hide nature</string>
<string name="menu_gridlayer">Grid</string>
<string name="theme_load">load theme extern</string>
</resources>

View File

@ -1,6 +1,7 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2016 devemux86
* Copyright 2017 Longri
*
* 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
@ -17,12 +18,16 @@ package org.oscim.android.filepicker;
import org.oscim.theme.ExternalRenderTheme;
import org.oscim.theme.ThemeFile;
import org.oscim.theme.ThemeUtils;
import org.oscim.theme.XmlMapsforgeThemeBuilder;
import org.oscim.theme.XmlThemeBuilder;
import org.oscim.tiling.TileSource.OpenResult;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.SAXParserFactory;
@ -34,9 +39,15 @@ public final class ValidRenderTheme implements ValidFileFilter {
@Override
public boolean accept(File file) {
try {
ThemeFile theme = new ExternalRenderTheme(file.getAbsolutePath());
XmlThemeBuilder renderThemeHandler = new XmlThemeBuilder(theme);
DefaultHandler renderThemeHandler;
if(ThemeUtils.isMapsforgeTheme(new FileInputStream(file))) {
renderThemeHandler = new XmlMapsforgeThemeBuilder(theme);
}else{
renderThemeHandler = new XmlThemeBuilder(theme);
}
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
xmlReader.setContentHandler(renderThemeHandler);
xmlReader.parse(new InputSource(theme.getRenderThemeAsStream()));

View File

@ -1,6 +1,7 @@
/*
* Copyright 2014 Hannes Janetzek
* Copyright 2016-2017 devemux86
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -25,6 +26,7 @@ import android.view.MenuItem;
import org.oscim.android.filepicker.FilePicker;
import org.oscim.android.filepicker.FilterByFileExtension;
import org.oscim.android.filepicker.ValidMapFile;
import org.oscim.android.filepicker.ValidRenderTheme;
import org.oscim.core.MapPosition;
import org.oscim.core.Tile;
import org.oscim.layers.TileGridLayer;
@ -38,12 +40,14 @@ import org.oscim.scalebar.ImperialUnitAdapter;
import org.oscim.scalebar.MapScaleBar;
import org.oscim.scalebar.MapScaleBarLayer;
import org.oscim.scalebar.MetricUnitAdapter;
import org.oscim.theme.ExternalRenderTheme;
import org.oscim.theme.VtmThemes;
import org.oscim.tiling.source.mapfile.MapFileTileSource;
import org.oscim.tiling.source.mapfile.MapInfo;
public class MapsforgeMapActivity extends MapActivity {
private static final int SELECT_MAP_FILE = 0;
private static final int SELECT_THEME_FILE = 1;
private TileGridLayer mGridLayer;
private DefaultMapScaleBar mMapScaleBar;
@ -71,6 +75,13 @@ public class MapsforgeMapActivity extends MapActivity {
}
}
public static class ThemeFilePicker extends FilePicker {
public ThemeFilePicker() {
setFileDisplayFilter(new FilterByFileExtension(".xml"));
setFileSelectFilter(new ValidRenderTheme());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.theme_menu, menu);
@ -106,6 +117,11 @@ public class MapsforgeMapActivity extends MapActivity {
item.setChecked(true);
return true;
case R.id.theme_load:
startActivityForResult(new Intent(MapsforgeMapActivity.this, ThemeFilePicker.class),
SELECT_THEME_FILE);
return true;
case R.id.gridlayer:
if (item.isChecked()) {
item.setChecked(false);
@ -163,6 +179,20 @@ public class MapsforgeMapActivity extends MapActivity {
mPrefs.clear();
}
} else if (requestCode == SELECT_THEME_FILE) {
if (resultCode != RESULT_OK || intent == null || intent.getStringExtra(FilePicker.SELECTED_FILE) == null) {
finish();
return;
}
String themePath = intent.getStringExtra(FilePicker.SELECTED_FILE);
ExternalRenderTheme externalRenderTheme = new ExternalRenderTheme(themePath);
try {
mMap.setTheme(externalRenderTheme, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -21,6 +21,7 @@ package org.oscim.android.canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import org.oscim.backend.canvas.Bitmap;
import org.oscim.backend.canvas.Canvas;
@ -96,4 +97,12 @@ public class AndroidCanvas implements Canvas {
public int getWidth() {
return canvas.getWidth();
}
@Override
public void fillRectangle(int x, int y, int width, int height, int color) {
RectF rec = new RectF(x, y, x + width, y + height);
android.graphics.Paint paint = new android.graphics.Paint();
paint.setColor(color);
canvas.drawRect(rec, paint);
}
}

View File

@ -183,12 +183,7 @@ public class AwtCanvas implements Canvas {
@Override
public void fillColor(int color) {
java.awt.Color awtColor = color == Color.TRANSPARENT ? TRANSPARENT : new java.awt.Color(color);
Composite originalComposite = this.canvas.getComposite();
this.canvas.setComposite(AlphaComposite.getInstance(color == Color.TRANSPARENT ? AlphaComposite.CLEAR : AlphaComposite.SRC_OVER));
this.canvas.setColor(awtColor);
this.canvas.fillRect(0, 0, getWidth(), getHeight());
this.canvas.setComposite(originalComposite);
fillRectangle(0, 0, getWidth(), getHeight(), color);
}
@Override
@ -200,4 +195,14 @@ public class AwtCanvas implements Canvas {
public int getWidth() {
return this.bitmap != null ? this.bitmap.getWidth() : 0;
}
@Override
public void fillRectangle(int x, int y, int width, int height, int color) {
java.awt.Color awtColor = color == Color.TRANSPARENT ? TRANSPARENT : new java.awt.Color(color);
Composite originalComposite = this.canvas.getComposite();
this.canvas.setComposite(AlphaComposite.getInstance(color == Color.TRANSPARENT ? AlphaComposite.CLEAR : AlphaComposite.SRC_OVER));
this.canvas.setColor(awtColor);
this.canvas.fillRect(x, y, width, height);
this.canvas.setComposite(originalComposite);
}
}

View File

@ -159,4 +159,12 @@ public class IosCanvas implements Canvas {
public int getWidth() {
return this.cgBitmapContext != null ? (int) this.cgBitmapContext.getWidth() : 0;
}
@Override
public void fillRectangle(int x, int y, int width, int height, int color) {
CGRect rect = new CGRect(x, y, width, height);
setFillColor(this.cgBitmapContext, (color));
this.cgBitmapContext.setBlendMode(CGBlendMode.Normal);
this.cgBitmapContext.fillRect(rect);
}
}

View File

@ -51,13 +51,18 @@ public abstract class BaseAppender extends AppenderBase<ILoggingEvent> {
}
@Override
protected void append(ILoggingEvent eventObject) {
if (eventObject != null && canLogClass(eventObject.getLoggerName())) {
stringBuilder.append(doLayout(eventObject));
String areaText = stringBuilder.toString();
this.textArea.setText(areaText);
this.textArea.setCaretPosition(areaText.length());
}
protected void append(final ILoggingEvent eventObject) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
if (eventObject != null && canLogClass(eventObject.getLoggerName())) {
textArea.append(doLayout(eventObject));
textArea.setCaretPosition(textArea.getDocument().getLength());
}
}
});
thread.start();
//TODO set Highlight for LogLevel [WARN], [ERROR]
}

View File

@ -2,6 +2,7 @@
* Copyright 2013 Hannes Janetzek
* Copyright 2016-2017 devemux86
* Copyright 2017 nebular
* Copyright 2017 Longri
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -124,4 +125,9 @@ public class GwtCanvas implements org.oscim.backend.canvas.Canvas {
public int getWidth() {
return this.bitmap != null ? this.bitmap.getWidth() : 0;
}
@Override
public void fillRectangle(int x, int y, int width, int height, int color) {
// TODO
}
}

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();
}