Output: GlyphCluster { source: 0..1, info: , glyphs: [Glyph { id: 9, info: GlyphInfo(0),…
- User
Output:
GlyphCluster { source: 0..1, info: , glyphs: [Glyph { id: 9, info: GlyphInfo(0), x: 0.0, y: 0.0, advance: 11.776001, data: 0 }], components: [], data: 0 } GlyphCluster { source: 1..2, info: , glyphs: [Glyph { id: 844, info: GlyphInfo(0), x: 0.0, y: 0.0, advance: 9.536, data: 0 }, Glyph { id: 877, info: GlyphInfo(8), x: -0.4, y: 0.0, advance: 0.0, data: 0 }], components: [], data: 0 } GlyphCluster { source: 2..3, info: , glyphs: [Glyph { id: 544, info: GlyphInfo(0), x: 0.0, y: 0.0, advance: 9.408, data: 0 }, Glyph { id: 694, info: GlyphInfo(8), x: -1.616, y: 0.0, advance: 0.0, data: 0 }], components: [], data: 0 } GlyphCluster { source: 3..4, info: , glyphs: [Glyph { id: 980, info: GlyphInfo(0), x: 0.0, y: 0.0, advance: 6.8160005, data: 0 }, Glyph { id: 1025, info: GlyphInfo(8), x: -0.35200003, y: 0.0, advance: 0.0, data: 0 }], components: [], data: 0 } GlyphCluster { source: 4..5, info: , glyphs: [Glyph { id: 640, info: GlyphInfo(0), x: 0.0, y: 0.0, advance: 10.128, data: 0 }], components: [], data: 0 } GlyphCluster { source: 5..6, info: , glyphs: [Glyph { id: 598, info: GlyphInfo(0), x: 0.0, y: 0.0, advance: 9.616, data: 0 }, Glyph { id: 1020, info: GlyphInfo(8), x: 0.016, y: 0.0, advance: 0.0, data: 0 }], components: [], data: 0 } GlyphCluster { source: 6..7, info: , glyphs: [Glyph { id: 972, info: GlyphInfo(0), x: 0.0, y: 0.0, advance: 10.736, data: 0 }, Glyph { id: 664, info: GlyphInfo(8), x: 0.0, y: 0.0, advance: 0.0, data: 0 }], components: [], data: 0 } GlyphCluster { source: 7..8, info: , glyphs: [Glyph { id: 549, info: GlyphInfo(0), x: 0.0, y: 0.0, advance: 8.416, data: 0 }], components: [], data: 0 }
Code:
use anyhow::{Result, bail, anyhow}; use arrayvec::ArrayVec; use swash::{scale::{ScaleContext, StrikeWith}, shape::ShapeContext, zeno::{Command as SwashCmd, PathData}, FontRef};
#[derive(strum::Display, Debug, Clone, Copy)] pub enum Command { #[strum(to_string = "M {0} {1}")] M(f32, f32), #[strum(to_string = "L {0} {1}")] L(f32, f32), #[strum(to_string = "Q {0} {1} {2} {3}")] Q(f32, f32, f32, f32), Z, }
impl Command { fn to_coords(self) -> ArrayVec<f32, 4> { match self { Command::M(x, y) | Command::L(x, y) => ArrayVec::from_iter([x, y]), Command::Q(cx, cy, x, y) => ArrayVec::from_iter([cx, cy, x, y]), Command::Z => ArrayVec::new() } } }
impl std::ops::Add<[f32; 2]> for Command { type Output = Self;
fn add(self, rhs: [f32; 2]) -> Self::Output { match self { Command::M(x, y) => Command::M(x + rhs[0], y + rhs[1]), Command::L(x, y) => Command::L(x + rhs[0], y + rhs[1]), Command::Q(cx, cy, x, y) => Command::Q(cx + rhs[0], cy + rhs[1], x + rhs[0], y + rhs[1]), Command::Z => self, } }}
fn render(font: &str, text: &str, font_size: f32) { let font_data = std::fs::read(font).expect("Failed to read font file"); let font = FontRef::from_index(&font_data, 0).expect("No font in file");
let mut context = ShapeContext::new(); let mut shaper= context.builder(font) .size(font_size) .build(); shaper.add_str(text); let mut path = vec![]; let mut advance = 0.0; shaper.shape_with(|cluster| { println!("{:?}", cluster); for glyph in cluster.glyphs { let mut context = ScaleContext::new(); let mut scaler = context .builder(font) .size(font_size) .build(); let outline = scaler.scale_outline(glyph.id).expect("No outline"); let offset = [glyph.x + advance, glyph.y]; for command in outline.path().commands() { path.push( match command { SwashCmd::MoveTo(p) => Command::M(p.x, -p.y) + offset, SwashCmd::LineTo(p) => Command::L(p.x, -p.y) + offset, SwashCmd::QuadTo(control, to) => Command::Q(control.x, -control.y, to.x, -to.y) + offset, SwashCmd::Close => Command::Z, SwashCmd::CurveTo(..) => unreachable!() } ); } } advance += cluster.advance(); }); for cmd in path { print!("{} ", cmd); // println!("{:?}", cmd.to_coords()); } Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.