-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqrcode.cpp
More file actions
81 lines (67 loc) · 1.92 KB
/
qrcode.cpp
File metadata and controls
81 lines (67 loc) · 1.92 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
#include "qrcode.hpp"
#include <lua.hpp>
#include <iostream>
#include <vector>
#include <qrcodegen.hpp>
#include "textmodule_lua.hpp"
#include "textmodule_string.hpp"
#include "textmodule_exception.hpp"
using qrcodegen::QrCode;
using qrcodegen::QrSegment;
#define QRCODE_WHITE false
#define QRCODE_BLACK true
QrCode get_qrcode(lua_State* L, int idx) {
std::vector<QrSegment> segment = QrSegment::makeSegments(tm_tostring(L, idx));
QrCode::Ecc ecc = static_cast<QrCode::Ecc>(tm_tonumber_s(L, idx + 1, 2)-1);
int min_version = tm_tointeger_s(L, idx + 2, QrCode::MIN_VERSION);
int max_version = tm_tointeger_s(L, idx + 3, QrCode::MAX_VERSION);
int mask = tm_tointeger_s(L, idx + 4, -1);
bool boost_ecc = tm_toboolean_s(L, idx + 5, true);
return QrCode::encodeSegments(segment, ecc, min_version, max_version, mask, boost_ecc);
}
int qrcode_encode(lua_State* L) {
QrCode qr = get_qrcode(L, 1);
lua_newtable(L);
for (int x = 0; x < qr.getSize(); x++) {
lua_pushinteger(L, x + 1);
lua_newtable(L);
for (int y = 0; y < qr.getSize(); y++) {
lua_settablevalue(L, y + 1, qr.getModule(x, y));
}
lua_settable(L, -3);
}
return 1;
}
int qrcode_encode_string(lua_State* L) {
lua_Wstring black = tm_towstring(L, 1);
lua_Wstring white = tm_towstring(L, 2);
lua_Wstring ret;
QrCode qr = get_qrcode(L, 3);
for (int x = 0; x < qr.getSize(); x++) {
for (int y = 0; y < qr.getSize(); y++) {
if (qr.getModule(x, y))
ret += black;
else
ret += white;
}
ret += L"\n";
}
lua_pushwstring(L, ret);
return 1;
}
static luaL_Reg TEXTMODULE_QRCODE_REG[] = {
{"encode", qrcode_encode},
{"encode_string", qrcode_encode_string},
{ nullptr, nullptr }
};
void luaReg_qrcode(lua_State* L, lua_Option opt) {
if (opt["api"]["qrcode"]) {
tm_debuglog_apiloaded(opt, "qrcode");
lua_newtable(L);
luaL_register(L, NULL, TEXTMODULE_QRCODE_REG);
lua_setfield(L, -2, "qrcode");
}
else {
tm_debuglog_apinoloaded(opt, "qrcode");
}
}