font.cpp 1011 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #if defined(Hiro_Font)
  2. namespace hiro {
  3. auto pFont::size(const Font& font, const string& text) -> Size {
  4. return pFont::size(pFont::create(font), text);
  5. }
  6. auto pFont::size(const QFont& qtFont, const string& text) -> Size {
  7. QFontMetrics metrics(qtFont);
  8. signed maxWidth = 0;
  9. auto lines = text.split("\n");
  10. for(auto& line : lines) {
  11. maxWidth = max(maxWidth, metrics.width(QString::fromUtf8(line)));
  12. }
  13. return {maxWidth, metrics.height() * (signed)lines.size()};
  14. }
  15. auto pFont::family(const string& family) -> QString {
  16. if(family == Font::Sans ) return "Sans";
  17. if(family == Font::Serif) return "Serif";
  18. if(family == Font::Mono ) return "Liberation Mono";
  19. return family ? QString::fromUtf8(family) : "Sans";
  20. }
  21. auto pFont::create(const Font& font) -> QFont {
  22. QFont qtFont;
  23. qtFont.setFamily(family(font.family()));
  24. qtFont.setPointSize(Application::scale(font.size() ? font.size() : 8));
  25. qtFont.setBold(font.bold());
  26. qtFont.setItalic(font.italic());
  27. return qtFont;
  28. }
  29. }
  30. #endif