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 <libnr/nr-convert2geom.h>
25 #include <libnrtype/font-instance.h>
26 #include <libnrtype/raster-glyph.h>
27 #include <libnrtype/RasterFont.h>
28 #include <libnrtype/TextWrapper.h>
29 #include <libnrtype/one-glyph.h>
30 #include <libnrtype/font-lister.h>
32 #include <2geom/transforms.h>
34 #include <gtk/gtk.h>
35 #include <gtk/gtkframe.h>
36 #include <gtk/gtkscrolledwindow.h>
37 #include <gtk/gtkclist.h>
38 #include <gtk/gtkvbox.h>
39 #include <gtk/gtkcombo.h>
40 #include <gtk/gtkentry.h>
41 #include <gtk/gtkdrawingarea.h>
43 #include "../display/nr-plain-stuff-gdk.h"
44 #include <glibmm/i18n.h>
46 #include "../desktop.h"
47 #include "font-selector.h"
49 /* SPFontSelector */
51 struct SPFontSelector
52 {
53 GtkHBox hbox;
55 unsigned int block_emit : 1;
57 GtkWidget *family;
58 GtkWidget *style;
59 GtkWidget *size;
61 GtkWidget *family_treeview;
62 GtkWidget *style_treeview;
64 NRNameList families;
65 NRStyleList styles;
66 int familyidx;
67 int styleidx;
68 gfloat fontsize;
69 bool fontsize_dirty;
70 font_instance *font;
71 };
74 struct SPFontSelectorClass
75 {
76 GtkHBoxClass parent_class;
78 void (* font_set) (SPFontSelector *fsel, font_instance *font);
79 };
81 enum {
82 FONT_SET,
83 LAST_SIGNAL
84 };
86 static void sp_font_selector_class_init (SPFontSelectorClass *c);
87 static void sp_font_selector_init (SPFontSelector *fsel);
88 static void sp_font_selector_destroy (GtkObject *object);
90 static void sp_font_selector_family_select_row (GtkTreeSelection *selection,
91 SPFontSelector *fsel);
93 static void sp_font_selector_style_select_row (GtkTreeSelection *selection,
94 SPFontSelector *fsel);
96 static void sp_font_selector_size_changed (GtkComboBox *combobox,
97 SPFontSelector *fsel);
99 static void sp_font_selector_emit_set (SPFontSelector *fsel);
101 namespace {
102 const char *sizes[] = {
103 "4", "6", "8", "9", "10", "11", "12", "13", "14",
104 "16", "18", "20", "22", "24", "28",
105 "32", "36", "40", "48", "56", "64", "72", "144",
106 NULL
107 };
108 }
110 static GtkHBoxClass *fs_parent_class = NULL;
111 static guint fs_signals[LAST_SIGNAL] = { 0 };
113 GType sp_font_selector_get_type()
114 {
115 static GType type = 0;
116 if (!type) {
117 GTypeInfo info = {
118 sizeof(SPFontSelectorClass),
119 0, // base_init
120 0, // base_finalize
121 (GClassInitFunc)sp_font_selector_class_init,
122 0, // class_finalize
123 0, // class_data
124 sizeof(SPFontSelector),
125 0, // n_preallocs
126 (GInstanceInitFunc)sp_font_selector_init,
127 0 // value_table
128 };
129 type = g_type_register_static(GTK_TYPE_HBOX, "SPFontSelector", &info, static_cast<GTypeFlags>(0));
130 }
131 return type;
132 }
134 static void sp_font_selector_class_init(SPFontSelectorClass *c)
135 {
136 GtkObjectClass *object_class = (GtkObjectClass *) c;
138 fs_parent_class = (GtkHBoxClass* )gtk_type_class(GTK_TYPE_HBOX);
140 fs_signals[FONT_SET] = gtk_signal_new ("font_set",
141 GTK_RUN_FIRST,
142 GTK_CLASS_TYPE(object_class),
143 GTK_SIGNAL_OFFSET(SPFontSelectorClass, font_set),
144 gtk_marshal_NONE__POINTER,
145 GTK_TYPE_NONE,
146 1, GTK_TYPE_POINTER);
148 object_class->destroy = sp_font_selector_destroy;
149 }
151 static void sp_font_selector_init(SPFontSelector *fsel)
152 {
153 gtk_box_set_homogeneous(GTK_BOX(fsel), TRUE);
154 gtk_box_set_spacing(GTK_BOX(fsel), 4);
156 /* Family frame */
157 GtkWidget *f = gtk_frame_new(_("Font family"));
158 gtk_widget_show (f);
159 gtk_box_pack_start (GTK_BOX(fsel), f, TRUE, TRUE, 0);
161 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
162 gtk_widget_show(sw);
163 gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
164 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
165 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
166 gtk_container_add(GTK_CONTAINER(f), sw);
168 Inkscape::FontLister* fontlister = Inkscape::FontLister::get_instance();
170 fsel->family_treeview = gtk_tree_view_new ();
171 GtkTreeViewColumn *column = gtk_tree_view_column_new ();
172 GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
173 gtk_tree_view_column_pack_start (column, cell, FALSE);
174 gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
175 gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->family_treeview), column);
176 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->family_treeview), FALSE);
177 Glib::RefPtr<Gtk::ListStore> store = fontlister->get_font_list();
178 gtk_tree_view_set_model (GTK_TREE_VIEW(fsel->family_treeview), GTK_TREE_MODEL (Glib::unwrap (store)));
179 gtk_container_add(GTK_CONTAINER(sw), fsel->family_treeview);
180 gtk_widget_show_all (sw);
182 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->family_treeview));
183 g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_family_select_row), fsel);
184 g_object_set_data (G_OBJECT(fsel), "family-treeview", fsel->family_treeview);
187 //TRANSLATORS: only translate "string" in "context|string".
188 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
189 /* Style frame */
190 f = gtk_frame_new(Q_("fontselector|Style"));
191 gtk_widget_show(f);
192 gtk_box_pack_start(GTK_BOX (fsel), f, TRUE, TRUE, 0);
194 GtkWidget *vb = gtk_vbox_new(FALSE, 4);
195 gtk_widget_show(vb);
196 gtk_container_set_border_width(GTK_CONTAINER (vb), 4);
197 gtk_container_add(GTK_CONTAINER(f), vb);
199 sw = gtk_scrolled_window_new(NULL, NULL);
200 gtk_widget_show(sw);
201 gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
202 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
203 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
204 gtk_box_pack_start(GTK_BOX (vb), sw, TRUE, TRUE, 0);
206 fsel->style_treeview = gtk_tree_view_new ();
207 column = gtk_tree_view_column_new ();
208 cell = gtk_cell_renderer_text_new ();
209 gtk_tree_view_column_pack_start (column, cell, FALSE);
210 gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
211 gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->style_treeview), column);
212 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->style_treeview), FALSE);
213 gtk_container_add(GTK_CONTAINER(sw), fsel->style_treeview);
214 gtk_widget_show_all (sw);
216 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->style_treeview));
217 g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_style_select_row), fsel);
219 GtkWidget *hb = gtk_hbox_new(FALSE, 4);
220 gtk_widget_show(hb);
221 gtk_box_pack_start(GTK_BOX(vb), hb, FALSE, FALSE, 0);
223 fsel->size = gtk_combo_box_entry_new_text ();
224 gtk_widget_set_size_request(fsel->size, 90, -1);
225 g_signal_connect (G_OBJECT(fsel->size), "changed", G_CALLBACK (sp_font_selector_size_changed), fsel);
226 gtk_box_pack_end (GTK_BOX(hb), fsel->size, FALSE, FALSE, 0);
228 GtkWidget *l = gtk_label_new(_("Font size:"));
229 gtk_widget_show_all (l);
230 gtk_box_pack_end(GTK_BOX (hb), l, FALSE, FALSE, 0);
232 for (unsigned int n = 0; sizes[n]; ++n)
233 {
234 gtk_combo_box_append_text (GTK_COMBO_BOX(fsel->size), sizes[n]);
235 }
237 gtk_widget_show_all (fsel->size);
239 fsel->familyidx = 0;
240 fsel->styleidx = 0;
241 fsel->fontsize = 10.0;
242 fsel->fontsize_dirty = false;
243 fsel->font = NULL;
244 }
246 static void sp_font_selector_destroy(GtkObject *object)
247 {
248 SPFontSelector *fsel = SP_FONT_SELECTOR (object);
250 if (fsel->font) {
251 fsel->font->Unref();
252 fsel->font = NULL;
253 }
255 if (fsel->families.length > 0) {
256 nr_name_list_release(&fsel->families);
257 fsel->families.length = 0;
258 }
260 if (fsel->styles.length > 0) {
261 nr_style_list_release(&fsel->styles);
262 fsel->styles.length = 0;
263 }
265 if (GTK_OBJECT_CLASS(fs_parent_class)->destroy) {
266 GTK_OBJECT_CLASS(fs_parent_class)->destroy(object);
267 }
268 }
270 static void sp_font_selector_family_select_row(GtkTreeSelection *selection,
271 SPFontSelector *fsel)
272 {
273 GtkTreeIter iter;
274 GtkTreeModel *model;
275 GtkListStore *store;
276 GtkTreePath *path;
277 GList *list=0;
279 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
281 path = gtk_tree_model_get_path (model, &iter);
282 gtk_tree_model_get (model, &iter, 1, &list, -1);
283 fsel->familyidx = gtk_tree_path_get_indices (path)[0];
284 fsel->styleidx = 0;
286 store = gtk_list_store_new (1, G_TYPE_STRING);
288 for ( ; list ; list = list->next )
289 {
290 gtk_list_store_append (store, &iter);
291 gtk_list_store_set (store, &iter, 0, (char*)list->data, -1);
292 }
294 gtk_tree_view_set_model (GTK_TREE_VIEW (fsel->style_treeview), GTK_TREE_MODEL (store));
295 path = gtk_tree_path_new ();
296 gtk_tree_path_append_index (path, 0);
297 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path);
298 gtk_tree_path_free (path);
299 }
301 static void sp_font_selector_style_select_row (GtkTreeSelection *selection,
302 SPFontSelector *fsel)
303 {
304 GtkTreeModel *model;
305 GtkTreePath *path;
306 GtkTreeIter iter;
308 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
310 path = gtk_tree_model_get_path (model, &iter);
311 fsel->styleidx = gtk_tree_path_get_indices (path)[0];
313 if (!fsel->block_emit)
314 {
315 sp_font_selector_emit_set (fsel);
316 }
317 }
319 static void sp_font_selector_size_changed( GtkComboBox */*cbox*/, SPFontSelector *fsel )
320 {
321 char *text = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
322 gfloat old_size = fsel->fontsize;
324 gchar *endptr;
325 gdouble value = -1;
326 if (text) {
327 value = g_strtod (text, &endptr);
328 if (endptr == text) // conversion failed, non-numeric input
329 value = -1;
330 free (text);
331 }
332 if (value <= 0) {
333 return; // could not parse value
334 }
335 if (value > 10000)
336 value = 10000; // somewhat arbitrary, but text&font preview freezes with too huge fontsizes
338 fsel->fontsize = value;
339 if ( fabs(fsel->fontsize-old_size) > 0.001)
340 {
341 fsel->fontsize_dirty = true;
342 }
344 sp_font_selector_emit_set (fsel);
345 }
347 static void sp_font_selector_emit_set (SPFontSelector *fsel)
348 {
349 font_instance *font;
351 GtkTreeSelection *selection_family;
352 GtkTreeSelection *selection_style;
353 GtkTreeModel *model_family;
354 GtkTreeModel *model_style;
355 GtkTreeIter iter_family;
356 GtkTreeIter iter_style;
357 char *family=0, *style=0;
359 //We need to check this here since most GtkTreeModel operations are not atomic
360 //See GtkListStore documenation, Chapter "Atomic Operations" --mderezynski
362 model_family = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->family_treeview));
363 if (!model_family) return;
364 model_style = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->style_treeview));
365 if (!model_style) return;
367 selection_family = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview));
368 selection_style = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview));
370 if (!gtk_tree_selection_get_selected (selection_family, NULL, &iter_family)) return;
371 if (!gtk_tree_selection_get_selected (selection_style, NULL, &iter_style)) return;
373 gtk_tree_model_get (model_family, &iter_family, 0, &family, -1);
374 gtk_tree_model_get (model_style, &iter_style, 0, &style, -1);
376 if ((!family) || (!style)) return;
378 font = (font_factory::Default())->FaceFromUIStrings (family, style);
380 // FIXME: when a text object uses non-available font, font==NULL and we can't set size
381 // (and the size shown in the widget is invalid). To fix, here we must always get some
382 // default font, exactly the same as sptext uses for on-canvas display, so that
383 // font!=NULL ever.
384 if (font != fsel->font || ( font && fsel->fontsize_dirty ) ) {
385 if ( font ) {
386 font->Ref();
387 }
388 if ( fsel->font ) {
389 fsel->font->Unref();
390 }
391 fsel->font = font;
392 gtk_signal_emit(GTK_OBJECT(fsel), fs_signals[FONT_SET], fsel->font);
393 }
394 fsel->fontsize_dirty = false;
395 if (font) {
396 font->Unref();
397 }
398 font = NULL;
399 }
401 GtkWidget *sp_font_selector_new()
402 {
403 SPFontSelector *fsel = (SPFontSelector*) gtk_type_new(SP_TYPE_FONT_SELECTOR);
405 return (GtkWidget *) fsel;
406 }
408 void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, double size)
409 {
410 if (font)
411 {
412 Gtk::TreePath path;
413 font_instance *tempFont = NULL;
415 Glib::ustring family = font_factory::Default()->GetUIFamilyString(font->descr);
417 try {
418 path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
419 } catch (...) {
420 return;
421 }
423 fsel->block_emit = TRUE;
424 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
425 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
426 fsel->block_emit = FALSE;
428 GList *list = 0;
429 GtkTreeIter iter;
430 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
431 gtk_tree_model_get_iter (model, &iter, path.gobj());
432 gtk_tree_model_get (model, &iter, 1, &list, -1);
434 unsigned int currentStyleNumber = 0;
435 unsigned int bestStyleNumber = 0;
437 PangoFontDescription *incomingFont = pango_font_description_copy(font->descr);
438 pango_font_description_unset_fields(incomingFont, PANGO_FONT_MASK_SIZE);
440 char *incomingFontString = pango_font_description_to_string(incomingFont);
442 tempFont = (font_factory::Default())->FaceFromUIStrings(family.c_str(), (char*)list->data);
444 PangoFontDescription *bestMatchForFont = NULL;
445 if (tempFont) {
446 bestMatchForFont = pango_font_description_copy(tempFont->descr);
447 tempFont->Unref();
448 tempFont = NULL;
449 }
451 pango_font_description_unset_fields(bestMatchForFont, PANGO_FONT_MASK_SIZE);
453 list = list->next;
455 while (list) {
456 currentStyleNumber++;
458 tempFont = font_factory::Default()->FaceFromUIStrings(family.c_str(), (char*)list->data);
460 PangoFontDescription *currentMatchForFont = NULL;
461 if (tempFont) {
462 currentMatchForFont = pango_font_description_copy(tempFont->descr);
463 tempFont->Unref();
464 tempFont = NULL;
465 }
467 if (currentMatchForFont) {
468 pango_font_description_unset_fields(currentMatchForFont, PANGO_FONT_MASK_SIZE);
470 char *currentMatchString = pango_font_description_to_string(currentMatchForFont);
472 if (!strcmp(incomingFontString, currentMatchString)
473 || pango_font_description_better_match(incomingFont, bestMatchForFont, currentMatchForFont)) {
474 // Found a better match for the font we are looking for
475 pango_font_description_free(bestMatchForFont);
476 bestMatchForFont = pango_font_description_copy(currentMatchForFont);
477 bestStyleNumber = currentStyleNumber;
478 }
480 g_free(currentMatchString);
482 pango_font_description_free(currentMatchForFont);
483 }
485 list = list->next;
486 }
488 if (bestMatchForFont)
489 pango_font_description_free(bestMatchForFont);
490 if (incomingFont)
491 pango_font_description_free(incomingFont);
492 g_free(incomingFontString);
494 GtkTreePath *path_c = gtk_tree_path_new ();
495 gtk_tree_path_append_index (path_c, bestStyleNumber);
496 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
497 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
499 if (size != fsel->fontsize)
500 {
501 gchar s[8];
502 g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
503 gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
504 fsel->fontsize = size;
505 }
506 }
508 }
510 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
511 {
512 if (fsel->font) {
513 fsel->font->Ref();
514 }
516 return fsel->font;
517 }
519 double sp_font_selector_get_size(SPFontSelector *fsel)
520 {
521 return fsel->fontsize;
522 }
524 /* SPFontPreview */
526 struct SPFontPreview
527 {
528 GtkDrawingArea darea;
530 font_instance *font;
531 raster_font *rfont;
532 gchar *phrase;
533 unsigned long rgba;
534 };
536 struct SPFontPreviewClass
537 {
538 GtkDrawingAreaClass parent_class;
539 };
541 static void sp_font_preview_class_init(SPFontPreviewClass *c);
542 static void sp_font_preview_init(SPFontPreview *fsel);
543 static void sp_font_preview_destroy(GtkObject *object);
545 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req);
546 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
548 static GtkDrawingAreaClass *fp_parent_class = NULL;
550 GType sp_font_preview_get_type()
551 {
552 static GType type = 0;
553 if (!type) {
554 GTypeInfo info = {
555 sizeof(SPFontPreviewClass),
556 0, // base_init
557 0, // base_finalize
558 (GClassInitFunc)sp_font_preview_class_init,
559 0, // class_finalize
560 0, // class_data
561 sizeof(SPFontPreview),
562 0, // n_preallocs
563 (GInstanceInitFunc)sp_font_preview_init,
564 0 // value_table
565 };
566 type = g_type_register_static(GTK_TYPE_DRAWING_AREA, "SPFontPreview", &info, static_cast<GTypeFlags>(0));
567 }
568 return type;
569 }
571 static void sp_font_preview_class_init (SPFontPreviewClass *c)
572 {
573 GtkObjectClass *object_class = (GtkObjectClass *) c;
574 GtkWidgetClass *widget_class = (GtkWidgetClass *) c;
576 fp_parent_class = (GtkDrawingAreaClass*) gtk_type_class(GTK_TYPE_DRAWING_AREA);
578 object_class->destroy = sp_font_preview_destroy;
580 widget_class->size_request = sp_font_preview_size_request;
581 widget_class->expose_event = sp_font_preview_expose;
582 }
584 static void sp_font_preview_init(SPFontPreview *fprev)
585 {
586 fprev->rgba = 0x000000ff;
587 }
589 static void sp_font_preview_destroy(GtkObject *object)
590 {
591 SPFontPreview *fprev = SP_FONT_PREVIEW (object);
593 if (fprev->rfont) {
594 fprev->rfont->Unref();
595 fprev->rfont = NULL;
596 }
598 if (fprev->font) {
599 fprev->font->Unref();
600 fprev->font = NULL;
601 }
603 g_free(fprev->phrase);
604 fprev->phrase = NULL;
606 if (GTK_OBJECT_CLASS (fp_parent_class)->destroy) {
607 GTK_OBJECT_CLASS (fp_parent_class)->destroy(object);
608 }
609 }
611 void sp_font_preview_size_request(GtkWidget */*widget*/, GtkRequisition *req)
612 {
613 req->width = 256;
614 req->height = 32;
615 }
617 #define SPFP_MAX_LEN 64
619 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
620 {
621 SPFontPreview *fprev = SP_FONT_PREVIEW(widget);
623 if (GTK_WIDGET_DRAWABLE (widget)) {
624 if (fprev->rfont) {
626 int glyphs[SPFP_MAX_LEN];
627 double hpos[SPFP_MAX_LEN];
629 font_instance *tface = fprev->rfont->daddy;
631 double theSize = fprev->rfont->style.transform.descrim();
633 gchar const *p;
634 if (fprev->phrase) {
635 p = fprev->phrase;
636 } else {
637 /* TRANSLATORS: Test string used in text and font dialog (when no
638 * text has been entered) to get a preview of the font. Choose
639 * some representative characters that users of your locale will be
640 * interested in. */
641 p = _("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()");
642 }
643 int len = 0;
645 NRRect bbox;
646 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
648 text_wrapper* str_text=new text_wrapper;
649 str_text->SetDefaultFont(tface);
650 str_text->AppendUTF8(p,-1);
651 if ( str_text->uni32_length > 0 ) {
652 str_text->DoLayout();
653 if ( str_text->glyph_length > 0 ) {
654 PangoFont *curPF = NULL;
655 font_instance *curF = NULL;
656 for (int i = 0; i < str_text->glyph_length && i < SPFP_MAX_LEN; i++) {
657 if ( str_text->glyph_text[i].font != curPF ) {
658 curPF = str_text->glyph_text[i].font;
659 if (curF) {
660 curF->Unref();
661 }
662 curF = NULL;
663 if ( curPF ) {
664 PangoFontDescription* pfd = pango_font_describe(curPF);
665 curF = (font_factory::Default())->Face(pfd);
666 pango_font_description_free(pfd);
667 }
668 }
669 Geom::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
670 base_pt *= theSize;
672 glyphs[len] = str_text->glyph_text[i].gl;
673 hpos[len] = base_pt[0];
674 len++;
675 if ( curF ) {
676 Geom::OptRect nbbox = curF->BBox(str_text->glyph_text[i].gl);
677 if (nbbox) {
678 bbox.x0 = MIN(bbox.x0, base_pt[Geom::X] + theSize * (nbbox->min())[0]);
679 bbox.y0 = MIN(bbox.y0, base_pt[Geom::Y] - theSize * (nbbox->max())[1]);
680 bbox.x1 = MAX(bbox.x1, base_pt[Geom::X] + theSize * (nbbox->max())[0]);
681 bbox.y1 = MAX(bbox.y1, base_pt[Geom::Y] - theSize * (nbbox->min())[1]);
682 }
683 }
684 }
685 if ( curF ) {
686 curF->Unref();
687 }
688 }
689 }
691 // XXX: FIXME: why does this code ignore adv.y
692 /* while (p && *p && (len < SPFP_MAX_LEN)) {
693 unsigned int unival;
694 NRRect gbox;
695 unival = g_utf8_get_char (p);
696 glyphs[len] = tface->MapUnicodeChar( unival);
697 hpos[len] = (int)px;
698 Geom::Point adv = fprev->rfont->Advance(glyphs[len]);
699 fprev->rfont->BBox( glyphs[len], &gbox);
700 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
701 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
702 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
703 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
704 px += adv[Geom::X];
705 len += 1;
706 p = g_utf8_next_char (p);
707 }*/
709 float startx = (widget->allocation.width - (bbox.x1 - bbox.x0)) / 2;
710 float starty = widget->allocation.height - (widget->allocation.height - (bbox.y1 - bbox.y0)) / 2 - bbox.y1;
712 for (int y = event->area.y; y < event->area.y + event->area.height; y += 64) {
713 for (int x = event->area.x; x < event->area.x + event->area.width; x += 64) {
714 NRPixBlock pb, m;
715 int x0 = x;
716 int y0 = y;
717 int x1 = MIN(x0 + 64, event->area.x + event->area.width);
718 int y1 = MIN(y0 + 64, event->area.y + event->area.height);
719 guchar *ps = nr_pixelstore_16K_new (TRUE, 0xff);
720 nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, ps, 3 * (x1 - x0), FALSE, FALSE);
721 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, x0, y0, x1, y1, TRUE);
722 pb.empty = FALSE;
724 PangoFont *curPF = NULL;
725 font_instance *curF = NULL;
726 raster_font *curRF = NULL;
727 for (int i=0; i < len; i++) {
728 if ( str_text->glyph_text[i].font != curPF ) {
729 curPF=str_text->glyph_text[i].font;
730 if ( curF ) {
731 curF->Unref();
732 }
733 curF = NULL;
734 if ( curPF ) {
735 PangoFontDescription* pfd = pango_font_describe(curPF);
736 curF=(font_factory::Default())->Face(pfd);
737 pango_font_description_free(pfd);
738 }
739 if ( curF ) {
740 if ( curRF ) {
741 curRF->Unref();
742 }
743 curRF = NULL;
744 curRF = curF->RasterFont(fprev->rfont->style);
745 }
746 }
747 raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
748 if ( g ) {
749 g->Blit(Geom::Point(hpos[i] + startx, starty), m);
750 }
751 }
752 if (curRF) {
753 curRF->Unref();
754 }
755 if (curF) {
756 curF->Unref();
757 }
759 nr_blit_pixblock_mask_rgba32(&pb, &m, fprev->rgba);
760 gdk_draw_rgb_image(widget->window, widget->style->black_gc,
761 x0, y0, x1 - x0, y1 - y0,
762 GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs);
763 nr_pixblock_release(&m);
764 nr_pixblock_release(&pb);
765 nr_pixelstore_16K_free(ps);
766 }
767 }
769 delete str_text;
771 } else {
772 nr_gdk_draw_gray_garbage(widget->window, widget->style->black_gc,
773 event->area.x, event->area.y,
774 event->area.width, event->area.height);
775 }
776 }
778 return TRUE;
779 }
781 GtkWidget * sp_font_preview_new()
782 {
783 GtkWidget *w = (GtkWidget*) gtk_type_new(SP_TYPE_FONT_PREVIEW);
785 return w;
786 }
788 void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontSelector *fsel)
789 {
790 if (font)
791 {
792 font->Ref();
793 }
795 if (fprev->font)
796 {
797 fprev->font->Unref();
798 }
800 fprev->font = font;
802 if (fprev->rfont)
803 {
804 fprev->rfont->Unref();
805 fprev->rfont=NULL;
806 }
808 if (fprev->font)
809 {
810 Geom::Matrix flip(Geom::Scale(fsel->fontsize, -fsel->fontsize));
811 fprev->rfont = fprev->font->RasterFont(flip, 0);
812 }
814 if (GTK_WIDGET_DRAWABLE (fprev)) gtk_widget_queue_draw (GTK_WIDGET (fprev));
815 }
817 void sp_font_preview_set_rgba32(SPFontPreview *fprev, guint32 rgba)
818 {
819 fprev->rgba = rgba;
820 if (GTK_WIDGET_DRAWABLE (fprev)) {
821 gtk_widget_queue_draw (GTK_WIDGET (fprev));
822 }
823 }
825 void sp_font_preview_set_phrase(SPFontPreview *fprev, const gchar *phrase)
826 {
827 g_free (fprev->phrase);
828 if (phrase) {
829 fprev->phrase = g_strdup (phrase);
830 } else {
831 fprev->phrase = NULL;
832 }
833 if (GTK_WIDGET_DRAWABLE(fprev)) {
834 gtk_widget_queue_draw (GTK_WIDGET (fprev));
835 }
836 }
839 /*
840 Local Variables:
841 mode:c++
842 c-file-style:"stroustrup"
843 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
844 indent-tabs-mode:nil
845 fill-column:99
846 End:
847 */
848 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :