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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
#include <QObject>
#include <QDebug>
#include "commonstrings.h"
#include "sctextstruct.h"
#include "scfonts.h"
#include "resourcecollection.h"
#include "styles/style.h"
#include "charstyle.h"
#include "desaxe/saxiohelper.h"
#include "desaxe/simple_actions.h"
#include "prefsmanager.h"
#include "util_math.h"
StyleFlag& StyleFlag::operator&= (const StyleFlag& right){
int result = static_cast<int>(value) & static_cast<int>(right.value);
value = static_cast<StyleFlagValue>(result);
return *this;
}
StyleFlag& StyleFlag::operator|= (const StyleFlag& right)
{
int result = static_cast<int>(value) | static_cast<int>(right.value);
value = static_cast<StyleFlagValue>(result);
return *this;
}
StyleFlag StyleFlag::operator& (const StyleFlag& right)
{
int val = static_cast<int>(value) & static_cast<int>(right.value);
StyleFlag result(static_cast<StyleFlagValue>(val));
return result;
}
StyleFlag StyleFlag::operator& (int right)
{
int val = static_cast<int>(value) & right;
StyleFlag result(static_cast<StyleFlagValue>(val));
return result;
}
StyleFlag StyleFlag::operator| (const StyleFlag& right)
{
int val = static_cast<int>(value) | static_cast<int>(right.value);
StyleFlag result(static_cast<StyleFlagValue>(val));
return result;
}
StyleFlag StyleFlag::operator^ (const StyleFlag& right)
{
int val = static_cast<int>(value) ^ static_cast<int>(right.value);
StyleFlag result(static_cast<StyleFlagValue>(val));
return result;
}
StyleFlag StyleFlag::operator^ (int right)
{
int val = static_cast<int>(value) ^ right;
StyleFlag result(static_cast<StyleFlagValue>(val));
return result;
}
StyleFlag StyleFlag::operator~ ()
{
int val = ~ static_cast<int>(value);
StyleFlag result(static_cast<StyleFlagValue>(val));
return result;
}
bool StyleFlag::operator== (const StyleFlag& right) const
{
int result = static_cast<int>( (value ^ right.value) & ScStyle_UserStyles);
return (result == 0);
}
bool StyleFlag::operator== (const StyleFlagValue right) const
{
int result = static_cast<int>( (value ^ right) & ScStyle_UserStyles);
return (result == 0);
}
bool StyleFlag::operator== (int right) const
{
int result = static_cast<int>( (value ^ right) & ScStyle_UserStyles);
return (result == 0);
}
bool StyleFlag::operator!= (const StyleFlag& right) const
{
return !(*this==right);
}
bool StyleFlag::operator!= (const StyleFlagValue right) const
{
return !(*this==right);
}
void CharStyle::applyCharStyle(const CharStyle & other)
{
if (other.hasParent() && (other.parent() != Style::INHERIT_PARENT))
{
setStyle(other);
return;
}
Style::applyStyle(other);
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
if (! other.inh_##attr_NAME) \
set##attr_NAME(other.m_##attr_NAME);
#include "charstyle.attrdefs.cxx"
#undef ATTRDEF
updateFeatures();
}
void CharStyle::eraseCharStyle(const CharStyle & other)
{
other.validate();
Style::eraseStyle(other);
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
if (!inh_##attr_NAME && m_##attr_NAME == other.m_##attr_NAME) \
reset##attr_NAME();
#include "charstyle.attrdefs.cxx"
#undef ATTRDEF
updateFeatures();
}
void CharStyle::eraseDirectFormatting()
{
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
if (!inh_##attr_NAME) \
reset##attr_NAME();
#include "charstyle.attrdefs.cxx"
#undef ATTRDEF
updateFeatures();
}
bool CharStyle::equiv(const Style & other) const
{
other.validate();
const CharStyle * oth = dynamic_cast<const CharStyle*> ( & other );
return oth &&
parent() == oth->parent()
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
&& (inh_##attr_NAME == oth->inh_##attr_NAME) \
&& (inh_##attr_NAME || isequiv(m_##attr_NAME, oth->m_##attr_NAME))
#include "charstyle.attrdefs.cxx"
#undef ATTRDEF
;
}
QString CharStyle::displayName() const
{
if ( isDefaultStyle() )
return CommonStrings::trDefaultCharacterStyle;
if ( hasName() || !hasParent() || ! m_context)
return name();
// else if ( inheritsAll() )
// return parent()->displayName();
else
return parentStyle()->displayName() + "+";
}
QString CharStyle::asString() const
{
QString result;
if ( !inh_Font )
result += QObject::tr("font %1 ").arg(font().scName());
if ( !inh_FontSize )
result += QObject::tr("size %1 ").arg(fontSize());
if ( !inh_Features )
result += QObject::tr("+style ");
if ( !inh_StrokeColor || !inh_StrokeShade || !inh_FillColor || !inh_FillShade )
result += QObject::tr("+color ");
if ( !inh_UnderlineWidth || !inh_UnderlineOffset )
result += underlineWidth() > 0 ? QObject::tr("+underline ") : QObject::tr("-underline ");
if ( !inh_StrikethruWidth || !inh_StrikethruOffset )
result += strikethruWidth() > 0 ? QObject::tr("+strikeout ") : QObject::tr("-strikeout ");
if ( !inh_ShadowXOffset || !inh_ShadowYOffset )
result += shadowXOffset() != 0 || shadowYOffset() != 0 ? QObject::tr("+shadow ") : QObject::tr("-shadow ");
if ( !inh_OutlineWidth )
result += outlineWidth() > 0 ? QObject::tr("+outline ") : QObject::tr("-outline ");
if ( !inh_Tracking )
result += tracking() > 0 ? QObject::tr("+tracking %1 ").arg(tracking()) : QObject::tr("-tracking ");
if ( !inh_BaselineOffset )
result += QObject::tr("+baseline %1 ").arg(baselineOffset());
if ( !inh_ScaleH || !inh_ScaleV )
result += QObject::tr("+stretch ");
if ( hasParent() )
result += QObject::tr("parent= %1").arg(parent());
return result.trimmed();
}
void CharStyle::update(const StyleContext* context)
{
Style::update(context);
const CharStyle * oth = dynamic_cast<const CharStyle*> ( parentStyle() );
if (oth) {
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
if (inh_##attr_NAME) \
m_##attr_NAME = oth->attr_GETTER();
#include "charstyle.attrdefs.cxx"
#undef ATTRDEF
}
updateFeatures();
}
const QString CharStyle::INHERIT = "inherit";
const QString CharStyle::BOLD = "bold";
const QString CharStyle::ITALIC = "italic";
const QString CharStyle::UNDERLINE = "underline";
const QString CharStyle::UNDERLINEWORDS = "underlinewords";
const QString CharStyle::STRIKETHROUGH = "strike";
const QString CharStyle::SUPERSCRIPT = "superscript";
const QString CharStyle::SUBSCRIPT = "subscript";
const QString CharStyle::OUTLINE = "outline";
const QString CharStyle::SHADOWED = "shadowed";
const QString CharStyle::ALLCAPS = "allcaps";
const QString CharStyle::SMALLCAPS = "smallcaps";
// This is for loading legacy docs only. Scribus 1.3.4 should write smart hyphens in another way
static const QString SHYPHEN = "shyphen";
QStringList StyleFlag::featureList() const
{
QStringList result(CharStyle::INHERIT);
if (*this & ScStyle_Underline)
result << CharStyle::UNDERLINE;
if (*this & ScStyle_UnderlineWords)
result << CharStyle::UNDERLINEWORDS;
if (*this & ScStyle_Strikethrough)
result << CharStyle::STRIKETHROUGH;
if (*this & ScStyle_Superscript)
result << CharStyle::SUPERSCRIPT;
if (*this & ScStyle_Subscript)
result << CharStyle::SUBSCRIPT;
if (*this & ScStyle_Outline)
result << CharStyle::OUTLINE;
if (*this & ScStyle_Shadowed)
result << CharStyle::SHADOWED;
if (*this & ScStyle_AllCaps)
result << CharStyle::ALLCAPS;
if (*this & ScStyle_SmallCaps)
result << CharStyle::SMALLCAPS;
if (*this & ScStyle_HyphenationPossible)
result << SHYPHEN;
return result;
}
void CharStyle::updateFeatures()
{
m_Effects &= ~ScStyle_UserStyles;
runFeatures(m_Features, dynamic_cast<const CharStyle*>(parentStyle()));
/* need to access global fontlist :-/
if (!font().name().endsWith(fontVariant()))
{
m_font = ScFonts.instance().findFont(font().family() + fontVariant());
}
*/
}
void CharStyle::runFeatures(const QStringList& featureList, const CharStyle* parent)
{
QStringList::ConstIterator it;
for (it = featureList.begin(); it != featureList.end(); ++it)
{
QString feature = it->trimmed();
if (feature == INHERIT)
{
if (parent)
runFeatures(parent->features(), dynamic_cast<const CharStyle*>(parent->parentStyle()));
}
else if (feature == BOLD)
{
// select bolder font
}
else if (feature == ITALIC)
{
// select italic font
}
else if (feature == UNDERLINE)
{
m_Effects |= ScStyle_Underline;
}
else if (feature == UNDERLINEWORDS)
{
m_Effects |= ScStyle_UnderlineWords;
}
else if (feature == STRIKETHROUGH)
{
m_Effects |= ScStyle_Strikethrough;
}
else if (feature == SUPERSCRIPT)
{
m_Effects |= ScStyle_Superscript;
}
else if (feature == SUBSCRIPT)
{
m_Effects |= ScStyle_Subscript;
}
else if (feature == OUTLINE)
{
m_Effects |= ScStyle_Outline;
}
else if (feature == SHADOWED)
{
m_Effects |= ScStyle_Shadowed;
}
else if (feature == ALLCAPS)
{
m_Effects |= ScStyle_AllCaps;
}
else if (feature == SMALLCAPS)
{
m_Effects |= ScStyle_SmallCaps;
}
else if (feature == SHYPHEN)
{
m_Effects |= ScStyle_HyphenationPossible;
}
else if (feature.startsWith("-"))
{
QString no_feature = feature.mid(1);
if (no_feature == BOLD)
{
// deselect bolder font
}
else if (no_feature == ITALIC)
{
// deselect italic font
}
else if (no_feature == UNDERLINE)
{
m_Effects &= ~ScStyle_Underline;
}
else if (no_feature == UNDERLINEWORDS)
{
m_Effects &= ~ScStyle_UnderlineWords;
}
else if (no_feature == STRIKETHROUGH)
{
m_Effects &= ~ScStyle_Strikethrough;
}
else if (no_feature == SUPERSCRIPT)
{
m_Effects &= ~ScStyle_Superscript;
}
else if (no_feature == SUBSCRIPT)
{
m_Effects &= ~ScStyle_Subscript;
}
else if (no_feature == OUTLINE)
{
m_Effects &= ~ScStyle_Outline;
}
else if (no_feature == SHADOWED)
{
m_Effects &= ~ScStyle_Shadowed;
}
else if (no_feature == ALLCAPS)
{
m_Effects &= ~ScStyle_AllCaps;
}
else if (no_feature == SMALLCAPS)
{
m_Effects &= ~ScStyle_SmallCaps;
}
else {
qDebug("CharStyle: unknown feature: %s", feature.toLocal8Bit().constData());
}
}
else {
qDebug("CharStyle: unknown feature: %s", feature.toLocal8Bit().constData());
}
}
}
void CharStyle::setStyle(const CharStyle& other)
{
other.validate();
setParent(other.parent());
m_contextversion = -1;
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
inh_##attr_NAME = other.inh_##attr_NAME; \
m_##attr_NAME = other.m_##attr_NAME;
#include "charstyle.attrdefs.cxx"
#undef ATTRDEF
updateFeatures();
}
void CharStyle::getNamedResources(ResourceCollection& lists) const
{
for (const Style* sty = parentStyle(); sty != NULL; sty = sty->parentStyle())
lists.collectCharStyle(sty->name());
lists.collectColor(fillColor());
lists.collectColor(strokeColor());
lists.collectFont(font().scName());
}
void CharStyle::replaceNamedResources(ResourceCollection& newNames)
{
QMap<QString,QString>::ConstIterator it;
if (!inh_FillColor && (it = newNames.colors().find(fillColor())) != newNames.colors().end())
setFillColor(it.value());
if (!inh_StrokeColor && (it = newNames.colors().find(strokeColor())) != newNames.colors().end())
setStrokeColor(it.value());
if (hasParent() && (it = newNames.charStyles().find(parent())) != newNames.charStyles().end())
setParent(it.value());
if (!inh_Font && (it = newNames.fonts().find(font().scName())) != newNames.fonts().end())
setFont(newNames.availableFonts->findFont(it.value(), NULL));
updateFeatures();
}
/*
bool CharStyle::definesAll() const
{
return definesLineSpacing() &&
definesLeftMargin() &&
definesRightMargin() &&
definesFirstIndent() &&
definesAlignment() &&
definesGapBefore() &&
definesLineSpacingMode() &&
definesGapAfter() &&
definesHasDropCap() &&
definesDropCapOffset() &&
definesDropCapLines() &&
definesUseBaselineGrid() &&
charStyle().definesAll() ;
}
// equiv. to "*this == CharStyle()"
bool CharStyle::inheritsAll() const
{
return inheritsLineSpacing() &&
inheritsLeftMargin() &&
inheritsRightMargin() &&
inheritsFirstIndent() &&
inheritsAlignment() &&
inheritsGapBefore() &&
inheritsLineSpacingMode() &&
inheritsGapAfter() &&
inheritsHasDropCap() &&
inheritsDropCapOffset() &&
inheritsDropCapLines() &&
inheritsUseBaselineGrid() &&
charStyle().inheritsAll() ;
}
*/
Xml_string toXMLString(StyleFlag val)
{
return toXMLString(static_cast<unsigned int>(val & ScStyle_UserStyles));
}
void CharStyle::saxx(SaxHandler& handler, const Xml_string& elemtag) const
{
Xml_attr att;
Style::saxxAttributes(att);
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
if (!inh_##attr_NAME) \
att.insert(# attr_NAME, toXMLString(m_##attr_NAME));
#include "charstyle.attrdefs.cxx"
#undef ATTRDEF
if (!name().isEmpty())
att["id"] = mkXMLName(elemtag + name());
handler.begin(elemtag, att);
// if (hasParent() && parentStyle())
// parentStyle()->saxx(handler);
handler.end(elemtag);
}
template<>
StyleFlag parse<StyleFlag>(const Xml_string& str)
{
return StyleFlag(parseInt(str));
}
template<>
ScFace parse<ScFace>(const Xml_string& str)
{
// FIXME: enable font substitution here
return PrefsManager::instance()->appPrefs.AvailFonts[str];
}
using namespace desaxe;
const Xml_string CharStyle::saxxDefaultElem("charstyle");
void CharStyle::desaxeRules(const Xml_string& prefixPattern, Digester& ruleset, Xml_string elemtag)
{
Xml_string stylePrefix(Digester::concat(prefixPattern, elemtag));
ruleset.addRule(stylePrefix, Factory<CharStyle>());
ruleset.addRule(stylePrefix, IdRef<CharStyle>());
Style::desaxeRules<CharStyle>(prefixPattern, ruleset, elemtag);
#define ATTRDEF(attr_TYPE, attr_GETTER, attr_NAME, attr_DEFAULT) \
ruleset.addRule(stylePrefix, SetAttributeWithConversion<CharStyle, attr_TYPE> ( & CharStyle::set##attr_NAME, # attr_NAME, &parse<attr_TYPE> ));
#include "charstyle.attrdefs.cxx"
#undef ATTRDEF
}
|