How use render the color emoji font on Android4.4 device??

423 views
Skip to first unread message

bj

unread,
Oct 16, 2014, 5:57:32 AM10/16/14
to skia-d...@googlegroups.com
I want to display the color emoji on Android 4.4. But did not rendering.

I get glyph index by freetype library. And I render the color emoji to skBitmap. 
then I store the skbitmap to local storage. 
The image is not rendering.

What is the problem?

This is my test code.

=========================================================================================================
#include <string.h>
#include <jni.h>
#include <vector>
#include <string>

#ifdef ANDROID
#include <android/log.h>
#include <unistd.h>

#define LOG_TAG "SKIA-TEST"

#define SKIA_FUNC_START() {usleep(500);__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "+%s(%d)", __PRETTY_FUNCTION__, __LINE__);}
#define SKIA_FUNC_END() {usleep(500);__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, "-%s(%d)", __PRETTY_FUNCTION__, __LINE__);}

#define SKIA_LINE() __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, " --%6ld, %s", __LINE__, __PRETTY_FUNCTION__)

#define SKIA_LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define SKIA_LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define SKIA_LOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define SKIA_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

#endif

#include "SkCanvas.h"
#include "SkImageDecoder.h"
#include <ft2build.h>
#include FT_FREETYPE_H
#include <freetype/tttables.h>
#include <freetype/fttypes.h>
#include <freetype/ftglyph.h>
#include <freetype/ttnameid.h>
#include <freetype/ftsnames.h>
#include <freetype/fterrors.h>
#include <freetype/ftglyph.h>
#include <freetype/ftxf86.h>
#include <freetype/fterrors.h>

#define INDEX 0

#ifndef __CPLUSPLUS
extern "C" {
#endif

JNIEXPORT void JNICALL
Java_com_example_runskia_SkiaView_print(JNIEnv *, jobject);


#ifndef __CPLUSPLUS
}
#endif

void EncdoeBitmap(SkBitmap& bitmap, int32_t glyphIndex) {
    SKIA_LOGE("EncdoeBitmap 1");
    SkImageEncoder::Type type = SkImageEncoder::kPNG_Type;

    char buf[256] = {0,};
    sprintf(buf, "%d", glyphIndex);
    std::string path;
    path.assign("/mnt/sdcard/emoji/");
    path.append("FruityGirl");
    path.append(buf);
    path.append(".png");
    SKIA_LOGE("path = %s", path.c_str());
    bool flag = SkImageEncoder::EncodeFile(path.c_str(), bitmap, type, 80);
    if(!flag) {
        SKIA_LOGE("EncdoeBitmap false");
        return ;
    }
}

void FontRender(std::vector<int32_t>& glyphIndexs) {
    SKIA_LOGE("FontRender 1");
    SkBitmap* bitmap = new SkBitmap();
    bitmap->setConfig(SkBitmap::kARGB_8888_Config,100, 100);
    if(bitmap->allocPixels() == true) {
        memset(bitmap->getPixels(), 0x00, 100 * 100 * 4);
    } else {
        SKIA_LOGE("Bitmap Failed the allowcate");
        delete bitmap;
        return ;
    }

    SkCanvas* canvas = new SkCanvas(*bitmap);
    SkPaint paint;
//    paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
    paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
    canvas->drawLine(0.F, 0.F, 100.F, 100.F, paint);
    paint.setColor(0xFF00FF00);
    paint.setTextSize(45.F);
    SKIA_LOGE("FontRender 2");
    for (int32_t i = 0; i < glyphIndexs.size(); ++i) {
        canvas->drawText(&glyphIndexs[i], 1, 20, 50, paint);
        EncdoeBitmap(*bitmap, glyphIndexs[i]);
        memset(bitmap->getPixels(), 0x00, 100 * 100 * 4);
    }
    SKIA_LOGE("FontRender 3");
}

void Java_com_example_runskia_SkiaView_print(JNIEnv *env, jobject thiz) {
    SKIA_LOGE("Java_com_example_runskia_SkiaView_print");

    FT_Library library;
    FT_Face face;
    FT_Init_FreeType(&library);

    if (FT_Init_FreeType(&library)) {
        SKIA_LOGE("error 1");
        return ;
    }

    std::string fontPath("/mnt/sdcard/FruityGirl.ttf");

    if (FT_New_Face(library, fontPath.c_str(), 0, &face)) {
        return ;
    }

    SKIA_LOGE("Java_com_example_runskia_SkiaView_print 1");
    // 0 is success.
    FT_Error error;

    std::vector<int32_t> glyphIndexs;
    for (int32_t i = 0; i < 100000000; ++i) {
         error = FT_Load_Glyph(face, i, FT_LOAD_DEFAULT );
         if (!error) {
             glyphIndexs.push_back(i);
         }
    }

    SKIA_LOGE("Java_com_example_runskia_SkiaView_print 2");
    FT_Done_Face(face);
    FT_Done_FreeType(library);

    FontRender(glyphIndexs);
}

=========================================================================================================
Thank you

bj

unread,
Oct 16, 2014, 5:59:00 AM10/16/14
to skia-d...@googlegroups.com
I tested the FruityGirl.ttf file .

2014년 10월 16일 목요일 오후 6시 57분 32초 UTC+9, bj 님의 말:

Ratnakar Pai

unread,
Oct 16, 2014, 7:15:09 AM10/16/14
to skia-d...@googlegroups.com

Hello,

Since emoji is rendered as a bitmap font, You may need to enable the embedded bitmap flag on the paint object like this:

<code>
SkPaint paint;
..
paint.setFlags(SkPaint::kEmbeddedBitmapText_Flag); // call before the drawing operation
..
canvas->draw text(...)
</code>

Best regards,
Ratnakar

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To post to this group, send email to skia-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/skia-discuss.
For more options, visit https://groups.google.com/d/optout.

bungeman

unread,
Oct 16, 2014, 11:41:56 AM10/16/14
to skia-d...@googlegroups.com
Comment below inline.
This paint does does not have an SkTypeface set on it, so it will just use the default (empty) font. You need to create an SkTypeface from your font file and set it on the paint for it to be used.

The drawText glyph encoding takes a array of uint16_t, not uint32_t. It also takes a number of bytes, not a count. This would need to be 2 instead of a 1.

bungeman

unread,
Oct 16, 2014, 11:43:12 AM10/16/14
to skia-d...@googlegroups.com
There is no need for SkPaint::kEmbeddedBitmapText_Flag here, as that does not apply to bitmap only fonts. This setting should only affect outline fonts with embedded bitmaps.
Message has been deleted

bj

unread,
Oct 19, 2014, 10:34:36 PM10/19/14
to skia-d...@googlegroups.com
Dear bungeman.

As you mentioned, I have modified the code that is numberbyte(1->2byte) and set SkTypeface to Skpaint.
But the emoji is not rendering. Finally I have setting SkPaint::KEmbeddedBitmapTest_Flag to Skpaint. The emoji is rendering. 
I think that SkPaint set the flag need for emoji rendering.

*My test code.
#define EMOJI_PATH "system/fonts/NotoColorEmoji.ttf"
SkTypeface* face = SkTypeface::CreateFromFile(EMOJI_PATH);
paint.setTypeface(face);
paint.setFlags(SkPaint::kEmbeddedBitmapTest_flag);
cavnas->drawText(&glyphindex[i], 2, 20, 50, paint);

Best regards,
Byungjik

2014년 10월 17일 금요일 오전 12시 43분 12초 UTC+9, bungeman 님의 말:
Reply all
Reply to author
Forward
0 new messages