blitz-pdf 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +23 -6
  2. package/THIRD_PARTY_NOTICES.md +18 -0
  3. package/dist/types.d.ts +2 -1
  4. package/dist/types.d.ts.map +1 -1
  5. package/licenses/Noto-OFL.txt +94 -0
  6. package/licenses/NotoCJK-OFL.txt +92 -0
  7. package/licenses/NotoEmoji-OFL.txt +93 -0
  8. package/licenses/Unifont-OFL.txt +90 -0
  9. package/licenses/font-sources.json +106 -0
  10. package/package.json +1 -1
  11. package/src/types.ts +2 -1
  12. package/vendor/README.md +19 -0
  13. package/vendor/fontique/Cargo.toml +194 -0
  14. package/vendor/fontique/LICENSE-APACHE +201 -0
  15. package/vendor/fontique/LICENSE-MIT +25 -0
  16. package/vendor/fontique/README.md +54 -0
  17. package/vendor/fontique/examples/generic_families.rs +37 -0
  18. package/vendor/fontique/src/attributes.rs +42 -0
  19. package/vendor/fontique/src/backend/android.rs +218 -0
  20. package/vendor/fontique/src/backend/coretext.rs +206 -0
  21. package/vendor/fontique/src/backend/dwrite.rs +395 -0
  22. package/vendor/fontique/src/backend/fontconfig.rs +844 -0
  23. package/vendor/fontique/src/backend/mod.rs +75 -0
  24. package/vendor/fontique/src/charmap.rs +127 -0
  25. package/vendor/fontique/src/collection/mod.rs +1016 -0
  26. package/vendor/fontique/src/collection/query.rs +392 -0
  27. package/vendor/fontique/src/fallback.rs +293 -0
  28. package/vendor/fontique/src/family.rs +111 -0
  29. package/vendor/fontique/src/family_name.rs +153 -0
  30. package/vendor/fontique/src/font.rs +562 -0
  31. package/vendor/fontique/src/generic.rs +36 -0
  32. package/vendor/fontique/src/impl_fontconfig.rs +159 -0
  33. package/vendor/fontique/src/lib.rs +87 -0
  34. package/vendor/fontique/src/matching.rs +483 -0
  35. package/vendor/fontique/src/scan.rs +312 -0
  36. package/vendor/fontique/src/script.rs +271 -0
  37. package/vendor/fontique/src/source.rs +108 -0
  38. package/vendor/fontique/src/source_cache.rs +254 -0
  39. package/wasm/blitz_pdf_bg.wasm +0 -0
@@ -0,0 +1,111 @@
1
+ // Copyright 2024 the Parley Authors
2
+ // SPDX-License-Identifier: Apache-2.0 OR MIT
3
+
4
+ //! Model for font families.
5
+
6
+ use super::{FontStyle, FontWeight, FontWidth, family_name::FamilyName, font::FontInfo};
7
+ use crate::AtomicCounter;
8
+ use alloc::sync::Arc;
9
+ use core::sync::atomic::Ordering;
10
+ use smallvec::SmallVec;
11
+
12
+ /// Unique identifier for a font family.
13
+ #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
14
+ #[repr(transparent)]
15
+ pub struct FamilyId(u64);
16
+
17
+ impl FamilyId {
18
+ /// Creates a new unique identifier.
19
+ #[allow(clippy::new_without_default)]
20
+ pub fn new() -> Self {
21
+ /// Make sure this is larger than the largest generic family id.
22
+ static ID_COUNTER: AtomicCounter = AtomicCounter::new(64);
23
+ #[allow(clippy::useless_conversion, reason = "Not useless on 32-bit platforms")]
24
+ Self(ID_COUNTER.fetch_add(1, Ordering::Relaxed).into())
25
+ }
26
+
27
+ /// Returns the underlying integer value.
28
+ pub fn to_u64(self) -> u64 {
29
+ self.0
30
+ }
31
+ }
32
+
33
+ /// Named set of fonts that are instances of a core design.
34
+ #[derive(Clone, Debug)]
35
+ pub struct FamilyInfo(Arc<FamilyInner>);
36
+
37
+ impl FamilyInfo {
38
+ /// Creates a new font family object with the given name and collection of
39
+ /// fonts.
40
+ pub fn new(name: FamilyName, fonts: impl IntoIterator<Item = FontInfo>) -> Self {
41
+ let fonts: SmallVec<[FontInfo; 4]> = fonts.into_iter().collect();
42
+ let default_font = super::matching::match_font(
43
+ &fonts[..],
44
+ FontWidth::default(),
45
+ FontStyle::default(),
46
+ FontWeight::default(),
47
+ false,
48
+ )
49
+ .unwrap_or(0);
50
+ Self(Arc::new(FamilyInner {
51
+ name,
52
+ default_font,
53
+ fonts,
54
+ }))
55
+ }
56
+
57
+ /// Returns the unique identifier for the family.
58
+ pub fn id(&self) -> FamilyId {
59
+ self.0.name.id()
60
+ }
61
+
62
+ /// Returns the name of the family.
63
+ pub fn name(&self) -> &str {
64
+ self.0.name.name()
65
+ }
66
+
67
+ /// Returns the collection of fonts that are members of the family.
68
+ pub fn fonts(&self) -> &[FontInfo] {
69
+ &self.0.fonts
70
+ }
71
+
72
+ /// Returns index of the default font of the family.
73
+ pub fn default_font_index(&self) -> usize {
74
+ self.0.default_font
75
+ }
76
+
77
+ /// Returns the default font of the family.
78
+ pub fn default_font(&self) -> Option<&FontInfo> {
79
+ self.0.fonts.get(self.0.default_font)
80
+ }
81
+
82
+ /// Returns the index of the best font from the family for the given attributes.
83
+ pub fn match_index(
84
+ &self,
85
+ width: FontWidth,
86
+ style: FontStyle,
87
+ weight: FontWeight,
88
+ synthesize_style: bool,
89
+ ) -> Option<usize> {
90
+ super::matching::match_font(self.fonts(), width, style, weight, synthesize_style)
91
+ }
92
+
93
+ /// Selects the best font from the family for the given attributes.
94
+ pub fn match_font(
95
+ &self,
96
+ width: FontWidth,
97
+ style: FontStyle,
98
+ weight: FontWeight,
99
+ synthesize_style: bool,
100
+ ) -> Option<&FontInfo> {
101
+ self.fonts()
102
+ .get(self.match_index(width, style, weight, synthesize_style)?)
103
+ }
104
+ }
105
+
106
+ #[derive(Clone, Debug)]
107
+ struct FamilyInner {
108
+ pub(crate) name: FamilyName,
109
+ default_font: usize,
110
+ fonts: SmallVec<[FontInfo; 4]>,
111
+ }
@@ -0,0 +1,153 @@
1
+ // Copyright 2024 the Parley Authors
2
+ // SPDX-License-Identifier: Apache-2.0 OR MIT
3
+
4
+ //! Model for font family names.
5
+
6
+ use super::family::FamilyId;
7
+ use alloc::sync::Arc;
8
+ use hashbrown::HashMap;
9
+ use smallvec::SmallVec;
10
+
11
+ /// Handle for a font family that includes both the name and a unique
12
+ /// identifier.
13
+ #[derive(Clone, Debug)]
14
+ pub struct FamilyName {
15
+ id: FamilyId,
16
+ name: Arc<str>,
17
+ }
18
+
19
+ impl FamilyName {
20
+ /// Returns the unique identifier for the font family.
21
+ pub fn id(&self) -> FamilyId {
22
+ self.id
23
+ }
24
+
25
+ /// Returns the name of the font family.
26
+ pub fn name(&self) -> &str {
27
+ &self.name
28
+ }
29
+ }
30
+
31
+ impl From<&FamilyName> for FamilyId {
32
+ fn from(value: &FamilyName) -> Self {
33
+ value.id
34
+ }
35
+ }
36
+
37
+ /// Bidirectional map that associates font family names with unique
38
+ /// identifiers.
39
+ #[derive(Clone, Default)]
40
+ pub struct FamilyNameMap {
41
+ name_map: HashMap<Arc<[u8]>, FamilyName>,
42
+ id_map: HashMap<FamilyId, FamilyName>,
43
+ }
44
+
45
+ impl FamilyNameMap {
46
+ /// Returns the family name object for the given name.
47
+ pub fn get(&self, name: &str) -> Option<&FamilyName> {
48
+ let key = NameKey::from_str(name);
49
+ self.name_map.get(key.as_bytes())
50
+ }
51
+
52
+ /// Returns the family name object for the given identifier.
53
+ pub fn get_by_id(&self, id: FamilyId) -> Option<&FamilyName> {
54
+ self.id_map.get(&id)
55
+ }
56
+
57
+ /// Returns the family name object with the given name or creates
58
+ /// a new one if it doesn't exist.
59
+ pub fn get_or_insert(&mut self, name: &str) -> FamilyName {
60
+ let key = NameKey::from_str(name);
61
+ if let Some(name) = self.name_map.get(key.as_bytes()) {
62
+ name.clone()
63
+ } else {
64
+ let new_name = FamilyName {
65
+ name: name.into(),
66
+ id: FamilyId::new(),
67
+ };
68
+ self.name_map
69
+ .insert(key.as_bytes().into(), new_name.clone());
70
+ self.id_map.insert(new_name.id, new_name.clone());
71
+ new_name
72
+ }
73
+ }
74
+
75
+ /// Adds `name` as an alias for the given family identifier.
76
+ #[allow(unused)]
77
+ pub fn add_alias(&mut self, id: FamilyId, name: &str) {
78
+ if self.id_map.contains_key(&id) {
79
+ let key = NameKey::from_str(name);
80
+ if self.name_map.contains_key(key.as_bytes()) {
81
+ return;
82
+ }
83
+ let new_name = FamilyName {
84
+ name: name.into(),
85
+ id,
86
+ };
87
+ self.name_map.insert(key.as_bytes().into(), new_name);
88
+ }
89
+ }
90
+
91
+ /// Returns an iterator over all of the font family names, including aliases.
92
+ pub fn iter(&self) -> impl Iterator<Item = &FamilyName> + Clone {
93
+ self.name_map.values()
94
+ }
95
+
96
+ /// Returns an iterator over all of the font family identifiers.
97
+ pub fn ids(&self) -> impl Iterator<Item = FamilyId> + Clone + '_ {
98
+ self.id_map.keys().copied()
99
+ }
100
+ }
101
+
102
+ /// Key for case-insensitive lookup of family names.
103
+ #[derive(Default)]
104
+ struct NameKey {
105
+ data: SmallVec<[u8; 128]>,
106
+ }
107
+
108
+ impl NameKey {
109
+ fn from_str(s: &str) -> Self {
110
+ let mut res = Self::default();
111
+ let mut buf = [0_u8; 4];
112
+ for ch in s.chars() {
113
+ for ch in ch.to_lowercase() {
114
+ res.data
115
+ .extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
116
+ }
117
+ }
118
+ res
119
+ }
120
+
121
+ pub fn as_bytes(&self) -> &[u8] {
122
+ &self.data
123
+ }
124
+ }
125
+
126
+ #[cfg(test)]
127
+ mod tests {
128
+ use super::FamilyNameMap;
129
+ use alloc::vec;
130
+ use alloc::vec::Vec;
131
+
132
+ #[test]
133
+ fn iterate_names_and_ids() {
134
+ let mut map = FamilyNameMap::default();
135
+ let name = map.get_or_insert("Family");
136
+ map.add_alias(name.id(), "Alias");
137
+ let other_name = map.get_or_insert("Other family");
138
+
139
+ let mut names: Vec<_> = map.iter().map(|f| f.name()).collect();
140
+ names.sort_unstable();
141
+ assert_eq!(
142
+ names,
143
+ &["Alias", "Family", "Other family"],
144
+ "family names and aliases should both show up"
145
+ );
146
+
147
+ let mut ids: Vec<_> = map.ids().collect();
148
+ ids.sort_unstable();
149
+ let mut expected = vec![name.id(), other_name.id()];
150
+ expected.sort_unstable();
151
+ assert_eq!(ids, expected, "family identifiers should only show up once");
152
+ }
153
+ }