Code

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