判断 ttf 字体文件是否包含某个字

判断 ttf 字体文件是否包含某个字

标签: unity字体FreeType
  956人阅读  评论(2)  收藏  举报
  分类:

在Unity中,如果指定的字体不包含某个字,Unity就会从 自带的 Arial 中读取字形来创建这个字,本意是好的,但是总是会有问题。



在我们游戏中就有这样的问题,自己制作的微软雅黑字库不包含繁体字,Unity 就会用 Arial 来补全显示。这样看起来很怪。

文章转自http://blog.csdn.NET/huutu http://www.thisisgame.com.cn

我一开始是猜测 我们字库不包含繁体字的。想起来之前做过 Freetype ,于是拿起 Freetype 来做一个验证,通过 Freetype 返回的数据 是否为空,来判断这个 字 在字库中是否存在。


主要是这段代码

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //读取一个字,创建Character;  
  2. bool getCharacter(wchar_t ch)  
  3. {  
  4.     if (m_character[ch].x0 == 0 &&  
  5.         m_character[ch].y0 == 0 &&  
  6.         m_character[ch].x1 == 0 &&  
  7.         m_character[ch].y1 == 0)  
  8.     {  
  9.         //Glyph:字形,图形字符 [glif];  
  10.         //加载这个字的字形,加载到 m_FTFace上面去;  
  11.         FT_Load_Glyph(m_FTFace, FT_Get_Char_Index(m_FTFace, ch), FT_LOAD_DEFAULT);  
  12.   
  13.         //从 m_FTFace上面读取这个字形  到 glyph 变量;  
  14.         FT_Glyph glyph;  
  15.         FT_Get_Glyph(m_FTFace->glyph, &glyph);  
  16.   
  17.         //根据字体大小决定是否启用反锯齿;  
  18.         //字体较小建议使用FT_Render_Mode_Mono;  
  19.         //>12建议使用FT_Render_Mode_Normal;   
  20.         if (!(ch >= L'0' && ch <= L'9'))  
  21.         {  
  22.             FT_Glyph_To_Bitmap(&glyph, ft_render_mode_normal, 0, 1);  
  23.         }  
  24.         else  
  25.         {  
  26.             FT_Glyph_To_Bitmap(&glyph, ft_render_mode_mono, 0, 1);  
  27.         }  
  28.   
  29.         FT_BitmapGlyph bitmapGlyph = (FT_BitmapGlyph)glyph;  
  30.   
  31.         //返回引用;  
  32.         FT_Bitmap& bitmap = bitmapGlyph->bitmap;  
  33.   
  34.         FT_Bitmap targetBitmap;  
  35.   
  36.         //创建了新的 FT_Bitmap;  
  37.         FT_Bitmap_New(&targetBitmap);  
  38.   
  39.         if (bitmap.pixel_mode == FT_PIXEL_MODE_MONO)  
  40.         {  
  41.             //如果Convert成功;  
  42.             if (FT_Bitmap_Convert(m_FTLibrary, &bitmap, &targetBitmap, 1) == 0)  
  43.             {  
  44.                 for (unsigned char* p = targetBitmap.buffer, *endP = p + targetBitmap.width*targetBitmap.rows; p != endP;++p)  
  45.                 {  
  46.                     *p ^= -*p ^ *p;  
  47.                 }  
  48.                 bitmap = targetBitmap;  
  49.             }  
  50.         }  
  51.   
  52.         //如果没有数据;  
  53.         if (bitmap.width == 0 || bitmap.rows == 0)  
  54.         {  
  55.               
  56.             return false;  
  57.         }  
  58.         else  
  59.         {  
  60.               
  61.             return true;  
  62.         }  
  63.     }  
  64. }  

工程打包下载:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. http://pan.baidu.com/s/1jHyJirO  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值