19
19
********************************************************************
22
$Author: cohen@irascible.com $:
23
$Date: 2012-07-18 15:52:11 +0200 (Wed, 18 Jul 2012) $
22
$Author: irascibl@gmail.com $:
23
$Date: 2012-10-13 10:32:03 +0200 (Sat, 13 Oct 2012) $
25
25
********************************************************************/
29
29
#include "../utils/misc.h"
30
30
#include "../utils/textutils.h"
31
//#include "../debugdialog.h"
31
#include "../debugdialog.h"
32
32
#include "svgpathparser.h"
33
33
#include "svgpathlexer.h"
34
34
#include "svgpathrunner.h"
86
84
contents.replace('\r', ' ');
88
86
// get rid of inkscape stuff too
89
contents.remove(TextUtils::SodipodiDetector);
87
TextUtils::cleanSodipodi(contents);
356
354
bool doChildren = false;
357
355
QString nodeName = element.nodeName();
358
356
if (nodeName.compare("g") == 0) {
359
fixStyleAttribute(element);
357
TextUtils::fixStyleAttribute(element);
360
358
normalizeAttribute(element, "stroke-width", sNewWidth, vbWidth);
361
359
setStrokeOrFill(element, blackOnly, "black", false);
362
360
doChildren = true;
364
362
else if (nodeName.compare("circle") == 0) {
365
fixStyleAttribute(element);
363
TextUtils::fixStyleAttribute(element);
366
364
normalizeAttribute(element, "cx", sNewWidth, vbWidth);
367
365
normalizeAttribute(element, "cy", sNewHeight, vbHeight);
368
366
normalizeAttribute(element, "r", sNewWidth, vbWidth);
370
368
setStrokeOrFill(element, blackOnly, "black", false);
372
370
else if (nodeName.compare("line") == 0) {
373
fixStyleAttribute(element);
371
TextUtils::fixStyleAttribute(element);
374
372
normalizeAttribute(element, "x1", sNewWidth, vbWidth);
375
373
normalizeAttribute(element, "y1", sNewHeight, vbHeight);
376
374
normalizeAttribute(element, "x2", sNewWidth, vbWidth);
379
377
setStrokeOrFill(element, blackOnly, "black", false);
381
379
else if (nodeName.compare("rect") == 0) {
382
fixStyleAttribute(element);
380
TextUtils::fixStyleAttribute(element);
383
381
normalizeAttribute(element, "width", sNewWidth, vbWidth);
384
382
normalizeAttribute(element, "height", sNewHeight, vbHeight);
385
383
normalizeAttribute(element, "x", sNewWidth, vbWidth);
396
394
setStrokeOrFill(element, blackOnly, "black", false);
398
396
else if (nodeName.compare("ellipse") == 0) {
399
fixStyleAttribute(element);
397
TextUtils::fixStyleAttribute(element);
400
398
normalizeAttribute(element, "cx", sNewWidth, vbWidth);
401
399
normalizeAttribute(element, "cy", sNewHeight, vbHeight);
402
400
normalizeAttribute(element, "rx", sNewWidth, vbWidth);
405
403
setStrokeOrFill(element, blackOnly, "black", false);
407
405
else if (nodeName.compare("polygon") == 0 || nodeName.compare("polyline") == 0) {
408
fixStyleAttribute(element);
406
TextUtils::fixStyleAttribute(element);
409
407
normalizeAttribute(element, "stroke-width", sNewWidth, vbWidth);
410
408
QString data = element.attribute("points");
411
409
if (!data.isEmpty()) {
424
422
setStrokeOrFill(element, blackOnly, "black", false);
426
424
else if (nodeName.compare("path") == 0) {
427
fixStyleAttribute(element);
425
TextUtils::fixStyleAttribute(element);
428
426
normalizeAttribute(element, "stroke-width", sNewWidth, vbWidth);
429
427
setStrokeOrFill(element, blackOnly, "black", false);
430
428
QString data = element.attribute("d").trimmed();
444
442
else if (nodeName.compare("text") == 0) {
445
fixStyleAttribute(element);
443
TextUtils::fixStyleAttribute(element);
446
444
normalizeAttribute(element, "x", sNewWidth, vbWidth);
447
445
normalizeAttribute(element, "y", sNewHeight, vbHeight);
448
446
normalizeAttribute(element, "stroke-width", sNewWidth, vbWidth);
488
486
bool SvgFileSplitter::normalizeAttribute(QDomElement & element, const char * attributeName, double num, double denom)
490
double n = element.attribute(attributeName).toDouble() * num / denom;
488
QString attributeValue = element.attribute(attributeName);
489
if (attributeValue.isEmpty()) return true;
492
double n = attributeValue.toDouble(&ok) * num / denom;
495
QTextStream stream(&string);
496
element.save(stream, 0);
497
DebugDialog::debug("bad attribute " + string);
491
500
element.setAttribute(attributeName, QString::number(n));
495
504
QString SvgFileSplitter::shift(double x, double y, const QString & elementID, bool shiftTransforms)
1055
1064
void SvgFileSplitter::fixStyleAttributeRecurse(QDomElement & element) {
1056
fixStyleAttribute(element);
1065
TextUtils::fixStyleAttribute(element);
1057
1066
QDomElement childElement = element.firstChildElement();
1058
1067
while (!childElement.isNull()) {
1059
1068
fixStyleAttributeRecurse(childElement);
1064
1073
void SvgFileSplitter::fixColorRecurse(QDomElement & element, const QString & newColor, const QStringList & exceptions) {
1065
fixStyleAttribute(element);
1074
TextUtils::fixStyleAttribute(element);
1066
1075
bool gotException = false;
1067
1076
QString s = element.attribute("stroke");
1068
1077
QString f = element.attribute("fill");
1107
void SvgFileSplitter::fixStyleAttribute(QDomElement & element)
1109
QString style = element.attribute("style");
1110
if (style.isEmpty()) return;
1112
fixStyleAttribute(element, style, "stroke-width");
1113
fixStyleAttribute(element, style, "stroke");
1114
fixStyleAttribute(element, style, "fill");
1115
fixStyleAttribute(element, style, "fill-opacity");
1116
fixStyleAttribute(element, style, "stroke-opacity");
1117
fixStyleAttribute(element, style, "font-size");
1119
if (style.trimmed().isEmpty()) {
1120
element.removeAttribute("style");
1123
element.setAttribute("style", style);
1127
//QTextStream stream(&deleteMe);
1128
//stream << element;
1129
//DebugDialog::debug(deleteMe);
1132
void SvgFileSplitter::fixStyleAttribute(QDomElement & element, QString & style, const QString & attributeName)
1134
QString str = findStyle.arg(attributeName);
1136
if (sw.indexIn(style) >= 0) {
1137
QString value = sw.cap(1);
1139
element.setAttribute(attributeName, value);
1143
1116
bool SvgFileSplitter::getSvgSizeAttributes(const QString & svg, QString & width, QString & height, QString & viewBox)
1145
1118
QXmlStreamReader xml(svg);
1189
void SvgFileSplitter::forceStrokeWidth(QDomElement & element, double delta, const QString & stroke, bool recurse) {
1191
double sw = element.attribute("stroke-width").toDouble(&ok);
1194
element.setAttribute("stroke-width", QString::number(sw + delta));
1195
element.setAttribute("stroke", stroke);
1198
QDomElement child = element.firstChildElement();
1199
while (!child.isNull()) {
1200
forceStrokeWidth(child, delta, stroke, recurse);
1201
child = child.nextSiblingElement();
1216
1206
bool SvgFileSplitter::changeColors(const QString & svg, QString & toColor, QStringList & exceptions, QByteArray & byteArray) {
1217
1207
QString errorStr;