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 GtkType sp_font_selector_get_type()
111 {
112 static GtkType type = 0;
113 if (!type) {
114 static const GtkTypeInfo info = {
115 "SPFontSelector",
116 sizeof(SPFontSelector),
117 sizeof(SPFontSelectorClass),
118 (GtkClassInitFunc) sp_font_selector_class_init,
119 (GtkObjectInitFunc) sp_font_selector_init,
120 NULL, NULL, NULL
121 };
122 type = gtk_type_unique(GTK_TYPE_HBOX, &info);
123 }
124 return type;
125 }
127 static void sp_font_selector_class_init(SPFontSelectorClass *c)
128 {
129 GtkObjectClass *object_class = (GtkObjectClass *) c;
131 fs_parent_class = (GtkHBoxClass* )gtk_type_class(GTK_TYPE_HBOX);
133 fs_signals[FONT_SET] = gtk_signal_new ("font_set",
134 GTK_RUN_FIRST,
135 GTK_CLASS_TYPE(object_class),
136 GTK_SIGNAL_OFFSET(SPFontSelectorClass, font_set),
137 gtk_marshal_NONE__POINTER,
138 GTK_TYPE_NONE,
139 1, GTK_TYPE_POINTER);
141 object_class->destroy = sp_font_selector_destroy;
142 }
144 static void sp_font_selector_init(SPFontSelector *fsel)
145 {
146 gtk_box_set_homogeneous(GTK_BOX(fsel), TRUE);
147 gtk_box_set_spacing(GTK_BOX(fsel), 4);
149 /* Family frame */
150 GtkWidget *f = gtk_frame_new(_("Font family"));
151 gtk_widget_show (f);
152 gtk_box_pack_start (GTK_BOX(fsel), f, TRUE, TRUE, 0);
154 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
155 gtk_widget_show(sw);
156 gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
157 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
158 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
159 gtk_container_add(GTK_CONTAINER(f), sw);
161 Inkscape::FontLister* fontlister = Inkscape::FontLister::get_instance();
163 fsel->family_treeview = gtk_tree_view_new ();
164 GtkTreeViewColumn *column = gtk_tree_view_column_new ();
165 GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
166 gtk_tree_view_column_pack_start (column, cell, FALSE);
167 gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
168 gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->family_treeview), column);
169 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->family_treeview), FALSE);
170 Glib::RefPtr<Gtk::ListStore> store = fontlister->get_font_list();
171 gtk_tree_view_set_model (GTK_TREE_VIEW(fsel->family_treeview), GTK_TREE_MODEL (Glib::unwrap (store)));
172 gtk_container_add(GTK_CONTAINER(sw), fsel->family_treeview);
173 gtk_widget_show_all (sw);
175 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->family_treeview));
176 g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_family_select_row), fsel);
177 g_object_set_data (G_OBJECT(fsel), "family-treeview", fsel->family_treeview);
180 /* Style frame */
181 f = gtk_frame_new(_("Style"));
182 gtk_widget_show(f);
183 gtk_box_pack_start(GTK_BOX (fsel), f, TRUE, TRUE, 0);
185 GtkWidget *vb = gtk_vbox_new(FALSE, 4);
186 gtk_widget_show(vb);
187 gtk_container_set_border_width(GTK_CONTAINER (vb), 4);
188 gtk_container_add(GTK_CONTAINER(f), vb);
190 sw = gtk_scrolled_window_new(NULL, NULL);
191 gtk_widget_show(sw);
192 gtk_container_set_border_width(GTK_CONTAINER (sw), 4);
193 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
194 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN);
195 gtk_box_pack_start(GTK_BOX (vb), sw, TRUE, TRUE, 0);
197 fsel->style_treeview = gtk_tree_view_new ();
198 column = gtk_tree_view_column_new ();
199 cell = gtk_cell_renderer_text_new ();
200 gtk_tree_view_column_pack_start (column, cell, FALSE);
201 gtk_tree_view_column_set_attributes (column, cell, "text", 0, NULL);
202 gtk_tree_view_append_column (GTK_TREE_VIEW(fsel->style_treeview), column);
203 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(fsel->style_treeview), FALSE);
204 gtk_container_add(GTK_CONTAINER(sw), fsel->style_treeview);
205 gtk_widget_show_all (sw);
207 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(fsel->style_treeview));
208 g_signal_connect (G_OBJECT(selection), "changed", G_CALLBACK (sp_font_selector_style_select_row), fsel);
210 GtkWidget *hb = gtk_hbox_new(FALSE, 4);
211 gtk_widget_show(hb);
212 gtk_box_pack_start(GTK_BOX(vb), hb, FALSE, FALSE, 0);
214 fsel->size = gtk_combo_box_entry_new_text ();
215 gtk_widget_set_size_request(fsel->size, 90, -1);
216 g_signal_connect (G_OBJECT(fsel->size), "changed", G_CALLBACK (sp_font_selector_size_changed), fsel);
217 gtk_box_pack_end (GTK_BOX(hb), fsel->size, FALSE, FALSE, 0);
219 GtkWidget *l = gtk_label_new(_("Font size:"));
220 gtk_widget_show_all (l);
221 gtk_box_pack_end(GTK_BOX (hb), l, FALSE, FALSE, 0);
223 for (unsigned int n = 0; sizes[n]; ++n)
224 {
225 gtk_combo_box_append_text (GTK_COMBO_BOX(fsel->size), sizes[n]);
226 }
228 gtk_widget_show_all (fsel->size);
230 fsel->familyidx = 0;
231 fsel->styleidx = 0;
232 fsel->fontsize = 10.0;
233 fsel->fontsize_dirty = false;
234 fsel->font = NULL;
235 }
237 static void sp_font_selector_destroy(GtkObject *object)
238 {
239 SPFontSelector *fsel = SP_FONT_SELECTOR (object);
241 if (fsel->font) {
242 fsel->font->Unref();
243 fsel->font = NULL;
244 }
246 if (fsel->families.length > 0) {
247 nr_name_list_release(&fsel->families);
248 fsel->families.length = 0;
249 }
251 if (fsel->styles.length > 0) {
252 nr_style_list_release(&fsel->styles);
253 fsel->styles.length = 0;
254 }
256 if (GTK_OBJECT_CLASS(fs_parent_class)->destroy) {
257 GTK_OBJECT_CLASS(fs_parent_class)->destroy(object);
258 }
259 }
261 static void sp_font_selector_family_select_row(GtkTreeSelection *selection,
262 SPFontSelector *fsel)
263 {
264 GtkTreeIter iter;
265 GtkTreeModel *model;
266 GtkListStore *store;
267 GtkTreePath *path;
268 GList *list=0;
270 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
272 path = gtk_tree_model_get_path (model, &iter);
273 gtk_tree_model_get (model, &iter, 1, &list, -1);
274 fsel->familyidx = gtk_tree_path_get_indices (path)[0];
275 fsel->styleidx = 0;
277 store = gtk_list_store_new (1, G_TYPE_STRING);
279 for ( ; list ; list = list->next )
280 {
281 gtk_list_store_append (store, &iter);
282 gtk_list_store_set (store, &iter, 0, (char*)list->data, -1);
283 }
285 gtk_tree_view_set_model (GTK_TREE_VIEW (fsel->style_treeview), GTK_TREE_MODEL (store));
286 path = gtk_tree_path_new ();
287 gtk_tree_path_append_index (path, 0);
288 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path);
289 gtk_tree_path_free (path);
290 }
292 static void sp_font_selector_style_select_row (GtkTreeSelection *selection,
293 SPFontSelector *fsel)
294 {
295 GtkTreeModel *model;
296 GtkTreePath *path;
297 GtkTreeIter iter;
299 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) return;
301 path = gtk_tree_model_get_path (model, &iter);
302 fsel->styleidx = gtk_tree_path_get_indices (path)[0];
304 if (!fsel->block_emit)
305 {
306 sp_font_selector_emit_set (fsel);
307 }
308 }
310 static void sp_font_selector_size_changed( GtkComboBox */*cbox*/, SPFontSelector *fsel )
311 {
312 char *text = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
313 gfloat old_size = fsel->fontsize;
315 gchar *endptr;
316 gdouble value = -1;
317 if (text) {
318 value = g_strtod (text, &endptr);
319 if (endptr == text) // conversion failed, non-numeric input
320 value = -1;
321 free (text);
322 }
323 if (value <= 0) {
324 return; // could not parse value
325 }
326 if (value > 10000)
327 value = 10000; // somewhat arbitrary, but text&font preview freezes with too huge fontsizes
329 fsel->fontsize = value;
330 if ( fabs(fsel->fontsize-old_size) > 0.001)
331 {
332 fsel->fontsize_dirty = true;
333 }
335 sp_font_selector_emit_set (fsel);
336 }
338 static void sp_font_selector_emit_set (SPFontSelector *fsel)
339 {
340 font_instance *font;
342 GtkTreeSelection *selection_family;
343 GtkTreeSelection *selection_style;
344 GtkTreeModel *model_family;
345 GtkTreeModel *model_style;
346 GtkTreeIter iter_family;
347 GtkTreeIter iter_style;
348 char *family=0, *style=0;
350 //We need to check this here since most GtkTreeModel operations are not atomic
351 //See GtkListStore documenation, Chapter "Atomic Operations" --mderezynski
353 model_family = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->family_treeview));
354 if (!model_family) return;
355 model_style = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->style_treeview));
356 if (!model_style) return;
358 selection_family = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview));
359 selection_style = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview));
361 if (!gtk_tree_selection_get_selected (selection_family, NULL, &iter_family)) return;
362 if (!gtk_tree_selection_get_selected (selection_style, NULL, &iter_style)) return;
364 gtk_tree_model_get (model_family, &iter_family, 0, &family, -1);
365 gtk_tree_model_get (model_style, &iter_style, 0, &style, -1);
367 if ((!family) || (!style)) return;
369 font = (font_factory::Default())->FaceFromUIStrings (family, style);
371 // FIXME: when a text object uses non-available font, font==NULL and we can't set size
372 // (and the size shown in the widget is invalid). To fix, here we must always get some
373 // default font, exactly the same as sptext uses for on-canvas display, so that
374 // font!=NULL ever.
375 if (font != fsel->font || ( font && fsel->fontsize_dirty ) ) {
376 if ( font ) {
377 font->Ref();
378 }
379 if ( fsel->font ) {
380 fsel->font->Unref();
381 }
382 fsel->font = font;
383 gtk_signal_emit(GTK_OBJECT(fsel), fs_signals[FONT_SET], fsel->font);
384 }
385 fsel->fontsize_dirty = false;
386 if (font) {
387 font->Unref();
388 }
389 font = NULL;
390 }
392 GtkWidget *sp_font_selector_new()
393 {
394 SPFontSelector *fsel = (SPFontSelector*) gtk_type_new(SP_TYPE_FONT_SELECTOR);
396 return (GtkWidget *) fsel;
397 }
399 void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, double size)
400 {
401 if (font)
402 {
403 Gtk::TreePath path;
404 font_instance *tempFont = NULL;
406 Glib::ustring family = font_factory::Default()->GetUIFamilyString(font->descr);
408 try {
409 path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
410 } catch (...) {
411 return;
412 }
414 fsel->block_emit = TRUE;
415 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
416 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
417 fsel->block_emit = FALSE;
419 GList *list = 0;
420 GtkTreeIter iter;
421 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
422 gtk_tree_model_get_iter (model, &iter, path.gobj());
423 gtk_tree_model_get (model, &iter, 1, &list, -1);
425 unsigned int currentStyleNumber = 0;
426 unsigned int bestStyleNumber = 0;
428 PangoFontDescription *incomingFont = pango_font_description_copy(font->descr);
429 pango_font_description_unset_fields(incomingFont, PANGO_FONT_MASK_SIZE);
431 char *incomingFontString = pango_font_description_to_string(incomingFont);
433 tempFont = (font_factory::Default())->FaceFromUIStrings(family.c_str(), (char*)list->data);
435 PangoFontDescription *bestMatchForFont = NULL;
436 if (tempFont) {
437 bestMatchForFont = pango_font_description_copy(tempFont->descr);
438 tempFont->Unref();
439 tempFont = NULL;
440 }
442 pango_font_description_unset_fields(bestMatchForFont, PANGO_FONT_MASK_SIZE);
444 list = list->next;
446 while (list) {
447 currentStyleNumber++;
449 tempFont = font_factory::Default()->FaceFromUIStrings(family.c_str(), (char*)list->data);
451 PangoFontDescription *currentMatchForFont = NULL;
452 if (tempFont) {
453 currentMatchForFont = pango_font_description_copy(tempFont->descr);
454 tempFont->Unref();
455 tempFont = NULL;
456 }
458 if (currentMatchForFont) {
459 pango_font_description_unset_fields(currentMatchForFont, PANGO_FONT_MASK_SIZE);
461 char *currentMatchString = pango_font_description_to_string(currentMatchForFont);
463 if (!strcmp(incomingFontString, currentMatchString)
464 || pango_font_description_better_match(incomingFont, bestMatchForFont, currentMatchForFont)) {
465 // Found a better match for the font we are looking for
466 pango_font_description_free(bestMatchForFont);
467 bestMatchForFont = pango_font_description_copy(currentMatchForFont);
468 bestStyleNumber = currentStyleNumber;
469 }
471 g_free(currentMatchString);
473 pango_font_description_free(currentMatchForFont);
474 }
476 list = list->next;
477 }
479 if (bestMatchForFont)
480 pango_font_description_free(bestMatchForFont);
481 if (incomingFont)
482 pango_font_description_free(incomingFont);
483 g_free(incomingFontString);
485 GtkTreePath *path_c = gtk_tree_path_new ();
486 gtk_tree_path_append_index (path_c, bestStyleNumber);
487 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
488 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
490 if (size != fsel->fontsize)
491 {
492 gchar s[8];
493 g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
494 gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
495 fsel->fontsize = size;
496 }
497 }
499 }
501 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
502 {
503 if (fsel->font) {
504 fsel->font->Ref();
505 }
507 return fsel->font;
508 }
510 double sp_font_selector_get_size(SPFontSelector *fsel)
511 {
512 return fsel->fontsize;
513 }
515 /* SPFontPreview */
517 struct SPFontPreview
518 {
519 GtkDrawingArea darea;
521 font_instance *font;
522 raster_font *rfont;
523 gchar *phrase;
524 unsigned long rgba;
525 };
527 struct SPFontPreviewClass
528 {
529 GtkDrawingAreaClass parent_class;
530 };
532 static void sp_font_preview_class_init(SPFontPreviewClass *c);
533 static void sp_font_preview_init(SPFontPreview *fsel);
534 static void sp_font_preview_destroy(GtkObject *object);
536 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req);
537 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
539 static GtkDrawingAreaClass *fp_parent_class = NULL;
541 GtkType sp_font_preview_get_type()
542 {
543 static GtkType type = 0;
544 if (!type) {
545 static const GtkTypeInfo info = {
546 "SPFontPreview",
547 sizeof (SPFontPreview),
548 sizeof (SPFontPreviewClass),
549 (GtkClassInitFunc) sp_font_preview_class_init,
550 (GtkObjectInitFunc) sp_font_preview_init,
551 NULL, NULL, NULL
552 };
553 type = gtk_type_unique (GTK_TYPE_DRAWING_AREA, &info);
554 }
555 return type;
556 }
558 static void sp_font_preview_class_init (SPFontPreviewClass *c)
559 {
560 GtkObjectClass *object_class = (GtkObjectClass *) c;
561 GtkWidgetClass *widget_class = (GtkWidgetClass *) c;
563 fp_parent_class = (GtkDrawingAreaClass*) gtk_type_class(GTK_TYPE_DRAWING_AREA);
565 object_class->destroy = sp_font_preview_destroy;
567 widget_class->size_request = sp_font_preview_size_request;
568 widget_class->expose_event = sp_font_preview_expose;
569 }
571 static void sp_font_preview_init(SPFontPreview *fprev)
572 {
573 fprev->rgba = 0x000000ff;
574 }
576 static void sp_font_preview_destroy(GtkObject *object)
577 {
578 SPFontPreview *fprev = SP_FONT_PREVIEW (object);
580 if (fprev->rfont) {
581 fprev->rfont->Unref();
582 fprev->rfont = NULL;
583 }
585 if (fprev->font) {
586 fprev->font->Unref();
587 fprev->font = NULL;
588 }
590 g_free(fprev->phrase);
591 fprev->phrase = NULL;
593 if (GTK_OBJECT_CLASS (fp_parent_class)->destroy) {
594 GTK_OBJECT_CLASS (fp_parent_class)->destroy(object);
595 }
596 }
598 void sp_font_preview_size_request(GtkWidget */*widget*/, GtkRequisition *req)
599 {
600 req->width = 256;
601 req->height = 32;
602 }
604 #define SPFP_MAX_LEN 64
606 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
607 {
608 SPFontPreview *fprev = SP_FONT_PREVIEW(widget);
610 if (GTK_WIDGET_DRAWABLE (widget)) {
611 if (fprev->rfont) {
613 int glyphs[SPFP_MAX_LEN];
614 double hpos[SPFP_MAX_LEN];
616 font_instance *tface = fprev->rfont->daddy;
618 double theSize = NR_MATRIX_DF_EXPANSION (&fprev->rfont->style.transform);
620 gchar const *p;
621 if (fprev->phrase) {
622 p = fprev->phrase;
623 } else {
624 /* TRANSLATORS: Test string used in text and font dialog (when no
625 * text has been entered) to get a preview of the font. Choose
626 * some representative characters that users of your locale will be
627 * interested in. */
628 p = _("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()");
629 }
630 int len = 0;
632 NRRect bbox;
633 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
635 text_wrapper* str_text=new text_wrapper;
636 str_text->SetDefaultFont(tface);
637 str_text->AppendUTF8(p,-1);
638 if ( str_text->uni32_length > 0 ) {
639 str_text->DoLayout();
640 if ( str_text->glyph_length > 0 ) {
641 PangoFont *curPF = NULL;
642 font_instance *curF = NULL;
643 for (int i = 0; i < str_text->glyph_length && i < SPFP_MAX_LEN; i++) {
644 if ( str_text->glyph_text[i].font != curPF ) {
645 curPF = str_text->glyph_text[i].font;
646 if (curF) {
647 curF->Unref();
648 }
649 curF = NULL;
650 if ( curPF ) {
651 PangoFontDescription* pfd = pango_font_describe(curPF);
652 curF = (font_factory::Default())->Face(pfd);
653 pango_font_description_free(pfd);
654 }
655 }
656 NR::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
657 base_pt *= theSize;
659 glyphs[len] = str_text->glyph_text[i].gl;
660 hpos[len] = base_pt[0];
661 len++;
662 if ( curF ) {
663 NR::Maybe<NR::Rect> nbbox = curF->BBox(str_text->glyph_text[i].gl);
664 if (nbbox) {
665 bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox->min())[0]);
666 bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox->max())[1]);
667 bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox->max())[0]);
668 bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox->min())[1]);
669 }
670 }
671 }
672 if ( curF ) {
673 curF->Unref();
674 }
675 }
676 }
678 // XXX: FIXME: why does this code ignore adv.y
679 /* while (p && *p && (len < SPFP_MAX_LEN)) {
680 unsigned int unival;
681 NRRect gbox;
682 unival = g_utf8_get_char (p);
683 glyphs[len] = tface->MapUnicodeChar( unival);
684 hpos[len] = (int)px;
685 NR::Point adv = fprev->rfont->Advance(glyphs[len]);
686 fprev->rfont->BBox( glyphs[len], &gbox);
687 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
688 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
689 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
690 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
691 px += adv[NR::X];
692 len += 1;
693 p = g_utf8_next_char (p);
694 }*/
696 float startx = (widget->allocation.width - (bbox.x1 - bbox.x0)) / 2;
697 float starty = widget->allocation.height - (widget->allocation.height - (bbox.y1 - bbox.y0)) / 2 - bbox.y1;
699 for (int y = event->area.y; y < event->area.y + event->area.height; y += 64) {
700 for (int x = event->area.x; x < event->area.x + event->area.width; x += 64) {
701 NRPixBlock pb, m;
702 int x0 = x;
703 int y0 = y;
704 int x1 = MIN(x0 + 64, event->area.x + event->area.width);
705 int y1 = MIN(y0 + 64, event->area.y + event->area.height);
706 guchar *ps = nr_pixelstore_16K_new (TRUE, 0xff);
707 nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, ps, 3 * (x1 - x0), FALSE, FALSE);
708 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, x0, y0, x1, y1, TRUE);
709 pb.empty = FALSE;
711 PangoFont *curPF = NULL;
712 font_instance *curF = NULL;
713 raster_font *curRF = NULL;
714 for (int i=0; i < len; i++) {
715 if ( str_text->glyph_text[i].font != curPF ) {
716 curPF=str_text->glyph_text[i].font;
717 if ( curF ) {
718 curF->Unref();
719 }
720 curF = NULL;
721 if ( curPF ) {
722 PangoFontDescription* pfd = pango_font_describe(curPF);
723 curF=(font_factory::Default())->Face(pfd);
724 pango_font_description_free(pfd);
725 }
726 if ( curF ) {
727 if ( curRF ) {
728 curRF->Unref();
729 }
730 curRF = NULL;
731 curRF = curF->RasterFont(fprev->rfont->style);
732 }
733 }
734 raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
735 if ( g ) {
736 g->Blit(NR::Point(hpos[i] + startx, starty), m);
737 }
738 }
739 if (curRF) {
740 curRF->Unref();
741 }
742 if (curF) {
743 curF->Unref();
744 }
746 nr_blit_pixblock_mask_rgba32(&pb, &m, fprev->rgba);
747 gdk_draw_rgb_image(widget->window, widget->style->black_gc,
748 x0, y0, x1 - x0, y1 - y0,
749 GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs);
750 nr_pixblock_release(&m);
751 nr_pixblock_release(&pb);
752 nr_pixelstore_16K_free(ps);
753 }
754 }
756 delete str_text;
758 } else {
759 nr_gdk_draw_gray_garbage(widget->window, widget->style->black_gc,
760 event->area.x, event->area.y,
761 event->area.width, event->area.height);
762 }
763 }
765 return TRUE;
766 }
768 GtkWidget * sp_font_preview_new()
769 {
770 GtkWidget *w = (GtkWidget*) gtk_type_new(SP_TYPE_FONT_PREVIEW);
772 return w;
773 }
775 void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontSelector *fsel)
776 {
777 if (font)
778 {
779 font->Ref();
780 }
782 if (fprev->font)
783 {
784 fprev->font->Unref();
785 }
787 fprev->font = font;
789 if (fprev->rfont)
790 {
791 fprev->rfont->Unref();
792 fprev->rfont=NULL;
793 }
795 if (fprev->font)
796 {
797 NRMatrix flip;
798 nr_matrix_set_scale (&flip, fsel->fontsize, -fsel->fontsize);
799 fprev->rfont = fprev->font->RasterFont(flip, 0);
800 }
802 if (GTK_WIDGET_DRAWABLE (fprev)) gtk_widget_queue_draw (GTK_WIDGET (fprev));
803 }
805 void sp_font_preview_set_rgba32(SPFontPreview *fprev, guint32 rgba)
806 {
807 fprev->rgba = rgba;
808 if (GTK_WIDGET_DRAWABLE (fprev)) {
809 gtk_widget_queue_draw (GTK_WIDGET (fprev));
810 }
811 }
813 void sp_font_preview_set_phrase(SPFontPreview *fprev, const gchar *phrase)
814 {
815 g_free (fprev->phrase);
816 if (phrase) {
817 fprev->phrase = g_strdup (phrase);
818 } else {
819 fprev->phrase = NULL;
820 }
821 if (GTK_WIDGET_DRAWABLE(fprev)) {
822 gtk_widget_queue_draw (GTK_WIDGET (fprev));
823 }
824 }
827 /*
828 Local Variables:
829 mode:c++
830 c-file-style:"stroustrup"
831 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
832 indent-tabs-mode:nil
833 fill-column:99
834 End:
835 */
836 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :