forked from cehbz/fontconvert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatconvert_compression.cpp
More file actions
99 lines (90 loc) · 2.65 KB
/
flatconvert_compression.cpp
File metadata and controls
99 lines (90 loc) · 2.65 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
/*
TrueType to Adafruit_GFX font converter. Derived from Peter Jakobs'
Adafruit_ftGFX fork & makefont tool, and Paul Kourany's Adafruit_mfGFX.
NOT AN ARDUINO SKETCH. This is a command-line tool for preprocessing
fonts to be used with the Adafruit_GFX Arduino library.
For UNIX-like systems. Outputs to stdout; redirect to header file, e.g.:
./fontconvert ~/Library/Fonts/FreeSans.ttf 18 > FreeSans18pt7b.h
REQUIRES FREETYPE LIBRARY. www.freetype.org
Currently this only extracts the printable 7-bit ASCII chars of a font.
Will eventually extend with some int'l chars a la ftGFX, not there yet.
Keep 7-bit fonts around as an option in that case, more compact.
See notes at end for glyph nomenclature & other tidbits.
*/
#include "flatconvert.h"
extern "C"
{
#include "heatshrink_encoder.h"
}
/**
*
* @return
*/
bool FontConverter::compressInPlace(uint8_t *in, int &inoutSize)
{
uint8_t tmp[FC_BUFFER_SIZE];
//printf("Compressing...\n");
bitPusher.align();
const uint8_t *src=in;
int size=inoutSize;
heatshrink_encoder *hse = heatshrink_encoder_alloc(8, 4); // 8 4
if(!hse)
{
printf("Cannot initialize heatshrink\n");
exit(-1);
}
size_t sunk = 0;
size_t count=0;
size_t polled = 0;
size_t comp_sz=FC_BUFFER_SIZE;
while (sunk < size)
{
HSE_sink_res esres = heatshrink_encoder_sink(hse, (uint8_t *)(src+sunk), size - sunk, &count);
if(esres <0)
{
printf("sink fail\n");
exit(-1);
}
sunk += count;
if (sunk == size)
{
if(HSER_FINISH_MORE!= heatshrink_encoder_finish(hse))
{
printf("Finish fail\n");
exit(-1);
}
}
HSE_poll_res pres;
do
{
pres = heatshrink_encoder_poll(hse, &tmp[polled], comp_sz - polled, &count);
polled += count;
} while (pres == HSER_POLL_MORE);
if(HSER_POLL_EMPTY!= pres)
{
printf("Poll fail\n");
exit(-1);
}
if (polled >= comp_sz)
{
printf("compression overflow\n");
exit(-1);
}
if (sunk == size)
{
if(HSER_FINISH_DONE!= heatshrink_encoder_finish(hse))
{
printf("done state failed\n");
exit(-1);
}
}
}
//printf("Input size =%d, output size=%d\n",(int)size,(int)polled);
//printf("Shrinked to %d %%\n",(int)((polled*100)/size));
// copy tmp to bitPusher
heatshrink_encoder_free(hse);
hse=NULL;
memcpy(in,tmp,polled);
inoutSize=polled;
return true;
}