c++ - 使用 Freetype 绘制文本轮廓

标签 c++ text opengl-es freetype freetype2

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

现在我想在我的文字上做一个轮廓效果。我该怎么做?

Effect like that
(来源:googlecode.com)

  • 我试过两次绘制文本,一次较大,背景为黑色,第二次为白色,结果错误。
  • 我试过两次绘制文本,一次是粗体,第二次是在前景,结果又是错误的。

我想再做一个测试:用“大纲”模式绘制“背景”文本,用常规模式绘制前景文本。你怎么看?


(来源:googlecode.com)

最佳答案

用 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 阶段完成纹理化或应用渐变。

关于c++ - 使用 Freetype 绘制文本轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20874056/

相关文章:

ios - iOS 上的实际风景 OpenGL View

c++ - QMediaPlayer duration() 总是返回 0

c++ - 在 C++ 中比较 vector 的最快方法是什么?

c++ - 返回临时的 const 引用

c++ - 删除动态分配的数组时出错

text - 多语言支持在 TextMesh Pro 中不起作用

asp.net - 设置文本样式 asp.net javascript

javascript - 如何将文本添加到由 css、html 和 javascript 制作的图表的不同部分?

ios - OpenGL ES 2.0 iOS - 将矩形绘制到模板缓冲区中并限制仅在其中绘制

iphone - 什么是 DOT3 照明?