Skip to content

Commit f206811

Browse files
committed
next attempt
1 parent 0f85a14 commit f206811

File tree

5 files changed

+106
-40
lines changed

5 files changed

+106
-40
lines changed

krilla-macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use quote::{format_ident, quote};
33
use sitro::Renderer;
44
use syn::parse::{Parse, ParseStream};
55
use syn::punctuated::Punctuated;
6-
use syn::{parse_macro_input, parse_quote, Ident, ItemFn, Token};
6+
use syn::{parse_macro_input, Ident, ItemFn, Token};
77

88
struct AttributeInput {
99
identifiers: Punctuated<Ident, Token![,]>,

src/font/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,6 @@ fn draw(font_data: Arc<Vec<u8>>, glyphs: Option<Vec<(GlyphId, String)>>, name: &
387387
col as f32 * size as f32,
388388
(row + 1) as f32 * size as f32,
389389
)
390-
// .pre_concat(Transform::from_scale(
391-
// size as f32 / units_per_em,
392-
// size as f32 / units_per_em,
393-
// ))
394390
}
395391

396392
surface.push_transform(&get_transform(cur_point, size, num_cols, units_per_em));
@@ -402,7 +398,6 @@ fn draw(font_data: Arc<Vec<u8>>, glyphs: Option<Vec<(GlyphId, String)>>, name: &
402398
font.clone(),
403399
&text,
404400
);
405-
// let res = single_glyph(&font, GlyphId::new(i), &mut builder);
406401
surface.pop();
407402

408403
cur_point += size;

src/font/svg.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,3 @@ pub fn draw_glyph(
4848

4949
None
5050
}
51-
52-
#[cfg(test)]
53-
mod tests {
54-
use crate::font::draw;
55-
use std::sync::Arc;
56-
57-
#[ignore]
58-
#[test]
59-
fn svg_twitter() {
60-
let font_data = std::fs::read("/Library/Fonts/TwitterColorEmoji-SVGinOT.ttf").unwrap();
61-
draw(Arc::new(font_data), None, "svg_twitter");
62-
}
63-
}

src/tests/manual.rs

Lines changed: 94 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,45 @@ use crate::tests::{
1313
use crate::util::SliceExt;
1414
use crate::Fill;
1515
use rustybuzz::Direction;
16-
use skrifa::instance::Location;
17-
use skrifa::GlyphId;
16+
use skrifa::instance::{Location, LocationRef, Size};
17+
use skrifa::{GlyphId, MetadataProvider};
1818
use std::sync::Arc;
19+
use skrifa::raw::TableProvider;
1920

21+
#[ignore]
2022
#[test]
2123
fn simple_shape_demo() {
2224
let mut y = 25.0;
2325

2426
let data = vec![
2527
(
26-
NOTO_SANS_ARABIC,
28+
NOTO_SANS_ARABIC.clone(),
2729
"هذا نص أطول لتجربة القدرات.",
2830
Direction::RightToLeft,
2931
14.0,
3032
),
3133
(
32-
NOTO_SANS,
34+
NOTO_SANS.clone(),
3335
"Hi there, this is a very simple test!",
3436
Direction::LeftToRight,
3537
14.0,
3638
),
3739
(
38-
DEJAVU_SANS_MONO,
40+
DEJAVU_SANS_MONO.clone(),
3941
"Here with a mono font, some longer text.",
4042
Direction::LeftToRight,
4143
16.0,
4244
),
43-
(NOTO_SANS, "z͈̤̭͖̉͑́a̳ͫ́̇͑̽͒ͯlͨ͗̍̀̍̔̀ģ͔̫̫̄o̗̠͔̦̳͆̏̓͢", Direction::LeftToRight, 14.0),
44-
(NOTO_SANS, " birth\u{ad}day ", Direction::LeftToRight, 14.0),
45+
(NOTO_SANS.clone(), "z͈̤̭͖̉͑́a̳ͫ́̇͑̽͒ͯlͨ͗̍̀̍̔̀ģ͔̫̫̄o̗̠͔̦̳͆̏̓͢", Direction::LeftToRight, 14.0),
46+
(NOTO_SANS.clone(), " birth\u{ad}day ", Direction::LeftToRight, 14.0),
4547
(
46-
NOTO_SANS_CJK,
48+
NOTO_SANS_CJK.clone(),
4749
"你好世界,这是一段很长的测试文章",
4850
Direction::LeftToRight,
4951
14.0,
5052
),
5153
(
52-
NOTO_SANS_DEVANAGARI,
54+
NOTO_SANS_DEVANAGARI.clone(),
5355
"आ रु॒क्मैरा यु॒धा नर॑ ऋ॒ष्वा ऋ॒ष्टीर॑सृक्षत ।",
5456
Direction::LeftToRight,
5557
14.0,
@@ -137,3 +139,86 @@ fn cosmic_text_integration() {
137139
let pdf = document_builder.finish();
138140
write_manual_to_store("cosmic_text", &pdf);
139141
}
142+
143+
#[ignore]
144+
#[test]
145+
fn svg_twitter() {
146+
let font_data = std::fs::read("/Library/Fonts/TwitterColorEmoji-SVGinOT.ttf").unwrap();
147+
all_glyphs_to_pdf(Arc::new(font_data), None, "svg_twitter");
148+
}
149+
150+
fn all_glyphs_to_pdf(font_data: Arc<Vec<u8>>, glyphs: Option<Vec<(GlyphId, String)>>, name: &str) {
151+
use crate::document::Document;
152+
use crate::object::color_space::rgb::Rgb;
153+
use crate::serialize::SerializeSettings;
154+
use crate::stream::Glyph;
155+
use crate::Transform;
156+
157+
let font = Font::new(font_data, 0, Location::default()).unwrap();
158+
let font_ref = font.font_ref();
159+
160+
let glyphs = glyphs.unwrap_or_else(|| {
161+
let file =
162+
std::fs::read("/Users/lstampfl/Programming/GitHub/krilla/src/font/emojis.txt").unwrap();
163+
let file = std::str::from_utf8(&file).unwrap();
164+
file.chars()
165+
.filter_map(|c| {
166+
font_ref
167+
.cmap()
168+
.unwrap()
169+
.map_codepoint(c)
170+
.map(|g| (g, c.to_string()))
171+
})
172+
.collect::<Vec<_>>()
173+
});
174+
175+
let metrics = font_ref.metrics(Size::unscaled(), LocationRef::default());
176+
let num_glyphs = glyphs.len();
177+
let width = 400;
178+
179+
let size = 40u32;
180+
let num_cols = width / size;
181+
let height = (num_glyphs as f32 / num_cols as f32).ceil() as u32 * size;
182+
let units_per_em = metrics.units_per_em as f32;
183+
let mut cur_point = 0;
184+
185+
let page_size = tiny_skia_path::Size::from_wh(width as f32, height as f32).unwrap();
186+
let mut document_builder = Document::new(SerializeSettings::default());
187+
let mut builder = document_builder.start_page(page_size);
188+
let mut surface = builder.surface();
189+
190+
for (i, text) in glyphs.iter().cloned() {
191+
fn get_transform(cur_point: u32, size: u32, num_cols: u32, _: f32) -> Transform {
192+
let el = cur_point / size;
193+
let col = el % num_cols;
194+
let row = el / num_cols;
195+
196+
Transform::from_row(
197+
1.0,
198+
0.0,
199+
0.0,
200+
1.0,
201+
col as f32 * size as f32,
202+
(row + 1) as f32 * size as f32,
203+
)
204+
}
205+
206+
surface.push_transform(&get_transform(cur_point, size, num_cols, units_per_em));
207+
surface.draw_glyph_run(
208+
0.0,
209+
0.0,
210+
crate::Fill::<Rgb>::default(),
211+
&[Glyph::new(i, 0.0, 0.0, 0.0, 0..text.len(), size as f32)],
212+
font.clone(),
213+
&text,
214+
);
215+
surface.pop();
216+
217+
cur_point += size;
218+
}
219+
220+
surface.finish();
221+
builder.finish();
222+
let pdf = document_builder.finish();
223+
write_manual_to_store(name, &pdf);
224+
}

src/tests/mod.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ use sitro::{
88
render_quartz, RenderOptions, RenderedDocument, RenderedPage, Renderer,
99
};
1010
use skrifa::GlyphId;
11-
use std::cell::LazyCell;
1211
use std::cmp::max;
1312
use std::path::PathBuf;
14-
use std::sync::Arc;
13+
use std::sync::{Arc, LazyLock};
1514
use oxipng::{InFile, OutFile};
1615
use tiny_skia_path::{Path, PathBuilder, Rect};
1716

@@ -21,39 +20,39 @@ mod visreg;
2120
const REPLACE: bool = true;
2221
const STORE: bool = true;
2322

24-
const SNAPSHOT_PATH: LazyCell<PathBuf> = LazyCell::new(|| {
23+
static SNAPSHOT_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
2524
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/snapshots");
2625
let _ = std::fs::create_dir_all(&path);
2726
path
2827
});
2928

30-
const REFS_PATH: LazyCell<PathBuf> = LazyCell::new(|| {
29+
static REFS_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
3130
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/refs");
3231
let _ = std::fs::create_dir_all(&path);
3332
path
3433
});
3534

36-
const DIFFS_PATH: LazyCell<PathBuf> = LazyCell::new(|| {
35+
static DIFFS_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
3736
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/diffs");
3837
let _ = std::fs::remove_dir_all(&path);
39-
let _ = std::fs::create_dir_all(&path).unwrap();
38+
let _ = std::fs::create_dir_all(&path);
4039
path
4140
});
4241

43-
const STORE_PATH: LazyCell<PathBuf> = LazyCell::new(|| {
42+
static STORE_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
4443
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/store");
4544
let _ = std::fs::remove_dir_all(&path);
46-
let _ = std::fs::create_dir_all(&path).unwrap();
45+
let _ = std::fs::create_dir_all(&path);
4746
path
4847
});
4948

50-
const FONT_PATH: LazyCell<PathBuf> =
51-
LazyCell::new(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fonts"));
49+
static FONT_PATH: LazyLock<PathBuf> =
50+
LazyLock::new(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fonts"));
5251

5352
macro_rules! lazy_font {
5453
($name:ident, $path:expr) => {
55-
pub const $name: LazyCell<Arc<Vec<u8>>> =
56-
LazyCell::new(|| Arc::new(std::fs::read($path).unwrap()));
54+
pub static $name: LazyLock<Arc<Vec<u8>>> =
55+
LazyLock::new(|| Arc::new(std::fs::read($path).unwrap()));
5756
};
5857
}
5958

0 commit comments

Comments
 (0)