Debian Codegraph @

subprojects/fontconfig/fc-fontations/charset.rs

raw
1 /*
2 * fontconfig/fc-fontations/charset.rs
3 *
4 * Copyright 2025 Google LLC.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25 use font_types::GlyphId;
26
27 use skrifa::outline::DrawSettings;
28 use skrifa::prelude::{LocationRef, Size};
29 use skrifa::{outline::pen::OutlinePen, FontRef, MetadataProvider};
30
31 use crate::pattern_bindings::fc_wrapper::FcCharSetWrapper;
32
33 struct HasAnyContourPen {
34 has_ink: bool,
35 }
36
37 impl HasAnyContourPen {
38 pub fn new() -> Self {
39 Self { has_ink: false }
40 }
41 }
42
43 impl OutlinePen for HasAnyContourPen {
44 fn move_to(&mut self, _x: f32, _y: f32) {}
45 fn line_to(&mut self, _x: f32, _y: f32) {}
46 fn quad_to(&mut self, _cx0: f32, _cy0: f32, _x: f32, _y: f32) {}
47 fn curve_to(&mut self, _cx0: f32, _cy0: f32, _cx1: f32, _cy1: f32, _x: f32, _y: f32) {}
48 fn close(&mut self) {
49 self.has_ink = true;
50 }
51 }
52
53 const CONTROL_CHAR_MAX: u32 = 0x1F;
54
55 // For control characters, check for whether they have ink/any-contour since
56 // these might be mapped to space. Simulates fcfreetype.c behavior.
57 // Specifically, FreeType checks for face->glyph->outline.n_contours - so,
58 // number of contours, not for actually pixel coverage / contour area.
59 fn control_char_without_ink(font: &FontRef, char: u32, glyph: GlyphId) -> bool {
60 let mut pen = HasAnyContourPen::new();
61 if char <= CONTROL_CHAR_MAX {
62 font.outline_glyphs().get(glyph).and_then(|outline| {
63 outline
64 .draw(
65 DrawSettings::unhinted(Size::new(12.0), LocationRef::default()),
66 &mut pen,
67 )
68 .ok()
69 });
70 if !pen.has_ink {
71 return true;
72 }
73 }
74 false
75 }
76
77 pub fn make_charset(font: &FontRef) -> Option<FcCharSetWrapper> {
78 let mut charset = FcCharSetWrapper::new()?;
79 let mappings = font.charmap().mappings();
80 for mapping in mappings {
81 if control_char_without_ink(font, mapping.0, mapping.1) {
82 continue;
83 }
84
85 if charset.add_char(mapping.0).is_err() {
86 return None;
87 }
88 }
89 Some(charset)
90 }