Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / widgets / font-selector.cpp
1 #define __SP_FONT_SELECTOR_C__
3 /*
4  * Font selection widgets
5  *
6  * Authors:
7  *   Chris Lahey <clahey@ximian.com>
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
11  *
12  * Copyright (C) 1999-2001 Ximian, Inc.
13  * Copyright (C) 2002 Lauris Kaplinski
14  * Copyright (C) -2007 Authors
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include <libnr/nr-blit.h>
24 #include <libnr/nr-convert2geom.h>
25 #include <libnrtype/font-instance.h>
26 #include <libnrtype/raster-glyph.h>
27 #include <libnrtype/RasterFont.h>
28 #include <libnrtype/TextWrapper.h>
29 #include <libnrtype/one-glyph.h>
30 #include <libnrtype/font-lister.h>
32 #include <2geom/transforms.h>
34 #include <gtk/gtk.h>
35 #include <gtk/gtkframe.h>
36 #include <gtk/gtkscrolledwindow.h>
37 #include <gtk/gtkclist.h>
38 #include <gtk/gtkvbox.h>
39 #include <gtk/gtkcombo.h>
40 #include <gtk/gtkentry.h>
41 #include <gtk/gtkdrawingarea.h>
43 #include "../display/nr-plain-stuff-gdk.h"
44 #include <glibmm/i18n.h>
46 #include "../desktop.h"
47 #include "font-selector.h"
49 /* SPFontSelector */
51 struct SPFontSelector
52 {
53     GtkHBox hbox;
55     unsigned int block_emit : 1;
57     GtkWidget *family;
58     GtkWidget *style;
59     GtkWidget *size;
61     GtkWidget *family_treeview;
62     GtkWidget *style_treeview;
64     NRNameList families;
65     NRStyleList styles;
66     int familyidx;
67     int styleidx;
68     gfloat fontsize;
69     bool fontsize_dirty;
70     font_instance *font;
71 };
74 struct SPFontSelectorClass
75 {
76     GtkHBoxClass parent_class;
78     void (* font_set) (SPFontSelector *fsel, font_instance *font);
79 };
81 enum {
82     FONT_SET,
83     LAST_SIGNAL
84 };
86 static void sp_font_selector_class_init         (SPFontSelectorClass    *c);
87 static void sp_font_selector_init               (SPFontSelector         *fsel);
88 static void sp_font_selector_destroy            (GtkObject              *object);
90 static void sp_font_selector_family_select_row  (GtkTreeSelection       *selection,
91                                                  SPFontSelector         *fsel);
93 static void sp_font_selector_style_select_row   (GtkTreeSelection       *selection,
94                                                  SPFontSelector         *fsel);
96 static void sp_font_selector_size_changed       (GtkComboBox            *combobox,
97                                                  SPFontSelector         *fsel);
99 static void sp_font_selector_emit_set           (SPFontSelector         *fsel);
101 namespace {
102     const char *sizes[] = {
103         "4", "6", "8", "9", "10", "11", "12", "13", "14",
104         "16", "18", "20", "22", "24", "28",
105         "32", "36", "40", "48", "56", "64", "72", "144",
106         NULL
107     };
110 static GtkHBoxClass *fs_parent_class = NULL;
111 static guint fs_signals[LAST_SIGNAL] = { 0 };
113 GType sp_font_selector_get_type()
115     static GType type = 0;
116     if (!type) {
117         GTypeInfo info = {
118             sizeof(SPFontSelectorClass),
119             0, // base_init
120             0, // base_finalize
121             (GClassInitFunc)sp_font_selector_class_init,
122             0, // class_finalize
123             0, // class_data
124             sizeof(SPFontSelector),
125             0, // n_preallocs
126             (GInstanceInitFunc)sp_font_selector_init,
127             0 // value_table
128         };
129         type = g_type_register_static(GTK_TYPE_HBOX, "SPFontSelector", &info, static_cast<GTypeFlags>(0));
130     }
131     return type;
134 static void sp_font_selector_class_init(SPFontSelectorClass *c)
136     GtkObjectClass *object_class = (GtkObjectClass *) c;
138     fs_parent_class = (GtkHBoxClass* )gtk_type_class(GTK_TYPE_HBOX);
140     fs_signals[FONT_SET] = gtk_signal_new ("font_set",
141                                            GTK_RUN_FIRST,
142                                            GTK_CLASS_TYPE(object_class),
143                                            GTK_SIGNAL_OFFSET(SPFontSelectorClass, font_set),
144                                            gtk_marshal_NONE__POINTER,
145                                            GTK_TYPE_NONE,
146                                            1, GTK_TYPE_POINTER);
148     object_class->destroy = sp_font_selector_destroy;
151 static void sp_font_selector_init(SPFontSelector *fsel)
153         gtk_box_set_homogeneous(GTK_BOX(fsel), TRUE);
154         gtk_box_set_spacing(GTK_BOX(fsel), 4);
156         /* Family frame */
157         GtkWidget *f = gtk_frame_new(_("Font family"));
158         gtk_widget_show (f);
159         gtk_box_pack_start (GTK_BOX(fsel), f, TRUE, TRUE, 0);
161         GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
162         gtk_widget_show(sw);
163         gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
164         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
165         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
166         gtk_container_add(GTK_CONTAINER(f), sw);
168         Inkscape::FontLister* fontlister = Inkscape::FontLister::get_instance();
170         fsel->family_treeview = gtk_tree_view_new ();
171         GtkTreeViewColumn *column = gtk_tree_view_column_new ();
172         GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
173         gtk_tree_view_column_pack_start (column, cell, FALSE);
174         gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
175         gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->family_treeview), column);
176         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->family_treeview), FALSE);
177         Glib::RefPtr<Gtk::ListStore> store = fontlister->get_font_list();
178         gtk_tree_view_set_model (GTK_TREE_VIEW(fsel->family_treeview), GTK_TREE_MODEL (Glib::unwrap (store)));
179         gtk_container_add(GTK_CONTAINER(sw), fsel->family_treeview);
180         gtk_widget_show_all (sw);
182         GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->family_treeview));
183         g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_family_select_row), fsel);
184         g_object_set_data (G_OBJECT(fsel), "family-treeview", fsel->family_treeview);
187         /* Style frame */
188         f = gtk_frame_new(_("Style"));
189         gtk_widget_show(f);
190         gtk_box_pack_start(GTK_BOX (fsel), f, TRUE, TRUE, 0);
192         GtkWidget *vb = gtk_vbox_new(FALSE, 4);
193         gtk_widget_show(vb);
194         gtk_container_set_border_width(GTK_CONTAINER (vb), 4);
195         gtk_container_add(GTK_CONTAINER(f), vb);
197         sw = gtk_scrolled_window_new(NULL, NULL);
198         gtk_widget_show(sw);
199         gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
200         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
201         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
202         gtk_box_pack_start(GTK_BOX (vb), sw, TRUE, TRUE, 0);
204         fsel->style_treeview = gtk_tree_view_new ();
205         column = gtk_tree_view_column_new ();
206         cell = gtk_cell_renderer_text_new ();
207         gtk_tree_view_column_pack_start (column, cell, FALSE);
208         gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
209         gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->style_treeview), column);
210         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->style_treeview), FALSE);
211         gtk_container_add(GTK_CONTAINER(sw), fsel->style_treeview);
212         gtk_widget_show_all (sw);
214         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->style_treeview));
215         g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_style_select_row), fsel);
217         GtkWidget *hb = gtk_hbox_new(FALSE, 4);
218         gtk_widget_show(hb);
219         gtk_box_pack_start(GTK_BOX(vb), hb, FALSE, FALSE, 0);
221         fsel->size = gtk_combo_box_entry_new_text ();
222         gtk_widget_set_size_request(fsel->size, 90, -1);
223         g_signal_connect (G_OBJECT(fsel->size), "changed", G_CALLBACK (sp_font_selector_size_changed), fsel);
224         gtk_box_pack_end (GTK_BOX(hb), fsel->size, FALSE, FALSE, 0);
226         GtkWidget *l = gtk_label_new(_("Font size:"));
227         gtk_widget_show_all (l);
228         gtk_box_pack_end(GTK_BOX (hb), l, FALSE, FALSE, 0);
230         for (unsigned int n = 0; sizes[n]; ++n)
231             {
232                 gtk_combo_box_append_text (GTK_COMBO_BOX(fsel->size), sizes[n]);
233             }
235         gtk_widget_show_all (fsel->size);
237         fsel->familyidx = 0;
238         fsel->styleidx = 0;
239         fsel->fontsize = 10.0;
240         fsel->fontsize_dirty = false;
241         fsel->font = NULL;
244 static void sp_font_selector_destroy(GtkObject *object)
246     SPFontSelector *fsel = SP_FONT_SELECTOR (object);
248     if (fsel->font) {
249         fsel->font->Unref();
250         fsel->font = NULL;
251     }
253     if (fsel->families.length > 0) {
254         nr_name_list_release(&fsel->families);
255         fsel->families.length = 0;
256     }
258     if (fsel->styles.length > 0) {
259         nr_style_list_release(&fsel->styles);
260         fsel->styles.length = 0;
261     }
263     if (GTK_OBJECT_CLASS(fs_parent_class)->destroy) {
264         GTK_OBJECT_CLASS(fs_parent_class)->destroy(object);
265     }
268 static void sp_font_selector_family_select_row(GtkTreeSelection *selection,
269                                                SPFontSelector *fsel)
271     GtkTreeIter   iter;
272     GtkTreeModel *model;
273     GtkListStore *store;
274     GtkTreePath  *path;
275     GList        *list=0;
277     if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
279     path = gtk_tree_model_get_path (model, &iter);
280     gtk_tree_model_get (model, &iter, 1, &list, -1);
281     fsel->familyidx = gtk_tree_path_get_indices (path)[0];
282     fsel->styleidx = 0;
284     store = gtk_list_store_new (1, G_TYPE_STRING);
286     for ( ; list ; list = list->next )
287     {
288         gtk_list_store_append (store, &iter);
289         gtk_list_store_set (store, &iter, 0, (char*)list->data, -1);
290     }
292     gtk_tree_view_set_model (GTK_TREE_VIEW (fsel->style_treeview), GTK_TREE_MODEL (store));
293     path = gtk_tree_path_new ();
294     gtk_tree_path_append_index (path, 0);
295     gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path);
296     gtk_tree_path_free (path);
299 static void sp_font_selector_style_select_row (GtkTreeSelection *selection,
300                                                SPFontSelector   *fsel)
302     GtkTreeModel *model;
303     GtkTreePath  *path;
304     GtkTreeIter   iter;
306     if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
308     path = gtk_tree_model_get_path (model, &iter);
309     fsel->styleidx = gtk_tree_path_get_indices (path)[0];
311     if (!fsel->block_emit)
312     {
313         sp_font_selector_emit_set (fsel);
314     }
317 static void sp_font_selector_size_changed( GtkComboBox */*cbox*/, SPFontSelector *fsel )
319     char *text = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
320     gfloat old_size = fsel->fontsize;
322     gchar *endptr;
323     gdouble value = -1;
324     if (text) {
325         value = g_strtod (text, &endptr);
326         if (endptr == text) // conversion failed, non-numeric input
327             value = -1;
328         free (text);
329     }
330     if (value <= 0) {
331         return; // could not parse value 
332     }
333     if (value > 10000)
334         value = 10000; // somewhat arbitrary, but text&font preview freezes with too huge fontsizes
336     fsel->fontsize = value;
337     if ( fabs(fsel->fontsize-old_size) > 0.001)
338     {
339         fsel->fontsize_dirty = true;
340     }
342     sp_font_selector_emit_set (fsel);
345 static void sp_font_selector_emit_set (SPFontSelector *fsel)
347     font_instance *font;
349     GtkTreeSelection *selection_family;
350     GtkTreeSelection *selection_style;
351     GtkTreeModel     *model_family;
352     GtkTreeModel     *model_style;
353     GtkTreeIter       iter_family;
354     GtkTreeIter       iter_style;
355     char             *family=0, *style=0;
357     //We need to check this here since most GtkTreeModel operations are not atomic
358     //See GtkListStore documenation, Chapter "Atomic Operations" --mderezynski
360     model_family = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->family_treeview));
361     if (!model_family) return;
362     model_style = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->style_treeview));
363     if (!model_style) return;
365     selection_family = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview));
366     selection_style = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview));
368     if (!gtk_tree_selection_get_selected (selection_family, NULL, &iter_family)) return;
369     if (!gtk_tree_selection_get_selected (selection_style, NULL, &iter_style)) return;
371     gtk_tree_model_get (model_family, &iter_family, 0, &family, -1);
372     gtk_tree_model_get (model_style, &iter_style, 0, &style, -1);
374     if ((!family) || (!style)) return;
376     font = (font_factory::Default())->FaceFromUIStrings (family, style);
378     // FIXME: when a text object uses non-available font, font==NULL and we can't set size
379     // (and the size shown in the widget is invalid). To fix, here we must always get some
380     // default font, exactly the same as sptext uses for on-canvas display, so that
381     // font!=NULL ever.
382     if (font != fsel->font || ( font && fsel->fontsize_dirty ) ) {
383         if ( font ) {
384             font->Ref();
385         }
386         if ( fsel->font ) {
387             fsel->font->Unref();
388         }
389         fsel->font = font;
390         gtk_signal_emit(GTK_OBJECT(fsel), fs_signals[FONT_SET], fsel->font);
391     }
392     fsel->fontsize_dirty = false;
393     if (font) {
394         font->Unref();
395     }
396     font = NULL;
399 GtkWidget *sp_font_selector_new()
401     SPFontSelector *fsel = (SPFontSelector*) gtk_type_new(SP_TYPE_FONT_SELECTOR);
403     return (GtkWidget *) fsel;
406 void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, double size)
408     if (font)
409     {  
410         Gtk::TreePath path;
411         font_instance *tempFont = NULL;
412         
413         Glib::ustring family = font_factory::Default()->GetUIFamilyString(font->descr);
415         try {
416             path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
417         } catch (...) {
418             return;
419         }
421         fsel->block_emit = TRUE;
422         gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
423         gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
424         fsel->block_emit = FALSE;
426         GList *list = 0;
427         GtkTreeIter iter;
428         GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
429         gtk_tree_model_get_iter (model, &iter, path.gobj());
430         gtk_tree_model_get (model, &iter, 1, &list, -1);
432         unsigned int currentStyleNumber = 0;
433         unsigned int bestStyleNumber = 0;
434         
435         PangoFontDescription *incomingFont = pango_font_description_copy(font->descr);
436         pango_font_description_unset_fields(incomingFont, PANGO_FONT_MASK_SIZE);
437         
438         char *incomingFontString = pango_font_description_to_string(incomingFont);
439         
440         tempFont = (font_factory::Default())->FaceFromUIStrings(family.c_str(), (char*)list->data);
441         
442         PangoFontDescription *bestMatchForFont = NULL;
443         if (tempFont) {
444             bestMatchForFont = pango_font_description_copy(tempFont->descr);
445             tempFont->Unref();
446             tempFont = NULL;
447         }
448         
449         pango_font_description_unset_fields(bestMatchForFont, PANGO_FONT_MASK_SIZE);
450         
451         list = list->next;
452         
453         while (list) {
454             currentStyleNumber++;
455             
456             tempFont = font_factory::Default()->FaceFromUIStrings(family.c_str(), (char*)list->data);
457             
458             PangoFontDescription *currentMatchForFont = NULL;
459             if (tempFont) {
460                 currentMatchForFont = pango_font_description_copy(tempFont->descr);
461                 tempFont->Unref();
462                 tempFont = NULL;
463             }
464             
465             if (currentMatchForFont) {
466                 pango_font_description_unset_fields(currentMatchForFont, PANGO_FONT_MASK_SIZE);
467                 
468                 char *currentMatchString = pango_font_description_to_string(currentMatchForFont);
469                 
470                 if (!strcmp(incomingFontString, currentMatchString)
471                         || pango_font_description_better_match(incomingFont, bestMatchForFont, currentMatchForFont)) {
472                     // Found a better match for the font we are looking for
473                     pango_font_description_free(bestMatchForFont);
474                     bestMatchForFont = pango_font_description_copy(currentMatchForFont);
475                     bestStyleNumber = currentStyleNumber;
476                 }
477                 
478                 g_free(currentMatchString);
479                 
480                 pango_font_description_free(currentMatchForFont);
481             }
482             
483             list = list->next;
484         }
485         
486         if (bestMatchForFont)
487             pango_font_description_free(bestMatchForFont);
488         if (incomingFont)
489             pango_font_description_free(incomingFont);
490         g_free(incomingFontString);
492         GtkTreePath *path_c = gtk_tree_path_new ();
493         gtk_tree_path_append_index (path_c, bestStyleNumber);
494         gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
495         gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
496     
497         if (size != fsel->fontsize)
498         {
499             gchar s[8];
500             g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
501             gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
502             fsel->fontsize = size;
503         }
504     }
505     
508 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
510     if (fsel->font) {
511         fsel->font->Ref();
512     }
514     return fsel->font;
517 double sp_font_selector_get_size(SPFontSelector *fsel)
519     return fsel->fontsize;
522 /* SPFontPreview */
524 struct SPFontPreview
526     GtkDrawingArea darea;
528     font_instance *font;
529     raster_font *rfont;
530     gchar *phrase;
531     unsigned long rgba;
532 };
534 struct SPFontPreviewClass
536     GtkDrawingAreaClass parent_class;
537 };
539 static void sp_font_preview_class_init(SPFontPreviewClass *c);
540 static void sp_font_preview_init(SPFontPreview *fsel);
541 static void sp_font_preview_destroy(GtkObject *object);
543 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req);
544 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
546 static GtkDrawingAreaClass *fp_parent_class = NULL;
548 GType sp_font_preview_get_type()
550     static GType type = 0;
551     if (!type) {
552         GTypeInfo info = {
553             sizeof(SPFontPreviewClass),
554             0, // base_init
555             0, // base_finalize
556             (GClassInitFunc)sp_font_preview_class_init,
557             0, // class_finalize
558             0, // class_data
559             sizeof(SPFontPreview),
560             0, // n_preallocs
561             (GInstanceInitFunc)sp_font_preview_init,
562             0 // value_table
563         };
564         type = g_type_register_static(GTK_TYPE_DRAWING_AREA, "SPFontPreview", &info, static_cast<GTypeFlags>(0));
565     }
566     return type;
569 static void sp_font_preview_class_init (SPFontPreviewClass *c)
571     GtkObjectClass *object_class = (GtkObjectClass *) c;
572     GtkWidgetClass *widget_class = (GtkWidgetClass *) c;
574     fp_parent_class = (GtkDrawingAreaClass*) gtk_type_class(GTK_TYPE_DRAWING_AREA);
576     object_class->destroy = sp_font_preview_destroy;
578     widget_class->size_request = sp_font_preview_size_request;
579     widget_class->expose_event = sp_font_preview_expose;
582 static void sp_font_preview_init(SPFontPreview *fprev)
584     fprev->rgba = 0x000000ff;
587 static void sp_font_preview_destroy(GtkObject *object)
589     SPFontPreview *fprev = SP_FONT_PREVIEW (object);
591     if (fprev->rfont) {
592         fprev->rfont->Unref();
593         fprev->rfont = NULL;
594     }
596     if (fprev->font) {
597         fprev->font->Unref();
598         fprev->font = NULL;
599     }
601     g_free(fprev->phrase);
602     fprev->phrase = NULL;
604     if (GTK_OBJECT_CLASS (fp_parent_class)->destroy) {
605         GTK_OBJECT_CLASS (fp_parent_class)->destroy(object);
606     }
609 void sp_font_preview_size_request(GtkWidget */*widget*/, GtkRequisition *req)
611     req->width = 256;
612     req->height = 32;
615 #define SPFP_MAX_LEN 64
617 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
619     SPFontPreview *fprev = SP_FONT_PREVIEW(widget);
621     if (GTK_WIDGET_DRAWABLE (widget)) {
622         if (fprev->rfont) {
624             int glyphs[SPFP_MAX_LEN];
625             double hpos[SPFP_MAX_LEN];
627             font_instance *tface = fprev->rfont->daddy;
629             double theSize = NR::expansion(fprev->rfont->style.transform);
631             gchar const *p;
632             if (fprev->phrase) {
633                 p = fprev->phrase;
634             } else {
635                 /* TRANSLATORS: Test string used in text and font dialog (when no
636                  * text has been entered) to get a preview of the font.  Choose
637                  * some representative characters that users of your locale will be
638                  * interested in. */
639                 p = _("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()");
640             }
641             int len = 0;
643             NRRect bbox;
644             bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
646             text_wrapper* str_text=new text_wrapper;
647             str_text->SetDefaultFont(tface);
648             str_text->AppendUTF8(p,-1);
649             if ( str_text->uni32_length > 0 ) {
650                 str_text->DoLayout();
651                 if ( str_text->glyph_length > 0 ) {
652                     PangoFont *curPF = NULL;
653                     font_instance *curF = NULL;
654                     for (int i = 0; i < str_text->glyph_length && i < SPFP_MAX_LEN; i++) {
655                         if ( str_text->glyph_text[i].font != curPF ) {
656                             curPF = str_text->glyph_text[i].font;
657                             if (curF) {
658                                 curF->Unref();
659                             }
660                             curF = NULL;
661                             if ( curPF ) {
662                                 PangoFontDescription* pfd = pango_font_describe(curPF);
663                                 curF = (font_factory::Default())->Face(pfd);
664                                 pango_font_description_free(pfd);
665                             }
666                         }
667                         Geom::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
668                         base_pt *= theSize;
670                         glyphs[len] = str_text->glyph_text[i].gl;
671                         hpos[len] = base_pt[0];
672                         len++;
673                         if ( curF ) {
674                             boost::optional<NR::Rect> nbbox = curF->BBox(str_text->glyph_text[i].gl);
675                             if (nbbox) {
676                                 bbox.x0 = MIN(bbox.x0, base_pt[Geom::X] + theSize * (nbbox->min())[0]);
677                                 bbox.y0 = MIN(bbox.y0, base_pt[Geom::Y] - theSize * (nbbox->max())[1]);
678                                 bbox.x1 = MAX(bbox.x1, base_pt[Geom::X] + theSize * (nbbox->max())[0]);
679                                 bbox.y1 = MAX(bbox.y1, base_pt[Geom::Y] - theSize * (nbbox->min())[1]);
680                             }
681                         }
682                     }
683                     if ( curF ) {
684                         curF->Unref();
685                     }
686                 }
687             }
689             // XXX: FIXME: why does this code ignore adv.y
690             /*                  while (p && *p && (len < SPFP_MAX_LEN)) {
691                                 unsigned int unival;
692                                 NRRect gbox;
693                                 unival = g_utf8_get_char (p);
694                                 glyphs[len] =  tface->MapUnicodeChar( unival);
695                                 hpos[len] = (int)px;
696                                 Geom::Point adv = fprev->rfont->Advance(glyphs[len]);
697                                 fprev->rfont->BBox( glyphs[len], &gbox);
698                                 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
699                                 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
700                                 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
701                                 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
702                                 px += adv[Geom::X];
703                                 len += 1;
704                                 p = g_utf8_next_char (p);
705                                 }*/
707             float startx = (widget->allocation.width - (bbox.x1 - bbox.x0)) / 2;
708             float starty = widget->allocation.height - (widget->allocation.height - (bbox.y1 - bbox.y0)) / 2 - bbox.y1;
710             for (int y = event->area.y; y < event->area.y + event->area.height; y += 64) {
711                 for (int x = event->area.x; x < event->area.x + event->area.width; x += 64) {
712                     NRPixBlock pb, m;
713                     int x0 = x;
714                     int y0 = y;
715                     int x1 = MIN(x0 + 64, event->area.x + event->area.width);
716                     int y1 = MIN(y0 + 64, event->area.y + event->area.height);
717                     guchar *ps = nr_pixelstore_16K_new (TRUE, 0xff);
718                     nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, ps, 3 * (x1 - x0), FALSE, FALSE);
719                     nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, x0, y0, x1, y1, TRUE);
720                     pb.empty = FALSE;
722                     PangoFont *curPF = NULL;
723                     font_instance *curF = NULL;
724                     raster_font *curRF = NULL;
725                     for (int i=0; i < len; i++) {
726                         if ( str_text->glyph_text[i].font != curPF ) {
727                             curPF=str_text->glyph_text[i].font;
728                             if ( curF ) {
729                                 curF->Unref();
730                             }
731                             curF = NULL;
732                             if ( curPF ) {
733                                 PangoFontDescription* pfd = pango_font_describe(curPF);
734                                 curF=(font_factory::Default())->Face(pfd);
735                                 pango_font_description_free(pfd);
736                             }
737                             if ( curF ) {
738                                 if ( curRF ) {
739                                     curRF->Unref();
740                                 }
741                                 curRF = NULL;
742                                 curRF = curF->RasterFont(fprev->rfont->style);
743                             }
744                         }
745                         raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
746                         if ( g ) {
747                             g->Blit(Geom::Point(hpos[i] + startx, starty), m);
748                         }
749                     }
750                     if (curRF) {
751                         curRF->Unref();
752                     }
753                     if (curF) {
754                         curF->Unref();
755                     }
757                     nr_blit_pixblock_mask_rgba32(&pb, &m, fprev->rgba);
758                     gdk_draw_rgb_image(widget->window, widget->style->black_gc,
759                                        x0, y0, x1 - x0, y1 - y0,
760                                        GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs);
761                     nr_pixblock_release(&m);
762                     nr_pixblock_release(&pb);
763                     nr_pixelstore_16K_free(ps);
764                 }
765             }
767             delete str_text;
769         } else {
770             nr_gdk_draw_gray_garbage(widget->window, widget->style->black_gc,
771                                      event->area.x, event->area.y,
772                                      event->area.width, event->area.height);
773         }
774     }
776     return TRUE;
779 GtkWidget * sp_font_preview_new()
781     GtkWidget *w = (GtkWidget*) gtk_type_new(SP_TYPE_FONT_PREVIEW);
783     return w;
786 void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontSelector *fsel)
788         if (font)
789         {
790             font->Ref();
791         }
793         if (fprev->font)
794         {
795             fprev->font->Unref();
796         }
798         fprev->font = font;
800         if (fprev->rfont)
801         {
802             fprev->rfont->Unref();
803             fprev->rfont=NULL;
804         }
806         if (fprev->font)
807         {
808             Geom::Matrix flip(Geom::Scale(fsel->fontsize, -fsel->fontsize));
809             fprev->rfont = fprev->font->RasterFont(flip, 0);
810         }
812         if (GTK_WIDGET_DRAWABLE (fprev)) gtk_widget_queue_draw (GTK_WIDGET (fprev));
815 void sp_font_preview_set_rgba32(SPFontPreview *fprev, guint32 rgba)
817     fprev->rgba = rgba;
818     if (GTK_WIDGET_DRAWABLE (fprev)) {
819         gtk_widget_queue_draw (GTK_WIDGET (fprev));
820     }
823 void sp_font_preview_set_phrase(SPFontPreview *fprev, const gchar *phrase)
825     g_free (fprev->phrase);
826     if (phrase) {
827         fprev->phrase = g_strdup (phrase);
828     } else {
829         fprev->phrase = NULL;
830     }
831     if (GTK_WIDGET_DRAWABLE(fprev)) {
832         gtk_widget_queue_draw (GTK_WIDGET (fprev));
833     }
837 /*
838   Local Variables:
839   mode:c++
840   c-file-style:"stroustrup"
841   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
842   indent-tabs-mode:nil
843   fill-column:99
844   End:
845 */
846 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :