Code

improving SVG Fonts UI
[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 }
26 void SvgFontDrawingArea::set_svgfont(SvgFont* svgfont){
27         this->svgfont = svgfont;
28 }
30 void SvgFontDrawingArea::set_text(Glib::ustring text){
31         this->text = text;
32 }
34 void SvgFontDrawingArea::set_size(int x, int y){
35     this->x = x;
36     this->y = y;
37     ((Gtk::Widget*) this)->set_size_request(x, y);
38 }
40 void SvgFontDrawingArea::redraw(){
41         ((Gtk::Widget*) this)->queue_draw();
42 }
44 bool SvgFontDrawingArea::on_expose_event (GdkEventExpose *event){
45   if (this->svgfont){
46     Glib::RefPtr<Gdk::Window> window = get_window();
47     Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
48     cr->set_font_face( Cairo::RefPtr<Cairo::FontFace>(new Cairo::FontFace(this->svgfont->get_font_face(), false /* does not have reference */)) );
49     cr->set_font_size (this->y-20);
50     cr->move_to (10, 10);
51     cr->show_text (this->text.c_str());
52   }
53   return TRUE;
54 }
56 namespace Inkscape {
57 namespace UI {
58 namespace Dialog {
60 /*** SvgFontsDialog ***/
62 GlyphComboBox::GlyphComboBox(){
63 }
65 void GlyphComboBox::update(SPFont* spfont){
66     if (spfont) {
67         this->clear();
68         for(SPObject* node = spfont->children; node; node=node->next){
69             if (SP_IS_GLYPH(node)){
70                 this->append_text(((SPGlyph*)node)->unicode);
71             }
72         }
73     }
74 }
76 void SvgFontsDialog::on_kerning_changed(){
77     if (this->kerning_pair){
78         this->kerning_pair->k = kerning_spin.get_value();
79         kerning_preview.redraw();
80         _font_da.redraw();
81     }
82 }
84 void SvgFontsDialog::on_glyphs_changed(){
85     std::string str1(first_glyph.get_active_text());
86     std::string str2(second_glyph.get_active_text());
87     kerning_preview.set_text((gchar*) (str1+str2).c_str());
88     kerning_preview.redraw();
91     //look for this kerning pair on the currently selected font
92     this->kerning_pair = NULL;
93     for(SPObject* node = this->get_selected_spfont()->children; node; node=node->next){
94         if (SP_IS_HKERN(node) && ((SPGlyphKerning*)node)->u1->contains((gchar) first_glyph.get_active_text().c_str()[0])
95                                   && ((SPGlyphKerning*)node)->u2->contains((gchar) second_glyph.get_active_text().c_str()[0]) ){
96             this->kerning_pair = (SPGlyphKerning*)node;
97             continue;
98         }
99     }
101 //TODO:
102     //if not found,
103       //create new kern node
104     if (this->kerning_pair)
105         kerning_spin.set_value(this->kerning_pair->k);
108 /* Add all fonts in the document to the combobox. */
109 void SvgFontsDialog::update_fonts()
111     SPDesktop* desktop = this->getDesktop();
112     SPDocument* document = sp_desktop_document(desktop);
113     const GSList* fonts = sp_document_get_resource_list(document, "font");
115     _model->clear();
116     for(const GSList *l = fonts; l; l = l->next) {
117         Gtk::TreeModel::Row row = *_model->append();
118         SPFont* f = (SPFont*)l->data;
119         row[_columns.spfont] = f;
120         row[_columns.svgfont] = new SvgFont(f);
121         const gchar* lbl = f->label();
122         const gchar* id = SP_OBJECT_ID(f);
123         row[_columns.label] = lbl ? lbl : (id ? id : "font");
124     }
127 void SvgFontsDialog::on_preview_text_changed(){
128     _font_da.set_text((gchar*) _preview_entry.get_text().c_str());
129     _font_da.set_text(_preview_entry.get_text());
130     _font_da.redraw();
133 void SvgFontsDialog::on_font_selection_changed(){
134     SPFont* spfont = this->get_selected_spfont();
135     SvgFont* svgfont = this->get_selected_svgfont();
136     first_glyph.update(spfont);
137     second_glyph.update(spfont);
138     kerning_preview.set_svgfont(svgfont);
139     _font_da.set_svgfont(svgfont);
140     _font_da.redraw();
142     int steps = 50;
143     double set_width = spfont->horiz_adv_x;
144     kerning_spin.set_range(0,set_width);
145     kerning_spin.set_increments(int(set_width/steps),2*int(set_width/steps));
146     kerning_spin.set_value(0);
149 SvgFont* SvgFontsDialog::get_selected_svgfont()
151     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
152     if(i)
153         return (*i)[_columns.svgfont];
154     return NULL;
157 SPFont* SvgFontsDialog::get_selected_spfont()
159     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
160     if(i)
161         return (*i)[_columns.spfont];
162     return NULL;
165 SvgFontsDialog::SvgFontsDialog()
166  : UI::Widget::Panel("", "dialogs.svgfonts", SP_VERB_DIALOG_SVG_FONTS)
168     Gtk::HBox* hbox = Gtk::manage(new Gtk::HBox());
169     hbox->add(_font_list);
170     hbox->add(_font_settings);
171     _getContents()->add(*hbox);
173 //List of SVGFonts declared in a document:
174     _model = Gtk::ListStore::create(_columns);
175     _font_list.set_model(_model);
176     _font_list.append_column_editable("_Font", _columns.label);
177     _font_list.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_font_selection_changed));
179     this->update_fonts();
181 //kerning setup:
182     Gtk::VBox* kernvbox = Gtk::manage(new Gtk::VBox());
183     _font_settings.add(*kernvbox);
184     kernvbox->add(*Gtk::manage(new Gtk::Label("Kerning Setup:")));
185     Gtk::HBox* kerning_selector = Gtk::manage(new Gtk::HBox());
186     kerning_selector->add(*Gtk::manage(new Gtk::Label("1st Glyph:")));
187     kerning_selector->add(first_glyph);
188     kerning_selector->add(*Gtk::manage(new Gtk::Label("2nd Glyph:")));
189     kerning_selector->add(second_glyph);
190     first_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
191     second_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
192     kerning_spin.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_kerning_changed));
194     kernvbox->add(*kerning_selector);
195     kernvbox->add((Gtk::Widget&) kerning_preview);
197     Gtk::HBox* kerning_amount_hbox = Gtk::manage(new Gtk::HBox());
198     kernvbox->add(*kerning_amount_hbox);
199     kerning_amount_hbox->add(*Gtk::manage(new Gtk::Label("Kerning value:")));
200     kerning_amount_hbox->add(kerning_spin);
202     kerning_preview.set_size(300 + 20, 150 + 20);
203     _font_da.set_size(150 + 20, 50 + 20);
205 //Text Preview:
206     _preview_entry.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_preview_text_changed));
207     _getContents()->add((Gtk::Widget&) _font_da);
208     _preview_entry.set_text("Sample Text");
209     _font_da.set_text("Sample Text");
211     Gtk::HBox* preview_entry_hbox = Gtk::manage(new Gtk::HBox());
212     _getContents()->add(*preview_entry_hbox);
213     preview_entry_hbox->add(*Gtk::manage(new Gtk::Label("Preview Text:")));
214     preview_entry_hbox->add(_preview_entry);
216     _getContents()->show_all();
219 SvgFontsDialog::~SvgFontsDialog(){}
221 } // namespace Dialog
222 } // namespace UI
223 } // namespace Inkscape
225 #endif //#ifdef ENABLE_SVG_FONTS