Code

ac9556e1433a953a402cf55c6b0b4de7d694c403
[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 <gtkmm/notebook.h>
19 #include "svg-fonts-dialog.h"
20 #include <glibmm/i18n.h>
21 #include <string.h>
23 SvgFontDrawingArea::SvgFontDrawingArea(){
24         this->text = "";
25         this->svgfont = NULL;
26 }
28 void SvgFontDrawingArea::set_svgfont(SvgFont* svgfont){
29         this->svgfont = svgfont;
30 }
32 void SvgFontDrawingArea::set_text(Glib::ustring text){
33         this->text = text;
34 }
36 void SvgFontDrawingArea::set_size(int x, int y){
37     this->x = x;
38     this->y = y;
39     ((Gtk::Widget*) this)->set_size_request(x, y);
40 }
42 void SvgFontDrawingArea::redraw(){
43         ((Gtk::Widget*) this)->queue_draw();
44 }
46 bool SvgFontDrawingArea::on_expose_event (GdkEventExpose *event){
47   if (this->svgfont){
48     Glib::RefPtr<Gdk::Window> window = get_window();
49     Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
50     cr->set_font_face( Cairo::RefPtr<Cairo::FontFace>(new Cairo::FontFace(this->svgfont->get_font_face(), false /* does not have reference */)) );
51     cr->set_font_size (this->y-20);
52     cr->move_to (10, 10);
53     cr->show_text (this->text.c_str());
54   }
55   return TRUE;
56 }
58 namespace Inkscape {
59 namespace UI {
60 namespace Dialog {
62 /*** SvgFontsDialog ***/
64 GlyphComboBox::GlyphComboBox(){
65 }
67 void GlyphComboBox::update(SPFont* spfont){
68     if (spfont) {
69         this->clear();
70         for(SPObject* node = spfont->children; node; node=node->next){
71             if (SP_IS_GLYPH(node)){
72                 this->append_text(((SPGlyph*)node)->unicode);
73             }
74         }
75     }
76 }
78 void SvgFontsDialog::on_kerning_changed(){
79     if (this->kerning_pair){
80         this->kerning_pair->k = kerning_spin.get_value();
81         kerning_preview.redraw();
82         _font_da.redraw();
83     }
84 }
86 void SvgFontsDialog::on_glyphs_changed(){
87     std::string str1(first_glyph.get_active_text());
88     std::string str2(second_glyph.get_active_text());
89     kerning_preview.set_text((gchar*) (str1+str2).c_str());
90     kerning_preview.redraw();
93     //look for this kerning pair on the currently selected font
94     this->kerning_pair = NULL;
95     for(SPObject* node = this->get_selected_spfont()->children; node; node=node->next){
96         if (SP_IS_HKERN(node) && ((SPGlyphKerning*)node)->u1->contains((gchar) first_glyph.get_active_text().c_str()[0])
97                                   && ((SPGlyphKerning*)node)->u2->contains((gchar) second_glyph.get_active_text().c_str()[0]) ){
98             this->kerning_pair = (SPGlyphKerning*)node;
99             continue;
100         }
101     }
103 //TODO:
104     //if not found,
105       //create new kern node
106     if (this->kerning_pair)
107         kerning_spin.set_value(this->kerning_pair->k);
110 /* Add all fonts in the document to the combobox. */
111 void SvgFontsDialog::update_fonts()
113     SPDesktop* desktop = this->getDesktop();
114     SPDocument* document = sp_desktop_document(desktop);
115     const GSList* fonts = sp_document_get_resource_list(document, "font");
117     _model->clear();
118     for(const GSList *l = fonts; l; l = l->next) {
119         Gtk::TreeModel::Row row = *_model->append();
120         SPFont* f = (SPFont*)l->data;
121         row[_columns.spfont] = f;
122         row[_columns.svgfont] = new SvgFont(f);
123         const gchar* lbl = f->label();
124         const gchar* id = SP_OBJECT_ID(f);
125         row[_columns.label] = lbl ? lbl : (id ? id : "font");
126     }
129 void SvgFontsDialog::on_preview_text_changed(){
130     _font_da.set_text((gchar*) _preview_entry.get_text().c_str());
131     _font_da.set_text(_preview_entry.get_text());
132     _font_da.redraw();
135 void SvgFontsDialog::on_font_selection_changed(){
136     SPFont* spfont = this->get_selected_spfont();
137     SvgFont* svgfont = this->get_selected_svgfont();
138     first_glyph.update(spfont);
139     second_glyph.update(spfont);
140     kerning_preview.set_svgfont(svgfont);
141     _font_da.set_svgfont(svgfont);
142     _font_da.redraw();
144     int steps = 50;
145     double set_width = spfont->horiz_adv_x;
146     setwidth_spin.set_value(set_width);
148     kerning_spin.set_range(0,set_width);
149     kerning_spin.set_increments(int(set_width/steps),2*int(set_width/steps));
150     kerning_spin.set_value(0);
153 void SvgFontsDialog::on_setwidth_changed(){
154     SPFont* spfont = this->get_selected_spfont();
155     if (spfont){
156         spfont->horiz_adv_x = setwidth_spin.get_value();
157         //TODO: tell cairo that the glyphs cache has to be invalidated
158     }
161 SvgFont* SvgFontsDialog::get_selected_svgfont()
163     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
164     if(i)
165         return (*i)[_columns.svgfont];
166     return NULL;
169 SPFont* SvgFontsDialog::get_selected_spfont()
171     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
172     if(i)
173         return (*i)[_columns.spfont];
174     return NULL;
177 Gtk::VBox* SvgFontsDialog::global_settings_tab(){
178     Gtk::VBox* global_vbox = Gtk::manage(new Gtk::VBox());
180 //Set Width (horiz_adv_x):
181     Gtk::HBox* setwidth_hbox = Gtk::manage(new Gtk::HBox());
182     setwidth_hbox->add(*Gtk::manage(new Gtk::Label(_("Set width:"))));
183     setwidth_hbox->add(setwidth_spin);
185     setwidth_spin.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_setwidth_changed));
186     setwidth_spin.set_range(0, 4096);
187     setwidth_spin.set_increments(10, 100);
188     global_vbox->add(*setwidth_hbox);
190     return global_vbox;
193 Gtk::VBox* SvgFontsDialog::glyphs_tab(){
194     Gtk::VBox* glyphs_vbox = Gtk::manage(new Gtk::VBox());
195     return glyphs_vbox;
198 Gtk::VBox* SvgFontsDialog::kerning_tab(){
200 //kerning setup:
201     Gtk::VBox* kernvbox = Gtk::manage(new Gtk::VBox());
203 //Kerning Setup:
204     kernvbox->add(*Gtk::manage(new Gtk::Label(_("Kerning Setup:"))));
205     Gtk::HBox* kerning_selector = Gtk::manage(new Gtk::HBox());
206     kerning_selector->add(*Gtk::manage(new Gtk::Label(_("1st Glyph:"))));
207     kerning_selector->add(first_glyph);
208     kerning_selector->add(*Gtk::manage(new Gtk::Label(_("2nd Glyph:"))));
209     kerning_selector->add(second_glyph);
210     first_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
211     second_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
212     kerning_spin.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_kerning_changed));
214     kernvbox->add(*kerning_selector);
215     kernvbox->add((Gtk::Widget&) kerning_preview);
217     Gtk::HBox* kerning_amount_hbox = Gtk::manage(new Gtk::HBox());
218     kernvbox->add(*kerning_amount_hbox);
219     kerning_amount_hbox->add(*Gtk::manage(new Gtk::Label(_("Kerning value:"))));
220     kerning_amount_hbox->add(kerning_spin);
222     kerning_preview.set_size(300 + 20, 150 + 20);
223     _font_da.set_size(150 + 20, 50 + 20);
225     return kernvbox;
228 SvgFontsDialog::SvgFontsDialog()
229  : UI::Widget::Panel("", "dialogs.svgfonts", SP_VERB_DIALOG_SVG_FONTS)
231     Gtk::HBox* hbox = Gtk::manage(new Gtk::HBox());
232     hbox->add(_font_list);
233     hbox->add(_font_settings);
234     _getContents()->add(*hbox);
236 //List of SVGFonts declared in a document:
237     _model = Gtk::ListStore::create(_columns);
238     _font_list.set_model(_model);
239     _font_list.append_column_editable(_("_Font"), _columns.label);
240     _font_list.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_font_selection_changed));
242     this->update_fonts();
244     Gtk::Notebook *tabs = Gtk::manage(new Gtk::Notebook());
245     tabs->set_scrollable();
247     tabs->append_page(*global_settings_tab(), _("_Global Settings"), true);
248     tabs->append_page(*glyphs_tab(), _("_Glyphs"), true);
249     tabs->append_page(*kerning_tab(), _("_Kerning"), true);
251     _font_settings.add(*tabs);
253 //Text Preview:
254     _preview_entry.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_preview_text_changed));
255     _getContents()->add((Gtk::Widget&) _font_da);
256     _preview_entry.set_text("Sample Text");
257     _font_da.set_text("Sample Text");
259     Gtk::HBox* preview_entry_hbox = Gtk::manage(new Gtk::HBox());
260     _getContents()->add(*preview_entry_hbox);
261     preview_entry_hbox->add(*Gtk::manage(new Gtk::Label(_("Preview Text:"))));
262     preview_entry_hbox->add(_preview_entry);
264     _getContents()->show_all();
267 SvgFontsDialog::~SvgFontsDialog(){}
269 } // namespace Dialog
270 } // namespace UI
271 } // namespace Inkscape
273 #endif //#ifdef ENABLE_SVG_FONTS