typeset 0.1.0
縦書き・横書きの日本語組版ライブラリ(JLReq 水準、ラスタ / PDF / SVG)
読み取り中…
検索中…
一致する文字列を見つけられません
font_set.hpp
[詳解]
1#ifndef TYPESET_FONT_FONT_SET_HPP
2#define TYPESET_FONT_FONT_SET_HPP
3
4#include <map>
5#include <memory>
6#include <optional>
7#include <string>
8#include <vector>
9
10#include <glyphware/Face.h>
11
12#include "typeset/geom.hpp"
13
14struct hb_font_t;
15
16namespace typeset { struct FontSpec; }
17
31namespace typeset::font {
32
40 std::string key;
41 std::string path;
42 int faceIndex = 0;
43 std::vector<std::string> family;
44 int weight = 0;
45 std::optional<bool> italic;
46 std::vector<std::string> languages;
47 std::vector<glyphware::CodepointRange> ranges;
48};
49
50class FontSet {
51public:
54
55 FontSet(const FontSet&) = delete;
56 FontSet& operator=(const FontSet&) = delete;
57
62 std::shared_ptr<glyphware::Face> loadFile(const std::string& path,
63 const std::string& key = std::string(),
64 int faceIndex = 0);
65
67 std::shared_ptr<glyphware::Face> loadMemory(const std::string& key,
68 const void* data, size_t size,
69 int faceIndex = 0);
70
77
79 bool has(const std::string& key) const;
81 bool isLoaded(const std::string& key) const;
83 std::vector<std::string> keys() const;
84
86 std::shared_ptr<glyphware::Face> find(const std::string& keyOrFamily);
87
90 std::shared_ptr<glyphware::Face> select(const std::string& keyOrFamily, int weight, bool italic,
91 const std::map<std::string, float>& variations = {});
92
97 std::shared_ptr<glyphware::Face> instance(const std::shared_ptr<glyphware::Face>& base,
98 const std::vector<glyphware::VarCoord>& coords);
99
105 std::shared_ptr<glyphware::Face> resolve(const FontSpec& spec, char32_t cp,
106 const std::string& language = std::string(),
107 int colorPreference = 0);
108
110 std::shared_ptr<glyphware::Face> primary(const FontSpec& spec);
111
116 void setLanguageFonts(const std::string& language, std::vector<std::string> families);
118 std::vector<std::string> languageFonts(const std::string& language) const;
119
123 hb_font_t* hbFont(const glyphware::Face& face);
124
126 size_t size() const { return entries_.size(); }
127
128private:
129 struct Entry {
130 FontDeclaration decl;
131 std::shared_ptr<glyphware::Face> face;
132 bool failed = false;
133 std::vector<std::string> names;
134 int weight = 400;
135 bool italic = false;
136 bool matches(const std::string& name) const;
137 bool languageMatches(const std::string& language) const;
138 };
139
140 Entry* entryFor(const std::string& key);
141 void adopt(Entry& e, std::shared_ptr<glyphware::Face> face);
142 bool ensureLoaded(Entry& e);
143 bool covers(Entry& e, char32_t cp);
145 Entry* best(const std::string& name, int weight, bool italic);
146 std::shared_ptr<glyphware::Face> faceOf(Entry* e);
147
149 std::shared_ptr<glyphware::Face> withVariations(const std::shared_ptr<glyphware::Face>& base,
150 int weight, bool italic,
151 const std::map<std::string, float>& variations);
152
153 std::vector<std::unique_ptr<Entry>> entries_;
154 std::map<std::string, std::vector<std::string>> languageFonts_;
155 std::map<const glyphware::Face*, hb_font_t*> hbFonts_;
157 std::map<std::pair<const glyphware::Face*, std::string>, std::shared_ptr<glyphware::Face>> instances_;
158};
159
161inline uint32_t makeTag(const std::string& s) {
162 uint32_t t = 0;
163 for (size_t i = 0; i < 4; ++i) t = (t << 8) | static_cast<uint8_t>(i < s.size() ? s[i] : ' ');
164 return t;
165}
166
168int effectiveWeight(const glyphware::Face& face);
169
171float unitsPerEm(const glyphware::Face& face);
172
174void faceAscentDescent(const glyphware::Face& face, Pt size, Pt& ascent, Pt& descent);
175
186
188DecorationMetrics decorationMetrics(FontSet& fonts, const glyphware::Face& face, Pt size);
189
190} // namespace typeset::font
191
192#endif // TYPESET_FONT_FONT_SET_HPP
std::shared_ptr< glyphware::Face > find(const std::string &keyOrFamily)
登録済み face(キー、または family 名で。通常ウェイト・非斜体を優先。無ければ nullptr)。必要なら開く
void setLanguageFonts(const std::string &language, std::vector< std::string > families)
言語 → 先に試す family の列。"zh-Hans" のように地域・用字系まで書いた言語は、その完全一致が無ければ 主言語("zh")の表を使う。families が空なら削除
FontSet & operator=(const FontSet &)=delete
FontSet(const FontSet &)=delete
hb_font_t * hbFont(const glyphware::Face &face)
シェイピング用 hb_font(フォントユニットスケール)。FontSet が所有する
std::vector< std::string > languageFonts(const std::string &language) const
language に対応する family 列(置換表+宣言の languages にその言語を持つキー)。無ければ空
std::vector< std::string > keys() const
登録したキー(登録順)
std::shared_ptr< glyphware::Face > loadMemory(const std::string &key, const void *data, size_t size, int faceIndex=0)
メモリから開く(バイト列はコピーして保持する)
std::shared_ptr< glyphware::Face > select(const std::string &keyOrFamily, int weight, bool italic, const std::map< std::string, float > &variations={})
キー/family 名と weight / italic に最も近い face。必要なら開く。 バリアブルフォントなら wght 軸を weight に合わせたインスタンス(variations に他の...
std::shared_ptr< glyphware::Face > primary(const FontSpec &spec)
FontSpec の第一候補(行のメトリクス基準に使う。言語の置換は見ない)
std::shared_ptr< glyphware::Face > resolve(const FontSpec &spec, char32_t cp, const std::string &language=std::string(), int colorPreference=0)
FontSpec の family 列(language の置換表・宣言の languages が先)を順に見て、cp を持つ最初の face を返す。 family ごとに weight / ital...
std::shared_ptr< glyphware::Face > instance(const std::shared_ptr< glyphware::Face > &base, const std::vector< glyphware::VarCoord > &coords)
バリアブルフォントのインスタンス(軸の値を固定した別の Face)。同じ base と座標なら同じ Face を返す。 軸の無い face、座標が空ならそのまま base
bool has(const std::string &key) const
登録済みか(開いているかどうかは問わない)
std::shared_ptr< glyphware::Face > loadFile(const std::string &path, const std::string &key=std::string(), int faceIndex=0)
ファイルから開く。key を省略するとパスがキーになる。family / weight / italic は name・OS/2 から
size_t size() const
登録した数(宣言だけのものも含む)
Definition font_set.hpp:126
bool declare(FontDeclaration decl)
開かずに宣言する。同じキーがあれば置き換える。ファイルは初回使用時に開く(無ければそのとき失敗し、 以後は無いものとして扱う)
bool isLoaded(const std::string &key) const
開いているか(declare しただけなら false。開くのに失敗したものも false)
font — フォント層(glyphware の薄い包み)
Definition font_set.hpp:31
DecorationMetrics decorationMetrics(FontSet &fonts, const glyphware::Face &face, Pt size)
post / OS/2 から取る(hb_ot_metrics)。無ければ下線 0.1em / 太さ 0.05em、打消し線 -0.3em
void faceAscentDescent(const glyphware::Face &face, Pt size, Pt &ascent, Pt &descent)
face の ascender / descender(正・正、pt)を size で返す
uint32_t makeTag(const std::string &s)
OpenType タグ("wght")を glyphware / HarfBuzz の 32bit タグに
Definition font_set.hpp:161
float unitsPerEm(const glyphware::Face &face)
face の unitsPerEm(取れなければ 1000)
int effectiveWeight(const glyphware::Face &face)
face の実効ウェイト。バリアブルフォントのインスタンスなら wght 軸の値、無ければ OS/2 の値
geom — 単位と幾何
float Pt
Definition geom.hpp:17
フォントの論理指定
Definition style.hpp:24
下線・打消し線のメトリクス(pt、size で換算)。位置はベースラインからの距離で下が正 (横組みの block 軸と同じ向き。打消し線は通常負)
Definition font_set.hpp:180
フォントの宣言(開かずに登録するためのメタデータ)
Definition font_set.hpp:39
std::vector< std::string > family
別名(同じ family の別ウェイトは同じ名前を書く)
Definition font_set.hpp:43
std::string key
一意なキー(TextStyle の family に書く名前)
Definition font_set.hpp:40
std::optional< bool > italic
nullopt でフォントから
Definition font_set.hpp:45
std::string path
ファイル(初回使用時に開く)
Definition font_set.hpp:41
int weight
100〜900。0 でフォントから
Definition font_set.hpp:44
std::vector< glyphware::CodepointRange > ranges
カバレッジ(空なら開いて cmap を見る)
Definition font_set.hpp:47
std::vector< std::string > languages
BCP47。この言語のテキストで先に試される
Definition font_set.hpp:46