-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfont.hpp
129 lines (114 loc) · 3.59 KB
/
font.hpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once
//=====================================================================//
/*! @file
@brief フォント定義
@author 平松邦仁 ([email protected])
@copyright Copyright (C) 2019, 2022 Kunihito Hiramatsu @n
Released under the MIT license @n
https://github.com/hirakuni45/RX/blob/master/LICENSE
*/
//=====================================================================//
#include <cstdint>
#include "graphics/afont.hpp"
#include "graphics/kfont.hpp"
namespace graphics {
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
/*!
@brief フォント定義
@param[in] AFONT ASCII フォント
@param[in] KFONT KANJI フォント
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
template <class AFONT, class KFONT>
class font {
public:
static constexpr int16_t height = KFONT::height < AFONT::height
? AFONT::height : KFONT::height;
typedef AFONT a_type;
typedef KFONT k_type;
private:
AFONT& afont_;
KFONT& kfont_;
public:
//-----------------------------------------------------------------//
/*!
@brief コンストラクター
*/
//-----------------------------------------------------------------//
font(AFONT& afont, KFONT& kfont) noexcept : afont_(afont), kfont_(kfont) { }
//-----------------------------------------------------------------//
/*!
@brief アスキーフォントの参照を返す
@return アスキーフォントの参照
*/
//-----------------------------------------------------------------//
AFONT& at_afont() noexcept { return afont_; }
//-----------------------------------------------------------------//
/*!
@brief 漢字フォントの参照を返す
@return 漢字フォントの参照
*/
//-----------------------------------------------------------------//
KFONT& at_kfont() noexcept { return kfont_; }
//-----------------------------------------------------------------//
/*!
@brief フォント幅取得 (UTF-8)
@param[in] cha 文字コード
@param[in] prop プロポーショナルの場合「true」
@param[in] back 背景を描画する場合「true」
@return 文字の幅 (X)
*/
//-----------------------------------------------------------------//
int16_t get_font_size(char cha, bool prop = false, bool back = false) noexcept
{
int16_t w = 0;
if(static_cast<uint8_t>(cha) < 0x80) {
uint8_t code = static_cast<uint8_t>(cha);
if(prop) {
w = AFONT::get_kern(code);
w += AFONT::get_width(code);
} else {
w = AFONT::width;
}
} else {
if(kfont_.injection_utf8(static_cast<uint8_t>(cha))) {
auto code = kfont_.get_utf16();
if(code >= 0x80) {
w = KFONT::width;
}
}
}
return w;
}
//-----------------------------------------------------------------//
/*!
@brief テキストの描画サイズを得る
@param[in] text テキスト(UTF-8)
@param[in] prop プロポーショナルの場合「true」
@return 描画サイズ
*/
//-----------------------------------------------------------------//
vtx::spos get_text_size(const char* text, bool prop = false) noexcept
{
vtx::spos sz(0);
if(text == nullptr) return sz;
char cha;
int16_t x = 0;
while((cha = *text++) != 0) {
if(cha == '\n') {
if(sz.x < x) sz.x = x;
x = 0;
sz.y += height;
} else {
// 画面外を指定してサイズだけ取得
x += get_font_size(cha, prop);
}
}
if(sz.x < x) sz.x = x;
if(sz.y == 0) sz.y = height;
else if(x > 0) sz.y += height;
return sz;
}
};
typedef font<afont_null, kfont_null> font_null;
}