Debian Codegraph @

phoenix/qt/font.cpp

raw
1 namespace phoenix {
2
3 string pFont::serif(unsigned size, string style) {
4 if(size == 0) size = 8;
5 if(style == "") style = "Normal";
6 return {"Serif, ", size, ", ", style};
7 }
8
9 string pFont::sans(unsigned size, string style) {
10 if(size == 0) size = 8;
11 if(style == "") style = "Normal";
12 return {"Sans, ", size, ", ", style};
13 }
14
15 string pFont::monospace(unsigned size, string style) {
16 if(size == 0) size = 8;
17 if(style == "") style = "Normal";
18 return {"Liberation Mono, ", size, ", ", style};
19 }
20
21 Size pFont::size(string font, string text) {
22 return pFont::size(pFont::create(font), text);
23 }
24
25 QFont pFont::create(string description) {
26 lstring part;
27 part.split<2>(",", description);
28 for(auto& item : part) item.trim(" ");
29
30 string family = "Sans";
31 unsigned size = 8u;
32 bool bold = false;
33 bool italic = false;
34
35 if(part[0] != "") family = part[0];
36 if(part.size() >= 2) size = decimal(part[1]);
37 if(part.size() >= 3) bold = part[2].find("Bold");
38 if(part.size() >= 3) italic = part[2].find("Italic");
39
40 QFont qtFont;
41 qtFont.setFamily(family);
42 qtFont.setPointSize(size);
43 if(bold) qtFont.setBold(true);
44 if(italic) qtFont.setItalic(true);
45 return qtFont;
46 }
47
48 Size pFont::size(const QFont& qtFont, string text) {
49 QFontMetrics metrics(qtFont);
50
51 lstring lines;
52 lines.split("\n", text);
53
54 unsigned maxWidth = 0;
55 for(auto& line : lines) {
56 maxWidth = max(maxWidth, metrics.width(line));
57 }
58
59 return {maxWidth, metrics.height() * lines.size()};
60 }
61
62 }