-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.c
executable file
·195 lines (171 loc) · 4.28 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#define MAX_FACES 10
static FT_Library library;
static FT_Face faces[MAX_FACES]; // 0 & 1 reserved for OCR-A/B
static char* names[MAX_FACES]; // strdup'd in main() for OCR-A/B
static int mults[MAX_FACES] = { 90, 90 };
static FT_GlyphSlot slot;
static unsigned char* monomap; // monochrome bitmap
static int monomode; // monochrome mode
// Unpack a monochrome bitmap into anti-aliased bitmap form
static int mono_unpack(FT_Bitmap* bitmap) {
if (monomap)
free(monomap);
monomap = (unsigned char*)malloc(bitmap->width * bitmap->rows);
if (!monomap)
return -1;
unsigned char* bytes = monomap;
for (int y = 0; y < bitmap->rows; y++) {
unsigned char* bits = &bitmap->buffer[y * bitmap->pitch];
for (int x = 0; x < bitmap->width; x++) {
*bytes++ = (bits[x >> 3] & (1 << (7 - (x & 07)))) ? 255 : 0;
}
}
return 0;
}
int monochrome(int enable) {
monomode = enable ? 1 : 0;
return 0;
}
// The multiplier allows globally adjusting the font size by mult-%.
int load_font(const char* path, const char* name, int mult) {
FT_Error error;
int i;
// First look for a font with the same name
for (i = 2; i < MAX_FACES; i++) {
if (names[i] && strcasecmp(names[i], name) == 0) {
FT_Done_Face(faces[i]);
free(names[i]);
names[i] = 0;
faces[i] = 0;
break;
}
}
if (i == MAX_FACES) {
for (i = 2; i < MAX_FACES && names[i]; i++)
;
}
if (i == MAX_FACES) {
printf("load_font(%s,%s): too many fonts!\n", path, name);
return -1;
}
error = FT_New_Face(library, path, 0, &faces[i]);
if (error) {
printf("New_Face(%s,%s) Error! %d\n", path, name, error);
return error;
}
names[i] = strdup(name);
mults[i] = mult;
return 0;
}
int close_font(const char* name) {
for (int i = 2; i < MAX_FACES; i++) {
if (names[i] && strcasecmp(names[i], name) == 0) {
FT_Done_Face(faces[i]);
free(names[i]);
names[i] = 0;
faces[i] = 0;
break;
}
}
return 0;
}
int find_font(const char* name) {
for (int i = 0; i < MAX_FACES && names[i]; i++) {
if (strcasecmp(name, names[i]) == 0) {
return i;
}
}
// If not found, default is OCR-B
return 1;
}
unsigned char* get_bitmap(int font, int ch, int width, int height) {
FT_Error error;
FT_Face face;
if (font < 0 || font >= MAX_FACES || !names[font])
font = 1; // Default OCR-B
face = faces[font];
width = (width * mults[font] * 64) / 100;
height = (height * mults[font] * 64) / 100;
if (face == faces[1]) {
// The A - Z characters in OCR-B are rendered a tad short, by design.
// But it looks like hell when used in mixed alpha-numeric strings.
// Make them the same size as the digits.
if (ch >= 'A' && ch <= 'Z')
height = (height * 108) / 100;
}
/* 1pt == 1px == 72dpi */
error = FT_Set_Char_Size(face, width, height, 72, 0 );
if (error) {
printf("Set_Char_Size Error! %d\n", error);
return 0;
}
slot = face->glyph;
if (face == faces[0] || face == faces[1]) {
// The OCR fonts we use have some quirks in their glyph maps.
// ^ is mapped as U+02C6 circumflex
// ~ is mapped as U+02DC tilde
if (ch == '~')
ch = 0x02dc;
else if (ch == '^')
ch = 0x02c6;
}
if (monomode)
error = FT_Load_Char(face, ch, FT_LOAD_RENDER | FT_LOAD_TARGET_MONO);
else
error = FT_Load_Char(face, ch, FT_LOAD_RENDER);
if (error) {
printf("Load_Char Error! %d\n", error);
return 0;
}
if (monomode) {
if (mono_unpack(&slot->bitmap) != 0)
return 0;
return monomap;
}
return slot->bitmap.buffer;
}
unsigned get_left() {
return slot->bitmap_left;
}
unsigned get_top() {
return slot->bitmap_top;
}
unsigned get_width() {
return slot->bitmap.width;
}
unsigned get_height() {
return slot->bitmap.rows;
}
unsigned get_pitch() {
return slot->bitmap.pitch;
}
unsigned get_advance() {
return slot->advance.x >> 6;
}
int main() {
FT_Error error;
error = FT_Init_FreeType(&library);
if (error) {
printf("Init Error! %d\n", error);
return 1;
}
error = FT_New_Face(library, "OCRA.otf", 0, &faces[0]);
if (error) {
printf("New_Face(OCR-A) Error! %d\n", error);
return 1;
}
error = FT_New_Face(library, "OCRB.otf", 0, &faces[1]);
if (error) {
printf("New_Face(OCR-B) Error! %d\n", error);
return 1;
}
names[0] = strdup("OCR-A");
names[1] = strdup("OCR-B");
return 0;
}