Skip to content

Commit

Permalink
next attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Aug 29, 2024
1 parent 0f85a14 commit f206811
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 40 deletions.
2 changes: 1 addition & 1 deletion krilla-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use quote::{format_ident, quote};
use sitro::Renderer;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::{parse_macro_input, parse_quote, Ident, ItemFn, Token};
use syn::{parse_macro_input, Ident, ItemFn, Token};

struct AttributeInput {
identifiers: Punctuated<Ident, Token![,]>,
Expand Down
5 changes: 0 additions & 5 deletions src/font/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,6 @@ fn draw(font_data: Arc<Vec<u8>>, glyphs: Option<Vec<(GlyphId, String)>>, name: &
col as f32 * size as f32,
(row + 1) as f32 * size as f32,
)
// .pre_concat(Transform::from_scale(
// size as f32 / units_per_em,
// size as f32 / units_per_em,
// ))
}

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

cur_point += size;
Expand Down
13 changes: 0 additions & 13 deletions src/font/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,3 @@ pub fn draw_glyph(

None
}

#[cfg(test)]
mod tests {
use crate::font::draw;
use std::sync::Arc;

#[ignore]
#[test]
fn svg_twitter() {
let font_data = std::fs::read("/Library/Fonts/TwitterColorEmoji-SVGinOT.ttf").unwrap();
draw(Arc::new(font_data), None, "svg_twitter");
}
}
103 changes: 94 additions & 9 deletions src/tests/manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,45 @@ use crate::tests::{
use crate::util::SliceExt;
use crate::Fill;
use rustybuzz::Direction;
use skrifa::instance::Location;
use skrifa::GlyphId;
use skrifa::instance::{Location, LocationRef, Size};
use skrifa::{GlyphId, MetadataProvider};
use std::sync::Arc;
use skrifa::raw::TableProvider;

#[ignore]
#[test]
fn simple_shape_demo() {
let mut y = 25.0;

let data = vec![
(
NOTO_SANS_ARABIC,
NOTO_SANS_ARABIC.clone(),
"هذا نص أطول لتجربة القدرات.",
Direction::RightToLeft,
14.0,
),
(
NOTO_SANS,
NOTO_SANS.clone(),
"Hi there, this is a very simple test!",
Direction::LeftToRight,
14.0,
),
(
DEJAVU_SANS_MONO,
DEJAVU_SANS_MONO.clone(),
"Here with a mono font, some longer text.",
Direction::LeftToRight,
16.0,
),
(NOTO_SANS, "z͈̤̭͖̉͑́a̳ͫ́̇͑̽͒ͯlͨ͗̍̀̍̔̀ģ͔̫̫̄o̗̠͔̦̳͆̏̓͢", Direction::LeftToRight, 14.0),
(NOTO_SANS, " birth\u{ad}day ", Direction::LeftToRight, 14.0),
(NOTO_SANS.clone(), "z͈̤̭͖̉͑́a̳ͫ́̇͑̽͒ͯlͨ͗̍̀̍̔̀ģ͔̫̫̄o̗̠͔̦̳͆̏̓͢", Direction::LeftToRight, 14.0),
(NOTO_SANS.clone(), " birth\u{ad}day ", Direction::LeftToRight, 14.0),
(
NOTO_SANS_CJK,
NOTO_SANS_CJK.clone(),
"你好世界,这是一段很长的测试文章",
Direction::LeftToRight,
14.0,
),
(
NOTO_SANS_DEVANAGARI,
NOTO_SANS_DEVANAGARI.clone(),
"आ रु॒क्मैरा यु॒धा नर॑ ऋ॒ष्वा ऋ॒ष्टीर॑सृक्षत ।",
Direction::LeftToRight,
14.0,
Expand Down Expand Up @@ -137,3 +139,86 @@ fn cosmic_text_integration() {
let pdf = document_builder.finish();
write_manual_to_store("cosmic_text", &pdf);
}

#[ignore]
#[test]
fn svg_twitter() {
let font_data = std::fs::read("/Library/Fonts/TwitterColorEmoji-SVGinOT.ttf").unwrap();
all_glyphs_to_pdf(Arc::new(font_data), None, "svg_twitter");
}

fn all_glyphs_to_pdf(font_data: Arc<Vec<u8>>, glyphs: Option<Vec<(GlyphId, String)>>, name: &str) {
use crate::document::Document;
use crate::object::color_space::rgb::Rgb;
use crate::serialize::SerializeSettings;
use crate::stream::Glyph;
use crate::Transform;

let font = Font::new(font_data, 0, Location::default()).unwrap();
let font_ref = font.font_ref();

let glyphs = glyphs.unwrap_or_else(|| {
let file =
std::fs::read("/Users/lstampfl/Programming/GitHub/krilla/src/font/emojis.txt").unwrap();
let file = std::str::from_utf8(&file).unwrap();
file.chars()
.filter_map(|c| {
font_ref
.cmap()
.unwrap()
.map_codepoint(c)
.map(|g| (g, c.to_string()))
})
.collect::<Vec<_>>()
});

let metrics = font_ref.metrics(Size::unscaled(), LocationRef::default());
let num_glyphs = glyphs.len();
let width = 400;

let size = 40u32;
let num_cols = width / size;
let height = (num_glyphs as f32 / num_cols as f32).ceil() as u32 * size;
let units_per_em = metrics.units_per_em as f32;
let mut cur_point = 0;

let page_size = tiny_skia_path::Size::from_wh(width as f32, height as f32).unwrap();
let mut document_builder = Document::new(SerializeSettings::default());
let mut builder = document_builder.start_page(page_size);
let mut surface = builder.surface();

for (i, text) in glyphs.iter().cloned() {
fn get_transform(cur_point: u32, size: u32, num_cols: u32, _: f32) -> Transform {
let el = cur_point / size;
let col = el % num_cols;
let row = el / num_cols;

Transform::from_row(
1.0,
0.0,
0.0,
1.0,
col as f32 * size as f32,
(row + 1) as f32 * size as f32,
)
}

surface.push_transform(&get_transform(cur_point, size, num_cols, units_per_em));
surface.draw_glyph_run(
0.0,
0.0,
crate::Fill::<Rgb>::default(),
&[Glyph::new(i, 0.0, 0.0, 0.0, 0..text.len(), size as f32)],
font.clone(),
&text,
);
surface.pop();

cur_point += size;
}

surface.finish();
builder.finish();
let pdf = document_builder.finish();
write_manual_to_store(name, &pdf);
}
23 changes: 11 additions & 12 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use sitro::{
render_quartz, RenderOptions, RenderedDocument, RenderedPage, Renderer,
};
use skrifa::GlyphId;
use std::cell::LazyCell;
use std::cmp::max;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use oxipng::{InFile, OutFile};
use tiny_skia_path::{Path, PathBuilder, Rect};

Expand All @@ -21,39 +20,39 @@ mod visreg;
const REPLACE: bool = true;
const STORE: bool = true;

const SNAPSHOT_PATH: LazyCell<PathBuf> = LazyCell::new(|| {
static SNAPSHOT_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/snapshots");
let _ = std::fs::create_dir_all(&path);
path
});

const REFS_PATH: LazyCell<PathBuf> = LazyCell::new(|| {
static REFS_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/refs");
let _ = std::fs::create_dir_all(&path);
path
});

const DIFFS_PATH: LazyCell<PathBuf> = LazyCell::new(|| {
static DIFFS_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/diffs");
let _ = std::fs::remove_dir_all(&path);
let _ = std::fs::create_dir_all(&path).unwrap();
let _ = std::fs::create_dir_all(&path);
path
});

const STORE_PATH: LazyCell<PathBuf> = LazyCell::new(|| {
static STORE_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/store");
let _ = std::fs::remove_dir_all(&path);
let _ = std::fs::create_dir_all(&path).unwrap();
let _ = std::fs::create_dir_all(&path);
path
});

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

macro_rules! lazy_font {
($name:ident, $path:expr) => {
pub const $name: LazyCell<Arc<Vec<u8>>> =
LazyCell::new(|| Arc::new(std::fs::read($path).unwrap()));
pub static $name: LazyLock<Arc<Vec<u8>>> =
LazyLock::new(|| Arc::new(std::fs::read($path).unwrap()));
};
}

Expand Down

0 comments on commit f206811

Please sign in to comment.