[glabels/vala] Initial vala implementation of QREncode backend.



commit 3a1c1dd4fc7d6058651d179d4c824cc21de69dc0
Author: Jim Evins <evins snaught com>
Date:   Fri Sep 28 22:22:28 2012 -0400

    Initial vala implementation of QREncode backend.

 configure.ac                              |   23 +++++
 glabels/Makefile.am                       |    5 +
 glabels/barcode_backends.vala             |    2 +
 glabels/barcode_backends/libqrencode.vapi |   52 +++++++++++
 glabels/barcode_backends/qrencode.vala    |  133 +++++++++++++++++++++++++++++
 5 files changed, 215 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index a8b96d4..405facc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -164,6 +164,28 @@ fi
 
 
 dnl ---------------------------------------------------------------------------
+dnl - Check for optional QRencode Barcode backend
+dnl ---------------------------------------------------------------------------
+AC_ARG_WITH(libqrencode,
+	    [AS_HELP_STRING([--without-libqrencode],[build without QR code support])])
+have_libqrencode=no
+if test "x$with_libqrencode" != xno; then
+	PKG_CHECK_MODULES(LIBQRENCODE, libqrencode >= $LIBQRENCODE_REQUIRED,
+			  [have_libqrencode=yes], [have_libqrencode=no])
+fi
+
+if test "x$have_libqrencode" = "xyes"; then
+	AC_DEFINE(HAVE_LIBQRENCODE,1,[Define to 1 for QR code support])
+	LIBQRENCODE_VALAFLAGS="-D HAVE_LIBQRENCODE"
+	AC_SUBST(LIBQRENCODE_VALAFLAGS)
+	AC_SUBST(LIBQRENCODE_CFLAGS)
+	AC_SUBST(LIBQRENCODE_LIBS)
+else
+	help_libqrencode="(See http://megaui.net/fukuchi/works/qrencode/index.en.html)"
+fi
+
+
+dnl ---------------------------------------------------------------------------
 dnl - Check for IEC16022 Barcode backend
 dnl ---------------------------------------------------------------------------
 AC_ARG_WITH(libiec16022,
@@ -227,6 +249,7 @@ Configuration:
 Optional barcode backends:
 
         GNU Barcode ............. ${have_libbarcode} ${help_libbarcode}
+        QR Code ................. ${have_libqrencode} ${help_libqrencode}
         IEC 16022 ............... ${have_libiec16022} ${help_libiec16022}
 
 "
diff --git a/glabels/Makefile.am b/glabels/Makefile.am
index 8ddc3ce..e75f75a 100644
--- a/glabels/Makefile.am
+++ b/glabels/Makefile.am
@@ -14,6 +14,8 @@ glabels_4_SOURCES = \
 	barcode_backends/iec16022.vala \
 	barcode_backends/libbarcode.vapi \
 	barcode_backends/libiec16022.vapi \
+	barcode_backends/libqrencode.vapi \
+	barcode_backends/qrencode.vala \
 	barcode_menu.vala \
 	barcode_menu_button.vala \
 	barcode_menu_item.vala \
@@ -97,6 +99,7 @@ INCLUDES = \
 	-DGLABELS_BRANCH=\""$(GLABELS_BRANCH)"\" \
 	$(LIBBARCODE_CFLAGS) \
 	$(LIBIEC16022_CFLAGS) \
+	$(LIBQRENCODE_CFLAGS) \
 	$(NULL)
 
 VALAFLAGS = \
@@ -113,6 +116,7 @@ VALAFLAGS = \
 	--pkg libglbarcode-4 \
 	$(LIBBARCODE_VALAFLAGS) \
 	$(LIBIEC16022_VALAFLAGS) \
+	$(LIBQRENCODE_VALAFLAGS) \
 	$(NULL)
 
 
@@ -122,6 +126,7 @@ glabels_4_LDADD = \
 	$(GLABELS_LIBS) \
 	$(LIBBARCODE_LIBS) \
 	$(LIBIEC16022_LIBS) \
+	$(LIBQRENCODE_LIBS) \
 	$(NULL)
 
 
diff --git a/glabels/barcode_backends.vala b/glabels/barcode_backends.vala
index 7c3ed98..7435e66 100644
--- a/glabels/barcode_backends.vala
+++ b/glabels/barcode_backends.vala
@@ -407,6 +407,8 @@ namespace glabels
 
 			register_backend( "qrencode", _("QREncode") );
 
+			glbarcode.Factory.register_type( "qrencode:IEC18004", typeof(BackendQrencode.QRcode) );
+
 			register_style( "qrencode:IEC18004", "qrencode", _("IEC18004 (QRCode)"),
 			                false, false, true, false, "12345678", true, 8 );
 
diff --git a/glabels/barcode_backends/libqrencode.vapi b/glabels/barcode_backends/libqrencode.vapi
new file mode 100644
index 0000000..cb97f19
--- /dev/null
+++ b/glabels/barcode_backends/libqrencode.vapi
@@ -0,0 +1,52 @@
+
+/*
+ * Minimal VAPI file for LIBQRENCODE Barcode library.
+ */
+
+#if HAVE_LIBQRENCODE
+
+namespace qrencode
+{
+
+	[CCode (cheader_filename = "qrencode.h", cname = "QRcode", unref_function = "QRcode_free")]
+	public class QRcode
+	{
+		[CCode (cname = "QRcode_encodeString")]
+		public QRcode.encodeString( string digits, int version, EcLevel level, Mode hint, int casesensitive );
+
+		public int    version;
+		public int    width;
+		public string data;
+	}
+
+	[CCode (cheader_filename = "qrencode.h", cname="QRencLevel")]
+	public enum EcLevel {
+		[CCode (cname="QR_ECLEVEL_L")]
+		L,
+		[CCode (cname="QR_ECLEVEL_M")]
+		M,
+		[CCode (cname="QR_ECLEVEL_Q")]
+		Q,
+		[CCode (cname="QR_ECLEVEL_H")]
+		H
+	}
+
+	[CCode (cheader_filename = "qrencode.h", cname="QRencodeMode")]
+	public enum Mode {
+		[CCode (cname="QR_MODE_NUL")]
+		NUL,
+		[CCode (cname="QR_MODE_NUM")]
+		NUM,
+		[CCode (cname="QR_MODE_AN")]
+		AN,
+		[CCode (cname="QR_MODE_8")]
+		B8,
+		[CCode (cname="QR_MODE_KANJI")]
+		KANJI,
+		[CCode (cname="QR_MODE_STRUCTURE")]
+		STRUCTURE
+	}
+
+}
+
+#endif
diff --git a/glabels/barcode_backends/qrencode.vala b/glabels/barcode_backends/qrencode.vala
new file mode 100644
index 0000000..a512f3e
--- /dev/null
+++ b/glabels/barcode_backends/qrencode.vala
@@ -0,0 +1,133 @@
+/*  qrencode.vala
+ *
+ *  Copyright (C) 2012  Jim Evins <evins snaught com>
+ *
+ *  This file is part of libglbarcode.
+ *
+ *  libglabels is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Lesser General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  libglabels 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 Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public License
+ *  along with libglabels.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#if HAVE_LIBQRENCODE
+
+using GLib;
+using glbarcode.Constants;
+
+namespace glabels
+{
+
+	namespace BackendQrencode
+	{
+
+		/**
+		 * QRcode Barcode
+		 */
+		public class QRcode : glbarcode.Barcode
+		{
+
+			private const double MIN_PIXEL_SIZE = 1.0;
+
+
+			/**
+			 * QRcode Barcode data validation method
+			 */
+			protected override bool validate( string data )
+			{
+				if ( data.length == 0 )
+				{
+					return false;
+				}
+
+				return true;
+			}
+
+
+			/**
+			 * QRcode Barcode encoding method
+			 */
+			protected override string encode( string data )
+			{
+				return ""; /* Actual encoding done in vectorize. */
+			}
+
+
+			/**
+			 * QRcode Barcode vectorization method
+			 */
+			protected override void vectorize( string coded_data, string data, string text )
+			{
+				/*
+				 * First encode using QRENCODE Barcode library.
+				 */
+
+				qrencode.QRcode qrcode = new qrencode.QRcode.encodeString( data, 0,
+				                                                           qrencode.EcLevel.M,
+				                                                           qrencode.Mode.B8, 1 );
+
+				/*
+				 * Now do the actual vectorization.
+				 */
+
+				/* Treat requested size as a bounding box, scale to maintain aspect
+				 * ratio while fitting it in this bounding box. */
+				if ( h > w )
+				{
+					h = w;
+				}
+				else
+				{
+					w = h;
+				}
+
+				/* Now determine pixel size. */
+				double pixel_size = w / qrcode.width;
+				if ( pixel_size < MIN_PIXEL_SIZE )
+				{
+					pixel_size = MIN_PIXEL_SIZE;
+				}
+
+				/* Now traverse the code string and create a list of boxes */
+				for ( int iy = 0; iy < qrcode.width; iy++ )
+				{
+
+					for ( int ix = 0; ix < qrcode.width; ix++ )
+					{
+
+						/* Symbol data is represented as an array contains 
+						 * width*width uchars. Each uchar represents a module 
+						 * (dot). If the less significant bit of the uchar 
+						 * is 1, the corresponding module is black. The other
+						 * bits are meaningless for us. */
+						if ( (qrcode.data[iy*qrcode.width + ix] & 1) != 0 )
+						{
+							add_box( ix*pixel_size, iy*pixel_size, pixel_size, pixel_size );
+						}
+
+					}
+
+				}
+
+				/* Fill in other info */
+				w = h = qrcode.width  * pixel_size;
+
+			}
+
+
+		}
+
+	}
+
+}
+
+#endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]