summaryrefslogtreecommitdiffstats
path: root/font/src/ft/fc/mod.rs
blob: ae8fb808767205810b74c5e54a7aa5727b9d31c8 (plain)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
use std::fmt;
use std::ptr;

use foreign_types::{ForeignType, ForeignTypeRef};

use fontconfig::fontconfig as ffi;

use self::ffi::FcResultNoMatch;
use self::ffi::{FcFontList, FcFontMatch, FcFontSort};
use self::ffi::{FcMatchFont, FcMatchPattern, FcMatchScan};
use self::ffi::{FcSetApplication, FcSetSystem};
use self::ffi::{FC_SLANT_ITALIC, FC_SLANT_OBLIQUE, FC_SLANT_ROMAN};
use self::ffi::{FC_WEIGHT_BLACK, FC_WEIGHT_BOLD, FC_WEIGHT_EXTRABLACK, FC_WEIGHT_EXTRABOLD};
use self::ffi::{FC_WEIGHT_BOOK, FC_WEIGHT_MEDIUM, FC_WEIGHT_REGULAR, FC_WEIGHT_SEMIBOLD};
use self::ffi::{FC_WEIGHT_EXTRALIGHT, FC_WEIGHT_LIGHT, FC_WEIGHT_THIN};

pub mod config;
pub use self::config::{Config, ConfigRef};

pub mod font_set;
pub use self::font_set::{FontSet, FontSetRef};

pub mod object_set;
pub use self::object_set::{ObjectSet, ObjectSetRef};

pub mod char_set;
pub use self::char_set::{CharSet, CharSetRef};

pub mod pattern;
pub use self::pattern::{Pattern, PatternRef};

/// Find the font closest matching the provided pattern.
///
/// The returned pattern is the result of Pattern::render_prepare.
pub fn font_match(config: &ConfigRef, pattern: &mut PatternRef) -> Option<Pattern> {
    pattern.config_substitute(config, MatchKind::Pattern);
    pattern.default_substitute();

    unsafe {
        // What is this result actually used for? Seems redundant with
        // return type.
        let mut result = FcResultNoMatch;
        let ptr = FcFontMatch(config.as_ptr(), pattern.as_ptr(), &mut result);

        if ptr.is_null() {
            None
        } else {
            Some(Pattern::from_ptr(ptr))
        }
    }
}

/// list fonts by closeness to the pattern
pub fn font_sort(config: &ConfigRef, pattern: &mut PatternRef) -> Option<FontSet> {
    pattern.config_substitute(config, MatchKind::Pattern);
    pattern.default_substitute();

    unsafe {
        // What is this result actually used for? Seems redundant with
        // return type.
        let mut result = FcResultNoMatch;

        let mut charsets: *mut _ = ptr::null_mut();

        let ptr = FcFontSort(
            config.as_ptr(),
            pattern.as_ptr(),
            0, // false
            &mut charsets,
            &mut result,
        );

        if ptr.is_null() {
            None
        } else {
            Some(FontSet::from_ptr(ptr))
        }
    }
}

/// List fonts matching pattern
pub fn font_list(
    config: &ConfigRef,
    pattern: &mut PatternRef,
    objects: &ObjectSetRef,
) -> Option<FontSet> {
    pattern.config_substitute(config, MatchKind::Pattern);
    pattern.default_substitute();

    unsafe {
        let ptr = FcFontList(config.as_ptr(), pattern.as_ptr(), objects.as_ptr());

        if ptr.is_null() {
            None
        } else {
            Some(FontSet::from_ptr(ptr))
        }
    }
}

/// Available font sets
#[derive(Debug, Copy, Clone)]
pub enum SetName {
    System = FcSetSystem as isize,
    Application = FcSetApplication as isize,
}

/// When matching, how to match
#[derive(Debug, Copy, Clone)]
pub enum MatchKind {
    Font = FcMatchFont as isize,
    Pattern = FcMatchPattern as isize,
    Scan = FcMatchScan as isize,
}

#[derive(Debug, Copy, Clone)]
pub enum Slant {
    Italic = FC_SLANT_ITALIC as isize,
    Oblique = FC_SLANT_OBLIQUE as isize,
    Roman = FC_SLANT_ROMAN as isize,
}

#[derive(Debug, Copy, Clone)]
pub enum Weight {
    Thin = FC_WEIGHT_THIN as isize,
    Extralight = FC_WEIGHT_EXTRALIGHT as isize,
    Light = FC_WEIGHT_LIGHT as isize,
    Book = FC_WEIGHT_BOOK as isize,
    Regular = FC_WEIGHT_REGULAR as isize,
    Medium = FC_WEIGHT_MEDIUM as isize,
    Semibold = FC_WEIGHT_SEMIBOLD as isize,
    Bold = FC_WEIGHT_BOLD as isize,
    Extrabold = FC_WEIGHT_EXTRABOLD as isize,
    Black = FC_WEIGHT_BLACK as isize,
    Extrablack = FC_WEIGHT_EXTRABLACK as isize,
}

#[derive(Debug, Copy, Clone)]
pub enum Width {
    Ultracondensed,
    Extracondensed,
    Condensed,
    Semicondensed,
    Normal,
    Semiexpanded,
    Expanded,
    Extraexpanded,
    Ultraexpanded,
    Other(i32),
}

impl Width {
    fn to_isize(self) -> isize {
        use self::Width::*;
        match self {
            Ultracondensed => 50,
            Extracondensed => 63,
            Condensed => 75,
            Semicondensed => 87,
            Normal => 100,
            Semiexpanded => 113,
            Expanded => 125,
            Extraexpanded => 150,
            Ultraexpanded => 200,
            Other(value) => value as isize,
        }
    }
}

impl From<isize> for Width {
    fn from(value: isize) -> Self {
        match value {
            50 => Width::Ultracondensed,
            63 => Width::Extracondensed,
            75 => Width::Condensed,
            87 => Width::Semicondensed,
            100 => Width::Normal,
            113 => Width::Semiexpanded,
            125 => Width::Expanded,
            150 => Width::Extraexpanded,
            200 => Width::Ultraexpanded,
            _ => Width::Other(value as _),
        }
    }
}

/// Subpixel geometry
pub enum Rgba {
    Unknown,
    Rgb,
    Bgr,
    Vrgb,
    Vbgr,
    None,
}

impl Rgba {
    fn to_isize(&self) -> isize {
        match *self {
            Rgba::Unknown => 0,
            Rgba::Rgb => 1,
            Rgba::Bgr => 2,
            Rgba::Vrgb => 3,
            Rgba::Vbgr => 4,
            Rgba::None => 5,
        }
    }
}

impl fmt::Display for Rgba {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::Rgba::*;
        f.write_str(match *self {
            Unknown => "unknown",
            Rgb => "rgb",
            Bgr => "bgr",
            Vrgb => "vrgb",
            Vbgr => "vbgr",
            None => "none",
        })
    }
}

impl From<isize> for Rgba {
    fn from(val: isize) -> Rgba {
        match val {
            1 => Rgba::Rgb,
            2 => Rgba::Bgr,
            3 => Rgba::Vrgb,
            4 => Rgba::Vbgr,
            5 => Rgba::None,
            _ => Rgba::Unknown,
        }
    }
}

/// Hinting Style
#[derive(Debug, Copy, Clone)]
pub enum HintStyle {
    None,
    Slight,
    Medium,
    Full,
}

impl fmt::Display for HintStyle {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(match *self {
            HintStyle::None => "none",
            HintStyle::Slight => "slight",
            HintStyle::Medium => "medium",
            HintStyle::Full => "full",