什么是使用 Pango 使用 FreeType 字体将文本渲染为图像的简单示例?
Posted
技术标签:
【中文标题】什么是使用 Pango 使用 FreeType 字体将文本渲染为图像的简单示例?【英文标题】:What is a simple example of using Pango to render text into an image using a FreeType font? 【发布时间】:2013-08-25 09:32:40 【问题描述】:我想使用 Pango 渲染文本,但我找不到不使用 Cairo 的示例代码。有人可以给我一个使用 FreeType 后端的 Pango 的简单示例吗?这就是我认为它应该大致工作的方式,但是正如您在代码中看到的那样,有些东西我还没有理解。
// context and font stuff
PangoFontMap *fontmap = pango_ft2_font_map_new();
// do I have to initialize the fontmap?
PangoContext *context = pango_font_map_create_context(fontmap);
PangoFontDescription *desc = pango_font_description_new();
pango_font_description_set_style(desc, PANGO_STYLE_NORMAL); // not bold or italic
pango_font_description_set_size(desc, 12); // 12pt font
PangoFont *font = pango_context_load_font(context, desc);
// target bitmap
FT_Bitmap bitmap;
// how do I say how big the bitmap should be? write it into the struct?
FT_Bitmap_New(&bitmap);
// text
char *text = "Hello World";
PangoAttrList *attrs = pango_attr_list_new();
GList *items = pango_itemize(context, text, 0, strlen(text), attrs, NULL);
// do I have to initialize this?
PangoGlyphString *glyphs = pango_glyph_string_new();
pango_shape(text, strlen(text),
/* PangoAnalysis here – where do I get that? */, glyphs);
pango_ft2_render(&bitmap, font, PangoGlyphString *glyphs,
0, 0/*or does this have to be height-1?*/);
【问题讨论】:
【参考方案1】:我会看一下 pango-view 的源代码,pango 中包含的“玩具”应用程序来展示它的功能。它可以在所有 pango 模式下渲染
pangotext-rendering
【讨论】:
答案不应该只引用外部资源。以上是关于什么是使用 Pango 使用 FreeType 字体将文本渲染为图像的简单示例?的主要内容,如果未能解决你的问题,请参考以下文章
LGVL配合FreeType为可变字体设置字重-ESP32篇
如何在 Python 和 Gtk3 中使用 Pango.AttrType.FOREGROUND?