| 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 {"Georgia, ", 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 {"Tahoma, ", 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 {"Lucida Console, ", size, ", ", style}; |
| 19 |
} |
| 20 |
|
| 21 |
Size pFont::size(string font, string text) { |
| 22 |
HFONT hfont = pFont::create(font); |
| 23 |
Size size = pFont::size(hfont, text); |
| 24 |
pFont::free(hfont); |
| 25 |
return size; |
| 26 |
} |
| 27 |
|
| 28 |
HFONT pFont::create(string description) { |
| 29 |
lstring part; |
| 30 |
part.split(",", description); |
| 31 |
for(auto& item : part) item.trim(" "); |
| 32 |
|
| 33 |
string family = "Sans"; |
| 34 |
unsigned size = 8u; |
| 35 |
bool bold = false; |
| 36 |
bool italic = false; |
| 37 |
|
| 38 |
if(part[0] != "") family = part[0]; |
| 39 |
if(part.size() >= 2) size = decimal(part[1]); |
| 40 |
if(part.size() >= 3) bold = part[2].find("Bold"); |
| 41 |
if(part.size() >= 3) italic = part[2].find("Italic"); |
| 42 |
|
| 43 |
return CreateFont( |
| 44 |
-(size * 96.0 / 72.0 + 0.5), |
| 45 |
0, 0, 0, bold == false ? FW_NORMAL : FW_BOLD, italic, 0, 0, 0, 0, 0, 0, 0, |
| 46 |
utf16_t(family) |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
void pFont::free(HFONT hfont) { |
| 51 |
DeleteObject(hfont); |
| 52 |
} |
| 53 |
|
| 54 |
Size pFont::size(HFONT hfont, string text) { |
| 55 |
//temporary fix: empty text string returns height of zero; bad for eg Button height |
| 56 |
if(text.empty()) text = " "; |
| 57 |
|
| 58 |
HDC hdc = GetDC(0); |
| 59 |
SelectObject(hdc, hfont); |
| 60 |
RECT rc = {0, 0, 0, 0}; |
| 61 |
DrawText(hdc, utf16_t(text), -1, &rc, DT_CALCRECT); |
| 62 |
ReleaseDC(0, hdc); |
| 63 |
return {rc.right, rc.bottom}; |
| 64 |
} |
| 65 |
|
| 66 |
} |