mirror of
https://github.com/servo/servo
synced 2026-04-25 17:15:48 +02:00
Compare commits
11 Commits
f9cfd05af8
...
wr-skrifa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
873a23ead1 | ||
|
|
0b55d6cbb3 | ||
|
|
044596ee71 | ||
|
|
e8b79d0458 | ||
|
|
4de78b26f7 | ||
|
|
9b2f50c151 | ||
|
|
4669f24c4f | ||
|
|
ab83b08663 | ||
|
|
c1a6900286 | ||
|
|
abe58c575a | ||
|
|
e6bb7753c4 |
146
Cargo.lock
generated
146
Cargo.lock
generated
@@ -1088,7 +1088,7 @@ dependencies = [
|
||||
"tracing",
|
||||
"unicode-script",
|
||||
"vello",
|
||||
"vello_cpu",
|
||||
"vello_cpu 0.0.1 (git+https://github.com/linebender/vello?rev=b0e2e598ac62c7b3d04d8660e7b1b7659b596970)",
|
||||
"webrender_api",
|
||||
]
|
||||
|
||||
@@ -2400,7 +2400,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2586,6 +2586,16 @@ dependencies = [
|
||||
"bytemuck",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fontconfig-cache-parser"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7f8afb20c8069fd676d27b214559a337cc619a605d25a87baa90b49a06f3b18"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"thiserror 1.0.69",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fontconfig-parser"
|
||||
version = "0.5.8"
|
||||
@@ -2609,6 +2619,29 @@ dependencies = [
|
||||
"ttf-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fontique"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39f97079e1293b8c1e9fb03a2875d328bd2ee8f3b95ce62959c0acc04049c708"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"fontconfig-cache-parser",
|
||||
"hashbrown",
|
||||
"icu_locid",
|
||||
"memmap2",
|
||||
"objc2 0.6.1",
|
||||
"objc2-core-foundation",
|
||||
"objc2-core-text",
|
||||
"objc2-foundation 0.3.1",
|
||||
"peniko",
|
||||
"read-fonts 0.29.3",
|
||||
"roxmltree",
|
||||
"smallvec",
|
||||
"windows 0.58.0",
|
||||
"windows-core 0.58.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fonts"
|
||||
version = "0.0.1"
|
||||
@@ -2629,6 +2662,7 @@ dependencies = [
|
||||
"fontsan",
|
||||
"freetype-sys",
|
||||
"harfbuzz-sys",
|
||||
"harfrust",
|
||||
"ipc-channel",
|
||||
"itertools 0.14.0",
|
||||
"libc",
|
||||
@@ -2640,7 +2674,7 @@ dependencies = [
|
||||
"parking_lot",
|
||||
"profile_traits",
|
||||
"range",
|
||||
"read-fonts",
|
||||
"read-fonts 0.31.3",
|
||||
"serde",
|
||||
"servo-tracing",
|
||||
"servo_allocator",
|
||||
@@ -2648,7 +2682,7 @@ dependencies = [
|
||||
"servo_config",
|
||||
"servo_malloc_size_of",
|
||||
"servo_url",
|
||||
"skrifa",
|
||||
"skrifa 0.33.2",
|
||||
"smallvec",
|
||||
"stylo",
|
||||
"stylo_atoms",
|
||||
@@ -3048,7 +3082,7 @@ dependencies = [
|
||||
"gobject-sys",
|
||||
"libc",
|
||||
"system-deps",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3602,12 +3636,27 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "harfrust"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcd79170b62101f61285314e749ea0ccad616f8779d99571c6380a86d690ab9a"
|
||||
dependencies = [
|
||||
"bitflags 2.9.1",
|
||||
"bytemuck",
|
||||
"core_maths",
|
||||
"read-fonts 0.31.3",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
|
||||
dependencies = [
|
||||
"allocator-api2",
|
||||
"equivalent",
|
||||
"foldhash",
|
||||
"serde",
|
||||
]
|
||||
@@ -4553,7 +4602,7 @@ checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -4857,7 +4906,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07033963ba89ebaf1584d767badaa2e8fcec21aedea6b8c0346d487d49c28667"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"windows-targets 0.52.6",
|
||||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -5825,6 +5874,16 @@ dependencies = [
|
||||
"objc2-foundation 0.2.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc2-core-text"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ba833d4a1cb1aac330f8c973fd92b6ff1858e4aef5cdd00a255eefb28022fb5"
|
||||
dependencies = [
|
||||
"bitflags 2.9.1",
|
||||
"objc2-core-foundation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "objc2-encode"
|
||||
version = "4.1.0"
|
||||
@@ -6239,7 +6298,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "peek-poke"
|
||||
version = "0.3.0"
|
||||
source = "git+https://github.com/servo/webrender?branch=0.67#15318d6627e91ec19fc0a44a7434b08673413140"
|
||||
dependencies = [
|
||||
"euclid",
|
||||
"peek-poke-derive",
|
||||
@@ -6248,7 +6306,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "peek-poke-derive"
|
||||
version = "0.3.0"
|
||||
source = "git+https://github.com/servo/webrender?branch=0.67#15318d6627e91ec19fc0a44a7434b08673413140"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -6832,6 +6889,17 @@ dependencies = [
|
||||
"font-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "read-fonts"
|
||||
version = "0.31.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b8250b8f09ed4b9ba9271e06f10e7b1f03e8f8e3619e2368a991ecb25efa204"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"core_maths",
|
||||
"font-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.4.1"
|
||||
@@ -7016,7 +7084,7 @@ dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys 0.4.15",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -7029,7 +7097,7 @@ dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys 0.9.4",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -7902,7 +7970,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbeb4ca4399663735553a09dd17ce7e49a0a0203f03b706b39628c4d913a8607"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"read-fonts",
|
||||
"read-fonts 0.29.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "skrifa"
|
||||
version = "0.33.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78cc1f79e5624f166718224ef883095bea315801aca75b62c64f3937755a586d"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"read-fonts 0.31.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -8407,7 +8485,7 @@ dependencies = [
|
||||
"getrandom 0.3.3",
|
||||
"once_cell",
|
||||
"rustix 1.0.8",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -9176,7 +9254,7 @@ dependencies = [
|
||||
"log",
|
||||
"peniko",
|
||||
"png",
|
||||
"skrifa",
|
||||
"skrifa 0.31.3",
|
||||
"static_assertions",
|
||||
"thiserror 2.0.12",
|
||||
"vello_encoding",
|
||||
@@ -9184,6 +9262,20 @@ dependencies = [
|
||||
"wgpu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vello_common"
|
||||
version = "0.0.1"
|
||||
source = "git+https://github.com/linebender/vello?rev=931e6fdc135507486324adec019edffa2f70a203#931e6fdc135507486324adec019edffa2f70a203"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"fearless_simd",
|
||||
"log",
|
||||
"peniko",
|
||||
"png",
|
||||
"skrifa 0.31.3",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vello_common"
|
||||
version = "0.0.1"
|
||||
@@ -9194,17 +9286,26 @@ dependencies = [
|
||||
"log",
|
||||
"peniko",
|
||||
"png",
|
||||
"skrifa",
|
||||
"skrifa 0.31.3",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vello_cpu"
|
||||
version = "0.0.1"
|
||||
source = "git+https://github.com/linebender/vello?rev=931e6fdc135507486324adec019edffa2f70a203#931e6fdc135507486324adec019edffa2f70a203"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"vello_common 0.0.1 (git+https://github.com/linebender/vello?rev=931e6fdc135507486324adec019edffa2f70a203)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vello_cpu"
|
||||
version = "0.0.1"
|
||||
source = "git+https://github.com/linebender/vello?rev=b0e2e598ac62c7b3d04d8660e7b1b7659b596970#b0e2e598ac62c7b3d04d8660e7b1b7659b596970"
|
||||
dependencies = [
|
||||
"bytemuck",
|
||||
"vello_common",
|
||||
"vello_common 0.0.1 (git+https://github.com/linebender/vello?rev=b0e2e598ac62c7b3d04d8660e7b1b7659b596970)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -9215,7 +9316,7 @@ dependencies = [
|
||||
"bytemuck",
|
||||
"guillotiere",
|
||||
"peniko",
|
||||
"skrifa",
|
||||
"skrifa 0.31.3",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
@@ -9663,7 +9764,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "webrender"
|
||||
version = "0.66.0"
|
||||
source = "git+https://github.com/servo/webrender?branch=0.67#15318d6627e91ec19fc0a44a7434b08673413140"
|
||||
dependencies = [
|
||||
"allocator-api2",
|
||||
"bincode",
|
||||
@@ -9698,7 +9798,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "webrender_api"
|
||||
version = "0.66.0"
|
||||
source = "git+https://github.com/servo/webrender?branch=0.67#15318d6627e91ec19fc0a44a7434b08673413140"
|
||||
dependencies = [
|
||||
"app_units",
|
||||
"bitflags 2.9.1",
|
||||
@@ -9719,7 +9818,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "webrender_build"
|
||||
version = "0.0.2"
|
||||
source = "git+https://github.com/servo/webrender?branch=0.67#15318d6627e91ec19fc0a44a7434b08673413140"
|
||||
dependencies = [
|
||||
"bitflags 2.9.1",
|
||||
"lazy_static",
|
||||
@@ -9942,7 +10040,7 @@ version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||
dependencies = [
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -10431,24 +10529,27 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "wr_glyph_rasterizer"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/servo/webrender?branch=0.67#15318d6627e91ec19fc0a44a7434b08673413140"
|
||||
dependencies = [
|
||||
"core-foundation 0.9.4",
|
||||
"core-graphics",
|
||||
"core-text",
|
||||
"dwrote",
|
||||
"euclid",
|
||||
"fontique",
|
||||
"freetype",
|
||||
"fxhash",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"log",
|
||||
"malloc_size_of_derive",
|
||||
"memmap2",
|
||||
"objc",
|
||||
"rayon",
|
||||
"serde",
|
||||
"skrifa 0.31.3",
|
||||
"smallvec",
|
||||
"tracy-rs",
|
||||
"vello_cpu 0.0.1 (git+https://github.com/linebender/vello?rev=931e6fdc135507486324adec019edffa2f70a203)",
|
||||
"webrender_api",
|
||||
"wr_malloc_size_of",
|
||||
]
|
||||
@@ -10456,7 +10557,6 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "wr_malloc_size_of"
|
||||
version = "0.2.0"
|
||||
source = "git+https://github.com/servo/webrender?branch=0.67#15318d6627e91ec19fc0a44a7434b08673413140"
|
||||
dependencies = [
|
||||
"app_units",
|
||||
"euclid",
|
||||
|
||||
11
Cargo.toml
11
Cargo.toml
@@ -75,6 +75,7 @@ gstreamer-gl-sys = "0.23"
|
||||
gstreamer-sys = "0.23"
|
||||
gstreamer-video = "0.23"
|
||||
harfbuzz-sys = "0.6.1"
|
||||
harfrust = "0.1.1"
|
||||
headers = "0.4"
|
||||
hitrace = "0.1.5"
|
||||
html5ever = "0.35"
|
||||
@@ -122,7 +123,7 @@ rand_core = "0.6"
|
||||
rand_isaac = "0.3"
|
||||
raw-window-handle = "0.6"
|
||||
rayon = "1"
|
||||
read-fonts = "0.29.2"
|
||||
read-fonts = "0.31.3"
|
||||
regex = "1.11"
|
||||
resvg = "0.45.0"
|
||||
rustls = { version = "0.23", default-features = false, features = ["logging", "std", "tls12"] }
|
||||
@@ -138,7 +139,7 @@ servo-media-dummy = { git = "https://github.com/servo/media" }
|
||||
servo-media-gstreamer = { git = "https://github.com/servo/media" }
|
||||
servo-tracing = { path = "components/servo_tracing" }
|
||||
servo_arc = { git = "https://github.com/servo/stylo", branch = "2025-08-01" }
|
||||
skrifa = "0.31.3"
|
||||
skrifa = "0.33.2"
|
||||
smallbitvec = "2.6.0"
|
||||
smallvec = { version = "1.15", features = ["serde", "union"] }
|
||||
string_cache = "0.8"
|
||||
@@ -178,8 +179,8 @@ vello_cpu = { git = "https://github.com/linebender/vello", rev = "b0e2e598ac62c7
|
||||
webdriver = "0.53.0"
|
||||
webgpu_traits = { path = "components/shared/webgpu" }
|
||||
webpki-roots = "1.0"
|
||||
webrender = { git = "https://github.com/servo/webrender", branch = "0.67", features = ["capture"] }
|
||||
webrender_api = { git = "https://github.com/servo/webrender", branch = "0.67" }
|
||||
webrender = { git = "https://github.com/servo/webrender", branch = "fontations", features = ["capture", "fontations"] }
|
||||
webrender_api = { git = "https://github.com/servo/webrender", branch = "fontations" }
|
||||
webxr-api = { path = "components/shared/webxr" }
|
||||
wgpu-core = "25"
|
||||
wgpu-types = "25"
|
||||
@@ -187,7 +188,7 @@ winapi = "0.3"
|
||||
windows-sys = "0.59"
|
||||
winit = "0.30.12"
|
||||
wio = "0.2"
|
||||
wr_malloc_size_of = { git = "https://github.com/servo/webrender", branch = "0.67" }
|
||||
wr_malloc_size_of = { git = "https://github.com/servo/webrender", branch = "fontations" }
|
||||
xi-unicode = "0.3.0"
|
||||
xml5ever = "0.35"
|
||||
|
||||
|
||||
@@ -9,6 +9,13 @@ use style::color::AbsoluteColor;
|
||||
use crate::backend::Convert;
|
||||
use crate::canvas_data::Filter;
|
||||
|
||||
impl Convert<peniko::Font> for fonts::RawFont {
|
||||
fn convert(self) -> peniko::Font {
|
||||
use std::sync::Arc;
|
||||
peniko::Font::new(peniko::Blob::new(Arc::new(self.data)), self.index)
|
||||
}
|
||||
}
|
||||
|
||||
impl Convert<kurbo::Join> for LineJoinStyle {
|
||||
fn convert(self) -> kurbo::Join {
|
||||
match self {
|
||||
|
||||
@@ -333,8 +333,9 @@ impl GenericDrawTarget for raqote::DrawTarget {
|
||||
SHARED_FONT_CACHE.with(|font_cache| {
|
||||
let identifier = template.identifier();
|
||||
if !font_cache.borrow().contains_key(&identifier) {
|
||||
let data = std::sync::Arc::new(run.font.data().as_ref().to_vec());
|
||||
let Ok(font) = Font::from_bytes(data, identifier.index()) else {
|
||||
let raw_font = run.font.raw_font();
|
||||
let data = std::sync::Arc::new(raw_font.data.as_ref().to_vec());
|
||||
let Ok(font) = Font::from_bytes(data, raw_font.index) else {
|
||||
return;
|
||||
};
|
||||
font_cache.borrow_mut().insert(identifier.clone(), font);
|
||||
|
||||
@@ -402,13 +402,8 @@ impl GenericDrawTarget for VelloDrawTarget {
|
||||
SHARED_FONT_CACHE.with(|font_cache| {
|
||||
let identifier = template.identifier();
|
||||
if !font_cache.borrow().contains_key(&identifier) {
|
||||
font_cache.borrow_mut().insert(
|
||||
identifier.clone(),
|
||||
peniko::Font::new(
|
||||
peniko::Blob::from(run.font.data().as_ref().to_vec()),
|
||||
identifier.index(),
|
||||
),
|
||||
);
|
||||
let font = run.font.raw_font().clone().convert();
|
||||
font_cache.borrow_mut().insert(identifier.clone(), font);
|
||||
}
|
||||
|
||||
let font_cache = font_cache.borrow();
|
||||
|
||||
@@ -305,13 +305,8 @@ impl GenericDrawTarget for VelloCPUDrawTarget {
|
||||
SHARED_FONT_CACHE.with(|font_cache| {
|
||||
let identifier = template.identifier();
|
||||
if !font_cache.borrow().contains_key(&identifier) {
|
||||
font_cache.borrow_mut().insert(
|
||||
identifier.clone(),
|
||||
peniko::Font::new(
|
||||
peniko::Blob::from(run.font.data().as_ref().to_vec()),
|
||||
identifier.index(),
|
||||
),
|
||||
);
|
||||
let font = run.font.raw_font().clone().convert();
|
||||
font_cache.borrow_mut().insert(identifier.clone(), font);
|
||||
}
|
||||
|
||||
let font_cache = font_cache.borrow();
|
||||
|
||||
@@ -14,6 +14,9 @@ test = true
|
||||
doctest = false
|
||||
|
||||
[features]
|
||||
default = ["harfrust"]
|
||||
harfbuzz = ["dep:harfbuzz-sys"]
|
||||
harfrust = ["dep:harfrust"]
|
||||
tracing = ["dep:tracing"]
|
||||
|
||||
[dependencies]
|
||||
@@ -27,7 +30,8 @@ fnv = { workspace = true }
|
||||
fonts_traits = { workspace = true }
|
||||
fontsan = { git = "https://github.com/servo/fontsan" }
|
||||
# FIXME (#34517): macOS only needs this when building libservo without `--features media-gstreamer`
|
||||
harfbuzz-sys = { workspace = true, features = ["bundled"] }
|
||||
harfbuzz-sys = { workspace = true, optional = true, features = ["bundled"] }
|
||||
harfrust = { workspace = true, optional = true }
|
||||
ipc-channel = { workspace = true }
|
||||
itertools = { workspace = true }
|
||||
libc = { workspace = true }
|
||||
|
||||
@@ -32,7 +32,7 @@ pub use crate::platform::font_list::fallback_font_families;
|
||||
use crate::{
|
||||
ByteIndex, EmojiPresentationPreference, FallbackFontSelectionOptions, FontContext, FontData,
|
||||
FontIdentifier, FontTemplateDescriptor, FontTemplateRef, FontTemplateRefMethods, GlyphData,
|
||||
GlyphId, GlyphStore, LocalFontIdentifier, Shaper,
|
||||
GlyphId, GlyphStore, LocalFontIdentifier, Shaper, TShaper as _,
|
||||
};
|
||||
|
||||
#[macro_export]
|
||||
@@ -49,6 +49,7 @@ pub const SBIX: u32 = ot_tag!('s', 'b', 'i', 'x');
|
||||
pub const CBDT: u32 = ot_tag!('C', 'B', 'D', 'T');
|
||||
pub const COLR: u32 = ot_tag!('C', 'O', 'L', 'R');
|
||||
pub const BASE: u32 = ot_tag!('B', 'A', 'S', 'E');
|
||||
pub const LIGA: u32 = ot_tag!('l', 'i', 'g', 'a');
|
||||
|
||||
pub const LAST_RESORT_GLYPH_ADVANCE: FractionalPixel = 10.0;
|
||||
|
||||
@@ -227,6 +228,15 @@ impl malloc_size_of::MallocSizeOf for CachedShapeData {
|
||||
}
|
||||
}
|
||||
|
||||
/// Raw data and index for a font
|
||||
#[derive(Clone)]
|
||||
pub struct RawFont {
|
||||
/// The raw font file data (.ttf, .otf, .ttc, etc)
|
||||
pub data: FontData,
|
||||
/// The index of the font within the file (0 if the file is not a ttc)
|
||||
pub index: u32,
|
||||
}
|
||||
|
||||
pub struct Font {
|
||||
pub handle: PlatformFont,
|
||||
pub template: FontTemplateRef,
|
||||
@@ -234,7 +244,7 @@ pub struct Font {
|
||||
pub descriptor: FontDescriptor,
|
||||
|
||||
/// The data for this font. This might be uninitialized for system fonts.
|
||||
data: OnceLock<FontData>,
|
||||
raw: OnceLock<RawFont>,
|
||||
|
||||
shaper: OnceLock<Shaper>,
|
||||
cached_shape_data: RwLock<CachedShapeData>,
|
||||
@@ -288,7 +298,9 @@ impl Font {
|
||||
template,
|
||||
metrics,
|
||||
descriptor,
|
||||
data: data.map(OnceLock::from).unwrap_or_default(),
|
||||
raw: data
|
||||
.map(|data| OnceLock::from(RawFont { data, index: 0 }))
|
||||
.unwrap_or_default(),
|
||||
shaper: OnceLock::new(),
|
||||
cached_shape_data: Default::default(),
|
||||
font_instance_key: Default::default(),
|
||||
@@ -323,16 +335,18 @@ impl Font {
|
||||
|
||||
/// Return the data for this `Font`. Note that this is currently highly inefficient for system
|
||||
/// fonts and should not be used except in legacy canvas code.
|
||||
pub fn data(&self) -> &FontData {
|
||||
self.data.get_or_init(|| {
|
||||
pub fn raw_font(&self) -> &RawFont {
|
||||
self.raw.get_or_init(|| {
|
||||
let FontIdentifier::Local(local_font_identifier) = self.identifier() else {
|
||||
unreachable!("All web fonts should already have initialized data");
|
||||
};
|
||||
FontData::from_bytes(
|
||||
&local_font_identifier
|
||||
.read_data_from_file()
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
let Some((bytes, index)) = local_font_identifier.read_data_from_file() else {
|
||||
panic!("Failed to load raw font data");
|
||||
};
|
||||
|
||||
let data = FontData::from_bytes(&bytes);
|
||||
|
||||
RawFont { data, index }
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -425,9 +439,8 @@ impl Font {
|
||||
}
|
||||
|
||||
fn shape_text_harfbuzz(&self, text: &str, options: &ShapingOptions, glyphs: &mut GlyphStore) {
|
||||
let this = self as *const Font;
|
||||
self.shaper
|
||||
.get_or_init(|| Shaper::new(this))
|
||||
.get_or_init(|| Shaper::new(self))
|
||||
.shape_text(text, options, glyphs);
|
||||
}
|
||||
|
||||
@@ -543,8 +556,7 @@ impl Font {
|
||||
|
||||
/// Get the [`FontBaseline`] for this font.
|
||||
pub fn baseline(&self) -> Option<FontBaseline> {
|
||||
let this = self as *const Font;
|
||||
self.shaper.get_or_init(|| Shaper::new(this)).baseline()
|
||||
self.shaper.get_or_init(|| Shaper::new(self)).baseline()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ mod font_template;
|
||||
mod glyph;
|
||||
#[allow(unsafe_code)]
|
||||
pub mod platform;
|
||||
mod shaper;
|
||||
mod shapers;
|
||||
mod system_font_service;
|
||||
|
||||
use std::sync::Arc;
|
||||
@@ -23,7 +23,7 @@ pub use font_template::*;
|
||||
pub use glyph::*;
|
||||
use ipc_channel::ipc::IpcSharedMemory;
|
||||
pub use platform::LocalFontIdentifier;
|
||||
pub use shaper::*;
|
||||
pub use shapers::*;
|
||||
pub use system_font_service::*;
|
||||
use unicode_properties::{EmojiStatus, UnicodeEmoji, emoji};
|
||||
|
||||
@@ -106,10 +106,12 @@ impl FallbackFontSelectionOptions {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "harfbuzz")]
|
||||
pub(crate) fn float_to_fixed(before: usize, f: f64) -> i32 {
|
||||
((1i32 << before) as f64 * f) as i32
|
||||
}
|
||||
|
||||
#[cfg(feature = "harfbuzz")]
|
||||
pub(crate) fn fixed_to_float(before: usize, f: i32) -> f64 {
|
||||
f as f64 * 1.0f64 / ((1i32 << before) as f64)
|
||||
}
|
||||
|
||||
@@ -56,9 +56,9 @@ impl LocalFontIdentifier {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn read_data_from_file(&self) -> Option<Vec<u8>> {
|
||||
pub(crate) fn read_data_from_file(&self) -> Option<(Vec<u8>, u32)> {
|
||||
let file = File::open(Path::new(&*self.path)).ok()?;
|
||||
let mmap = unsafe { Mmap::map(&file).ok()? };
|
||||
Some(mmap[..].to_vec())
|
||||
Some((mmap[..].to_vec(), self.variation_index as u32))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use base::text::{UnicodeBlock, UnicodeBlockMethod, unicode_plane};
|
||||
use log::debug;
|
||||
use malloc_size_of_derive::MallocSizeOf;
|
||||
use memmap2::Mmap;
|
||||
use read_fonts::FileRef;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use style::Atom;
|
||||
use style::values::computed::font::GenericFontFamily;
|
||||
@@ -43,7 +44,7 @@ impl LocalFontIdentifier {
|
||||
0
|
||||
}
|
||||
|
||||
pub(crate) fn read_data_from_file(&self) -> Option<Vec<u8>> {
|
||||
pub(crate) fn read_data_from_file(&self) -> Option<(Vec<u8>, u32)> {
|
||||
// TODO: This is incorrect, if the font file is a TTC (collection) with more than
|
||||
// one font. In that case we either need to reconstruct the pertinent tables into
|
||||
// a bundle of font data (expensive) or make sure that the value returned by
|
||||
@@ -52,7 +53,48 @@ impl LocalFontIdentifier {
|
||||
// listed in the font.
|
||||
let file = File::open(Path::new(&*self.path)).ok()?;
|
||||
let mmap = unsafe { Mmap::map(&file).ok()? };
|
||||
Some(mmap[..].to_vec())
|
||||
|
||||
// Determine index
|
||||
let file_ref = FileRef::new(mmap.as_ref()).ok()?;
|
||||
let index = ttc_index_from_postscript_name(file_ref, &self.postscript_name);
|
||||
|
||||
Some((mmap[..].to_vec(), index))
|
||||
}
|
||||
}
|
||||
|
||||
/// CoreText font enumaration gives us a postscript name rather than an index.
|
||||
/// This functions maps from postscript name to index
|
||||
fn ttc_index_from_postscript_name(font_file: FileRef<'_>, postscript_name: &str) -> u32 {
|
||||
use read_fonts::types::NameId;
|
||||
use read_fonts::{FileRef, TableProvider as _};
|
||||
|
||||
match font_file {
|
||||
FileRef::Font(_) => 0,
|
||||
FileRef::Collection(collection) => 'idx: {
|
||||
for i in 0..collection.len() {
|
||||
let font = collection.get(i).unwrap();
|
||||
let name_table = font.name().unwrap();
|
||||
if name_table
|
||||
.name_record()
|
||||
.iter()
|
||||
.filter(|record| record.name_id() == NameId::POSTSCRIPT_NAME)
|
||||
.any(|record| {
|
||||
record
|
||||
.string(name_table.string_data())
|
||||
.unwrap()
|
||||
.chars()
|
||||
.eq(postscript_name.chars())
|
||||
})
|
||||
{
|
||||
break 'idx i;
|
||||
}
|
||||
}
|
||||
|
||||
panic!(
|
||||
"Font with postscript_name {} not found in collection",
|
||||
postscript_name
|
||||
);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -67,14 +67,15 @@ impl LocalFontIdentifier {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn read_data_from_file(&self) -> Option<Vec<u8>> {
|
||||
pub(crate) fn read_data_from_file(&self) -> Option<(Vec<u8>, u32)> {
|
||||
let font = FontCollection::system()
|
||||
.font_from_descriptor(&self.font_descriptor)
|
||||
.ok()??;
|
||||
let face = font.create_font_face();
|
||||
let index = face.get_index();
|
||||
let files = face.get_files();
|
||||
assert!(!files.is_empty());
|
||||
Some(files[0].get_font_file_bytes())
|
||||
Some((files[0].get_font_file_bytes(), index))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,735 +0,0 @@
|
||||
/* 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/. */
|
||||
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use std::os::raw::{c_char, c_int, c_uint, c_void};
|
||||
use std::sync::LazyLock;
|
||||
use std::{char, cmp, ptr};
|
||||
|
||||
use app_units::Au;
|
||||
use base::text::is_bidi_control;
|
||||
use euclid::default::Point2D;
|
||||
// Eventually we would like the shaper to be pluggable, as many operating systems have their own
|
||||
// shapers. For now, however, HarfBuzz is a hard dependency.
|
||||
use harfbuzz_sys::{
|
||||
HB_DIRECTION_LTR, HB_DIRECTION_RTL, HB_MEMORY_MODE_READONLY, HB_OT_LAYOUT_BASELINE_TAG_HANGING,
|
||||
HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT, HB_OT_LAYOUT_BASELINE_TAG_ROMAN,
|
||||
hb_blob_create, hb_blob_t, hb_bool_t, hb_buffer_add_utf8, hb_buffer_create, hb_buffer_destroy,
|
||||
hb_buffer_get_glyph_infos, hb_buffer_get_glyph_positions, hb_buffer_get_length,
|
||||
hb_buffer_set_direction, hb_buffer_set_script, hb_buffer_t, hb_codepoint_t,
|
||||
hb_face_create_for_tables, hb_face_destroy, hb_face_t, hb_feature_t, hb_font_create,
|
||||
hb_font_destroy, hb_font_funcs_create, hb_font_funcs_set_glyph_h_advance_func,
|
||||
hb_font_funcs_set_nominal_glyph_func, hb_font_funcs_t, hb_font_set_funcs, hb_font_set_ppem,
|
||||
hb_font_set_scale, hb_font_t, hb_glyph_info_t, hb_glyph_position_t, hb_ot_layout_get_baseline,
|
||||
hb_position_t, hb_shape, hb_tag_t,
|
||||
};
|
||||
use log::debug;
|
||||
use num_traits::Zero;
|
||||
|
||||
use crate::font::advance_for_shaped_glyph;
|
||||
use crate::platform::font::FontTable;
|
||||
use crate::{
|
||||
BASE, ByteIndex, Font, FontBaseline, FontTableMethods, FontTableTag, GlyphData, GlyphId,
|
||||
GlyphStore, KERN, ShapingFlags, ShapingOptions, fixed_to_float, float_to_fixed, ot_tag,
|
||||
};
|
||||
|
||||
const NO_GLYPH: i32 = -1;
|
||||
const LIGA: u32 = ot_tag!('l', 'i', 'g', 'a');
|
||||
const HB_OT_TAG_DEFAULT_SCRIPT: u32 = ot_tag!('D', 'F', 'L', 'T');
|
||||
const HB_OT_TAG_DEFAULT_LANGUAGE: u32 = ot_tag!('d', 'f', 'l', 't');
|
||||
|
||||
pub struct ShapedGlyphData {
|
||||
count: usize,
|
||||
glyph_infos: *mut hb_glyph_info_t,
|
||||
pos_infos: *mut hb_glyph_position_t,
|
||||
}
|
||||
|
||||
pub struct ShapedGlyphEntry {
|
||||
codepoint: GlyphId,
|
||||
advance: Au,
|
||||
offset: Option<Point2D<Au>>,
|
||||
}
|
||||
|
||||
impl ShapedGlyphData {
|
||||
/// Create a new [`ShapedGlyphData`] from the given HarfBuzz buffer.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// Passing an invalid buffer pointer to this function results in undefined behavior.
|
||||
pub unsafe fn new(buffer: *mut hb_buffer_t) -> ShapedGlyphData {
|
||||
let mut glyph_count = 0;
|
||||
let glyph_infos = unsafe { hb_buffer_get_glyph_infos(buffer, &mut glyph_count) };
|
||||
assert!(!glyph_infos.is_null());
|
||||
let mut pos_count = 0;
|
||||
let pos_infos = unsafe { hb_buffer_get_glyph_positions(buffer, &mut pos_count) };
|
||||
assert!(!pos_infos.is_null());
|
||||
assert_eq!(glyph_count, pos_count);
|
||||
|
||||
ShapedGlyphData {
|
||||
count: glyph_count as usize,
|
||||
glyph_infos,
|
||||
pos_infos,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn byte_offset_of_glyph(&self, i: usize) -> u32 {
|
||||
assert!(i < self.count);
|
||||
|
||||
unsafe {
|
||||
let glyph_info_i = self.glyph_infos.add(i);
|
||||
(*glyph_info_i).cluster
|
||||
}
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.count
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.count == 0
|
||||
}
|
||||
|
||||
/// Returns shaped glyph data for one glyph, and updates the y-position of the pen.
|
||||
pub fn entry_for_glyph(&self, i: usize, y_pos: &mut Au) -> ShapedGlyphEntry {
|
||||
assert!(i < self.count);
|
||||
|
||||
unsafe {
|
||||
let glyph_info_i = self.glyph_infos.add(i);
|
||||
let pos_info_i = self.pos_infos.add(i);
|
||||
let x_offset = Shaper::fixed_to_float((*pos_info_i).x_offset);
|
||||
let y_offset = Shaper::fixed_to_float((*pos_info_i).y_offset);
|
||||
let x_advance = Shaper::fixed_to_float((*pos_info_i).x_advance);
|
||||
let y_advance = Shaper::fixed_to_float((*pos_info_i).y_advance);
|
||||
|
||||
let x_offset = Au::from_f64_px(x_offset);
|
||||
let y_offset = Au::from_f64_px(y_offset);
|
||||
let x_advance = Au::from_f64_px(x_advance);
|
||||
let y_advance = Au::from_f64_px(y_advance);
|
||||
|
||||
let offset = if x_offset.is_zero() && y_offset.is_zero() && y_advance.is_zero() {
|
||||
None
|
||||
} else {
|
||||
// adjust the pen..
|
||||
if y_advance > Au::zero() {
|
||||
*y_pos -= y_advance;
|
||||
}
|
||||
|
||||
Some(Point2D::new(x_offset, *y_pos - y_offset))
|
||||
};
|
||||
|
||||
ShapedGlyphEntry {
|
||||
codepoint: (*glyph_info_i).codepoint as GlyphId,
|
||||
advance: x_advance,
|
||||
offset,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Shaper {
|
||||
hb_face: *mut hb_face_t,
|
||||
hb_font: *mut hb_font_t,
|
||||
font: *const Font,
|
||||
}
|
||||
|
||||
// The HarfBuzz API is thread safe as well as our `Font`, so we can make the data
|
||||
// structures here as thread-safe as well. This doesn't seem to be documented,
|
||||
// but was expressed as one of the original goals of the HarfBuzz API.
|
||||
unsafe impl Sync for Shaper {}
|
||||
unsafe impl Send for Shaper {}
|
||||
|
||||
impl Drop for Shaper {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
assert!(!self.hb_face.is_null());
|
||||
hb_face_destroy(self.hb_face);
|
||||
|
||||
assert!(!self.hb_font.is_null());
|
||||
hb_font_destroy(self.hb_font);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Shaper {
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)] // Has an unsafe block inside
|
||||
pub fn new(font: *const Font) -> Shaper {
|
||||
unsafe {
|
||||
let hb_face: *mut hb_face_t = hb_face_create_for_tables(
|
||||
Some(font_table_func),
|
||||
font as *const c_void as *mut c_void,
|
||||
None,
|
||||
);
|
||||
let hb_font: *mut hb_font_t = hb_font_create(hb_face);
|
||||
|
||||
// Set points-per-em. if zero, performs no hinting in that direction.
|
||||
let pt_size = (*font).descriptor.pt_size.to_f64_px();
|
||||
hb_font_set_ppem(hb_font, pt_size as c_uint, pt_size as c_uint);
|
||||
|
||||
// Set scaling. Note that this takes 16.16 fixed point.
|
||||
hb_font_set_scale(
|
||||
hb_font,
|
||||
Shaper::float_to_fixed(pt_size) as c_int,
|
||||
Shaper::float_to_fixed(pt_size) as c_int,
|
||||
);
|
||||
|
||||
// configure static function callbacks.
|
||||
hb_font_set_funcs(
|
||||
hb_font,
|
||||
HB_FONT_FUNCS.0,
|
||||
font as *mut Font as *mut c_void,
|
||||
None,
|
||||
);
|
||||
|
||||
Shaper {
|
||||
hb_face,
|
||||
hb_font,
|
||||
font,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn float_to_fixed(f: f64) -> i32 {
|
||||
float_to_fixed(16, f)
|
||||
}
|
||||
|
||||
fn fixed_to_float(i: hb_position_t) -> f64 {
|
||||
fixed_to_float(16, i)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unicode_to_hb_script(script: unicode_script::Script) -> harfbuzz_sys::hb_script_t {
|
||||
use harfbuzz_sys::*;
|
||||
use unicode_script::Script::*;
|
||||
match script {
|
||||
Adlam => HB_SCRIPT_ADLAM,
|
||||
Ahom => HB_SCRIPT_AHOM,
|
||||
Anatolian_Hieroglyphs => HB_SCRIPT_ANATOLIAN_HIEROGLYPHS,
|
||||
Arabic => HB_SCRIPT_ARABIC,
|
||||
Armenian => HB_SCRIPT_ARMENIAN,
|
||||
Avestan => HB_SCRIPT_AVESTAN,
|
||||
Balinese => HB_SCRIPT_BALINESE,
|
||||
Bamum => HB_SCRIPT_BAMUM,
|
||||
Bassa_Vah => HB_SCRIPT_BASSA_VAH,
|
||||
Batak => HB_SCRIPT_BATAK,
|
||||
Bengali => HB_SCRIPT_BENGALI,
|
||||
Bhaiksuki => HB_SCRIPT_BHAIKSUKI,
|
||||
Bopomofo => HB_SCRIPT_BOPOMOFO,
|
||||
Brahmi => HB_SCRIPT_BRAHMI,
|
||||
Braille => HB_SCRIPT_BRAILLE,
|
||||
Buginese => HB_SCRIPT_BUGINESE,
|
||||
Buhid => HB_SCRIPT_BUHID,
|
||||
Canadian_Aboriginal => HB_SCRIPT_CANADIAN_SYLLABICS,
|
||||
Carian => HB_SCRIPT_CARIAN,
|
||||
Caucasian_Albanian => HB_SCRIPT_CAUCASIAN_ALBANIAN,
|
||||
Chakma => HB_SCRIPT_CHAKMA,
|
||||
Cham => HB_SCRIPT_CHAM,
|
||||
Cherokee => HB_SCRIPT_CHEROKEE,
|
||||
Chorasmian => HB_SCRIPT_CHORASMIAN,
|
||||
Common => HB_SCRIPT_COMMON,
|
||||
Coptic => HB_SCRIPT_COPTIC,
|
||||
Cuneiform => HB_SCRIPT_CUNEIFORM,
|
||||
Cypriot => HB_SCRIPT_CYPRIOT,
|
||||
Cyrillic => HB_SCRIPT_CYRILLIC,
|
||||
Deseret => HB_SCRIPT_DESERET,
|
||||
Devanagari => HB_SCRIPT_DEVANAGARI,
|
||||
Dives_Akuru => HB_SCRIPT_DIVES_AKURU,
|
||||
Dogra => HB_SCRIPT_DOGRA,
|
||||
Duployan => HB_SCRIPT_DUPLOYAN,
|
||||
Egyptian_Hieroglyphs => HB_SCRIPT_EGYPTIAN_HIEROGLYPHS,
|
||||
Elbasan => HB_SCRIPT_ELBASAN,
|
||||
Elymaic => HB_SCRIPT_ELYMAIC,
|
||||
Ethiopic => HB_SCRIPT_ETHIOPIC,
|
||||
Georgian => HB_SCRIPT_GEORGIAN,
|
||||
Glagolitic => HB_SCRIPT_GLAGOLITIC,
|
||||
Gothic => HB_SCRIPT_GOTHIC,
|
||||
Grantha => HB_SCRIPT_GRANTHA,
|
||||
Greek => HB_SCRIPT_GREEK,
|
||||
Gujarati => HB_SCRIPT_GUJARATI,
|
||||
Gunjala_Gondi => HB_SCRIPT_GUNJALA_GONDI,
|
||||
Gurmukhi => HB_SCRIPT_GURMUKHI,
|
||||
Han => HB_SCRIPT_HAN,
|
||||
Hangul => HB_SCRIPT_HANGUL,
|
||||
Hanifi_Rohingya => HB_SCRIPT_HANIFI_ROHINGYA,
|
||||
Hanunoo => HB_SCRIPT_HANUNOO,
|
||||
Hatran => HB_SCRIPT_HATRAN,
|
||||
Hebrew => HB_SCRIPT_HEBREW,
|
||||
Hiragana => HB_SCRIPT_HIRAGANA,
|
||||
Imperial_Aramaic => HB_SCRIPT_IMPERIAL_ARAMAIC,
|
||||
Inherited => HB_SCRIPT_INHERITED,
|
||||
Inscriptional_Pahlavi => HB_SCRIPT_INSCRIPTIONAL_PAHLAVI,
|
||||
Inscriptional_Parthian => HB_SCRIPT_INSCRIPTIONAL_PARTHIAN,
|
||||
Javanese => HB_SCRIPT_JAVANESE,
|
||||
Kaithi => HB_SCRIPT_KAITHI,
|
||||
Kannada => HB_SCRIPT_KANNADA,
|
||||
Katakana => HB_SCRIPT_KATAKANA,
|
||||
Kayah_Li => HB_SCRIPT_KAYAH_LI,
|
||||
Kharoshthi => HB_SCRIPT_KHAROSHTHI,
|
||||
Khitan_Small_Script => HB_SCRIPT_KHITAN_SMALL_SCRIPT,
|
||||
Khmer => HB_SCRIPT_KHMER,
|
||||
Khojki => HB_SCRIPT_KHOJKI,
|
||||
Khudawadi => HB_SCRIPT_KHUDAWADI,
|
||||
Lao => HB_SCRIPT_LAO,
|
||||
Latin => HB_SCRIPT_LATIN,
|
||||
Lepcha => HB_SCRIPT_LEPCHA,
|
||||
Limbu => HB_SCRIPT_LIMBU,
|
||||
Linear_A => HB_SCRIPT_LINEAR_A,
|
||||
Linear_B => HB_SCRIPT_LINEAR_B,
|
||||
Lisu => HB_SCRIPT_LISU,
|
||||
Lycian => HB_SCRIPT_LYCIAN,
|
||||
Lydian => HB_SCRIPT_LYDIAN,
|
||||
Mahajani => HB_SCRIPT_MAHAJANI,
|
||||
Makasar => HB_SCRIPT_MAKASAR,
|
||||
Malayalam => HB_SCRIPT_MALAYALAM,
|
||||
Mandaic => HB_SCRIPT_MANDAIC,
|
||||
Manichaean => HB_SCRIPT_MANICHAEAN,
|
||||
Marchen => HB_SCRIPT_MARCHEN,
|
||||
Masaram_Gondi => HB_SCRIPT_MASARAM_GONDI,
|
||||
Medefaidrin => HB_SCRIPT_MEDEFAIDRIN,
|
||||
Meetei_Mayek => HB_SCRIPT_MEETEI_MAYEK,
|
||||
Mende_Kikakui => HB_SCRIPT_MENDE_KIKAKUI,
|
||||
Meroitic_Cursive => HB_SCRIPT_MEROITIC_CURSIVE,
|
||||
Meroitic_Hieroglyphs => HB_SCRIPT_MEROITIC_HIEROGLYPHS,
|
||||
Miao => HB_SCRIPT_MIAO,
|
||||
Modi => HB_SCRIPT_MODI,
|
||||
Mongolian => HB_SCRIPT_MONGOLIAN,
|
||||
Mro => HB_SCRIPT_MRO,
|
||||
Multani => HB_SCRIPT_MULTANI,
|
||||
Myanmar => HB_SCRIPT_MYANMAR,
|
||||
Nabataean => HB_SCRIPT_NABATAEAN,
|
||||
Nandinagari => HB_SCRIPT_NANDINAGARI,
|
||||
New_Tai_Lue => HB_SCRIPT_NEW_TAI_LUE,
|
||||
Newa => HB_SCRIPT_NEWA,
|
||||
Nko => HB_SCRIPT_NKO,
|
||||
Nushu => HB_SCRIPT_NUSHU,
|
||||
Nyiakeng_Puachue_Hmong => HB_SCRIPT_NYIAKENG_PUACHUE_HMONG,
|
||||
Ogham => HB_SCRIPT_OGHAM,
|
||||
Ol_Chiki => HB_SCRIPT_OL_CHIKI,
|
||||
Old_Hungarian => HB_SCRIPT_OLD_HUNGARIAN,
|
||||
Old_Italic => HB_SCRIPT_OLD_ITALIC,
|
||||
Old_North_Arabian => HB_SCRIPT_OLD_NORTH_ARABIAN,
|
||||
Old_Permic => HB_SCRIPT_OLD_PERMIC,
|
||||
Old_Persian => HB_SCRIPT_OLD_PERSIAN,
|
||||
Old_Sogdian => HB_SCRIPT_OLD_SOGDIAN,
|
||||
Old_South_Arabian => HB_SCRIPT_OLD_SOUTH_ARABIAN,
|
||||
Old_Turkic => HB_SCRIPT_OLD_TURKIC,
|
||||
Oriya => HB_SCRIPT_ORIYA,
|
||||
Osage => HB_SCRIPT_OSAGE,
|
||||
Osmanya => HB_SCRIPT_OSMANYA,
|
||||
Pahawh_Hmong => HB_SCRIPT_PAHAWH_HMONG,
|
||||
Palmyrene => HB_SCRIPT_PALMYRENE,
|
||||
Pau_Cin_Hau => HB_SCRIPT_PAU_CIN_HAU,
|
||||
Phags_Pa => HB_SCRIPT_PHAGS_PA,
|
||||
Phoenician => HB_SCRIPT_PHOENICIAN,
|
||||
Psalter_Pahlavi => HB_SCRIPT_PSALTER_PAHLAVI,
|
||||
Rejang => HB_SCRIPT_REJANG,
|
||||
Runic => HB_SCRIPT_RUNIC,
|
||||
Samaritan => HB_SCRIPT_SAMARITAN,
|
||||
Saurashtra => HB_SCRIPT_SAURASHTRA,
|
||||
Sharada => HB_SCRIPT_SHARADA,
|
||||
Shavian => HB_SCRIPT_SHAVIAN,
|
||||
Siddham => HB_SCRIPT_SIDDHAM,
|
||||
SignWriting => HB_SCRIPT_SIGNWRITING,
|
||||
Sinhala => HB_SCRIPT_SINHALA,
|
||||
Sogdian => HB_SCRIPT_SOGDIAN,
|
||||
Sora_Sompeng => HB_SCRIPT_SORA_SOMPENG,
|
||||
Soyombo => HB_SCRIPT_SOYOMBO,
|
||||
Sundanese => HB_SCRIPT_SUNDANESE,
|
||||
Syloti_Nagri => HB_SCRIPT_SYLOTI_NAGRI,
|
||||
Syriac => HB_SCRIPT_SYRIAC,
|
||||
Tagalog => HB_SCRIPT_TAGALOG,
|
||||
Tagbanwa => HB_SCRIPT_TAGBANWA,
|
||||
Tai_Le => HB_SCRIPT_TAI_LE,
|
||||
Tai_Tham => HB_SCRIPT_TAI_THAM,
|
||||
Tai_Viet => HB_SCRIPT_TAI_VIET,
|
||||
Takri => HB_SCRIPT_TAKRI,
|
||||
Tamil => HB_SCRIPT_TAMIL,
|
||||
Tangut => HB_SCRIPT_TANGUT,
|
||||
Telugu => HB_SCRIPT_TELUGU,
|
||||
Thaana => HB_SCRIPT_THAANA,
|
||||
Thai => HB_SCRIPT_THAI,
|
||||
Tibetan => HB_SCRIPT_TIBETAN,
|
||||
Tifinagh => HB_SCRIPT_TIFINAGH,
|
||||
Tirhuta => HB_SCRIPT_TIRHUTA,
|
||||
Ugaritic => HB_SCRIPT_UGARITIC,
|
||||
Unknown => HB_SCRIPT_UNKNOWN,
|
||||
Vai => HB_SCRIPT_VAI,
|
||||
Warang_Citi => HB_SCRIPT_WARANG_CITI,
|
||||
Wancho => HB_SCRIPT_WANCHO,
|
||||
Yezidi => HB_SCRIPT_YEZIDI,
|
||||
Yi => HB_SCRIPT_YI,
|
||||
Zanabazar_Square => HB_SCRIPT_ZANABAZAR_SQUARE,
|
||||
_ => HB_SCRIPT_UNKNOWN,
|
||||
}
|
||||
}
|
||||
|
||||
impl Shaper {
|
||||
/// Calculate the layout metrics associated with the given text when painted in a specific
|
||||
/// font.
|
||||
pub(crate) fn shape_text(&self, text: &str, options: &ShapingOptions, glyphs: &mut GlyphStore) {
|
||||
unsafe {
|
||||
let hb_buffer: *mut hb_buffer_t = hb_buffer_create();
|
||||
hb_buffer_set_direction(
|
||||
hb_buffer,
|
||||
if options.flags.contains(ShapingFlags::RTL_FLAG) {
|
||||
HB_DIRECTION_RTL
|
||||
} else {
|
||||
HB_DIRECTION_LTR
|
||||
},
|
||||
);
|
||||
|
||||
hb_buffer_set_script(hb_buffer, unicode_to_hb_script(options.script));
|
||||
|
||||
hb_buffer_add_utf8(
|
||||
hb_buffer,
|
||||
text.as_ptr() as *const c_char,
|
||||
text.len() as c_int,
|
||||
0,
|
||||
text.len() as c_int,
|
||||
);
|
||||
|
||||
let mut features = Vec::new();
|
||||
if options
|
||||
.flags
|
||||
.contains(ShapingFlags::IGNORE_LIGATURES_SHAPING_FLAG)
|
||||
{
|
||||
features.push(hb_feature_t {
|
||||
tag: LIGA,
|
||||
value: 0,
|
||||
start: 0,
|
||||
end: hb_buffer_get_length(hb_buffer),
|
||||
})
|
||||
}
|
||||
if options
|
||||
.flags
|
||||
.contains(ShapingFlags::DISABLE_KERNING_SHAPING_FLAG)
|
||||
{
|
||||
features.push(hb_feature_t {
|
||||
tag: KERN,
|
||||
value: 0,
|
||||
start: 0,
|
||||
end: hb_buffer_get_length(hb_buffer),
|
||||
})
|
||||
}
|
||||
|
||||
hb_shape(
|
||||
self.hb_font,
|
||||
hb_buffer,
|
||||
features.as_mut_ptr(),
|
||||
features.len() as u32,
|
||||
);
|
||||
self.save_glyph_results(text, options, glyphs, hb_buffer);
|
||||
hb_buffer_destroy(hb_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
fn save_glyph_results(
|
||||
&self,
|
||||
text: &str,
|
||||
options: &ShapingOptions,
|
||||
glyphs: &mut GlyphStore,
|
||||
buffer: *mut hb_buffer_t,
|
||||
) {
|
||||
let glyph_data = unsafe { ShapedGlyphData::new(buffer) };
|
||||
let glyph_count = glyph_data.len();
|
||||
let byte_max = text.len();
|
||||
|
||||
debug!(
|
||||
"Shaped text[byte count={}], got back {} glyph info records.",
|
||||
byte_max, glyph_count
|
||||
);
|
||||
|
||||
// make map of what chars have glyphs
|
||||
let mut byte_to_glyph = vec![NO_GLYPH; byte_max];
|
||||
|
||||
debug!("(glyph idx) -> (text byte offset)");
|
||||
for i in 0..glyph_data.len() {
|
||||
let loc = glyph_data.byte_offset_of_glyph(i) as usize;
|
||||
if loc < byte_max {
|
||||
byte_to_glyph[loc] = i as i32;
|
||||
} else {
|
||||
debug!(
|
||||
"ERROR: tried to set out of range byte_to_glyph: idx={}, glyph idx={}",
|
||||
loc, i
|
||||
);
|
||||
}
|
||||
debug!("{} -> {}", i, loc);
|
||||
}
|
||||
|
||||
debug!("text: {:?}", text);
|
||||
debug!("(char idx): char->(glyph index):");
|
||||
for (i, ch) in text.char_indices() {
|
||||
debug!("{}: {:?} --> {}", i, ch, byte_to_glyph[i]);
|
||||
}
|
||||
|
||||
let mut glyph_span = 0..0;
|
||||
let mut byte_range = 0..0;
|
||||
|
||||
let mut y_pos = Au::zero();
|
||||
|
||||
// main loop over each glyph. each iteration usually processes 1 glyph and 1+ chars.
|
||||
// in cases with complex glyph-character associations, 2+ glyphs and 1+ chars can be
|
||||
// processed.
|
||||
while glyph_span.start < glyph_count {
|
||||
debug!("Processing glyph at idx={}", glyph_span.start);
|
||||
glyph_span.end = glyph_span.start;
|
||||
byte_range.end = glyph_data.byte_offset_of_glyph(glyph_span.start) as usize;
|
||||
|
||||
while byte_range.end < byte_max {
|
||||
byte_range.end += 1;
|
||||
// Extend the byte range to include any following byte without its own glyph.
|
||||
while byte_range.end < byte_max && byte_to_glyph[byte_range.end] == NO_GLYPH {
|
||||
byte_range.end += 1;
|
||||
}
|
||||
|
||||
// Extend the glyph range to include all glyphs covered by bytes processed so far.
|
||||
let mut max_glyph_idx = glyph_span.end;
|
||||
for glyph_idx in &byte_to_glyph[byte_range.clone()] {
|
||||
if *glyph_idx != NO_GLYPH {
|
||||
max_glyph_idx = cmp::max(*glyph_idx as usize + 1, max_glyph_idx);
|
||||
}
|
||||
}
|
||||
if max_glyph_idx > glyph_span.end {
|
||||
glyph_span.end = max_glyph_idx;
|
||||
debug!("Extended glyph span to {:?}", glyph_span);
|
||||
}
|
||||
|
||||
// if there's just one glyph, then we don't need further checks.
|
||||
if glyph_span.len() == 1 {
|
||||
break;
|
||||
}
|
||||
|
||||
// if no glyphs were found yet, extend the char byte range more.
|
||||
if glyph_span.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If byte_range now includes all the byte offsets found in glyph_span, then we
|
||||
// have found a contiguous "cluster" and can stop extending it.
|
||||
let mut all_glyphs_are_within_cluster: bool = true;
|
||||
for j in glyph_span.clone() {
|
||||
let loc = glyph_data.byte_offset_of_glyph(j) as usize;
|
||||
if !(byte_range.start <= loc && loc < byte_range.end) {
|
||||
all_glyphs_are_within_cluster = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if all_glyphs_are_within_cluster {
|
||||
break;
|
||||
}
|
||||
|
||||
// Otherwise, the bytes we have seen so far correspond to a non-contiguous set of
|
||||
// glyphs. Keep extending byte_range until we fill in all the holes in the glyph
|
||||
// span or reach the end of the text.
|
||||
}
|
||||
|
||||
assert!(!byte_range.is_empty());
|
||||
assert!(!glyph_span.is_empty());
|
||||
|
||||
// Now byte_range is the ligature clump formed by the glyphs in glyph_span.
|
||||
// We will save these glyphs to the glyph store at the index of the first byte.
|
||||
let byte_idx = ByteIndex(byte_range.start as isize);
|
||||
|
||||
if glyph_span.len() == 1 {
|
||||
// Fast path: 1-to-1 mapping of byte offset to single glyph.
|
||||
//
|
||||
// TODO(Issue #214): cluster ranges need to be computed before
|
||||
// shaping, and then consulted here.
|
||||
// for now, just pretend that every character is a cluster start.
|
||||
// (i.e., pretend there are no combining character sequences).
|
||||
// 1-to-1 mapping of character to glyph also treated as ligature start.
|
||||
//
|
||||
// NB: When we acquire the ability to handle ligatures that cross word boundaries,
|
||||
// we'll need to do something special to handle `word-spacing` properly.
|
||||
let character = text[byte_range.clone()].chars().next().unwrap();
|
||||
if is_bidi_control(character) {
|
||||
// Don't add any glyphs for bidi control chars
|
||||
} else {
|
||||
let (glyph_id, advance, offset) = if character == '\t' {
|
||||
// Treat tabs in pre-formatted text as a fixed number of spaces. The glyph id does
|
||||
// not matter here as Servo doesn't render any glyphs for whitespace.
|
||||
//
|
||||
// TODO: Proper tab stops. This should happen in layout and be based on the
|
||||
// size of the space character of the inline formatting context.
|
||||
let font = unsafe { &(*self.font) };
|
||||
(
|
||||
font.glyph_index(' ').unwrap_or(0) as hb_codepoint_t,
|
||||
font.metrics.space_advance * 8,
|
||||
Default::default(),
|
||||
)
|
||||
} else {
|
||||
let shape = glyph_data.entry_for_glyph(glyph_span.start, &mut y_pos);
|
||||
let advance = advance_for_shaped_glyph(shape.advance, character, options);
|
||||
(shape.codepoint, advance, shape.offset)
|
||||
};
|
||||
|
||||
let data = GlyphData::new(glyph_id, advance, offset, true, true);
|
||||
glyphs.add_glyph_for_byte_index(byte_idx, character, &data);
|
||||
}
|
||||
} else {
|
||||
// collect all glyphs to be assigned to the first character.
|
||||
let mut datas = vec![];
|
||||
|
||||
for glyph_i in glyph_span.clone() {
|
||||
let shape = glyph_data.entry_for_glyph(glyph_i, &mut y_pos);
|
||||
datas.push(GlyphData::new(
|
||||
shape.codepoint,
|
||||
shape.advance,
|
||||
shape.offset,
|
||||
true, // treat as cluster start
|
||||
glyph_i > glyph_span.start,
|
||||
));
|
||||
// all but first are ligature continuations
|
||||
}
|
||||
// now add the detailed glyph entry.
|
||||
glyphs.add_glyphs_for_byte_index(byte_idx, &datas);
|
||||
}
|
||||
|
||||
glyph_span.start = glyph_span.end;
|
||||
byte_range.start = byte_range.end;
|
||||
}
|
||||
|
||||
// this must be called after adding all glyph data; it sorts the
|
||||
// lookup table for finding detailed glyphs by associated char index.
|
||||
glyphs.finalize_changes();
|
||||
}
|
||||
|
||||
pub fn baseline(&self) -> Option<FontBaseline> {
|
||||
unsafe { (*self.font).table_for_tag(BASE)? };
|
||||
|
||||
let mut hanging_baseline = 0;
|
||||
let mut alphabetic_baseline = 0;
|
||||
let mut ideographic_baseline = 0;
|
||||
|
||||
unsafe {
|
||||
hb_ot_layout_get_baseline(
|
||||
self.hb_font,
|
||||
HB_OT_LAYOUT_BASELINE_TAG_ROMAN,
|
||||
HB_DIRECTION_LTR,
|
||||
HB_OT_TAG_DEFAULT_SCRIPT,
|
||||
HB_OT_TAG_DEFAULT_LANGUAGE,
|
||||
&mut alphabetic_baseline as *mut _,
|
||||
);
|
||||
|
||||
hb_ot_layout_get_baseline(
|
||||
self.hb_font,
|
||||
HB_OT_LAYOUT_BASELINE_TAG_HANGING,
|
||||
HB_DIRECTION_LTR,
|
||||
HB_OT_TAG_DEFAULT_SCRIPT,
|
||||
HB_OT_TAG_DEFAULT_LANGUAGE,
|
||||
&mut hanging_baseline as *mut _,
|
||||
);
|
||||
|
||||
hb_ot_layout_get_baseline(
|
||||
self.hb_font,
|
||||
HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
|
||||
HB_DIRECTION_LTR,
|
||||
HB_OT_TAG_DEFAULT_SCRIPT,
|
||||
HB_OT_TAG_DEFAULT_LANGUAGE,
|
||||
&mut ideographic_baseline as *mut _,
|
||||
);
|
||||
}
|
||||
|
||||
Some(FontBaseline {
|
||||
ideographic_baseline: Shaper::fixed_to_float(ideographic_baseline) as f32,
|
||||
alphabetic_baseline: Shaper::fixed_to_float(alphabetic_baseline) as f32,
|
||||
hanging_baseline: Shaper::fixed_to_float(hanging_baseline) as f32,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Callbacks from Harfbuzz when font map and glyph advance lookup needed.
|
||||
struct FontFuncs(*mut hb_font_funcs_t);
|
||||
|
||||
unsafe impl Sync for FontFuncs {}
|
||||
unsafe impl Send for FontFuncs {}
|
||||
|
||||
static HB_FONT_FUNCS: LazyLock<FontFuncs> = LazyLock::new(|| unsafe {
|
||||
let hb_funcs = hb_font_funcs_create();
|
||||
hb_font_funcs_set_nominal_glyph_func(hb_funcs, Some(glyph_func), ptr::null_mut(), None);
|
||||
hb_font_funcs_set_glyph_h_advance_func(
|
||||
hb_funcs,
|
||||
Some(glyph_h_advance_func),
|
||||
ptr::null_mut(),
|
||||
None,
|
||||
);
|
||||
|
||||
FontFuncs(hb_funcs)
|
||||
});
|
||||
|
||||
extern "C" fn glyph_func(
|
||||
_: *mut hb_font_t,
|
||||
font_data: *mut c_void,
|
||||
unicode: hb_codepoint_t,
|
||||
glyph: *mut hb_codepoint_t,
|
||||
_: *mut c_void,
|
||||
) -> hb_bool_t {
|
||||
let font: *const Font = font_data as *const Font;
|
||||
assert!(!font.is_null());
|
||||
|
||||
match unsafe { (*font).glyph_index(char::from_u32(unicode).unwrap()) } {
|
||||
Some(g) => {
|
||||
unsafe { *glyph = g as hb_codepoint_t };
|
||||
true as hb_bool_t
|
||||
},
|
||||
None => false as hb_bool_t,
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn glyph_h_advance_func(
|
||||
_: *mut hb_font_t,
|
||||
font_data: *mut c_void,
|
||||
glyph: hb_codepoint_t,
|
||||
_: *mut c_void,
|
||||
) -> hb_position_t {
|
||||
let font: *mut Font = font_data as *mut Font;
|
||||
assert!(!font.is_null());
|
||||
|
||||
let advance = unsafe { (*font).glyph_h_advance(glyph as GlyphId) };
|
||||
Shaper::float_to_fixed(advance)
|
||||
}
|
||||
|
||||
/// Callback to get a font table out of a font.
|
||||
extern "C" fn font_table_func(
|
||||
_: *mut hb_face_t,
|
||||
tag: hb_tag_t,
|
||||
user_data: *mut c_void,
|
||||
) -> *mut hb_blob_t {
|
||||
// NB: These asserts have security implications.
|
||||
let font = user_data as *const Font;
|
||||
assert!(!font.is_null());
|
||||
|
||||
// TODO(Issue #197): reuse font table data, which will change the unsound trickery here.
|
||||
let Some(font_table) = (unsafe { (*font).table_for_tag(tag as FontTableTag) }) else {
|
||||
return ptr::null_mut();
|
||||
};
|
||||
|
||||
// `Box::into_raw` intentionally leaks the FontTable so we don't destroy the buffer
|
||||
// while HarfBuzz is using it. When HarfBuzz is done with the buffer, it will pass
|
||||
// this raw pointer back to `destroy_blob_func` which will deallocate the Box.
|
||||
let font_table_ptr = Box::into_raw(Box::new(font_table));
|
||||
|
||||
let buf = unsafe { (*font_table_ptr).buffer() };
|
||||
// HarfBuzz calls `destroy_blob_func` when the buffer is no longer needed.
|
||||
let blob = unsafe {
|
||||
hb_blob_create(
|
||||
buf.as_ptr() as *const c_char,
|
||||
buf.len() as c_uint,
|
||||
HB_MEMORY_MODE_READONLY,
|
||||
font_table_ptr as *mut c_void,
|
||||
Some(destroy_blob_func),
|
||||
)
|
||||
};
|
||||
|
||||
assert!(!blob.is_null());
|
||||
blob
|
||||
}
|
||||
|
||||
extern "C" fn destroy_blob_func(font_table_ptr: *mut c_void) {
|
||||
unsafe {
|
||||
drop(Box::from_raw(font_table_ptr as *mut FontTable));
|
||||
}
|
||||
}
|
||||
404
components/fonts/shapers/harfbuzz.rs
Normal file
404
components/fonts/shapers/harfbuzz.rs
Normal file
@@ -0,0 +1,404 @@
|
||||
/* 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/. */
|
||||
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use std::os::raw::{c_char, c_int, c_uint, c_void};
|
||||
use std::sync::LazyLock;
|
||||
use std::{char, ptr};
|
||||
|
||||
use app_units::Au;
|
||||
use euclid::default::Point2D;
|
||||
// Eventually we would like the shaper to be pluggable, as many operating systems have their own
|
||||
// shapers. For now, however, HarfBuzz is a hard dependency.
|
||||
use harfbuzz_sys::{
|
||||
HB_DIRECTION_LTR, HB_DIRECTION_RTL, HB_MEMORY_MODE_READONLY, HB_OT_LAYOUT_BASELINE_TAG_HANGING,
|
||||
HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT, HB_OT_LAYOUT_BASELINE_TAG_ROMAN,
|
||||
hb_blob_create, hb_blob_t, hb_bool_t, hb_buffer_add_utf8, hb_buffer_create, hb_buffer_destroy,
|
||||
hb_buffer_get_glyph_infos, hb_buffer_get_glyph_positions, hb_buffer_get_length,
|
||||
hb_buffer_set_direction, hb_buffer_set_script, hb_buffer_t, hb_codepoint_t,
|
||||
hb_face_create_for_tables, hb_face_destroy, hb_face_t, hb_feature_t, hb_font_create,
|
||||
hb_font_destroy, hb_font_funcs_create, hb_font_funcs_set_glyph_h_advance_func,
|
||||
hb_font_funcs_set_nominal_glyph_func, hb_font_funcs_t, hb_font_set_funcs, hb_font_set_ppem,
|
||||
hb_font_set_scale, hb_font_t, hb_glyph_info_t, hb_glyph_position_t, hb_ot_layout_get_baseline,
|
||||
hb_position_t, hb_shape, hb_tag_t,
|
||||
};
|
||||
use num_traits::Zero;
|
||||
|
||||
use super::{ShapedGlyphEntry, THarfShapedGlyphData, THarfShaper, unicode_to_hb_script};
|
||||
use crate::platform::font::FontTable;
|
||||
use crate::{
|
||||
BASE, Font, FontBaseline, FontTableMethods, FontTableTag, GlyphId, KERN, LIGA, ShapingFlags,
|
||||
ShapingOptions, fixed_to_float, float_to_fixed, ot_tag,
|
||||
};
|
||||
|
||||
const HB_OT_TAG_DEFAULT_SCRIPT: u32 = ot_tag!('D', 'F', 'L', 'T');
|
||||
const HB_OT_TAG_DEFAULT_LANGUAGE: u32 = ot_tag!('d', 'f', 'l', 't');
|
||||
|
||||
pub struct ShapedGlyphData {
|
||||
count: usize,
|
||||
buffer: *mut hb_buffer_t,
|
||||
glyph_infos: *mut hb_glyph_info_t,
|
||||
pos_infos: *mut hb_glyph_position_t,
|
||||
}
|
||||
|
||||
impl ShapedGlyphData {
|
||||
/// Create a new [`ShapedGlyphData`] from the given HarfBuzz buffer.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// - Passing an invalid buffer pointer to this function results in undefined behavior.
|
||||
/// - This function takes ownership of the buffer and the ShapedGlyphData destroys the buffer when dropped
|
||||
/// so the pointer must an owned pointer and must not be used after being passed to this function
|
||||
pub unsafe fn new(buffer: *mut hb_buffer_t) -> ShapedGlyphData {
|
||||
let mut glyph_count = 0;
|
||||
let glyph_infos = unsafe { hb_buffer_get_glyph_infos(buffer, &mut glyph_count) };
|
||||
assert!(!glyph_infos.is_null());
|
||||
let mut pos_count = 0;
|
||||
let pos_infos = unsafe { hb_buffer_get_glyph_positions(buffer, &mut pos_count) };
|
||||
assert!(!pos_infos.is_null());
|
||||
assert_eq!(glyph_count, pos_count);
|
||||
|
||||
ShapedGlyphData {
|
||||
count: glyph_count as usize,
|
||||
buffer,
|
||||
glyph_infos,
|
||||
pos_infos,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ShapedGlyphData {
|
||||
fn drop(&mut self) {
|
||||
unsafe { hb_buffer_destroy(self.buffer) }
|
||||
}
|
||||
}
|
||||
|
||||
impl THarfShapedGlyphData for ShapedGlyphData {
|
||||
#[inline]
|
||||
fn len(&self) -> usize {
|
||||
self.count
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn byte_offset_of_glyph(&self, i: usize) -> usize {
|
||||
assert!(i < self.count);
|
||||
|
||||
unsafe {
|
||||
let glyph_info_i = self.glyph_infos.add(i);
|
||||
(*glyph_info_i).cluster as usize
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns shaped glyph data for one glyph, and updates the y-position of the pen.
|
||||
fn entry_for_glyph(&self, i: usize, y_pos: &mut Au) -> ShapedGlyphEntry {
|
||||
assert!(i < self.count);
|
||||
|
||||
unsafe {
|
||||
let glyph_info_i = self.glyph_infos.add(i);
|
||||
let pos_info_i = self.pos_infos.add(i);
|
||||
let x_offset = Shaper::fixed_to_float((*pos_info_i).x_offset);
|
||||
let y_offset = Shaper::fixed_to_float((*pos_info_i).y_offset);
|
||||
let x_advance = Shaper::fixed_to_float((*pos_info_i).x_advance);
|
||||
let y_advance = Shaper::fixed_to_float((*pos_info_i).y_advance);
|
||||
|
||||
let x_offset = Au::from_f64_px(x_offset);
|
||||
let y_offset = Au::from_f64_px(y_offset);
|
||||
let x_advance = Au::from_f64_px(x_advance);
|
||||
let y_advance = Au::from_f64_px(y_advance);
|
||||
|
||||
let offset = if x_offset.is_zero() && y_offset.is_zero() && y_advance.is_zero() {
|
||||
None
|
||||
} else {
|
||||
// adjust the pen..
|
||||
if y_advance > Au::zero() {
|
||||
*y_pos -= y_advance;
|
||||
}
|
||||
|
||||
Some(Point2D::new(x_offset, *y_pos - y_offset))
|
||||
};
|
||||
|
||||
ShapedGlyphEntry {
|
||||
codepoint: (*glyph_info_i).codepoint as GlyphId,
|
||||
advance: x_advance,
|
||||
offset,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Shaper {
|
||||
hb_face: *mut hb_face_t,
|
||||
hb_font: *mut hb_font_t,
|
||||
font: *const Font,
|
||||
}
|
||||
|
||||
// The HarfBuzz API is thread safe as well as our `Font`, so we can make the data
|
||||
// structures here as thread-safe as well. This doesn't seem to be documented,
|
||||
// but was expressed as one of the original goals of the HarfBuzz API.
|
||||
unsafe impl Sync for Shaper {}
|
||||
unsafe impl Send for Shaper {}
|
||||
|
||||
impl Drop for Shaper {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
assert!(!self.hb_face.is_null());
|
||||
hb_face_destroy(self.hb_face);
|
||||
|
||||
assert!(!self.hb_font.is_null());
|
||||
hb_font_destroy(self.hb_font);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Shaper {
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)] // Has an unsafe block inside
|
||||
pub fn new(font: &Font) -> Shaper {
|
||||
let font = font as *const Font;
|
||||
unsafe {
|
||||
let hb_face: *mut hb_face_t = hb_face_create_for_tables(
|
||||
Some(font_table_func),
|
||||
font as *const c_void as *mut c_void,
|
||||
None,
|
||||
);
|
||||
let hb_font: *mut hb_font_t = hb_font_create(hb_face);
|
||||
|
||||
// Set points-per-em. if zero, performs no hinting in that direction.
|
||||
let pt_size = (*font).descriptor.pt_size.to_f64_px();
|
||||
hb_font_set_ppem(hb_font, pt_size as c_uint, pt_size as c_uint);
|
||||
|
||||
// Set scaling. Note that this takes 16.16 fixed point.
|
||||
hb_font_set_scale(
|
||||
hb_font,
|
||||
Shaper::float_to_fixed(pt_size) as c_int,
|
||||
Shaper::float_to_fixed(pt_size) as c_int,
|
||||
);
|
||||
|
||||
// configure static function callbacks.
|
||||
hb_font_set_funcs(
|
||||
hb_font,
|
||||
HB_FONT_FUNCS.0,
|
||||
font as *mut Font as *mut c_void,
|
||||
None,
|
||||
);
|
||||
|
||||
Shaper {
|
||||
hb_face,
|
||||
hb_font,
|
||||
font,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn float_to_fixed(f: f64) -> i32 {
|
||||
float_to_fixed(16, f)
|
||||
}
|
||||
|
||||
fn fixed_to_float(i: hb_position_t) -> f64 {
|
||||
fixed_to_float(16, i)
|
||||
}
|
||||
}
|
||||
|
||||
impl THarfShaper for Shaper {
|
||||
type ShapedGlyphData = ShapedGlyphData;
|
||||
|
||||
/// Calculate the layout metrics associated with the given text when painted in a specific font.
|
||||
fn shape_text(&self, text: &str, options: &ShapingOptions) -> Self::ShapedGlyphData {
|
||||
unsafe {
|
||||
let hb_buffer: *mut hb_buffer_t = hb_buffer_create();
|
||||
hb_buffer_set_direction(
|
||||
hb_buffer,
|
||||
if options.flags.contains(ShapingFlags::RTL_FLAG) {
|
||||
HB_DIRECTION_RTL
|
||||
} else {
|
||||
HB_DIRECTION_LTR
|
||||
},
|
||||
);
|
||||
|
||||
hb_buffer_set_script(hb_buffer, unicode_to_hb_script(options.script));
|
||||
|
||||
hb_buffer_add_utf8(
|
||||
hb_buffer,
|
||||
text.as_ptr() as *const c_char,
|
||||
text.len() as c_int,
|
||||
0,
|
||||
text.len() as c_int,
|
||||
);
|
||||
|
||||
let mut features = Vec::new();
|
||||
if options
|
||||
.flags
|
||||
.contains(ShapingFlags::IGNORE_LIGATURES_SHAPING_FLAG)
|
||||
{
|
||||
features.push(hb_feature_t {
|
||||
tag: LIGA,
|
||||
value: 0,
|
||||
start: 0,
|
||||
end: hb_buffer_get_length(hb_buffer),
|
||||
})
|
||||
}
|
||||
if options
|
||||
.flags
|
||||
.contains(ShapingFlags::DISABLE_KERNING_SHAPING_FLAG)
|
||||
{
|
||||
features.push(hb_feature_t {
|
||||
tag: KERN,
|
||||
value: 0,
|
||||
start: 0,
|
||||
end: hb_buffer_get_length(hb_buffer),
|
||||
})
|
||||
}
|
||||
|
||||
hb_shape(
|
||||
self.hb_font,
|
||||
hb_buffer,
|
||||
features.as_mut_ptr(),
|
||||
features.len() as u32,
|
||||
);
|
||||
|
||||
ShapedGlyphData::new(hb_buffer)
|
||||
}
|
||||
}
|
||||
|
||||
fn font(&self) -> &Font {
|
||||
unsafe { &(*self.font) }
|
||||
}
|
||||
|
||||
fn baseline(&self) -> Option<FontBaseline> {
|
||||
unsafe { (*self.font).table_for_tag(BASE)? };
|
||||
|
||||
let mut hanging_baseline = 0;
|
||||
let mut alphabetic_baseline = 0;
|
||||
let mut ideographic_baseline = 0;
|
||||
|
||||
unsafe {
|
||||
hb_ot_layout_get_baseline(
|
||||
self.hb_font,
|
||||
HB_OT_LAYOUT_BASELINE_TAG_ROMAN,
|
||||
HB_DIRECTION_LTR,
|
||||
HB_OT_TAG_DEFAULT_SCRIPT,
|
||||
HB_OT_TAG_DEFAULT_LANGUAGE,
|
||||
&mut alphabetic_baseline as *mut _,
|
||||
);
|
||||
|
||||
hb_ot_layout_get_baseline(
|
||||
self.hb_font,
|
||||
HB_OT_LAYOUT_BASELINE_TAG_HANGING,
|
||||
HB_DIRECTION_LTR,
|
||||
HB_OT_TAG_DEFAULT_SCRIPT,
|
||||
HB_OT_TAG_DEFAULT_LANGUAGE,
|
||||
&mut hanging_baseline as *mut _,
|
||||
);
|
||||
|
||||
hb_ot_layout_get_baseline(
|
||||
self.hb_font,
|
||||
HB_OT_LAYOUT_BASELINE_TAG_IDEO_EMBOX_BOTTOM_OR_LEFT,
|
||||
HB_DIRECTION_LTR,
|
||||
HB_OT_TAG_DEFAULT_SCRIPT,
|
||||
HB_OT_TAG_DEFAULT_LANGUAGE,
|
||||
&mut ideographic_baseline as *mut _,
|
||||
);
|
||||
}
|
||||
|
||||
Some(FontBaseline {
|
||||
ideographic_baseline: Shaper::fixed_to_float(ideographic_baseline) as f32,
|
||||
alphabetic_baseline: Shaper::fixed_to_float(alphabetic_baseline) as f32,
|
||||
hanging_baseline: Shaper::fixed_to_float(hanging_baseline) as f32,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Callbacks from Harfbuzz when font map and glyph advance lookup needed.
|
||||
struct FontFuncs(*mut hb_font_funcs_t);
|
||||
|
||||
unsafe impl Sync for FontFuncs {}
|
||||
unsafe impl Send for FontFuncs {}
|
||||
|
||||
static HB_FONT_FUNCS: LazyLock<FontFuncs> = LazyLock::new(|| unsafe {
|
||||
let hb_funcs = hb_font_funcs_create();
|
||||
hb_font_funcs_set_nominal_glyph_func(hb_funcs, Some(glyph_func), ptr::null_mut(), None);
|
||||
hb_font_funcs_set_glyph_h_advance_func(
|
||||
hb_funcs,
|
||||
Some(glyph_h_advance_func),
|
||||
ptr::null_mut(),
|
||||
None,
|
||||
);
|
||||
|
||||
FontFuncs(hb_funcs)
|
||||
});
|
||||
|
||||
extern "C" fn glyph_func(
|
||||
_: *mut hb_font_t,
|
||||
font_data: *mut c_void,
|
||||
unicode: hb_codepoint_t,
|
||||
glyph: *mut hb_codepoint_t,
|
||||
_: *mut c_void,
|
||||
) -> hb_bool_t {
|
||||
let font: *const Font = font_data as *const Font;
|
||||
assert!(!font.is_null());
|
||||
|
||||
match unsafe { (*font).glyph_index(char::from_u32(unicode).unwrap()) } {
|
||||
Some(g) => {
|
||||
unsafe { *glyph = g as hb_codepoint_t };
|
||||
true as hb_bool_t
|
||||
},
|
||||
None => false as hb_bool_t,
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn glyph_h_advance_func(
|
||||
_: *mut hb_font_t,
|
||||
font_data: *mut c_void,
|
||||
glyph: hb_codepoint_t,
|
||||
_: *mut c_void,
|
||||
) -> hb_position_t {
|
||||
let font: *mut Font = font_data as *mut Font;
|
||||
assert!(!font.is_null());
|
||||
|
||||
let advance = unsafe { (*font).glyph_h_advance(glyph as GlyphId) };
|
||||
Shaper::float_to_fixed(advance)
|
||||
}
|
||||
|
||||
/// Callback to get a font table out of a font.
|
||||
extern "C" fn font_table_func(
|
||||
_: *mut hb_face_t,
|
||||
tag: hb_tag_t,
|
||||
user_data: *mut c_void,
|
||||
) -> *mut hb_blob_t {
|
||||
// NB: These asserts have security implications.
|
||||
let font = user_data as *const Font;
|
||||
assert!(!font.is_null());
|
||||
|
||||
// TODO(Issue #197): reuse font table data, which will change the unsound trickery here.
|
||||
let Some(font_table) = (unsafe { (*font).table_for_tag(tag as FontTableTag) }) else {
|
||||
return ptr::null_mut();
|
||||
};
|
||||
|
||||
// `Box::into_raw` intentionally leaks the FontTable so we don't destroy the buffer
|
||||
// while HarfBuzz is using it. When HarfBuzz is done with the buffer, it will pass
|
||||
// this raw pointer back to `destroy_blob_func` which will deallocate the Box.
|
||||
let font_table_ptr = Box::into_raw(Box::new(font_table));
|
||||
|
||||
let buf = unsafe { (*font_table_ptr).buffer() };
|
||||
// HarfBuzz calls `destroy_blob_func` when the buffer is no longer needed.
|
||||
let blob = unsafe {
|
||||
hb_blob_create(
|
||||
buf.as_ptr() as *const c_char,
|
||||
buf.len() as c_uint,
|
||||
HB_MEMORY_MODE_READONLY,
|
||||
font_table_ptr as *mut c_void,
|
||||
Some(destroy_blob_func),
|
||||
)
|
||||
};
|
||||
|
||||
assert!(!blob.is_null());
|
||||
blob
|
||||
}
|
||||
|
||||
extern "C" fn destroy_blob_func(font_table_ptr: *mut c_void) {
|
||||
unsafe {
|
||||
drop(Box::from_raw(font_table_ptr as *mut FontTable));
|
||||
}
|
||||
}
|
||||
167
components/fonts/shapers/harfrust.rs
Normal file
167
components/fonts/shapers/harfrust.rs
Normal file
@@ -0,0 +1,167 @@
|
||||
/* 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/. */
|
||||
|
||||
use app_units::Au;
|
||||
use euclid::default::Point2D;
|
||||
use harfrust::{
|
||||
Feature, FontRef as HarfRustFontRef, GlyphBuffer, Script, ShaperData, Tag, UnicodeBuffer,
|
||||
};
|
||||
use num_traits::Zero as _;
|
||||
use read_fonts::TableProvider;
|
||||
use smallvec::SmallVec;
|
||||
|
||||
use super::{ShapedGlyphEntry, THarfShapedGlyphData, THarfShaper, unicode_to_hb_script};
|
||||
use crate::{Font, FontData, ShapingFlags};
|
||||
|
||||
pub struct ShapedGlyphData {
|
||||
data: GlyphBuffer,
|
||||
scale: f64,
|
||||
}
|
||||
|
||||
impl THarfShapedGlyphData for ShapedGlyphData {
|
||||
fn len(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
|
||||
fn byte_offset_of_glyph(&self, i: usize) -> usize {
|
||||
self.data.glyph_infos()[i].cluster as usize
|
||||
}
|
||||
|
||||
fn entry_for_glyph(&self, i: usize, y_pos: &mut app_units::Au) -> super::ShapedGlyphEntry {
|
||||
let glyph_info_i = self.data.glyph_infos()[i];
|
||||
let pos_info_i = self.data.glyph_positions()[i];
|
||||
let x_offset = Au::from_f64_px(pos_info_i.x_offset as f64 * self.scale);
|
||||
let y_offset = Au::from_f64_px(pos_info_i.y_offset as f64 * self.scale);
|
||||
let x_advance = Au::from_f64_px(pos_info_i.x_advance as f64 * self.scale);
|
||||
let y_advance = Au::from_f64_px(pos_info_i.y_advance as f64 * self.scale);
|
||||
|
||||
let offset = if x_offset.is_zero() && y_offset.is_zero() && y_advance.is_zero() {
|
||||
None
|
||||
} else {
|
||||
// adjust the pen..
|
||||
if y_advance > Au::zero() {
|
||||
*y_pos -= y_advance;
|
||||
}
|
||||
|
||||
Some(Point2D::new(x_offset, *y_pos - y_offset))
|
||||
};
|
||||
|
||||
ShapedGlyphEntry {
|
||||
codepoint: glyph_info_i.glyph_id,
|
||||
advance: x_advance,
|
||||
offset,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Shaper {
|
||||
font: *const Font,
|
||||
/// The raw byte data of the font
|
||||
font_data: FontData,
|
||||
/// The index of a font in it's collection (.ttc)
|
||||
/// If the font file is not a collection then this is 0
|
||||
font_index: u32,
|
||||
// Used for scaling HarfRust's output
|
||||
scale: f64,
|
||||
ppem: f64,
|
||||
}
|
||||
|
||||
// `Font` and `FontData` are both threadsafe, so we can make the data structures here as thread-safe as well.
|
||||
#[allow(unsafe_code)]
|
||||
unsafe impl Sync for Shaper {}
|
||||
#[allow(unsafe_code)]
|
||||
unsafe impl Send for Shaper {}
|
||||
|
||||
impl Shaper {
|
||||
pub(crate) fn new(font: &Font) -> Self {
|
||||
let raw_font = font.raw_font();
|
||||
let font_data = raw_font.data.clone();
|
||||
let font_index = raw_font.index;
|
||||
// Set points-per-em. if zero, performs no hinting in that direction
|
||||
let ppem = font.descriptor.pt_size.to_f64_px();
|
||||
let font_ref = read_fonts::FontRef::from_index(font_data.as_ref(), font_index).unwrap();
|
||||
let units_per_em = font_ref.head().unwrap().units_per_em();
|
||||
let scale = ppem / (units_per_em as f64);
|
||||
Self {
|
||||
font: font as *const Font,
|
||||
font_data,
|
||||
font_index,
|
||||
scale,
|
||||
ppem,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl THarfShaper for Shaper {
|
||||
type ShapedGlyphData = ShapedGlyphData;
|
||||
|
||||
fn shape_text(&self, text: &str, options: &crate::ShapingOptions) -> ShapedGlyphData {
|
||||
let mut buffer = UnicodeBuffer::new();
|
||||
|
||||
// Set direction
|
||||
buffer.set_direction(if options.flags.contains(ShapingFlags::RTL_FLAG) {
|
||||
harfrust::Direction::RightToLeft
|
||||
} else {
|
||||
harfrust::Direction::LeftToRight
|
||||
});
|
||||
|
||||
// Set script
|
||||
let script =
|
||||
Script::from_iso15924_tag(Tag::from_u32(unicode_to_hb_script(options.script))).unwrap();
|
||||
buffer.set_script(script);
|
||||
|
||||
// Push text
|
||||
buffer.push_str(text);
|
||||
|
||||
// Features
|
||||
let mut features = SmallVec::<[Feature; 2]>::new();
|
||||
if options
|
||||
.flags
|
||||
.contains(ShapingFlags::IGNORE_LIGATURES_SHAPING_FLAG)
|
||||
{
|
||||
features.push(Feature::new(Tag::new(b"liga"), 0, ..));
|
||||
}
|
||||
if options
|
||||
.flags
|
||||
.contains(ShapingFlags::DISABLE_KERNING_SHAPING_FLAG)
|
||||
{
|
||||
features.push(Feature::new(Tag::new(b"kern"), 0, ..));
|
||||
}
|
||||
|
||||
let hr_font =
|
||||
HarfRustFontRef::from_index(self.font_data.as_ref(), self.font_index).unwrap();
|
||||
let shaper_data = ShaperData::new(&hr_font);
|
||||
|
||||
// TODO: handle font variations
|
||||
// let variations: Vec<Variation> = Vec::new();
|
||||
// let instance_data = ShaperInstance::from_variations(&hr_font, &variations);
|
||||
|
||||
let shaper = shaper_data
|
||||
.shaper(&hr_font)
|
||||
// Set the instance
|
||||
// .instance(Some(&instance_data))
|
||||
.build();
|
||||
let glyph_buffer = shaper.shape(buffer, &features);
|
||||
|
||||
ShapedGlyphData {
|
||||
data: glyph_buffer,
|
||||
scale: self.scale,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
fn font(&self) -> &Font {
|
||||
// SAFETY: the font actually owns this shaper so it cannot have been dropped
|
||||
unsafe { &(*self.font) }
|
||||
}
|
||||
|
||||
fn baseline(&self) -> Option<crate::FontBaseline> {
|
||||
// TODO: Implement baseline extraction
|
||||
Some(crate::FontBaseline {
|
||||
ideographic_baseline: self.ppem as f32,
|
||||
alphabetic_baseline: self.ppem as f32,
|
||||
hanging_baseline: self.ppem as f32,
|
||||
})
|
||||
}
|
||||
}
|
||||
245
components/fonts/shapers/mod.rs
Normal file
245
components/fonts/shapers/mod.rs
Normal file
@@ -0,0 +1,245 @@
|
||||
/* 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/. */
|
||||
|
||||
use std::cmp;
|
||||
|
||||
use app_units::Au;
|
||||
use base::text::is_bidi_control;
|
||||
use euclid::default::Point2D;
|
||||
use fonts_traits::ByteIndex;
|
||||
use log::debug;
|
||||
use num_traits::Zero as _;
|
||||
|
||||
use crate::{
|
||||
Font, FontBaseline, GlyphData, GlyphId, GlyphStore, ShapingOptions, advance_for_shaped_glyph,
|
||||
};
|
||||
|
||||
#[cfg(feature = "harfbuzz")]
|
||||
mod harfbuzz;
|
||||
#[cfg(feature = "harfbuzz")]
|
||||
pub use harfbuzz::{ShapedGlyphData, Shaper};
|
||||
|
||||
#[cfg(feature = "harfrust")]
|
||||
mod harfrust;
|
||||
#[cfg(feature = "harfrust")]
|
||||
pub use harfrust::{ShapedGlyphData, Shaper};
|
||||
|
||||
const NO_GLYPH: i32 = -1;
|
||||
|
||||
/// Utility function to convert a `unicode_script::Script` enum into the corresponding `c_uint` tag that
|
||||
/// harfbuzz uses to represent unicode scipts.
|
||||
fn unicode_to_hb_script(script: unicode_script::Script) -> core::ffi::c_uint {
|
||||
let bytes: [u8; 4] = match script {
|
||||
unicode_script::Script::Unknown => *b"Zzzz",
|
||||
_ => {
|
||||
let short_name = script.short_name();
|
||||
short_name.as_bytes().try_into().unwrap()
|
||||
},
|
||||
};
|
||||
|
||||
u32::from_be_bytes(bytes) as core::ffi::c_uint
|
||||
}
|
||||
|
||||
struct ShapedGlyphEntry {
|
||||
codepoint: GlyphId,
|
||||
advance: Au,
|
||||
offset: Option<Point2D<Au>>,
|
||||
}
|
||||
|
||||
/// A servo shaper
|
||||
pub(crate) trait TShaper: Send + Sync {
|
||||
/// Calculate the layout metrics associated with the given text when painted in a specific font.
|
||||
fn shape_text(&self, text: &str, options: &ShapingOptions, glyphs: &mut GlyphStore);
|
||||
fn baseline(&self) -> Option<FontBaseline>;
|
||||
}
|
||||
|
||||
/// Holds the results of shaping. Abstracts over HarfBuzz and HarfRust which return data in very similar
|
||||
/// form but with different types
|
||||
trait THarfShapedGlyphData {
|
||||
/// The number of shaped glyphs
|
||||
fn len(&self) -> usize;
|
||||
/// The byte offset of the shaped glyph in the souce text
|
||||
fn byte_offset_of_glyph(&self, i: usize) -> usize;
|
||||
/// Returns shaped glyph data for one glyph, and updates the y-position of the pen.
|
||||
fn entry_for_glyph(&self, i: usize, y_pos: &mut Au) -> ShapedGlyphEntry;
|
||||
}
|
||||
|
||||
/// A Servo shaper based on HarfBuzz (HarfBuzz or HarfRust)
|
||||
trait THarfShaper: Send + Sync {
|
||||
type ShapedGlyphData: THarfShapedGlyphData;
|
||||
fn shape_text(&self, text: &str, options: &ShapingOptions) -> ShapedGlyphData;
|
||||
fn font(&self) -> &Font;
|
||||
fn baseline(&self) -> Option<FontBaseline>;
|
||||
}
|
||||
|
||||
impl<T: THarfShaper> TShaper for T {
|
||||
fn baseline(&self) -> Option<FontBaseline> {
|
||||
THarfShaper::baseline(self)
|
||||
}
|
||||
|
||||
fn shape_text(&self, text: &str, options: &ShapingOptions, glyphs: &mut GlyphStore) {
|
||||
let glyph_data = self.shape_text(text, options);
|
||||
let glyph_count = glyph_data.len();
|
||||
let byte_max = text.len();
|
||||
|
||||
debug!(
|
||||
"Shaped text[byte count={}], got back {} glyph info records.",
|
||||
byte_max, glyph_count
|
||||
);
|
||||
|
||||
// make map of what chars have glyphs
|
||||
let mut byte_to_glyph = vec![NO_GLYPH; byte_max];
|
||||
|
||||
debug!("(glyph idx) -> (text byte offset)");
|
||||
for i in 0..glyph_data.len() {
|
||||
let loc = glyph_data.byte_offset_of_glyph(i);
|
||||
if loc < byte_max {
|
||||
byte_to_glyph[loc] = i as i32;
|
||||
} else {
|
||||
debug!(
|
||||
"ERROR: tried to set out of range byte_to_glyph: idx={}, glyph idx={}",
|
||||
loc, i
|
||||
);
|
||||
}
|
||||
debug!("{} -> {}", i, loc);
|
||||
}
|
||||
|
||||
debug!("text: {:?}", text);
|
||||
debug!("(char idx): char->(glyph index):");
|
||||
for (i, ch) in text.char_indices() {
|
||||
debug!("{}: {:?} --> {}", i, ch, byte_to_glyph[i]);
|
||||
}
|
||||
|
||||
let mut glyph_span = 0..0;
|
||||
let mut byte_range = 0..0;
|
||||
|
||||
let mut y_pos = Au::zero();
|
||||
|
||||
// main loop over each glyph. each iteration usually processes 1 glyph and 1+ chars.
|
||||
// in cases with complex glyph-character associations, 2+ glyphs and 1+ chars can be
|
||||
// processed.
|
||||
while glyph_span.start < glyph_count {
|
||||
debug!("Processing glyph at idx={}", glyph_span.start);
|
||||
glyph_span.end = glyph_span.start;
|
||||
byte_range.end = glyph_data.byte_offset_of_glyph(glyph_span.start);
|
||||
|
||||
while byte_range.end < byte_max {
|
||||
byte_range.end += 1;
|
||||
// Extend the byte range to include any following byte without its own glyph.
|
||||
while byte_range.end < byte_max && byte_to_glyph[byte_range.end] == NO_GLYPH {
|
||||
byte_range.end += 1;
|
||||
}
|
||||
|
||||
// Extend the glyph range to include all glyphs covered by bytes processed so far.
|
||||
let mut max_glyph_idx = glyph_span.end;
|
||||
for glyph_idx in &byte_to_glyph[byte_range.clone()] {
|
||||
if *glyph_idx != NO_GLYPH {
|
||||
max_glyph_idx = cmp::max(*glyph_idx as usize + 1, max_glyph_idx);
|
||||
}
|
||||
}
|
||||
if max_glyph_idx > glyph_span.end {
|
||||
glyph_span.end = max_glyph_idx;
|
||||
debug!("Extended glyph span to {:?}", glyph_span);
|
||||
}
|
||||
|
||||
// if there's just one glyph, then we don't need further checks.
|
||||
if glyph_span.len() == 1 {
|
||||
break;
|
||||
}
|
||||
|
||||
// if no glyphs were found yet, extend the char byte range more.
|
||||
if glyph_span.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If byte_range now includes all the byte offsets found in glyph_span, then we
|
||||
// have found a contiguous "cluster" and can stop extending it.
|
||||
let mut all_glyphs_are_within_cluster: bool = true;
|
||||
for j in glyph_span.clone() {
|
||||
let loc = glyph_data.byte_offset_of_glyph(j);
|
||||
if !(byte_range.start <= loc && loc < byte_range.end) {
|
||||
all_glyphs_are_within_cluster = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if all_glyphs_are_within_cluster {
|
||||
break;
|
||||
}
|
||||
|
||||
// Otherwise, the bytes we have seen so far correspond to a non-contiguous set of
|
||||
// glyphs. Keep extending byte_range until we fill in all the holes in the glyph
|
||||
// span or reach the end of the text.
|
||||
}
|
||||
|
||||
assert!(!byte_range.is_empty());
|
||||
assert!(!glyph_span.is_empty());
|
||||
|
||||
// Now byte_range is the ligature clump formed by the glyphs in glyph_span.
|
||||
// We will save these glyphs to the glyph store at the index of the first byte.
|
||||
let byte_idx = ByteIndex(byte_range.start as isize);
|
||||
|
||||
if glyph_span.len() == 1 {
|
||||
// Fast path: 1-to-1 mapping of byte offset to single glyph.
|
||||
//
|
||||
// TODO(Issue #214): cluster ranges need to be computed before
|
||||
// shaping, and then consulted here.
|
||||
// for now, just pretend that every character is a cluster start.
|
||||
// (i.e., pretend there are no combining character sequences).
|
||||
// 1-to-1 mapping of character to glyph also treated as ligature start.
|
||||
//
|
||||
// NB: When we acquire the ability to handle ligatures that cross word boundaries,
|
||||
// we'll need to do something special to handle `word-spacing` properly.
|
||||
let character = text[byte_range.clone()].chars().next().unwrap();
|
||||
if is_bidi_control(character) {
|
||||
// Don't add any glyphs for bidi control chars
|
||||
} else {
|
||||
let (glyph_id, advance, offset) = if character == '\t' {
|
||||
// Treat tabs in pre-formatted text as a fixed number of spaces. The glyph id does
|
||||
// not matter here as Servo doesn't render any glyphs for whitespace.
|
||||
//
|
||||
// TODO: Proper tab stops. This should happen in layout and be based on the
|
||||
// size of the space character of the inline formatting context.
|
||||
let font = self.font();
|
||||
(
|
||||
font.glyph_index(' ').unwrap_or(0),
|
||||
font.metrics.space_advance * 8,
|
||||
Default::default(),
|
||||
)
|
||||
} else {
|
||||
let shape = glyph_data.entry_for_glyph(glyph_span.start, &mut y_pos);
|
||||
let advance = advance_for_shaped_glyph(shape.advance, character, options);
|
||||
(shape.codepoint, advance, shape.offset)
|
||||
};
|
||||
|
||||
let data = GlyphData::new(glyph_id, advance, offset, true, true);
|
||||
glyphs.add_glyph_for_byte_index(byte_idx, character, &data);
|
||||
}
|
||||
} else {
|
||||
// collect all glyphs to be assigned to the first character.
|
||||
let mut datas = vec![];
|
||||
|
||||
for glyph_i in glyph_span.clone() {
|
||||
let shape = glyph_data.entry_for_glyph(glyph_i, &mut y_pos);
|
||||
datas.push(GlyphData::new(
|
||||
shape.codepoint,
|
||||
shape.advance,
|
||||
shape.offset,
|
||||
true, // treat as cluster start
|
||||
glyph_i > glyph_span.start,
|
||||
));
|
||||
// all but first are ligature continuations
|
||||
}
|
||||
// now add the detailed glyph entry.
|
||||
glyphs.add_glyphs_for_byte_index(byte_idx, &datas);
|
||||
}
|
||||
|
||||
glyph_span.start = glyph_span.end;
|
||||
byte_range.start = byte_range.end;
|
||||
}
|
||||
|
||||
// this must be called after adding all glyph data; it sorts the
|
||||
// lookup table for finding detailed glyphs by associated char index.
|
||||
glyphs.finalize_changes();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user