Code

fix compile
[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 <glibmm/i18n.h>
20 #include <string.h>
22 SvgFontDrawingArea::SvgFontDrawingArea(){
23         this->text = "";
24         this->svgfont = NULL;
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::set_size(int x, int y){
36     this->x = x;
37     this->y = y;
38     ((Gtk::Widget*) this)->set_size_request(x, y);
39 }
41 void SvgFontDrawingArea::redraw(){
42         ((Gtk::Widget*) this)->queue_draw();
43 }
45 bool SvgFontDrawingArea::on_expose_event (GdkEventExpose *event){
46   if (this->svgfont){
47     Glib::RefPtr<Gdk::Window> window = get_window();
48     Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
49     cr->set_font_face( Cairo::RefPtr<Cairo::FontFace>(new Cairo::FontFace(this->svgfont->get_font_face(), false /* does not have reference */)) );
50     cr->set_font_size (this->y-20);
51     cr->move_to (10, 10);
52     cr->show_text (this->text.c_str());
53   }
54   return TRUE;
55 }
57 namespace Inkscape {
58 namespace UI {
59 namespace Dialog {
61 /*** SvgFontsDialog ***/
63 GlyphComboBox::GlyphComboBox(){
64 }
66 void GlyphComboBox::update(SPFont* spfont){
67     if (spfont) {
68         this->clear();
69         for(SPObject* node = spfont->children; node; node=node->next){
70             if (SP_IS_GLYPH(node)){
71                 this->append_text(((SPGlyph*)node)->unicode);
72             }
73         }
74     }
75 }
77 void SvgFontsDialog::on_kerning_changed(){
78     if (this->kerning_pair){
79         this->kerning_pair->k = kerning_spin.get_value();
80         kerning_preview.redraw();
81         _font_da.redraw();
82     }
83 }
85 void SvgFontsDialog::on_glyphs_changed(){
86     std::string str1(first_glyph.get_active_text());
87     std::string str2(second_glyph.get_active_text());
88     kerning_preview.set_text((gchar*) (str1+str2).c_str());
89     kerning_preview.redraw();
92     //look for this kerning pair on the currently selected font
93     this->kerning_pair = NULL;
94     for(SPObject* node = this->get_selected_spfont()->children; node; node=node->next){
95         if (SP_IS_HKERN(node) && ((SPGlyphKerning*)node)->u1->contains((gchar) first_glyph.get_active_text().c_str()[0])
96                                   && ((SPGlyphKerning*)node)->u2->contains((gchar) second_glyph.get_active_text().c_str()[0]) ){
97             this->kerning_pair = (SPGlyphKerning*)node;
98             continue;
99         }
100     }
102 //TODO:
103     //if not found,
104       //create new kern node
105     if (this->kerning_pair)
106         kerning_spin.set_value(this->kerning_pair->k);
109 /* Add all fonts in the document to the combobox. */
110 void SvgFontsDialog::update_fonts()
112     SPDesktop* desktop = this->getDesktop();
113     SPDocument* document = sp_desktop_document(desktop);
114     const GSList* fonts = sp_document_get_resource_list(document, "font");
116     _model->clear();
117     for(const GSList *l = fonts; l; l = l->next) {
118         Gtk::TreeModel::Row row = *_model->append();
119         SPFont* f = (SPFont*)l->data;
120         row[_columns.spfont] = f;
121         row[_columns.svgfont] = new SvgFont(f);
122         const gchar* lbl = f->label();
123         const gchar* id = SP_OBJECT_ID(f);
124         row[_columns.label] = lbl ? lbl : (id ? id : "font");
125     }
128 void SvgFontsDialog::on_preview_text_changed(){
129     _font_da.set_text((gchar*) _preview_entry.get_text().c_str());
130     _font_da.set_text(_preview_entry.get_text());
131     _font_da.redraw();
134 void SvgFontsDialog::on_font_selection_changed(){
135     SPFont* spfont = this->get_selected_spfont();
136     SvgFont* svgfont = this->get_selected_svgfont();
137     first_glyph.update(spfont);
138     second_glyph.update(spfont);
139     kerning_preview.set_svgfont(svgfont);
140     _font_da.set_svgfont(svgfont);
141     _font_da.redraw();
143     int steps = 50;
144     double set_width = spfont->horiz_adv_x;
145     setwidth_spin.set_value(set_width);
147     kerning_spin.set_range(0,set_width);
148     kerning_spin.set_increments(int(set_width/steps),2*int(set_width/steps));
149     kerning_spin.set_value(0);
152 void SvgFontsDialog::on_setwidth_changed(){
153     SPFont* spfont = this->get_selected_spfont();
154     if (spfont){
155         spfont->horiz_adv_x = setwidth_spin.get_value();
156         //TODO: tell cairo that the glyphs cache has to be invalidated
157     }
160 SvgFont* SvgFontsDialog::get_selected_svgfont()
162     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
163     if(i)
164         return (*i)[_columns.svgfont];
165     return NULL;
168 SPFont* SvgFontsDialog::get_selected_spfont()
170     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
171     if(i)
172         return (*i)[_columns.spfont];
173     return NULL;
176 SvgFontsDialog::SvgFontsDialog()
177  : UI::Widget::Panel("", "dialogs.svgfonts", SP_VERB_DIALOG_SVG_FONTS)
179     Gtk::HBox* hbox = Gtk::manage(new Gtk::HBox());
180     hbox->add(_font_list);
181     hbox->add(_font_settings);
182     _getContents()->add(*hbox);
184 //List of SVGFonts declared in a document:
185     _model = Gtk::ListStore::create(_columns);
186     _font_list.set_model(_model);
187     _font_list.append_column_editable(_("_Font"), _columns.label);
188     _font_list.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_font_selection_changed));
190     this->update_fonts();
192 //kerning setup:
193     Gtk::VBox* kernvbox = Gtk::manage(new Gtk::VBox());
194     _font_settings.add(*kernvbox);
196 //Set Width (horiz_adv_x):
197     Gtk::HBox* setwidth_hbox = Gtk::manage(new Gtk::HBox());
198     setwidth_hbox->add(*Gtk::manage(new Gtk::Label(_("Set width (not working yet):"))));
199     setwidth_hbox->add(setwidth_spin);
201     setwidth_spin.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_setwidth_changed));
202     setwidth_spin.set_range(0, 4096);
203     setwidth_spin.set_increments(10, 100);
204     _font_settings.add(*setwidth_hbox);
206 //Kerning Setup:
207     kernvbox->add(*Gtk::manage(new Gtk::Label(_("Kerning Setup:"))));
208     Gtk::HBox* kerning_selector = Gtk::manage(new Gtk::HBox());
209     kerning_selector->add(*Gtk::manage(new Gtk::Label(_("1st Glyph:"))));
210     kerning_selector->add(first_glyph);
211     kerning_selector->add(*Gtk::manage(new Gtk::Label(_("2nd Glyph:"))));
212     kerning_selector->add(second_glyph);
213     first_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
214     second_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
215     kerning_spin.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_kerning_changed));
217     kernvbox->add(*kerning_selector);
218     kernvbox->add((Gtk::Widget&) kerning_preview);
220     Gtk::HBox* kerning_amount_hbox = Gtk::manage(new Gtk::HBox());
221     kernvbox->add(*kerning_amount_hbox);
222     kerning_amount_hbox->add(*Gtk::manage(new Gtk::Label(_("Kerning value:"))));
223     kerning_amount_hbox->add(kerning_spin);
225     kerning_preview.set_size(300 + 20, 150 + 20);
226     _font_da.set_size(150 + 20, 50 + 20);
228 //Text Preview:
229     _preview_entry.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_preview_text_changed));
230     _getContents()->add((Gtk::Widget&) _font_da);
231     _preview_entry.set_text("Sample Text");
232     _font_da.set_text("Sample Text");
234     Gtk::HBox* preview_entry_hbox = Gtk::manage(new Gtk::HBox());
235     _getContents()->add(*preview_entry_hbox);
236     preview_entry_hbox->add(*Gtk::manage(new Gtk::Label(_("Preview Text:"))));
237     preview_entry_hbox->add(_preview_entry);
239     _getContents()->show_all();
242 SvgFontsDialog::~SvgFontsDialog(){}
244 } // namespace Dialog
245 } // namespace UI
246 } // namespace Inkscape
248 #endif //#ifdef ENABLE_SVG_FONTS