This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
XTBFont4Bit.cpp
202 lines (171 loc) · 4.02 KB
/
XTBFont4Bit.cpp
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
/*
* XTBFont4Bit.cpp
* XTBook
*
* Created by Nexhawks on 2/11/11.
* Copyright 2011 Nexhawks. All rights reserved.
*
*/
#include "XTBFont4Bit.h"
#include <tcw/twSDLDC.h>
XTBFont4Bit::XTBFont4Bit(unsigned char *fontData, unsigned int *indexMap, unsigned char *widthMap)
:XTBFont(fontData, indexMap, widthMap){
}
#define doDraw() \
do{ \
register int alphaValue=lastData&0xf; \
if(alphaValue==0) \
break; \
register unsigned int tmp1=*pixels2; \
register unsigned int tmp2, tmp3=0; \
tmp2=(tmp1&0xF81F)*(0x10-alphaValue); \
tmp2+=((unsigned int)color&0xF81F)*alphaValue; \
tmp3=(tmp2>>4)&0xF81F; \
tmp2=(tmp1&0x7E0)*(0x10-alphaValue); \
tmp2+=((unsigned int)color&0x7E0)*alphaValue; \
tmp3|=(tmp2>>4)&0x7FE0; \
*pixels2=tmp3; \
}while(0)
void XTBFont4Bit::drawBlock(twSDLDC *dc, uint16_t color, twPoint pt, const unsigned char *data) const{
SDL_Surface *surface=dc->getSurface();
uint16_t *pixels=(uint16_t *)(surface->pixels);
int pitch=surface->pitch/2;
twRect clipRect=dc->getClipRect();
twRect bounds=dc->getBounds();
clipRect.x-=bounds.x;
clipRect.y-=bounds.y;
clipRect.w+=clipRect.x;
clipRect.h+=clipRect.y;
if(clipRect.x<0){
clipRect.w-=clipRect.x;
clipRect.x=0;
}
if(clipRect.y<0){
clipRect.h-=clipRect.y;
clipRect.y=0;
}
if(clipRect.w>surface->w){
clipRect.w=surface->w;
}
if(clipRect.h>surface->h){
clipRect.h=surface->h;
}
// cull out the invisible block
if(pt.x<=clipRect.x-6 || pt.y<=clipRect.y-12 || pt.x>=clipRect.w || pt.y>=clipRect.h)
return;
if(pt.x>=clipRect.x && pt.y>=clipRect.y && pt.x+6<=clipRect.w && pt.y+12<=clipRect.h){
// if the block is completely visible...
pixels+=pt.x+pt.y*pitch;
register unsigned char lastData=*data;
register int bitPos=0;
for(int h=0;h<12;h++){
register uint16_t *pixels2=pixels;
for(int x=0;x<6;x++){
doDraw();
bitPos++;
if(bitPos==2){
// next data!
bitPos=0;
data++;
lastData=*data;
}else{
lastData>>=4;
}
pixels2++;
}
pixels+=pitch;
}
}else{
// or, if the block is partially visible... clip.
pixels+=pt.x+pt.y*pitch;
register unsigned char lastData=*data;
register int bitPos=0;
register int px, py;
py=pt.y;
for(int h=0;h<12;h++){
register uint16_t *pixels2=pixels;
px=pt.x;
for(int x=0;x<6;x++){
if(px>=clipRect.x && py>=clipRect.y && px<clipRect.w && py<clipRect.h)
doDraw();
bitPos++;
if(bitPos==2){
// next data!
bitPos=0;
data++;
lastData=*data;
}else{
lastData>>=4;
}
pixels2++;
px++;
}
py++;
pixels+=pitch;
}
}
}
void XTBFont4Bit::drawGlyph(twSDLDC *dc, uint16_t color, twPoint pt, uint16_t chr) const{
if(indexMap[chr]==0xffffffffU){
// the glyph isn't available.
return;
}
int w=widthMap[chr];
int blockCount=0;
while(w>0){
blockCount++;
w-=6;
} // blockCount=(w+5)/6
unsigned char *data=fontData+indexMap[chr];
for(int block=0;block<blockCount;block++){
drawBlock(dc, color, twPoint(pt.x, pt.y), data);
pt.x+=6;
data+=36;
}
}
void XTBFont4Bit::bitmapForGlyph(uint8_t *glyphBuf, uint16_t chr) const{
uint8_t *buf=glyphBuf;
int w=widthMap[chr];
int blockCount=0;
while(w>0){
blockCount++;
w-=6;
} // blockCount=(w+5)/6
w=widthMap[chr];
unsigned char *data=fontData+indexMap[chr];
int leftW=w;
for(int block=0;block<blockCount;block++){
placeBlock(buf, w, data, std::min(6, leftW));
data+=36;
buf+=6;
leftW-=6;
}
}
void XTBFont4Bit::placeBlock(uint8_t *dest, size_t pitch,
const unsigned char *data, int rows) const{
for(int h=0;h<12;h++){
uint8_t *dest2=dest;
const unsigned char *data2=data;
register unsigned char lastData=*data2;
register int bitPos=0;
register int x;
for(x=0;x<rows;x++){
{
register uint8_t px=lastData;
px=px&0xf;
px|=px<<4;
*(dest2++)=px;
}
bitPos++;
if(bitPos==2){
bitPos=0;
data2++;
lastData=*data2;
}else{
lastData>>=4;
}
}
dest+=pitch;
data+=3;
}
}