使用Freetype绘制文本大纲

 施小露107 发布于 2023-02-07 10:34

我在我的程序中实现了FreeType,我可以用颜色和样式绘制文本(粗体,斜体,下划线).

现在我想对我的文字进行轮廓效果.我该怎么做?

像http://freetype-gl.googlecode.com/svn/wiki/images/cartoon.png这样的效果

我曾尝试两次绘制文本,一个是背景中的黑色,另一个是前景中的白色,结果是错误的.

我试图两次绘制文本,一个是粗体,第二个是前景,这里的结果再次出错了.

我想再次进行测试:在"大纲"模式下绘制"背景"文本,在常规模式下绘制前景文本.你怎么看待这件事?

http://freetype-gl.googlecode.com/svn/wiki/images/outline.png

1 个回答
  • 使用2次传递渲染文本是关键,但您必须正确定位文本.首先,您应该渲染整个轮廓,然后在它上面渲染文本.

    渲染大纲:

    // initialize stroker, so you can create outline font
    FT_Stroker stroker;
    FT_Stroker_New(library, &stroker);
    //  2 * 64 result in 2px outline
    FT_Stroker_Set(stroker, 2 * 64, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
    ...
    // generation of an outline for single glyph:
    FT_UInt glyphIndex = FT_Get_Char_Index(face, glyphId);
    FT_Load_Glyph(face, glyphIndex, FT_LOAD_DEFAULT);
    FT_Glyph glyph;
    FT_Get_Glyph(face->glyph, &glyph);
    FT_Glyph_StrokeBorder(&glyph, stroker, false, true);
    FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, nullptr, true);
    FT_BitmapGlyph bitmapGlyph = reinterpret_cast<FT_BitmapGlyph>(glyph);
    // blit the glyph here on your target surface.
    // For positioning use bitmapGlyph->left, bitmapGlyph->top
    // For iteration over the glyph data use bitmapGlyph->bitmap.buffer, bitmapGlyph->bitmap.width, bitmapGlyph->bitmap.rows, bitmapGlyph->bitmap.pitch. 
    

    接下来,您必须在与大纲相同的数据上呈现文本本身.使用上面的代码,但删除该FT_Glyph_StrokeBorder(&glyph, stroker, false, true);行.这样,您将在大纲顶部显示文本.

    要实现这种"卡通"文字效果,您必须完成4次传递:3个轮廓+ 1个文本.在blitting阶段应该进行纹理化或应用渐变.

    2023-02-07 10:37 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有