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;
50 unsigned int block_emit : 1;
52 GtkWidget *family;
53 GtkWidget *style;
54 GtkWidget *size;
56 GtkWidget *family_treeview;
57 GtkWidget *style_treeview;
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;
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);
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 };
103 }
105 static GtkHBoxClass *fs_parent_class = NULL;
106 static guint fs_signals[LAST_SIGNAL] = { 0 };
108 GtkType sp_font_selector_get_type()
109 {
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;
123 }
125 static void sp_font_selector_class_init(SPFontSelectorClass *c)
126 {
127 GtkObjectClass *object_class = (GtkObjectClass *) c;
129 fs_parent_class = (GtkHBoxClass* )gtk_type_class(GTK_TYPE_HBOX);
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);
139 object_class->destroy = sp_font_selector_destroy;
140 }
142 static void sp_font_selector_init(SPFontSelector *fsel)
143 {
144 gtk_box_set_homogeneous(GTK_BOX(fsel), TRUE);
145 gtk_box_set_spacing(GTK_BOX(fsel), 4);
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);
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);
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);
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);
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);
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);
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);
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);
228 fsel->familyidx = 0;
229 fsel->styleidx = 0;
230 fsel->fontsize = 10.0;
231 fsel->fontsize_dirty = false;
232 fsel->font = NULL;
233 }
235 static void sp_font_selector_destroy(GtkObject *object)
236 {
237 SPFontSelector *fsel = SP_FONT_SELECTOR (object);
239 if (fsel->font) {
240 fsel->font->Unref();
241 fsel->font = NULL;
242 }
244 if (fsel->families.length > 0) {
245 nr_name_list_release(&fsel->families);
246 fsel->families.length = 0;
247 }
249 if (fsel->styles.length > 0) {
250 nr_style_list_release(&fsel->styles);
251 fsel->styles.length = 0;
252 }
254 if (GTK_OBJECT_CLASS(fs_parent_class)->destroy) {
255 GTK_OBJECT_CLASS(fs_parent_class)->destroy(object);
256 }
257 }
259 static void sp_font_selector_family_select_row(GtkTreeSelection *selection,
260 SPFontSelector *fsel)
261 {
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);
288 }
290 static void sp_font_selector_style_select_row (GtkTreeSelection *selection,
291 SPFontSelector *fsel)
292 {
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 }
306 }
308 static void sp_font_selector_size_changed (GtkComboBox *cbox, SPFontSelector *fsel)
309 {
310 #if GTK_CHECK_VERSION(2,6,0)
311 char *sstr = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
312 #else // GTK_CHECK_VERSION(2,6,0)
313 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (fsel->size));
314 GtkTreeIter iter;
315 char *sstr = NULL;
317 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (fsel->size), &iter) && model)
318 gtk_tree_model_get (model, &iter, 0, &sstr, -1);
319 #endif // GTK_CHECK_VERSION(2,6,0)
320 gfloat old_size = fsel->fontsize;
321 fsel->fontsize = MAX(atof(sstr), 0.1);
322 if ( fabs(fsel->fontsize-old_size) > 0.001)
323 {
324 fsel->fontsize_dirty = true;
325 }
327 sp_font_selector_emit_set (fsel);
329 free (sstr);
330 }
332 static void sp_font_selector_emit_set (SPFontSelector *fsel)
333 {
334 font_instance *font;
336 GtkTreeSelection *selection_family;
337 GtkTreeSelection *selection_style;
338 GtkTreeModel *model_family;
339 GtkTreeModel *model_style;
340 GtkTreeIter iter_family;
341 GtkTreeIter iter_style;
342 char *family=0, *style=0;
344 //We need to check this here since most GtkTreeModel operations are not atomic
345 //See GtkListStore documenation, Chapter "Atomic Operations" --mderezynski
347 model_family = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->family_treeview));
348 if (!model_family) return;
349 model_style = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->style_treeview));
350 if (!model_style) return;
352 selection_family = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview));
353 selection_style = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview));
355 if (!gtk_tree_selection_get_selected (selection_family, NULL, &iter_family)) return;
356 if (!gtk_tree_selection_get_selected (selection_style, NULL, &iter_style)) return;
358 gtk_tree_model_get (model_family, &iter_family, 0, &family, -1);
359 gtk_tree_model_get (model_style, &iter_style, 0, &style, -1);
361 if ((!family) || (!style)) return;
363 font = (font_factory::Default())->FaceFromDescr (family, style);
365 // FIXME: when a text object uses non-available font, font==NULL and we can't set size
366 // (and the size shown in the widget is invalid). To fix, here we must always get some
367 // default font, exactly the same as sptext uses for on-canvas display, so that
368 // font!=NULL ever.
369 if (font != fsel->font || ( font && fsel->fontsize_dirty ) ) {
370 if ( font ) {
371 font->Ref();
372 }
373 if ( fsel->font ) {
374 fsel->font->Unref();
375 }
376 fsel->font = font;
377 gtk_signal_emit(GTK_OBJECT(fsel), fs_signals[FONT_SET], fsel->font);
378 }
379 fsel->fontsize_dirty = false;
380 if (font) {
381 font->Unref();
382 }
383 font = NULL;
384 }
386 GtkWidget *sp_font_selector_new()
387 {
388 SPFontSelector *fsel = (SPFontSelector*) gtk_type_new(SP_TYPE_FONT_SELECTOR);
390 return (GtkWidget *) fsel;
391 }
393 void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, double size)
394 {
395 if (font && (fsel->font != font || size != fsel->fontsize))
396 {
397 gchar family[256];
398 font->Family (family, 256);
400 Gtk::TreePath path;
402 try {
403 path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
404 } catch (...) {
405 return;
406 }
408 fsel->block_emit = TRUE;
409 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
410 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
411 fsel->block_emit = FALSE;
413 unsigned int i = path[0];
415 gchar descr[256];
416 font->Name(descr, 256);
418 GList *list = 0;
419 GtkTreeIter iter;
420 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
421 gtk_tree_model_get_iter (model, &iter, path.gobj());
422 gtk_tree_model_get (model, &iter, 1, &list, -1);
424 std::string descr_best (descr);
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 best_i = 0;
432 for ( ; list ; list = list->next)
433 {
434 std::string descr_try (descr);
435 descr_try += ((char*)list->data);
436 PangoFontDescription *try_ = pango_font_description_from_string(descr_try.c_str());
437 if (pango_font_description_better_match (descr_, best_, try_))
438 {
439 pango_font_description_free (best_);
440 best_ = pango_font_description_from_string (descr_try.c_str ());
441 best_i = i;
442 }
443 pango_font_description_free(try_);
444 ++i;
445 }
447 GtkTreePath *path_c = gtk_tree_path_new ();
448 gtk_tree_path_append_index (path_c, best_i);
449 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
450 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
452 if (size != fsel->fontsize)
453 {
454 gchar s[8];
455 g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
456 gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
457 fsel->fontsize = size;
458 }
459 }
460 }
462 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
463 {
464 if (fsel->font) {
465 fsel->font->Ref();
466 }
468 return fsel->font;
469 }
471 double sp_font_selector_get_size(SPFontSelector *fsel)
472 {
473 return fsel->fontsize;
474 }
476 /* SPFontPreview */
478 struct SPFontPreview
479 {
480 GtkDrawingArea darea;
482 font_instance *font;
483 raster_font *rfont;
484 gchar *phrase;
485 unsigned long rgba;
486 };
488 struct SPFontPreviewClass
489 {
490 GtkDrawingAreaClass parent_class;
491 };
493 static void sp_font_preview_class_init(SPFontPreviewClass *c);
494 static void sp_font_preview_init(SPFontPreview *fsel);
495 static void sp_font_preview_destroy(GtkObject *object);
497 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req);
498 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
500 static GtkDrawingAreaClass *fp_parent_class = NULL;
502 GtkType sp_font_preview_get_type()
503 {
504 static GtkType type = 0;
505 if (!type) {
506 static const GtkTypeInfo info = {
507 "SPFontPreview",
508 sizeof (SPFontPreview),
509 sizeof (SPFontPreviewClass),
510 (GtkClassInitFunc) sp_font_preview_class_init,
511 (GtkObjectInitFunc) sp_font_preview_init,
512 NULL, NULL, NULL
513 };
514 type = gtk_type_unique (GTK_TYPE_DRAWING_AREA, &info);
515 }
516 return type;
517 }
519 static void sp_font_preview_class_init (SPFontPreviewClass *c)
520 {
521 GtkObjectClass *object_class = (GtkObjectClass *) c;
522 GtkWidgetClass *widget_class = (GtkWidgetClass *) c;
524 fp_parent_class = (GtkDrawingAreaClass*) gtk_type_class(GTK_TYPE_DRAWING_AREA);
526 object_class->destroy = sp_font_preview_destroy;
528 widget_class->size_request = sp_font_preview_size_request;
529 widget_class->expose_event = sp_font_preview_expose;
530 }
532 static void sp_font_preview_init(SPFontPreview *fprev)
533 {
534 fprev->rgba = 0x000000ff;
535 }
537 static void sp_font_preview_destroy(GtkObject *object)
538 {
539 SPFontPreview *fprev = SP_FONT_PREVIEW (object);
541 if (fprev->rfont) {
542 fprev->rfont->Unref();
543 fprev->rfont = NULL;
544 }
546 if (fprev->font) {
547 fprev->font->Unref();
548 fprev->font = NULL;
549 }
551 g_free(fprev->phrase);
552 fprev->phrase = NULL;
554 if (GTK_OBJECT_CLASS (fp_parent_class)->destroy) {
555 GTK_OBJECT_CLASS (fp_parent_class)->destroy(object);
556 }
557 }
559 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req)
560 {
561 req->width = 256;
562 req->height = 32;
563 }
565 #define SPFP_MAX_LEN 64
567 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
568 {
569 SPFontPreview *fprev = SP_FONT_PREVIEW(widget);
571 if (GTK_WIDGET_DRAWABLE (widget)) {
572 if (fprev->rfont) {
574 int glyphs[SPFP_MAX_LEN];
575 double hpos[SPFP_MAX_LEN];
577 font_instance *tface = fprev->rfont->daddy;
579 double theSize = NR_MATRIX_DF_EXPANSION (&fprev->rfont->style.transform);
581 gchar const *p;
582 if (fprev->phrase) {
583 p = fprev->phrase;
584 } else {
585 /* TRANSLATORS: Test string used in text and font dialog (when no
586 * text has been entered) to get a preview of the font. Choose
587 * some representative characters that users of your locale will be
588 * interested in. */
589 p = _("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()");
590 }
591 int len = 0;
593 NRRect bbox;
594 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
596 text_wrapper* str_text=new text_wrapper;
597 str_text->SetDefaultFont(tface);
598 str_text->AppendUTF8(p,-1);
599 if ( str_text->uni32_length > 0 ) {
600 str_text->DoLayout();
601 if ( str_text->glyph_length > 0 ) {
602 PangoFont *curPF = NULL;
603 font_instance *curF = NULL;
604 for (int i = 0; i < str_text->glyph_length && i < SPFP_MAX_LEN; i++) {
605 if ( str_text->glyph_text[i].font != curPF ) {
606 curPF = str_text->glyph_text[i].font;
607 if (curF) {
608 curF->Unref();
609 }
610 curF = NULL;
611 if ( curPF ) {
612 PangoFontDescription* pfd = pango_font_describe(curPF);
613 curF = (font_factory::Default())->Face(pfd);
614 pango_font_description_free(pfd);
615 }
616 }
617 NR::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
618 base_pt *= theSize;
620 glyphs[len] = str_text->glyph_text[i].gl;
621 hpos[len] = base_pt[0];
622 len++;
623 if ( curF ) {
624 NR::Rect nbbox = curF->BBox(str_text->glyph_text[i].gl);
625 bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox.min())[0]);
626 bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox.max())[1]);
627 bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox.max())[0]);
628 bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox.min())[1]);
629 }
630 }
631 if ( curF ) {
632 curF->Unref();
633 }
634 }
635 }
637 // XXX: FIXME: why does this code ignore adv.y
638 /* while (p && *p && (len < SPFP_MAX_LEN)) {
639 unsigned int unival;
640 NRRect gbox;
641 unival = g_utf8_get_char (p);
642 glyphs[len] = tface->MapUnicodeChar( unival);
643 hpos[len] = (int)px;
644 NR::Point adv = fprev->rfont->Advance(glyphs[len]);
645 fprev->rfont->BBox( glyphs[len], &gbox);
646 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
647 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
648 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
649 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
650 px += adv[NR::X];
651 len += 1;
652 p = g_utf8_next_char (p);
653 }*/
655 float startx = (widget->allocation.width - (bbox.x1 - bbox.x0)) / 2;
656 float starty = widget->allocation.height - (widget->allocation.height - (bbox.y1 - bbox.y0)) / 2 - bbox.y1;
658 for (int y = event->area.y; y < event->area.y + event->area.height; y += 64) {
659 for (int x = event->area.x; x < event->area.x + event->area.width; x += 64) {
660 NRPixBlock pb, m;
661 int x0 = x;
662 int y0 = y;
663 int x1 = MIN(x0 + 64, event->area.x + event->area.width);
664 int y1 = MIN(y0 + 64, event->area.y + event->area.height);
665 guchar *ps = nr_pixelstore_16K_new (TRUE, 0xff);
666 nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, ps, 3 * (x1 - x0), FALSE, FALSE);
667 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, x0, y0, x1, y1, TRUE);
668 pb.empty = FALSE;
670 PangoFont *curPF = NULL;
671 font_instance *curF = NULL;
672 raster_font *curRF = NULL;
673 for (int i=0; i < len; i++) {
674 if ( str_text->glyph_text[i].font != curPF ) {
675 curPF=str_text->glyph_text[i].font;
676 if ( curF ) {
677 curF->Unref();
678 }
679 curF = NULL;
680 if ( curPF ) {
681 PangoFontDescription* pfd = pango_font_describe(curPF);
682 curF=(font_factory::Default())->Face(pfd);
683 pango_font_description_free(pfd);
684 }
685 if ( curF ) {
686 if ( curRF ) {
687 curRF->Unref();
688 }
689 curRF = NULL;
690 curRF = curF->RasterFont(fprev->rfont->style);
691 }
692 }
693 raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
694 if ( g ) {
695 g->Blit(NR::Point(hpos[i] + startx, starty), m);
696 }
697 }
698 if (curRF) {
699 curRF->Unref();
700 }
701 if (curF) {
702 curF->Unref();
703 }
705 nr_blit_pixblock_mask_rgba32(&pb, &m, fprev->rgba);
706 gdk_draw_rgb_image(widget->window, widget->style->black_gc,
707 x0, y0, x1 - x0, y1 - y0,
708 GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs);
709 nr_pixblock_release(&m);
710 nr_pixblock_release(&pb);
711 nr_pixelstore_16K_free(ps);
712 }
713 }
715 delete str_text;
717 } else {
718 nr_gdk_draw_gray_garbage(widget->window, widget->style->black_gc,
719 event->area.x, event->area.y,
720 event->area.width, event->area.height);
721 }
722 }
724 return TRUE;
725 }
727 GtkWidget * sp_font_preview_new()
728 {
729 GtkWidget *w = (GtkWidget*) gtk_type_new(SP_TYPE_FONT_PREVIEW);
731 return w;
732 }
734 void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontSelector *fsel)
735 {
736 if (font)
737 {
738 font->Ref();
739 }
741 if (fprev->font)
742 {
743 fprev->font->Unref();
744 }
746 fprev->font = font;
748 if (fprev->rfont)
749 {
750 fprev->rfont->Unref();
751 fprev->rfont=NULL;
752 }
754 if (fprev->font)
755 {
756 NRMatrix flip;
757 nr_matrix_set_scale (&flip, fsel->fontsize, -fsel->fontsize);
758 fprev->rfont = fprev->font->RasterFont(flip, 0);
759 }
761 if (GTK_WIDGET_DRAWABLE (fprev)) gtk_widget_queue_draw (GTK_WIDGET (fprev));
762 }
764 void sp_font_preview_set_rgba32(SPFontPreview *fprev, guint32 rgba)
765 {
766 fprev->rgba = rgba;
767 if (GTK_WIDGET_DRAWABLE (fprev)) {
768 gtk_widget_queue_draw (GTK_WIDGET (fprev));
769 }
770 }
772 void sp_font_preview_set_phrase(SPFontPreview *fprev, const gchar *phrase)
773 {
774 g_free (fprev->phrase);
775 if (phrase) {
776 fprev->phrase = g_strdup (phrase);
777 } else {
778 fprev->phrase = NULL;
779 }
780 if (GTK_WIDGET_DRAWABLE(fprev)) {
781 gtk_widget_queue_draw (GTK_WIDGET (fprev));
782 }
783 }
786 /*
787 Local Variables:
788 mode:c++
789 c-file-style:"stroustrup"
790 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
791 indent-tabs-mode:nil
792 fill-column:99
793 End:
794 */
795 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :