-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagefont.cpp
More file actions
executable file
·129 lines (93 loc) · 3.99 KB
/
imagefont.cpp
File metadata and controls
executable file
·129 lines (93 loc) · 3.99 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
118
119
120
121
122
123
124
125
126
127
128
129
#include "imagefont.h"
ImageFont::ImageFont(const char* glyphs, QImage font_image){
this->height_px = font_image.height();
int glyph_index = 0;
//printf("Glyph string is %s\n", glyphs);
//assumes that the image starts with a column of the separation color
QRgb separation_color = font_image.pixel(0, 0);
//iterate over top row of image
for (int x = 1; x < font_image.width(); ++x){
//detect first column of new glyph
if (font_image.pixel(x, 0) != separation_color){
//find the end column of the glyph
int past_x;
for (past_x = x; past_x < font_image.width(); ++past_x){
if (font_image.pixel(past_x, 0) == separation_color){
break;
}
}
//make the pixel map for the glyph
QPixmap glyph_image = QPixmap::fromImage(font_image.copy(x, 0, past_x - x, font_image.height()));
//pair it with its proper character
char c = glyphs[glyph_index];
if (this->characters.contains(c)){
fprintf(stderr, "Font already contains %c (%d), skipping...\n", c, c);
} else {
//printf("Insert %c (%d)\n", c, c);
this->characters.insert(c, glyph_image);
}
++glyph_index;
if (!c) break; //null terminator reached
//skip to the next separator
x = past_x - 1;
}
}
this->background_color = this->getPixmapFor(' ').toImage().pixelColor(0, 0);
this->font_height = font_image.height();
}
void ImageFont::print(QPainter* painter, QPoint start, alignment_enum alignment, QString string, double sx, double sy){
painter->save();
painter->translate(start);
if (sy == 0){
sy = sx;
}
painter->scale(sx, sy);
int string_width = 0;
for (int i = 0; i < string.length(); i++){
QPixmap glyph_pixmap = this->getPixmapFor(static_cast<char>(string[i].unicode()));
string_width += glyph_pixmap.width() + 1;
}
painter->setBrush(this->background_color);
painter->setPen(this->background_color);
if (alignment == RIGHT_ALIGN){
painter->drawRect(QRect(-string_width, 0, string_width, this->font_height));
for (int i = string.length()-1; i >= 0; i--){
QPixmap glyph_pixmap = this->getPixmapFor(static_cast<char>(string[i].unicode()));
painter->translate(-(glyph_pixmap.width()+1), 0);
painter->drawPixmap(glyph_pixmap.rect(), glyph_pixmap);
}
} else {
painter->drawRect(QRect(0, 0, string_width, this->font_height));
for (int i = 0; i < string.length(); i++){
QPixmap glyph_pixmap = this->getPixmapFor(static_cast<char>(string[i].unicode()));
painter->drawPixmap(glyph_pixmap.rect(), glyph_pixmap);
painter->translate(glyph_pixmap.width()+1, 0);
}
}
painter->restore();
}
QPixmap ImageFont::getPixmapFor(char c){
if (this->characters.contains(c)){
return this->characters.value(c);
}
if (this->isAlpha(c)){
char othercase = this->switchCase(c);
if (this->characters.contains(othercase)){
return this->characters.value(othercase);
}
fprintf(stderr, "\'%c\' (%u) AND \'%c\' (%u) not found in glyph list for Image Font!\n",
c, c, othercase, othercase);
} else {
fprintf(stderr, "\'%c\' (%u) not found in glyph list for Image Font!\n", c, c);
}
return QPixmap();
}
char ImageFont::switchCase(char c){
if (c <= 'Z'){
return c + this->case_difference;
}
return c - this->case_difference;
}
bool ImageFont::isAlpha(char c){
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}