Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        去年关闭。
                                                                                            
                
        
是否有使用Cario(定位OpenGL)作为渲染器的现成的textarea类?

我所说的textarea是指具有自动换行以及宽度和高度限制的多行文本字段。
需要使用此类的代码是用C ++编写的。

最佳答案

一种解决方案是使用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通道值。如果您还需要其他格式,请拨弄format变量。编译... pkg-config --cflags --libs pangocairo应该在Linux上执行。我对Windows一无所知。

10-06 01:27