[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-kimageshop
Subject:    SVG/PNG Brushes
From:       Cyrille Berger <cberger () cberger ! net>
Date:       2010-10-03 19:22:35
Message-ID: 201010032122.35664.cberger () cberger ! net
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hello,

Today was lets-do-something-new-day for me. So I have implemented two new 
types of brushes, png and svg (because I need it for my nature brush set [1]).
I am sending the patch here for backup purposes, and if anyone wants to use 
it.

If someone wants to commit it to the essen branch, I have no issue, but I 
won't track it there, and will keep working on it locally (and post update 
here), and will not merge it from the branch.

[1] https://bitbucket.org/cyrille/librenatureresourceset
-- 
Cyrille Berger

[Attachment #5 (text/html)]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" \
"http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" \
content="1" /><style type="text/css"> p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'DejaVu Sans Mono'; font-size:9pt; \
font-weight:400; font-style:normal;"> <p style=" margin-top:0px; margin-bottom:0px; \
margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; \
-qt-user-state:0;">Hello,</p> <p style="-qt-paragraph-type:empty; margin-top:0px; \
margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; \
text-indent:0px; -qt-user-state:0;"></p> <p style=" margin-top:0px; \
margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; \
text-indent:0px; -qt-user-state:0;">Today was lets-do-something-new-day for me. So I \
have implemented two new types of brushes, png and svg (because I need it for my \
nature brush set [1]).</p> <p style=" margin-top:0px; margin-bottom:0px; \
margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; \
-qt-user-state:0;">I am sending the patch here for backup purposes, and if anyone \
wants to use it.</p> <p style="-qt-paragraph-type:empty; margin-top:0px; \
margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; \
text-indent:0px; -qt-user-state:0;"></p> <p style=" margin-top:0px; \
margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; \
text-indent:0px; -qt-user-state:0;">If someone wants to commit it to the essen \
branch, I have no issue, but I won't track it there, and will keep working on it \
locally (and post update here), and will not merge it from the branch.</p> <p \
style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; \
margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"></p> <p \
style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; \
-qt-block-indent:0; text-indent:0px; -qt-user-state:0;">[1] \
https://bitbucket.org/cyrille/librenatureresourceset</p> <p style=" margin-top:0px; \
margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; \
text-indent:0px; -qt-user-state:0;">-- </p> <p style=" margin-top:0px; \
margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; \
text-indent:0px; -qt-user-state:0;">Cyrille Berger</p></body></html>


["svg-png-brush.diff" (text/x-patch)]

Index: kis_svg_brush_factory.cpp
===================================================================
--- kis_svg_brush_factory.cpp	(revision 0)
+++ kis_svg_brush_factory.cpp	(revision 0)
@@ -0,0 +1,45 @@
+/*
+ *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "kis_svg_brush_factory.h"
+
+#include <QDomDocument>
+
+#include <KoResourceServer.h>
+#include <KoResourceServerAdapter.h>
+
+#include "kis_brush_server.h"
+
+
+KisSvgBrushFactory::KisSvgBrushFactory()
+{
+}
+
+KisBrushSP KisSvgBrushFactory::getOrCreateBrush(const QDomElement& brushDefinition)
+{
+    KoResourceServer<KisBrush>* rServer = KisBrushServer::instance()->brushServer();
+    QString brushFileName = brushDefinition.attribute("filename", "");
+    KisBrushSP brush = rServer->getResourceByFilename(brushFileName);
+    if(!brush)
+        return 0;
+    
+    double spacing = brushDefinition.attribute("spacing", "0.25").toDouble();
+    brush->setSpacing(spacing);
+
+    return brush;
+}
Index: kis_png_brush.h
===================================================================
--- kis_png_brush.h	(revision 0)
+++ kis_png_brush.h	(revision 0)
@@ -0,0 +1,37 @@
+/*
+ *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef KIS_PNG_BRUSH_
+#define KIS_PNG_BRUSH_
+
+#include "kis_brush.h"
+
+class BRUSH_EXPORT KisPngBrush : public KisBrush
+{
+public:
+    /// Construct brush to load filename later as brush
+    KisPngBrush(const QString& filename);
+    virtual bool load();
+    virtual QString defaultFileExtension() const;
+    void toXML(QDomDocument& d, QDomElement& e) const;
+private:
+    struct Private;
+    Private* const d;
+};
+
+#endif
Index: kis_svg_brush.h
===================================================================
--- kis_svg_brush.h	(revision 0)
+++ kis_svg_brush.h	(revision 0)
@@ -0,0 +1,37 @@
+/*
+ *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef KIS_SVG_BRUSH_
+#define KIS_SVG_BRUSH_
+
+#include "kis_brush.h"
+
+class BRUSH_EXPORT KisSvgBrush : public KisBrush
+{
+public:
+    /// Construct brush to load filename later as brush
+    KisSvgBrush(const QString& filename);
+    virtual bool load();
+    virtual QString defaultFileExtension() const;
+    void toXML(QDomDocument& d, QDomElement& e) const;
+private:
+    struct Private;
+    Private* const d;
+};
+
+#endif
Index: kis_brush_server.cpp
===================================================================
--- kis_brush_server.cpp	(revision 1181489)
+++ kis_brush_server.cpp	(working copy)
@@ -33,13 +33,15 @@
 #include "kis_abr_brush_collection.h"
 #include "kis_gbr_brush.h"
 #include "kis_imagepipe_brush.h"
+#include "kis_png_brush.h"
+#include "kis_svg_brush.h"
 
 class BrushResourceServer : public KoResourceServer<KisBrush>, public \
KoResourceServerObserver<KisBrush>  {
 
 public:
 
-    BrushResourceServer() : KoResourceServer<KisBrush>("kis_brushes", \
"*.gbr:*.gih:*.abr") { +    BrushResourceServer() : \
KoResourceServer<KisBrush>("kis_brushes", "*.gbr:*.gih:*.abr:*.png:*.svg") {  \
addObserver(this, true);  }
 
@@ -120,6 +122,10 @@
             brush = new KisGbrBrush(filename);
         } else if (fileExtension == "gih") {
             brush = new KisImagePipeBrush(filename);
+        } else if (fileExtension == "png") {
+            brush = new KisPngBrush(filename);
+        } else if (fileExtension == "svg") {
+            brush = new KisSvgBrush(filename);
         }
         return brush;
     }
Index: kis_png_brush_factory.h
===================================================================
--- kis_png_brush_factory.h	(revision 0)
+++ kis_png_brush_factory.h	(revision 0)
@@ -0,0 +1,58 @@
+/*
+ *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+#ifndef KIS_PNG_BRUSH_FACTORY
+#define KIS_PNG_BRUSH_FACTORY
+
+#include <QString>
+#include <QDomElement>
+
+#include "kis_brush_factory.h"
+#include "kis_brush.h"
+
+/**
+ * A brush factory can create a new brush instance based
+ * on a properties object that contains a serialized representation
+ * of the object.
+ */
+class BRUSH_EXPORT KisPngBrushFactory : public KisBrushFactory
+{
+
+public:
+
+    /**
+     * Creating the KisBrushFactory will load all png
+     * brushes.
+     */
+    KisPngBrushFactory();
+    virtual ~KisPngBrushFactory() {}
+
+    virtual QString id() const {
+        return "png_brush";
+    }
+
+    /**
+     * Create a new brush from the given data or return an existing KisBrush
+     * object. If this call leads to the creation of a resource, it should be
+     * added to the resource provider, too.
+     */
+    KisBrushSP getOrCreateBrush(const QDomElement& brushDefinition);
+
+
+};
+
+#endif
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(revision 1181489)
+++ CMakeLists.txt	(working copy)
@@ -13,6 +13,10 @@
     kis_abr_brush_collection.cpp
     kis_imagepipe_brush.cpp
     kis_imagepipe_brush_p.cpp
+    kis_png_brush.cpp
+    kis_png_brush_factory.cpp
+    kis_svg_brush.cpp
+    kis_svg_brush_factory.cpp
     kis_qimage_mask.cpp
     kis_scaled_brush.cpp
     kis_text_brush.cpp
Index: kis_png_brush.cpp
===================================================================
--- kis_png_brush.cpp	(revision 0)
+++ kis_png_brush.cpp	(revision 0)
@@ -0,0 +1,76 @@
+/*
+ *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "kis_png_brush.h"
+
+#include <QDomElement>
+#include <QFileInfo>
+#include <QImageReader>
+
+struct KisPngBrush::Private {
+};
+
+KisPngBrush::KisPngBrush(const QString& filename) : KisBrush(filename), d(new \
Private) +{
+    setBrushType(INVALID);
+    setSpacing(0.25);
+    setHasColor(false);
+    
+}
+
+bool KisPngBrush::load()
+{
+    QImageReader reader(filename(), "PNG");
+    if(reader.textKeys().contains("brush_spacing"))
+    {
+        setSpacing(reader.text("brush_spacing").toDouble());
+    }
+    if(reader.textKeys().contains("brush_name"))
+    {
+        setName(reader.text("brush_name"));
+    } else {
+        QFileInfo info(filename());
+        setName(info.baseName());
+    }
+    setImage(reader.read());
+    setValid(!image().isNull());
+    if(image().isGrayscale())
+    {
+        setBrushType(MASK);
+        setHasColor(false);
+    } else {
+        setBrushType(IMAGE);
+        setHasColor(true);
+    }
+    setWidth(image().width());
+    setHeight(image().height());
+    return !image().isNull();
+}
+
+QString KisPngBrush::defaultFileExtension() const
+{
+    return QString(".png");
+}
+
+void KisPngBrush::toXML(QDomDocument& d, QDomElement& e) const
+{
+    Q_UNUSED(d);
+    e.setAttribute("type", "png_brush");
+    e.setAttribute("filename", shortFilename());
+    e.setAttribute("spacing", spacing());
+}
Index: kis_svg_brush_factory.h
===================================================================
--- kis_svg_brush_factory.h	(revision 0)
+++ kis_svg_brush_factory.h	(revision 0)
@@ -0,0 +1,58 @@
+/*
+ *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+#ifndef KIS_SVG_BRUSH_FACTORY
+#define KIS_SVG_BRUSH_FACTORY
+
+#include <QString>
+#include <QDomElement>
+
+#include "kis_brush_factory.h"
+#include "kis_brush.h"
+
+/**
+ * A brush factory can create a new brush instance based
+ * on a properties object that contains a serialized representation
+ * of the object.
+ */
+class BRUSH_EXPORT KisSvgBrushFactory : public KisBrushFactory
+{
+
+public:
+
+    /**
+     * Creating the KisBrushFactory will load all png
+     * brushes.
+     */
+    KisSvgBrushFactory();
+    virtual ~KisSvgBrushFactory() {}
+
+    virtual QString id() const {
+        return "svg_brush";
+    }
+
+    /**
+     * Create a new brush from the given data or return an existing KisBrush
+     * object. If this call leads to the creation of a resource, it should be
+     * added to the resource provider, too.
+     */
+    KisBrushSP getOrCreateBrush(const QDomElement& brushDefinition);
+
+
+};
+
+#endif
Index: kis_svg_brush.cpp
===================================================================
--- kis_svg_brush.cpp	(revision 0)
+++ kis_svg_brush.cpp	(revision 0)
@@ -0,0 +1,85 @@
+/*
+ *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "kis_svg_brush.h"
+
+#include <QDomElement>
+#include <QFileInfo>
+#include <QPainter>
+#include <QImageReader>
+#include <QSvgRenderer>
+
+struct KisSvgBrush::Private {
+};
+
+KisSvgBrush::KisSvgBrush(const QString& filename) : KisBrush(filename), d(new \
Private) +{
+    setBrushType(INVALID);
+    setSpacing(0.25);
+    setHasColor(false);
+    
+}
+
+bool KisSvgBrush::load()
+{
+    QSvgRenderer renderer(filename());
+    
+    QRect box = renderer.viewBox();
+    if(box.isEmpty()) return false;
+    
+    QImage image_(1000, (1000 * box.height()) / box.width(), QImage::Format_ARGB32 \
); +    {
+        QPainter p(&image_);
+        p.fillRect(box, Qt::white);
+        renderer.render(&p);
+    }
+    
+    QVector<QRgb> table;
+    for(int i = 0; i < 256; ++i) table.push_back(qRgb(i,i,i));
+    image_ = image_.convertToFormat(QImage::Format_Indexed8, table);
+    
+    setImage(image_);
+    
+    setValid(true);
+    
+    // Well for now, always true
+    if(image().isGrayscale())
+    {
+        setBrushType(MASK);
+        setHasColor(false);
+    } else {
+        setBrushType(IMAGE);
+        setHasColor(true);
+    }
+    setWidth(image().width());
+    setHeight(image().height());
+    return !image().isNull();
+}
+
+QString KisSvgBrush::defaultFileExtension() const
+{
+    return QString(".svg");
+}
+
+void KisSvgBrush::toXML(QDomDocument& d, QDomElement& e) const
+{
+    Q_UNUSED(d);
+    e.setAttribute("type", "svg_brush");
+    e.setAttribute("filename", shortFilename());
+    e.setAttribute("spacing", spacing());
+}
Index: kis_brush_registry.cpp
===================================================================
--- kis_brush_registry.cpp	(revision 1181489)
+++ kis_brush_registry.cpp	(working copy)
@@ -37,6 +37,8 @@
 #include "kis_gbr_brush_factory.h"
 #include "kis_abr_brush_factory.h"
 #include "kis_text_brush_factory.h"
+#include "kis_png_brush_factory.h"
+#include "kis_svg_brush_factory.h"
 
 KisBrushRegistry::KisBrushRegistry()
 {
@@ -59,6 +61,8 @@
         s_instance->add(new KisGbrBrushFactory());
         s_instance->add(new KisAbrBrushFactory());
         s_instance->add(new KisTextBrushFactory());
+        s_instance->add(new KisPngBrushFactory());
+        s_instance->add(new KisSvgBrushFactory());
         KoPluginLoader::instance()->load("Krita/Brush", "Type == 'Service' and \
([X-Krita-Version] == 3)");  }
     return s_instance;
Index: kis_png_brush_factory.cpp
===================================================================
--- kis_png_brush_factory.cpp	(revision 0)
+++ kis_png_brush_factory.cpp	(revision 0)
@@ -0,0 +1,45 @@
+/*
+ *  Copyright (c) 2010 Cyrille Berger <cberger@cberger.net>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "kis_png_brush_factory.h"
+
+#include <QDomDocument>
+
+#include <KoResourceServer.h>
+#include <KoResourceServerAdapter.h>
+
+#include "kis_brush_server.h"
+
+
+KisPngBrushFactory::KisPngBrushFactory()
+{
+}
+
+KisBrushSP KisPngBrushFactory::getOrCreateBrush(const QDomElement& brushDefinition)
+{
+    KoResourceServer<KisBrush>* rServer = KisBrushServer::instance()->brushServer();
+    QString brushFileName = brushDefinition.attribute("filename", "");
+    KisBrushSP brush = rServer->getResourceByFilename(brushFileName);
+    if(!brush)
+        return 0;
+    
+    double spacing = brushDefinition.attribute("spacing", "0.25").toDouble();
+    brush->setSpacing(spacing);
+
+    return brush;
+}



_______________________________________________
kimageshop mailing list
kimageshop@kde.org
https://mail.kde.org/mailman/listinfo/kimageshop


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic