guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
typescale.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/theme/typescale.hpp"
24
25#include <stdexcept>
26
27namespace guillaume::theme
28{
29 namespace
30 {
31 std::size_t toIndex(TypeRole role)
32 {
33 const auto index = static_cast<std::size_t>(role);
34 if (index >= static_cast<std::size_t>(TypeRole::Count)) {
35 throw std::out_of_range("Invalid typescale role");
36 }
37 return index;
38 }
39
40 TypeToken token(FontWeight weight, float size, float lineHeight,
41 float letterSpacing)
42 {
43 TypeToken result;
44 result.fontWeight = weight;
45 result.fontSize = size;
46 result.lineHeight = lineHeight;
47 result.letterSpacing = letterSpacing;
48 return result;
49 }
50 } // namespace
51
52 const std::string &fontPath(FontWeight weight)
53 {
54 static const std::string regular("fonts/Roboto-Regular.ttf");
55 static const std::string medium("fonts/Roboto-Medium.ttf");
56 return weight == FontWeight::Medium ? medium : regular;
57 }
58
60 : _tokens()
61 {
62 constexpr auto R = FontWeight::Regular;
63 constexpr auto M = FontWeight::Medium;
64
65 // M3 baseline, as published in react-native-paper v5 tokens.
66 set(TypeRole::DisplayLarge, token(R, 57.0f, 64.0f, 0.0f));
67 set(TypeRole::DisplayMedium, token(R, 45.0f, 52.0f, 0.0f));
68 set(TypeRole::DisplaySmall, token(R, 36.0f, 44.0f, 0.0f));
69 set(TypeRole::HeadlineLarge, token(R, 32.0f, 40.0f, 0.0f));
70 set(TypeRole::HeadlineMedium, token(R, 28.0f, 36.0f, 0.0f));
71 set(TypeRole::HeadlineSmall, token(R, 24.0f, 32.0f, 0.0f));
72 set(TypeRole::TitleLarge, token(R, 22.0f, 28.0f, 0.0f));
73 set(TypeRole::TitleMedium, token(M, 16.0f, 24.0f, 0.15f));
74 set(TypeRole::TitleSmall, token(M, 14.0f, 20.0f, 0.1f));
75 set(TypeRole::LabelLarge, token(M, 14.0f, 20.0f, 0.1f));
76 set(TypeRole::LabelMedium, token(M, 12.0f, 16.0f, 0.5f));
77 set(TypeRole::LabelSmall, token(M, 11.0f, 16.0f, 0.5f));
78 set(TypeRole::BodyLarge, token(R, 16.0f, 24.0f, 0.15f));
79 set(TypeRole::BodyMedium, token(R, 14.0f, 20.0f, 0.25f));
80 set(TypeRole::BodySmall, token(R, 12.0f, 16.0f, 0.4f));
81 set(TypeRole::Default, token(R, 14.0f, 20.0f, 0.0f));
82 }
83
85 {
86 return _tokens[toIndex(role)];
87 }
88
89 void Typescale::set(TypeRole role, const TypeToken &token)
90 {
91 if (token.fontSize <= 0.0f || token.lineHeight <= 0.0f) {
92 throw std::invalid_argument(
93 "Typescale font size and line height must be positive");
94 }
95 _tokens[toIndex(role)] = token;
96 }
97
98} // namespace guillaume::theme
void set(TypeRole role, const TypeToken &token)
Replace the text style of a role.
Definition typescale.cpp:89
const TypeToken & get(TypeRole role) const
Get the text style of a role.
Definition typescale.cpp:84
Typescale(void)
Create the scale with the Material 3 baseline styles listed in TypeRole.
Definition typescale.cpp:59
Design tokens: colors, type scale, shapes, elevation, motion and spacing. Read them through guillaume...
const std::string & fontPath(FontWeight weight)
Get the font file shipped with the library for a weight.
Definition typescale.cpp:52
TypeRole
The named text styles of Material Design 3: what a piece of text is, not how big it is.
Definition typescale.hpp:47
@ BodyLarge
16 / 24, regular.
Definition typescale.hpp:60
@ LabelSmall
11 / 16, medium weight.
Definition typescale.hpp:59
@ DisplayLarge
57 / 64, regular.
Definition typescale.hpp:48
@ LabelMedium
12 / 16, medium weight.
Definition typescale.hpp:58
@ HeadlineLarge
32 / 40, regular.
Definition typescale.hpp:51
@ TitleSmall
14 / 20, medium weight.
Definition typescale.hpp:56
@ BodySmall
12 / 16, regular.
Definition typescale.hpp:62
@ TitleLarge
22 / 28, regular.
Definition typescale.hpp:54
@ DisplayMedium
45 / 52, regular.
Definition typescale.hpp:49
@ LabelLarge
14 / 20, medium weight.
Definition typescale.hpp:57
@ TitleMedium
16 / 24, medium weight.
Definition typescale.hpp:55
@ HeadlineMedium
28 / 36, regular.
Definition typescale.hpp:52
@ DisplaySmall
36 / 44, regular.
Definition typescale.hpp:50
@ Default
14 / 20, regular: text with no specific role.
Definition typescale.hpp:63
@ HeadlineSmall
24 / 32, regular.
Definition typescale.hpp:53
@ BodyMedium
14 / 20, regular.
Definition typescale.hpp:61
@ Count
Number of roles; not a role.
Definition typescale.hpp:64
FontWeight
Font weights available to the typescale. The value is the usual numeric weight (400 regular,...
Definition typescale.hpp:71
One text style: everything needed to measure and draw a piece of text.
Definition typescale.hpp:83
FontWeight fontWeight
Font weight.
Definition typescale.hpp:85
float fontSize
Font size, in dp.
Definition typescale.hpp:86
float lineHeight
Distance between two baselines, in dp.
Definition typescale.hpp:87