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 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
|
/* This file is part of the KDE project
* Copyright (C) 2002-2005,2007 Rob Buis <buis@kde.org>
* Copyright (C) 2002-2004 Nicolas Goutte <nicolasg@snafu.de>
* Copyright (C) 2005-2006 Tim Beaulen <tbscope@gmail.com>
* Copyright (C) 2005-2009 Jan Hambrecht <jaham@gmx.net>
* Copyright (C) 2005,2007 Thomas Zander <zander@kde.org>
* Copyright (C) 2006-2007 Inge Wallin <inge@lysator.liu.se>
* Copyright (C) 2007-2008,2010 Thorsten Zachmann <zachmann@kde.org>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "SvgParser.h"
#include "SvgUtil.h"
#include "SvgShape.h"
#include <KoShape.h>
#include <KoShapeRegistry.h>
#include <KoShapeFactoryBase.h>
#include <KoShapeLayer.h>
#include <KoShapeContainer.h>
#include <KoShapeGroup.h>
#include <KoPathShape.h>
#include <KoDocumentResourceManager.h>
#include <KoPathShapeLoader.h>
#include <commands/KoShapeGroupCommand.h>
#include <commands/KoShapeUngroupCommand.h>
#include <KoImageData.h>
#include <KoImageCollection.h>
#include <KoColorBackground.h>
#include <KoGradientBackground.h>
#include <KoPatternBackground.h>
#include <KoFilterEffectRegistry.h>
#include <KoFilterEffect.h>
#include "KoFilterEffectStack.h"
#include "KoFilterEffectLoadingContext.h"
#include <KoClipPath.h>
#include <KoXmlNS.h>
#include <KDebug>
#include <QColor>
SvgParser::SvgParser(KoDocumentResourceManager *documentResourceManager)
: m_context(documentResourceManager)
, m_documentResourceManager(documentResourceManager)
{
}
SvgParser::~SvgParser()
{
}
void SvgParser::setXmlBaseDir(const QString &baseDir)
{
m_context.setInitialXmlBaseDir(baseDir);
}
QList<KoShape*> SvgParser::shapes() const
{
return m_shapes;
}
// Helper functions
// ---------------------------------------------------------------------------------------
SvgGradientHelper* SvgParser::findGradient(const QString &id, const QString &href)
{
// check if gradient was already parsed, and return it
if (m_gradients.contains(id))
return &m_gradients[ id ];
// check if gradient was stored for later parsing
if (!m_context.hasDefinition(id))
return 0;
const KoXmlElement &e = m_context.definition(id);
if (!e.tagName().contains("Gradient"))
return 0;
if (e.childNodesCount() == 0) {
QString mhref = e.attribute("xlink:href").mid(1);
if (m_context.hasDefinition(mhref))
return findGradient(mhref, id);
else
return 0;
} else {
// ok parse gradient now
if (! parseGradient(m_context.definition(id), m_context.definition(href)))
return 0;
}
// return successfully parsed gradient or NULL
QString n;
if (href.isEmpty())
n = id;
else
n = href;
if (m_gradients.contains(n))
return &m_gradients[ n ];
else
return 0;
}
SvgPatternHelper* SvgParser::findPattern(const QString &id)
{
// check if pattern was already parsed, and return it
if (m_patterns.contains(id))
return &m_patterns[ id ];
// check if pattern was stored for later parsing
if (!m_context.hasDefinition(id))
return 0;
SvgPatternHelper pattern;
const KoXmlElement &e = m_context.definition(id);
if (e.tagName() != "pattern")
return 0;
// are we referencing another pattern ?
if (e.hasAttribute("xlink:href")) {
QString mhref = e.attribute("xlink:href").mid(1);
SvgPatternHelper *refPattern = findPattern(mhref);
// inherit attributes of referenced pattern
if (refPattern)
pattern = *refPattern;
}
// ok parse pattern now
parsePattern(pattern, m_context.definition(id));
// add to parsed pattern list
m_patterns.insert(id, pattern);
return &m_patterns[ id ];
}
SvgFilterHelper* SvgParser::findFilter(const QString &id, const QString &href)
{
// check if filter was already parsed, and return it
if (m_filters.contains(id))
return &m_filters[ id ];
// check if filter was stored for later parsing
if (!m_context.hasDefinition(id))
return 0;
const KoXmlElement &e = m_context.definition(id);
if (e.childNodesCount() == 0) {
QString mhref = e.attribute("xlink:href").mid(1);
if (m_context.hasDefinition(mhref))
return findFilter(mhref, id);
else
return 0;
} else {
// ok parse filter now
if (! parseFilter(m_context.definition(id), m_context.definition(href)))
return 0;
}
// return successfully parsed filter or NULL
QString n;
if (href.isEmpty())
n = id;
else
n = href;
if (m_filters.contains(n))
return &m_filters[ n ];
else
return 0;
}
SvgClipPathHelper* SvgParser::findClipPath(const QString &id, const QString &href)
{
// check if clip path was already parsed, and return it
if (m_clipPaths.contains(id))
return &m_clipPaths[ id ];
// check if clip path was stored for later parsing
if (!m_context.hasDefinition(id))
return 0;
const KoXmlElement &e = m_context.definition(id);
if (e.childNodesCount() == 0) {
QString mhref = e.attribute("xlink:href").mid(1);
if (m_context.hasDefinition(mhref))
return findClipPath(mhref, id);
else
return 0;
} else {
// ok clip path filter now
if (! parseClipPath(m_context.definition(id), m_context.definition(href)))
return 0;
}
// return successfully parsed clip path or NULL
const QString n = href.isEmpty() ? id : href;
if (m_clipPaths.contains(n))
return &m_clipPaths[ n ];
else
return 0;
}
// Parsing functions
// ---------------------------------------------------------------------------------------
qreal SvgParser::parseUnit(const QString &unit, bool horiz, bool vert, const QRectF &bbox)
{
return SvgUtil::parseUnit(m_context.currentGC(), unit, horiz, vert, bbox);
}
qreal SvgParser::parseUnitX(const QString &unit)
{
return SvgUtil::parseUnitX(m_context.currentGC(), unit);
}
qreal SvgParser::parseUnitY(const QString &unit)
{
return SvgUtil::parseUnitY(m_context.currentGC(), unit);
}
qreal SvgParser::parseUnitXY(const QString &unit)
{
return SvgUtil::parseUnitXY(m_context.currentGC(), unit);
}
bool SvgParser::parseGradient(const KoXmlElement &e, const KoXmlElement &referencedBy)
{
// IMPROVEMENTS:
// - Store the parsed colorstops in some sort of a cache so they don't need to be parsed again.
// - A gradient inherits attributes it does not have from the referencing gradient.
// - Gradients with no color stops have no fill or stroke.
// - Gradients with one color stop have a solid color.
SvgGraphicsContext *gc = m_context.currentGC();
if (!gc)
return false;
SvgGradientHelper gradhelper;
if (e.hasAttribute("xlink:href")) {
QString href = e.attribute("xlink:href").mid(1);
if (! href.isEmpty()) {
// copy the referenced gradient if found
SvgGradientHelper *pGrad = findGradient(href);
if (pGrad)
gradhelper = *pGrad;
} else {
//gc->fillType = SvgGraphicsContext::None; // <--- TODO Fill OR Stroke are none
return false;
}
}
// Use the gradient that is referencing, or if there isn't one, the original gradient.
KoXmlElement b;
if (!referencedBy.isNull())
b = referencedBy;
else
b = e;
QString gradientId = b.attribute("id");
if (! gradientId.isEmpty()) {
// check if we have this gradient already parsed
// copy existing gradient if it exists
if (m_gradients.find(gradientId) != m_gradients.end())
gradhelper.copyGradient(m_gradients[ gradientId ].gradient());
}
if (b.attribute("gradientUnits") == "userSpaceOnUse")
gradhelper.setGradientUnits(SvgGradientHelper::UserSpaceOnUse);
// parse color prop
QColor c = gc->currentColor;
if (!b.attribute("color").isEmpty()) {
m_context.styleParser().parseColor(c, b.attribute("color"));
} else {
// try style attr
QString style = b.attribute("style").simplified();
QStringList substyles = style.split(';', QString::SkipEmptyParts);
for (QStringList::Iterator it = substyles.begin(); it != substyles.end(); ++it) {
QStringList substyle = it->split(':');
QString command = substyle[0].trimmed();
QString params = substyle[1].trimmed();
if (command == "color")
m_context.styleParser().parseColor(c, params);
}
}
gc->currentColor = c;
if (b.tagName() == "linearGradient") {
QLinearGradient *g = new QLinearGradient();
if (gradhelper.gradientUnits() == SvgGradientHelper::ObjectBoundingBox) {
g->setCoordinateMode(QGradient::ObjectBoundingMode);
g->setStart(QPointF(SvgUtil::fromPercentage(b.attribute("x1", "0%")),
SvgUtil::fromPercentage(b.attribute("y1", "0%"))));
g->setFinalStop(QPointF(SvgUtil::fromPercentage(b.attribute("x2", "100%")),
SvgUtil::fromPercentage(b.attribute("y2", "0%"))));
} else {
g->setStart(QPointF(SvgUtil::fromUserSpace(b.attribute("x1").toDouble()),
SvgUtil::fromUserSpace(b.attribute("y1").toDouble())));
g->setFinalStop(QPointF(SvgUtil::fromUserSpace(b.attribute("x2").toDouble()),
SvgUtil::fromUserSpace(b.attribute("y2").toDouble())));
}
// preserve color stops
if (gradhelper.gradient())
g->setStops(gradhelper.gradient()->stops());
gradhelper.setGradient(g);
} else if (b.tagName() == "radialGradient") {
QRadialGradient *g = new QRadialGradient();
if (gradhelper.gradientUnits() == SvgGradientHelper::ObjectBoundingBox) {
g->setCoordinateMode(QGradient::ObjectBoundingMode);
g->setCenter(QPointF(SvgUtil::fromPercentage(b.attribute("cx", "50%")),
SvgUtil::fromPercentage(b.attribute("cy", "50%"))));
g->setRadius(SvgUtil::fromPercentage(b.attribute("r", "50%")));
g->setFocalPoint(QPointF(SvgUtil::fromPercentage(b.attribute("fx", "50%")),
SvgUtil::fromPercentage(b.attribute("fy", "50%"))));
} else {
g->setCenter(QPointF(SvgUtil::fromUserSpace(b.attribute("cx").toDouble()),
SvgUtil::fromUserSpace(b.attribute("cy").toDouble())));
g->setFocalPoint(QPointF(SvgUtil::fromUserSpace(b.attribute("fx").toDouble()),
SvgUtil::fromUserSpace(b.attribute("fy").toDouble())));
g->setRadius(SvgUtil::fromUserSpace(b.attribute("r").toDouble()));
}
// preserve color stops
if (gradhelper.gradient())
g->setStops(gradhelper.gradient()->stops());
gradhelper.setGradient(g);
} else {
return false;
}
// handle spread method
QString spreadMethod = b.attribute("spreadMethod");
if (!spreadMethod.isEmpty()) {
if (spreadMethod == "reflect")
gradhelper.gradient()->setSpread(QGradient::ReflectSpread);
else if (spreadMethod == "repeat")
gradhelper.gradient()->setSpread(QGradient::RepeatSpread);
else
gradhelper.gradient()->setSpread(QGradient::PadSpread);
} else
gradhelper.gradient()->setSpread(QGradient::PadSpread);
// Parse the color stops. The referencing gradient does not have colorstops,
// so use the stops from the gradient it references to (e in this case and not b)
m_context.styleParser().parseColorStops(gradhelper.gradient(), e);
gradhelper.setTransform(SvgUtil::parseTransform(b.attribute("gradientTransform")));
m_gradients.insert(gradientId, gradhelper);
return true;
}
void SvgParser::parsePattern(SvgPatternHelper &pattern, const KoXmlElement &e)
{
if (e.attribute("patternUnits") == "userSpaceOnUse") {
pattern.setPatternUnits(SvgPatternHelper::UserSpaceOnUse);
}
if (e.attribute("patternContentUnits") == "objectBoundingBox") {
pattern.setPatternContentUnits(SvgPatternHelper::ObjectBoundingBox);
}
const QString viewBox = e.attribute("viewBox");
if (!viewBox.isEmpty()) {
pattern.setPatternContentViewbox(SvgUtil::parseViewBox(viewBox));
}
const QString transform = e.attribute("patternTransform");
if (!transform.isEmpty()) {
pattern.setTransform(SvgUtil::parseTransform(transform));
}
const QString x = e.attribute("x");
const QString y = e.attribute("y");
const QString w = e.attribute("width");
const QString h = e.attribute("height");
// parse tile reference rectangle
if (pattern.patternUnits() == SvgPatternHelper::UserSpaceOnUse) {
if (!x.isEmpty() && !y.isEmpty()) {
pattern.setPosition(QPointF(parseUnitX(x), parseUnitY(y)));
}
if (!w.isEmpty() && !h.isEmpty()) {
pattern.setSize(QSizeF(parseUnitX(w), parseUnitY(h)));
}
} else {
// x, y, width, height are in percentages of the object referencing the pattern
// so we just parse the percentages
if (!x.isEmpty() && !y.isEmpty()) {
pattern.setPosition(QPointF(SvgUtil::fromPercentage(x), SvgUtil::fromPercentage(y)));
}
if (!w.isEmpty() && !h.isEmpty()) {
pattern.setSize(QSizeF(SvgUtil::fromPercentage(w), SvgUtil::fromPercentage(h)));
}
}
if (e.hasChildNodes()) {
pattern.setContent(e);
}
}
bool SvgParser::parseFilter(const KoXmlElement &e, const KoXmlElement &referencedBy)
{
SvgFilterHelper filter;
// Use the filter that is referencing, or if there isn't one, the original filter
KoXmlElement b;
if (!referencedBy.isNull())
b = referencedBy;
else
b = e;
// check if we are referencing another filter
if (e.hasAttribute("xlink:href")) {
QString href = e.attribute("xlink:href").mid(1);
if (! href.isEmpty()) {
// copy the referenced filter if found
SvgFilterHelper *refFilter = findFilter(href);
if (refFilter)
filter = *refFilter;
}
} else {
filter.setContent(b);
}
if (b.attribute("filterUnits") == "userSpaceOnUse")
filter.setFilterUnits(SvgFilterHelper::UserSpaceOnUse);
if (b.attribute("primitiveUnits") == "objectBoundingBox")
filter.setPrimitiveUnits(SvgFilterHelper::ObjectBoundingBox);
// parse filter region rectangle
if (filter.filterUnits() == SvgFilterHelper::UserSpaceOnUse) {
filter.setPosition(QPointF(parseUnitX(b.attribute("x")),
parseUnitY(b.attribute("y"))));
filter.setSize(QSizeF(parseUnitX(b.attribute("width")),
parseUnitY(b.attribute("height"))));
} else {
// x, y, width, height are in percentages of the object referencing the filter
// so we just parse the percentages
filter.setPosition(QPointF(SvgUtil::fromPercentage(b.attribute("x", "-0.1")),
SvgUtil::fromPercentage(b.attribute("y", "-0.1"))));
filter.setSize(QSizeF(SvgUtil::fromPercentage(b.attribute("width", "1.2")),
SvgUtil::fromPercentage(b.attribute("height", "1.2"))));
}
m_filters.insert(b.attribute("id"), filter);
return true;
}
bool SvgParser::parseClipPath(const KoXmlElement &e, const KoXmlElement &referencedBy)
{
SvgClipPathHelper clipPath;
// Use the filter that is referencing, or if there isn't one, the original filter
KoXmlElement b;
if (!referencedBy.isNull())
b = referencedBy;
else
b = e;
// check if we are referencing another clip path
if (e.hasAttribute("xlink:href")) {
QString href = e.attribute("xlink:href").mid(1);
if (! href.isEmpty()) {
// copy the referenced clip path if found
SvgClipPathHelper *refClipPath = findClipPath(href);
if (refClipPath)
clipPath = *refClipPath;
}
} else {
clipPath.setContent(b);
}
if (b.attribute("clipPathUnits") == "objectBoundingBox")
clipPath.setClipPathUnits(SvgClipPathHelper::ObjectBoundingBox);
m_clipPaths.insert(b.attribute("id"), clipPath);
return true;
}
void SvgParser::applyStyle(KoShape *obj, const KoXmlElement &e)
{
applyStyle(obj, m_context.styleParser().collectStyles(e));
}
void SvgParser::applyStyle(KoShape *obj, const SvgStyles &styles)
{
SvgGraphicsContext *gc = m_context.currentGC();
if (!gc)
return;
m_context.styleParser().parseStyle(styles);
if (!obj)
return;
if (!dynamic_cast<KoShapeGroup*>(obj)) {
applyFillStyle(obj);
applyStrokeStyle(obj);
}
applyFilter(obj);
applyClipping(obj);
if (! gc->display)
obj->setVisible(false);
obj->setTransparency(1.0 - gc->opacity);
}
void SvgParser::applyFillStyle(KoShape *shape)
{
SvgGraphicsContext *gc = m_context.currentGC();
if (! gc)
return;
if (gc->fillType == SvgGraphicsContext::None) {
shape->setBackground(0);
} else if (gc->fillType == SvgGraphicsContext::Solid) {
shape->setBackground(new KoColorBackground(gc->fillColor));
} else if (gc->fillType == SvgGraphicsContext::Complex) {
// try to find referenced gradient
SvgGradientHelper *gradient = findGradient(gc->fillId);
if (gradient) {
// great, we have a gradient fill
KoGradientBackground *bg = 0;
if (gradient->gradientUnits() == SvgGradientHelper::ObjectBoundingBox) {
bg = new KoGradientBackground(*gradient->gradient());
bg->setTransform(gradient->transform());
} else {
QGradient *convertedGradient = SvgGradientHelper::convertGradient(gradient->gradient(), shape->size());
bg = new KoGradientBackground(*convertedGradient);
delete convertedGradient;
QTransform invShapematrix = shape->transformation().inverted();
bg->setTransform(gradient->transform() * gc->matrix * invShapematrix);
}
shape->setBackground(bg);
} else {
// try to find referenced pattern
SvgPatternHelper *pattern = findPattern(gc->fillId);
KoImageCollection *imageCollection = m_documentResourceManager->imageCollection();
if (pattern && imageCollection) {
// great we have a pattern fill
QRectF objectBound = QRectF(QPoint(), shape->size());
QRectF currentBoundbox = gc->currentBoundbox;
// properties from the object are not inherited
// so we are creating a new context without copying
SvgGraphicsContext *gc = m_context.pushGraphicsContext(pattern->content(), false);
// the pattern establishes a new coordinate system with its
// origin at the patterns x and y attributes
gc->matrix = QTransform();
// object bounding box units are relative to the object the pattern is applied
if (pattern->patternContentUnits() == SvgPatternHelper::ObjectBoundingBox) {
gc->currentBoundbox = objectBound;
gc->forcePercentage = true;
} else {
// inherit the current bounding box
gc->currentBoundbox = currentBoundbox;
}
applyStyle(0, pattern->content());
// parse the pattern content elements
QList<KoShape*> patternContent = parseContainer(pattern->content());
// generate the pattern image from the shapes and the object bounding rect
QImage image = pattern->generateImage(objectBound, patternContent);
m_context.popGraphicsContext();
// delete the shapes created from the pattern content
qDeleteAll(patternContent);
if (!image.isNull()) {
KoPatternBackground *bg = new KoPatternBackground(imageCollection);
bg->setPattern(image);
QPointF refPoint = shape->documentToShape(pattern->position(objectBound));
QSizeF tileSize = pattern->size(objectBound);
bg->setPatternDisplaySize(tileSize);
if (pattern->patternUnits() == SvgPatternHelper::ObjectBoundingBox) {
if (tileSize == objectBound.size())
bg->setRepeat(KoPatternBackground::Stretched);
}
// calculate pattern reference point offset in percent of tileSize
// and relative to the topleft corner of the shape
qreal fx = refPoint.x() / tileSize.width();
qreal fy = refPoint.y() / tileSize.height();
if (fx < 0.0)
fx = floor(fx);
else if (fx > 1.0)
fx = ceil(fx);
else
fx = 0.0;
if (fy < 0.0)
fy = floor(fy);
else if (fx > 1.0)
fy = ceil(fy);
else
fy = 0.0;
qreal offsetX = 100.0 * (refPoint.x() - fx * tileSize.width()) / tileSize.width();
qreal offsetY = 100.0 * (refPoint.y() - fy * tileSize.height()) / tileSize.height();
bg->setReferencePointOffset(QPointF(offsetX, offsetY));
shape->setBackground(bg);
}
} else {
// no referenced fill found, use fallback color
shape->setBackground(new KoColorBackground(gc->fillColor));
}
}
}
KoPathShape *path = dynamic_cast<KoPathShape*>(shape);
if (path)
path->setFillRule(gc->fillRule);
}
void SvgParser::applyStrokeStyle(KoShape *shape)
{
SvgGraphicsContext *gc = m_context.currentGC();
if (! gc)
return;
if (gc->strokeType == SvgGraphicsContext::None) {
shape->setBorder(0);
} else if (gc->strokeType == SvgGraphicsContext::Solid) {
double lineWidth = gc->stroke.lineWidth();
QVector<qreal> dashes = gc->stroke.lineDashes();
KoLineBorder *border = new KoLineBorder(gc->stroke);
// apply line width to dashes and dash offset
if (dashes.count() && lineWidth > 0.0) {
QVector<qreal> dashes = border->lineDashes();
for (int i = 0; i < dashes.count(); ++i)
dashes[i] /= lineWidth;
double dashOffset = border->dashOffset();
border->setLineStyle(Qt::CustomDashLine, dashes);
border->setDashOffset(dashOffset / lineWidth);
} else {
border->setLineStyle(Qt::SolidLine, QVector<qreal>());
}
shape->setBorder(border);
} else if (gc->strokeType == SvgGraphicsContext::Complex) {
// try to find referenced gradient
SvgGradientHelper *gradient = findGradient(gc->strokeId);
if (gradient) {
// great, we have a gradient stroke
QBrush brush;
if (gradient->gradientUnits() == SvgGradientHelper::ObjectBoundingBox) {
brush = *gradient->gradient();
brush.setTransform(gradient->transform());
} else {
QGradient *convertedGradient(SvgGradientHelper::convertGradient(gradient->gradient(), shape->size()));
brush = *convertedGradient;
delete convertedGradient;
brush.setTransform(gradient->transform() * gc->matrix * shape->transformation().inverted());
}
KoLineBorder *border = new KoLineBorder(gc->stroke);
border->setLineBrush(brush);
border->setLineStyle(Qt::SolidLine, QVector<qreal>());
shape->setBorder(border);
} else {
// no referenced stroke found, use fallback color
KoLineBorder *border = new KoLineBorder(gc->stroke);
border->setLineStyle(Qt::SolidLine, QVector<qreal>());
shape->setBorder(border);
}
}
}
void SvgParser::applyFilter(KoShape *shape)
{
SvgGraphicsContext *gc = m_context.currentGC();
if (! gc)
return;
if (gc->filterId.isEmpty())
return;
SvgFilterHelper *filter = findFilter(gc->filterId);
if (! filter)
return;
KoXmlElement content = filter->content();
// parse filter region
QRectF bound(shape->position(), shape->size());
// work on bounding box without viewbox tranformation applied
// so user space coordinates of bounding box and filter region match up
bound = gc->viewboxTransform.inverted().mapRect(bound);
QRectF filterRegion(filter->position(bound), filter->size(bound));
// convert filter region to boundingbox units
QRectF objectFilterRegion;
objectFilterRegion.setTopLeft(SvgUtil::userSpaceToObject(filterRegion.topLeft(), bound));
objectFilterRegion.setSize(SvgUtil::userSpaceToObject(filterRegion.size(), bound));
KoFilterEffectLoadingContext context(m_context.xmlBaseDir());
context.setShapeBoundingBox(bound);
// enable units conversion
context.enableFilterUnitsConversion(filter->filterUnits() == SvgFilterHelper::UserSpaceOnUse);
context.enableFilterPrimitiveUnitsConversion(filter->primitiveUnits() == SvgFilterHelper::UserSpaceOnUse);
KoFilterEffectRegistry *registry = KoFilterEffectRegistry::instance();
KoFilterEffectStack *filterStack = 0;
QSet<QString> stdInputs;
stdInputs << "SourceGraphic" << "SourceAlpha";
stdInputs << "BackgroundImage" << "BackgroundAlpha";
stdInputs << "FillPaint" << "StrokePaint";
QMap<QString, KoFilterEffect*> inputs;
// create the filter effects and add them to the shape
for (KoXmlNode n = content.firstChild(); !n.isNull(); n = n.nextSibling()) {
KoXmlElement primitive = n.toElement();
KoFilterEffect *filterEffect = registry->createFilterEffectFromXml(primitive, context);
if (!filterEffect) {
kWarning(30514) << "filter effect" << primitive.tagName() << "is not implemented yet";
continue;
}
const QString input = primitive.attribute("in");
if (!input.isEmpty()) {
filterEffect->setInput(0, input);
}
const QString output = primitive.attribute("result");
if (!output.isEmpty()) {
filterEffect->setOutput(output);
}
QRectF subRegion;
// parse subregion
if (filter->primitiveUnits() == SvgFilterHelper::UserSpaceOnUse) {
const QString xa = primitive.attribute("x");
const QString ya = primitive.attribute("y");
const QString wa = primitive.attribute("width");
const QString ha = primitive.attribute("height");
if (xa.isEmpty() || ya.isEmpty() || wa.isEmpty() || ha.isEmpty()) {
bool hasStdInput = false;
bool isFirstEffect = filterStack == 0;
// check if one of the inputs is a standard input
foreach(const QString &input, filterEffect->inputs()) {
if ((isFirstEffect && input.isEmpty()) || stdInputs.contains(input)) {
hasStdInput = true;
break;
}
}
if (hasStdInput || primitive.tagName() == "feImage") {
// default to 0%, 0%, 100%, 100%
subRegion.setTopLeft(QPointF(0, 0));
subRegion.setSize(QSizeF(1, 1));
} else {
// defaults to bounding rect of all referenced nodes
foreach(const QString &input, filterEffect->inputs()) {
if (!inputs.contains(input))
continue;
KoFilterEffect *inputFilter = inputs[input];
if (inputFilter)
subRegion |= inputFilter->filterRect();
}
}
} else {
const qreal x = parseUnitX(xa);
const qreal y = parseUnitY(ya);
const qreal w = parseUnitX(wa);
const qreal h = parseUnitY(ha);
subRegion.setTopLeft(SvgUtil::userSpaceToObject(QPointF(x, y), bound));
subRegion.setSize(SvgUtil::userSpaceToObject(QSizeF(w, h), bound));
}
} else {
// x, y, width, height are in percentages of the object referencing the filter
// so we just parse the percentages
const qreal x = SvgUtil::fromPercentage(primitive.attribute("x", "0"));
const qreal y = SvgUtil::fromPercentage(primitive.attribute("y", "0"));
const qreal w = SvgUtil::fromPercentage(primitive.attribute("width", "1"));
const qreal h = SvgUtil::fromPercentage(primitive.attribute("height", "1"));
subRegion = QRectF(QPointF(x, y), QSizeF(w, h));
}
filterEffect->setFilterRect(subRegion);
if (!filterStack)
filterStack = new KoFilterEffectStack();
filterStack->appendFilterEffect(filterEffect);
inputs[filterEffect->output()] = filterEffect;
}
if (filterStack) {
filterStack->setClipRect(objectFilterRegion);
shape->setFilterEffectStack(filterStack);
}
}
void SvgParser::applyClipping(KoShape *shape)
{
SvgGraphicsContext *gc = m_context.currentGC();
if (! gc)
return;
if (gc->clipPathId.isEmpty())
return;
SvgClipPathHelper *clipPath = findClipPath(gc->clipPathId);
if (! clipPath)
return;
kDebug(30514) << "applying clip path" << gc->clipPathId << "clip rule" << gc->clipRule;
const bool boundingBoxUnits = clipPath->clipPathUnits() == SvgClipPathHelper::ObjectBoundingBox;
kDebug(30514) << "using" << (boundingBoxUnits ? "boundingBoxUnits" : "userSpaceOnUse");
QTransform shapeMatrix = shape->absoluteTransformation(0);
// TODO:
// clip path element can have a clip-path property
// -> clip-path = intersection of children with referenced clip-path
// any of its children can have a clip-path property
// -> child element is clipped and the ORed with other children
m_context.pushGraphicsContext();
if (boundingBoxUnits) {
SvgGraphicsContext *gc = m_context.currentGC();
gc->matrix.reset();
gc->viewboxTransform.reset();
gc->currentBoundbox = shape->boundingRect();
gc->forcePercentage = true;
}
QList<KoShape*> clipShapes = parseContainer(clipPath->content());
QList<KoPathShape*> pathShapes;
while (!clipShapes.isEmpty()) {
KoShape *clipShape = clipShapes.first();
clipShapes.removeFirst();
// remove clip shape from list of all parsed shapes
m_shapes.removeOne(clipShape);
// check if we have a path shape
KoPathShape *path = dynamic_cast<KoPathShape*>(clipShape);
if (!path) {
// if shape is a group, ungroup and add children to lits of clip shapes
KoShapeGroup *group = dynamic_cast<KoShapeGroup*>(clipShape);
if (group) {
QList<KoShape*> groupedShapes = group->shapes();
KoShapeUngroupCommand cmd(group, groupedShapes);
cmd.redo();
clipShapes.append(groupedShapes);
} else {
// shape is not a group shape, use its outline as clip path
QPainterPath outline = clipShape->absoluteTransformation(0).map(clipShape->outline());
path = KoPathShape::createShapeFromPainterPath(outline);
}
delete clipShape;
}
if (path) {
kDebug(30514) << "using shape" << path->name() << "as clip path";
pathShapes.append(path);
if (boundingBoxUnits)
path->applyAbsoluteTransformation(shapeMatrix);
}
}
m_context.popGraphicsContext();
if (pathShapes.count()) {
QTransform transformToShape;
if (!boundingBoxUnits)
transformToShape = shape->absoluteTransformation(0).inverted();
KoClipData *clipData = new KoClipData(pathShapes);
KoClipPath *clipPath = new KoClipPath(shape, clipData);
clipPath->setClipRule(gc->clipRule);
shape->setClipPath(clipPath);
}
}
QList<KoShape*> SvgParser::parseUse(const KoXmlElement &e)
{
QList<KoShape*> shapes;
QString id = e.attribute("xlink:href");
//
if (!id.isEmpty()) {
SvgGraphicsContext *gc = m_context.pushGraphicsContext(e);
// TODO: use width and height attributes too
gc->matrix.translate(parseUnitX(e.attribute("x", "0")), parseUnitY(e.attribute("y", "0")));
QString key = id.mid(1);
if (m_context.hasDefinition(key)) {
const KoXmlElement &a = m_context.definition(key);
SvgStyles styles = m_context.styleParser().mergeStyles(e, a);
if (a.tagName() == "g" || a.tagName() == "a" || a.tagName() == "symbol") {
m_context.pushGraphicsContext(a);
KoShapeGroup *group = new KoShapeGroup();
group->setZIndex(m_context.nextZIndex());
applyStyle(0, styles);
m_context.styleParser().parseFont(styles);
QList<KoShape*> childShapes = parseContainer(a);
// handle id
applyId(a.attribute("id"), group);
addToGroup(childShapes, group);
applyStyle(group, styles); // apply style to group after size is set
shapes.append(group);
m_context.popGraphicsContext();
} else {
// Create the object with the merged styles.
// The object inherits all style attributes from the use tag, but keeps it's own attributes.
// So, not just use the style attributes of the use tag, but merge them first.
KoShape *shape = createObject(a, styles);
if (shape)
shapes.append(shape);
}
} else {
// TODO: any named object can be referenced too
}
m_context.popGraphicsContext();
}
return shapes;
}
void SvgParser::addToGroup(QList<KoShape*> shapes, KoShapeGroup *group)
{
m_shapes += shapes;
if (! group)
return;
KoShapeGroupCommand cmd(group, shapes);
cmd.redo();
}
QList<KoShape*> SvgParser::parseSvg(const KoXmlElement &e, QSizeF *fragmentSize)
{
// check if we are the root svg element
const bool isRootSvg = !m_context.currentGC();
SvgGraphicsContext *gc = m_context.pushGraphicsContext();
applyStyle(0, e);
QRectF viewBox;
const QString viewBoxStr = e.attribute("viewBox");
if (!viewBoxStr.isEmpty()) {
viewBox = SvgUtil::parseViewBox(viewBoxStr);
}
const QString w = e.attribute("width");
const QString h = e.attribute("height");
const qreal width = w.isEmpty() ? 550.0 : parseUnit(w, true, false, viewBox);
const qreal height = h.isEmpty() ? 841.0 : parseUnit(h, false, true, viewBox);
QSizeF svgFragmentSize(QSizeF(width, height));
if (fragmentSize)
*fragmentSize = svgFragmentSize;
gc->currentBoundbox = QRectF(QPointF(0, 0), svgFragmentSize);
if (! isRootSvg) {
QTransform move;
// x and y attribute has no meaning for outermost svg elements
const qreal x = parseUnit(e.attribute("x", "0"));
const qreal y = parseUnit(e.attribute("y", "0"));
move.translate(x, y);
gc->matrix = move * gc->matrix;
gc->viewboxTransform = move *gc->viewboxTransform;
}
if (!viewBoxStr.isEmpty()) {
QTransform viewTransform;
viewTransform.translate(viewBox.x(), viewBox.y());
viewTransform.scale(width / viewBox.width() , height / viewBox.height());
gc->matrix = viewTransform * gc->matrix;
gc->viewboxTransform = viewTransform *gc->viewboxTransform;
gc->currentBoundbox.setWidth(gc->currentBoundbox.width() * (viewBox.width() / width));
gc->currentBoundbox.setHeight(gc->currentBoundbox.height() * (viewBox.height() / height));
}
QList<KoShape*> shapes = parseContainer(e);
m_context.popGraphicsContext();
return shapes;
}
QList<KoShape*> SvgParser::parseContainer(const KoXmlElement &e)
{
QList<KoShape*> shapes;
// are we parsing a switch container
bool isSwitch = e.tagName() == "switch";
for (KoXmlNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
KoXmlElement b = n.toElement();
if (b.isNull())
continue;
if (isSwitch) {
// if we are parsing a switch check the requiredFeatures, requiredExtensions
// and systemLanguage attributes
// TODO: evaluate feature list
if (b.hasAttribute("requiredFeatures")) {
continue;
}
if (b.hasAttribute("requiredExtensions")) {
// we do not support any extensions
continue;
}
if (b.hasAttribute("systemLanguage")) {
// not implemeted yet
}
}
if (b.tagName() == "svg") {
shapes += parseSvg(b);
} else if (b.tagName() == "g" || b.tagName() == "a" || b.tagName() == "symbol") {
// treat svg link <a> as group so we don't miss its child elements
m_context.pushGraphicsContext(b);
KoShapeGroup *group = new KoShapeGroup();
group->setZIndex(m_context.nextZIndex());
SvgStyles styles = m_context.styleParser().collectStyles(b);
m_context.styleParser().parseFont(styles);
applyStyle(0, styles); // parse style for inheritance
QList<KoShape*> childShapes = parseContainer(b);
// handle id
applyId(b.attribute("id"), group);
addToGroup(childShapes, group);
const QString viewBoxStr = b.attribute("viewBox");
if (!viewBoxStr.isEmpty()) {
QRectF viewBox = SvgUtil::parseViewBox(viewBoxStr);
QTransform viewTransform;
viewTransform.translate(viewBox.x(), viewBox.y());
viewTransform.scale(group->size().width() / viewBox.width() , group->size().height() / viewBox.height());
group->applyAbsoluteTransformation(viewTransform);
}
applyStyle(group, styles); // apply style to this group after size is set
shapes.append(group);
m_context.popGraphicsContext();
} else if (b.tagName() == "switch") {
m_context.pushGraphicsContext(b);
shapes += parseContainer(b);
m_context.popGraphicsContext();
} else if (b.tagName() == "defs") {
parseDefs(b);
} else if (b.tagName() == "linearGradient" || b.tagName() == "radialGradient") {
parseGradient(b);
} else if (b.tagName() == "pattern") {
m_context.addDefinition(b);
} else if (b.tagName() == "filter") {
parseFilter(b);
} else if (b.tagName() == "clipPath") {
parseClipPath(b);
} else if (b.tagName() == "style") {
m_context.addStyleSheet(b);
} else if (b.tagName() == "rect" ||
b.tagName() == "ellipse" ||
b.tagName() == "circle" ||
b.tagName() == "line" ||
b.tagName() == "polyline" ||
b.tagName() == "polygon" ||
b.tagName() == "path" ||
b.tagName() == "image" ||
b.tagName() == "text") {
KoShape *shape = createObject(b);
if (shape)
shapes.append(shape);
} else if (b.tagName() == "use") {
shapes += parseUse(b);
} else {
// this is an unkown element, so try to load it anyway
// there might be a shape that handles that element
KoShape *shape = createObject(b);
if (shape) {
shapes.append(shape);
} else {
continue;
}
}
// if we are parsing a switch, stop after the first supported element
if (isSwitch)
break;
}
return shapes;
}
void SvgParser::parseDefs(const KoXmlElement &e)
{
for (KoXmlNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
KoXmlElement b = n.toElement();
if (b.isNull())
continue;
if (b.tagName() == "style") {
m_context.addStyleSheet(b);
} else if (b.tagName() == "defs") {
parseDefs(b);
} else {
m_context.addDefinition(b);
}
}
}
// Creating functions
// ---------------------------------------------------------------------------------------
KoShape * SvgParser::createPath(const KoXmlElement &element)
{
KoShape *obj = 0;
if (element.tagName() == "line") {
KoPathShape *path = static_cast<KoPathShape*>(createShape(KoPathShapeId));
if (path) {
double x1 = element.attribute("x1").isEmpty() ? 0.0 : parseUnitX(element.attribute("x1"));
double y1 = element.attribute("y1").isEmpty() ? 0.0 : parseUnitY(element.attribute("y1"));
double x2 = element.attribute("x2").isEmpty() ? 0.0 : parseUnitX(element.attribute("x2"));
double y2 = element.attribute("y2").isEmpty() ? 0.0 : parseUnitY(element.attribute("y2"));
path->clear();
path->moveTo(QPointF(x1, y1));
path->lineTo(QPointF(x2, y2));
path->normalize();
obj = path;
}
} else if (element.tagName() == "polyline" || element.tagName() == "polygon") {
KoPathShape *path = static_cast<KoPathShape*>(createShape(KoPathShapeId));
if (path) {
path->clear();
bool bFirst = true;
QString points = element.attribute("points").simplified();
points.replace(',', ' ');
points.remove('\r');
points.remove('\n');
QStringList pointList = points.split(' ', QString::SkipEmptyParts);
for (QStringList::Iterator it = pointList.begin(); it != pointList.end(); ++it) {
QPointF point;
point.setX(SvgUtil::fromUserSpace((*it).toDouble()));
++it;
if (it == pointList.end())
break;
point.setY(SvgUtil::fromUserSpace((*it).toDouble()));
if (bFirst) {
path->moveTo(point);
bFirst = false;
} else
path->lineTo(point);
}
if (element.tagName() == "polygon")
path->close();
path->setPosition(path->normalize());
obj = path;
}
} else if (element.tagName() == "path") {
KoPathShape *path = static_cast<KoPathShape*>(createShape(KoPathShapeId));
if (path) {
path->clear();
KoPathShapeLoader loader(path);
loader.parseSvg(element.attribute("d"), true);
path->setPosition(path->normalize());
QPointF newPosition = QPointF(SvgUtil::fromUserSpace(path->position().x()),
SvgUtil::fromUserSpace(path->position().y()));
QSizeF newSize = QSizeF(SvgUtil::fromUserSpace(path->size().width()),
SvgUtil::fromUserSpace(path->size().height()));
path->setSize(newSize);
path->setPosition(newPosition);
obj = path;
}
}
return obj;
}
KoShape * SvgParser::createObject(const KoXmlElement &b, const SvgStyles &style)
{
m_context.pushGraphicsContext(b);
KoShape *obj = createShapeFromElement(b, m_context);
if (obj) {
obj->applyAbsoluteTransformation(m_context.currentGC()->matrix);
SvgStyles objStyle = style.isEmpty() ? m_context.styleParser().collectStyles(b) : style;
m_context.styleParser().parseFont(objStyle);
applyStyle(obj, objStyle);
// handle id
applyId(b.attribute("id"), obj);
obj->setZIndex(m_context.nextZIndex());
}
m_context.popGraphicsContext();
return obj;
}
KoShape * SvgParser::createShapeFromElement(const KoXmlElement &element, SvgLoadingContext &context)
{
KoShape *object = 0;
QList<KoShapeFactoryBase*> factories = KoShapeRegistry::instance()->factoriesForElement(KoXmlNS::svg, element.tagName());
foreach (KoShapeFactoryBase *f, factories) {
KoShape *shape = f->createDefaultShape(m_documentResourceManager);
if (!shape)
continue;
SvgShape *svgShape = dynamic_cast<SvgShape*>(shape);
if (!svgShape) {
delete shape;
continue;
}
// reset transformation that might come from the default shape
shape->setTransformation(QTransform());
// reset border
KoShapeBorderModel *oldBorder = shape->border();
shape->setBorder(0);
delete oldBorder;
// reset fill
KoShapeBackground *oldFill = shape->background();
shape->setBackground(0);
delete oldFill;
if (!svgShape->loadSvg(element, context)) {
delete shape;
continue;
}
object = shape;
break;
}
if (!object) {
object = createPath(element);
}
return object;
}
KoShape * SvgParser::createShape(const QString &shapeID)
{
KoShapeFactoryBase *factory = KoShapeRegistry::instance()->get(shapeID);
if (!factory) {
kWarning(30514) << "Could not find factory for shape id" << shapeID;
return 0;
}
KoShape *shape = factory->createDefaultShape(m_documentResourceManager);
if (!shape) {
kWarning(30514) << "Could not create Default shape for shape id" << shapeID;
return 0;
}
if (shape->shapeId().isEmpty())
shape->setShapeId(factory->id());
// reset tranformation that might come from the default shape
shape->setTransformation(QTransform());
// reset border
KoShapeBorderModel *oldBorder = shape->border();
shape->setBorder(0);
delete oldBorder;
// reset fill
KoShapeBackground *oldFill = shape->background();
shape->setBackground(0);
delete oldFill;
return shape;
}
void SvgParser::applyId(const QString &id, KoShape *shape)
{
if (id.isEmpty())
return;
shape->setName(id);
m_context.registerShape(id, shape);
}
|