File: font.cpp

package info (click to toggle)
higan 106-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 9,640 kB
  • sloc: cpp: 108,736; ansic: 809; makefile: 22; sh: 7
file content (42 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download
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
#if defined(Hiro_Font)

namespace hiro {

auto pFont::size(const Font& font, const string& text) -> Size {
  auto hfont = pFont::create(font);
  auto size = pFont::size(hfont, text);
  pFont::free(hfont);
  return size;
}

auto pFont::size(HFONT hfont, const string& text) -> Size {
  HDC hdc = GetDC(0);
  SelectObject(hdc, hfont);
  RECT rc = {0, 0, 0, 0};
  DrawText(hdc, utf16_t(text), -1, &rc, DT_CALCRECT);
  ReleaseDC(0, hdc);
  return {rc.right, rc.bottom};
}

auto pFont::family(const string& family) -> string {
  if(family == Font::Sans ) return "Tahoma";
  if(family == Font::Serif) return "Georgia";
  if(family == Font::Mono ) return "Lucida Console";
  return family ? family : "Tahoma";
}

auto pFont::create(const Font& font) -> HFONT {
  return CreateFont(
    -(Application::scale(font.size() ? font.size() : 8) * 96.0 / 72.0 + 0.5),
    0, 0, 0, font.bold() ? FW_BOLD : FW_NORMAL, font.italic(), 0, 0, 0, 0, 0, 0, 0,
    utf16_t(family(font.family()))
  );
}

auto pFont::free(HFONT hfont) -> void {
  DeleteObject(hfont);
}

}

#endif