aboutsummaryrefslogtreecommitdiffstats
path: root/components/fonts/tests/font_context.rs
blob: 69ba4af479face8e1fca179b0ced218012325529 (plain) (blame)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

// This currently only works on FreeType platforms as it requires being able to create
// local font identifiers from paths.
#[cfg(target_os = "linux")]
mod font_context {
    use std::collections::HashMap;
    use std::ffi::OsStr;
    use std::path::PathBuf;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicI32, Ordering};
    use std::thread;

    use app_units::Au;
    use fonts::platform::font::PlatformFont;
    use fonts::{
        FallbackFontSelectionOptions, FontContext, FontDescriptor, FontFamilyDescriptor,
        FontIdentifier, FontSearchScope, FontTemplate, FontTemplates, LocalFontIdentifier,
        PlatformFontMethods, SystemFontServiceMessage, SystemFontServiceProxy,
        SystemFontServiceProxySender, fallback_font_families,
    };
    use ipc_channel::ipc::{self, IpcReceiver};
    use net_traits::ResourceThreads;
    use parking_lot::Mutex;
    use servo_arc::Arc as ServoArc;
    use style::ArcSlice;
    use style::properties::longhands::font_variant_caps::computed_value::T as FontVariantCaps;
    use style::properties::style_structs::Font as FontStyleStruct;
    use style::values::computed::font::{
        FamilyName, FontFamily, FontFamilyList, FontFamilyNameSyntax, FontSize, FontStretch,
        FontStyle, FontWeight, SingleFontFamily,
    };
    use style::values::computed::{FontLanguageOverride, XLang};
    use style::values::generics::font::LineHeight;
    use stylo_atoms::Atom;
    use webrender_api::{FontInstanceKey, FontKey, IdNamespace};
    use webrender_traits::CrossProcessCompositorApi;

    struct TestContext {
        context: FontContext,
        system_font_service: Arc<MockSystemFontService>,
        system_font_service_proxy: SystemFontServiceProxy,
    }

    impl TestContext {
        fn new() -> TestContext {
            let (system_font_service, system_font_service_proxy) = MockSystemFontService::spawn();
            let (core_sender, _) = ipc::channel().unwrap();
            let (storage_sender, _) = ipc::channel().unwrap();
            let mock_resource_threads = ResourceThreads::new(core_sender, storage_sender);
            let mock_compositor_api = CrossProcessCompositorApi::dummy();

            let proxy_clone = Arc::new(system_font_service_proxy.to_sender().to_proxy());
            Self {
                context: FontContext::new(proxy_clone, mock_compositor_api, mock_resource_threads),
                system_font_service,
                system_font_service_proxy,
            }
        }
    }

    impl Drop for TestContext {
        fn drop(&mut self) {
            self.system_font_service_proxy.exit();
        }
    }

    fn font_face_name(identifier: &FontIdentifier) -> String {
        let FontIdentifier::Local(local_identifier) = identifier else {
            unreachable!("Should never create a web font for this test.");
        };
        PathBuf::from(&*local_identifier.path)
            .file_name()
            .and_then(OsStr::to_str)
            .map(|string| string.replace(".ttf", ""))
            .expect("Could not extract font face name.")
    }

    struct MockSystemFontService {
        families: Mutex<HashMap<String, FontTemplates>>,
        find_font_count: AtomicI32,
    }

    impl MockSystemFontService {
        fn spawn() -> (Arc<MockSystemFontService>, SystemFontServiceProxy) {
            let (sender, receiver) = ipc::channel().unwrap();
            let system_font_service = Arc::new(Self::new());

            let system_font_service_clone = system_font_service.clone();
            thread::Builder::new()
                .name("MockSystemFontService".to_owned())
                .spawn(move || system_font_service_clone.run(receiver))
                .expect("Thread spawning failed");
            (
                system_font_service,
                SystemFontServiceProxySender(sender).to_proxy(),
            )
        }

        fn run(&self, receiver: IpcReceiver<SystemFontServiceMessage>) {
            loop {
                match receiver.recv().unwrap() {
                    SystemFontServiceMessage::GetFontTemplates(
                        descriptor_to_match,
                        font_family,
                        result_sender,
                    ) => {
                        self.find_font_count.fetch_add(1, Ordering::Relaxed);

                        let SingleFontFamily::FamilyName(family_name) = font_family else {
                            let _ = result_sender.send(vec![]);
                            continue;
                        };

                        let _ = result_sender.send(
                            self.families
                                .lock()
                                .get_mut(&*family_name.name)
                                .map(|family| {
                                    family.find_for_descriptor(descriptor_to_match.as_ref())
                                })
                                .unwrap()
                                .into_iter()
                                .map(|template| template.borrow().clone())
                                .collect(),
                        );
                    },
                    SystemFontServiceMessage::GetFontInstanceKey(result_sender) |
                    SystemFontServiceMessage::GetFontInstance(_, _, _, result_sender) => {
                        let _ = result_sender.send(FontInstanceKey(IdNamespace(0), 0));
                    },
                    SystemFontServiceMessage::GetFontKey(result_sender) => {
                        let _ = result_sender.send(FontKey(IdNamespace(0), 0));
                    },
                    SystemFontServiceMessage::Exit(result_sender) => {
                        let _ = result_sender.send(());
                        break;
                    },
                    SystemFontServiceMessage::Ping => {},
                }
            }
        }

        fn new() -> Self {
            let proxy = Self {
                families: Default::default(),
                find_font_count: AtomicI32::new(0),
            };

            let mut csstest_ascii = FontTemplates::default();
            proxy.add_face(&mut csstest_ascii, "csstest-ascii");

            let mut csstest_basic = FontTemplates::default();
            proxy.add_face(&mut csstest_basic, "csstest-basic-regular");

            let mut fallback = FontTemplates::default();
            proxy.add_face(&mut fallback, "csstest-basic-regular");

            {
                let mut families = proxy.families.lock();
                families.insert("CSSTest ASCII".to_owned(), csstest_ascii);
                families.insert("CSSTest Basic".to_owned(), csstest_basic);
                families.insert(
                    fallback_font_families(FallbackFontSelectionOptions::default())[0].to_owned(),
                    fallback,
                );
            }

            proxy
        }

        fn add_face(&self, family: &mut FontTemplates, name: &str) {
            let mut path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "tests", "support", "CSSTest"]
                .iter()
                .collect();
            path.push(format!("{}.ttf", name));

            let local_font_identifier = LocalFontIdentifier {
                path: path.to_str().expect("Could not load test font").into(),
                variation_index: 0,
            };
            let handle =
                PlatformFont::new_from_local_font_identifier(local_font_identifier.clone(), None)
                    .expect("Could not load test font");

            family.add_template(FontTemplate::new(
                FontIdentifier::Local(local_font_identifier),
                handle.descriptor(),
                None,
            ));
        }
    }

    fn style() -> FontStyleStruct {
        let mut style = FontStyleStruct {
            font_family: FontFamily::serif(),
            font_style: FontStyle::NORMAL,
            font_variant_caps: FontVariantCaps::Normal,
            font_weight: FontWeight::normal(),
            font_size: FontSize::medium(),
            font_stretch: FontStretch::hundred(),
            hash: 0,
            font_language_override: FontLanguageOverride::normal(),
            line_height: LineHeight::Normal,
            _x_lang: XLang::get_initial_value(),
        };
        style.compute_font_hash();
        style
    }

    fn font_family(names: Vec<&str>) -> FontFamily {
        let names = names.into_iter().map(|name| {
            SingleFontFamily::FamilyName(FamilyName {
                name: Atom::from(name),
                syntax: FontFamilyNameSyntax::Quoted,
            })
        });

        FontFamily {
            families: FontFamilyList {
                list: ArcSlice::from_iter(names),
            },
            is_system_font: false,
            is_initial: false,
        }
    }

    #[test]
    fn test_font_group_is_cached_by_style() {
        let context = TestContext::new();

        let style1 = style();

        let mut style2 = style();
        style2.set_font_style(FontStyle::ITALIC);

        assert!(
            std::ptr::eq(
                &*context
                    .context
                    .font_group(ServoArc::new(style1.clone()))
                    .read(),
                &*context
                    .context
                    .font_group(ServoArc::new(style1.clone()))
                    .read()
            ),
            "the same font group should be returned for two styles with the same hash"
        );

        assert!(
            !std::ptr::eq(
                &*context
                    .context
                    .font_group(ServoArc::new(style1.clone()))
                    .read(),
                &*context
                    .context
                    .font_group(ServoArc::new(style2.clone()))
                    .read()
            ),
            "different font groups should be returned for two styles with different hashes"
        )
    }

    #[test]
    fn test_font_group_find_by_codepoint() {
        let mut context = TestContext::new();

        let mut style = style();
        style.set_font_family(font_family(vec!["CSSTest ASCII", "CSSTest Basic"]));

        let group = context.context.font_group(ServoArc::new(style));

        let font = group
            .write()
            .find_by_codepoint(&mut context.context, 'a', None, None)
            .unwrap();
        assert_eq!(&font_face_name(&font.identifier()), "csstest-ascii");
        assert_eq!(
            context
                .system_font_service
                .find_font_count
                .fetch_add(0, Ordering::Relaxed),
            1,
            "only the first font in the list should have been loaded"
        );

        let font = group
            .write()
            .find_by_codepoint(&mut context.context, 'a', None, None)
            .unwrap();
        assert_eq!(&font_face_name(&font.identifier()), "csstest-ascii");
        assert_eq!(
            context
                .system_font_service
                .find_font_count
                .fetch_add(0, Ordering::Relaxed),
            1,
            "we shouldn't load the same font a second time"
        );

        let font = group
            .write()
            .find_by_codepoint(&mut context.context, 'á', None, None)
            .unwrap();
        assert_eq!(&font_face_name(&font.identifier()), "csstest-basic-regular");
        assert_eq!(
            context
                .system_font_service
                .find_font_count
                .fetch_add(0, Ordering::Relaxed),
            2,
            "both fonts should now have been loaded"
        );
    }

    #[test]
    fn test_font_fallback() {
        let mut context = TestContext::new();

        let mut style = style();
        style.set_font_family(font_family(vec!["CSSTest ASCII"]));

        let group = context.context.font_group(ServoArc::new(style));

        let font = group
            .write()
            .find_by_codepoint(&mut context.context, 'a', None, None)
            .unwrap();
        assert_eq!(
            &font_face_name(&font.identifier()),
            "csstest-ascii",
            "a family in the group should be used if there is a matching glyph"
        );

        let font = group
            .write()
            .find_by_codepoint(&mut context.context, 'á', None, None)
            .unwrap();
        assert_eq!(
            &font_face_name(&font.identifier()),
            "csstest-basic-regular",
            "a fallback font should be used if there is no matching glyph in the group"
        );
    }

    #[test]
    fn test_font_template_is_cached() {
        let context = TestContext::new();

        let mut font_descriptor = FontDescriptor {
            weight: FontWeight::normal(),
            stretch: FontStretch::hundred(),
            style: FontStyle::normal(),
            variant: FontVariantCaps::Normal,
            pt_size: Au(10),
        };

        let family = SingleFontFamily::FamilyName(FamilyName {
            name: "CSSTest Basic".into(),
            syntax: FontFamilyNameSyntax::Quoted,
        });
        let family_descriptor = FontFamilyDescriptor::new(family, FontSearchScope::Any);

        let font_template = context
            .context
            .matching_templates(&font_descriptor, &family_descriptor)[0]
            .clone();

        let _ = context
            .context
            .matching_templates(&font_descriptor, &family_descriptor);

        assert_eq!(
            context
                .system_font_service
                .find_font_count
                .fetch_add(0, Ordering::Relaxed),
            1,
            "we should only have requested matching templates from the font service once"
        );

        let font1 = context
            .context
            .font(font_template.clone(), &font_descriptor)
            .unwrap();

        font_descriptor.pt_size = Au(20);
        let font2 = context
            .context
            .font(font_template.clone(), &font_descriptor)
            .unwrap();

        assert_ne!(
            font1.descriptor.pt_size, font2.descriptor.pt_size,
            "the same font should not have been returned"
        );

        assert_eq!(
            context
                .system_font_service
                .find_font_count
                .fetch_add(0, Ordering::Relaxed),
            1,
            "we should only have fetched the template data from the cache thread once"
        );
    }
}