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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// SPDX-License-Identifier: LGPL-3.0-or-later OR MPL-2.0
// This file is a part of `piet-cosmic-text`.
//
// `piet-cosmic-text` is free software: you can redistribute it and/or modify it under the
// terms of either:
//
// * GNU Lesser General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
// * Mozilla Public License as published by the Mozilla Foundation, version 2.
//
// `piet-cosmic-text` is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU Lesser General Public License or the Mozilla Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License and the Mozilla
// Public License along with `piet-cosmic-text`. If not, see <https://www.gnu.org/licenses/>.
//! An implementation of [`piet`]'s text API using [`cosmic-text`].
//!
//! This library implements [`piet`]'s [`Text`] API using primitives from [`cosmic-text`].
//! The intention is for this library to act as a stepping stone to be able to use drawing
//! frameworks that do not natively support text rendering (like OpenGL) by using the
//! [`cosmic-text`] library to render text to a texture, and then using that texture
//! in the drawing framework.
//!
//! This library provides a [`Text`](crate::Text), a [`TextLayoutBuilder`] and a
//! [`TextLayout`]. All of these are intended to be used in the same way as the
//! corresponding types in [`piet`]. However, [`TextLayout`] has a `buffer` method that
//! can be used to get the underlying text.
//!
//! The structures provided by this crate are completely renderer-agnostic. The [`TextLayout`]
//! structure exposes the underlying [`Buffer`] structure, which can be used to render the text.
//!
//! # Embedded Fonts
//!
//! In order to make it easier to use this crate in a cross platform setting, it embeds a handful
//! of fonts into the binary. These fonts are used as a fallback when the system fonts are not
//! available. For instance, on web targets, there are no fonts available by default, so these
//! fonts are used instead. In addition, if attributes fail to match any of the fonts on the
//! system, these fonts are used as a fallback.
//!
//! Without compression, these fonts add around 1.5 megabytes to the final binary. With the
//! `DEFLATE` compression algorithm, which is enabled by default, this is reduced to around
//! 1.1 megabytes. In practice it's actually around 700 kilobytes, as the remaining data is used
//! by the compressions algorithm. [`yazi`] is used to compress the font data; as it is also used
//! by [`swash`], which is often used with [`cosmic-text`], the actual amount of data saved should
//! be closer to the theoretical maximum.
//!
//! To disable font compression, disable the default `compress-fonts` feature. To disable embedding
//! fonts altogether, disable the default `embed-fonts` feature.
//!
//! # Font Initialization
//!
//! The initialization of the [`FontSystem`] can take some time, especially on slower systems with
//! many thousand fonts. In order to prevent font loading from blocking the main windowing thread,
//! [`Text`] has an option to use a background thread to load the fonts. Enabling the `rayon`
//! feature (not enabled by default) will export font loading to the [`rayon`] thread pool.
//! Without this feature, font loading will be done on the current thread.
//!
//! As web targets do not support threads, enabling the `rayon` feature on web targets will lead
//! to compilation errors.
//!
//! Sufficiently complex programs usually already have a system set up to handle blocking tasks.
//! For `async` programs, this is usually [`tokio`]'s [`spawn_blocking`] function or the
//! [`blocking`] thread pool. In these cases you can implement the [`ExportWork`] trait and then
//! pass it to [`Text::with_thread`]. This will allow you to use the same thread pool for both
//! font loading and other blocking tasks.
//!
//! The `is_loaded` method of [`Text`](crate::Text) can be used to check if the font system is
//! fully loaded.
//!
//! # Limitations
//!
//! The text does not support variable font sizes. Attempting to use these will result in emitting
//! an error to the logs, and the text size not actually being changed.
//!
//! [`piet`]: https://docs.rs/piet
//! [`cosmic-text`]: https://docs.rs/cosmic-text
//! [`Buffer`]: https://docs.rs/cosmic-text/latest/cosmic_text/struct.Buffer.html
//! [`Text`]: https://docs.rs/piet/latest/piet/trait.Text.html
//! [`FontSystem`]: https://docs.rs/cosmic-text/latest/cosmic_text/fontdb/struct.FontSystem.html
//! [`yazi`]: https://docs.rs/yazi
//! [`swash`]: https://docs.rs/swash
//! [`rayon`]: https://docs.rs/rayon
//! [`tokio`]: https://docs.rs/tokio
//! [`spawn_blocking`]: https://docs.rs/tokio/latest/tokio/task/fn.spawn_blocking.html
//! [`blocking`]: https://docs.rs/blocking
// Public dependencies.
pub use cosmic_text;
pub use piet;
use cosmic_text as ct;
use fmt;
pub use ;
pub use ;
pub use Metadata;
pub use Text;
pub use TextLayout;
pub use TextLayoutBuilder;
pub use Rayon;
const STANDARD_DPI: f64 = 96.0;
const POINTS_PER_INCH: f64 = 72.0;
;
/// The error type for this library.
pub