0

レンダラーとして Cario (OpenGL をターゲット) を使用する既製の textarea クラスはありますか?

テキストエリアとは、ワードラップと幅と高さの制約がある複数行のテキストフィールドを意味します。このクラスを使用する必要があるコードは C++ で記述されています。

4

1 に答える 1

1

1 つの解決策は、 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 ビットのアルファ チャネル値のみが含まれます。何か他のものが必要な場合は、フォーマット変数をいじってください。コンパイル中... Linuxpkg-config --cflags --libs pangocairoで実行する必要があります。窓についてはわかりません。

于 2010-08-03T13:24:26.323 に答える