-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqrprint.c
More file actions
140 lines (116 loc) · 3.35 KB
/
qrprint.c
File metadata and controls
140 lines (116 loc) · 3.35 KB
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
/*
* qrencode - QR Code encoder
*
* Printing.
* Copyright (c) 2021-∞ blurryroots innovation qanat OÜ
*
* This library 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.0 of the License, or any later version.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*! \file qrprint.h
\brief QR code printint.
^^
*/
#include "qrprint.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void writeUTF8_margin(FILE* fp, int margin, int realwidth,
const char* white, const char *reset, const char* full)
{
int x, y;
for (y = 0; y < margin/2; y++) {
fputs(white, fp);
for (x = 0; x < realwidth; x++)
fputs(full, fp);
fputs(reset, fp);
fputc('\n', fp);
}
}
static int writeUTF8(FILE *fp, const QRcode *qrcode, int use_ansi, int invert, int margin)
{
int x, y;
int realwidth;
const char *white, *reset;
const char *empty, *lowhalf, *uphalf, *full;
empty = " ";
lowhalf = "\342\226\204";
uphalf = "\342\226\200";
full = "\342\226\210";
if (invert) {
const char *tmp;
tmp = empty;
empty = full;
full = tmp;
tmp = lowhalf;
lowhalf = uphalf;
uphalf = tmp;
}
if (use_ansi){
if (use_ansi == 2) {
white = "\033[38;5;231m\033[48;5;16m";
} else {
white = "\033[40;37;1m";
}
reset = "\033[0m";
} else {
white = "";
reset = "";
}
realwidth = (qrcode->width + margin * 2);
/* top margin */
writeUTF8_margin(fp, 0, realwidth, white, reset, full);
/* data */
for(y = 0; y < qrcode->width; y += 2) {
unsigned char *row1, *row2;
row1 = qrcode->data + y*qrcode->width;
row2 = row1 + qrcode->width;
fputs(white, fp);
for (x = 0; x < margin; x++) {
fputs(full, fp);
}
for (x = 0; x < qrcode->width; x++) {
if(row1[x] & 1) {
if(y < qrcode->width - 1 && row2[x] & 1) {
fputs(empty, fp);
} else {
fputs(lowhalf, fp);
}
} else if(y < qrcode->width - 1 && row2[x] & 1) {
fputs(uphalf, fp);
} else {
fputs(full, fp);
}
}
for (x = 0; x < margin; x++)
fputs(full, fp);
fputs(reset, fp);
fputc('\n', fp);
}
/* bottom margin */
writeUTF8_margin(fp, 0, realwidth, white, reset, full);
return 0;
}
void QRprint_utf8(FILE* out, const char* text, QRecLevel quality, int version, int casesensitive)
{
QRcode* code = QRcode_encodeString(
text,
version,
quality,
QR_MODE_8,
casesensitive
);
writeUTF8(out, code, 0, 0, 0);
free(code);
}