Code

Hold perspectives on document level rather than globally; this corrects the changes...
[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 <libnrtype/font-instance.h>
25 #include <libnrtype/raster-glyph.h>
26 #include <libnrtype/RasterFont.h>
27 #include <libnrtype/TextWrapper.h>
28 #include <libnrtype/one-glyph.h>
29 #include <libnrtype/font-lister.h>
31 #include <gtk/gtk.h>
32 #include <gtk/gtkframe.h>
33 #include <gtk/gtkscrolledwindow.h>
34 #include <gtk/gtkclist.h>
35 #include <gtk/gtkvbox.h>
36 #include <gtk/gtkcombo.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkdrawingarea.h>
40 #include "../display/nr-plain-stuff-gdk.h"
41 #include <glibmm/i18n.h>
43 #include "../desktop.h"
44 #include "font-selector.h"
46 /* SPFontSelector */
48 struct SPFontSelector
49 {
50     GtkHBox hbox;
51     
52     unsigned int block_emit : 1;
53     
54     GtkWidget *family;
55     GtkWidget *style;
56     GtkWidget *size;
58     GtkWidget *family_treeview;
59     GtkWidget *style_treeview;
60     
61     NRNameList families;
62     NRStyleList styles;
63     int familyidx;
64     int styleidx;
65     gfloat fontsize;
66     bool fontsize_dirty;
67     font_instance *font;
68 };
71 struct SPFontSelectorClass
72 {
73     GtkHBoxClass parent_class;
74         
75     void (* font_set) (SPFontSelector *fsel, font_instance *font);
76 };
78 enum {
79     FONT_SET,
80     LAST_SIGNAL
81 };
83 static void sp_font_selector_class_init         (SPFontSelectorClass    *c);
84 static void sp_font_selector_init               (SPFontSelector         *fsel);
85 static void sp_font_selector_destroy            (GtkObject              *object);
87 static void sp_font_selector_family_select_row  (GtkTreeSelection       *selection,
88                                                  SPFontSelector         *fsel);
90 static void sp_font_selector_style_select_row   (GtkTreeSelection       *selection,
91                                                  SPFontSelector         *fsel);
92  
93 static void sp_font_selector_size_changed       (GtkComboBox            *combobox,
94                                                  SPFontSelector         *fsel);
96 static void sp_font_selector_emit_set           (SPFontSelector         *fsel);
98 namespace {
99     const char *sizes[] = {
100         "4", "6", "8", "9", "10", "11", "12", "13", "14",
101         "16", "18", "20", "22", "24", "28",
102         "32", "36", "40", "48", "56", "64", "72", "144",
103         NULL
104     };
107 static GtkHBoxClass *fs_parent_class = NULL;
108 static guint fs_signals[LAST_SIGNAL] = { 0 };
110 GtkType sp_font_selector_get_type()
112     static GtkType type = 0;
113     if (!type) {
114         static const GtkTypeInfo info = {
115             "SPFontSelector",
116             sizeof(SPFontSelector),
117             sizeof(SPFontSelectorClass),
118             (GtkClassInitFunc) sp_font_selector_class_init,
119             (GtkObjectInitFunc) sp_font_selector_init,
120             NULL, NULL, NULL
121         };
122         type = gtk_type_unique(GTK_TYPE_HBOX, &info);
123     }
124     return type;
127 static void sp_font_selector_class_init(SPFontSelectorClass *c)
129     GtkObjectClass *object_class = (GtkObjectClass *) c;
130   
131     fs_parent_class = (GtkHBoxClass* )gtk_type_class(GTK_TYPE_HBOX);
132         
133     fs_signals[FONT_SET] = gtk_signal_new ("font_set",
134                                            GTK_RUN_FIRST,
135                                            GTK_CLASS_TYPE(object_class),
136                                            GTK_SIGNAL_OFFSET(SPFontSelectorClass, font_set),
137                                            gtk_marshal_NONE__POINTER,
138                                            GTK_TYPE_NONE,
139                                            1, GTK_TYPE_POINTER);
140         
141         object_class->destroy = sp_font_selector_destroy;
144 static void sp_font_selector_init(SPFontSelector *fsel)
146         gtk_box_set_homogeneous(GTK_BOX(fsel), TRUE);
147         gtk_box_set_spacing(GTK_BOX(fsel), 4);
148         
149         /* Family frame */
150         GtkWidget *f = gtk_frame_new(_("Font family"));
151         gtk_widget_show (f);
152         gtk_box_pack_start (GTK_BOX(fsel), f, TRUE, TRUE, 0);
153         
154         GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
155         gtk_widget_show(sw);
156         gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
157         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
158         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
159         gtk_container_add(GTK_CONTAINER(f), sw);
161         Inkscape::FontLister* fontlister = Inkscape::FontLister::get_instance();
163         fsel->family_treeview = gtk_tree_view_new ();
164         GtkTreeViewColumn *column = gtk_tree_view_column_new ();
165         GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
166         gtk_tree_view_column_pack_start (column, cell, FALSE);
167         gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
168         gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->family_treeview), column);
169         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->family_treeview), FALSE);
170         Glib::RefPtr<Gtk::ListStore> store = fontlister->get_font_list();
171         gtk_tree_view_set_model (GTK_TREE_VIEW(fsel->family_treeview), GTK_TREE_MODEL (Glib::unwrap (store)));
172         gtk_container_add(GTK_CONTAINER(sw), fsel->family_treeview);
173         gtk_widget_show_all (sw);
175         GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->family_treeview));
176         g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_family_select_row), fsel);
177         g_object_set_data (G_OBJECT(fsel), "family-treeview", fsel->family_treeview);
178         
179         
180         /* Style frame */
181         f = gtk_frame_new(_("Style"));
182         gtk_widget_show(f);
183         gtk_box_pack_start(GTK_BOX (fsel), f, TRUE, TRUE, 0);
184         
185         GtkWidget *vb = gtk_vbox_new(FALSE, 4);
186         gtk_widget_show(vb);
187         gtk_container_set_border_width(GTK_CONTAINER (vb), 4);
188         gtk_container_add(GTK_CONTAINER(f), vb);
189         
190         sw = gtk_scrolled_window_new(NULL, NULL);
191         gtk_widget_show(sw);
192         gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
193         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
194         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
195         gtk_box_pack_start(GTK_BOX (vb), sw, TRUE, TRUE, 0);
197         fsel->style_treeview = gtk_tree_view_new ();
198         column = gtk_tree_view_column_new ();
199         cell = gtk_cell_renderer_text_new ();
200         gtk_tree_view_column_pack_start (column, cell, FALSE);
201         gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
202         gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->style_treeview), column);
203         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->style_treeview), FALSE);
204         gtk_container_add(GTK_CONTAINER(sw), fsel->style_treeview);
205         gtk_widget_show_all (sw);
207         selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->style_treeview));
208         g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_style_select_row), fsel);
210         GtkWidget *hb = gtk_hbox_new(FALSE, 4);
211         gtk_widget_show(hb);
212         gtk_box_pack_start(GTK_BOX(vb), hb, FALSE, FALSE, 0);
213         
214         fsel->size = gtk_combo_box_entry_new_text ();
215         gtk_widget_set_size_request(fsel->size, 90, -1);
216         g_signal_connect (G_OBJECT(fsel->size), "changed", G_CALLBACK (sp_font_selector_size_changed), fsel);
217         gtk_box_pack_end (GTK_BOX(hb), fsel->size, FALSE, FALSE, 0);
218         
219         GtkWidget *l = gtk_label_new(_("Font size:"));
220         gtk_widget_show_all (l);
221         gtk_box_pack_end(GTK_BOX (hb), l, FALSE, FALSE, 0);
222         
223         for (unsigned int n = 0; sizes[n]; ++n)
224             {
225                 gtk_combo_box_append_text (GTK_COMBO_BOX(fsel->size), sizes[n]);
226             }
228         gtk_widget_show_all (fsel->size);
229         
230         fsel->familyidx = 0;
231         fsel->styleidx = 0;
232         fsel->fontsize = 10.0;
233         fsel->fontsize_dirty = false;
234         fsel->font = NULL;
237 static void sp_font_selector_destroy(GtkObject *object)
239     SPFontSelector *fsel = SP_FONT_SELECTOR (object);
240     
241     if (fsel->font) {
242         fsel->font->Unref();
243         fsel->font = NULL;
244     }
245     
246     if (fsel->families.length > 0) {
247         nr_name_list_release(&fsel->families);
248         fsel->families.length = 0;
249     }
250     
251     if (fsel->styles.length > 0) {
252         nr_style_list_release(&fsel->styles);
253         fsel->styles.length = 0;
254     }
255     
256     if (GTK_OBJECT_CLASS(fs_parent_class)->destroy) {
257         GTK_OBJECT_CLASS(fs_parent_class)->destroy(object);
258     }
261 static void sp_font_selector_family_select_row(GtkTreeSelection *selection,
262                                                SPFontSelector *fsel)
264     GtkTreeIter   iter;
265     GtkTreeModel *model;
266     GtkListStore *store;
267     GtkTreePath  *path;
268     GList        *list=0;
270     if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
272     path = gtk_tree_model_get_path (model, &iter);
273     gtk_tree_model_get (model, &iter, 1, &list, -1);
274     fsel->familyidx = gtk_tree_path_get_indices (path)[0];
275     fsel->styleidx = 0;
277     store = gtk_list_store_new (1, G_TYPE_STRING);
279     for ( ; list ; list = list->next ) 
280     {
281         gtk_list_store_append (store, &iter);
282         gtk_list_store_set (store, &iter, 0, (char*)list->data, -1);
283     }
285     gtk_tree_view_set_model (GTK_TREE_VIEW (fsel->style_treeview), GTK_TREE_MODEL (store));
286     path = gtk_tree_path_new ();
287     gtk_tree_path_append_index (path, 0);
288     gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path);
289     gtk_tree_path_free (path);
292 static void sp_font_selector_style_select_row (GtkTreeSelection *selection,
293                                                SPFontSelector   *fsel)
295     GtkTreeModel *model; 
296     GtkTreePath  *path;
297     GtkTreeIter   iter;
299     if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
301     path = gtk_tree_model_get_path (model, &iter);
302     fsel->styleidx = gtk_tree_path_get_indices (path)[0];
304     if (!fsel->block_emit)
305     {
306         sp_font_selector_emit_set (fsel);
307     }
310 static void sp_font_selector_size_changed (GtkComboBox *cbox, SPFontSelector *fsel)
312 #if GTK_CHECK_VERSION(2,6,0)
313     char *sstr = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
314 #else // GTK_CHECK_VERSION(2,6,0)
315     GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (fsel->size));
316     GtkTreeIter iter;
317     char *sstr = NULL;
319     if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (fsel->size), &iter) && model)
320         gtk_tree_model_get (model, &iter, 0, &sstr, -1);
321 #endif // GTK_CHECK_VERSION(2,6,0)
322     gfloat old_size = fsel->fontsize;
323     fsel->fontsize = MAX(atof(sstr), 0.1);
324     if ( fabs(fsel->fontsize-old_size) > 0.001)
325     {
326         fsel->fontsize_dirty = true;
327     }
328         
329     sp_font_selector_emit_set (fsel);
331     free (sstr);
334 static void sp_font_selector_emit_set (SPFontSelector *fsel)
336     font_instance *font;
337    
338     GtkTreeSelection *selection_family;
339     GtkTreeSelection *selection_style;
340     GtkTreeModel     *model_family;
341     GtkTreeModel     *model_style;
342     GtkTreeIter       iter_family;
343     GtkTreeIter       iter_style;
344     char             *family=0, *style=0;
346     //We need to check this here since most GtkTreeModel operations are not atomic
347     //See GtkListStore documenation, Chapter "Atomic Operations" --mderezynski
349     model_family = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->family_treeview));
350     if (!model_family) return;
351     model_style = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->style_treeview));
352     if (!model_style) return;
354     selection_family = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview));
355     selection_style = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview));
357     if (!gtk_tree_selection_get_selected (selection_family, NULL, &iter_family)) return;
358     if (!gtk_tree_selection_get_selected (selection_style, NULL, &iter_style)) return;
360     gtk_tree_model_get (model_family, &iter_family, 0, &family, -1);
361     gtk_tree_model_get (model_style, &iter_style, 0, &style, -1);
363     if ((!family) || (!style)) return;
365     font = (font_factory::Default())->FaceFromDescr (family, style);
366     
367     // FIXME: when a text object uses non-available font, font==NULL and we can't set size
368     // (and the size shown in the widget is invalid). To fix, here we must always get some
369     // default font, exactly the same as sptext uses for on-canvas display, so that
370     // font!=NULL ever.
371     if (font != fsel->font || ( font && fsel->fontsize_dirty ) ) {
372         if ( font ) {
373             font->Ref();
374         }
375         if ( fsel->font ) {
376             fsel->font->Unref();
377         }
378         fsel->font = font;
379         gtk_signal_emit(GTK_OBJECT(fsel), fs_signals[FONT_SET], fsel->font);
380     }
381     fsel->fontsize_dirty = false;
382     if (font) {
383         font->Unref();
384     }
385     font = NULL;
388 GtkWidget *sp_font_selector_new()
390     SPFontSelector *fsel = (SPFontSelector*) gtk_type_new(SP_TYPE_FONT_SELECTOR);
391   
392     return (GtkWidget *) fsel;
395 void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, double size)
397     if (font)
398     {
399             gchar family[256];
400             font->Family (family, 256);
401             
402             Gtk::TreePath path;
404             try {
405                 path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
406             } catch (...) {
407                 return;
408             }
410             fsel->block_emit = TRUE;
411             gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
412             gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
413             fsel->block_emit = FALSE;
415             GList *list = 0;
416             GtkTreeIter iter;
417             GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
418             gtk_tree_model_get_iter (model, &iter, path.gobj());
419             gtk_tree_model_get (model, &iter, 1, &list, -1);
421             gchar descr[256];
422             font->Name(descr, 256);
423             std::string descr_best (family);
424             descr_best += " ";
425             descr_best += ((char*)list->data);
427             PangoFontDescription *descr_ = pango_font_description_from_string(descr);
428             PangoFontDescription *best_ = pango_font_description_from_string(descr_best.c_str());
430             unsigned int i = 0;
431             unsigned int best_i = 0;
432             
433             // try to find best match with style description (i.e. bold, italic ?)                         
434             for (list = list->next ; list ; list = list->next)
435             {
436                 i++;
437                 std::string descr_try (family);
438                 descr_try += " ";
439                 descr_try += ((char*)list->data);
440                 PangoFontDescription *try_ = pango_font_description_from_string(descr_try.c_str());
441                 if (pango_font_description_better_match (descr_, best_, try_))
442                 {
443                     pango_font_description_free (best_);
444                     best_ = pango_font_description_from_string (descr_try.c_str ());
445                     best_i = i;
446                 }
447                 pango_font_description_free(try_);
448             }
449             pango_font_description_free(descr_);
450             pango_font_description_free(best_);
452             GtkTreePath *path_c = gtk_tree_path_new ();
453             gtk_tree_path_append_index (path_c, best_i);
454             gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
455             gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
456         
457             if (size != fsel->fontsize)
458             {
459                 gchar s[8];
460                 g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
461                 gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
462                 fsel->fontsize = size;
463             }
464     }
467 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
469     if (fsel->font) {
470         fsel->font->Ref();
471     }
472     
473     return fsel->font;
476 double sp_font_selector_get_size(SPFontSelector *fsel)
478     return fsel->fontsize;
481 /* SPFontPreview */
483 struct SPFontPreview
485     GtkDrawingArea darea;
486     
487     font_instance *font;
488     raster_font *rfont;
489     gchar *phrase;
490     unsigned long rgba;
491 };
493 struct SPFontPreviewClass
495     GtkDrawingAreaClass parent_class;
496 };
498 static void sp_font_preview_class_init(SPFontPreviewClass *c);
499 static void sp_font_preview_init(SPFontPreview *fsel);
500 static void sp_font_preview_destroy(GtkObject *object);
502 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req);
503 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
505 static GtkDrawingAreaClass *fp_parent_class = NULL;
507 GtkType sp_font_preview_get_type()
509     static GtkType type = 0;
510     if (!type) {
511         static const GtkTypeInfo info = {
512             "SPFontPreview",
513             sizeof (SPFontPreview),
514             sizeof (SPFontPreviewClass),
515             (GtkClassInitFunc) sp_font_preview_class_init,
516             (GtkObjectInitFunc) sp_font_preview_init,
517             NULL, NULL, NULL
518         };
519         type = gtk_type_unique (GTK_TYPE_DRAWING_AREA, &info);
520     }
521     return type;
524 static void sp_font_preview_class_init (SPFontPreviewClass *c)
526     GtkObjectClass *object_class = (GtkObjectClass *) c;
527     GtkWidgetClass *widget_class = (GtkWidgetClass *) c;
528     
529     fp_parent_class = (GtkDrawingAreaClass*) gtk_type_class(GTK_TYPE_DRAWING_AREA);
530     
531     object_class->destroy = sp_font_preview_destroy;
532         
533     widget_class->size_request = sp_font_preview_size_request;
534     widget_class->expose_event = sp_font_preview_expose;
537 static void sp_font_preview_init(SPFontPreview *fprev)
539     fprev->rgba = 0x000000ff;
542 static void sp_font_preview_destroy(GtkObject *object)
544     SPFontPreview *fprev = SP_FONT_PREVIEW (object);
545         
546     if (fprev->rfont) {
547         fprev->rfont->Unref();
548         fprev->rfont = NULL;
549     }
550         
551     if (fprev->font) {
552         fprev->font->Unref();
553         fprev->font = NULL;
554     }
555         
556     g_free(fprev->phrase);
557     fprev->phrase = NULL;
558         
559     if (GTK_OBJECT_CLASS (fp_parent_class)->destroy) {
560         GTK_OBJECT_CLASS (fp_parent_class)->destroy(object);
561     }
564 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req)
566     req->width = 256;
567     req->height = 32;
570 #define SPFP_MAX_LEN 64
572 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
574     SPFontPreview *fprev = SP_FONT_PREVIEW(widget);
575         
576     if (GTK_WIDGET_DRAWABLE (widget)) {
577         if (fprev->rfont) {
578             
579             int glyphs[SPFP_MAX_LEN];
580             double hpos[SPFP_MAX_LEN];
581             
582             font_instance *tface = fprev->rfont->daddy;
583             
584             double theSize = NR_MATRIX_DF_EXPANSION (&fprev->rfont->style.transform);
585             
586             gchar const *p;
587             if (fprev->phrase) {
588                 p = fprev->phrase;
589             } else {
590                 /* TRANSLATORS: Test string used in text and font dialog (when no
591                  * text has been entered) to get a preview of the font.  Choose
592                  * some representative characters that users of your locale will be
593                  * interested in. */
594                 p = _("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()");
595             }
596             int len = 0;
597             
598             NRRect bbox;
599             bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
600             
601             text_wrapper* str_text=new text_wrapper;
602             str_text->SetDefaultFont(tface);
603             str_text->AppendUTF8(p,-1);
604             if ( str_text->uni32_length > 0 ) {
605                 str_text->DoLayout();
606                 if ( str_text->glyph_length > 0 ) {
607                     PangoFont *curPF = NULL;
608                     font_instance *curF = NULL;
609                     for (int i = 0; i < str_text->glyph_length && i < SPFP_MAX_LEN; i++) {
610                         if ( str_text->glyph_text[i].font != curPF ) {
611                             curPF = str_text->glyph_text[i].font;
612                             if (curF) {
613                                 curF->Unref();
614                             }
615                             curF = NULL;
616                             if ( curPF ) {
617                                 PangoFontDescription* pfd = pango_font_describe(curPF);
618                                 curF = (font_factory::Default())->Face(pfd);
619                                 pango_font_description_free(pfd);
620                             }
621                         }
622                         NR::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
623                         base_pt *= theSize;
624                                                 
625                         glyphs[len] = str_text->glyph_text[i].gl;
626                         hpos[len] = base_pt[0];
627                         len++;
628                         if ( curF ) {
629                             NR::Maybe<NR::Rect> nbbox = curF->BBox(str_text->glyph_text[i].gl);
630                             if (nbbox) {
631                                 bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox->min())[0]);
632                                 bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox->max())[1]);
633                                 bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox->max())[0]);
634                                 bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox->min())[1]);
635                             }
636                         }
637                     }
638                     if ( curF ) {
639                         curF->Unref();
640                     }
641                 }
642             }
643             
644             // XXX: FIXME: why does this code ignore adv.y
645             /*                  while (p && *p && (len < SPFP_MAX_LEN)) {
646                                 unsigned int unival;
647                                 NRRect gbox;
648                                 unival = g_utf8_get_char (p);
649                                 glyphs[len] =  tface->MapUnicodeChar( unival);
650                                 hpos[len] = (int)px;
651                                 NR::Point adv = fprev->rfont->Advance(glyphs[len]);
652                                 fprev->rfont->BBox( glyphs[len], &gbox);
653                                 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
654                                 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
655                                 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
656                                 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
657                                 px += adv[NR::X];
658                                 len += 1;
659                                 p = g_utf8_next_char (p);
660                                 }*/
661             
662             float startx = (widget->allocation.width - (bbox.x1 - bbox.x0)) / 2;
663             float starty = widget->allocation.height - (widget->allocation.height - (bbox.y1 - bbox.y0)) / 2 - bbox.y1;
664                         
665             for (int y = event->area.y; y < event->area.y + event->area.height; y += 64) {
666                 for (int x = event->area.x; x < event->area.x + event->area.width; x += 64) {
667                     NRPixBlock pb, m;
668                     int x0 = x;
669                     int y0 = y;
670                     int x1 = MIN(x0 + 64, event->area.x + event->area.width);
671                     int y1 = MIN(y0 + 64, event->area.y + event->area.height);
672                     guchar *ps = nr_pixelstore_16K_new (TRUE, 0xff);
673                     nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, ps, 3 * (x1 - x0), FALSE, FALSE);
674                     nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, x0, y0, x1, y1, TRUE);
675                     pb.empty = FALSE;
676                     
677                     PangoFont *curPF = NULL;
678                     font_instance *curF = NULL;
679                     raster_font *curRF = NULL;
680                     for (int i=0; i < len; i++) {
681                         if ( str_text->glyph_text[i].font != curPF ) {
682                             curPF=str_text->glyph_text[i].font;
683                             if ( curF ) {
684                                 curF->Unref();
685                             }
686                             curF = NULL;
687                             if ( curPF ) {
688                                 PangoFontDescription* pfd = pango_font_describe(curPF);
689                                 curF=(font_factory::Default())->Face(pfd);
690                                 pango_font_description_free(pfd);
691                             }
692                             if ( curF ) {
693                                 if ( curRF ) {
694                                     curRF->Unref();
695                                 }
696                                 curRF = NULL;
697                                 curRF = curF->RasterFont(fprev->rfont->style);
698                             }
699                         }
700                         raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
701                         if ( g ) {
702                             g->Blit(NR::Point(hpos[i] + startx, starty), m);
703                         }
704                     }
705                     if (curRF) {
706                         curRF->Unref();
707                     }
708                     if (curF) {
709                         curF->Unref();
710                     }
711                     
712                     nr_blit_pixblock_mask_rgba32(&pb, &m, fprev->rgba);
713                     gdk_draw_rgb_image(widget->window, widget->style->black_gc,
714                                        x0, y0, x1 - x0, y1 - y0,
715                                        GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs);
716                     nr_pixblock_release(&m);
717                     nr_pixblock_release(&pb);
718                     nr_pixelstore_16K_free(ps);
719                 }
720             }
721             
722             delete str_text;
723             
724         } else {
725             nr_gdk_draw_gray_garbage(widget->window, widget->style->black_gc,
726                                      event->area.x, event->area.y,
727                                      event->area.width, event->area.height);
728         }
729     }
730     
731     return TRUE;
734 GtkWidget * sp_font_preview_new()
736     GtkWidget *w = (GtkWidget*) gtk_type_new(SP_TYPE_FONT_PREVIEW);
737     
738     return w;
741 void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontSelector *fsel)
743         if (font)
744         {
745             font->Ref();
746         }
748         if (fprev->font)
749         {
750             fprev->font->Unref();
751         }
753         fprev->font = font;
754         
755         if (fprev->rfont)
756         {
757             fprev->rfont->Unref();
758             fprev->rfont=NULL;
759         }
761         if (fprev->font)
762         {
763             NRMatrix flip;
764             nr_matrix_set_scale (&flip, fsel->fontsize, -fsel->fontsize);
765             fprev->rfont = fprev->font->RasterFont(flip, 0);
766         }
768         if (GTK_WIDGET_DRAWABLE (fprev)) gtk_widget_queue_draw (GTK_WIDGET (fprev));
771 void sp_font_preview_set_rgba32(SPFontPreview *fprev, guint32 rgba)
773     fprev->rgba = rgba;
774     if (GTK_WIDGET_DRAWABLE (fprev)) {
775         gtk_widget_queue_draw (GTK_WIDGET (fprev));
776     }
779 void sp_font_preview_set_phrase(SPFontPreview *fprev, const gchar *phrase)
781     g_free (fprev->phrase);
782     if (phrase) {
783         fprev->phrase = g_strdup (phrase);
784     } else {
785         fprev->phrase = NULL;
786     }
787     if (GTK_WIDGET_DRAWABLE(fprev)) {
788         gtk_widget_queue_draw (GTK_WIDGET (fprev));
789     }
793 /*
794   Local Variables:
795   mode:c++
796   c-file-style:"stroustrup"
797   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
798   indent-tabs-mode:nil
799   fill-column:99
800   End:
801 */
802 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :