1 | // Aseprite |
---|---|
2 | // Copyright (C) 2020 Igara Studio S.A. |
3 | // Copyright (C) 2001-2017 David Capello |
4 | // |
5 | // This program is distributed under the terms of |
6 | // the End-User License Agreement for Aseprite. |
7 | |
8 | #ifdef HAVE_CONFIG_H |
9 | #include "config.h" |
10 | #endif |
11 | |
12 | #include "app/ui/skin/font_data.h" |
13 | |
14 | #include "os/font.h" |
15 | #include "os/system.h" |
16 | #include "ui/scale.h" |
17 | |
18 | #include <set> |
19 | |
20 | namespace app { |
21 | namespace skin { |
22 | |
23 | FontData::FontData(os::FontType type) |
24 | : m_type(type) |
25 | , m_antialias(false) |
26 | , m_fallback(nullptr) |
27 | , m_fallbackSize(0) |
28 | { |
29 | } |
30 | |
31 | os::FontRef FontData::getFont(int size) |
32 | { |
33 | if (m_type == os::FontType::SpriteSheet) |
34 | size = 1; // Same size always |
35 | |
36 | // Use cache |
37 | size *= ui::guiscale(); |
38 | auto it = m_fonts.find(size); |
39 | if (it != m_fonts.end()) |
40 | return it->second; |
41 | |
42 | os::FontRef font = nullptr; |
43 | |
44 | switch (m_type) { |
45 | case os::FontType::SpriteSheet: |
46 | font = os::instance()->loadSpriteSheetFont(m_filename.c_str(), size); |
47 | break; |
48 | case os::FontType::FreeType: { |
49 | font = os::instance()->loadTrueTypeFont(m_filename.c_str(), size); |
50 | if (font) |
51 | font->setAntialias(m_antialias); |
52 | break; |
53 | } |
54 | } |
55 | |
56 | if (m_fallback) { |
57 | os::FontRef fallback = m_fallback->getFont(m_fallbackSize); |
58 | if (font) |
59 | font->setFallback(fallback.get()); |
60 | else |
61 | return fallback; // Don't double-cache the fallback font |
62 | } |
63 | |
64 | // Cache this font |
65 | m_fonts[size] = font; |
66 | return font; |
67 | } |
68 | |
69 | } // namespace skin |
70 | } // namespace app |
71 |