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
/
XTBFont1Bit.cpp
119 lines (108 loc) · 2.55 KB
/
XTBFont1Bit.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
/*
* XTBFont1Bit.cpp
* XTBook
*
* Created by Nexhawks on 2/11/11.
* Copyright 2011 Nexhawks. All rights reserved.
*
*/
#include "XTBFont1Bit.h"
#include <tcw/twSDLDC.h>
XTBFont1Bit::XTBFont1Bit(unsigned char *fontData, unsigned int *indexMap, unsigned char *widthMap)
:XTBFont(fontData, indexMap, widthMap){
}
void XTBFont1Bit::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++){
if(lastData&1)
*pixels2=color;
bitPos++;
if(bitPos==8){
// next data!
bitPos=0;
data++;
lastData=*data;
}else{
lastData>>=1;
}
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(lastData&1){
if(px>=clipRect.x && py>=clipRect.y && px<clipRect.w && py<clipRect.h)
*pixels2=color;
}
bitPos++;
if(bitPos==8){
// next data!
bitPos=0;
data++;
lastData=*data;
}else{
lastData>>=1;
}
pixels2++;
px++;
}
py++;
pixels+=pitch;
}
}
}
void XTBFont1Bit::drawGlyph(twSDLDC *dc, uint16_t color, twPoint pt, uint16_t chr) const{
if(indexMap[chr]==0xffffffffU){
// the glyph isn't available.
return;
}
int blockCount=(widthMap[chr]+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+=9;
}
}