BLOG ARTICLE Andrioid System font | 1 ARTICLE FOUND

  1. 2014.09.24 Android, Draw text using system font OpenGLES 2.0


1. Typeface

  you can get typeface info by [Typeface.createFromAsset] method. You don't call this method every frame. Because it happen memory leak problem, so I recommend to use Map class to Manage font data. Below show code using Typeface.createFromAsset.


Hashtable<String, Typeface> m_font_type_face = new Hashtable<String, Typeface>();
	
private ResourceManager()
{
	m_font_type_face.put("CORBEL.TTF", Typeface.createFromAsset(MainActivity.context.getAssets(), "CORBEL.TTF"));
};
	
public Typeface getTypeface(String key)
{
	if(m_font_type_face.containsKey(key))
		return m_font_type_face.get(key);
	return null;
}

※ Use ttf font. Some device don't  support ttc or other fonts.


2. Make Bitmap

  In Android, you can draw a string on Canvas using font predefined. Through Paint class, you can set size, color, etc. I use below code to use in NDK OpenGLES code.

public static void decodeStringTexture(String str,String str_key, String font_name, float fontSize, int r, int g, int b, int a ) { Log.i("AndroidOpenGL", "getStringTextureData start, str : "+ str+" size : "+ fontSize); try { Rect rect = new Rect(); Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG); paint.setTypeface(ResourceManager.instance.getTypeface(font_name)); paint.setTextSize(fontSize); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); paint.setTextAlign(Align.LEFT); paint.setARGB(a, r, g, b); paint.getTextBounds(str,0, str.length(), rect); int width = rect.right; int height = rect.height(); int tex_width = 8; int tex_height = 8; while(width > tex_width) tex_width *= 2; while(height > tex_height) tex_height *= 2; Bitmap bitmap = Bitmap.createBitmap(tex_width, tex_height, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawText(str, 0, 0-rect.top, paint); ByteBuffer bb = ByteBuffer.allocate(tex_width*tex_height*4); bitmap.copyPixelsToBuffer(bb); bitmap.recycle(); Log.i("AndroidOpenGL", "getStringTextureData end"); Log.i("AndroidOpenGL", "Allocated allocate buffer capacity:"+ bb.capacity()); Log.i("AndroidOpenGL", "getStringTextureData end, bb width:"+ tex_width +" height:"+tex_height); onDecodeStringTextureComplete(bb.array(), width, height, tex_width, tex_height, str_key); } catch(Exception e) { Log.e("AndroidOpenGL", "getStringTexture failed.\n"+e.toString()); } }

paint.setTypeface - Set system font predefined.

paint.antialias - make smooth font

paint.getTextBound - get string width, height

cavas.drawText(str, 0, 0-rect.top, paint)


glTexImage2D's width, height parameter is 2^n(exponent) so I set canvas size exponent of 2.

onDecodeStringTextureComplet is jni call. I send  size of string and size of canvas to calculate tex-coordinate value. For example

m_tex_coord[0] = 0.0f;
m_tex_coord[1] = 0.0f;

m_tex_coord[2] = 0.0f;
m_tex_coord[3] = string_height/canvas_height;

m_tex_coord[4] = string_width/canvas_whdith;
m_tex_coord[5] = 0.0f;

m_tex_coord[6] = string_width/canvas_whdith;
m_tex_coord[7] = string_height/canvas_height;

 lastly you can use send data by glTexImage2D.


3. Result Scene


<OpenGL Diary>