-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfont_library.cpp
More file actions
46 lines (38 loc) · 1.57 KB
/
font_library.cpp
File metadata and controls
46 lines (38 loc) · 1.57 KB
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
43
44
45
46
#include "font_library.hpp"
#include <algorithm>
#include <utility>
namespace mfl
{
namespace
{
font_parameters make_font_parameters(const points size, const font_library& fonts)
{
const auto& face = fonts.get_face(font_family::roman, size);
const auto x_index = face.glyph_index_from_code_point('x', false);
const auto x_height = face.glyph_info(x_index).height;
const auto capital_m_index = face.glyph_index_from_code_point('M', false);
const auto capital_m_width = face.glyph_info(capital_m_index).width;
return {
.size = size, .x_height = x_height, .capital_m_width = capital_m_width, .math_info = face.constants()};
}
}
font_library::font_library(const points font_size, font_face_creator create_face)
: create_face_(std::move(create_face))
, normal_parameters_(make_font_parameters(font_size, *this))
, small_parameters_(make_font_parameters(font_size * 0.7, *this))
, tiny_parameters_(make_font_parameters(font_size * 0.5, *this))
{
}
const abstract_font_face& font_library::get_face(const font_family family, const points size) const
{
auto it = std::ranges::find_if(cached_faces_, [&](const auto& entry) { return entry.first == family; });
if (it == cached_faces_.end())
{
cached_faces_.emplace_back(family, create_face_(family));
it = cached_faces_.end() - 1;
}
auto& face = *it->second;
face.set_size(size);
return face;
}
}