Cario 的文本区域:OpenGL

发布于 2024-09-13 08:03:32 字数 1539 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你的手,一向走下去 2024-09-20 08:03:32

一种解决方案是使用 pango 的 cairo 绑定。使用它可能很快就会变得非常混乱,所以这里有一个要点。如果需要,您可以在 C++ 中围绕它创建类。

#include <pango/pangocairo.h>

// Pango context
PangoContext* pangoContext = pango_font_map_create_context(
    pango_cairo_font_map_get_default());

// Layout and attributes
PangoLayout* pangoLayout = pango_layout_new(pangoContext);
pango_layout_set_wrap(pangoLayout, PANGO_WRAP_WORD_CHAR); 
pango_layout_set_width(pangoLayout, maxWidth * PANGO_SCALE);
pango_layout_set_height(pangoLayout, maxHeight * PANGO_SCALE);

// Set font
PangoFontDescription* fontDesc =
    pango_font_description_from_string("Verdana 10");
pango_layout_set_font_description(pangoLayout, fontDesc);
pango_font_description_free(fontDesc);

// Set text to render
pango_layout_set_text(pangoLayout, text.data(), text.length());

// Allocate buffer
const cairo_format_t format = CAIRO_FORMAT_A8;
const int stride = cairo_format_stride_for_width(format, maxWidth);
GLubyte* buffer = new GLubyte[stride * maxHeight];
std::fill(buffer, buffer + stride * maxHeight, 0);

// Create cairo surface for buffer
cairo_surface_t* crSurface = cairo_image_surface_create_for_data(
    buffer, format, maxWidth, maxHeight, stride);
if (cairo_surface_status(crSurface) != CAIRO_STATUS_SUCCESS) {
    // Error
}

// Create cairo context
cairo_t* crContext = cairo_create(crSurface);
if (cairo_status(crContext) != CAIRO_STATUS_SUCCESS) {
    // Error
}

// Draw
cairo_set_source_rgb(crContext, 1.0, 1.0, 1.0);
pango_cairo_show_layout(crContext, pangoLayout);

// Cleanup
cairo_destroy(crContext);
cairo_surface_destroy(crSurface);
g_object_unref(pangoLayout);
g_object_unref(pangoContext);

// TODO: you can do whatever you want with buffer now
// copy on the texture maybe?

delete[] buffer;

在这种情况下,缓冲区将仅包含 8 位 Alpha 通道值。如果你想要别的东西,可以修改格式变量。编译... pkg-config --cflags --libs pangocairo 应该在 Linux 上完成。我对窗户一无所知。

One solution is to use pango's cairo bindings. Using it could get quite confusing really fast so here's a essentials. You could create class around it in C++ if you want.

#include <pango/pangocairo.h>

// Pango context
PangoContext* pangoContext = pango_font_map_create_context(
    pango_cairo_font_map_get_default());

// Layout and attributes
PangoLayout* pangoLayout = pango_layout_new(pangoContext);
pango_layout_set_wrap(pangoLayout, PANGO_WRAP_WORD_CHAR); 
pango_layout_set_width(pangoLayout, maxWidth * PANGO_SCALE);
pango_layout_set_height(pangoLayout, maxHeight * PANGO_SCALE);

// Set font
PangoFontDescription* fontDesc =
    pango_font_description_from_string("Verdana 10");
pango_layout_set_font_description(pangoLayout, fontDesc);
pango_font_description_free(fontDesc);

// Set text to render
pango_layout_set_text(pangoLayout, text.data(), text.length());

// Allocate buffer
const cairo_format_t format = CAIRO_FORMAT_A8;
const int stride = cairo_format_stride_for_width(format, maxWidth);
GLubyte* buffer = new GLubyte[stride * maxHeight];
std::fill(buffer, buffer + stride * maxHeight, 0);

// Create cairo surface for buffer
cairo_surface_t* crSurface = cairo_image_surface_create_for_data(
    buffer, format, maxWidth, maxHeight, stride);
if (cairo_surface_status(crSurface) != CAIRO_STATUS_SUCCESS) {
    // Error
}

// Create cairo context
cairo_t* crContext = cairo_create(crSurface);
if (cairo_status(crContext) != CAIRO_STATUS_SUCCESS) {
    // Error
}

// Draw
cairo_set_source_rgb(crContext, 1.0, 1.0, 1.0);
pango_cairo_show_layout(crContext, pangoLayout);

// Cleanup
cairo_destroy(crContext);
cairo_surface_destroy(crSurface);
g_object_unref(pangoLayout);
g_object_unref(pangoContext);

// TODO: you can do whatever you want with buffer now
// copy on the texture maybe?

delete[] buffer;

In this case buffer will contain 8 bit alpha channel values only. Fiddle with format variable if you want something else. Compiling... pkg-config --cflags --libs pangocairo should do it on Linux. I have no idea about windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文