Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5
//! Access to font metrics from the style system.
6
7
#![deny(missing_docs)]
8
9
use crate::context::SharedStyleContext;
10
use crate::Atom;
11
use app_units::Au;
12
13
/// Represents the font metrics that style needs from a font to compute the
14
/// value of certain CSS units like `ex`.
15
#[derive(Clone, Debug, Default, PartialEq)]
16
pub struct FontMetrics {
17
/// The x-height of the font.
18
pub x_height: Option<Au>,
19
/// The zero advance. This is usually writing mode dependent
20
pub zero_advance_measure: Option<Au>,
21
}
22
23
/// Type of font metrics to retrieve.
24
#[derive(Clone, Debug, PartialEq)]
25
pub enum FontMetricsOrientation {
26
/// Get metrics for horizontal or vertical according to the Context's
27
/// writing mode.
28
MatchContext,
29
/// Force getting horizontal metrics.
30
Horizontal,
31
}
32
33
/// A trait used to represent something capable of providing us font metrics.
34
pub trait FontMetricsProvider {
35
/// Obtain the metrics for given font family.
36
fn query(
37
&self,
38
_context: &crate::values::computed::Context,
39
_base_size: crate::values::specified::length::FontBaseSize,
40
_orientation: FontMetricsOrientation,
41
) -> FontMetrics {
42
Default::default()
43
}
44
45
/// Get default size of a given language and generic family.
46
fn get_size(
47
&self,
48
font_name: &Atom,
49
font_family: crate::values::computed::font::GenericFontFamily,
50
) -> Au;
51
52
/// Construct from a shared style context
53
fn create_from(context: &SharedStyleContext) -> Self
54
where
55
Self: Sized;
56
}
57
58
// TODO: Servo's font metrics provider will probably not live in this crate, so this will
59
// have to be replaced with something else (perhaps a trait method on TElement)
60
// when we get there
61
#[derive(Debug)]
62
#[cfg(feature = "servo")]
63
/// Dummy metrics provider for Servo. Knows nothing about fonts and does not provide
64
/// any metrics.
65
pub struct ServoMetricsProvider;
66
67
#[cfg(feature = "servo")]
68
impl FontMetricsProvider for ServoMetricsProvider {
69
fn create_from(_: &SharedStyleContext) -> Self {
70
ServoMetricsProvider
71
}
72
73
fn get_size(&self, _: &Atom, _: crate::values::computed::font::GenericFontFamily) -> Au {
74
unreachable!("Dummy provider should never be used to compute font size")
75
}
76
}
77
78
// Servo's font metrics provider will probably not live in this crate, so this will
79
// have to be replaced with something else (perhaps a trait method on TElement)
80
// when we get there
81
82
#[cfg(feature = "gecko")]
83
/// Construct a font metrics provider for the current product
84
pub fn get_metrics_provider_for_product() -> crate::gecko::wrapper::GeckoFontMetricsProvider {
85
crate::gecko::wrapper::GeckoFontMetricsProvider::new()
86
}
87
88
#[cfg(feature = "servo")]
89
/// Construct a font metrics provider for the current product
90
pub fn get_metrics_provider_for_product() -> ServoMetricsProvider {
91
ServoMetricsProvider
92
}