summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/text/LocalGlyphRasterizer.java
blob: 920a1270ac3f195322a528729037c365443a7e0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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 {

  /***
   * 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;
  }
}