gtk-demo: Font rendering - add outlines

Should outlines as well.
This commit is contained in:
Matthias Clasen 2021-09-04 21:20:55 -04:00
parent 24d69ef125
commit c7215d1199
2 changed files with 106 additions and 34 deletions

View File

@ -17,10 +17,12 @@ static GtkWidget *down_button = NULL;
static GtkWidget *text_radio = NULL; static GtkWidget *text_radio = NULL;
static GtkWidget *show_grid = NULL; static GtkWidget *show_grid = NULL;
static GtkWidget *show_extents = NULL; static GtkWidget *show_extents = NULL;
static GtkWidget *show_pixels = NULL;
static GtkWidget *show_outlines = NULL;
static PangoContext *context; static PangoContext *context;
static int scale = 9; static int scale = 7;
static void static void
update_image (void) update_image (void)
@ -39,6 +41,7 @@ update_image (void)
cairo_hint_style_t hintstyle; cairo_hint_style_t hintstyle;
cairo_hint_metrics_t hintmetrics; cairo_hint_metrics_t hintmetrics;
cairo_antialias_t antialias; cairo_antialias_t antialias;
cairo_path_t *path;
if (!context) if (!context)
context = gtk_widget_create_pango_context (image); context = gtk_widget_create_pango_context (image);
@ -94,10 +97,22 @@ update_image (void)
cairo_set_source_rgb (cr, 1, 1, 1); cairo_set_source_rgb (cr, 1, 1, 1);
cairo_paint (cr); cairo_paint (cr);
cairo_set_source_rgb (cr, 0, 0, 0); if (gtk_check_button_get_active (GTK_CHECK_BUTTON (show_pixels)))
{
if (gtk_check_button_get_active (GTK_CHECK_BUTTON (show_outlines)))
cairo_set_source_rgba (cr, 0, 0, 0, 0.5);
else
cairo_set_source_rgba (cr, 0, 0, 0, 1);
}
else
cairo_set_source_rgba (cr, 0, 0, 0, 0);
cairo_move_to (cr, 10, 10); cairo_move_to (cr, 10, 10);
pango_cairo_show_layout (cr, layout); pango_cairo_show_layout (cr, layout);
pango_cairo_layout_path (cr, layout);
path = cairo_copy_path (cr);
cairo_destroy (cr); cairo_destroy (cr);
g_object_unref (layout); g_object_unref (layout);
@ -136,7 +151,7 @@ update_image (void)
if (gtk_check_button_get_active (GTK_CHECK_BUTTON (show_extents))) if (gtk_check_button_get_active (GTK_CHECK_BUTTON (show_extents)))
{ {
cairo_set_source_rgba (cr, 0, 0, 1, 1); cairo_set_source_rgb (cr, 0, 0, 1);
cairo_rectangle (cr, cairo_rectangle (cr,
scale * (10 + pango_units_to_double (logical.x)) - 0.5, scale * (10 + pango_units_to_double (logical.x)) - 0.5,
@ -149,7 +164,7 @@ update_image (void)
cairo_line_to (cr, scale * (10 + pango_units_to_double (logical.x + logical.width)) + 1, cairo_line_to (cr, scale * (10 + pango_units_to_double (logical.x + logical.width)) + 1,
scale * (10 + pango_units_to_double (baseline)) - 0.5); scale * (10 + pango_units_to_double (baseline)) - 0.5);
cairo_stroke (cr); cairo_stroke (cr);
cairo_set_source_rgba (cr, 1, 0, 0, 1); cairo_set_source_rgb (cr, 1, 0, 0);
cairo_rectangle (cr, cairo_rectangle (cr,
scale * (10 + pango_units_to_double (pink.x)) + 0.5, scale * (10 + pango_units_to_double (pink.x)) + 0.5,
scale * (10 + pango_units_to_double (pink.y)) + 0.5, scale * (10 + pango_units_to_double (pink.y)) + 0.5,
@ -158,8 +173,39 @@ update_image (void)
cairo_stroke (cr); cairo_stroke (cr);
} }
if (gtk_check_button_get_active (GTK_CHECK_BUTTON (show_outlines)))
{
for (int i = 0; i < path->num_data; i += path->data[i].header.length)
{
cairo_path_data_t *data = &path->data[i];
switch (data->header.type)
{
case CAIRO_PATH_CURVE_TO:
data[3].point.x *= scale; data[3].point.y *= scale;
data[2].point.x *= scale; data[2].point.y *= scale;
data[1].point.x *= scale; data[1].point.y *= scale;
break;
case CAIRO_PATH_LINE_TO:
case CAIRO_PATH_MOVE_TO:
data[1].point.x *= scale; data[1].point.y *= scale;
break;
case CAIRO_PATH_CLOSE_PATH:
break;
default:
g_assert_not_reached ();
}
}
cairo_set_source_rgba (cr, 0, 0, 1, 1);
cairo_move_to (cr, scale * 20 - 0.5, scale * 20 - 0.5);
cairo_append_path (cr, path);
cairo_stroke (cr);
}
cairo_surface_destroy (surface); cairo_surface_destroy (surface);
cairo_destroy (cr); cairo_destroy (cr);
cairo_path_destroy (path);
} }
else else
{ {
@ -281,6 +327,8 @@ do_fontrendering (GtkWidget *do_widget)
text_radio = GTK_WIDGET (gtk_builder_get_object (builder, "text_radio")); text_radio = GTK_WIDGET (gtk_builder_get_object (builder, "text_radio"));
show_grid = GTK_WIDGET (gtk_builder_get_object (builder, "show_grid")); show_grid = GTK_WIDGET (gtk_builder_get_object (builder, "show_grid"));
show_extents = GTK_WIDGET (gtk_builder_get_object (builder, "show_extents")); show_extents = GTK_WIDGET (gtk_builder_get_object (builder, "show_extents"));
show_pixels = GTK_WIDGET (gtk_builder_get_object (builder, "show_pixels"));
show_outlines = GTK_WIDGET (gtk_builder_get_object (builder, "show_outlines"));
g_signal_connect (up_button, "clicked", G_CALLBACK (scale_up), NULL); g_signal_connect (up_button, "clicked", G_CALLBACK (scale_up), NULL);
g_signal_connect (down_button, "clicked", G_CALLBACK (scale_down), NULL); g_signal_connect (down_button, "clicked", G_CALLBACK (scale_down), NULL);
@ -292,6 +340,8 @@ do_fontrendering (GtkWidget *do_widget)
g_signal_connect (text_radio, "notify::active", G_CALLBACK (update_image), NULL); g_signal_connect (text_radio, "notify::active", G_CALLBACK (update_image), NULL);
g_signal_connect (show_grid, "notify::active", G_CALLBACK (update_image), NULL); g_signal_connect (show_grid, "notify::active", G_CALLBACK (update_image), NULL);
g_signal_connect (show_extents, "notify::active", G_CALLBACK (update_image), NULL); g_signal_connect (show_extents, "notify::active", G_CALLBACK (update_image), NULL);
g_signal_connect (show_pixels, "notify::active", G_CALLBACK (update_image), NULL);
g_signal_connect (show_outlines, "notify::active", G_CALLBACK (update_image), NULL);
update_image (); update_image ();

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<object class="GtkWindow" id="window"> <object class="GtkWindow" id="window">
<property name="default-width">1080</property> <property name="default-width">1024</property>
<property name="default-height">430</property> <property name="default-height">768</property>
<child type="titlebar"> <child type="titlebar">
<object class="GtkHeaderBar"> <object class="GtkHeaderBar">
<child type="title"> <child type="title">
@ -82,14 +82,51 @@
</object> </object>
</child> </child>
<child> <child>
<object class="GtkLabel"> <object class="GtkCheckButton" id="show_pixels">
<property name="label">Hinting</property> <property name="label">Show Pixels</property>
<property name="xalign">1</property> <property name="active">1</property>
<style>
<class name="dim-label"/>
</style>
<layout> <layout>
<property name="column">3</property> <property name="column">3</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkCheckButton" id="show_outlines">
<property name="label">Show Outline</property>
<layout>
<property name="column">3</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkBox">
<property name="spacing">6</property>
<child>
<object class="GtkLabel">
<property name="label">Hinting</property>
<property name="xalign">1</property>
<style>
<class name="dim-label"/>
</style>
</object>
</child>
<child>
<object class="GtkComboBoxText" id="hinting">
<property name="active">0</property>
<property name="valign">center</property>
<items>
<item translatable="yes" id="none">None</item>
<item translatable="yes" id="slight">Slight</item>
<item translatable="yes" id="medium">Medium</item>
<item translatable="yes" id="full">Full</item>
</items>
</object>
</child>
<layout>
<property name="column">4</property>
<property name="column-span">2</property>
</layout> </layout>
</object> </object>
</child> </child>
@ -97,24 +134,9 @@
<object class="GtkCheckButton" id="antialias"> <object class="GtkCheckButton" id="antialias">
<property name="label">Antialias</property> <property name="label">Antialias</property>
<property name="active">1</property> <property name="active">1</property>
<layout>
<property name="column">3</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="GtkComboBoxText" id="hinting">
<property name="active">0</property>
<property name="valign">center</property>
<items>
<item translatable="yes" id="none">None</item>
<item translatable="yes" id="slight">Slight</item>
<item translatable="yes" id="medium">Medium</item>
<item translatable="yes" id="full">Full</item>
</items>
<layout> <layout>
<property name="column">4</property> <property name="column">4</property>
<property name="row">1</property>
</layout> </layout>
</object> </object>
</child> </child>
@ -122,7 +144,7 @@
<object class="GtkCheckButton" id="hint_metrics"> <object class="GtkCheckButton" id="hint_metrics">
<property name="label">Hint Metrics</property> <property name="label">Hint Metrics</property>
<layout> <layout>
<property name="column">4</property> <property name="column">5</property>
<property name="row">1</property> <property name="row">1</property>
</layout> </layout>
</object> </object>
@ -131,7 +153,7 @@
<object class="GtkCheckButton" id="show_extents"> <object class="GtkCheckButton" id="show_extents">
<property name="label">Show Extents</property> <property name="label">Show Extents</property>
<layout> <layout>
<property name="column">5</property> <property name="column">6</property>
<property name="row">0</property> <property name="row">0</property>
</layout> </layout>
</object> </object>
@ -140,7 +162,7 @@
<object class="GtkCheckButton" id="show_grid"> <object class="GtkCheckButton" id="show_grid">
<property name="label">Show Grid</property> <property name="label">Show Grid</property>
<layout> <layout>
<property name="column">5</property> <property name="column">6</property>
<property name="row">1</property> <property name="row">1</property>
</layout> </layout>
</object> </object>
@ -152,7 +174,7 @@
<class name="circular"/> <class name="circular"/>
</style> </style>
<layout> <layout>
<property name="column">6</property> <property name="column">7</property>
<property name="row">0</property> <property name="row">0</property>
</layout> </layout>
</object> </object>
@ -164,7 +186,7 @@
<class name="circular"/> <class name="circular"/>
</style> </style>
<layout> <layout>
<property name="column">6</property> <property name="column">7</property>
<property name="row">1</property> <property name="row">1</property>
</layout> </layout>
</object> </object>
@ -173,7 +195,7 @@
<object class="GtkLabel"> <object class="GtkLabel">
<property name="hexpand">1</property> <property name="hexpand">1</property>
<layout> <layout>
<property name="column">7</property> <property name="column">8</property>
</layout> </layout>
</object> </object>
</child> </child>