1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* SVGFonts rendering implementation
*
* Authors:
* Felipe C. da S. Sanches <juca@members.fsf.org>
* Jon A. Cruz <jon@joncruz.org>
*
* Copyright (C) 2008 Felipe C. da S. Sanches
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
* Read the file 'COPYING' for more information.
*/
#include <2geom/pathvector.h>
#include <2geom/transforms.h>
#include <cairo.h>
#include <vector>
#include "svg/svg.h"
#include "display/cairo-utils.h"
#include "display/nr-svgfonts.h"
#include "display/nr-svgfonts.h"
#include "display/curve.h"
#include "xml/repr.h"
#include "object/sp-path.h"
#include "object/sp-object-group.h"
#include "object/sp-use.h"
#include "object/sp-use-reference.h"
#include "object/sp-font-face.h"
#include "object/sp-glyph.h"
#include "object/sp-missing-glyph.h"
#include "object/sp-font.h"
#include "object/sp-glyph-kerning.h"
// ************************//
// UserFont Implementation //
// ************************//
// I wrote this binding code because Cairomm does not yet support userfonts. I have moved this code to cairomm and sent them a patch.
// Once Cairomm incorporate the UserFonts binding, this code should be removed from inkscape and Cairomm API should be used.
static cairo_user_data_key_t key;
static cairo_status_t font_init_cb (cairo_scaled_font_t *scaled_font,
cairo_t * /*cairo*/, cairo_font_extents_t *metrics){
cairo_font_face_t* face = cairo_scaled_font_get_font_face(scaled_font);
SvgFont* instance = static_cast<SvgFont*>(cairo_font_face_get_user_data(face, &key));
return instance->scaled_font_init(scaled_font, metrics);
}
static cairo_status_t font_text_to_glyphs_cb ( cairo_scaled_font_t *scaled_font,
const char *utf8,
int utf8_len,
cairo_glyph_t **glyphs,
int *num_glyphs,
cairo_text_cluster_t **clusters,
int *num_clusters,
cairo_text_cluster_flags_t *flags){
cairo_font_face_t* face = cairo_scaled_font_get_font_face(scaled_font);
SvgFont* instance = static_cast<SvgFont*>(cairo_font_face_get_user_data(face, &key));
return instance->scaled_font_text_to_glyphs(scaled_font, utf8, utf8_len, glyphs, num_glyphs, clusters, num_clusters, flags);
}
static cairo_status_t font_render_glyph_cb (cairo_scaled_font_t *scaled_font,
unsigned long glyph,
cairo_t *cr,
cairo_text_extents_t *metrics){
cairo_font_face_t* face = cairo_scaled_font_get_font_face(scaled_font);
SvgFont* instance = static_cast<SvgFont*>(cairo_font_face_get_user_data(face, &key));
return instance->scaled_font_render_glyph(scaled_font, glyph, cr, metrics);
}
UserFont::UserFont(SvgFont* instance){
this->face = cairo_user_font_face_create ();
cairo_user_font_face_set_init_func (this->face, font_init_cb);
cairo_user_font_face_set_render_glyph_func (this->face, font_render_glyph_cb);
cairo_user_font_face_set_text_to_glyphs_func(this->face, font_text_to_glyphs_cb);
cairo_font_face_set_user_data (this->face, &key, (void*)instance, (cairo_destroy_func_t) nullptr);
}
//******************************//
// SvgFont class Implementation //
//******************************//
SvgFont::SvgFont(SPFont* spfont){
this->font = spfont;
this->missingglyph = nullptr;
this->userfont = nullptr;
}
cairo_status_t
SvgFont::scaled_font_init (cairo_scaled_font_t */*scaled_font*/,
cairo_font_extents_t */*metrics*/)
{
//TODO
// metrics->ascent = .75;
// metrics->descent = .25;
return CAIRO_STATUS_SUCCESS;
}
unsigned int size_of_substring(const char* substring, gchar* str){
const gchar* original_substring = substring;
while((g_utf8_get_char(substring)==g_utf8_get_char(str)) && g_utf8_get_char(substring) != 0 && g_utf8_get_char(str) != 0){
substring = g_utf8_next_char(substring);
str = g_utf8_next_char(str);
}
if (g_utf8_get_char(substring)==0)
return substring - original_substring;
else
return 0;
}
namespace {
//TODO: in these functions, verify what happens when using unicode strings.
bool MatchVKerningRule(SPVkern const *vkern,
SPGlyph *glyph,
char const *previous_unicode,
gchar const *previous_glyph_name)
{
bool value = (vkern->u1->contains(previous_unicode[0])
|| vkern->g1->contains(previous_glyph_name))
&& (vkern->u2->contains(glyph->unicode[0])
|| vkern->g2->contains(glyph->glyph_name.c_str()));
return value;
}
bool MatchHKerningRule(SPHkern const *hkern,
SPGlyph *glyph,
char const *previous_unicode,
gchar const *previous_glyph_name)
{
bool value = (hkern->u1->contains(previous_unicode[0])
|| hkern->g1->contains(previous_glyph_name))
&& (hkern->u2->contains(glyph->unicode[0])
|| hkern->g2->contains(glyph->glyph_name.c_str()));
return value;
}
} // namespace
cairo_status_t
SvgFont::scaled_font_text_to_glyphs (cairo_scaled_font_t */*scaled_font*/,
const char *utf8,
int /*utf8_len*/,
cairo_glyph_t **glyphs,
int *num_glyphs,
cairo_text_cluster_t **/*clusters*/,
int */*num_clusters*/,
cairo_text_cluster_flags_t */*flags*/)
{
//This function receives a text string to be rendered. It then defines what is the sequence of glyphs that
// is used to properly render this string. It also defines the respective coordinates of each glyph. Thus, it
// has to read the attributes of the SVGFont hkern and vkern nodes in order to adjust the glyph kerning.
//It also determines the usage of the missing-glyph in portions of the string that does not match any of the declared glyphs.
unsigned long i;
int count = 0;
gchar* _utf8 = (gchar*) utf8;
unsigned int len;
bool missing;
//First we find out what's the number of glyphs needed.
while(g_utf8_get_char(_utf8)){
missing = true;
for (i=0; i < (unsigned long) this->glyphs.size(); i++){
if ( (len = size_of_substring(this->glyphs[i]->unicode.c_str(), _utf8)) ){
//TODO: store this cluster
_utf8+=len;
count++;
missing=false;
break;
}
}
if (missing){
//TODO: store this cluster
_utf8++;
count++;
}
}
//We use that info to allocate memory for the glyphs
*glyphs = (cairo_glyph_t*) malloc(count*sizeof(cairo_glyph_t));
char* previous_unicode = nullptr; //This is used for kerning
gchar* previous_glyph_name = nullptr; //This is used for kerning
count=0;
double x=0, y=0;//These vars store the position of the glyph within the rendered string
bool is_horizontal_text = true; //TODO
_utf8 = (char*) utf8;
double font_height = units_per_em();
while(g_utf8_get_char(_utf8)){
len = 0;
for (i=0; i < (unsigned long) this->glyphs.size(); i++){
//check whether is there a glyph declared on the SVG document
// that matches with the text string in its current position
if ( (len = size_of_substring(this->glyphs[i]->unicode.c_str(), _utf8)) ){
for(auto& node: font->children) {
if (!previous_unicode) {
break;
}
//apply glyph kerning if appropriate
SPHkern *hkern = dynamic_cast<SPHkern *>(&node);
if (hkern && is_horizontal_text &&
MatchHKerningRule(hkern, this->glyphs[i], previous_unicode, previous_glyph_name) ){
x -= (hkern->k / font_height);
}
SPVkern *vkern = dynamic_cast<SPVkern *>(&node);
if (vkern && !is_horizontal_text &&
MatchVKerningRule(vkern, this->glyphs[i], previous_unicode, previous_glyph_name) ){
y -= (vkern->k / font_height);
}
}
previous_unicode = const_cast<char*>(this->glyphs[i]->unicode.c_str());//used for kerning checking
previous_glyph_name = const_cast<char*>(this->glyphs[i]->glyph_name.c_str());//used for kerning checking
(*glyphs)[count].index = i;
(*glyphs)[count].x = x;
(*glyphs)[count++].y = y;
//advance glyph coordinates:
if (is_horizontal_text) {
if (this->glyphs[i]->horiz_adv_x != 0) {
x+=(this->glyphs[i]->horiz_adv_x/font_height);
} else {
x+=(this->font->horiz_adv_x/font_height);
}
} else {
y+=(this->font->vert_adv_y/font_height);
}
_utf8+=len; //advance 'len' bytes in our string pointer
//continue;
goto raptorz;
}
}
raptorz:
if (len==0){
(*glyphs)[count].index = i;
(*glyphs)[count].x = x;
(*glyphs)[count++].y = y;
//advance glyph coordinates:
if (is_horizontal_text) x+=(this->font->horiz_adv_x/font_height);//TODO: use here the height of the font
else y+=(this->font->vert_adv_y/font_height);//TODO: use here the "height" of the font
_utf8 = g_utf8_next_char(_utf8); //advance 1 char in our string pointer
}
}
*num_glyphs = count;
return CAIRO_STATUS_SUCCESS;
}
void
SvgFont::render_glyph_path(cairo_t* cr, Geom::PathVector* pathv){
if (!pathv->empty()){
//This glyph has a path description on its d attribute, so we render it:
cairo_new_path(cr);
//adjust scale of the glyph
Geom::Scale s(1.0/units_per_em());
Geom::Rect area( Geom::Point(0,0), Geom::Point(1,1) ); //I need help here! (reaction: note that the 'area' parameter is an *optional* rect, so you can pass an empty Geom::OptRect() )
feed_pathvector_to_cairo (cr, *pathv, s, area, false, 0);
cairo_fill(cr);
}
}
void
SvgFont::glyph_modified(SPObject* /* blah */, unsigned int /* bleh */){
this->refresh();
//TODO: update rendering on svgfonts preview widget (in the svg fonts dialog)
}
Geom::PathVector
SvgFont::flip_coordinate_system(SPFont* spfont, Geom::PathVector pathv){
double units_per_em = 1024;
for(auto& obj: spfont->children) {
if (dynamic_cast<SPFontFace *>(&obj)) {
//XML Tree being directly used here while it shouldn't be.
units_per_em = obj.getRepr()->getAttributeDouble("units_per_em", units_per_em);
}
}
double baseline_offset = units_per_em - spfont->horiz_origin_y;
//This matrix flips y-axis and places the origin at baseline
Geom::Affine m(Geom::Coord(1),Geom::Coord(0),Geom::Coord(0),Geom::Coord(-1),Geom::Coord(0),Geom::Coord(baseline_offset));
return pathv*m;
}
cairo_status_t
SvgFont::scaled_font_render_glyph (cairo_scaled_font_t */*scaled_font*/,
unsigned long glyph,
cairo_t *cr,
cairo_text_extents_t */*metrics*/)
{
// This method does the actual rendering of glyphs.
// We have glyphs.size() glyphs and possibly one missing-glyph declared on this SVG document
// The id of the missing-glyph is always equal to glyphs.size()
// All the other glyphs have ids ranging from 0 to glyphs.size()-1
if (glyph > this->glyphs.size()) return CAIRO_STATUS_SUCCESS;//TODO: this is an error!
SPObject *node = nullptr;
if (glyph == glyphs.size()){
if (!missingglyph) {
return CAIRO_STATUS_SUCCESS;
}
node = missingglyph;
} else {
node = glyphs[glyph];
}
if (!dynamic_cast<SPGlyph *>(node) && !dynamic_cast<SPMissingGlyph *>(node)) {
return CAIRO_STATUS_SUCCESS; // FIXME: is this the right code to return?
}
SPFont* spfont = dynamic_cast<SPFont *>(node->parent);
if (!spfont) {
return CAIRO_STATUS_SUCCESS; // FIXME: is this the right code to return?
}
//glyphs can be described by arbitrary SVG declared in the childnodes of a glyph node
// or using the d attribute of a glyph node.
// pathv stores the path description from the d attribute:
Geom::PathVector pathv;
SPGlyph *glyphNode = dynamic_cast<SPGlyph *>(node);
if (glyphNode && glyphNode->d) {
pathv = sp_svg_read_pathv(glyphNode->d);
pathv = flip_coordinate_system(spfont, pathv);
render_glyph_path(cr, &pathv);
} else {
SPMissingGlyph *missing = dynamic_cast<SPMissingGlyph *>(node);
if (missing && missing->d) {
pathv = sp_svg_read_pathv(missing->d);
pathv = flip_coordinate_system(spfont, pathv);
render_glyph_path(cr, &pathv);
}
}
if (node->hasChildren()){
//render the SVG described on this glyph's child nodes.
for(auto& child: node->children) {
{
SPPath *path = dynamic_cast<SPPath *>(&child);
if (path) {
pathv = path->curve()->get_pathvector();
pathv = flip_coordinate_system(spfont, pathv);
render_glyph_path(cr, &pathv);
}
}
if (dynamic_cast<SPObjectGroup *>(&child)) {
g_warning("TODO: svgfonts: render OBJECTGROUP");
}
SPUse *use = dynamic_cast<SPUse *>(&child);
if (use) {
SPItem* item = use->ref->getObject();
SPPath *path = dynamic_cast<SPPath *>(item);
if (path) {
SPShape *shape = dynamic_cast<SPShape *>(item);
g_assert(shape != nullptr);
pathv = shape->curve()->get_pathvector();
pathv = flip_coordinate_system(spfont, pathv);
this->render_glyph_path(cr, &pathv);
}
glyph_modified_connection = item->connectModified(sigc::mem_fun(*this, &SvgFont::glyph_modified));
}
}
}
return CAIRO_STATUS_SUCCESS;
}
cairo_font_face_t*
SvgFont::get_font_face(){
if (!this->userfont) {
for(auto& node: font->children) {
SPGlyph *glyph = dynamic_cast<SPGlyph *>(&node);
if (glyph) {
glyphs.push_back(glyph);
}
SPMissingGlyph *missing = dynamic_cast<SPMissingGlyph *>(&node);
if (missing) {
missingglyph = missing;
}
}
this->userfont = new UserFont(this);
}
return this->userfont->face;
}
void SvgFont::refresh(){
this->glyphs.clear();
delete this->userfont;
this->userfont = nullptr;
}
double SvgFont::units_per_em() {
double units_per_em = 1024;
for (auto& obj: font->children) {
if (dynamic_cast<SPFontFace *>(&obj)) {
//XML Tree being directly used here while it shouldn't be.
units_per_em = obj.getRepr()->getAttributeDouble("units-per-em", units_per_em);
}
}
if (units_per_em <= 0.0) {
units_per_em = 1024;
}
return units_per_em;
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
|