File indexing completed on 2024-12-22 04:10:13

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_asl_parser_test.h"
0008 
0009 #include <simpletest.h>
0010 
0011 #include <testutil.h>
0012 #include "testimage.h"
0013 
0014 #include <QDomDocument>
0015 
0016 #include <resources/KoPattern.h>
0017 
0018 #include <asl/kis_asl_reader.h>
0019 #include <asl/kis_asl_xml_parser.h>
0020 #include <asl/kis_asl_object_catcher.h>
0021 #include <asl/kis_asl_callback_object_catcher.h>
0022 
0023 #include <kis_debug.h>
0024 
0025 void KisAslParserTest::test()
0026 {
0027     QString fileName(TestUtil::fetchDataFileLazy("asl/freebie.asl"));
0028     QFile aslFile(fileName);
0029     aslFile.open(QIODevice::ReadOnly);
0030 
0031     KisAslReader reader;
0032     QDomDocument doc = reader.readFile(aslFile);
0033 
0034     dbgKrita << ppVar(doc.toString());
0035 
0036     KisAslObjectCatcher trivialCatcher;
0037     KisAslXmlParser parser;
0038     parser.parseXML(doc, trivialCatcher);
0039 }
0040 
0041 struct CallbackVerifier {
0042     CallbackVerifier() : m_numCallsHappened(0) {}
0043 
0044     void setColor(const KoColor &color) {
0045         KoColor c = KoColor::fromXML("<color channeldepth='U8'><sRGB r='1.0' g='1.0' b='1.0'/></color>");
0046         QVERIFY(color == c);
0047         m_numCallsHappened++;
0048     }
0049 
0050     void setOpacity(double opacity) {
0051         QVERIFY(qFuzzyCompare(opacity, 75));
0052         m_numCallsHappened++;
0053     }
0054 
0055     void setBlendingMode(const QString &mode) {
0056         QVERIFY(mode == "Scrn");
0057         m_numCallsHappened++;
0058     }
0059 
0060     void setEnabled(bool value) {
0061         QVERIFY(value);
0062         m_numCallsHappened++;
0063     }
0064 
0065     void setCurve(const QString &name, const QVector<QPointF> &points) {
0066         QCOMPARE(name, QString("Linear"));
0067         QCOMPARE(points[0], QPointF());
0068         QCOMPARE(points[1], QPointF(255.0, 255.0));
0069         m_numCallsHappened++;
0070     }
0071 
0072     void setText(const QString &text) {
0073         QCOMPARE(text, QString("11adf7a2-a120-11e1-957c-d1ee226781a4"));
0074         m_numCallsHappened++;
0075     }
0076 
0077     void setPattern(const KoPatternSP pattern) {
0078         dbgKrita << ppVar(pattern->name());
0079         dbgKrita << ppVar(pattern->filename());
0080 
0081         //QCOMPARE(text, QString("11adf7a2-a120-11e1-957c-d1ee226781a4"));
0082         m_numCallsHappened++;
0083     }
0084 
0085     int m_numCallsHappened;
0086 };
0087 
0088 void KisAslParserTest::testWithCallbacks()
0089 {
0090     using namespace std::placeholders;
0091     QString fileName(TestUtil::fetchDataFileLazy("asl/freebie.asl"));
0092     QFile aslFile(fileName);
0093     aslFile.open(QIODevice::ReadOnly);
0094 
0095 
0096     KisAslReader reader;
0097     QDomDocument doc = reader.readFile(aslFile);
0098 
0099     KisAslCallbackObjectCatcher c;
0100 
0101     CallbackVerifier verifier;
0102 
0103     c.subscribeColor("/Styl/Lefx/IrGl/Clr ", std::bind(&CallbackVerifier::setColor, &verifier, _1));
0104     c.subscribeUnitFloat("/Styl/Lefx/IrGl/Opct", "#Prc", std::bind(&CallbackVerifier::setOpacity, &verifier, _1));
0105     c.subscribeEnum("/Styl/Lefx/IrGl/Md  ", "BlnM", std::bind(&CallbackVerifier::setBlendingMode, &verifier, _1));
0106     c.subscribeBoolean("/Styl/Lefx/IrGl/enab", std::bind(&CallbackVerifier::setEnabled, &verifier, _1));
0107     c.subscribeCurve("/Styl/Lefx/OrGl/TrnS", std::bind(&CallbackVerifier::setCurve, &verifier, _1, _2));
0108     c.subscribeText("/null/Idnt", std::bind(&CallbackVerifier::setText, &verifier, _1));
0109 
0110     KisAslXmlParser parser;
0111     parser.parseXML(doc, c);
0112 
0113     QCOMPARE(verifier.m_numCallsHappened, 6);
0114 }
0115 
0116 #include <asl/kis_asl_xml_writer.h>
0117 
0118 void KisAslParserTest::testASLXMLWriter()
0119 {
0120     KisAslXmlWriter w;
0121 
0122     QImage testImage(QSize(16, 16), QImage::Format_ARGB32);
0123     KoPatternSP testPattern1(new KoPattern(testImage, "Some very nice name ;)", ""));
0124     KoPatternSP testPattern2(new KoPattern(testImage, "Another very nice name ;P", ""));
0125 
0126     w.enterList(ResourceType::Patterns);
0127     w.writePattern("", testPattern1);
0128     w.writePattern("", testPattern2);
0129     w.leaveList();
0130 
0131     w.enterDescriptor("", "", "null");
0132     w.writeText("Nm  ", "www.designpanoply.com - Freebie 5");
0133     w.writeText("Idnt", "11adf7a2-a120-11e1-957c-d1ee226781a4");
0134     w.leaveDescriptor();
0135 
0136     w.enterDescriptor("", "", "Styl");
0137 
0138     w.enterDescriptor("documentMode", "", "documentMode");
0139     w.leaveDescriptor();
0140 
0141     w.enterDescriptor("Lefx", "", "Lefx");
0142 
0143     w.writeUnitFloat("Scl ", "#Prc", 100);
0144     w.writeBoolean("masterFxSwitch", true);
0145 
0146     w.enterDescriptor("DrSh", "", "DrSh");
0147 
0148     w.writeBoolean("enab", true);
0149     w.writeEnum("Md  ", "BlnM", "Mltp");
0150     KoColor c;
0151     c.fromQColor(Qt::green);
0152     w.writeColor("Clr ", c);
0153 
0154 
0155     w.writeUnitFloat("Opct", "#Prc", 16);
0156     w.writeBoolean("uglg", false);
0157     w.writeUnitFloat("lagl", "#Prc", 100);
0158     w.writeUnitFloat("Dstn", "#Pxl", 100);
0159     w.writeUnitFloat("Ckmt", "#Pxl", 100);
0160     w.writeUnitFloat("blur", "#Pxl", 100);
0161     w.writeUnitFloat("Nose", "#Prc", 100);
0162 
0163     w.writeBoolean("anta", true);
0164 
0165     w.writeCurve("TrnS",
0166                  "Linear",
0167                  QVector<QPointF>() << QPointF() << QPointF(255, 255));
0168 
0169     w.writeBoolean("layerConceals", true);
0170 
0171     w.leaveDescriptor();
0172 
0173     w.leaveDescriptor();
0174 
0175     w.leaveDescriptor();
0176 
0177     dbgKrita << ppVar(w.document().toString());
0178 
0179 }
0180 
0181 #include <resources/KoStopGradient.h>
0182 #include <resources/KoSegmentGradient.h>
0183 
0184 void KisAslParserTest::testWritingGradients()
0185 {
0186     KisAslXmlWriter w1;
0187 
0188     KoSegmentGradient segmentGradient;
0189     segmentGradient.createSegment(INTERP_LINEAR, COLOR_INTERP_RGB,
0190                                   0.0, 0.3, 0.15,
0191                                   Qt::black, Qt::red);
0192     segmentGradient.createSegment(INTERP_LINEAR, COLOR_INTERP_RGB,
0193                                   0.3, 0.6, 0.45,
0194                                   Qt::red, Qt::green);
0195     segmentGradient.createSegment(INTERP_LINEAR, COLOR_INTERP_RGB,
0196                                   0.6, 1.0, 0.8,
0197                                   Qt::green, Qt::white);
0198 
0199     w1.writeSegmentGradient("tstG", segmentGradient);
0200     //dbgKrita << "===";
0201     //dbgKrita << ppVar(w1.document().toString());
0202 
0203     KisAslXmlWriter w2;
0204 
0205     const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
0206 
0207     QList<KoGradientStop> stops;
0208     stops << KoGradientStop(0.0, KoColor(Qt::black, cs), COLORSTOP);
0209     stops << KoGradientStop(0.3, KoColor(Qt::red, cs), COLORSTOP);
0210     stops << KoGradientStop(0.6, KoColor(Qt::green, cs), COLORSTOP);
0211     stops << KoGradientStop(1.0, KoColor(Qt::white, cs), COLORSTOP);
0212 
0213     KoStopGradient stopGradient;
0214     stopGradient.setStops(stops);
0215 
0216     w2.writeStopGradient("tstG", stopGradient);
0217 
0218     //dbgKrita << "===";
0219     //dbgKrita << ppVar(w2.document().toString());
0220 
0221     QCOMPARE(w1.document().toString(),
0222              w2.document().toString());
0223 
0224 }
0225 
0226 #include <asl/kis_asl_writer.h>
0227 
0228 void KisAslParserTest::testASLWriter()
0229 {
0230     //QString srcFileName(TestUtil::fetchDataFileLazy("asl/testset/freebie_with_pattern.asl"));
0231     QString srcFileName(TestUtil::fetchDataFileLazy("asl/freebie.asl"));
0232 
0233     QDomDocument srcDoc;
0234 
0235     {
0236         QFile srcAslFile(srcFileName);
0237         srcAslFile.open(QIODevice::ReadOnly);
0238 
0239         KisAslReader reader;
0240         srcDoc = reader.readFile(srcAslFile);
0241 
0242         QFile tfile("src_parsed.xml");
0243         tfile.open(QIODevice::WriteOnly);
0244         tfile.write(srcDoc.toByteArray());
0245         tfile.close();
0246     }
0247 
0248     QString dstFileName("test.asl");
0249 
0250     {
0251         QFile dstAslFile(dstFileName);
0252         dstAslFile.open(QIODevice::WriteOnly);
0253 
0254         KisAslWriter writer;
0255         writer.writeFile(dstAslFile, srcDoc);
0256 
0257         dstAslFile.flush();
0258         dstAslFile.close();
0259     }
0260 
0261     QDomDocument dstDoc;
0262 
0263     {
0264         QFile roundTripAslFile(dstFileName);
0265         roundTripAslFile.open(QIODevice::ReadOnly);
0266 
0267         KisAslReader reader;
0268         dstDoc = reader.readFile(roundTripAslFile);
0269 
0270         QFile tfile("dst_parsed.xml");
0271         tfile.open(QIODevice::WriteOnly);
0272         tfile.write(dstDoc.toByteArray());
0273         tfile.close();
0274     }
0275 
0276     QCOMPARE(srcDoc.toByteArray(), dstDoc.toByteArray());
0277 }
0278 
0279 void KisAslParserTest::testParserWithPatterns()
0280 {
0281     QDir dir(QString(FILES_DATA_DIR) + '/' + "testset");
0282 
0283     QFileInfoList files = dir.entryInfoList(QStringList() << "*.asl", QDir::Files);
0284 
0285 
0286     int index = 0;
0287 
0288     Q_FOREACH (const QFileInfo &fileInfo, files) {
0289 
0290         //if (index != 12) {index++; continue;}
0291 
0292         dbgKrita << "===" << index << "===";
0293         dbgKrita << ppVar(fileInfo.fileName());
0294 
0295         QFile aslFile(fileInfo.absoluteFilePath());
0296         aslFile.open(QIODevice::ReadOnly);
0297 
0298         KisAslReader reader;
0299         QDomDocument doc = reader.readFile(aslFile);
0300 
0301         QFile xmlFile("mydata.xml");
0302         xmlFile.open(QIODevice::WriteOnly);
0303         xmlFile.write(doc.toByteArray());
0304 
0305         //dbgKrita << ppVar(doc.toString());
0306 
0307         CallbackVerifier verifier;
0308         KisAslCallbackObjectCatcher c;
0309 
0310         c.subscribePattern("/Patterns/KisPattern", std::bind(&CallbackVerifier::setPattern, &verifier, std::placeholders::_1));
0311         c.subscribePattern("/patterns/KisPattern", std::bind(&CallbackVerifier::setPattern, &verifier, std::placeholders::_1));
0312 
0313         KisAslXmlParser parser;
0314         parser.parseXML(doc, c);
0315 
0316         //QCOMPARE(verifier.m_numCallsHappened, 7);
0317 
0318         index++;
0319         //break;
0320     }
0321 }
0322 
0323 KISTEST_MAIN(KisAslParserTest)