├── qmldir ├── examples ├── imagefile.qml.png ├── multipage.qml.png ├── singlepage.qml.1.png ├── singlepage.qml.2.png ├── imagecomponents.qml.1.png ├── imagecomponents.qml.2.png ├── 1024px-Methane_venting_offshore_Virginia.jpg ├── imagefile.qml ├── imagecomponents.qml ├── singlepage.qml ├── multipage.qml └── rustacean-flat-happy.svg ├── componentprinter_plugin.cpp ├── componentprinter_plugin.h ├── ComponentPrinter.pro ├── printer.h ├── printer.cpp ├── README.md └── LICENSE /qmldir: -------------------------------------------------------------------------------- 1 | module com.foxmoxie.Printer 2 | plugin Printer 3 | typeinfo plugins.qmltypes 4 | -------------------------------------------------------------------------------- /examples/imagefile.qml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danieloneill/ComponentPrinter/HEAD/examples/imagefile.qml.png -------------------------------------------------------------------------------- /examples/multipage.qml.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danieloneill/ComponentPrinter/HEAD/examples/multipage.qml.png -------------------------------------------------------------------------------- /examples/singlepage.qml.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danieloneill/ComponentPrinter/HEAD/examples/singlepage.qml.1.png -------------------------------------------------------------------------------- /examples/singlepage.qml.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danieloneill/ComponentPrinter/HEAD/examples/singlepage.qml.2.png -------------------------------------------------------------------------------- /examples/imagecomponents.qml.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danieloneill/ComponentPrinter/HEAD/examples/imagecomponents.qml.1.png -------------------------------------------------------------------------------- /examples/imagecomponents.qml.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danieloneill/ComponentPrinter/HEAD/examples/imagecomponents.qml.2.png -------------------------------------------------------------------------------- /examples/1024px-Methane_venting_offshore_Virginia.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danieloneill/ComponentPrinter/HEAD/examples/1024px-Methane_venting_offshore_Virginia.jpg -------------------------------------------------------------------------------- /componentprinter_plugin.cpp: -------------------------------------------------------------------------------- 1 | #include "componentprinter_plugin.h" 2 | #include "printer.h" 3 | 4 | #include 5 | 6 | void ComponentPrinterPlugin::registerTypes(const char *uri) 7 | { 8 | // @uri com.foxmoxie.Printer 9 | qmlRegisterType(uri, 1, 3, "Printer"); 10 | } 11 | 12 | 13 | -------------------------------------------------------------------------------- /componentprinter_plugin.h: -------------------------------------------------------------------------------- 1 | #ifndef COMPONENTPRINTER_PLUGIN_H 2 | #define COMPONENTPRINTER_PLUGIN_H 3 | 4 | #include 5 | 6 | class ComponentPrinterPlugin : public QQmlExtensionPlugin 7 | { 8 | Q_OBJECT 9 | Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") 10 | 11 | public: 12 | void registerTypes(const char *uri); 13 | }; 14 | 15 | #endif // COMPONENTPRINTER_PLUGIN_H 16 | 17 | -------------------------------------------------------------------------------- /ComponentPrinter.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = lib 2 | TARGET = Printer 3 | QT += qml quick printsupport 4 | CONFIG += qt plugin 5 | 6 | qtHaveModule(printsupport) { 7 | QT += printsupport 8 | } 9 | !qtHaveModule(printsupport) { 10 | DEFINES += QT_NO_PRINTER 11 | } 12 | 13 | TARGET = $$qtLibraryTarget($$TARGET) 14 | uri = com.foxmoxie.Printer 15 | 16 | # Input 17 | SOURCES += \ 18 | componentprinter_plugin.cpp \ 19 | printer.cpp 20 | 21 | HEADERS += \ 22 | componentprinter_plugin.h \ 23 | printer.h 24 | 25 | DISTFILES = qmldir \ 26 | LICENSE \ 27 | README.md \ 28 | examples/imagecomponents.qml \ 29 | examples/imagefile.qml \ 30 | examples/multipage.qml \ 31 | examples/singlepage.qml 32 | 33 | !equals(_PRO_FILE_PWD_, $$OUT_PWD) { 34 | copy_qmldir.target = $$OUT_PWD/qmldir 35 | copy_qmldir.depends = $$_PRO_FILE_PWD_/qmldir 36 | copy_qmldir.commands = $(COPY_FILE) \"$$replace(copy_qmldir.depends, /, $$QMAKE_DIR_SEP)\" \"$$replace(copy_qmldir.target, /, $$QMAKE_DIR_SEP)\" 37 | QMAKE_EXTRA_TARGETS += copy_qmldir 38 | PRE_TARGETDEPS += $$copy_qmldir.target 39 | } 40 | 41 | qmldir.files = qmldir 42 | unix|win32 { 43 | installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /) 44 | qmldir.path = $$installPath 45 | target.path = $$installPath 46 | INSTALLS += target qmldir 47 | } 48 | 49 | -------------------------------------------------------------------------------- /examples/imagefile.qml: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // This example uses QPrintDialog which requires a QApplication instance. 3 | // If running from QML runtime utility, specify the apptype: 4 | // $ qml -a widget imagefile.qml 5 | ///////////////////////////////////////////////////////////////////////////// 6 | 7 | import QtQuick 8 | import QtQuick.Controls 9 | import QtQuick.Window 10 | import com.foxmoxie.Printer 11 | 12 | Window { 13 | width: 640 14 | height: 640 15 | visible: true 16 | 17 | id: topWindow 18 | 19 | Rectangle { 20 | id: pageContainer 21 | color: 'white' 22 | width: printer.pageRect.width // When one sets a size using the 'setPageSize' method, this changes. 23 | height: printer.pageRect.height // When one sets a size using the 'setPageSize' method, this changes. 24 | visible: false // Can be true or false, still works! 25 | 26 | 27 | Rectangle { 28 | id: myAmazingComponent 29 | color: 'red' 30 | 31 | width: parent.width * 0.25 32 | height: parent.width * 0.25 33 | anchors.centerIn: parent // Or position it anywhere on your page. 34 | 35 | Rectangle { 36 | id: alsoAmazing 37 | color: 'blue' 38 | 39 | width: parent.width * 0.5 40 | height: parent.width * 0.5 41 | anchors.centerIn: parent 42 | 43 | Text { 44 | anchors.centerIn: parent 45 | font.pixelSize: parent.height * 0.5 46 | color: 'white' 47 | text: "😄" 48 | } 49 | } 50 | } 51 | } 52 | 53 | Printer { 54 | id: printer 55 | antialias: false 56 | monochrome: false 57 | item: pageContainer 58 | 59 | onPrintComplete: console.log("Print complete."); 60 | onPrintError: console.log("Print error!"); 61 | 62 | Component.onCompleted: scanPaperSizes(); 63 | 64 | function scanPaperSizes() 65 | { 66 | console.log( "Sizes: " ); 67 | printer.paperSizes.forEach( function(sz) { 68 | console.log(' - ' + sz); 69 | } ); 70 | 71 | // To use a standard size: 72 | printer.setPageSize( 'Letter / ANSI A' ); 73 | 74 | // Or custom: 75 | // printer.setPageSize( 640, 480, Printer.DevicePixel ); 76 | // Valid "units" are Millimeter, Point, Inch, Pica, Didot, Cicero, and DevicePixel. 77 | // These are resolution dependent (except DevicePixel) so I suggest having your resolution configured in advance. 78 | } 79 | } 80 | 81 | Button { 82 | anchors { 83 | top: parent.top 84 | left: parent.left 85 | margins: 5 86 | } 87 | text: 'Save' 88 | onClicked: { 89 | printer.saveImage("test.png", 'png', 100); 90 | console.log("Saved to 'test.png'."); 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /examples/imagecomponents.qml: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // This example uses QPrintDialog which requires a QApplication instance. 3 | // If running from QML runtime utility, specify the apptype: 4 | // $ qml -a widget imagecomponents.qml 5 | ///////////////////////////////////////////////////////////////////////////// 6 | 7 | 8 | /******************* 9 | * Assets included: 10 | * https://commons.wikimedia.org/wiki/File:Methane_venting_offshore_Virginia.jpg - Background Image 11 | * NOAA, Public domain, via Wikimedia Commons 12 | * 13 | * https://rustacean.net/ - Ferris the Crab 14 | * Karen Rustad Tölva, Public Domain 15 | *******************/ 16 | 17 | import QtQuick 18 | import QtQuick.Controls 19 | import QtQuick.Window 20 | import com.foxmoxie.Printer 21 | 22 | Window { 23 | width: 640 24 | height: 640 25 | visible: true 26 | 27 | id: topWindow 28 | 29 | Rectangle { 30 | id: pageContainer 31 | color: 'white' 32 | width: printer.pageRect.width // When one sets a size using the 'setPageSize' method, this changes. 33 | height: printer.pageRect.height // When one sets a size using the 'setPageSize' method, this changes. 34 | visible: true // Can be true or false, still works! 35 | 36 | 37 | Image { 38 | id: myAmazingComponent 39 | source: '1024px-Methane_venting_offshore_Virginia.jpg' 40 | anchors.centerIn: parent // Or position it anywhere on your page. 41 | 42 | Image { 43 | id: alsoAmazing 44 | 45 | width: parent.width * 0.5 46 | height: parent.width * 0.5 47 | anchors.centerIn: parent 48 | 49 | source: 'rustacean-flat-happy.svg' 50 | } 51 | } 52 | } 53 | 54 | Printer { 55 | id: printer 56 | antialias: false 57 | monochrome: false 58 | item: pageContainer 59 | 60 | onPrintComplete: console.log("Print complete."); 61 | onPrintError: console.log("Print error!"); 62 | 63 | Component.onCompleted: scanPaperSizes(); 64 | 65 | function scanPaperSizes() 66 | { 67 | console.log( "Sizes: " ); 68 | printer.paperSizes.forEach( function(sz) { 69 | console.log(' - ' + sz); 70 | } ); 71 | 72 | // To use a standard size: 73 | printer.setPageSize( 'Letter / ANSI A' ); 74 | 75 | // Or custom: 76 | // printer.setPageSize( 640, 480, Printer.DevicePixel ); 77 | // Valid "units" are Millimeter, Point, Inch, Pica, Didot, Cicero, and DevicePixel. 78 | // These are resolution dependent (except DevicePixel) so I suggest having your resolution configured in advance. 79 | } 80 | } 81 | 82 | Button { 83 | anchors { 84 | top: parent.top 85 | left: parent.left 86 | margins: 5 87 | } 88 | text: 'Save' 89 | onClicked: { 90 | printer.saveImage("test.png", 'png', 100); 91 | console.log("Saved to 'test.png'."); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /examples/singlepage.qml: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // This example uses QPrintDialog which requires a QApplication instance. 3 | // If running from QML runtime utility, specify the apptype: 4 | // $ qml -a widget singlepage.qml 5 | ///////////////////////////////////////////////////////////////////////////// 6 | 7 | import QtQuick 8 | import QtQuick.Controls 9 | import QtQuick.Window 10 | import com.foxmoxie.Printer 11 | 12 | Window { 13 | width: 640 14 | height: 640 15 | visible: true 16 | 17 | id: topWindow 18 | 19 | Rectangle { 20 | id: pageContainer 21 | color: 'white' 22 | width: printer.pageRect.width // When one sets a size using the 'setPageSize' method, this changes. 23 | height: printer.pageRect.height // When one sets a size using the 'setPageSize' method, this changes. 24 | visible: false // Can be true or false, still works! 25 | 26 | 27 | Rectangle { 28 | id: myAmazingComponent 29 | color: 'red' 30 | 31 | width: parent.width * 0.25 32 | height: parent.width * 0.25 33 | anchors.centerIn: parent // Or position it anywhere on your page. 34 | 35 | Rectangle { 36 | id: alsoAmazing 37 | color: 'blue' 38 | 39 | width: parent.width * 0.5 40 | height: parent.width * 0.5 41 | anchors.centerIn: parent 42 | 43 | Text { 44 | anchors.centerIn: parent 45 | font.pixelSize: parent.height * 0.5 46 | color: 'white' 47 | text: "😄" 48 | } 49 | } 50 | } 51 | } 52 | 53 | Printer { 54 | id: printer 55 | antialias: false 56 | monochrome: false 57 | item: pageContainer 58 | 59 | onPrintComplete: console.log("Print complete."); 60 | onPrintError: console.log("Print error!"); 61 | 62 | Component.onCompleted: scanPaperSizes(); 63 | 64 | function scanPaperSizes() 65 | { 66 | console.log( "Sizes: " ); 67 | printer.paperSizes.forEach( function(sz) { 68 | console.log(' - ' + sz); 69 | } ); 70 | 71 | // To use a standard size: 72 | printer.setPageSize( 'Letter / ANSI A' ); 73 | 74 | // Or custom: 75 | // printer.setPageSize( 640, 480, Printer.DevicePixel ); 76 | // Valid "units" are Millimeter, Point, Inch, Pica, Didot, Cicero, and DevicePixel. 77 | // These are resolution dependent (except DevicePixel) so I suggest having your resolution configured in advance. 78 | } 79 | } 80 | 81 | Button { 82 | anchors { 83 | top: parent.top 84 | left: parent.left 85 | margins: 5 86 | } 87 | text: 'Print' 88 | onClicked: { 89 | if( !printer.setup() ) 90 | { 91 | console.log("Cancelled in Print Setup."); 92 | return; 93 | } 94 | 95 | // Open a print job, print the component (item), then close/submit the print job. 96 | if( !printer.open() ) 97 | { 98 | console.log("Failed to open printer!"); 99 | return; 100 | } 101 | 102 | console.log("Okay, now we print!"); 103 | 104 | // This doesn't happen immediately, so the close will happen when the component 105 | // sends either the printComplete or printError signal: 106 | printer.print( function() { 107 | console.log("Job complete, closing printer context."); 108 | printer.close(); 109 | } ); 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /examples/multipage.qml: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////////////////////////////////// 2 | // This example uses QPrintDialog which requires a QApplication instance. 3 | // If running from QML runtime utility, specify the apptype: 4 | // $ qml -a widget multipage.qml 5 | ///////////////////////////////////////////////////////////////////////////// 6 | 7 | import QtQuick 8 | import QtQuick.Controls 9 | import QtQuick.Window 10 | import com.foxmoxie.Printer 11 | 12 | Window { 13 | width: 640 14 | height: 640 15 | visible: true 16 | 17 | id: topWindow 18 | 19 | Component { 20 | id: pageComponent 21 | 22 | Rectangle { 23 | id: pageContainer 24 | color: 'white' 25 | width: printer.pageRect.width // When one sets a size using the 'setPageSize' method, this changes. 26 | height: printer.pageRect.height // When one sets a size using the 'setPageSize' method, this changes. 27 | visible: false // Can be true or false, still works! 28 | 29 | property string text: 'Unset' 30 | 31 | // Any regular QML/Qt Quick can be inside your container. 32 | // It will be printed 'as seen on screen', with layers rendered 33 | // in the same order as on screen. 34 | Rectangle { 35 | id: myAmazingComponent 36 | color: 'red' 37 | 38 | width: parent.width * 0.25 39 | height: parent.width * 0.25 40 | anchors.centerIn: parent // Or position it anywhere on your page. 41 | 42 | Rectangle { 43 | id: alsoAmazing 44 | color: 'blue' 45 | 46 | width: parent.width * 0.5 47 | height: parent.width * 0.5 48 | anchors.centerIn: parent 49 | 50 | Text { 51 | anchors.centerIn: parent 52 | font.pixelSize: parent.height * 0.5 53 | color: 'white' 54 | text: pageContainer.text 55 | } 56 | } 57 | } 58 | } 59 | } 60 | 61 | Printer { 62 | id: printer 63 | antialias: false 64 | monochrome: false 65 | 66 | // Just for our sake to track which page number we're on. 67 | // In this example, it's also displayed in the box printed. 68 | property int page: 1 69 | 70 | onPrintComplete: nextPage(); 71 | onPrintError: console.log("Print error!"); 72 | 73 | Component.onCompleted: scanPaperSizes(); 74 | 75 | function scanPaperSizes() 76 | { 77 | console.log( "Sizes: " ); 78 | printer.paperSizes.forEach( function(sz) { 79 | console.log(' - ' + sz); 80 | } ); 81 | 82 | // To use a standard size: 83 | printer.setPageSize( 'Letter / ANSI A' ); 84 | 85 | // Or custom: 86 | // printer.setPageSize( 640, 480, Printer.DevicePixel ); 87 | // Valid "units" are Millimeter, Point, Inch, Pica, Didot, Cicero, and DevicePixel. 88 | // These are resolution dependent (except DevicePixel) so I suggest having your resolution configured in advance. 89 | } 90 | 91 | // First page, we don't new a 'newPage'. 92 | function start() 93 | { 94 | printer.page = 1; 95 | 96 | // Open a session: 97 | printer.open(); 98 | 99 | // Generate a page and print it: 100 | printer.printPage(); 101 | } 102 | 103 | function generatePage() 104 | { 105 | // Subsequent pages will start on a new page. 106 | printer.newPage(); 107 | 108 | // Generate the next page, and print it: 109 | printer.printPage(); 110 | } 111 | 112 | function printPage() 113 | { 114 | let newPageObject = pageComponent.createObject(printer, { text: printer.page }); 115 | if( !newPageObject ) 116 | { 117 | console.log("Failed to generate page #"+printer.page); 118 | return; 119 | } 120 | 121 | printer.item = newPageObject; 122 | printer.print(); 123 | } 124 | 125 | function nextPage() 126 | { 127 | console.log('Page '+page+' printed.'); 128 | if( printer.page < 4 ) 129 | { 130 | // Print the next one. 131 | printer.page++; 132 | generatePage(); 133 | } 134 | else 135 | // Done! Calling printer.close() completes this print job and submits it to the printing subsystem. 136 | printer.close(); 137 | } 138 | } 139 | 140 | Button { 141 | anchors { 142 | top: parent.top 143 | left: parent.left 144 | margins: 5 145 | } 146 | text: 'Print' 147 | onClicked: { 148 | printer.page = 1; 149 | 150 | if( printer.setup() ) 151 | printer.start(); 152 | } 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /printer.h: -------------------------------------------------------------------------------- 1 | #ifndef PRINTER_H 2 | #define PRINTER_H 3 | 4 | #ifndef QT_NO_PRINTER 5 | # include 6 | # include 7 | #endif 8 | #include 9 | #include 10 | 11 | class Printer : public QQuickItem 12 | { 13 | Q_OBJECT 14 | QML_ELEMENT 15 | Q_DISABLE_COPY(Printer) 16 | 17 | public: 18 | typedef enum { Print, PrintToFile, GrabOnly } GrabMode; 19 | Q_ENUMS(GrabMode); 20 | 21 | private: 22 | QSharedPointer m_result; 23 | QQuickItem *m_item; 24 | #ifndef QT_NO_PRINTER 25 | QPrintDialog *m_printDialogue; 26 | QPrinter *m_printer; 27 | bool m_pagePrinted; 28 | bool m_sessionOpen; 29 | int m_copyCount; 30 | QPainter *m_painter; 31 | 32 | bool m_antialias; 33 | bool m_monochrome; 34 | QString m_filepath; 35 | QRectF m_margins; 36 | #endif 37 | 38 | GrabMode m_mode; 39 | QString m_fileDest; 40 | QString m_fileType; 41 | int m_fileQuality; 42 | QJSValue m_callback; 43 | 44 | Q_PROPERTY(QQuickItem* item READ getItem WRITE setItem NOTIFY itemChanged) 45 | Q_PROPERTY(bool printingSupported READ printingSupported CONSTANT) 46 | #ifndef QT_NO_PRINTER 47 | Q_PROPERTY(QString filepath READ getFilePath WRITE setFilePath NOTIFY filePathChanged) 48 | Q_PROPERTY(bool antialias READ getAntialias WRITE setAntialias NOTIFY antialiasChanged) 49 | Q_PROPERTY(bool monochrome READ getMonochrome WRITE setMonochrome NOTIFY monochromeChanged) 50 | Q_PROPERTY(int resolution READ getResolution WRITE setResolution NOTIFY resolutionChanged) 51 | Q_PROPERTY(int copyCount READ getCopyCount WRITE setCopyCount NOTIFY copyCountChanged) 52 | Q_PROPERTY(QRectF pageRect READ getPageRect NOTIFY sizeChanged) 53 | Q_PROPERTY(QRectF paperRect READ getPaperRect NOTIFY sizeChanged) 54 | Q_PROPERTY(QStringList paperSizes READ getPaperSizes) 55 | Q_PROPERTY(QString printerName READ getPrinterName WRITE setPrinterName NOTIFY printerNameChanged) 56 | Q_PROPERTY(Status status READ getStatus) 57 | #endif 58 | 59 | public: 60 | Printer(QQuickItem *parent = 0); 61 | ~Printer(); 62 | 63 | #ifndef QT_NO_PRINTER 64 | typedef enum { 65 | Millimeter = QPageSize::Millimeter, 66 | Point = QPageSize::Point, 67 | Inch = QPageSize::Inch, 68 | Pica = QPageSize::Pica, 69 | Didot = QPageSize::Didot, 70 | Cicero = QPageSize::Cicero, 71 | DevicePixel 72 | } Unit; 73 | Q_ENUMS(Unit) 74 | 75 | typedef enum { 76 | Idle = QPrinter::Idle, 77 | Active = QPrinter::Active, 78 | Aborted = QPrinter::Aborted, 79 | Error = QPrinter::Error, 80 | Unknown 81 | } Status; 82 | Q_ENUMS(Status) 83 | #endif 84 | 85 | public slots: 86 | bool printingSupported() const; 87 | 88 | #ifndef QT_NO_PRINTER 89 | bool print(QJSValue callback=QJSValue()); 90 | bool setup(); 91 | bool open(); 92 | bool close(); 93 | bool newPage() const; 94 | bool abort(); 95 | #endif 96 | 97 | bool grabImage(const QString &fileFormat, int quality=100, QJSValue callback=QJSValue()); 98 | bool saveImage(const QString &fileName, const QString &fileFormat, int quality, QJSValue callback=QJSValue()); 99 | #ifndef QT_NO_PRINTER 100 | bool printImage(const QImage &img); 101 | bool printImageData(const QByteArray &img); 102 | #endif 103 | 104 | // Property Hooks: 105 | void setItem( QQuickItem *item ); 106 | #ifndef QT_NO_PRINTER 107 | void setFilePath(const QString &filepath); 108 | void setMonochrome(bool toggle); 109 | void setAntialias(bool toggle); 110 | void setMargins(double top, double right, double bottom, double left); 111 | bool setPageSize( qreal width, qreal height, Unit unit ); 112 | bool setPageSize( const QString &paperSize ); 113 | void setPrinterName(const QString &printerName); 114 | void setResolution(int dpi); 115 | void setCopyCount(int count); 116 | #endif 117 | 118 | QQuickItem *getItem() const { return m_item; } 119 | #ifndef QT_NO_PRINTER 120 | QString getFilePath() const { return m_filepath; } 121 | bool getMonochrome() const { return m_monochrome; } 122 | bool getAntialias() const { return m_antialias; } 123 | QRectF getMargins() const { return m_margins; } 124 | QRectF getPageRect(Unit unit=DevicePixel) const; 125 | QRectF getPaperRect(Unit unit=DevicePixel) const; 126 | QStringList getPaperSizes() const; 127 | QString getPrinterName() const; 128 | int getResolution() const { return m_printer->resolution(); } 129 | int getCopyCount() const { return m_printer->copyCount(); } 130 | Status getStatus() const; 131 | #endif 132 | 133 | private slots: 134 | bool grab(); 135 | void grabbed(); 136 | 137 | signals: 138 | void itemChanged(); 139 | void frameGrabbed(const QByteArray &imageData); 140 | void sizeChanged(); 141 | void printComplete(); 142 | void printError(); 143 | #ifndef QT_NO_PRINTER 144 | void filePathChanged(); 145 | void monochromeChanged(); 146 | void antialiasChanged(); 147 | void marginsChanged(); 148 | void printerNameChanged(); 149 | void resolutionChanged(); 150 | void copyCountChanged(); 151 | #endif 152 | }; 153 | 154 | #endif // PRINTER_H 155 | 156 | -------------------------------------------------------------------------------- /examples/rustacean-flat-happy.svg: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /printer.cpp: -------------------------------------------------------------------------------- 1 | #include "printer.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #ifndef QT_NO_PRINTER 7 | # include 8 | #endif 9 | #include 10 | 11 | // Just for converting QByteArray: 12 | #include 13 | 14 | Printer::Printer(QQuickItem *parent): 15 | QQuickItem(parent) 16 | { 17 | #ifndef QT_NO_PRINTER 18 | m_printDialogue = nullptr; 19 | m_printer = new QPrinter(QPrinter::ScreenResolution); 20 | m_pagePrinted = false; 21 | m_sessionOpen = false; 22 | m_copyCount = 1; 23 | m_painter = nullptr; 24 | m_antialias = true; 25 | m_monochrome = false; 26 | m_margins = QRectF(0, 0, 0, 0); 27 | m_filepath.clear(); 28 | #endif 29 | 30 | m_mode = Printer::GrabOnly; 31 | m_item = NULL; 32 | 33 | m_fileDest.clear(); 34 | m_fileType.clear(); 35 | m_fileQuality = 0; 36 | } 37 | 38 | Printer::~Printer() 39 | { 40 | #ifndef QT_NO_PRINTER 41 | delete m_printer; 42 | #endif 43 | } 44 | 45 | bool Printer::printingSupported() const 46 | { 47 | #ifdef QT_NO_PRINTER 48 | return false; 49 | #else 50 | return true; 51 | #endif 52 | } 53 | 54 | #ifndef QT_NO_PRINTER 55 | bool Printer::print(QJSValue callback) 56 | { 57 | m_mode = Printer::Print; 58 | m_callback = callback; 59 | return grab(); 60 | } 61 | #endif 62 | 63 | bool Printer::grabImage(const QString &fileFormat, int quality, QJSValue callback) 64 | { 65 | m_mode = Printer::GrabOnly; 66 | m_callback = callback; 67 | m_fileType = fileFormat; 68 | m_fileQuality = quality; 69 | return grab(); 70 | } 71 | 72 | bool Printer::saveImage(const QString &fileName, const QString &fileFormat, int quality, QJSValue callback) 73 | { 74 | m_mode = Printer::PrintToFile; 75 | m_callback = callback; 76 | m_fileDest = fileName; 77 | m_fileType = fileFormat; 78 | m_fileQuality = quality; 79 | return grab(); 80 | } 81 | 82 | #ifndef QT_NO_PRINTER 83 | bool Printer::printImage(const QImage &img) 84 | { 85 | QMarginsF margins( m_margins.left(), m_margins.top(), m_margins.right(), m_margins.bottom() ); 86 | if( !m_printer->setPageMargins( margins, QPageLayout::Millimeter ) ) 87 | { 88 | qWarning() << tr("Printer: Failed to set page margin (in mm) as configured."); 89 | return false; 90 | } 91 | 92 | if( !m_sessionOpen ) 93 | { 94 | qWarning() << tr("Printer: Attempt to print without first calling Printer::open(). (This behaviour changed in 1.2)");; 95 | return false; 96 | } 97 | 98 | if( m_monochrome ) 99 | // Convert to monochrome, no dithering: 100 | m_painter->drawImage( m_printer->paperRect(QPrinter::DevicePixel), img.convertToFormat(QImage::Format_Mono, Qt::MonoOnly | Qt::ThresholdDither) ); 101 | else 102 | m_painter->drawImage( m_printer->paperRect(QPrinter::DevicePixel), img ); 103 | 104 | return true; 105 | } 106 | 107 | bool Printer::printImageData(const QByteArray &data) 108 | { 109 | return printImage( QImage::fromData(data) ); 110 | } 111 | 112 | bool Printer::setup() 113 | { 114 | m_printer->setOutputFormat(QPrinter::NativeFormat); 115 | 116 | m_printDialogue = new QPrintDialog(m_printer); 117 | if( m_printDialogue->exec() == QDialog::Accepted ) 118 | { 119 | m_printDialogue->deleteLater(); 120 | return true; 121 | } 122 | 123 | delete m_printDialogue; 124 | 125 | return false; 126 | } 127 | 128 | bool Printer::open() 129 | { 130 | if( m_sessionOpen ) 131 | { 132 | qWarning() << tr("Printer::open called while already in a multipage session. (Call 'close' first.)"); 133 | return false; 134 | } 135 | 136 | m_painter = new QPainter(); 137 | if( !m_painter ) 138 | { 139 | qWarning() << tr("Printer::open failed to instantiate new QPainter. (Are you out of memory?)"); 140 | return false; 141 | } 142 | 143 | if( !m_painter->begin(m_printer) ) 144 | { 145 | qWarning() << tr("Failed to initialise QPainter to QPrintDevice."); 146 | return false; 147 | } 148 | 149 | m_painter->setRenderHint(QPainter::Antialiasing, m_antialias); 150 | m_painter->setRenderHint(QPainter::TextAntialiasing, m_antialias); 151 | m_painter->setRenderHint(QPainter::SmoothPixmapTransform, m_antialias); 152 | 153 | m_sessionOpen = true; 154 | return true; 155 | } 156 | 157 | bool Printer::close() 158 | { 159 | if( !m_sessionOpen ) 160 | { 161 | qWarning() << tr("Printer::close called while not in multipage session."); 162 | return false; 163 | } 164 | 165 | delete m_painter; 166 | m_painter = nullptr; 167 | m_sessionOpen = false; 168 | 169 | return true; 170 | } 171 | 172 | bool Printer::newPage() const 173 | { 174 | if( !m_sessionOpen ) 175 | { 176 | qWarning() << tr("Printer::newPage called while not in a multipage session. (Call Printer::open first.)"); 177 | return false; 178 | } 179 | 180 | return m_printer->newPage(); 181 | } 182 | 183 | bool Printer::abort() 184 | { 185 | if( m_sessionOpen ) 186 | close(); 187 | 188 | return m_printer->abort(); 189 | } 190 | 191 | void Printer::setMonochrome(bool toggle) 192 | { 193 | if( m_monochrome == toggle ) 194 | return; 195 | 196 | m_monochrome = toggle; 197 | emit monochromeChanged(); 198 | } 199 | 200 | void Printer::setAntialias(bool toggle) 201 | { 202 | if( m_antialias == toggle ) 203 | return; 204 | 205 | m_antialias = toggle; 206 | emit antialiasChanged(); 207 | } 208 | 209 | void Printer::setFilePath(const QString &filepath) 210 | { 211 | if( m_filepath == filepath ) 212 | return; 213 | 214 | m_filepath = filepath; 215 | emit filePathChanged(); 216 | } 217 | #endif 218 | 219 | void Printer::setItem(QQuickItem *item) 220 | { 221 | if( m_item == item ) 222 | return; 223 | 224 | m_item = item; 225 | emit itemChanged(); 226 | } 227 | 228 | #ifndef QT_NO_PRINTER 229 | void Printer::setMargins(double top, double right, double bottom, double left) 230 | { 231 | QRectF m( left, top, right-left, bottom-top ); 232 | if( m_margins == m ) 233 | return; 234 | 235 | m_margins = m; 236 | emit marginsChanged(); 237 | } 238 | 239 | bool Printer::setPageSize( const QString &paperSize ) 240 | { 241 | QPageSize size; 242 | // Run through each.. 243 | for( int x=0; x < QPageSize::LastPageSize; x++ ) 244 | { 245 | size = QPageSize((QPageSize::PageSizeId)x); 246 | if( size.name() == paperSize ) 247 | { 248 | bool result = m_printer->setPageSize( size ); 249 | emit sizeChanged(); 250 | return result; 251 | } 252 | } 253 | 254 | qWarning() << tr("Unknown paper size: ") << paperSize << tr(" (Refer to 'paperSizes()' for valid options.)"); 255 | return false; 256 | } 257 | 258 | bool Printer::setPageSize( qreal width, qreal height, Unit unit ) 259 | { 260 | QSizeF szf(width, height); 261 | QPageSize size; 262 | 263 | switch( unit ) 264 | { 265 | case DevicePixel: 266 | // Fanagle from DPI: 267 | szf /= m_printer->resolution(); 268 | size = QPageSize(szf, QPageSize::Inch); 269 | break; 270 | default: 271 | size = QPageSize(szf, (QPageSize::Unit)unit); 272 | break; 273 | } 274 | 275 | bool result = m_printer->setPageSize(size); 276 | emit sizeChanged(); 277 | return result; 278 | } 279 | 280 | void Printer::setPrinterName(const QString &printerName) 281 | { 282 | if( m_printer->printerName() == printerName ) 283 | return; 284 | 285 | m_printer->setPrinterName( printerName ); 286 | emit printerNameChanged(); 287 | } 288 | 289 | void Printer::setResolution(int dpi) 290 | { 291 | if( m_printer->resolution() == dpi ) 292 | return; 293 | 294 | m_printer->setResolution( dpi ); 295 | emit resolutionChanged(); 296 | } 297 | 298 | void Printer::setCopyCount(int count) 299 | { 300 | if( m_printer->copyCount() == count ) 301 | return; 302 | 303 | m_printer->setCopyCount( count ); 304 | emit copyCountChanged(); 305 | } 306 | 307 | QRectF Printer::getPageRect(Unit unit) const 308 | { 309 | return m_printer->pageRect( (QPrinter::Unit)unit ); 310 | } 311 | 312 | QRectF Printer::getPaperRect(Unit unit) const 313 | { 314 | return m_printer->paperRect( (QPrinter::Unit)unit ); 315 | } 316 | 317 | QStringList Printer::getPaperSizes() const 318 | { 319 | QStringList results; 320 | QPageSize size; 321 | // Run through each.. 322 | for( int x=0; x < QPageSize::LastPageSize; x++ ) 323 | { 324 | size = QPageSize((QPageSize::PageSizeId)x); 325 | results.append( size.name() ); 326 | } 327 | return results; 328 | } 329 | 330 | Printer::Status Printer::getStatus() const 331 | { 332 | QPrinter::PrinterState state = m_printer->printEngine()->printerState(); 333 | return (Printer::Status)state; 334 | } 335 | #endif 336 | 337 | bool Printer::grab() 338 | { 339 | if( !m_item ) 340 | { 341 | qWarning() << tr("Printer::grab: No item source specified. (Set it with the 'item' property.)"); 342 | return false; 343 | } 344 | 345 | QSharedPointer res = m_item->grabToImage(); 346 | if( !res ) 347 | { 348 | qWarning() << tr("Printer::grab: Grab failed for some reason. (Is the item loaded and rendered?)"); 349 | return false; 350 | } 351 | 352 | connect( res.data(), SIGNAL(ready()), this, SLOT(grabbed()) ); 353 | m_result = res; 354 | 355 | return true; 356 | } 357 | 358 | void Printer::grabbed() 359 | { 360 | const QImage img = m_result.data()->image(); 361 | m_result.clear(); 362 | 363 | QQmlEngine *jse = qmlEngine(this); 364 | jse->collectGarbage(); 365 | 366 | bool ret = true; 367 | 368 | if( m_mode == Printer::PrintToFile ) 369 | { 370 | ret = img.save(m_fileDest, m_fileType.toStdString().c_str(), m_fileQuality); 371 | if( m_callback.isCallable() ) 372 | { 373 | QJSValueList args; 374 | args << ret; 375 | m_callback.call(args); 376 | } 377 | } 378 | #ifndef QT_NO_PRINTER 379 | else if( m_mode == Printer::Print ) 380 | { 381 | if( !m_filepath.isEmpty() ) 382 | m_printer->setOutputFileName(m_filepath); 383 | 384 | ret = printImage(img); 385 | if( m_callback.isCallable() ) 386 | { 387 | QJSValueList args; 388 | args << ret; 389 | m_callback.call(args); 390 | } 391 | } 392 | #endif 393 | else if( m_callback.isCallable() ) 394 | { 395 | QImage image; 396 | QByteArray ba; 397 | QBuffer buffer(&ba); 398 | buffer.open(QIODevice::WriteOnly); 399 | ret = img.save(&buffer, m_fileType.toStdString().c_str(), m_fileQuality); 400 | buffer.close(); 401 | 402 | if( ret ) 403 | { 404 | QJSValueList args; 405 | args << jse->toScriptValue(ba); 406 | m_callback.call( args ); 407 | } 408 | } 409 | 410 | m_callback = QJSValue(); 411 | 412 | if( ret ) 413 | emit printComplete(); 414 | else 415 | emit printError(); 416 | } 417 | 418 | #ifndef QT_NO_PRINTER 419 | QString Printer::getPrinterName() const 420 | { 421 | return m_printer->printerName(); 422 | } 423 | #endif 424 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ComponentPrinter 2 | QML Component for printing (or saving images of) a component (and children therein). 3 | 4 | Please note that the API has changed significantly since the 1.0 version. 5 | 6 | --- 7 | 8 | ### Declaration: 9 | 10 | 11 | ``` 12 | import com.foxmoxie.Printer 1.3 // For Qt 5.x 13 | //import com.foxmoxie.Printer // for Qt 6.x 14 | 15 | Printer { 16 | id: myPrinter 17 | } 18 | ``` 19 | 20 | 21 | ### Properties: 22 | 23 | 24 | * bool antialias 25 | * enable or disable antialiasing 26 | 27 | * bool monochrome 28 | * enable or disable monochrome printing (eg, thermal printers) 29 | 30 | * int copyCount 31 | * specify how many copies a call to 'print()' will yield 32 | 33 | * int resolution 34 | * the dpi to print at 35 | 36 | * string filepath 37 | * the filepath (on some platforms) to print to when "print to file" is selected 38 | 39 | * object item 40 | * the qml component you want to print 41 | 42 | * QRectF pageRect (read-only, call setPageSize to change) 43 | * a QRectF object representing the page dimensions in device pixels. for other units, see getPaperRect() method. this is usually smaller than the paperRect() since the page normally has margins between its borders and the paper. 'x', 'y', 'width', and 'height' are the pertinent properties of the returned object. 44 | 45 | * QRectF paperRect (read-only, call setPageSize to change) 46 | * a QRectF object representing the paper dimensions in device pixels. for other units, see getPaperRect() method. 'x', 'y', 'width', and 'height' are the pertinent properties of the returned object. 47 | 48 | * object paperSizes (read-only) 49 | * an array of strings representing the known standard paper sizes configurable via the 'setPageSize(string)' method. 50 | 51 | * string printerName 52 | * the name of the destination printer 53 | 54 | * Status status 55 | * the printer's current status (if supported). unfortunately this property has no 'changed' signal. can be one of: 56 | * Printer::Idle 57 | * Printer::Active 58 | * Printer::Aborted 59 | * Printer::Error 60 | * Printer::Unknown 61 | 62 | 63 | ### Methods: 64 | 65 | * bool grabImage(string fileFormat, [int quality], [function(ByteArray data) callback]) 66 | * provides an image of the item via provided callback function as a byte array. 67 | 68 | * bool saveImage(string fileName, string fileFormat, int quality, [function(bool success) callback]) 69 | * save an image of the item to an image file, optionally with a callback upon completion. 70 | 71 | * bool open() 72 | * open a printing session (start a new print job). *This MUST be called before print* 73 | 74 | * bool print([function(bool success) callback]) 75 | * print the item using predeclared parameters, optionally with a callback upon completion. (Note that this method will usually return before printing is complete, so calling *close* immediately will likely break your entire world. Instead wait for printComplete, printError, or close in your provided callback function.) 76 | 77 | * bool printImageData( blob data ) 78 | * print a jpeg/png/etc. image from a ArrayBuffer. if format isn't supported, thing go kerplow (maybe, undefined behaviour) 79 | 80 | * bool newPage() 81 | * begins a new page. this does not need to be called for the first page. doing so will result in your first page being blank. 82 | 83 | * bool close() 84 | * close (and submit) the open printing session. *This "mUsT" be called after printing is complete* 85 | 86 | * bool abort() 87 | * attempt to abort the current print job, and close the session. 88 | 89 | * bool setup() 90 | * display a print dialogue. configures the printer context and returns true if the user clicks 'print', false otherwise. 91 | 92 | * setMargins( double top, double right, double bottom, double left ) 93 | * sets the page margins (auto-set by 'setup' method) 94 | 95 | * QRectF getMargins() 96 | * returns a QRectF representing the currently set margins. 'x', 'y', 'width', and 'height' are the pertinent properties of the returned object. 97 | 98 | * bool setPageSize( int width, int height, Unit unit ) 99 | * configure the page size to a custom size. valid units are: 100 | * Printer::Millimeter 101 | * Printer::Point 102 | * Printer::Inch 103 | * Printer::Pica 104 | * Printer::Didot 105 | * Printer::Cicero 106 | * Printer::DevicePixel 107 | 108 | * bool setPageSize( string paperSize ) 109 | * configure the page size to a standard size (one found in the 'paperSizes' property or via getPaperSizes) 110 | 111 | * QRectF getPageRect( Unit units ) (default unit used is DevicePixel) 112 | * fetch the page size in the specified units of measurement. 'x', 'y', 'width', and 'height' are the pertinent properties of the returned object. valid units are: 113 | * Printer::Millimeter 114 | * Printer::Point 115 | * Printer::Inch 116 | * Printer::Pica 117 | * Printer::Didot 118 | * Printer::Cicero 119 | * Printer::DevicePixel 120 | 121 | * QRectF getPaperRect( Unit units ) (Default unit used is DevicePixel) 122 | * fetch the paper size in the specified units of measurement. 'x', 'y', 'width', and 'height' are the pertinent properties of the returned object. valid units are: 123 | * Printer::Millimeter 124 | * Printer::Point 125 | * Printer::Inch 126 | * Printer::Pica 127 | * Printer::Didot 128 | * Printer::Cicero 129 | * Printer::DevicePixel 130 | 131 | * Status getStatus() 132 | * the printer's current status (if supported). can be one of: 133 | * Printer::Idle 134 | * Printer::Active 135 | * Printer::Aborted 136 | * Printer::Error 137 | * Printer::Unknown 138 | 139 | #### Property Hooks: (See the respective property documentation for details.) 140 | 141 | 142 | * void setAntialias( bool toggle ) 143 | * void setFilePath( string filepath ) 144 | * void setItem( object item ) 145 | * void setPrinterName( string printerName ) 146 | * void setResolution( int dpi ) 147 | * void setMonochrome( bool monochrome ) 148 | * void setCopyCount( int count ) 149 | 150 | * bool getAntialias() 151 | * string getFilePath() 152 | * object getItem() 153 | * object getPaperSizes() 154 | * string getPrinterName() 155 | * int getResolution() 156 | * bool getMonochrome() 157 | * int getCopyCount() 158 | 159 | ### Signals: 160 | 161 | 162 | * printComplete 163 | * when the component successfully prints this signal will be emitted. 164 | 165 | * printError 166 | * when the component unsuccessfully prints this signal will be emitted. 167 | 168 | * antialiasChanged 169 | * antialiasing setting changed 170 | 171 | * filePathChanged 172 | * the target print to file path has changed 173 | 174 | * itemChanged 175 | * the target component has changed 176 | 177 | * marginsChanged 178 | * somehow the margins were adjusted, by hand or by print dialogue 179 | 180 | * printerNameChanged 181 | * a new printer (name) was selected 182 | 183 | * resolutionChanged 184 | * the printer resolution has changed 185 | 186 | * sizeChanged 187 | * target printing size has changed 188 | 189 | * monochromeChanged 190 | * flag for printing to monochrome has changed 191 | 192 | * copyCountChanged 193 | * copy count has changed 194 | 195 | ### Example: 196 | 197 | **NOTE** 198 | *In order to execute the example below (or most of the included example files) you must launch with a QApplication initialised due to the usage of QPrintDialog which requires this!* 199 | 200 | ``` 201 | $ qml -a widget ./singlepage.qml 202 | ``` 203 | 204 | 205 | ``` 206 | ///////////////////////////////////////////////////////////////////////////// 207 | // This example uses QPrintDialog which requires a QApplication instance. 208 | // If running from QML runtime utility, specify the apptype: 209 | // $ qml -a widget singlepage.qml 210 | ///////////////////////////////////////////////////////////////////////////// 211 | 212 | import QtQuick 213 | import QtQuick.Controls 214 | import QtQuick.Window 215 | import com.foxmoxie.Printer 216 | 217 | Window { 218 | width: 640 219 | height: 640 220 | visible: true 221 | 222 | id: topWindow 223 | 224 | Rectangle { 225 | id: pageContainer 226 | color: 'white' 227 | width: printer.pageRect.width // When one sets a size using the 'setPageSize' method, this changes. 228 | height: printer.pageRect.height // When one sets a size using the 'setPageSize' method, this changes. 229 | visible: false // Can be true or false, still works! 230 | 231 | 232 | Rectangle { 233 | id: myAmazingComponent 234 | color: 'red' 235 | 236 | width: parent.width * 0.25 237 | height: parent.width * 0.25 238 | anchors.centerIn: parent // Or position it anywhere on your page. 239 | 240 | Rectangle { 241 | id: alsoAmazing 242 | color: 'blue' 243 | 244 | width: parent.width * 0.5 245 | height: parent.width * 0.5 246 | anchors.centerIn: parent 247 | 248 | Text { 249 | anchors.centerIn: parent 250 | font.pixelSize: parent.height * 0.5 251 | color: 'white' 252 | text: "😄" 253 | } 254 | } 255 | } 256 | } 257 | 258 | Printer { 259 | id: printer 260 | antialias: false 261 | monochrome: false 262 | item: pageContainer 263 | 264 | onPrintComplete: console.log("Print complete."); 265 | onPrintError: console.log("Print error!"); 266 | 267 | Component.onCompleted: scanPaperSizes(); 268 | 269 | function scanPaperSizes() 270 | { 271 | console.log( "Sizes: " ); 272 | printer.paperSizes.forEach( function(sz) { 273 | console.log(' - ' + sz); 274 | } ); 275 | 276 | // To use a standard size: 277 | printer.setPageSize( 'Letter / ANSI A' ); 278 | 279 | // Or custom: 280 | // printer.setPageSize( 640, 480, Printer.DevicePixel ); 281 | // Valid "units" are Millimeter, Point, Inch, Pica, Didot, Cicero, and DevicePixel. 282 | // These are resolution dependent (except DevicePixel) so I suggest having your resolution configured in advance. 283 | } 284 | } 285 | 286 | Button { 287 | anchors { 288 | top: parent.top 289 | left: parent.left 290 | margins: 5 291 | } 292 | text: 'Print' 293 | onClicked: { 294 | if( !printer.setup() ) 295 | { 296 | console.log("Cancelled in Print Setup."); 297 | return; 298 | } 299 | 300 | // Open a print job, print the component (item), then close/submit the print job. 301 | if( !printer.open() ) 302 | { 303 | console.log("Failed to open printer!"); 304 | return; 305 | } 306 | 307 | console.log("Okay, now we print!"); 308 | 309 | // This doesn't happen immediately, so the close will happen when the component 310 | // sends either the printComplete or printError signal: 311 | printer.print( function() { 312 | console.log("Job complete, closing printer context."); 313 | printer.close(); 314 | } ); 315 | } 316 | } 317 | } 318 | 319 | ``` 320 | 321 | The horrendously ugly example app should appear as follows: 322 | 323 | ![Example App](https://github.com/danieloneill/ComponentPrinter/blob/master/examples/singlepage.qml.1.png?raw=true "Yech") 324 | 325 | 326 | The resulting file (print.pdf by default) or page printed should resemble: 327 | 328 | ![Result](https://github.com/danieloneill/ComponentPrinter/blob/master/examples/singlepage.qml.2.png?raw=true "Result") 329 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | --------------------------------------------------------------------------------