Code

d280853675d16e89bc1fff6919816fb241c73aea
[inkscape.git] / src / ui / dialog / svg-fonts-dialog.cpp
1 /**
2  * \brief SVG Fonts dialog
3  *
4  * Authors:
5  *   Felipe C. da S. Sanches <felipe.sanches@gmail.com>
6  *
7  * Copyright (C) 2008 Authors
8  *
9  * Released under GNU GPLv2 (or later).  Read the file 'COPYING' for more information.
10  */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
16 #ifdef ENABLE_SVG_FONTS
18 #include "svg-fonts-dialog.h"
19 #include <string.h>
21 SvgFontDrawingArea::SvgFontDrawingArea(){
22         this->text = "";
23         this->svgfont = NULL;
24         ((Gtk::Widget*) this)->set_size_request(150, 50);
25 }
27 void SvgFontDrawingArea::set_svgfont(SvgFont* svgfont){
28         this->svgfont = svgfont;
29 }
31 void SvgFontDrawingArea::set_text(Glib::ustring text){
32         this->text = text;
33 }
35 void SvgFontDrawingArea::redraw(){
36         ((Gtk::Widget*) this)->queue_draw();
37 }
39 bool SvgFontDrawingArea::on_expose_event (GdkEventExpose *event){
40   if (this->svgfont){
41     Glib::RefPtr<Gdk::Window> window = get_window();
42     Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
43     cr->set_font_face( Cairo::RefPtr<Cairo::FontFace>(new Cairo::FontFace(this->svgfont->get_font_face(), false /* does not have reference */)) );
44     cr->set_font_size (20);
45     cr->move_to (20, 20);
46     cr->show_text (this->text.c_str());
47   }
48   return TRUE;
49 }
51 namespace Inkscape {
52 namespace UI {
53 namespace Dialog {
55 /*** SvgFontsDialog ***/
57 GlyphComboBox::GlyphComboBox(){
58 }
60 void GlyphComboBox::update(SPFont* spfont){
61     if (spfont) {
62         this->clear();
63         for(SPObject* node = spfont->children; node; node=node->next){
64             if (SP_IS_GLYPH(node)){
65                 this->append_text(((SPGlyph*)node)->unicode);
66             }
67         }
68     }
69 }
71 void SvgFontsDialog::on_glyphs_changed(){
72     std::string str1(first_glyph.get_active_text());
73     std::string str2(second_glyph.get_active_text());
74     kerning_preview.set_text((gchar*) (str1+str2).c_str());
75     kerning_preview.redraw();
76 }
78 /* Add all fonts in the document to the combobox. */
79 void SvgFontsDialog::update_fonts()
80 {
81     SPDesktop* desktop = this->getDesktop();
82     SPDocument* document = sp_desktop_document(desktop);
83     const GSList* fonts = sp_document_get_resource_list(document, "font");
85     _model->clear();
86     for(const GSList *l = fonts; l; l = l->next) {
87         Gtk::TreeModel::Row row = *_model->append();
88         SPFont* f = (SPFont*)l->data;
89         row[_columns.spfont] = f;
90         row[_columns.svgfont] = new SvgFont(f);
91         const gchar* lbl = f->label();
92         const gchar* id = SP_OBJECT_ID(f);
93         row[_columns.label] = lbl ? lbl : (id ? id : "font");
94     }
95 }
97 void SvgFontsDialog::on_preview_text_changed(){
98     _font_da.set_text((gchar*) _preview_entry.get_text().c_str());
99     _font_da.set_text(_preview_entry.get_text());
100     _font_da.redraw();
103 void SvgFontsDialog::on_font_selection_changed(){
104     first_glyph.update(this->get_selected_spfont());
105     second_glyph.update(this->get_selected_spfont());
106     kerning_preview.set_svgfont(this->get_selected_svgfont());
107     _font_da.set_svgfont(this->get_selected_svgfont());
108     _font_da.redraw();
111 SvgFont* SvgFontsDialog::get_selected_svgfont()
113     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
114     if(i)
115         return (*i)[_columns.svgfont];
116     return NULL;
119 SPFont* SvgFontsDialog::get_selected_spfont()
121     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
122     if(i)
123         return (*i)[_columns.spfont];
124     return NULL;
127 SvgFontsDialog::SvgFontsDialog()
128  : UI::Widget::Panel("", "dialogs.svgfonts", SP_VERB_DIALOG_SVG_FONTS)
130     Gtk::HBox* hbox = Gtk::manage(new Gtk::HBox());
131     hbox->add(_font_list);
132     hbox->add(_font_settings);
133     _getContents()->add(*hbox);
135 //List of SVGFonts declared in a document:
136     _model = Gtk::ListStore::create(_columns);
137     _font_list.set_model(_model);
138     _font_list.append_column_editable("_Font", _columns.label);
139     _font_list.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_font_selection_changed));
141     this->update_fonts();
143 //kerning setup:
144     Gtk::VBox* kernvbox = Gtk::manage(new Gtk::VBox());
145     _font_settings.add(*kernvbox);
146     kernvbox->add(*Gtk::manage(new Gtk::Label("Kerning Setup:")));
147     Gtk::HBox* kerning_selector = Gtk::manage(new Gtk::HBox());
148     kerning_selector->add(first_glyph);
149     kerning_selector->add(second_glyph);
150     first_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
151     second_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
153     Gtk::SpinButton* kerning_spin = Gtk::manage(new Gtk::SpinButton());
154     kernvbox->add(*kerning_selector);
155     kernvbox->add((Gtk::Widget&) kerning_preview);
156     kernvbox->add(*kerning_spin);
158 //Text Preview:
159     _preview_entry.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_preview_text_changed));
160     _getContents()->add(*Gtk::manage(new Gtk::Label("Preview Text:")));
161     _getContents()->add((Gtk::Widget&) _font_da);
162     _preview_entry.set_text("Sample Text");
163     _font_da.set_text("Sample Text");
164     _getContents()->add(_preview_entry);
165     _getContents()->show_all();
168 SvgFontsDialog::~SvgFontsDialog(){}
170 } // namespace Dialog
171 } // namespace UI
172 } // namespace Inkscape
174 #endif //#ifdef ENABLE_SVG_FONTS