【原创】Android 移植 freetype 应用实例

本例程使用Bitmap模拟嵌入式 LCD 屏

1、移植 freetype 源码

这个捡现成,可以使用cyanogenmod源码,

https://github.com/julienr/libfreetype-android

https://github.com/CyanogenMod/android_external_freetype

我用的第一个人的,下载下来直接复制过去即可,第二个你可以选不同版本

 

2、封装freetype接口

参考项目:https://github.com/julienr/libfont

freetype 接口使用很简单,可以分3步

  • FT_Init_FreeType 初始化库,library 是全局变量,后面都要使用
if (FT_Init_FreeType(&library)) {
        LOGE("Failed to initialize freetype library");
        error = 1;
        return error;
    }
  • FT_New_Face 加载ttf字体
if (FT_New_Face(library, path, 0, &fontFace)) { //path ttf文件路径
        LOGE("Error loading font face %s", path);
        error = 2;
        return error;
    }
  • FT_Load_Char 加载字符
    wchar_t word = L"鼎";
    
    FT_Set_Pixel_Sizes(fontFace, width, 0); //width字体宽度,单位不是像素
    if (FT_Load_Char(fontFace, word, FT_LOAD_RENDER)) {
    	LOGE("Error loading char %c", word);
    	return;
    }

    FT_Set_Pixel_Sizes用于设置字体尺寸,一般只传 width 或者 height,让字体按比例缩放,不至于变形,
    word 是unicode编码,FT_LOAD_RENDER 模式直接渲染字体,渲染之后可以得到字体点阵,有了点阵就可以渲染了,

        FT_GlyphSlot glyph = fontFace->glyph;
    	
        const int gr = glyph->bitmap.rows; //点阵高度
        const int gw = glyph->bitmap.width; //点阵宽度
    
        x += glyph->bitmap_left; //屏幕上位置 加上字体偏移 不然排版有问题
        y -= glyph->bitmap_top;
    
        for (int h = 0; h < gr; h++) {
            for (int w = 0; w < gw; w++) {
                //Need to add the top margin (marginSize*texSize offset) and the left margin (marginSize)
    //            setPixel(texture, x + w, y + h, texWidth, glyph->bitmap.buffer[w + h * gw], color);
                uint8_t g = glyph->bitmap.buffer[w + h * gw];
                if (g > 0) { //g取值 0~256,0表示透明,其他值可以称作不透明度,可以直接作为32位色的alpha通道,无通道你需要做颜色混合运算
                    if(x + w >= texW || y + h >= texH)
                        continue;
    
                    texture[(y + h) * texW + x + w] = color; //color就是渲染颜色
                    //setPixel(x+w, y+h, g, color);
                }
            }
        }
    //这是一个 rgb565 颜色混合算法
    static void setPixel(int x, int y, uint8_t val, uint16_t color) {
        uint16_t src = texture[x + y * texW];
    
        register uint32_t d1; // 计算用的中间变量,声明为寄存器变量快些
        register uint16_t wa = src; // 源颜色
        register uint16_t wb = color; // 目的颜色
        register uint32_t alpha = 32 - (0.125 * val); // alpha值,16位色的色深为32级,
    
        d1 = (((((((wa << 16) | wa) & 0x7e0f81f) - (((wb << 16) | wb) & 0x7e0f81f)) * alpha) >> 5) +
              (((wb << 16) | wb) & 0x7e0f81f)) & 0x7e0f81f;
        wa = (d1 & 0xffff0000) >> 16; // g...r...b => ..g..
        wb = d1 & 0xffff; // g...r...b => r...b
        uint16_t dst = wa | wb; // rgb
    
        texture[x + y * texW] = dst;
    }

    如此就完成了最简单也最实用的使用方式,大工告成,

  • 附上 demo https://github.com/Yichou/android-freetype-demo

 

参考

rgb565 像素混合算法:https://www.cnblogs.com/yaozhongxiao/archive/2011/12/20/2294544.html

freetype2的FT_Load_Char使用实例:https://blog.csdn.net/dreamInTheWorld/article/details/57413335

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值