[HarfBuzz] Building harfbuzz-ng using Android NDK
Shiva Kumar H R
shivahr at gmail.com
Wed Sep 21 09:59:46 PDT 2011
Hi,
I am glad to inform that I could use HarfBuzz-ng and FreeType to render
Indic text on Android Canvas.
Thank you for creating such an elegant lightweight library.
I referred https://github.com/anoek/ex-sdl-cairo-freetype-harfbuzz to come
up with my sample program, the essence of which looks as below:
---- start of indic-text-rendering code snippet -----
#include <hb-ft.h>
...
void drawIndicText(unsigned short* text, int textLength, int xStart, int
yBaseLine, int charHeight) {
FT_Library ft_library;
FT_Init_FreeType(&ft_library); /* initialize library */
char* fontFilePath =
"/sdcard/Android/data/org.iisc.mile.indictext.android/Lohit-Kannada.ttf";
FT_Face ft_face;
FT_New_Face(ft_library, fontFilePath, 0, &ft_face); /* create face
object */
hb_font_t *font = hb_ft_font_create(ft_face, NULL);
FT_Set_Pixel_Sizes(ft_face, 0, charHeight); /* set character size */
/* Create a buffer for harfbuzz to use */
hb_buffer_t *buffer = hb_buffer_create();
//hb_buffer_set_direction(buffer, HB_DIRECTION_LTR); /* or LTR */
hb_buffer_set_script(buffer, HB_SCRIPT_KANNADA); /* see hb-unicode.h */
//hb_buffer_set_language(buffer, hb_language_from_string("ka"));
/* Layout the text */
hb_buffer_add_utf16(buffer, text, textLength, 0, textLength);
hb_ot_shape(font, buffer, NULL, 0, 0);
int glyph_count = hb_buffer_get_length(buffer);
hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(buffer, 0);
hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buffer,
0);
for (int i = 0; i < glyph_count; i++) {
FT_UInt glyph_index = glyph_info[i].codepoint;
FT_Error error = FT_Load_Glyph(ft_face, glyph_index,
FT_LOAD_DEFAULT);
if (error) {
continue; /* ignore errors */
}
/* convert to an anti-aliased bitmap */
error = FT_Render_Glyph(ft_face->glyph, FT_RENDER_MODE_NORMAL);
if (error) {
continue;
}
/* now, draw to our target surface (convert position) */
FT_GlyphSlot slot = ft_face->glyph;
draw_bitmap(&slot->bitmap, xStart + slot->bitmap_left, yBaseLine -
slot->bitmap_top);
int glyphPosX = xStart + glyph_pos[i].x_offset;
int glyphPosY = yBaseLine - glyph_pos[i].y_offset;
//draw_bitmap(&slot->bitmap, glyphPosX, glyphPosY, env, thiz, lock);
/* increment pen position */
xStart += slot->advance.x >> 6;
//xStart += glyph_pos[i].x_advance / 64;
}
hb_buffer_destroy(buffer);
hb_font_destroy(font);
FT_Done_Face(ft_face);
FT_Done_FreeType(ft_library);
return;
}
...
---- end of indic-text-rendering code snippet -----
Few things I noticed:
1) I had earlier used hb_shape() but that didn't work. It worked when I used
hb_ot_shape(). Is this by design?
2) There are a few errors in the sequence of glyphs returned by HarfBuzz for
certain Kannada language words. In the attached snapshot, words in second
edit-box are not rendered properly. Where should I report these errors and
in what format?
I see a test case in
http://cgit.freedesktop.org/harfbuzz/tree/test/test-shape-complex.c . But I
couldn't figure out how to run this. Simply running test-shape-complex.exe
didn't help. Are there some instructions that I can look at?
--
Thanks & Regards,
Shiva Kumar H R
http://people.apache.org/~shivahr/
On Fri, Sep 16, 2011 at 5:05 PM, Shiva Kumar H R <shivahr at gmail.com> wrote:
> I have used a simple Android.mk as below and got the HarfBuzz to build on
> Android NDK. I got below warnings though.
>
> harfbuzz-ng/src/hb-object-private.hh:74:2: warning: #warning "Could not
> find any system to define atomic_int macros, library will NOT be
> thread-safe"
>
> Android.mk:
> ------Begin of Android.mk-------
> BASE_PATH := $(call my-dir)
> LOCAL_PATH:= $(call my-dir)
>
> #############################################################
> # build the harfbuzz library
> #
>
> include $(CLEAR_VARS)
>
> LOCAL_ARM_MODE := arm
>
> LOCAL_MODULE_TAGS := optional
>
> LOCAL_CPP_EXTENSION := .cc
>
> LOCAL_SRC_FILES:= \
> src/hb-blob.cc \
> src/hb-buffer.cc \
> src/hb-common.cc \
> src/hb-fallback-shape.cc \
> src/hb-font.cc \
> src/hb-ot-tag.cc \
> src/hb-shape.cc \
> src/hb-tt-font.cc \
> src/hb-unicode.cc \
> src/hb-ot-layout.cc \
> src/hb-ot-map.cc \
> src/hb-ot-shape.cc \
> src/hb-ot-shape-complex-arabic.cc \
> src/hb-ot-shape-complex-indic.cc \
> src/hb-ot-shape-complex-misc.cc \
> src/hb-ot-shape-normalize.cc \
> src/hb-ft.cc \
>
> LOCAL_C_INCLUDES += \
> $(LOCAL_PATH)/src $(LOCAL_PATH)/../freetype/include
>
> LOCAL_MODULE:= libharfbuzz
>
> LOCAL_STATIC_LIBRARIES := ft2
>
> include $(BUILD_SHARED_LIBRARY)
>
> ------End of Android.mk-------
>
>
> Yet to write a sample program and see if it is actually working.
>
>
> --
> Thanks & Regards,
> Shiva Kumar H R
>
>
> On Fri, Sep 16, 2011 at 3:26 PM, Shiva Kumar H R <shivahr at gmail.com>wrote:
>
>> Hi,
>> Is it possible to build harfbuzz-ng using Android-NDK. If yes, are there
>> any instructions that I can look at?
>>
>> I came across Android.mk in
>> http://tools.impjq.net/android/external/harfbuzz/ , but this seems to be
>> the old Harfbuzz tree and has a dependency on ICU4C.
>>
>> --
>> Thanks & Regards,
>> Shiva Kumar H R
>> http://people.apache.org/~shivahr/
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/harfbuzz/attachments/20110921/d765d729/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: device-2011-09-21-221919.png
Type: image/png
Size: 15391 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/harfbuzz/attachments/20110921/d765d729/attachment.png>
More information about the HarfBuzz
mailing list