forked from fedetft/mxgui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont.cpp
More file actions
117 lines (106 loc) · 4.25 KB
/
font.cpp
File metadata and controls
117 lines (106 loc) · 4.25 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/***************************************************************************
* Copyright (C) 2010 by Terraneo Federico *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* As a special exception, if other files instantiate templates or use *
* macros or inline functions from this file, or you compile this file *
* and link it with other works to produce a work based on this file, *
* this file does not by itself cause the resulting work to be covered *
* by the GNU General Public License. However the source code for this *
* file must still be made available in accordance with the GNU General *
* Public License. This exception does not invalidate any other reasons *
* why a work based on this file might be covered by the GNU General *
* Public License. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/> *
***************************************************************************/
#include "font.h"
#include "misc_inst.h"
#include <cstring>
using namespace std;
namespace mxgui {
//
// Class Font
//
bool Font::isInRange(char32_t c) const
{
for(int i=0;i<numBlocks;i+=2)
if(c >= blocks[i] && c < blocks[i]+blocks[i+1])
return true;
return false;
}
unsigned int Font::getVirtualCodepoint(char32_t codepoint) const
{
// traverse the ranges until the right one is found
int i=2;
// codepoint of the character as if we had one big contiguous range
unsigned int virtualCodepoint=0;
while(i<2*(numBlocks-1) && blocks[i]<=codepoint)
{
virtualCodepoint+=blocks[i-1];
i+=2;
}
// we end up in the first range after our character,
// so need to go back
char32_t rangeBase=blocks[i-2];
unsigned short rangeOffset=codepoint-rangeBase;
if(codepoint <= (rangeBase+blocks[i-1]))
virtualCodepoint+=rangeOffset;
else
virtualCodepoint=numGlyphs-1;
return virtualCodepoint;
}
short int Font::calculateLength(const char *s) const
{
if(isFixedWidth())
{
return strlen(s)*width;
} else {
short int result=0;
char32_t c;
while((c=miosix::Unicode::nextUtf8(s))!='\0')
{
unsigned int vc = getVirtualCodepoint(c);
if(!isInRange(c)) result+=widths[numGlyphs-1];//Width of startchar
else result+=widths[vc];
}
return result;
}
}
void Font::generatePalette(Color out[4], Color fgcolor, Color bgcolor)
{
#ifdef MXGUI_COLOR_DEPTH_16_BIT
unsigned short fgR=fgcolor; //& 0xf800; Optimization, & not required
unsigned short bgR=bgcolor; //& 0xf800; Optimization, & not required
unsigned short fgG=fgcolor & 0x7e0;
unsigned short bgG=bgcolor & 0x7e0;
unsigned short fgB=fgcolor & 0x1f;
unsigned short bgB=bgcolor & 0x1f;
unsigned short deltaR=((fgR-bgR)/3) & 0xf800;
unsigned short deltaG=((fgG-bgG)/3) & 0x7e0;
unsigned short deltaB=((fgB-bgB)/3) & 0x1f;
unsigned short delta=deltaR | deltaG | deltaB;
out[3]=fgcolor;
out[2]=Color(bgcolor+2*delta);
out[1]=Color(bgcolor+delta);
out[0]=bgcolor;
#elif defined(MXGUI_COLOR_DEPTH_8_BIT)
#error TODO
#elif defined(MXGUI_COLOR_DEPTH_1_BIT_LINEAR)
out[0]=out[1]=bgcolor ? white : black;
out[2]=out[3]=fgcolor ? white : black;
#else
#error unsupported color depth
#endif
}
} //namespace mxgui