From 2f2dcca34e2c74b251871cc42cdcee308b504ec8 Mon Sep 17 00:00:00 2001 From: "zhuoxian.dzx" Date: Mon, 27 May 2024 16:50:35 +0800 Subject: [PATCH] fix: wrap_text invalid eq --- Cargo.toml | 2 +- src/metrics/compose.rs | 2 +- tests/basic.rs | 29 ++++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8ccadb5..0d45f2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fontkit" -version = "0.6.0-beta.1" +version = "0.6.0-beta.2" edition = "2021" authors = ["Zimon Dai "] description = "A simple library for font loading and indexing" diff --git a/src/metrics/compose.rs b/src/metrics/compose.rs index 760a40f..b8cf254 100644 --- a/src/metrics/compose.rs +++ b/src/metrics/compose.rs @@ -241,7 +241,7 @@ where if rtl { std::mem::swap(span, &mut new_span); } - if !span.metrics.count() == 0 { + if span.metrics.count() != 0 { current_line.spans.push(span.clone()); } // Create a new line diff --git a/tests/basic.rs b/tests/basic.rs index 9be84f0..359c1f6 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,4 +1,4 @@ -use fontkit::{Error, FontKey}; +use fontkit::{Area, Error, FontKey, FontKit, Line, Span, TextMetrics}; use std::fs; use std::io::Read; @@ -7,7 +7,7 @@ pub fn test_font_loading() -> Result<(), Error> { let mut buf = vec![]; let mut f = fs::File::open("examples/OpenSans-Italic.ttf")?; f.read_to_end(&mut buf)?; - let fontkit = fontkit::FontKit::new(); + let fontkit = FontKit::new(); let _ = fontkit.add_font_from_buffer(buf)?; Ok(()) } @@ -17,7 +17,7 @@ pub fn test_variable_font_loading() -> Result<(), Error> { let mut buf = vec![]; let mut f = fs::File::open("examples/AlimamaFangYuanTiVF.ttf")?; f.read_to_end(&mut buf)?; - let fontkit = fontkit::FontKit::new(); + let fontkit = FontKit::new(); let _ = fontkit.add_font_from_buffer(buf)?; let mut key = FontKey::default(); key.family = "AlimamaFangYuanTiVF-Medium-Round".into(); @@ -33,3 +33,26 @@ pub fn test_variable_font_loading() -> Result<(), Error> { assert!(bitmap_1 > bitmap_2); Ok(()) } + +#[test] +pub fn test_text_wrap() -> Result<(), Error> { + let fontkit = FontKit::new(); + fontkit.search_fonts_from_path("examples/AlimamaFangYuanTiVF.ttf")?; + let key = fontkit.font_keys().next().unwrap(); + let mut area = Area::<(), TextMetrics>::new(); + let metrics = fontkit + .measure(&key, " 傲冬黑色真皮皮衣 穿着舒适显瘦") + .unwrap(); + let mut span = Span::default(); + span.font_key = key.clone(); + span.size = 66.0; + span.metrics = metrics; + area.lines.push(Line { + spans: vec![span], + hard_break: true, + }); + area.unwrap_text(); + area.wrap_text(576.0)?; + assert_eq!(area.width(), 549.12); + Ok(()) +}