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())->FaceFromDescr (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 gchar family[256];
391 font->Family (family, 256);
393 Gtk::TreePath path;
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 gchar descr[256];
413 font->Name(descr, 256);
414 std::string descr_best (family);
415 descr_best += " ";
416 descr_best += ((char*)list->data);
418 PangoFontDescription *descr_ = pango_font_description_from_string(descr);
419 PangoFontDescription *best_ = pango_font_description_from_string(descr_best.c_str());
421 unsigned int i = 0;
422 unsigned int best_i = 0;
424 // try to find best match with style description (i.e. bold, italic ?)
425 for (list = list->next ; list ; list = list->next)
426 {
427 i++;
428 std::string descr_try (family);
429 descr_try += " ";
430 descr_try += ((char*)list->data);
431 PangoFontDescription *try_ = pango_font_description_from_string(descr_try.c_str());
432 if (pango_font_description_better_match (descr_, best_, try_))
433 {
434 pango_font_description_free (best_);
435 best_ = pango_font_description_from_string (descr_try.c_str ());
436 best_i = i;
437 }
438 pango_font_description_free(try_);
439 }
440 pango_font_description_free(descr_);
441 pango_font_description_free(best_);
443 GtkTreePath *path_c = gtk_tree_path_new ();
444 gtk_tree_path_append_index (path_c, best_i);
445 gtk_tree_selection_select_path (gtk_tree_view_get_selection (GTK_TREE_VIEW (fsel->style_treeview)), path_c);
446 gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (fsel->style_treeview), path_c, NULL, TRUE, 0.5, 0.5);
448 if (size != fsel->fontsize)
449 {
450 gchar s[8];
451 g_snprintf (s, 8, "%.5g", size); // UI, so printf is ok
452 gtk_entry_set_text (GTK_ENTRY (GTK_BIN(fsel->size)->child), s);
453 fsel->fontsize = size;
454 }
455 }
456 }
458 font_instance* sp_font_selector_get_font(SPFontSelector *fsel)
459 {
460 if (fsel->font) {
461 fsel->font->Ref();
462 }
464 return fsel->font;
465 }
467 double sp_font_selector_get_size(SPFontSelector *fsel)
468 {
469 return fsel->fontsize;
470 }
472 /* SPFontPreview */
474 struct SPFontPreview
475 {
476 GtkDrawingArea darea;
478 font_instance *font;
479 raster_font *rfont;
480 gchar *phrase;
481 unsigned long rgba;
482 };
484 struct SPFontPreviewClass
485 {
486 GtkDrawingAreaClass parent_class;
487 };
489 static void sp_font_preview_class_init(SPFontPreviewClass *c);
490 static void sp_font_preview_init(SPFontPreview *fsel);
491 static void sp_font_preview_destroy(GtkObject *object);
493 void sp_font_preview_size_request(GtkWidget *widget, GtkRequisition *req);
494 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event);
496 static GtkDrawingAreaClass *fp_parent_class = NULL;
498 GtkType sp_font_preview_get_type()
499 {
500 static GtkType type = 0;
501 if (!type) {
502 static const GtkTypeInfo info = {
503 "SPFontPreview",
504 sizeof (SPFontPreview),
505 sizeof (SPFontPreviewClass),
506 (GtkClassInitFunc) sp_font_preview_class_init,
507 (GtkObjectInitFunc) sp_font_preview_init,
508 NULL, NULL, NULL
509 };
510 type = gtk_type_unique (GTK_TYPE_DRAWING_AREA, &info);
511 }
512 return type;
513 }
515 static void sp_font_preview_class_init (SPFontPreviewClass *c)
516 {
517 GtkObjectClass *object_class = (GtkObjectClass *) c;
518 GtkWidgetClass *widget_class = (GtkWidgetClass *) c;
520 fp_parent_class = (GtkDrawingAreaClass*) gtk_type_class(GTK_TYPE_DRAWING_AREA);
522 object_class->destroy = sp_font_preview_destroy;
524 widget_class->size_request = sp_font_preview_size_request;
525 widget_class->expose_event = sp_font_preview_expose;
526 }
528 static void sp_font_preview_init(SPFontPreview *fprev)
529 {
530 fprev->rgba = 0x000000ff;
531 }
533 static void sp_font_preview_destroy(GtkObject *object)
534 {
535 SPFontPreview *fprev = SP_FONT_PREVIEW (object);
537 if (fprev->rfont) {
538 fprev->rfont->Unref();
539 fprev->rfont = NULL;
540 }
542 if (fprev->font) {
543 fprev->font->Unref();
544 fprev->font = NULL;
545 }
547 g_free(fprev->phrase);
548 fprev->phrase = NULL;
550 if (GTK_OBJECT_CLASS (fp_parent_class)->destroy) {
551 GTK_OBJECT_CLASS (fp_parent_class)->destroy(object);
552 }
553 }
555 void sp_font_preview_size_request(GtkWidget */*widget*/, GtkRequisition *req)
556 {
557 req->width = 256;
558 req->height = 32;
559 }
561 #define SPFP_MAX_LEN 64
563 static gint sp_font_preview_expose(GtkWidget *widget, GdkEventExpose *event)
564 {
565 SPFontPreview *fprev = SP_FONT_PREVIEW(widget);
567 if (GTK_WIDGET_DRAWABLE (widget)) {
568 if (fprev->rfont) {
570 int glyphs[SPFP_MAX_LEN];
571 double hpos[SPFP_MAX_LEN];
573 font_instance *tface = fprev->rfont->daddy;
575 double theSize = NR_MATRIX_DF_EXPANSION (&fprev->rfont->style.transform);
577 gchar const *p;
578 if (fprev->phrase) {
579 p = fprev->phrase;
580 } else {
581 /* TRANSLATORS: Test string used in text and font dialog (when no
582 * text has been entered) to get a preview of the font. Choose
583 * some representative characters that users of your locale will be
584 * interested in. */
585 p = _("AaBbCcIiPpQq12369$\342\202\254\302\242?.;/()");
586 }
587 int len = 0;
589 NRRect bbox;
590 bbox.x0 = bbox.y0 = bbox.x1 = bbox.y1 = 0.0;
592 text_wrapper* str_text=new text_wrapper;
593 str_text->SetDefaultFont(tface);
594 str_text->AppendUTF8(p,-1);
595 if ( str_text->uni32_length > 0 ) {
596 str_text->DoLayout();
597 if ( str_text->glyph_length > 0 ) {
598 PangoFont *curPF = NULL;
599 font_instance *curF = NULL;
600 for (int i = 0; i < str_text->glyph_length && i < SPFP_MAX_LEN; i++) {
601 if ( str_text->glyph_text[i].font != curPF ) {
602 curPF = str_text->glyph_text[i].font;
603 if (curF) {
604 curF->Unref();
605 }
606 curF = NULL;
607 if ( curPF ) {
608 PangoFontDescription* pfd = pango_font_describe(curPF);
609 curF = (font_factory::Default())->Face(pfd);
610 pango_font_description_free(pfd);
611 }
612 }
613 NR::Point base_pt(str_text->glyph_text[i].x, str_text->glyph_text[i].y);
614 base_pt *= theSize;
616 glyphs[len] = str_text->glyph_text[i].gl;
617 hpos[len] = base_pt[0];
618 len++;
619 if ( curF ) {
620 NR::Maybe<NR::Rect> nbbox = curF->BBox(str_text->glyph_text[i].gl);
621 if (nbbox) {
622 bbox.x0 = MIN(bbox.x0, base_pt[NR::X] + theSize * (nbbox->min())[0]);
623 bbox.y0 = MIN(bbox.y0, base_pt[NR::Y] - theSize * (nbbox->max())[1]);
624 bbox.x1 = MAX(bbox.x1, base_pt[NR::X] + theSize * (nbbox->max())[0]);
625 bbox.y1 = MAX(bbox.y1, base_pt[NR::Y] - theSize * (nbbox->min())[1]);
626 }
627 }
628 }
629 if ( curF ) {
630 curF->Unref();
631 }
632 }
633 }
635 // XXX: FIXME: why does this code ignore adv.y
636 /* while (p && *p && (len < SPFP_MAX_LEN)) {
637 unsigned int unival;
638 NRRect gbox;
639 unival = g_utf8_get_char (p);
640 glyphs[len] = tface->MapUnicodeChar( unival);
641 hpos[len] = (int)px;
642 NR::Point adv = fprev->rfont->Advance(glyphs[len]);
643 fprev->rfont->BBox( glyphs[len], &gbox);
644 bbox.x0 = MIN (px + gbox.x0, bbox.x0);
645 bbox.y0 = MIN (py + gbox.y0, bbox.y0);
646 bbox.x1 = MAX (px + gbox.x1, bbox.x1);
647 bbox.y1 = MAX (py + gbox.y1, bbox.y1);
648 px += adv[NR::X];
649 len += 1;
650 p = g_utf8_next_char (p);
651 }*/
653 float startx = (widget->allocation.width - (bbox.x1 - bbox.x0)) / 2;
654 float starty = widget->allocation.height - (widget->allocation.height - (bbox.y1 - bbox.y0)) / 2 - bbox.y1;
656 for (int y = event->area.y; y < event->area.y + event->area.height; y += 64) {
657 for (int x = event->area.x; x < event->area.x + event->area.width; x += 64) {
658 NRPixBlock pb, m;
659 int x0 = x;
660 int y0 = y;
661 int x1 = MIN(x0 + 64, event->area.x + event->area.width);
662 int y1 = MIN(y0 + 64, event->area.y + event->area.height);
663 guchar *ps = nr_pixelstore_16K_new (TRUE, 0xff);
664 nr_pixblock_setup_extern(&pb, NR_PIXBLOCK_MODE_R8G8B8, x0, y0, x1, y1, ps, 3 * (x1 - x0), FALSE, FALSE);
665 nr_pixblock_setup_fast(&m, NR_PIXBLOCK_MODE_A8, x0, y0, x1, y1, TRUE);
666 pb.empty = FALSE;
668 PangoFont *curPF = NULL;
669 font_instance *curF = NULL;
670 raster_font *curRF = NULL;
671 for (int i=0; i < len; i++) {
672 if ( str_text->glyph_text[i].font != curPF ) {
673 curPF=str_text->glyph_text[i].font;
674 if ( curF ) {
675 curF->Unref();
676 }
677 curF = NULL;
678 if ( curPF ) {
679 PangoFontDescription* pfd = pango_font_describe(curPF);
680 curF=(font_factory::Default())->Face(pfd);
681 pango_font_description_free(pfd);
682 }
683 if ( curF ) {
684 if ( curRF ) {
685 curRF->Unref();
686 }
687 curRF = NULL;
688 curRF = curF->RasterFont(fprev->rfont->style);
689 }
690 }
691 raster_glyph *g = (curRF) ? curRF->GetGlyph(glyphs[i]) : NULL;
692 if ( g ) {
693 g->Blit(NR::Point(hpos[i] + startx, starty), m);
694 }
695 }
696 if (curRF) {
697 curRF->Unref();
698 }
699 if (curF) {
700 curF->Unref();
701 }
703 nr_blit_pixblock_mask_rgba32(&pb, &m, fprev->rgba);
704 gdk_draw_rgb_image(widget->window, widget->style->black_gc,
705 x0, y0, x1 - x0, y1 - y0,
706 GDK_RGB_DITHER_NONE, NR_PIXBLOCK_PX (&pb), pb.rs);
707 nr_pixblock_release(&m);
708 nr_pixblock_release(&pb);
709 nr_pixelstore_16K_free(ps);
710 }
711 }
713 delete str_text;
715 } else {
716 nr_gdk_draw_gray_garbage(widget->window, widget->style->black_gc,
717 event->area.x, event->area.y,
718 event->area.width, event->area.height);
719 }
720 }
722 return TRUE;
723 }
725 GtkWidget * sp_font_preview_new()
726 {
727 GtkWidget *w = (GtkWidget*) gtk_type_new(SP_TYPE_FONT_PREVIEW);
729 return w;
730 }
732 void sp_font_preview_set_font(SPFontPreview *fprev, font_instance *font, SPFontSelector *fsel)
733 {
734 if (font)
735 {
736 font->Ref();
737 }
739 if (fprev->font)
740 {
741 fprev->font->Unref();
742 }
744 fprev->font = font;
746 if (fprev->rfont)
747 {
748 fprev->rfont->Unref();
749 fprev->rfont=NULL;
750 }
752 if (fprev->font)
753 {
754 NRMatrix flip;
755 nr_matrix_set_scale (&flip, fsel->fontsize, -fsel->fontsize);
756 fprev->rfont = fprev->font->RasterFont(flip, 0);
757 }
759 if (GTK_WIDGET_DRAWABLE (fprev)) gtk_widget_queue_draw (GTK_WIDGET (fprev));
760 }
762 void sp_font_preview_set_rgba32(SPFontPreview *fprev, guint32 rgba)
763 {
764 fprev->rgba = rgba;
765 if (GTK_WIDGET_DRAWABLE (fprev)) {
766 gtk_widget_queue_draw (GTK_WIDGET (fprev));
767 }
768 }
770 void sp_font_preview_set_phrase(SPFontPreview *fprev, const gchar *phrase)
771 {
772 g_free (fprev->phrase);
773 if (phrase) {
774 fprev->phrase = g_strdup (phrase);
775 } else {
776 fprev->phrase = NULL;
777 }
778 if (GTK_WIDGET_DRAWABLE(fprev)) {
779 gtk_widget_queue_draw (GTK_WIDGET (fprev));
780 }
781 }
784 /*
785 Local Variables:
786 mode:c++
787 c-file-style:"stroustrup"
788 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
789 indent-tabs-mode:nil
790 fill-column:99
791 End:
792 */
793 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :