Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

[core, ios, macos, android] Support TinySDF for local glyph generation #10522

Merged
merged 17 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ set(MBGL_CORE_FILES
src/mbgl/text/glyph_pbf.cpp
src/mbgl/text/glyph_pbf.hpp
src/mbgl/text/glyph_range.hpp
src/mbgl/text/local_glyph_rasterizer.hpp
src/mbgl/text/placement.cpp
src/mbgl/text/placement.hpp
src/mbgl/text/quads.cpp
Expand Down Expand Up @@ -689,6 +690,8 @@ set(MBGL_CORE_FILES
src/mbgl/util/tile_coordinate.hpp
src/mbgl/util/tile_cover.cpp
src/mbgl/util/tile_cover.hpp
src/mbgl/util/tiny_sdf.cpp
src/mbgl/util/tiny_sdf.hpp
src/mbgl/util/token.hpp
src/mbgl/util/url.cpp
src/mbgl/util/url.hpp
Expand Down
3 changes: 2 additions & 1 deletion cmake/test-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ set(MBGL_TEST_FILES

# text
test/text/cross_tile_symbol_index.test.cpp
test/text/glyph_loader.test.cpp
test/text/local_glyph_rasterizer.test.cpp
test/text/glyph_manager.test.cpp
test/text/glyph_pbf.test.cpp
test/text/quads.test.cpp

Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Renderer {
public:
Renderer(RendererBackend&, float pixelRatio_, FileSource&, Scheduler&,
GLContextMode = GLContextMode::Unique,
const optional<std::string> programCacheDir = {});
const optional<std::string> programCacheDir = {},
const optional<std::string> localFontFamily = {});
~Renderer();

void markContextLost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
private void initialiseDrawingSurface(MapboxMapOptions options) {
if (options.getTextureMode()) {
TextureView textureView = new TextureView(getContext());
mapRenderer = new TextureViewMapRenderer(getContext(), textureView) {
mapRenderer = new TextureViewMapRenderer(getContext(), textureView, options.getLocalIdeographFontFamily()) {
@Override
protected void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.post(new Runnable() {
Expand All @@ -315,7 +315,7 @@ public void run() {
GLSurfaceView glSurfaceView = (GLSurfaceView) findViewById(R.id.surfaceView);
glSurfaceView.setZOrderMediaOverlay(mapboxMapOptions.getRenderSurfaceOnTop());

mapRenderer = new GLSurfaceViewMapRenderer(getContext(), glSurfaceView) {
mapRenderer = new GLSurfaceViewMapRenderer(getContext(), glSurfaceView, options.getLocalIdeographFontFamily()) {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
MapView.this.post(new Runnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class MapboxMapOptions implements Parcelable {
private float myLocationAccuracyThreshold;
private boolean prefetchesTiles = true;
private boolean zMediaOverlay = false;
private String localIdeographFontFamily;

private String apiBaseUrl;

Expand Down Expand Up @@ -157,6 +158,7 @@ private MapboxMapOptions(Parcel in) {
textureMode = in.readByte() != 0;
prefetchesTiles = in.readByte() != 0;
zMediaOverlay = in.readByte() != 0;
localIdeographFontFamily = in.readString();
}

static Bitmap getBitmapFromDrawable(Drawable drawable) {
Expand Down Expand Up @@ -304,6 +306,8 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_enableTilePrefetch, true));
mapboxMapOptions.renderSurfaceOnTop(
typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_enableZMediaOverlay, false));
mapboxMapOptions.localIdeographFontFamily(
typedArray.getString(R.styleable.mapbox_MapView_mapbox_localIdeographFontFamily));
} finally {
typedArray.recycle();
}
Expand Down Expand Up @@ -733,6 +737,18 @@ public MapboxMapOptions setPrefetchesTiles(boolean enable) {
return this;
}

/**
* Set the font-family for generating glyphs locally for ideographs in the ‘CJK Unified Ideographs’
* and ‘Hangul Syllables’ ranges.
*
* @param fontFamily font family for local ideograph generation.
* @return This
*/
public MapboxMapOptions localIdeographFontFamily(String fontFamily) {
this.localIdeographFontFamily = fontFamily;
return this;
}

/**
* Check whether tile pre-fetching is enabled.
*
Expand Down Expand Up @@ -1079,6 +1095,16 @@ public boolean getTextureMode() {
return textureMode;
}

/**
* Returns the font-family for locally overriding generation of glyphs in the
* ‘CJK Unified Ideographs’ and ‘Hangul Syllables’ ranges.
*
* @return Local ideograph font family name.
*/
public String getLocalIdeographFontFamily() {
return localIdeographFontFamily;
}

public static final Parcelable.Creator<MapboxMapOptions> CREATOR = new Parcelable.Creator<MapboxMapOptions>() {
public MapboxMapOptions createFromParcel(Parcel in) {
return new MapboxMapOptions(in);
Expand Down Expand Up @@ -1145,6 +1171,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (textureMode ? 1 : 0));
dest.writeByte((byte) (prefetchesTiles ? 1 : 0));
dest.writeByte((byte) (zMediaOverlay ? 1 : 0));
dest.writeString(localIdeographFontFamily);
}

@Override
Expand Down Expand Up @@ -1274,6 +1301,9 @@ public boolean equals(Object o) {
if (zMediaOverlay != options.zMediaOverlay) {
return false;
}
if (localIdeographFontFamily != options.localIdeographFontFamily) {
return false;
}

return false;
}
Expand Down Expand Up @@ -1323,6 +1353,7 @@ public int hashCode() {
result = 31 * result + (style != null ? style.hashCode() : 0);
result = 31 * result + (prefetchesTiles ? 1 : 0);
result = 31 * result + (zMediaOverlay ? 1 : 0);
result = 31 * result + (localIdeographFontFamily != null ? localIdeographFontFamily.hashCode() : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ public abstract class MapRenderer implements MapRendererScheduler {

private MapboxMap.OnFpsChangedListener onFpsChangedListener;

public MapRenderer(Context context) {
public MapRenderer(Context context, String localIdeographFontFamily) {

FileSource fileSource = FileSource.getInstance(context);
float pixelRatio = context.getResources().getDisplayMetrics().density;
String programCacheDir = context.getCacheDir().getAbsolutePath();

// Initialise native peer
nativeInitialize(this, fileSource, pixelRatio, programCacheDir);
nativeInitialize(this, fileSource, pixelRatio, programCacheDir, localIdeographFontFamily);
}

public void onStart() {
Expand Down Expand Up @@ -112,7 +111,8 @@ void queueEvent(MapRendererRunnable runnable) {
private native void nativeInitialize(MapRenderer self,
FileSource fileSource,
float pixelRatio,
String programCacheDir);
String programCacheDir,
String localIdeographFontFamily);

@CallSuper
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class GLSurfaceViewMapRenderer extends MapRenderer implements GLSurfaceVi

private final GLSurfaceView glSurfaceView;

public GLSurfaceViewMapRenderer(Context context, GLSurfaceView glSurfaceView) {
super(context);
public GLSurfaceViewMapRenderer(Context context, GLSurfaceView glSurfaceView, String localIdeographFontFamily) {
super(context, localIdeographFontFamily);
this.glSurfaceView = glSurfaceView;
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setEGLConfigChooser(new EGLConfigChooser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public class TextureViewMapRenderer extends MapRenderer {
* @param context the current Context
* @param textureView the TextureView
*/
public TextureViewMapRenderer(@NonNull Context context, @NonNull TextureView textureView) {
super(context);
public TextureViewMapRenderer(@NonNull Context context,
@NonNull TextureView textureView,
String localIdeographFontFamily) {
super(context, localIdeographFontFamily);
renderThread = new TextureViewRenderThread(textureView, this);
renderThread.start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.mapbox.mapboxsdk.text;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.support.annotation.WorkerThread;

/**
* LocalGlyphRasterizer is the Android-specific platform implementation used
* by the portable local_glyph_rasterizer.hpp
*/
public class LocalGlyphRasterizer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be package protected I think? Could you please add short JavaDoc description of the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a short JavaDoc description. Made the drawGlyphMap static method protected -- is that what you meant?


/***
* Uses Android-native drawing code to rasterize a single glyph
* to a square @{link Bitmap} which can be returned to portable
* code for transformation into a Signed Distance Field glyph.
*
* @param fontFamily Font family string to pass to Typeface.create
* @param bold If true, use Typeface.BOLD option
* @param glyphID 16-bit Unicode BMP codepoint to draw
*
* @return Return a @{link Bitmap} to be displayed in the requested tile.
*/
@WorkerThread
protected static Bitmap drawGlyphBitmap(String fontFamily, boolean bold, char glyphID) {
/*
35x35px dimensions are hardwired to match local_glyph_rasterizer.cpp
These dimensions are large enough to draw a 24 point character in the middle
of the bitmap (y: 20) with some buffer around the edge
*/
Bitmap bitmap = Bitmap.createBitmap(35, 35, Bitmap.Config.ARGB_8888);

Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(24);
paint.setTypeface(Typeface.create(fontFamily, bold ? Typeface.BOLD : Typeface.NORMAL));

Canvas canvas = new Canvas();
canvas.setBitmap(bitmap);
canvas.drawText(String.valueOf(glyphID), 0, 20, paint);

return bitmap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<!--Configuration-->
<public name="mapbox_styleUrl" type="attr" />
<public name="mapbox_apiBaseUrl" type="attr" />
<public name="mapbox_localIdeographFontFamily" type="attr" />

<!--Camera-->
<public name="mapbox_cameraTargetLng" type="attr" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!--Configuration-->
<attr name="mapbox_styleUrl" format="string"/>
<attr name="mapbox_apiBaseUrl" format="string"/>
<attr name="mapbox_localIdeographFontFamily" format="string"/>

<!--Camera-->
<attr name="mapbox_cameraTargetLat" format="float"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,29 +367,32 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity android:name=".activity.snapshot.MapSnapshotterActivity"
android:description="@string/description_map_snapshotter"
android:label="@string/activity_map_snapshotter">
<activity
android:name=".activity.snapshot.MapSnapshotterActivity"
android:description="@string/description_map_snapshotter"
android:label="@string/activity_map_snapshotter">
<meta-data
android:name="@string/category"
android:value="@string/category_imagegenerator"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity android:name=".activity.snapshot.MapSnapshotterReuseActivity"
android:description="@string/description_map_snapshotter_reuse"
android:label="@string/activity_map_snapshotter_reuse">
<activity
android:name=".activity.snapshot.MapSnapshotterReuseActivity"
android:description="@string/description_map_snapshotter_reuse"
android:label="@string/activity_map_snapshotter_reuse">
<meta-data
android:name="@string/category"
android:value="@string/category_imagegenerator"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity android:name=".activity.snapshot.MapSnapshotterMarkerActivity"
android:description="@string/description_map_snapshotter_marker"
android:label="@string/activity_map_snapshotter_marker">
<activity
android:name=".activity.snapshot.MapSnapshotterMarkerActivity"
android:description="@string/description_map_snapshotter_marker"
android:label="@string/activity_map_snapshotter_marker">
<meta-data
android:name="@string/category"
android:value="@string/category_imagegenerator"/>
Expand Down Expand Up @@ -586,8 +589,8 @@
</activity>
<activity
android:name=".activity.style.AnimatedImageSourceActivity"
android:label="@string/activity_animated_image_source"
android:description="@string/description_animated_image_source">
android:description="@string/description_animated_image_source"
android:label="@string/activity_animated_image_source">
<meta-data
android:name="@string/category"
android:value="@string/category_style"/>
Expand All @@ -597,8 +600,8 @@
</activity>
<activity
android:name=".activity.style.GridSourceActivity"
android:label="@string/title_activity_grid_source"
android:description="@string/description_grid_source">
android:description="@string/description_grid_source"
android:label="@string/activity_grid_source">
<meta-data
android:name="@string/category"
android:value="@string/category_style"/>
Expand Down Expand Up @@ -730,36 +733,51 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>
<activity android:name=".activity.maplayout.BottomSheetActivity"
android:description="@string/description_bottom_sheet"
android:label="@string/activity_bottom_sheet">
<activity
android:name=".activity.maplayout.BottomSheetActivity"
android:description="@string/description_bottom_sheet"
android:label="@string/activity_bottom_sheet">
<meta-data
android:name="@string/category"
android:value="@string/category_maplayout"/>
</activity>

<!-- TextureView -->
<activity android:name=".activity.textureview.TextureViewDebugModeActivity"
android:description="@string/description_textureview_debug"
android:label="@string/activity_textureview_debug">
<activity
android:name=".activity.textureview.TextureViewDebugModeActivity"
android:description="@string/description_textureview_debug"
android:label="@string/activity_textureview_debug">
<meta-data
android:name="@string/category"
android:value="@string/category_textureview"/>
</activity>
<activity android:name=".activity.textureview.TextureViewResizeActivity"
android:description="@string/description_textureview_resize"
android:label="@string/activity_textureview_resize">
<activity
android:name=".activity.textureview.TextureViewResizeActivity"
android:description="@string/description_textureview_resize"
android:label="@string/activity_textureview_resize">
<meta-data
android:name="@string/category"
android:value="@string/category_textureview"/>
</activity>
<activity android:name=".activity.textureview.TextureViewAnimationActivity"
android:description="@string/description_textureview_animate"
android:label="@string/activity_textureview_animate">
<activity
android:name=".activity.textureview.TextureViewAnimationActivity"
android:description="@string/description_textureview_animate"
android:label="@string/activity_textureview_animate">
<meta-data
android:name="@string/category"
android:value="@string/category_textureview"/>
</activity>
<activity
android:name=".activity.maplayout.LocalGlyphActivity"
android:description="@string/description_local_glyph"
android:label="@string/activity_local_glyph">
<meta-data
android:name="@string/category"
android:value="@string/category_maplayout"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>

<!-- For Instrumentation tests -->
<activity
Expand Down
Loading