max*_*dev 7 c fonts freetype text-rendering cairo
我有一个只有freetype2和cairo库的系统.我想要实现的是:
不幸的是,文档并没有真正解释它应该如何完成,因为他们希望使用像Pango这样的更高级别的库.
我认为可能是正确的:创建一个缩放字体,cairo_scaled_font_create然后使用检索文本的字形cairo_scaled_font_text_to_glyphs.cairo_glyph_extents然后给出每个字形的范围.但是,我怎么能得到像字距调整和进步这样的东西呢?另外,我怎样才能获得每种字体的路径?
有关此主题的更多资源吗?这些功能是预期的方式吗?
好的,所以我找到了需要的东西。
您首先需要创建一个cairo_scaled_font_t代表特定大小的字体的。为此,可以cairo_get_scaled_font在设置字体后简单地使用它,它会为上下文中的当前设置创建缩放字体。
接下来,使用转换输入文本cairo_scaled_font_text_to_glyphs,这将提供一个字形数组,也可以将其聚类为输出。群集映射表示UTF-8字符串的哪一部分属于字形数组中的相应字形。
要获取字形的范围,请cairo_scaled_font_glyph_extents使用。它给出了每个字形/字形集的尺寸,行进和方位。
最后,字形的路径可以使用放置在上下文中cairo_glyph_path。然后可以根据需要绘制这些路径。
以下示例将输入字符串转换为字形,检索其范围并进行渲染:
const char* text = "Hello world";
int fontSize = 14;
cairo_font_face_t* fontFace = ...;
// get the scaled font object
cairo_set_font_face(cr, fontFace);
cairo_set_font_size(cr, fontSize);
auto scaled_face = cairo_get_scaled_font(cr);
// get glyphs for the text
cairo_glyph_t* glyphs = NULL;
int glyph_count;
cairo_text_cluster_t* clusters = NULL;
int cluster_count;
cairo_text_cluster_flags_t clusterflags;
auto stat = cairo_scaled_font_text_to_glyphs(scaled_face, 0, 0, text, strlen(text), &glyphs, &glyph_count, &clusters, &cluster_count,
&clusterflags);
// check if conversion was successful
if (stat == CAIRO_STATUS_SUCCESS) {
// text paints on bottom line
cairo_translate(cr, 0, fontSize);
// draw each cluster
int glyph_index = 0;
int byte_index = 0;
for (int i = 0; i < cluster_count; i++) {
cairo_text_cluster_t* cluster = &clusters[i];
cairo_glyph_t* clusterglyphs = &glyphs[glyph_index];
// get extents for the glyphs in the cluster
cairo_text_extents_t extents;
cairo_scaled_font_glyph_extents(scaled_face, clusterglyphs, cluster->num_glyphs, &extents);
// ... for later use
// put paths for current cluster to context
cairo_glyph_path(cr, clusterglyphs, cluster->num_glyphs);
// draw black text with green stroke
cairo_set_source_rgba(cr, 0.2, 0.2, 0.2, 1.0);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 0, 1, 0, 1.0);
cairo_set_line_width(cr, 0.5);
cairo_stroke(cr);
// glyph/byte position
glyph_index += cluster->num_glyphs;
byte_index += cluster->num_bytes;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2703 次 |
| 最近记录: |