我有一个只有freetype2和cairo库可用的系统。我想要实现的是:
获取utf-8文本的标志符号
布局文本,存储位置信息(我自己)
为每个要渲染的glyph获取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字符串的哪个部分属于glyph数组中相应的glyph。
要获取字形的范围,使用cairo_scaled_font_glyph_extents。它给出了每个字形/字形集的尺寸、前进方向和方向。
最后,glyph的路径可以使用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;
    }
}

08-16 02:29