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;
52 unsigned int block_emit : 1;
54 GtkWidget *family;
55 GtkWidget *style;
56 GtkWidget *size;
58 GtkWidget *family_treeview;
59 GtkWidget *style_treeview;
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;
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);
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 };
105 }
107 static GtkHBoxClass *fs_parent_class = NULL;
108 static guint fs_signals[LAST_SIGNAL] = { 0 };
110 GType sp_font_selector_get_type()
111 {
112 static GType type = 0;
113 if (!type) {
114 GTypeInfo info = {
115 sizeof(SPFontSelectorClass),
116 0, // base_init
117 0, // base_finalize
118 (GClassInitFunc)sp_font_selector_class_init,
119 0, // class_finalize
120 0, // class_data
121 sizeof(SPFontSelector),
122 0, // n_preallocs
123 (GInstanceInitFunc)sp_font_selector_init,
124 0 // value_table
125 };
126 type = g_type_register_static(GTK_TYPE_HBOX, "SPFontSelector", &info, static_cast<GTypeFlags>(0));
127 }
128 return type;
129 }
131 static void sp_font_selector_class_init(SPFontSelectorClass *c)
132 {
133 GtkObjectClass *object_class = (GtkObjectClass *) c;
135 fs_parent_class = (GtkHBoxClass* )gtk_type_class(GTK_TYPE_HBOX);
137 fs_signals[FONT_SET] = gtk_signal_new ("font_set",
138 GTK_RUN_FIRST,
139 GTK_CLASS_TYPE(object_class),
140 GTK_SIGNAL_OFFSET(SPFontSelectorClass, font_set),
141 gtk_marshal_NONE__POINTER,
142 GTK_TYPE_NONE,
143 1, GTK_TYPE_POINTER);
145 object_class->destroy = sp_font_selector_destroy;
146 }
148 static void sp_font_selector_init(SPFontSelector *fsel)
149 {
150 gtk_box_set_homogeneous(GTK_BOX(fsel), TRUE);
151 gtk_box_set_spacing(GTK_BOX(fsel), 4);
153 /* Family frame */
154 GtkWidget *f = gtk_frame_new(_("Font family"));
155 gtk_widget_show (f);
156 gtk_box_pack_start (GTK_BOX(fsel), f, TRUE, TRUE, 0);
158 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
159 gtk_widget_show(sw);
160 gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
161 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
162 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
163 gtk_container_add(GTK_CONTAINER(f), sw);
165 Inkscape::FontLister* fontlister = Inkscape::FontLister::get_instance();
167 fsel->family_treeview = gtk_tree_view_new ();
168 GtkTreeViewColumn *column = gtk_tree_view_column_new ();
169 GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
170 gtk_tree_view_column_pack_start (column, cell, FALSE);
171 gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
172 gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->family_treeview), column);
173 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->family_treeview), FALSE);
174 Glib::RefPtr<Gtk::ListStore> store = fontlister->get_font_list();
175 gtk_tree_view_set_model (GTK_TREE_VIEW(fsel->family_treeview), GTK_TREE_MODEL (Glib::unwrap (store)));
176 gtk_container_add(GTK_CONTAINER(sw), fsel->family_treeview);
177 gtk_widget_show_all (sw);
179 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->family_treeview));
180 g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_family_select_row), fsel);
181 g_object_set_data (G_OBJECT(fsel), "family-treeview", fsel->family_treeview);
184 /* Style frame */
185 f = gtk_frame_new(_("Style"));
186 gtk_widget_show(f);
187 gtk_box_pack_start(GTK_BOX (fsel), f, TRUE, TRUE, 0);
189 GtkWidget *vb = gtk_vbox_new(FALSE, 4);
190 gtk_widget_show(vb);
191 gtk_container_set_border_width(GTK_CONTAINER (vb), 4);
192 gtk_container_add(GTK_CONTAINER(f), vb);
194 sw = gtk_scrolled_window_new(NULL, NULL);
195 gtk_widget_show(sw);
196 gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
197 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
198 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
199 gtk_box_pack_start(GTK_BOX (vb), sw, TRUE, TRUE, 0);
201 fsel->style_treeview = gtk_tree_view_new ();
202 column = gtk_tree_view_column_new ();
203 cell = gtk_cell_renderer_text_new ();
204 gtk_tree_view_column_pack_start (column, cell, FALSE);
205 gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
206 gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->style_treeview), column);
207 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->style_treeview), FALSE);
208 gtk_container_add(GTK_CONTAINER(sw), fsel->style_treeview);
209 gtk_widget_show_all (sw);
211 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->style_treeview));
212 g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_style_select_row), fsel);
214 GtkWidget *hb = gtk_hbox_new(FALSE, 4);
215 gtk_widget_show(hb);
216 gtk_box_pack_start(GTK_BOX(vb), hb, FALSE, FALSE, 0);
218 fsel->size = gtk_combo_box_entry_new_text ();
219 gtk_widget_set_size_request(fsel->size, 90, -1);
220 g_signal_connect (G_OBJECT(fsel->size), "changed", G_CALLBACK (sp_font_selector_size_changed), fsel);
221 gtk_box_pack_end (GTK_BOX(hb), fsel->size, FALSE, FALSE, 0);
223 GtkWidget *l = gtk_label_new(_("Font size:"));
224 gtk_widget_show_all (l);
225 gtk_box_pack_end(GTK_BOX (hb), l, FALSE, FALSE, 0);
227 for (unsigned int n = 0; sizes[n]; ++n)
228 {
229 gtk_combo_box_append_text (GTK_COMBO_BOX(fsel->size), sizes[n]);
230 }
232 gtk_widget_show_all (fsel->size);
234 fsel->familyidx = 0;
235 fsel->styleidx = 0;
236 fsel->fontsize = 10.0;
237 fsel->fontsize_dirty = false;
238 fsel->font = NULL;
239 }
241 static void sp_font_selector_destroy(GtkObject *object)
242 {
243 SPFontSelector *fsel = SP_FONT_SELECTOR (object);
245 if (fsel->font) {
246 fsel->font->Unref();
247 fsel->font = NULL;
248 }
250 if (fsel->families.length > 0) {
251 nr_name_list_release(&fsel->families);
252 fsel->families.length = 0;
253 }
255 if (fsel->styles.length > 0) {
256 nr_style_list_release(&fsel->styles);
257 fsel->styles.length = 0;
258 }
260 if (GTK_OBJECT_CLASS(fs_parent_class)->destroy) {
261 GTK_OBJECT_CLASS(fs_parent_class)->destroy(object);
262 }
263 }
265 static void sp_font_selector_family_select_row(GtkTreeSelection *selection,
266 SPFontSelector *fsel)
267 {
268 GtkTreeIter iter;
269 GtkTreeModel *model;
270 GtkListStore *store;
271 GtkTreePath *path;
272 GList *list=0;
274 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
276 path = gtk_tree_model_get_path (model, &iter);
277 gtk_tree_model_get (model, &iter, 1, &list, -1);
278 fsel->familyidx = gtk_tree_path_get_indices (path)[0];
279 fsel->styleidx = 0;
281 store = gtk_list_store_new (1, G_TYPE_STRING);
283 for ( ; list ; list = list->next )
284 {
285 gtk_list_store_append (store, &iter);
286 gtk_list_store_set (store, &iter, 0, (char*)list->data, -1);
287 }
289 gtk_tree_view_set_model (GTK_TREE_VIEW (fsel->style_treeview), GTK_TREE_MODEL (store));
290 path = gtk_tree_path_new ();
291 gtk_tree_path_append_index (path, 0);
292 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path);
293 gtk_tree_path_free (path);
294 }
296 static void sp_font_selector_style_select_row (GtkTreeSelection *selection,
297 SPFontSelector *fsel)
298 {
299 GtkTreeModel *model;
300 GtkTreePath *path;
301 GtkTreeIter iter;
303 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
305 path = gtk_tree_model_get_path (model, &iter);
306 fsel->styleidx = gtk_tree_path_get_indices (path)[0];
308 if (!fsel->block_emit)
309 {
310 sp_font_selector_emit_set (fsel);
311 }
312 }
314 static void sp_font_selector_size_changed( GtkComboBox */*cbox*/, SPFontSelector *fsel )
315 {
316 char *text = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
317 gfloat old_size = fsel->fontsize;
319 gchar *endptr;
320 gdouble value = -1;
321 if (text) {
322 value = g_strtod (text, &endptr);
323 if (endptr == text) // conversion failed, non-numeric input
324 value = -1;
325 free (text);
326 }
327 if (value <= 0) {
328 return; // could not parse value
329 }
330 if (value > 10000)
331 value = 10000; // somewhat arbitrary, but text&font preview freezes with too huge fontsizes
333 fsel->fontsize = value;
334 if ( fabs(fsel->fontsize-old_size) > 0.001)
335 {
336 fsel->fontsize_dirty = true;
337 }
339 sp_font_selector_emit_set (fsel);
340 }
342 static void sp_font_selector_emit_set (SPFontSelector *fsel)
343 {
344 font_instance *font;
346 GtkTreeSelection *selection_family;
347 GtkTreeSelection *selection_style;
348 GtkTreeModel *model_family;
349 GtkTreeModel *model_style;
350 GtkTreeIter iter_family;
351 GtkTreeIter iter_style;
352 char *family=0, *style=0;
354 //We need to check this here since most GtkTreeModel operations are not atomic
355 //See GtkListStore documenation, Chapter "Atomic Operations" --mderezynski
357 model_family = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->family_treeview));
358 if (!model_family) return;
359 model_style = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->style_treeview));
360 if (!model_style) return;
362 selection_family = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview));
363 selection_style = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview));
365 if (!gtk_tree_selection_get_selected (selection_family, NULL, &iter_family)) return;
366 if (!gtk_tree_selection_get_selected (selection_style, NULL, &iter_style)) return;
368 gtk_tree_model_get (model_family, &iter_family, 0, &family, -1);
369 gtk_tree_model_get (model_style, &iter_style, 0, &style, -1);
371 if ((!family) || (!style)) return;
373 font = (font_factory::Default())->FaceFromUIStrings (family, style);
375 // FIXME: when a text object uses non-available font, font==NULL and we can't set size
376 // (and the size shown in the widget is invalid). To fix, here we must always get some
377 // default font, exactly the same as sptext uses for on-canvas display, so that
378 // font!=NULL ever.
379 if (font != fsel->font || ( font && fsel->fontsize_dirty ) ) {
380 if ( font ) {
381 font->Ref();
382 }
383 if ( fsel->font ) {
384 fsel->font->Unref();
385 }
386 fsel->font = font;
387 gtk_signal_emit(GTK_OBJECT(fsel), fs_signals[FONT_SET], fsel->font);
388 }
389 fsel->fontsize_dirty = false;
390 if (font) {
391 font->Unref();
392 }
393 font = NULL;
394 }
396 GtkWidget *sp_font_selector_new()
397 {
398 SPFontSelector *fsel = (SPFontSelector*) gtk_type_new(SP_TYPE_FONT_SELECTOR);
400 return (GtkWidget *) fsel;
401 }
403 void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, double size)
404 {
405 if (font)
406 {
407 Gtk::TreePath path;
408 font_instance *tempFont = NULL;
410 Glib::ustring family = font_factory::Default()->GetUIFamilyString(font->descr);
412 try {
413 path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
414 } catch (...) {
415 return;
416 }
418 fsel->block_emit = TRUE;
419 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
420 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
421 fsel->block_emit = FALSE;
423 GList *list = 0;
424 GtkTreeIter iter;
425 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
426 gtk_tree_model_get_iter (model, &iter, path.gobj());
427 gtk_tree_model_get (model, &iter, 1, &list, -1);
429 unsigned int currentStyleNumber = 0;
430 unsigned int bestStyleNumber = 0;
432 PangoFontDescription *incomingFont = pango_font_description_copy(font->descr);
433 pango_font_description_unset_fields(incomingFont, PANGO_FONT_MASK_SIZE);
435 char *incomingFontString = pango_font_description_to_string(incomingFont);
437 tempFont = (font_factory::Default())->FaceFromUIStrings(family.c_str(), (char*)list->data);
439 PangoFontDescription *bestMatchForFont = NULL;
440 if (tempFont) {
441 bestMatchForFont = pango_font_description_copy(tempFont->descr);
442 tempFont->Unref();
443 tempFont = NULL;
444 }
446 pango_font_description_unset_fields(bestMatchForFont, PANGO_FONT_MASK_SIZE);
448 list = list->next;
450 while (list) {
451 currentStyleNumber++;
453 tempFont = font_factory::Default()->FaceFromUIStrings(family.c_str(), (char*)list->data);
455 PangoFontDescription *currentMatchForFont = NULL;
456 if (tempFont) {
457 currentMatchForFont = pango_font_description_copy(tempFont->descr);
458 tempFont->Unref();
459 tempFont = NULL;
460 }
462 if (currentMatchForFont) {
463 pango_font_description_unset_fields(currentMatchForFont, PANGO_FONT_MASK_SIZE);
465 char *currentMatchString = pango_font_description_to_string(currentMatchForFont);
467 if (!strcmp(incomingFontString, currentMatchString)
468 || pango_font_description_better_match(incomingFont, bestMatchForFont, currentMatchForFont)) {
469 // Found a better match for the font we are looking for
470 pango_font_description_free(bestMatchForFont);
471 bestMatchForFont = pango_font_description_copy(currentMatchForFont);
472 bestStyleNumber = currentStyleNumber;
473 }
475 g_free(currentMatchString);
477 pango_font_description_free(currentMatchForFont);
478 }
480 list = list->next;
481 }
483 if (bestMatchForFont)
484 pango_font_description_free(bestMatchForFont);
485 if (incomingFont)
486 pango_font_description_free(incomingFont);
487 g_free(incomingFontString);
489 GtkTreePath *path_c = gtk_tree_path_new ();
490 gtk_tree_path_append_index (path_c, bestStyleNumber);
491 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
492 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
494 if (size != fsel->fontsize)
495 {
496 gchar s[8];
497 g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
498 gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
499 fsel->fontsize = size;
500 }
501 }
503 }
505 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
506 {
507 if (fsel->font) {
508 fsel->font->Ref();
509 }
511 return fsel->font;
512 }
514 double sp_font_selector_get_size(SPFontSelector *fsel)
515 {
516 return fsel->fontsize;
517 }
519 /* SPFontPreview */
521 struct SPFontPreview
522 {
523 GtkDrawingArea darea;
525 font_instance *font;
526 raster_font *rfont;
527 gchar *phrase;
528 unsigned long rgba;
529 };
531 struct SPFontPreviewClass
532 {
533 GtkDrawingAreaClass parent_class;
534 };
536 static void sp_font_preview_class_init(SPFontPreviewClass *c);
537 static void sp_font_preview_init(SPFontPreview *fsel);
538 static void sp_font_preview_destroy(GtkObject *object);
540 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req);
541 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
543 static GtkDrawingAreaClass *fp_parent_class = NULL;
545 GType sp_font_preview_get_type()
546 {
547 static GType type = 0;
548 if (!type) {
549 GTypeInfo info = {
550 sizeof(SPFontPreviewClass),
551 0, // base_init
552 0, // base_finalize
553 (GClassInitFunc)sp_font_preview_class_init,
554 0, // class_finalize
555 0, // class_data
556 sizeof(SPFontPreview),
557 0, // n_preallocs
558 (GInstanceInitFunc)sp_font_preview_init,
559 0 // value_table
560 };
561 type = g_type_register_static(GTK_TYPE_DRAWING_AREA, "SPFontPreview", &info, static_cast<GTypeFlags>(0));
562 }
563 return type;
564 }
566 static void sp_font_preview_class_init (SPFontPreviewClass *c)
567 {
568 GtkObjectClass *object_class = (GtkObjectClass *) c;
569 GtkWidgetClass *widget_class = (GtkWidgetClass *) c;
571 fp_parent_class = (GtkDrawingAreaClass*) gtk_type_class(GTK_TYPE_DRAWING_AREA);
573 object_class->destroy = sp_font_preview_destroy;
575 widget_class->size_request = sp_font_preview_size_request;
576 widget_class->expose_event = sp_font_preview_expose;
577 }
579 static void sp_font_preview_init(SPFontPreview *fprev)
580 {
581 fprev->rgba = 0x000000ff;
582 }
584 static void sp_font_preview_destroy(GtkObject *object)
585 {
586 SPFontPreview *fprev = SP_FONT_PREVIEW (object);
588 if (fprev->rfont) {
589 fprev->rfont->Unref();
590 fprev->rfont = NULL;
591 }
593 if (fprev->font) {
594 fprev->font->Unref();
595 fprev->font = NULL;
596 }
598 g_free(fprev->phrase);
599 fprev->phrase = NULL;
601 if (GTK_OBJECT_CLASS (fp_parent_class)->destroy) {
602 GTK_OBJECT_CLASS (fp_parent_class)->destroy(object);
603 }
604 }
606 void sp_font_preview_size_request(GtkWidget */*widget*/, GtkRequisition *req)
607 {
608 req->width = 256;
609 req->height = 32;
610 }
612 #define SPFP_MAX_LEN 64
614 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
615 {
616 SPFontPreview *fprev = SP_FONT_PREVIEW(widget);
618 if (GTK_WIDGET_DRAWABLE (widget)) {
619 if (fprev->rfont) {
621 int glyphs[SPFP_MAX_LEN];
622 double hpos[SPFP_MAX_LEN];
624 font_instance *tface = fprev->rfont->daddy;
626 double theSize = NR::expansion(fprev->rfont->style.transform);
628 gchar const *p;
629 if (fprev->phrase) {
630 p = fprev->phrase;
631 } else {
632 /* TRANSLATORS: Test string used in text and font dialog (when no
633 * text has been entered) to get a preview of the font. Choose
634 * some representative characters that users of your locale will be
635 * interested in. */
636 p = _("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()");
637 }
638 int len = 0;
640 NRRect bbox;
641 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
643 text_wrapper* str_text=new text_wrapper;
644 str_text->SetDefaultFont(tface);
645 str_text->AppendUTF8(p,-1);
646 if ( str_text->uni32_length > 0 ) {
647 str_text->DoLayout();
648 if ( str_text->glyph_length > 0 ) {
649 PangoFont *curPF = NULL;
650 font_instance *curF = NULL;
651 for (int i = 0; i < str_text->glyph_length && i < SPFP_MAX_LEN; i++) {
652 if ( str_text->glyph_text[i].font != curPF ) {
653 curPF = str_text->glyph_text[i].font;
654 if (curF) {
655 curF->Unref();
656 }
657 curF = NULL;
658 if ( curPF ) {
659 PangoFontDescription* pfd = pango_font_describe(curPF);
660 curF = (font_factory::Default())->Face(pfd);
661 pango_font_description_free(pfd);
662 }
663 }
664 NR::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
665 base_pt *= theSize;
667 glyphs[len] = str_text->glyph_text[i].gl;
668 hpos[len] = base_pt[0];
669 len++;
670 if ( curF ) {
671 NR::Maybe<NR::Rect> nbbox = curF->BBox(str_text->glyph_text[i].gl);
672 if (nbbox) {
673 bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox->min())[0]);
674 bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox->max())[1]);
675 bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox->max())[0]);
676 bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox->min())[1]);
677 }
678 }
679 }
680 if ( curF ) {
681 curF->Unref();
682 }
683 }
684 }
686 // XXX: FIXME: why does this code ignore adv.y
687 /* while (p && *p && (len < SPFP_MAX_LEN)) {
688 unsigned int unival;
689 NRRect gbox;
690 unival = g_utf8_get_char (p);
691 glyphs[len] = tface->MapUnicodeChar( unival);
692 hpos[len] = (int)px;
693 NR::Point adv = fprev->rfont->Advance(glyphs[len]);
694 fprev->rfont->BBox( glyphs[len], &gbox);
695 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
696 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
697 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
698 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
699 px += adv[NR::X];
700 len += 1;
701 p = g_utf8_next_char (p);
702 }*/
704 float startx = (widget->allocation.width - (bbox.x1 - bbox.x0)) / 2;
705 float starty = widget->allocation.height - (widget->allocation.height - (bbox.y1 - bbox.y0)) / 2 - bbox.y1;
707 for (int y = event->area.y; y < event->area.y + event->area.height; y += 64) {
708 for (int x = event->area.x; x < event->area.x + event->area.width; x += 64) {
709 NRPixBlock pb, m;
710 int x0 = x;
711 int y0 = y;
712 int x1 = MIN(x0 + 64, event->area.x + event->area.width);
713 int y1 = MIN(y0 + 64, event->area.y + event->area.height);
714 guchar *ps = nr_pixelstore_16K_new (TRUE, 0xff);
715 nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, ps, 3 * (x1 - x0), FALSE, FALSE);
716 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, x0, y0, x1, y1, TRUE);
717 pb.empty = FALSE;
719 PangoFont *curPF = NULL;
720 font_instance *curF = NULL;
721 raster_font *curRF = NULL;
722 for (int i=0; i < len; i++) {
723 if ( str_text->glyph_text[i].font != curPF ) {
724 curPF=str_text->glyph_text[i].font;
725 if ( curF ) {
726 curF->Unref();
727 }
728 curF = NULL;
729 if ( curPF ) {
730 PangoFontDescription* pfd = pango_font_describe(curPF);
731 curF=(font_factory::Default())->Face(pfd);
732 pango_font_description_free(pfd);
733 }
734 if ( curF ) {
735 if ( curRF ) {
736 curRF->Unref();
737 }
738 curRF = NULL;
739 curRF = curF->RasterFont(fprev->rfont->style);
740 }
741 }
742 raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
743 if ( g ) {
744 g->Blit(NR::Point(hpos[i] + startx, starty), m);
745 }
746 }
747 if (curRF) {
748 curRF->Unref();
749 }
750 if (curF) {
751 curF->Unref();
752 }
754 nr_blit_pixblock_mask_rgba32(&pb, &m, fprev->rgba);
755 gdk_draw_rgb_image(widget->window, widget->style->black_gc,
756 x0, y0, x1 - x0, y1 - y0,
757 GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs);
758 nr_pixblock_release(&m);
759 nr_pixblock_release(&pb);
760 nr_pixelstore_16K_free(ps);
761 }
762 }
764 delete str_text;
766 } else {
767 nr_gdk_draw_gray_garbage(widget->window, widget->style->black_gc,
768 event->area.x, event->area.y,
769 event->area.width, event->area.height);
770 }
771 }
773 return TRUE;
774 }
776 GtkWidget * sp_font_preview_new()
777 {
778 GtkWidget *w = (GtkWidget*) gtk_type_new(SP_TYPE_FONT_PREVIEW);
780 return w;
781 }
783 void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontSelector *fsel)
784 {
785 if (font)
786 {
787 font->Ref();
788 }
790 if (fprev->font)
791 {
792 fprev->font->Unref();
793 }
795 fprev->font = font;
797 if (fprev->rfont)
798 {
799 fprev->rfont->Unref();
800 fprev->rfont=NULL;
801 }
803 if (fprev->font)
804 {
805 NR::Matrix flip(NR::scale(fsel->fontsize, -fsel->fontsize));
806 fprev->rfont = fprev->font->RasterFont(flip, 0);
807 }
809 if (GTK_WIDGET_DRAWABLE (fprev)) gtk_widget_queue_draw (GTK_WIDGET (fprev));
810 }
812 void sp_font_preview_set_rgba32(SPFontPreview *fprev, guint32 rgba)
813 {
814 fprev->rgba = rgba;
815 if (GTK_WIDGET_DRAWABLE (fprev)) {
816 gtk_widget_queue_draw (GTK_WIDGET (fprev));
817 }
818 }
820 void sp_font_preview_set_phrase(SPFontPreview *fprev, const gchar *phrase)
821 {
822 g_free (fprev->phrase);
823 if (phrase) {
824 fprev->phrase = g_strdup (phrase);
825 } else {
826 fprev->phrase = NULL;
827 }
828 if (GTK_WIDGET_DRAWABLE(fprev)) {
829 gtk_widget_queue_draw (GTK_WIDGET (fprev));
830 }
831 }
834 /*
835 Local Variables:
836 mode:c++
837 c-file-style:"stroustrup"
838 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
839 indent-tabs-mode:nil
840 fill-column:99
841 End:
842 */
843 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :