渲染多属性textview

发布于 2024-10-25 07:01:55 字数 228 浏览 2 评论 0原文

问题:我有一个 Textview,在不同的选定文本区域中具有不同的 Texttags。在编辑结束时,我想将这个多标记文本缓冲区/文本视图渲染到绘图区域中。但我不知道如何使用 Pango/cairo 布局渲染多标记文本视图?如果我能得到任何显示文本绘图的 Gtk+/Gtkmm 代码,那就太好了。

例如:假设我的文本是“AAABBB”,并且在此 AAA 中设置了粗体标签,而 BBB 是非粗体和斜体设置。现在我如何呈现这种文本?

problem: I have a Textview with different Texttags in different selected text areas. At the end of editing, i would like to render this multi tagged textbuffer/textview into drawingarea.But I am not getting how can I render multi tagged textview using Pango/cairo layout ?. It would be great if I get any Gtk+/Gtkmm code that shows text drawing.

EX: Lets say My text is "AAABBB" and in this AAA has bold tag set and BBB is non bold and italic set..now how can i render this kind of text ?

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

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

发布评论

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

评论(1

何处潇湘 2024-11-01 07:01:55

假设您正在寻找 C++ 示例,请检查下面的代码是否适合您:

#include <gtk/gtk.h>

static gboolean on_expose_event_0(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
    GdkScreen *screen = gdk_drawable_get_screen(widget->window);
    PangoRenderer *renderer = gdk_pango_renderer_get_default (screen);
    GdkGC *gc = gdk_gc_new(widget->window);

    gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER (renderer), widget->window);
    gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER (renderer), gc);

    PangoContext *context = gdk_pango_context_get_for_screen (screen);
    PangoLayout *layout = pango_layout_new (context);

    pango_layout_set_markup(layout, "<b>AAA</b><i>bbb</i>", -1);

    PangoFontDescription *font = pango_font_description_from_string("Times 20");
    pango_layout_set_font_description(layout, font);
    pango_font_description_free(font);

    pango_renderer_draw_layout (renderer, layout, 1, 1);

    gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER (renderer), NULL);
    gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER (renderer), NULL);

    g_object_unref(layout);
    g_object_unref(context);
    g_object_unref(gc);

    return FALSE;
}

int main( int argc, char *argv[])
{
    GtkWidget *window;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK);

    g_signal_connect(window, "expose-event", G_CALLBACK(on_expose_event_0), NULL);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(window), "pango test");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
    gtk_widget_set_app_paintable(window, TRUE);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}

这里还有一个关于在 cairo 中使用 pango 的示例: 在 Cairo 中使用 Pango

希望这有帮助,问候

Assuming you're looking for a c++ example, check if code below would work for you:

#include <gtk/gtk.h>

static gboolean on_expose_event_0(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
    GdkScreen *screen = gdk_drawable_get_screen(widget->window);
    PangoRenderer *renderer = gdk_pango_renderer_get_default (screen);
    GdkGC *gc = gdk_gc_new(widget->window);

    gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER (renderer), widget->window);
    gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER (renderer), gc);

    PangoContext *context = gdk_pango_context_get_for_screen (screen);
    PangoLayout *layout = pango_layout_new (context);

    pango_layout_set_markup(layout, "<b>AAA</b><i>bbb</i>", -1);

    PangoFontDescription *font = pango_font_description_from_string("Times 20");
    pango_layout_set_font_description(layout, font);
    pango_font_description_free(font);

    pango_renderer_draw_layout (renderer, layout, 1, 1);

    gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER (renderer), NULL);
    gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER (renderer), NULL);

    g_object_unref(layout);
    g_object_unref(context);
    g_object_unref(gc);

    return FALSE;
}

int main( int argc, char *argv[])
{
    GtkWidget *window;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_widget_add_events (window, GDK_BUTTON_PRESS_MASK);

    g_signal_connect(window, "expose-event", G_CALLBACK(on_expose_event_0), NULL);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(window), "pango test");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
    gtk_widget_set_app_paintable(window, TRUE);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}

There is also an example on using pango with cairo here: Using Pango with Cairo

hope this helps, regards

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