好的,我找到了需要的内容。
首先,您需要创建一个
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 = ...;
cairo_set_font_face(cr, fontFace);
cairo_set_font_size(cr, fontSize);
auto scaled_face = cairo_get_scaled_font(cr);
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);
if (stat == CAIRO_STATUS_SUCCESS) {
cairo_translate(cr, 0, fontSize);
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];
cairo_text_extents_t extents;
cairo_scaled_font_glyph_extents(scaled_face, clusterglyphs, cluster->num_glyphs, &extents);
cairo_glyph_path(cr, clusterglyphs, cluster->num_glyphs);
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_index += cluster->num_glyphs;
byte_index += cluster->num_bytes;
}
}
cairo_font_face_t
。然后,您可以使用此字体创建缩放字体。从那时起,路线就不清楚了。 - maxdevcairo_font_face_t
,布局文本并进行渲染。虽然不建议这样做,请参阅cairo页面以了解Pango的使用情况。但是,由于您也无法使用Pango,因此这应该是可行的。为什么机器没有其他库可用? - oldtechaaFT_Bitmap
,然后将位图应用于您的Cairo表面? - oldtechaa