Code

now one can visually adjust glyph kerning. (still doesn save that info in the file...
[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_kerning_changed(){
72     //set kerning value = spin.value()
73     this->kerning_pair->k = kerning_spin.get_value();
74     kerning_preview.redraw();
75     _font_da.redraw();
76 }
78 void SvgFontsDialog::on_glyphs_changed(){
79     std::string str1(first_glyph.get_active_text());
80     std::string str2(second_glyph.get_active_text());
81     kerning_preview.set_text((gchar*) (str1+str2).c_str());
82     kerning_preview.redraw();
85     //look for this kerning pair on the currently selected font
86     this->kerning_pair = NULL;
87     for(SPObject* node = this->get_selected_spfont()->children; node; node=node->next){
88         if (SP_IS_HKERN(node) && ((SPGlyphKerning*)node)->u1->contains((gchar) first_glyph.get_active_text().c_str()[0])
89                                   && ((SPGlyphKerning*)node)->u2->contains((gchar) second_glyph.get_active_text().c_str()[0]) ){
90             this->kerning_pair = (SPGlyphKerning*)node;
91             continue;
92         }
93     }
95 //TODO:
96     //if not found,
97       //create new kern node
98 }
100 /* Add all fonts in the document to the combobox. */
101 void SvgFontsDialog::update_fonts()
103     SPDesktop* desktop = this->getDesktop();
104     SPDocument* document = sp_desktop_document(desktop);
105     const GSList* fonts = sp_document_get_resource_list(document, "font");
107     _model->clear();
108     for(const GSList *l = fonts; l; l = l->next) {
109         Gtk::TreeModel::Row row = *_model->append();
110         SPFont* f = (SPFont*)l->data;
111         row[_columns.spfont] = f;
112         row[_columns.svgfont] = new SvgFont(f);
113         const gchar* lbl = f->label();
114         const gchar* id = SP_OBJECT_ID(f);
115         row[_columns.label] = lbl ? lbl : (id ? id : "font");
116     }
119 void SvgFontsDialog::on_preview_text_changed(){
120     _font_da.set_text((gchar*) _preview_entry.get_text().c_str());
121     _font_da.set_text(_preview_entry.get_text());
122     _font_da.redraw();
125 void SvgFontsDialog::on_font_selection_changed(){
126     first_glyph.update(this->get_selected_spfont());
127     second_glyph.update(this->get_selected_spfont());
128     kerning_preview.set_svgfont(this->get_selected_svgfont());
129     _font_da.set_svgfont(this->get_selected_svgfont());
130     _font_da.redraw();
133 SvgFont* SvgFontsDialog::get_selected_svgfont()
135     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
136     if(i)
137         return (*i)[_columns.svgfont];
138     return NULL;
141 SPFont* SvgFontsDialog::get_selected_spfont()
143     Gtk::TreeModel::iterator i = _font_list.get_selection()->get_selected();
144     if(i)
145         return (*i)[_columns.spfont];
146     return NULL;
149 SvgFontsDialog::SvgFontsDialog()
150  : UI::Widget::Panel("", "dialogs.svgfonts", SP_VERB_DIALOG_SVG_FONTS)
152     Gtk::HBox* hbox = Gtk::manage(new Gtk::HBox());
153     hbox->add(_font_list);
154     hbox->add(_font_settings);
155     _getContents()->add(*hbox);
157 //List of SVGFonts declared in a document:
158     _model = Gtk::ListStore::create(_columns);
159     _font_list.set_model(_model);
160     _font_list.append_column_editable("_Font", _columns.label);
161     _font_list.get_selection()->signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_font_selection_changed));
163     this->update_fonts();
165 //kerning setup:
166     Gtk::VBox* kernvbox = Gtk::manage(new Gtk::VBox());
167     _font_settings.add(*kernvbox);
168     kernvbox->add(*Gtk::manage(new Gtk::Label("Kerning Setup:")));
169     Gtk::HBox* kerning_selector = Gtk::manage(new Gtk::HBox());
170     kerning_selector->add(first_glyph);
171     kerning_selector->add(second_glyph);
172     kerning_spin.set_range(0,1000);
173     kerning_spin.set_increments(10,20);
174     first_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
175     second_glyph.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_glyphs_changed));
176     kerning_spin.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_kerning_changed));
178     kernvbox->add(*kerning_selector);
179     kernvbox->add((Gtk::Widget&) kerning_preview);
180     kernvbox->add(kerning_spin);
182 //Text Preview:
183     _preview_entry.signal_changed().connect(sigc::mem_fun(*this, &SvgFontsDialog::on_preview_text_changed));
184     _getContents()->add(*Gtk::manage(new Gtk::Label("Preview Text:")));
185     _getContents()->add((Gtk::Widget&) _font_da);
186     _preview_entry.set_text("Sample Text");
187     _font_da.set_text("Sample Text");
188     _getContents()->add(_preview_entry);
189     _getContents()->show_all();
192 SvgFontsDialog::~SvgFontsDialog(){}
194 } // namespace Dialog
195 } // namespace UI
196 } // namespace Inkscape
198 #endif //#ifdef ENABLE_SVG_FONTS