This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegno_quark.py
251 lines (213 loc) · 8.96 KB
/
segno_quark.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 -- Lars Heuer - Semagia <http://www.semagia.com/>.
# All rights reserved.
#
# License: BSD License
#
"""\
Experimental Segno converter plugin to create SVG (Micro) QR Codes with some
effects.
"""
from __future__ import absolute_import, unicode_literals
import xml.etree.ElementTree as etree
import io
import copy
import random
import re
from segno import encoder
from segno.writers import _color_to_webcolor as color_to_webcolor
__version__ = '0.1.2'
_SVG_NS = 'http://www.w3.org/2000/svg'
_XLINK_NS = 'http://www.w3.org/1999/xlink'
# Used to minimize the XML
_REMOVE_WS_PATTERN = re.compile(r'(>)\s+(<)')
def _make_defs_if_not_exists(svg):
"""\
Adds a <defs/> element to the SVG iff it not exists.
:param svg: etree.Element
"""
el_defs = svg.find('{{{0}}}defs[1]'.format(_SVG_NS))
if el_defs is None:
el_defs = _make_svg_element('defs')
svg.insert(0, el_defs)
return el_defs
def _find_last_path(svg):
"""\
Returns the last within the SVG document.
:param svg: etree.Element
"""
return svg.find('{{{0}}}path[last()]'.format(_SVG_NS))
def _parse_element(xml):
"""\
Parses the provided XML and returns an etree.Element.
:param str xml: The string to parse.
"""
return etree.fromstring(_REMOVE_WS_PATTERN.sub(r'\1\2', xml))
def _make_svg_element(name, **kw):
"""\
Factory function to create an element in the SVG namespace.
"""
return etree.Element('{{{0}}}{1}'.format(_SVG_NS, name), **kw)
def _make_use_el(href, **kw):
"""\
Creates <use href=..."/> element.
"""
kw['{{{0}}}href'.format(_XLINK_NS)] = href
return _make_svg_element('use', **kw)
def _write_xml(xml, out, **kw):
"""\
Serializes the provided etree.
:param xml.etree.ElementTree: The etree to serialize
:param out: Output stream
:param **kw: Any keywords, but only 'encoding' and 'xmldecl' is evaluated.
"""
xml.write(out, encoding=str(kw.get('encoding', 'utf-8')),
xml_declaration=kw.get('xmldecl', True))
def _write_filter(qrcode, out, filter, filter_id, postparse_callback=None, **kw):
"""\
:param filter: etree.Element or str
:param filter_id: The filter identifier.
:param qrcode: The :py:class:`segno.QRCode`.
"""
xml = as_etree(qrcode, **kw)
svg = xml.getroot()
path = _find_last_path(svg)
if postparse_callback is not None:
postparse_callback(svg, path)
el = filter if isinstance(filter, etree.Element) else _parse_element(filter)
el.attrib['id'] = filter_id
_make_defs_if_not_exists(svg).append(el)
path.attrib['filter'] = 'url(#{0})'.format(filter_id)
_write_xml(xml, out, **kw)
def write_glow(qrcode, out, filter_id='segno-glow', deviation=.6, **kw):
"""\
Creates a "glow" effect.
:param qrcode: The :py:class:`segno.QRCode`.
:param out: Filename or a file-like object supporting to write bytes.
:param str filter_id: Name of the filter.
:param float deviation: Indicates the standard deviation for the blur
operation, default: ``.6``.
:param **kw: SVG parameters, see segno.QRCode.svg
"""
s = '''<filter filterUnits="userSpaceOnUse">
<feGaussianBlur stdDeviation="{0}" in="SourceGraphic" result="coloredBlur"/>
<feMerge>
<feMergeNode in="coloredBlur"/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>'''.format(deviation)
_write_filter(qrcode, out, s, filter_id=filter_id, **kw)
def write_blur(qrcode, out, filter_id='segno-blur', deviation=.3, **kw):
"""\
:param qrcode: The :py:class:`segno.QRCode`.
:param out: Filename or a file-like object supporting to write bytes.
:param str filter_id: Name of the filter.
:param **kw: SVG parameters, see segno.QRCode.svg
"""
s = '''<filter>
<feGaussianBlur in="SourceGraphic" stdDeviation="{0}" />
</filter>'''.format(deviation)
_write_filter(qrcode, out, s, filter_id=filter_id, **kw)
def write_pacman(qrcode, out, pacman_color='#fc0', dot_color='#fc0', ghosts=5,
ghost_colors=('#ff0c13', '#f2aeaf', '#1bb1e6', '#f97e16'),
**kw):
"""\
:param qrcode: The :py:class:`segno.QRCode`.
:param out: Filename or a file-like object supporting to write bytes.
:param pacman_color:
:param dot_color:
:param ghosts:
:param ghost_colors:
:param **kw: SVG parameters, see segno.QRCode.svg
"""
smiley_el = _parse_element('<path d="M.947.724a.5.5 0 1 1 .001-.446l-.448.222z" transform="scale(.8)"/>')
smiley_el.attrib['fill'] = color_to_webcolor(pacman_color)
xml = as_etree(qrcode, **kw)
svg = xml.getroot()
defs_el = _make_defs_if_not_exists(svg)
dot_el = _parse_element('<circle id="dot" cx="1" cy="1" r=".5" stroke="none" transform="scale(.2)"/>')
dot_el.attrib['fill'] = color_to_webcolor(dot_color)
defs_el.append(dot_el)
if ghosts > 0:
defs_el.append(etree.Comment('''
The "ghost" was created by James Fenton
Source: <https://thenounproject.com/term/pacman/193957/>
License: CC BY 3.0 US - <https://creativecommons.org/licenses/by/3.0/us/>
'''))
ghost_el = _parse_element('<g id="ghost" transform="scale(.08)"><circle cx="6.588" cy="4.515" r=".526"/><path d="M4.031 0c-2.136 0-3.883 1.663-4.02 3.757h-.011v5.799s.232.663.926-.168c.705-.831 1.21.168 1.21.168s.547 1 1.21 0c.663-1 1.315 0 1.315 0s.61.852 1.168 0c.558-.852 1.116-.168 1.116-.168s1.073 1.158 1.073 0c0-.863.042-5.157.042-5.357 0-2.221-1.8-4.031-4.031-4.031zm-.295 5.736c-.579 0-1.052-.547-1.052-1.231 0-.674.474-1.231 1.052-1.231.579 0 1.052.547 1.052 1.231s-.463 1.231-1.052 1.231zm2.515 0c-.579 0-1.052-.547-1.052-1.231 0-.674.474-1.231 1.052-1.231.579 0 1.052.547 1.052 1.231s-.474 1.231-1.052 1.231z"/><circle cx="4.073" cy="4.515" r=".526"/></g>')
defs_el.append(ghost_el)
qrcode_width, qrcode_height = qrcode.symbol_size()
function_matrix = encoder.make_matrix(qrcode_width, qrcode_height, reserve_regions=False, add_timing=False)
encoder.add_finder_patterns(function_matrix, qrcode_width, qrcode_height)
encoder.add_alignment_patterns(function_matrix, qrcode_width, qrcode_height)
def is_data_area(y, x):
return function_matrix[y][x] == 0x2
matrix = qrcode.matrix
max_length = -1
row_idx = -1
col_idx = -1
for i in range(len(matrix)):
length = 0
for j in range(len(matrix)):
if is_data_area(i, j) and matrix[i][j]:
length += 1
if length > max_length:
max_length = length
row_idx = i
col_idx = j - max_length + 1
else:
length = 0
border = kw.get('border', qrcode.default_border_size)
scale = kw.get('scale', 1)
g_el = _make_svg_element('g')
path = _find_last_path(svg)
svg.remove(path)
svg.append(g_el)
g_el.append(path)
if scale > 1:
g_el.attrib['transform'] = 'scale({0})'.format(scale)
del path.attrib['transform']
smiley_g = _make_svg_element('g')
offset = 0
if max_length > 2:
offset = 1
smiley_g.attrib['transform'] = 'translate({0}, {1})'.format(col_idx + offset + border + .1,
row_idx + border + .1)
smiley_g.append(smiley_el)
smiley_g.extend([_make_use_el('#dot', x=str(i + 1.2), y='.2') for i in range(max_length - 2)])
g_el.append(smiley_g)
if ghosts > 0:
ghost_groups = [_make_svg_element('g', fill=color_to_webcolor(clr)) for clr in ghost_colors]
ghost_count = 0
ghost_matrix = copy.deepcopy(matrix)
ghost_matrix[row_idx][col_idx:col_idx + max_length] = b'\0' * max_length
size = len(matrix) - 1
while ghost_count < ghosts:
x, y = random.randint(0, size), random.randint(0, size)
if is_data_area(y, x) and ghost_matrix[y][x] == 0x1:
ghost_matrix[y][x] = 0x0
if ghost_matrix[max(0, y - 1)][x] == 0x0 \
and ghost_matrix[min(size, y + 1)][x] == 0x0 \
and ghost_matrix[y][min(size, x + 1)] == 0x0 \
and ghost_matrix[y][max(0, x - 1)] == 0x0:
continue
ghost_count += 1
random.choice(ghost_groups).append(_make_use_el('#ghost',
x=str(x + border + .2),
y=str(y + border + .1)))
g_el.extend([ghost_group for ghost_group in ghost_groups if len(ghost_group)])
_write_xml(xml, out, **kw)
def as_etree(qrcode, **kw):
"""\
Returns the provided `qrcode` as SVG ElementTree.
:param qrcode: The :py:class:`segno.QRCode`.
:param **kw: SVG parameters, see py:method:`segno.QRCode.save()`
:rtype: :py:class:`xml.etree.ElementTree`
"""
buff = io.BytesIO()
qrcode.save(buff, kind='svg', **kw)
buff.seek(0)
return etree.parse(buff)
etree.register_namespace('', _SVG_NS)
etree.register_namespace('xlink', _XLINK_NS)