Code

init matrix variable, removes compiler warnings
[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     if (font && (fsel->font != font || size != fsel->fontsize))
387     {
388             gchar family[256];
389             font->Family (family, 256);
390             
391             Gtk::TreePath path;
393             try {
394                 path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
395             } catch (...) {
396                 return;
397             }
399             fsel->block_emit = TRUE;
400             gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
401             gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
402             fsel->block_emit = FALSE;
404             unsigned int i = path[0];
406             gchar descr[256];
407             font->Name(descr, 256);
409             GList *list = 0;
410             GtkTreeIter iter;
411             GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
412             gtk_tree_model_get_iter (model, &iter, path.gobj());
413             gtk_tree_model_get (model, &iter, 1, &list, -1);
415             std::string descr_best (descr);
416             descr_best += ((char*)list->data);
418             PangoFontDescription *descr_ = pango_font_description_from_string(descr);
419             PangoFontDescription *best_ = pango_font_description_from_string(descr_best.c_str());
421             unsigned int best_i = 0;
423             for ( ; list ; list = list->next)
424             {
425                 std::string descr_try (descr);
426                 descr_try += ((char*)list->data);
427                 PangoFontDescription *try_ = pango_font_description_from_string(descr_try.c_str());
428                 if (pango_font_description_better_match (descr_, best_, try_))
429                 {
430                     pango_font_description_free (best_);
431                     best_ = pango_font_description_from_string (descr_try.c_str ());
432                     best_i = i;
433                 }
434                 pango_font_description_free(try_);
435                 ++i;
436             }
437  
438             GtkTreePath *path_c = gtk_tree_path_new ();
439             gtk_tree_path_append_index (path_c, best_i);
440             gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
441             gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
442         
443             if (size != fsel->fontsize)
444             {
445                 gchar s[8];
446                 g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
447                 gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
448                 fsel->fontsize = size;
449             }
450     }
453 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
455     if (fsel->font) {
456         fsel->font->Ref();
457     }
458     
459     return fsel->font;
462 double sp_font_selector_get_size(SPFontSelector *fsel)
464     return fsel->fontsize;
467 /* SPFontPreview */
469 struct SPFontPreview
471     GtkDrawingArea darea;
472     
473     font_instance *font;
474     raster_font *rfont;
475     gchar *phrase;
476     unsigned long rgba;
477 };
479 struct SPFontPreviewClass
481     GtkDrawingAreaClass parent_class;
482 };
484 static void sp_font_preview_class_init(SPFontPreviewClass *c);
485 static void sp_font_preview_init(SPFontPreview *fsel);
486 static void sp_font_preview_destroy(GtkObject *object);
488 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req);
489 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
491 static GtkDrawingAreaClass *fp_parent_class = NULL;
493 GtkType sp_font_preview_get_type()
495     static GtkType type = 0;
496     if (!type) {
497         static const GtkTypeInfo info = {
498             "SPFontPreview",
499             sizeof (SPFontPreview),
500             sizeof (SPFontPreviewClass),
501             (GtkClassInitFunc) sp_font_preview_class_init,
502             (GtkObjectInitFunc) sp_font_preview_init,
503             NULL, NULL, NULL
504         };
505         type = gtk_type_unique (GTK_TYPE_DRAWING_AREA, &info);
506     }
507     return type;
510 static void sp_font_preview_class_init (SPFontPreviewClass *c)
512     GtkObjectClass *object_class = (GtkObjectClass *) c;
513     GtkWidgetClass *widget_class = (GtkWidgetClass *) c;
514     
515     fp_parent_class = (GtkDrawingAreaClass*) gtk_type_class(GTK_TYPE_DRAWING_AREA);
516     
517     object_class->destroy = sp_font_preview_destroy;
518         
519     widget_class->size_request = sp_font_preview_size_request;
520     widget_class->expose_event = sp_font_preview_expose;
523 static void sp_font_preview_init(SPFontPreview *fprev)
525     fprev->rgba = 0x000000ff;
528 static void sp_font_preview_destroy(GtkObject *object)
530     SPFontPreview *fprev = SP_FONT_PREVIEW (object);
531         
532     if (fprev->rfont) {
533         fprev->rfont->Unref();
534         fprev->rfont = NULL;
535     }
536         
537     if (fprev->font) {
538         fprev->font->Unref();
539         fprev->font = NULL;
540     }
541         
542     g_free(fprev->phrase);
543     fprev->phrase = NULL;
544         
545     if (GTK_OBJECT_CLASS (fp_parent_class)->destroy) {
546         GTK_OBJECT_CLASS (fp_parent_class)->destroy(object);
547     }
550 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req)
552     req->width = 256;
553     req->height = 32;
556 #define SPFP_MAX_LEN 64
558 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
560     SPFontPreview *fprev = SP_FONT_PREVIEW(widget);
561         
562     if (GTK_WIDGET_DRAWABLE (widget)) {
563         if (fprev->rfont) {
564             
565             int glyphs[SPFP_MAX_LEN];
566             double hpos[SPFP_MAX_LEN];
567             
568             font_instance *tface = fprev->rfont->daddy;
569             
570             double theSize = NR_MATRIX_DF_EXPANSION (&fprev->rfont->style.transform);
571             
572             gchar const *p;
573             if (fprev->phrase) {
574                 p = fprev->phrase;
575             } else {
576                 /* TRANSLATORS: Test string used in text and font dialog (when no
577                  * text has been entered) to get a preview of the font.  Choose
578                  * some representative characters that users of your locale will be
579                  * interested in. */
580                 p = _("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()");
581             }
582             int len = 0;
583             
584             NRRect bbox;
585             bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
586             
587             text_wrapper* str_text=new text_wrapper;
588             str_text->SetDefaultFont(tface);
589             str_text->AppendUTF8(p,-1);
590             if ( str_text->uni32_length > 0 ) {
591                 str_text->DoLayout();
592                 if ( str_text->glyph_length > 0 ) {
593                     PangoFont *curPF = NULL;
594                     font_instance *curF = NULL;
595                     for (int i = 0; i < str_text->glyph_length && i < SPFP_MAX_LEN; i++) {
596                         if ( str_text->glyph_text[i].font != curPF ) {
597                             curPF = str_text->glyph_text[i].font;
598                             if (curF) {
599                                 curF->Unref();
600                             }
601                             curF = NULL;
602                             if ( curPF ) {
603                                 PangoFontDescription* pfd = pango_font_describe(curPF);
604                                 curF = (font_factory::Default())->Face(pfd);
605                                 pango_font_description_free(pfd);
606                             }
607                         }
608                         NR::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
609                         base_pt *= theSize;
610                                                 
611                         glyphs[len] = str_text->glyph_text[i].gl;
612                         hpos[len] = base_pt[0];
613                         len++;
614                         if ( curF ) {
615                             NR::Rect nbbox = curF->BBox(str_text->glyph_text[i].gl);
616                             bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox.min())[0]);
617                             bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox.max())[1]);
618                             bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox.max())[0]);
619                             bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox.min())[1]);
620                         }
621                     }
622                     if ( curF ) {
623                         curF->Unref();
624                     }
625                 }
626             }
627             
628             // XXX: FIXME: why does this code ignore adv.y
629             /*                  while (p && *p && (len < SPFP_MAX_LEN)) {
630                                 unsigned int unival;
631                                 NRRect gbox;
632                                 unival = g_utf8_get_char (p);
633                                 glyphs[len] =  tface->MapUnicodeChar( unival);
634                                 hpos[len] = (int)px;
635                                 NR::Point adv = fprev->rfont->Advance(glyphs[len]);
636                                 fprev->rfont->BBox( glyphs[len], &gbox);
637                                 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
638                                 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
639                                 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
640                                 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
641                                 px += adv[NR::X];
642                                 len += 1;
643                                 p = g_utf8_next_char (p);
644                                 }*/
645             
646             float startx = (widget->allocation.width - (bbox.x1 - bbox.x0)) / 2;
647             float starty = widget->allocation.height - (widget->allocation.height - (bbox.y1 - bbox.y0)) / 2 - bbox.y1;
648                         
649             for (int y = event->area.y; y < event->area.y + event->area.height; y += 64) {
650                 for (int x = event->area.x; x < event->area.x + event->area.width; x += 64) {
651                     NRPixBlock pb, m;
652                     int x0 = x;
653                     int y0 = y;
654                     int x1 = MIN(x0 + 64, event->area.x + event->area.width);
655                     int y1 = MIN(y0 + 64, event->area.y + event->area.height);
656                     guchar *ps = nr_pixelstore_16K_new (TRUE, 0xff);
657                     nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, ps, 3 * (x1 - x0), FALSE, FALSE);
658                     nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, x0, y0, x1, y1, TRUE);
659                     pb.empty = FALSE;
660                     
661                     PangoFont *curPF = NULL;
662                     font_instance *curF = NULL;
663                     raster_font *curRF = NULL;
664                     for (int i=0; i < len; i++) {
665                         if ( str_text->glyph_text[i].font != curPF ) {
666                             curPF=str_text->glyph_text[i].font;
667                             if ( curF ) {
668                                 curF->Unref();
669                             }
670                             curF = NULL;
671                             if ( curPF ) {
672                                 PangoFontDescription* pfd = pango_font_describe(curPF);
673                                 curF=(font_factory::Default())->Face(pfd);
674                                 pango_font_description_free(pfd);
675                             }
676                             if ( curF ) {
677                                 if ( curRF ) {
678                                     curRF->Unref();
679                                 }
680                                 curRF = NULL;
681                                 curRF = curF->RasterFont(fprev->rfont->style);
682                             }
683                         }
684                         raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
685                         if ( g ) {
686                             g->Blit(NR::Point(hpos[i] + startx, starty), m);
687                         }
688                     }
689                     if (curRF) {
690                         curRF->Unref();
691                     }
692                     if (curF) {
693                         curF->Unref();
694                     }
695                     
696                     nr_blit_pixblock_mask_rgba32(&pb, &m, fprev->rgba);
697                     gdk_draw_rgb_image(widget->window, widget->style->black_gc,
698                                        x0, y0, x1 - x0, y1 - y0,
699                                        GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs);
700                     nr_pixblock_release(&m);
701                     nr_pixblock_release(&pb);
702                     nr_pixelstore_16K_free(ps);
703                 }
704             }
705             
706             delete str_text;
707             
708         } else {
709             nr_gdk_draw_gray_garbage(widget->window, widget->style->black_gc,
710                                      event->area.x, event->area.y,
711                                      event->area.width, event->area.height);
712         }
713     }
714     
715     return TRUE;
718 GtkWidget * sp_font_preview_new()
720     GtkWidget *w = (GtkWidget*) gtk_type_new(SP_TYPE_FONT_PREVIEW);
721     
722     return w;
725 void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontSelector *fsel)
727         if (font)
728         {
729             font->Ref();
730         }
732         if (fprev->font)
733         {
734             fprev->font->Unref();
735         }
737         fprev->font = font;
738         
739         if (fprev->rfont)
740         {
741             fprev->rfont->Unref();
742             fprev->rfont=NULL;
743         }
745         if (fprev->font)
746         {
747             NRMatrix flip;
748             nr_matrix_set_scale (&flip, fsel->fontsize, -fsel->fontsize);
749             fprev->rfont = fprev->font->RasterFont(flip, 0);
750         }
752         if (GTK_WIDGET_DRAWABLE (fprev)) gtk_widget_queue_draw (GTK_WIDGET (fprev));
755 void sp_font_preview_set_rgba32(SPFontPreview *fprev, guint32 rgba)
757     fprev->rgba = rgba;
758     if (GTK_WIDGET_DRAWABLE (fprev)) {
759         gtk_widget_queue_draw (GTK_WIDGET (fprev));
760     }
763 void sp_font_preview_set_phrase(SPFontPreview *fprev, const gchar *phrase)
765     g_free (fprev->phrase);
766     if (phrase) {
767         fprev->phrase = g_strdup (phrase);
768     } else {
769         fprev->phrase = NULL;
770     }
771     if (GTK_WIDGET_DRAWABLE(fprev)) {
772         gtk_widget_queue_draw (GTK_WIDGET (fprev));
773     }
777 /*
778   Local Variables:
779   mode:c++
780   c-file-style:"stroustrup"
781   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
782   indent-tabs-mode:nil
783   fill-column:99
784   End:
785 */
786 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :