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 *sstr = gtk_combo_box_get_active_text (GTK_COMBO_BOX (fsel->size));
313 gfloat old_size = fsel->fontsize;
314 fsel->fontsize = MAX(atof(sstr), 0.1);
315 if ( fabs(fsel->fontsize-old_size) > 0.001)
316 {
317 fsel->fontsize_dirty = true;
318 }
320 sp_font_selector_emit_set (fsel);
322 free (sstr);
323 }
325 static void sp_font_selector_emit_set (SPFontSelector *fsel)
326 {
327 font_instance *font;
329 GtkTreeSelection *selection_family;
330 GtkTreeSelection *selection_style;
331 GtkTreeModel *model_family;
332 GtkTreeModel *model_style;
333 GtkTreeIter iter_family;
334 GtkTreeIter iter_style;
335 char *family=0, *style=0;
337 //We need to check this here since most GtkTreeModel operations are not atomic
338 //See GtkListStore documenation, Chapter "Atomic Operations" --mderezynski
340 model_family = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->family_treeview));
341 if (!model_family) return;
342 model_style = gtk_tree_view_get_model (GTK_TREE_VIEW (fsel->style_treeview));
343 if (!model_style) return;
345 selection_family = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview));
346 selection_style = gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview));
348 if (!gtk_tree_selection_get_selected (selection_family, NULL, &iter_family)) return;
349 if (!gtk_tree_selection_get_selected (selection_style, NULL, &iter_style)) return;
351 gtk_tree_model_get (model_family, &iter_family, 0, &family, -1);
352 gtk_tree_model_get (model_style, &iter_style, 0, &style, -1);
354 if ((!family) || (!style)) return;
356 font = (font_factory::Default())->FaceFromUIStrings (family, style);
358 // FIXME: when a text object uses non-available font, font==NULL and we can't set size
359 // (and the size shown in the widget is invalid). To fix, here we must always get some
360 // default font, exactly the same as sptext uses for on-canvas display, so that
361 // font!=NULL ever.
362 if (font != fsel->font || ( font && fsel->fontsize_dirty ) ) {
363 if ( font ) {
364 font->Ref();
365 }
366 if ( fsel->font ) {
367 fsel->font->Unref();
368 }
369 fsel->font = font;
370 gtk_signal_emit(GTK_OBJECT(fsel), fs_signals[FONT_SET], fsel->font);
371 }
372 fsel->fontsize_dirty = false;
373 if (font) {
374 font->Unref();
375 }
376 font = NULL;
377 }
379 GtkWidget *sp_font_selector_new()
380 {
381 SPFontSelector *fsel = (SPFontSelector*) gtk_type_new(SP_TYPE_FONT_SELECTOR);
383 return (GtkWidget *) fsel;
384 }
386 void sp_font_selector_set_font (SPFontSelector *fsel, font_instance *font, double size)
387 {
388 if (font)
389 {
390 Gtk::TreePath path;
391 font_instance *tempFont = NULL;
393 Glib::ustring family = font_factory::Default()->GetUIFamilyString(font->descr);
395 try {
396 path = Inkscape::FontLister::get_instance()->get_row_for_font (family);
397 } catch (...) {
398 return;
399 }
401 fsel->block_emit = TRUE;
402 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->family_treeview)), path.gobj());
403 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->family_treeview), path.gobj(), NULL, TRUE, 0.5, 0.5);
404 fsel->block_emit = FALSE;
406 GList *list = 0;
407 GtkTreeIter iter;
408 GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW(fsel->family_treeview));
409 gtk_tree_model_get_iter (model, &iter, path.gobj());
410 gtk_tree_model_get (model, &iter, 1, &list, -1);
412 unsigned int currentStyleNumber = 0;
413 unsigned int bestStyleNumber = 0;
415 PangoFontDescription *incomingFont = pango_font_description_copy(font->descr);
416 pango_font_description_unset_fields(incomingFont, PANGO_FONT_MASK_SIZE);
418 char *incomingFontString = pango_font_description_to_string(incomingFont);
420 tempFont = (font_factory::Default())->FaceFromUIStrings(family.c_str(), (char*)list->data);
422 PangoFontDescription *bestMatchForFont = NULL;
423 if (tempFont) {
424 bestMatchForFont = pango_font_description_copy(tempFont->descr);
425 tempFont->Unref();
426 tempFont = NULL;
427 }
429 pango_font_description_unset_fields(bestMatchForFont, PANGO_FONT_MASK_SIZE);
431 list = list->next;
433 while (list) {
434 currentStyleNumber++;
436 tempFont = font_factory::Default()->FaceFromUIStrings(family.c_str(), (char*)list->data);
438 PangoFontDescription *currentMatchForFont = NULL;
439 if (tempFont) {
440 currentMatchForFont = pango_font_description_copy(tempFont->descr);
441 tempFont->Unref();
442 tempFont = NULL;
443 }
445 if (currentMatchForFont) {
446 pango_font_description_unset_fields(currentMatchForFont, PANGO_FONT_MASK_SIZE);
448 char *currentMatchString = pango_font_description_to_string(currentMatchForFont);
450 if (!strcmp(incomingFontString, currentMatchString)
451 || pango_font_description_better_match(incomingFont, bestMatchForFont, currentMatchForFont)) {
452 // Found a better match for the font we are looking for
453 pango_font_description_free(bestMatchForFont);
454 bestMatchForFont = pango_font_description_copy(currentMatchForFont);
455 bestStyleNumber = currentStyleNumber;
456 }
458 g_free(currentMatchString);
460 pango_font_description_free(currentMatchForFont);
461 }
463 list = list->next;
464 }
466 if (bestMatchForFont)
467 pango_font_description_free(bestMatchForFont);
468 if (incomingFont)
469 pango_font_description_free(incomingFont);
470 g_free(incomingFontString);
472 GtkTreePath *path_c = gtk_tree_path_new ();
473 gtk_tree_path_append_index (path_c, bestStyleNumber);
474 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
475 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
477 if (size != fsel->fontsize)
478 {
479 gchar s[8];
480 g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
481 gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
482 fsel->fontsize = size;
483 }
484 }
486 }
488 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
489 {
490 if (fsel->font) {
491 fsel->font->Ref();
492 }
494 return fsel->font;
495 }
497 double sp_font_selector_get_size(SPFontSelector *fsel)
498 {
499 return fsel->fontsize;
500 }
502 /* SPFontPreview */
504 struct SPFontPreview
505 {
506 GtkDrawingArea darea;
508 font_instance *font;
509 raster_font *rfont;
510 gchar *phrase;
511 unsigned long rgba;
512 };
514 struct SPFontPreviewClass
515 {
516 GtkDrawingAreaClass parent_class;
517 };
519 static void sp_font_preview_class_init(SPFontPreviewClass *c);
520 static void sp_font_preview_init(SPFontPreview *fsel);
521 static void sp_font_preview_destroy(GtkObject *object);
523 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req);
524 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
526 static GtkDrawingAreaClass *fp_parent_class = NULL;
528 GtkType sp_font_preview_get_type()
529 {
530 static GtkType type = 0;
531 if (!type) {
532 static const GtkTypeInfo info = {
533 "SPFontPreview",
534 sizeof (SPFontPreview),
535 sizeof (SPFontPreviewClass),
536 (GtkClassInitFunc) sp_font_preview_class_init,
537 (GtkObjectInitFunc) sp_font_preview_init,
538 NULL, NULL, NULL
539 };
540 type = gtk_type_unique (GTK_TYPE_DRAWING_AREA, &info);
541 }
542 return type;
543 }
545 static void sp_font_preview_class_init (SPFontPreviewClass *c)
546 {
547 GtkObjectClass *object_class = (GtkObjectClass *) c;
548 GtkWidgetClass *widget_class = (GtkWidgetClass *) c;
550 fp_parent_class = (GtkDrawingAreaClass*) gtk_type_class(GTK_TYPE_DRAWING_AREA);
552 object_class->destroy = sp_font_preview_destroy;
554 widget_class->size_request = sp_font_preview_size_request;
555 widget_class->expose_event = sp_font_preview_expose;
556 }
558 static void sp_font_preview_init(SPFontPreview *fprev)
559 {
560 fprev->rgba = 0x000000ff;
561 }
563 static void sp_font_preview_destroy(GtkObject *object)
564 {
565 SPFontPreview *fprev = SP_FONT_PREVIEW (object);
567 if (fprev->rfont) {
568 fprev->rfont->Unref();
569 fprev->rfont = NULL;
570 }
572 if (fprev->font) {
573 fprev->font->Unref();
574 fprev->font = NULL;
575 }
577 g_free(fprev->phrase);
578 fprev->phrase = NULL;
580 if (GTK_OBJECT_CLASS (fp_parent_class)->destroy) {
581 GTK_OBJECT_CLASS (fp_parent_class)->destroy(object);
582 }
583 }
585 void sp_font_preview_size_request(GtkWidget */*widget*/, GtkRequisition *req)
586 {
587 req->width = 256;
588 req->height = 32;
589 }
591 #define SPFP_MAX_LEN 64
593 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
594 {
595 SPFontPreview *fprev = SP_FONT_PREVIEW(widget);
597 if (GTK_WIDGET_DRAWABLE (widget)) {
598 if (fprev->rfont) {
600 int glyphs[SPFP_MAX_LEN];
601 double hpos[SPFP_MAX_LEN];
603 font_instance *tface = fprev->rfont->daddy;
605 double theSize = NR_MATRIX_DF_EXPANSION (&fprev->rfont->style.transform);
607 gchar const *p;
608 if (fprev->phrase) {
609 p = fprev->phrase;
610 } else {
611 /* TRANSLATORS: Test string used in text and font dialog (when no
612 * text has been entered) to get a preview of the font. Choose
613 * some representative characters that users of your locale will be
614 * interested in. */
615 p = _("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()");
616 }
617 int len = 0;
619 NRRect bbox;
620 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
622 text_wrapper* str_text=new text_wrapper;
623 str_text->SetDefaultFont(tface);
624 str_text->AppendUTF8(p,-1);
625 if ( str_text->uni32_length > 0 ) {
626 str_text->DoLayout();
627 if ( str_text->glyph_length > 0 ) {
628 PangoFont *curPF = NULL;
629 font_instance *curF = NULL;
630 for (int i = 0; i < str_text->glyph_length && i < SPFP_MAX_LEN; i++) {
631 if ( str_text->glyph_text[i].font != curPF ) {
632 curPF = str_text->glyph_text[i].font;
633 if (curF) {
634 curF->Unref();
635 }
636 curF = NULL;
637 if ( curPF ) {
638 PangoFontDescription* pfd = pango_font_describe(curPF);
639 curF = (font_factory::Default())->Face(pfd);
640 pango_font_description_free(pfd);
641 }
642 }
643 NR::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
644 base_pt *= theSize;
646 glyphs[len] = str_text->glyph_text[i].gl;
647 hpos[len] = base_pt[0];
648 len++;
649 if ( curF ) {
650 NR::Maybe<NR::Rect> nbbox = curF->BBox(str_text->glyph_text[i].gl);
651 if (nbbox) {
652 bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox->min())[0]);
653 bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox->max())[1]);
654 bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox->max())[0]);
655 bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox->min())[1]);
656 }
657 }
658 }
659 if ( curF ) {
660 curF->Unref();
661 }
662 }
663 }
665 // XXX: FIXME: why does this code ignore adv.y
666 /* while (p && *p && (len < SPFP_MAX_LEN)) {
667 unsigned int unival;
668 NRRect gbox;
669 unival = g_utf8_get_char (p);
670 glyphs[len] = tface->MapUnicodeChar( unival);
671 hpos[len] = (int)px;
672 NR::Point adv = fprev->rfont->Advance(glyphs[len]);
673 fprev->rfont->BBox( glyphs[len], &gbox);
674 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
675 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
676 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
677 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
678 px += adv[NR::X];
679 len += 1;
680 p = g_utf8_next_char (p);
681 }*/
683 float startx = (widget->allocation.width - (bbox.x1 - bbox.x0)) / 2;
684 float starty = widget->allocation.height - (widget->allocation.height - (bbox.y1 - bbox.y0)) / 2 - bbox.y1;
686 for (int y = event->area.y; y < event->area.y + event->area.height; y += 64) {
687 for (int x = event->area.x; x < event->area.x + event->area.width; x += 64) {
688 NRPixBlock pb, m;
689 int x0 = x;
690 int y0 = y;
691 int x1 = MIN(x0 + 64, event->area.x + event->area.width);
692 int y1 = MIN(y0 + 64, event->area.y + event->area.height);
693 guchar *ps = nr_pixelstore_16K_new (TRUE, 0xff);
694 nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, ps, 3 * (x1 - x0), FALSE, FALSE);
695 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, x0, y0, x1, y1, TRUE);
696 pb.empty = FALSE;
698 PangoFont *curPF = NULL;
699 font_instance *curF = NULL;
700 raster_font *curRF = NULL;
701 for (int i=0; i < len; i++) {
702 if ( str_text->glyph_text[i].font != curPF ) {
703 curPF=str_text->glyph_text[i].font;
704 if ( curF ) {
705 curF->Unref();
706 }
707 curF = NULL;
708 if ( curPF ) {
709 PangoFontDescription* pfd = pango_font_describe(curPF);
710 curF=(font_factory::Default())->Face(pfd);
711 pango_font_description_free(pfd);
712 }
713 if ( curF ) {
714 if ( curRF ) {
715 curRF->Unref();
716 }
717 curRF = NULL;
718 curRF = curF->RasterFont(fprev->rfont->style);
719 }
720 }
721 raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
722 if ( g ) {
723 g->Blit(NR::Point(hpos[i] + startx, starty), m);
724 }
725 }
726 if (curRF) {
727 curRF->Unref();
728 }
729 if (curF) {
730 curF->Unref();
731 }
733 nr_blit_pixblock_mask_rgba32(&pb, &m, fprev->rgba);
734 gdk_draw_rgb_image(widget->window, widget->style->black_gc,
735 x0, y0, x1 - x0, y1 - y0,
736 GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs);
737 nr_pixblock_release(&m);
738 nr_pixblock_release(&pb);
739 nr_pixelstore_16K_free(ps);
740 }
741 }
743 delete str_text;
745 } else {
746 nr_gdk_draw_gray_garbage(widget->window, widget->style->black_gc,
747 event->area.x, event->area.y,
748 event->area.width, event->area.height);
749 }
750 }
752 return TRUE;
753 }
755 GtkWidget * sp_font_preview_new()
756 {
757 GtkWidget *w = (GtkWidget*) gtk_type_new(SP_TYPE_FONT_PREVIEW);
759 return w;
760 }
762 void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontSelector *fsel)
763 {
764 if (font)
765 {
766 font->Ref();
767 }
769 if (fprev->font)
770 {
771 fprev->font->Unref();
772 }
774 fprev->font = font;
776 if (fprev->rfont)
777 {
778 fprev->rfont->Unref();
779 fprev->rfont=NULL;
780 }
782 if (fprev->font)
783 {
784 NRMatrix flip;
785 nr_matrix_set_scale (&flip, fsel->fontsize, -fsel->fontsize);
786 fprev->rfont = fprev->font->RasterFont(flip, 0);
787 }
789 if (GTK_WIDGET_DRAWABLE (fprev)) gtk_widget_queue_draw (GTK_WIDGET (fprev));
790 }
792 void sp_font_preview_set_rgba32(SPFontPreview *fprev, guint32 rgba)
793 {
794 fprev->rgba = rgba;
795 if (GTK_WIDGET_DRAWABLE (fprev)) {
796 gtk_widget_queue_draw (GTK_WIDGET (fprev));
797 }
798 }
800 void sp_font_preview_set_phrase(SPFontPreview *fprev, const gchar *phrase)
801 {
802 g_free (fprev->phrase);
803 if (phrase) {
804 fprev->phrase = g_strdup (phrase);
805 } else {
806 fprev->phrase = NULL;
807 }
808 if (GTK_WIDGET_DRAWABLE(fprev)) {
809 gtk_widget_queue_draw (GTK_WIDGET (fprev));
810 }
811 }
814 /*
815 Local Variables:
816 mode:c++
817 c-file-style:"stroustrup"
818 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
819 indent-tabs-mode:nil
820 fill-column:99
821 End:
822 */
823 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :