Skip to content

Commit ae29d42

Browse files
committed
Removed all references to memmap
1 parent a91a0ad commit ae29d42

File tree

3 files changed

+400
-406
lines changed

3 files changed

+400
-406
lines changed

Cargo.toml

+60-59
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,60 @@
1-
[package]
2-
name = "cosmic-text"
3-
description = "Pure Rust multi-line text handling"
4-
version = "0.9.0"
5-
authors = ["Jeremy Soller <[email protected]>"]
6-
edition = "2021"
7-
license = "MIT OR Apache-2.0"
8-
documentation = "https://docs.rs/cosmic-text/latest/cosmic_text/"
9-
repository = "https://github.com/pop-os/cosmic-text"
10-
11-
[dependencies]
12-
fontdb = { version = "0.14.1", default-features = false }
13-
libm = "0.2.6"
14-
log = "0.4.17"
15-
aliasable = "0.1.3"
16-
rustybuzz = { version = "0.8.0", default-features = false, features = ["libm"] }
17-
swash = { version = "0.1.6", optional = true }
18-
syntect = { version = "5.0.0", optional = true }
19-
sys-locale = { version = "0.3.0", optional = true }
20-
unicode-linebreak = "0.1.4"
21-
unicode-script = "0.5.5"
22-
unicode-segmentation = "1.10.0"
23-
rangemap = "1.2.0"
24-
hashbrown = { version = "0.14.0", optional = true, default-features = false }
25-
rustc-hash = { version = "1.1.0", default-features = false }
26-
27-
[dependencies.unicode-bidi]
28-
version = "0.3.8"
29-
default-features = false
30-
features = ["hardcoded-data"]
31-
32-
[features]
33-
default = ["std", "swash"]
34-
no_std = [
35-
"rustybuzz/libm",
36-
"hashbrown",
37-
]
38-
std = [
39-
"fontdb/std",
40-
"rustybuzz/std",
41-
"sys-locale",
42-
"unicode-bidi/std",
43-
]
44-
vi = ["syntect"]
45-
wasm-web = ["sys-locale?/js"]
46-
warn_on_missing_glyphs = []
47-
fontconfig = ["fontdb/fontconfig"]
48-
49-
[[bench]]
50-
name = "layout"
51-
harness = false
52-
53-
[workspace]
54-
members = [
55-
"examples/*",
56-
]
57-
58-
[dev-dependencies]
59-
criterion = { version = "0.5.1", default-features = false, features = ["cargo_bench_support"] }
1+
[package]
2+
name = "cosmic-text"
3+
description = "Pure Rust multi-line text handling"
4+
version = "0.9.0"
5+
authors = ["Jeremy Soller <[email protected]>"]
6+
edition = "2021"
7+
license = "MIT OR Apache-2.0"
8+
documentation = "https://docs.rs/cosmic-text/latest/cosmic_text/"
9+
repository = "https://github.com/pop-os/cosmic-text"
10+
11+
[dependencies]
12+
fontdb = { version = "0.14.1", default-features = false }
13+
libm = "0.2.6"
14+
log = "0.4.17"
15+
aliasable = "0.1.3"
16+
rustybuzz = { version = "0.8.0", default-features = false, features = ["libm"] }
17+
swash = { version = "0.1.6", optional = true }
18+
syntect = { version = "5.0.0", optional = true }
19+
sys-locale = { version = "0.3.0", optional = true }
20+
unicode-linebreak = "0.1.4"
21+
unicode-script = "0.5.5"
22+
unicode-segmentation = "1.10.0"
23+
rangemap = "1.2.0"
24+
hashbrown = { version = "0.14.0", optional = true, default-features = false }
25+
rustc-hash = { version = "1.1.0", default-features = false }
26+
27+
[dependencies.unicode-bidi]
28+
version = "0.3.8"
29+
default-features = false
30+
features = ["hardcoded-data"]
31+
32+
[features]
33+
default = ["std", "swash"]
34+
no_std = [
35+
"rustybuzz/libm",
36+
"hashbrown",
37+
]
38+
std = [
39+
"fontdb/std",
40+
"fontdb/fs",
41+
"rustybuzz/std",
42+
"sys-locale",
43+
"unicode-bidi/std",
44+
]
45+
vi = ["syntect"]
46+
wasm-web = ["sys-locale?/js"]
47+
warn_on_missing_glyphs = []
48+
fontconfig = ["fontdb/fontconfig"]
49+
50+
[[bench]]
51+
name = "layout"
52+
harness = false
53+
54+
[workspace]
55+
members = [
56+
"examples/*",
57+
]
58+
59+
[dev-dependencies]
60+
criterion = { version = "0.5.1", default-features = false, features = ["cargo_bench_support"] }

src/font/mod.rs

+134-136
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,134 @@
1-
// SPDX-License-Identifier: MIT OR Apache-2.0
2-
pub(crate) mod fallback;
3-
4-
use alloc::boxed::Box;
5-
use alloc::sync::Arc;
6-
7-
pub use self::system::*;
8-
mod system;
9-
10-
pub use font_inner::Font;
11-
12-
/// Encapsulates the self-referencing `Font` struct to ensure all field accesses have to go through
13-
/// safe methods.
14-
mod font_inner {
15-
use super::*;
16-
use aliasable::boxed::AliasableBox;
17-
use core::fmt;
18-
19-
/// A font
20-
//
21-
// # Safety invariant
22-
//
23-
// `data` must never have a mutable reference taken, nor be modified during the lifetime of
24-
// this `Font`.
25-
pub struct Font {
26-
#[cfg(feature = "swash")]
27-
swash: (u32, swash::CacheKey),
28-
rustybuzz: rustybuzz::Face<'static>,
29-
// Note: This field must be after rustybuzz to ensure that it is dropped later. Otherwise
30-
// there would be a dangling reference when dropping rustybuzz.
31-
data: aliasable::boxed::AliasableBox<Arc<dyn AsRef<[u8]> + Send + Sync>>,
32-
id: fontdb::ID,
33-
}
34-
35-
impl fmt::Debug for Font {
36-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37-
f.debug_struct("Font")
38-
.field("id", &self.id)
39-
.finish_non_exhaustive()
40-
}
41-
}
42-
43-
pub(super) struct FontTryBuilder<
44-
RustybuzzBuilder: for<'this> FnOnce(
45-
&'this Arc<dyn AsRef<[u8]> + Send + Sync>,
46-
) -> Option<rustybuzz::Face<'this>>,
47-
> {
48-
pub(super) id: fontdb::ID,
49-
pub(super) data: Arc<dyn AsRef<[u8]> + Send + Sync>,
50-
pub(super) rustybuzz_builder: RustybuzzBuilder,
51-
#[cfg(feature = "swash")]
52-
pub(super) swash: (u32, swash::CacheKey),
53-
}
54-
impl<
55-
RustybuzzBuilder: for<'this> FnOnce(
56-
&'this Arc<dyn AsRef<[u8]> + Send + Sync>,
57-
) -> Option<rustybuzz::Face<'this>>,
58-
> FontTryBuilder<RustybuzzBuilder>
59-
{
60-
pub(super) fn try_build(self) -> Option<Font> {
61-
unsafe fn change_lifetime<'old, 'new: 'old, T: 'new>(data: &'old T) -> &'new T {
62-
&*(data as *const _)
63-
}
64-
65-
let data: AliasableBox<Arc<dyn AsRef<[u8]> + Send + Sync>> =
66-
AliasableBox::from_unique(Box::new(self.data));
67-
68-
// Safety: We use AliasableBox to allow the references in rustybuzz::Face to alias with
69-
// the data stored behind the AliasableBox. In addition the entire public interface of
70-
// Font ensures that no mutable reference is given to data. And finally we use
71-
// for<'this> for the rustybuzz_builder to ensure it can't leak a reference. Combined
72-
// this ensures that it is sound to produce a self-referential type.
73-
let rustybuzz = (self.rustybuzz_builder)(unsafe { change_lifetime(&*data) })?;
74-
75-
Some(Font {
76-
id: self.id,
77-
data,
78-
rustybuzz,
79-
#[cfg(feature = "swash")]
80-
swash: self.swash,
81-
})
82-
}
83-
}
84-
85-
impl Font {
86-
pub fn id(&self) -> fontdb::ID {
87-
self.id
88-
}
89-
90-
pub fn data(&self) -> &[u8] {
91-
// Safety: This only gives an immutable access to `data`.
92-
(**self.data).as_ref()
93-
}
94-
95-
pub fn rustybuzz(&self) -> &rustybuzz::Face<'_> {
96-
&self.rustybuzz
97-
}
98-
99-
#[cfg(feature = "swash")]
100-
pub fn as_swash(&self) -> swash::FontRef<'_> {
101-
let swash = &self.swash;
102-
swash::FontRef {
103-
data: self.data(),
104-
offset: swash.0,
105-
key: swash.1,
106-
}
107-
}
108-
}
109-
}
110-
111-
impl Font {
112-
pub fn new(info: &fontdb::FaceInfo) -> Option<Self> {
113-
#[allow(unused_variables)]
114-
let data = match &info.source {
115-
fontdb::Source::Binary(data) => Arc::clone(data),
116-
#[cfg(feature = "std")]
117-
fontdb::Source::File(path) => {
118-
log::warn!("Unsupported fontdb Source::File('{}')", path.display());
119-
return None;
120-
}
121-
#[cfg(feature = "std")]
122-
fontdb::Source::SharedFile(_path, data) => Arc::clone(data),
123-
};
124-
font_inner::FontTryBuilder {
125-
id: info.id,
126-
#[cfg(feature = "swash")]
127-
swash: {
128-
let swash = swash::FontRef::from_index((*data).as_ref(), info.index as usize)?;
129-
(swash.offset, swash.key)
130-
},
131-
data,
132-
rustybuzz_builder: |data| rustybuzz::Face::from_slice((**data).as_ref(), info.index),
133-
}
134-
.try_build()
135-
}
136-
}
1+
// SPDX-License-Identifier: MIT OR Apache-2.0
2+
pub(crate) mod fallback;
3+
4+
use alloc::boxed::Box;
5+
use alloc::sync::Arc;
6+
7+
pub use self::system::*;
8+
mod system;
9+
10+
pub use font_inner::Font;
11+
12+
/// Encapsulates the self-referencing `Font` struct to ensure all field accesses have to go through
13+
/// safe methods.
14+
mod font_inner {
15+
use super::*;
16+
use aliasable::boxed::AliasableBox;
17+
use core::fmt;
18+
19+
/// A font
20+
//
21+
// # Safety invariant
22+
//
23+
// `data` must never have a mutable reference taken, nor be modified during the lifetime of
24+
// this `Font`.
25+
pub struct Font {
26+
#[cfg(feature = "swash")]
27+
swash: (u32, swash::CacheKey),
28+
rustybuzz: rustybuzz::Face<'static>,
29+
// Note: This field must be after rustybuzz to ensure that it is dropped later. Otherwise
30+
// there would be a dangling reference when dropping rustybuzz.
31+
data: aliasable::boxed::AliasableBox<Arc<dyn AsRef<[u8]> + Send + Sync>>,
32+
id: fontdb::ID,
33+
}
34+
35+
impl fmt::Debug for Font {
36+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37+
f.debug_struct("Font")
38+
.field("id", &self.id)
39+
.finish_non_exhaustive()
40+
}
41+
}
42+
43+
pub(super) struct FontTryBuilder<
44+
RustybuzzBuilder: for<'this> FnOnce(
45+
&'this Arc<dyn AsRef<[u8]> + Send + Sync>,
46+
) -> Option<rustybuzz::Face<'this>>,
47+
> {
48+
pub(super) id: fontdb::ID,
49+
pub(super) data: Arc<dyn AsRef<[u8]> + Send + Sync>,
50+
pub(super) rustybuzz_builder: RustybuzzBuilder,
51+
#[cfg(feature = "swash")]
52+
pub(super) swash: (u32, swash::CacheKey),
53+
}
54+
impl<
55+
RustybuzzBuilder: for<'this> FnOnce(
56+
&'this Arc<dyn AsRef<[u8]> + Send + Sync>,
57+
) -> Option<rustybuzz::Face<'this>>,
58+
> FontTryBuilder<RustybuzzBuilder>
59+
{
60+
pub(super) fn try_build(self) -> Option<Font> {
61+
unsafe fn change_lifetime<'old, 'new: 'old, T: 'new>(data: &'old T) -> &'new T {
62+
&*(data as *const _)
63+
}
64+
65+
let data: AliasableBox<Arc<dyn AsRef<[u8]> + Send + Sync>> =
66+
AliasableBox::from_unique(Box::new(self.data));
67+
68+
// Safety: We use AliasableBox to allow the references in rustybuzz::Face to alias with
69+
// the data stored behind the AliasableBox. In addition the entire public interface of
70+
// Font ensures that no mutable reference is given to data. And finally we use
71+
// for<'this> for the rustybuzz_builder to ensure it can't leak a reference. Combined
72+
// this ensures that it is sound to produce a self-referential type.
73+
let rustybuzz = (self.rustybuzz_builder)(unsafe { change_lifetime(&*data) })?;
74+
75+
Some(Font {
76+
id: self.id,
77+
data,
78+
rustybuzz,
79+
#[cfg(feature = "swash")]
80+
swash: self.swash,
81+
})
82+
}
83+
}
84+
85+
impl Font {
86+
pub fn id(&self) -> fontdb::ID {
87+
self.id
88+
}
89+
90+
pub fn data(&self) -> &[u8] {
91+
// Safety: This only gives an immutable access to `data`.
92+
(**self.data).as_ref()
93+
}
94+
95+
pub fn rustybuzz(&self) -> &rustybuzz::Face<'_> {
96+
&self.rustybuzz
97+
}
98+
99+
#[cfg(feature = "swash")]
100+
pub fn as_swash(&self) -> swash::FontRef<'_> {
101+
let swash = &self.swash;
102+
swash::FontRef {
103+
data: self.data(),
104+
offset: swash.0,
105+
key: swash.1,
106+
}
107+
}
108+
}
109+
}
110+
111+
impl Font {
112+
pub fn new(info: &fontdb::FaceInfo) -> Option<Self> {
113+
#[allow(unused_variables)]
114+
let data = match &info.source {
115+
fontdb::Source::Binary(data) => Arc::clone(data),
116+
#[cfg(feature = "std")]
117+
fontdb::Source::File(path) => {
118+
log::warn!("Unsupported fontdb Source::File('{}')", path.display());
119+
return None;
120+
}
121+
};
122+
font_inner::FontTryBuilder {
123+
id: info.id,
124+
#[cfg(feature = "swash")]
125+
swash: {
126+
let swash = swash::FontRef::from_index((*data).as_ref(), info.index as usize)?;
127+
(swash.offset, swash.key)
128+
},
129+
data,
130+
rustybuzz_builder: |data| rustybuzz::Face::from_slice((**data).as_ref(), info.index),
131+
}
132+
.try_build()
133+
}
134+
}

0 commit comments

Comments
 (0)