Skip to content

Commit

Permalink
Update cosmic-text and resvg
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jun 16, 2024
1 parent 42d989b commit dda9adf
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ ouroboros = "0.18"
palette = "0.7"
qrcode = { version = "0.13", default-features = false }
raw-window-handle = "0.6"
resvg = "0.41"
resvg = "0.42"
rustc-hash = "1.0"
smol = "1.0"
smol_str = "0.2"
Expand Down Expand Up @@ -204,4 +204,4 @@ useless_conversion = "deny"
broken_intra_doc_links = "forbid"

[patch.crates-io]
cosmic-text = { git = "https://github.com/hecrj/cosmic-text.git", rev = "a2b3f4064178afd7f91e0d86407a82bf507da55f" }
cosmic-text = { git = "https://github.com/hecrj/cosmic-text.git", rev = "542b20ca4376a3b5de5fa629db1a4ace44e18e0c" }
4 changes: 3 additions & 1 deletion graphics/src/geometry/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ impl Text {

let mut buffer = cosmic_text::BufferLine::new(
&self.content,
cosmic_text::LineEnding::default(),
cosmic_text::AttrsList::new(text::to_attributes(self.font)),
text::to_shaping(self.shaping),
);

let layout = buffer.layout(
font_system.raw(),
self.size.0,
f32::MAX,
None,
cosmic_text::Wrap::None,
None,
4,
);

let translation_x = match self.horizontal_alignment {
Expand Down
4 changes: 2 additions & 2 deletions graphics/src/text/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl Cache {

buffer.set_size(
font_system,
key.bounds.width,
key.bounds.height.max(key.line_height),
Some(key.bounds.width),
Some(key.bounds.height.max(key.line_height)),
);
buffer.set_text(
font_system,
Expand Down
16 changes: 9 additions & 7 deletions graphics/src/text/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ impl editor::Editor for Editor {
width,
y: (visual_line as i32 + visual_lines_offset)
as f32
* line_height,
* line_height
- buffer.scroll().vertical,
height: line_height,
})
} else {
Expand Down Expand Up @@ -218,7 +219,8 @@ impl editor::Editor for Editor {
Cursor::Caret(Point::new(
offset,
(visual_lines_offset + visual_line as i32) as f32
* line_height,
* line_height
- buffer.scroll().vertical,
))
}
}
Expand Down Expand Up @@ -461,8 +463,8 @@ impl editor::Editor for Editor {

buffer_mut_from_editor(&mut internal.editor).set_size(
font_system.raw(),
new_bounds.width,
new_bounds.height,
Some(new_bounds.width),
Some(new_bounds.height),
);

internal.bounds = new_bounds;
Expand Down Expand Up @@ -492,7 +494,8 @@ impl editor::Editor for Editor {
let buffer = buffer_from_editor(&internal.editor);

let scroll = buffer.scroll();
let mut window = buffer.visible_lines();
let mut window = (internal.bounds.height / buffer.metrics().line_height)
.ceil() as i32;

let last_visible_line = buffer.lines[scroll.line..]
.iter()
Expand Down Expand Up @@ -694,8 +697,7 @@ fn visual_lines_offset(line: usize, buffer: &cosmic_text::Buffer) -> i32 {
})
.sum();

(visual_lines_offset as i32 - scroll.layout)
* if scroll.line < line { 1 } else { -1 }
visual_lines_offset as i32 * if scroll.line < line { 1 } else { -1 }
}

fn to_motion(motion: Motion) -> cosmic_text::Motion {
Expand Down
8 changes: 4 additions & 4 deletions graphics/src/text/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl core::text::Paragraph for Paragraph {

buffer.set_size(
font_system.raw(),
text.bounds.width,
text.bounds.height,
Some(text.bounds.width),
Some(text.bounds.height),
);

buffer.set_text(
Expand Down Expand Up @@ -116,8 +116,8 @@ impl core::text::Paragraph for Paragraph {

internal.buffer.set_size(
font_system.raw(),
new_bounds.width,
new_bounds.height,
Some(new_bounds.width),
Some(new_bounds.height),
);

internal.bounds = new_bounds;
Expand Down
10 changes: 7 additions & 3 deletions tiny_skia/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,13 @@ impl Engine {
let transformation = transformation * *local_transformation;
let (width, height) = buffer.size();

let physical_bounds =
Rectangle::new(raw.position, Size::new(width, height))
* transformation;
let physical_bounds = Rectangle::new(
raw.position,
Size::new(
width.unwrap_or(clip_bounds.width),
height.unwrap_or(clip_bounds.height),
),
) * transformation;

if !clip_bounds.intersects(&physical_bounds) {
return;
Expand Down
8 changes: 7 additions & 1 deletion tiny_skia/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ impl Pipeline {
font_system.raw(),
&mut self.glyph_cache,
buffer,
Rectangle::new(position, Size::new(width, height)),
Rectangle::new(
position,
Size::new(
width.unwrap_or(pixels.width() as f32),
height.unwrap_or(pixels.height() as f32),
),
),
color,
alignment::Horizontal::Left,
alignment::Vertical::Top,
Expand Down
20 changes: 8 additions & 12 deletions tiny_skia/src/vector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::core::svg::{Data, Handle};
use crate::core::{Color, Rectangle, Size};
use crate::graphics::text;

use resvg::usvg;
use rustc_hash::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -83,26 +82,23 @@ impl Cache {
let id = handle.id();

if let hash_map::Entry::Vacant(entry) = self.trees.entry(id) {
let mut font_system =
text::font_system().write().expect("Write font system");

let mut svg = match handle.data() {
Data::Path(path) => {
fs::read_to_string(path).ok().and_then(|contents| {
usvg::Tree::from_str(
&contents,
&usvg::Options::default(),
font_system.raw().db(),
&usvg::Options::default(), // TODO: Set usvg::Options::fontdb
)
.ok()
})
}
Data::Bytes(bytes) => usvg::Tree::from_data(
bytes,
&usvg::Options::default(),
font_system.raw().db(),
)
.ok(),
Data::Bytes(bytes) => {
usvg::Tree::from_data(
bytes,
&usvg::Options::default(), // TODO: Set usvg::Options::fontdb
)
.ok()
}
};

if let Some(svg) = &mut svg {
Expand Down
10 changes: 2 additions & 8 deletions wgpu/src/image/vector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::core::svg;
use crate::core::{Color, Size};
use crate::graphics::text;
use crate::image::atlas::{self, Atlas};

use resvg::tiny_skia;
Expand Down Expand Up @@ -49,17 +48,13 @@ impl Cache {
return self.svgs.get(&handle.id()).unwrap();
}

let mut font_system =
text::font_system().write().expect("Write font system");

let svg = match handle.data() {
svg::Data::Path(path) => fs::read_to_string(path)
.ok()
.and_then(|contents| {
usvg::Tree::from_str(
&contents,
&usvg::Options::default(),
font_system.raw().db(),
&usvg::Options::default(), // TODO: Set usvg::Options::fontdb
)
.ok()
})
Expand All @@ -68,8 +63,7 @@ impl Cache {
svg::Data::Bytes(bytes) => {
match usvg::Tree::from_data(
bytes,
&usvg::Options::default(),
font_system.raw().db(),
&usvg::Options::default(), // TODO: Set usvg::Options::fontdb
) {
Ok(tree) => Svg::Loaded(tree),
Err(_) => Svg::NotFound,
Expand Down
8 changes: 7 additions & 1 deletion wgpu/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,13 @@ fn prepare(

(
buffer.as_ref(),
Rectangle::new(raw.position, Size::new(width, height)),
Rectangle::new(
raw.position,
Size::new(
width.unwrap_or(layer_bounds.width),
height.unwrap_or(layer_bounds.height),
),
),
alignment::Horizontal::Left,
alignment::Vertical::Top,
raw.color,
Expand Down

0 comments on commit dda9adf

Please sign in to comment.