File indexing completed on 2024-12-22 04:08:47

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisBrushModelTest.h"
0008 
0009 #include "KisBrushModel.h"
0010 #include "KisGlobalResourcesInterface.h"
0011 
0012 #define TESTBRUSH
0013 #include <kistest.h>
0014 
0015 #include "testutil.h"
0016 #include "KisResourceModel.h"
0017 #include <KisResourceCacheDb.h>
0018 #include "KisResourceLoaderRegistry.h"
0019 #include "KisResourceLoader.h"
0020 #include "KisResourceLocator.h"
0021 #include "kis_png_brush.h"
0022 #include "KoResourcePaths.h"
0023 
0024 void KisBrushModelTest::initTestCase()
0025 {
0026     {
0027         QString fullFileName = TestUtil::fetchDataFileLazy("kritaTransparent.png");
0028         KIS_ASSERT(!fullFileName.isEmpty());
0029         KIS_ASSERT(QFileInfo(fullFileName).exists());
0030 
0031         KisResourceModel model(ResourceType::Brushes);
0032         KoResourceSP result = model.importResourceFile(fullFileName, false);
0033         QVERIFY(result);
0034     }
0035 }
0036 
0037 void KisBrushModelTest::testAutoBrush()
0038 {
0039     KisBrushModel::BrushData data;
0040 
0041     data.type = KisBrushModel::Auto;
0042     data.common.angle = 3;
0043     data.common.spacing = 0.5;
0044     data.common.autoSpacingCoeff = 1.1;
0045     data.common.useAutoSpacing = true;
0046 
0047     data.autoBrush.density = 0.9;
0048     data.autoBrush.randomness = 0.1;
0049     data.autoBrush.generator.type = KisBrushModel::Gaussian;
0050     data.autoBrush.generator.shape = KisBrushModel::Circle;
0051     data.autoBrush.generator.diameter = 77;
0052     data.autoBrush.generator.horizontalFade = 0.5;
0053     data.autoBrush.generator.verticalFade = 0.6;
0054     data.autoBrush.generator.ratio = 0.8;
0055     data.autoBrush.generator.antialiasEdges = false;
0056     data.autoBrush.generator.spikes = 4;
0057 
0058     QCOMPARE(data, data);
0059 
0060     KisPropertiesConfiguration config;
0061 
0062     data.write(&config);
0063 
0064     std::optional<KisBrushModel::BrushData> newData =
0065         KisBrushModel::BrushData::read(&config, KisGlobalResourcesInterface::instance());
0066 
0067     QVERIFY(newData);
0068     QCOMPARE(*newData, data);
0069 }
0070 
0071 #include <KisResourceTypes.h>
0072 
0073 void KisBrushModelTest::testPredefinedBrush()
0074 {
0075     auto source = KisGlobalResourcesInterface::instance()->source<KisBrush>(ResourceType::Brushes);
0076 
0077     KisBrushSP fallbackBrush = source.fallbackResource();
0078     QVERIFY(fallbackBrush);
0079 
0080     KisBrushModel::BrushData data;
0081 
0082     data.type = KisBrushModel::Predefined;
0083     data.common.angle = 3;
0084     data.common.spacing = 0.5;
0085     data.common.autoSpacingCoeff = 1.1;
0086     data.common.useAutoSpacing = true;
0087 
0088     data.predefinedBrush.resourceSignature = fallbackBrush->signature();
0089     data.predefinedBrush.subtype = "png_brush";
0090     data.predefinedBrush.brushType = fallbackBrush->brushType();
0091     data.predefinedBrush.baseSize = { fallbackBrush->width(), fallbackBrush->height()};
0092     data.predefinedBrush.scale = 0.7;
0093     data.predefinedBrush.application = LIGHTNESSMAP;
0094     data.predefinedBrush.hasColorAndTransparency = true;
0095     data.predefinedBrush.autoAdjustMidPoint = true;
0096     data.predefinedBrush.adjustmentMidPoint = 120;
0097     data.predefinedBrush.brightnessAdjustment = 0.3;
0098     data.predefinedBrush.contrastAdjustment = 0.2;
0099 
0100     QCOMPARE(data, data);
0101 
0102     KisPropertiesConfiguration config;
0103 
0104     data.write(&config);
0105 
0106     std::optional<KisBrushModel::BrushData> newData =
0107         KisBrushModel::BrushData::read(&config, KisGlobalResourcesInterface::instance());
0108 
0109     QVERIFY(newData);
0110     QCOMPARE(*newData, data);
0111 }
0112 
0113 void KisBrushModelTest::testTextBrush()
0114 {
0115     KisBrushModel::BrushData data;
0116 
0117     data.type = KisBrushModel::Text;
0118     data.common.spacing = 0.5;
0119 
0120     // FIXME: angle and auto-spacing are not saved yet!
0121     // data.common.angle = 3;
0122     // data.common.autoSpacingCoeff = 1.1;
0123     // data.common.useAutoSpacing = true;
0124 
0125     // TODO: implement calculation of the size from the font
0126     // data.textBrush.baseSize = {10, 11};
0127 
0128     data.textBrush.font = "Font String";
0129 
0130     // FIXME: scale is not saved yet!
0131     //data.textBrush.scale = 0.3;
0132 
0133     data.textBrush.text = "Test text";
0134     data.textBrush.usePipeMode = true;
0135 
0136     QCOMPARE(data, data);
0137 
0138     KisPropertiesConfiguration config;
0139 
0140     data.write(&config);
0141 
0142     config.dump();
0143 
0144     std::optional<KisBrushModel::BrushData> newData =
0145         KisBrushModel::BrushData::read(&config, KisGlobalResourcesInterface::instance());
0146 
0147     QVERIFY(newData);
0148     QCOMPARE(*newData, data);
0149 }
0150 
0151 KISTEST_MAIN(KisBrushModelTest);