1 #include "config.h"
2 #ifdef ENABLE_SVG_FONTS
3 /*
4 * SVGFonts rendering implementation
5 *
6 * Authors:
7 * Felipe C. da S. Sanches <felipe.sanches@gmail.com>
8 *
9 * Copyright (C) 2008 Felipe C. da S. Sanches
10 *
11 * Released under GNU GPL version 2 or later.
12 * Read the file 'COPYING' for more information.
13 */
15 #include <2geom/pathvector.h>
16 #include <2geom/transforms.h>
17 #include "../style.h"
18 #include <cairo.h>
19 #include <vector>
20 #include "svg/svg.h"
21 #include "inkscape-cairo.h"
22 #include "nr-svgfonts.h"
24 //*************************//
25 // UserFont Implementation //
26 //*************************//
28 // I wrote this binding code because Cairomm does not yet support userfonts. I have moved this code to cairomm and sent them a patch.
29 // Once Cairomm incorporate the UserFonts binding, this code should be removed from inkscape and Cairomm API should be used.
31 static cairo_user_data_key_t key;
33 static cairo_status_t font_init_cb (cairo_scaled_font_t *scaled_font,
34 cairo_font_extents_t *metrics){
35 cairo_font_face_t* face;
36 face = cairo_scaled_font_get_font_face(scaled_font);
37 SvgFont* instance = (SvgFont*) cairo_font_face_get_user_data(face, &key);
38 return instance->scaled_font_init(scaled_font, metrics);
39 }
41 static cairo_status_t font_text_to_glyphs_cb (cairo_scaled_font_t *scaled_font,
42 const char *utf8,
43 cairo_glyph_t **glyphs,
44 int *num_glyphs){
45 cairo_font_face_t* face;
46 face = cairo_scaled_font_get_font_face(scaled_font);
47 SvgFont* instance = (SvgFont*) cairo_font_face_get_user_data(face, &key);
48 return instance->scaled_font_text_to_glyphs(scaled_font, utf8, glyphs, num_glyphs);
49 }
51 static cairo_status_t font_render_glyph_cb (cairo_scaled_font_t *scaled_font,
52 unsigned long glyph,
53 cairo_t *cr,
54 cairo_text_extents_t *metrics){
55 cairo_font_face_t* face;
56 face = cairo_scaled_font_get_font_face(scaled_font);
57 SvgFont* instance = (SvgFont*) cairo_font_face_get_user_data(face, &key);
58 return instance->scaled_font_render_glyph(scaled_font, glyph, cr, metrics);
59 }
61 UserFont::UserFont(SvgFont* instance){
62 this->face = cairo_user_font_face_create ();
63 cairo_user_font_face_set_init_func (this->face, font_init_cb);
64 cairo_user_font_face_set_render_glyph_func (this->face, font_render_glyph_cb);
65 cairo_user_font_face_set_text_to_glyphs_func (this->face, font_text_to_glyphs_cb);
67 cairo_font_face_set_user_data (this->face, &key, (void*)instance, (cairo_destroy_func_t) NULL);
68 }
70 //******************************//
71 // SvgFont class Implementation //
72 //******************************//
73 SvgFont::SvgFont(SPFont* spfont){
74 this->font = spfont;
75 this->missingglyph = NULL;
76 this->userfont = NULL;
77 }
79 cairo_status_t
80 SvgFont::scaled_font_init (cairo_scaled_font_t *scaled_font,
81 cairo_font_extents_t *metrics)
82 {
83 //TODO
84 // metrics->ascent = .75;
85 // metrics->descent = .25;
86 return CAIRO_STATUS_SUCCESS;
87 }
89 unsigned int compare_them(char* s1, char* s2){
90 unsigned int p=0;
91 while((s1[p] == s2[p]) && s1[p] != '\0' && s2[p] != '\0') p++;
92 if (s1[p]=='\0') return p;
93 else return 0;
94 }
96 cairo_status_t
97 SvgFont::scaled_font_text_to_glyphs (cairo_scaled_font_t *scaled_font,
98 const char *utf8,
99 cairo_glyph_t **glyphs,
100 int *num_glyphs)
101 {
102 //This function receives a text string to be rendered. It then defines what is the sequence of glyphs that
103 // is used to properly render this string. It alse defines the respective coordinates of each glyph. Thus, it
104 // has to read the attributes od the SVGFont hkern and vkern nodes in order to adjust the glyph kerning.
105 //It also determines the usage of the missing-glyph in portions of the string that does not match any of the declared glyphs.
107 unsigned long i;
108 int count = 0;
109 char* _utf8 = (char*) utf8;
110 unsigned int len;
112 //First we findout whats the worst case number of glyphs.
113 while(_utf8[0] != '\0'){
114 _utf8++;
115 count++;
116 }
118 //We use that info to allocate memory for the glyphs
119 *glyphs = (cairo_glyph_t*) malloc(count*sizeof(cairo_glyph_t));
121 char* previous_unicode = NULL; //This is used for kerning
122 gchar* previous_glyph_name = NULL; //This is used for kerning
124 count=0;
125 double x=0, y=0;//These vars store the position of the glyph within the rendered string
126 bool is_horizontal_text = true; //TODO
127 _utf8 = (char*) utf8;
128 while(_utf8[0] != '\0'){
129 len = 0;
130 for (i=0; i < (unsigned long) this->glyphs.size(); i++){
131 if ( (len = compare_them(this->glyphs[i]->unicode, _utf8)) ){
132 //check whether is there a glyph declared on the SVG document
133 // that matches with the text string in its current position
134 for(SPObject* node = this->font->children;previous_unicode && node;node=node->next){
135 //apply glyph kerning if appropriate
136 if (SP_IS_HKERN(node) && is_horizontal_text){
137 if ( (((SPHkern*)node)->u1->contains(previous_unicode[0])
138 || ((SPHkern*)node)->g1->contains(previous_glyph_name)) &&
139 (((SPHkern*)node)->u2->contains(this->glyphs[i]->unicode[0])
140 || ((SPHkern*)node)->g2->contains(this->glyphs[i]->glyph_name))
141 )//TODO: verify what happens when using unicode strings.
142 x -= (((SPHkern*)node)->k / this->font->horiz_adv_x);
143 }
144 if (SP_IS_VKERN(node) && !is_horizontal_text){
145 if ( (((SPVkern*)node)->u1->contains(previous_unicode[0])
146 || ((SPVkern*)node)->g1->contains(previous_glyph_name)) &&
147 (((SPVkern*)node)->u2->contains(this->glyphs[i]->unicode[0])
148 || ((SPVkern*)node)->g2->contains(this->glyphs[i]->glyph_name))
149 )//TODO: idem
150 y -= (((SPVkern*)node)->k / this->font->vert_adv_y);
151 }
152 }
153 previous_unicode = this->glyphs[i]->unicode;//used for kerning checking
154 previous_glyph_name = this->glyphs[i]->glyph_name;//used for kerning checking
155 (*glyphs)[count].index = i;
156 (*glyphs)[count].x = x;
157 (*glyphs)[count++].y = y;
158 //advance glyph coordinates:
159 if (is_horizontal_text) x++;
160 else y++;
161 _utf8+=len; //advance 'len' chars in our string pointer
162 //continue;
163 goto dirty;
164 }
165 }
166 dirty:
167 if (!len){
168 (*glyphs)[count].index = i;
169 (*glyphs)[count].x = x;
170 (*glyphs)[count++].y = y;
171 //advance glyph coordinates:
172 if (is_horizontal_text) x++;
173 else y++;
174 _utf8++; //advance 1 char in our string pointer
175 }
176 }
177 *num_glyphs = count;
178 return CAIRO_STATUS_SUCCESS;
179 }
181 cairo_status_t
182 SvgFont::scaled_font_render_glyph (cairo_scaled_font_t *scaled_font,
183 unsigned long glyph,
184 cairo_t *cr,
185 cairo_text_extents_t *metrics)
186 {
187 // This method does the actual rendering of glyphs.
189 // We have glyphs.size() glyphs and possibly one missing-glyph declared on this SVG document
190 // The id of the missing-glyph is always equal to glyphs.size()
191 // All the other glyphs have ids ranging from 0 to glyphs.size()-1
193 if (glyph > this->glyphs.size()) return CAIRO_STATUS_SUCCESS;//TODO: this is an error!
195 SPObject* node;
196 if (glyph == this->glyphs.size()){
197 if (!this->missingglyph) return CAIRO_STATUS_SUCCESS;
198 node = (SPObject*) this->missingglyph;
199 g_warning("RENDER MISSING-GLYPH");
200 } else {
201 node = (SPObject*) this->glyphs[glyph];
202 g_warning("RENDER %s", this->glyphs[glyph]->unicode);
203 }
205 //glyphs can be described by arbitrary SVG declared in the childnodes of a glyph node
206 // or using the d attribute of a glyph node.
207 // pathv stores the path description from the d attribute:
208 Geom::PathVector pathv;
209 if (SP_IS_GLYPH(node) && ((SPGlyph*)node)->d) {
210 pathv = sp_svg_read_pathv(((SPGlyph*)node)->d);
211 } else if (SP_IS_MISSING_GLYPH(node) && ((SPMissingGlyph*)node)->d) {
212 pathv = sp_svg_read_pathv(((SPMissingGlyph*)node)->d);
213 } else {
214 return CAIRO_STATUS_SUCCESS; // FIXME: is this the right code to return?
215 }
217 if (!pathv.empty()){
218 //This glyph has a path description on its d attribute, so we render it:
219 cairo_new_path(cr);
220 Geom::Scale s(1.0/((SPFont*) node->parent)->horiz_adv_x);
221 NRRect area(0,0,1,1); //I need help here!
222 feed_pathvector_to_cairo (cr, pathv, s, area.upgrade(), false, 0);
223 cairo_fill(cr);
224 }
226 //TODO: render the SVG described on this glyph's child nodes.
227 return CAIRO_STATUS_SUCCESS;
228 }
230 cairo_font_face_t*
231 SvgFont::get_font_face(){
232 if (!this->userfont) {
233 for(SPObject* node = this->font->children;node;node=node->next){
234 if (SP_IS_GLYPH(node)){
235 g_warning("glyphs.push_back((SPGlyph*)node); (node->unicode='%s')", ((SPGlyph*)node)->unicode);
236 this->glyphs.push_back((SPGlyph*)node);
237 }
238 if (SP_IS_MISSING_GLYPH(node)){
239 g_warning("missingglyph=(SPMissingGlyph*)node;");
240 this->missingglyph=(SPMissingGlyph*)node;
241 }
242 }
243 this->userfont = new UserFont(this);
244 }
245 return this->userfont->face;
246 }
247 #endif //#ifdef ENABLE_SVG_FONTS