summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorOwen Taylor <otaylor@redhat.com>2000-02-01 06:55:09 +0000
committerOwen Taylor <otaylor@src.gnome.org>2000-02-01 06:55:09 +0000
commit532c6902c34f47d5f838623c951e53d5fcdd395f (patch)
tree1a336bca112ada6b934f264b9c832bceb6fe1771 /examples
parent635469870a2b1375105cb305b9f4b09fa05d2208 (diff)
downloadpango-532c6902c34f47d5f838623c951e53d5fcdd395f.tar.gz
The great header file reorganization. Split up roughtly by objects.
Wed Feb 2 00:07:13 2000 Owen Taylor <otaylor@redhat.com> * libpango/pango-*.h: The great header file reorganization. Split up roughtly by objects. * libpango/pango-font.h libpango/fonts.h: Add generic font-loading and listing interfaces. * libpango/pangox.c: Implement font-listing/loading interfaces for X. * libpango/pango-context.[ch]: Flesh out context structure. Add appropriate accesors, font loading-methods, etc. * libpango/pango-coverage.[ch]: Coverage map objects. * examples/viewer.c: First stab at adding font-selection. Majorly deficient for the moment until we add font lists and fallbacks based on coverage maps.
Diffstat (limited to 'examples')
-rw-r--r--examples/viewer.c150
1 files changed, 139 insertions, 11 deletions
diff --git a/examples/viewer.c b/examples/viewer.c
index c5411ab8..9d512dd3 100644
--- a/examples/viewer.c
+++ b/examples/viewer.c
@@ -60,6 +60,7 @@ struct _Line {
int offset; /* Offset from left margin line, in pixels */
};
+PangoFontDescription font_description;
static PangoFont *font = NULL;
static Paragraph *highlight_para;
static int highlight_offset;
@@ -67,7 +68,7 @@ static int highlight_offset;
static GtkWidget *message_label;
GtkWidget *layout;
-gboolean global_rtl;
+PangoContext *context;
/* Read an entire file into a string
*/
@@ -294,15 +295,12 @@ layout_paragraph (Paragraph *para, int width)
GList *runs;
int remaining_width;
int height = 0;
- PangoContext context;
+ PangoDirection base_dir = pango_context_get_base_dir (context);
/* Break paragraph into runs with consistent shaping engine
* and direction
*/
- context.lang = "en_US";
- context.render_type = PANGO_RENDER_TYPE_X;
- context.direction = global_rtl ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
- runs = pango_itemize (&context, para->text, para->length, NULL, 0);
+ runs = pango_itemize (context, para->text, para->length, NULL, 0);
/* Break runs to fit on each line
*/
@@ -370,7 +368,7 @@ layout_paragraph (Paragraph *para, int width)
*/
GList *visual_list;
- line->offset = global_rtl ? remaining_width : 0;
+ line->offset = (base_dir == PANGO_DIRECTION_RTL) ? remaining_width : 0;
line->runs = g_list_reverse (line->runs);
remaining_width = width;
height += line->ascent + line->descent;
@@ -550,6 +548,7 @@ char_bounds (GList *paragraphs, Paragraph *para, int offset,
GList *para_list, *line_list, *run_list;
int pixels = 0;
int chars_seen = 0;
+ PangoDirection base_dir = pango_context_get_base_dir (context);
para_list = paragraphs;
while (para_list)
@@ -576,7 +575,7 @@ char_bounds (GList *paragraphs, Paragraph *para, int offset,
x, width);
*y = pixels;
*height = line->ascent + line->descent;
- if (global_rtl)
+ if (base_dir == PANGO_DIRECTION_RTL)
*x += line->offset;
return;
@@ -794,17 +793,128 @@ button_press (GtkWidget *layout, GdkEventButton *event, GList *paragraphs)
static void
checkbutton_toggled (GtkWidget *widget, gpointer data)
{
- global_rtl = GTK_TOGGLE_BUTTON (widget)->active;
+ pango_context_set_base_dir (context, GTK_TOGGLE_BUTTON (widget)->active ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR);
gtk_widget_queue_resize (layout);
}
+static void
+reload_font ()
+{
+ PangoFont *new_font;
+
+ new_font = pango_context_load_font (context, &font_description, 16.);
+
+ if (new_font)
+ {
+ pango_font_unref (font);
+ font = new_font;
+ }
+
+ gtk_widget_queue_resize (layout);
+}
+
+void
+set_family (GtkWidget *menu_item, gpointer data)
+{
+ char *label = gtk_object_get_data (GTK_OBJECT (menu_item), "family");
+
+ g_free (font_description.family_name);
+ font_description.family_name = g_strdup (label);
+
+ reload_font ();
+}
+
+GtkWidget *
+make_families_menu ()
+{
+ GtkWidget *menu, *menu_item, *option_menu;
+ GHashTable *families_hash = g_hash_table_new (g_str_hash, g_str_equal);
+ GSList *families = NULL;
+ GSList *tmp_list;
+ int i;
+
+ PangoFontDescription **descs;
+ int n_descs;
+ int index, active = -1;
+
+ pango_context_list_fonts (context, &descs, &n_descs);
+
+ for (i=0; i<n_descs; i++)
+ {
+ if (!g_hash_table_lookup (families_hash, descs[i]->family_name))
+ {
+ g_hash_table_insert (families_hash, descs[i]->family_name, descs[i]);
+ families = g_slist_prepend (families, descs[i]->family_name);
+ }
+ }
+
+ families = g_slist_reverse (families);
+
+ menu = gtk_menu_new ();
+
+ tmp_list = families;
+ index = 0;
+ while (tmp_list)
+ {
+ if (strcmp (tmp_list->data, font_description.family_name) == 0)
+ active = index;
+
+ menu_item = gtk_menu_item_new_with_label (tmp_list->data);
+ gtk_object_set_data_full (GTK_OBJECT (menu_item), "family",
+ g_strdup (tmp_list->data), (GtkDestroyNotify)g_free);
+
+ gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
+ GTK_SIGNAL_FUNC (set_family), NULL);
+ gtk_widget_show (menu_item);
+ gtk_menu_append (GTK_MENU (menu), menu_item);
+
+ tmp_list = tmp_list->next;
+ index++;
+ }
+
+ g_hash_table_destroy (families_hash);
+ g_slist_free (families);
+
+ pango_font_descriptions_free (descs, n_descs);
+
+ option_menu = gtk_option_menu_new ();
+ gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
+
+ if (active != -1)
+ gtk_option_menu_set_history (GTK_OPTION_MENU (option_menu), active);
+
+ return option_menu;
+}
+
+
+GtkWidget *
+make_font_selector (void)
+{
+ GtkWidget *hbox;
+ GtkWidget *util_hbox;
+ GtkWidget *label;
+ GtkWidget *option_menu;
+
+ hbox = gtk_hbox_new (FALSE, 4);
+
+ util_hbox = gtk_hbox_new (FALSE, 2);
+ label = gtk_label_new ("Family:");
+ gtk_box_pack_start (GTK_BOX (util_hbox), label, FALSE, FALSE, 0);
+ option_menu = make_families_menu ();
+ gtk_box_pack_start (GTK_BOX (util_hbox), option_menu, FALSE, FALSE, 0);
+
+ gtk_box_pack_start (GTK_BOX (hbox), util_hbox, FALSE, FALSE, 0);
+
+ return hbox;
+}
+
int
main (int argc, char **argv)
{
char *text;
GtkWidget *window;
GtkWidget *scrollwin;
- GtkWidget *vbox;
+ GtkWidget *vbox, *hbox;
GtkWidget *frame;
GtkWidget *checkbutton;
GList *paragraphs;
@@ -825,6 +935,19 @@ main (int argc, char **argv)
paragraphs = split_paragraphs (text);
+ context = pango_x_get_context (GDK_DISPLAY());
+ pango_context_set_lang (context, "en_US");
+ pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
+
+ font_description.family_name = g_strdup ("fixed");
+ font_description.style = PANGO_STYLE_NORMAL;
+ font_description.variant = PANGO_VARIANT_NORMAL;
+ font_description.weight = 500;
+ font_description.stretch = PANGO_STRETCH_NORMAL;
+
+ font = pango_context_load_font (context, &font_description, 16.);
+
+#if 0
/* We hard code a font globally for now
*/
font = pango_x_load_font (GDK_DISPLAY(),
@@ -832,6 +955,8 @@ main (int argc, char **argv)
"-tamil-tscakaram-medium-r-normal--*-120-*-*-*-*-tscii-0,"
"-gnu-unifont-medium-r-normal--*-160-*-*-c-*-iso10646-1,"
"-*-*-medium-r-normal--*-160-*-*-*-*-*-*");
+#endif
+
/* Create the user interface
*/
@@ -841,9 +966,12 @@ main (int argc, char **argv)
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
- vbox = gtk_vbox_new (FALSE, 3);
+ vbox = gtk_vbox_new (FALSE, 4);
gtk_container_add (GTK_CONTAINER (window), vbox);
+ hbox = make_font_selector ();
+ gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
+
scrollwin = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwin),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);