Skip to content

Commit

Permalink
Merge branch 'release/0.1.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
heuer committed Sep 15, 2016
2 parents 2ef16d1 + dd08ea7 commit c0610ff
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changes
=======

0.1.9 -- 2016-09-15
-------------------
* Added "color" parameter to the LaTeX serializer to define the color of the
dark modules.
* Fixed serious issue #23: Segno creates invalid QR Codes if boost_error
is not disabled (enabled by default)


0.1.8 -- 2016-09-14
-------------------
* Removed ``utils.matrix_with_border_iter``
Expand Down
5 changes: 4 additions & 1 deletion segno/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
VersionError, DataOverflowError
from . import writers, utils

__version__ = '0.1.8'
__version__ = '0.1.9'

__all__ = ('make', 'make_qr', 'make_micro', 'QRCode', 'QRCodeError',
'ErrorLevelError', 'ModeError', 'MaskError', 'VersionError',
Expand Down Expand Up @@ -604,6 +604,9 @@ def save(self, out, kind=None, **kw):
============= ==============================================================
kind "tex"
scale integer or float
color LaTeX color name (default: "black"). The color is written
"at it is", so ensure that the color is a standard color or it
has been defined in the enclosing LaTeX document.
url Default: ``None``. Optional URL where the QR Code should
point to. Requires the hyperref package.
============= ==============================================================
Expand Down
19 changes: 13 additions & 6 deletions segno/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def boost_error_level(version, error, segments):
:param segments: Instance of :py:class:`Segments`
"""
if error not in (consts.ERROR_LEVEL_H, None) and len(segments) == 1:
mode = segments[0].mode
modes = segments.modes
data_length = segments.data_length
levels = [consts.ERROR_LEVEL_L, consts.ERROR_LEVEL_M,
consts.ERROR_LEVEL_Q, consts.ERROR_LEVEL_H]
Expand All @@ -182,11 +182,18 @@ def boost_error_level(version, error, segments):
if version < consts.VERSION_M4:
levels.pop() # Error level Q isn't supported by M2 and M3
for level in levels[levels.index(error)+1:]:
try:
if consts.SYMBOL_CAPACITY[version][error][mode] >= data_length:
error = level
except KeyError:
pass
found = False
for mode in modes:
try:
if consts.SYMBOL_CAPACITY[version][level][mode] >= data_length:
found = True
except KeyError:
found = False
break
if found:
error = level
else:
break
return error


Expand Down
9 changes: 7 additions & 2 deletions segno/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def groups_of_eight(row):
f.close()


def write_tex(matrix, version, out, scale=1, border=None, unit='pt', url=None):
def write_tex(matrix, version, out, scale=1, border=None, color='black', unit='pt', url=None):
"""\
Serializes the matrix as LaTeX PGF picture.
Expand All @@ -729,6 +729,9 @@ def write_tex(matrix, version, out, scale=1, border=None, unit='pt', url=None):
:param int border: Integer indicating the size of the quiet zone.
If set to ``None`` (default), the recommended border size
will be used (``4`` for QR Codes, ``2`` for a Micro QR Codes).
:param str color: LaTeX color name. The color name is taken at it is, so
ensure that it refers either to a default color name or that the
color was defined previously.
:param unit: Unit of the drawing (default: ``pt``)
:param url: Optional URL where the QR Code should point to. Requires the
"hyperref" package. Default: ``None``.
Expand All @@ -744,9 +747,11 @@ def point(x, y):
write('%% Creator: {0}\n'.format(CREATOR))
write('%% Date: {0}\n'.format(time.strftime('%Y-%m-%dT%H:%M:%S')))
if url:
write('\href{{{0}}}{{\n'.format(url))
write('\href{{{0}}}{{'.format(url))
write('\\begin{pgfpicture}\n')
write(' \pgfsetlinewidth{{{0}{1}}}\n'.format(scale, unit))
if color and color != 'black':
write(' \color{{{0}}}\n'.format(color))
x, y = border, -border
for (x1, y1), (x2, y2) in matrix_to_lines(matrix, x, y, incby=-1):
write(' \pgfpathmoveto{{{0}}}\n'.format(point(x1 * scale, y1 * scale)))
Expand Down
37 changes: 37 additions & 0 deletions tests/test_issue23_boosterror.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 -- Lars Heuer - Semagia <http://www.semagia.com/>.
# All rights reserved.
#
# License: BSD License
#
"""\
Test against issue #23.
<https://github.com/heuer/segno/issues/23>
:author: Lars Heuer (heuer[at]semagia.com)
:organization: Semagia - http://www.semagia.com/
:license: BSD License
"""
from __future__ import unicode_literals, absolute_import
import segno


def test_boost_error_automatic():
qr = segno.make('http://www.example.org/bla/bla/')
assert '3-Q' == qr.designator


def test_boost_error_disabled():
qr = segno.make('http://www.example.org/bla/bla/', error='q', boost_error=False)
assert '3-Q' == qr.designator


def test_boost_error_disabled2():
qr = segno.make('http://www.example.org/bla/bla/', error='h', boost_error=False)
assert '4-H' == qr.designator


if __name__ == '__main__':
import pytest
pytest.main([__file__])
14 changes: 14 additions & 0 deletions tests/test_tex.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ def test_write_tex_omit_url2():
assert '\href' not in out.getvalue()


def test_write_tex_color():
qr = segno.make_qr('test', error='m', boost_error=False)
out = io.StringIO()
qr.save(out, kind='tex', border=4)
assert '\color' not in out.getvalue()


def test_write_tex_color2():
qr = segno.make_qr('test', error='m', boost_error=False)
out = io.StringIO()
qr.save(out, kind='tex', border=4, color='green')
assert '\color{green}' in out.getvalue()


_COMMAND_PATTERN = re.compile(r'pgfpath(move|line)to\{\\pgfqpoint\{(\-?[0-9]+)pt\}\{(\-?[0-9]+)pt\}')

def tex_as_matrix(buff, border):
Expand Down

0 comments on commit c0610ff

Please sign in to comment.