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
|
/**
* @file-update
* Operations to bump files from the pre-0.92 era into the 0.92+ era
* (90dpi vs 96dpi, line height problems, and related bugs)
*/
/* Authors:
* Tavmjong Bah
* Marc Jeanmougin
* su_v
*/
#include "file.h"
#include "sp-root.h"
#include "sp-text.h"
#include "sp-tspan.h"
#include "sp-flowdiv.h"
#include "sp-flowtext.h"
#include "sp-object.h"
#include "sp-item.h"
#include "style.h"
#include "document.h"
#include <string>
#include <clocale>
#include "text-editing.h"
using namespace std;
bool is_line(SPObject *i)
{
if (!(i->getAttribute("sodipodi:role")))
return false;
return !strcmp(i->getAttribute("sodipodi:role"), "line");
}
void fix_blank_line(SPObject *o)
{
if (SP_IS_TEXT(o))
((SPText *)o)->rebuildLayout();
else if (SP_IS_FLOWTEXT(o))
((SPFlowtext *)o)->rebuildLayout();
SPIFontSize fontsize = o->style->font_size;
SPILengthOrNormal lineheight = o->style->line_height;
vector<SPObject *> cl = o->childList(false);
bool beginning = true;
for (vector<SPObject *>::const_iterator ci = cl.begin(); ci != cl.end(); ++ci) {
SPObject *i = *ci;
if ((SP_IS_TSPAN(i) && is_line(i)) || SP_IS_FLOWPARA(i) || SP_IS_FLOWDIV(i)) {
if (sp_text_get_length((SPItem *)i) <= 1) { // empty line
Inkscape::Text::Layout::iterator pos = te_get_layout((SPItem*)(o))->charIndexToIterator(
((SP_IS_FLOWPARA(i) || SP_IS_FLOWDIV(i))?0:((ci==cl.begin())?0:1)) + sp_text_get_length_upto(o,i) );
sp_te_insert((SPItem *)o, pos, "\u00a0"); //"\u00a0"
gchar *l = g_strdup_printf("%f", lineheight.value);
gchar *f = g_strdup_printf("%f", fontsize.value);
i->style->line_height.readIfUnset(l);
if (!beginning)
i->style->font_size.read(f);
else
i->style->font_size.readIfUnset(f);
g_free(l);
g_free(f);
} else {
beginning = false;
fontsize = i->style->font_size;
lineheight = o->style->line_height;
}
}
}
}
void fix_line_spacing(SPObject *o)
{
SPILengthOrNormal lineheight = o->style->line_height;
bool inner = false;
vector<SPObject *> cl = o->childList(false);
for (vector<SPObject *>::const_iterator ci = cl.begin(); ci != cl.end(); ++ci) {
SPObject *i = *ci;
if ((SP_IS_TSPAN(i) && is_line(i)) || SP_IS_FLOWPARA(i) || SP_IS_FLOWDIV(i)) {
// if no line-height attribute, set it
gchar *l = g_strdup_printf("%f", lineheight.value);
i->style->line_height.readIfUnset(l);
g_free(l);
}
inner = true;
}
if (inner) {
if (SP_IS_TEXT(o)) {
o->style->line_height.read("0.00%");
} else {
o->style->line_height.read("0.01%");
}
}
}
void fix_font_name(SPObject *o)
{
vector<SPObject *> cl = o->childList(false);
for (vector<SPObject *>::const_iterator ci = cl.begin(); ci != cl.end(); ++ci)
fix_font_name(*ci);
string prev = o->style->font_family.value ? o->style->font_family.value : o->style->font_family.value_default;
if (prev == "Sans")
o->style->font_family.read("sans-serif");
else if (prev == "Serif")
o->style->font_family.read("serif");
else if (prev == "Monospace")
o->style->font_family.read("monospace");
}
void fix_font_size(SPObject *o)
{
SPIFontSize fontsize = o->style->font_size;
if (!fontsize.set)
return;
bool inner = false;
vector<SPObject *> cl = o->childList(false);
for (vector<SPObject *>::const_iterator ci = cl.begin(); ci != cl.end(); ++ci) {
SPObject *i = *ci;
fix_font_size(i);
if ((SP_IS_TSPAN(i) && is_line(i)) || SP_IS_FLOWPARA(i) || SP_IS_FLOWDIV(i)) {
inner = true;
gchar *s = g_strdup_printf("%f", fontsize.value);
if (fontsize.set)
i->style->font_size.readIfUnset(s);
g_free(s);
}
}
if (inner && (SP_IS_TEXT(o) || SP_IS_FLOWTEXT(o)))
o->style->font_size.clear();
}
// helper function
void sp_file_text_run_recursive(void (*f)(SPObject *), SPObject *o)
{
if (SP_IS_TEXT(o) || SP_IS_FLOWTEXT(o))
f(o);
else {
vector<SPObject *> cl = o->childList(false);
for (vector<SPObject *>::const_iterator ci = cl.begin(); ci != cl.end(); ++ci)
sp_file_text_run_recursive(f, *ci);
}
}
void fix_update(SPObject *o) {
o->style->write();
o->updateRepr();
}
void sp_file_convert_text_baseline_spacing(SPDocument *doc)
{
char *oldlocale = g_strdup(setlocale(LC_NUMERIC, NULL));
setlocale(LC_NUMERIC,"C");
sp_file_text_run_recursive(fix_blank_line, doc->getRoot());
sp_file_text_run_recursive(fix_line_spacing, doc->getRoot());
sp_file_text_run_recursive(fix_font_size, doc->getRoot());
setlocale(LC_NUMERIC, oldlocale);
g_free(oldlocale);
sp_file_text_run_recursive(fix_update, doc->getRoot());
}
void sp_file_convert_font_name(SPDocument *doc)
{
sp_file_text_run_recursive(fix_font_name, doc->getRoot());
sp_file_text_run_recursive(fix_update, doc->getRoot());
}
/*
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 :
|