Skip to content

Commit

Permalink
Share font database between the text and svg pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-deason committed Jun 10, 2023
1 parent 9aa1b93 commit 31167fa
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion tiny_skia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ features = ["tiny-skia"]

[dependencies.cosmic-text]
git = "https://github.com/hecrj/cosmic-text.git"
rev = "b85d6a4f2376f8a8a7dadc0f8bcb89d4db10a1c9"
rev = "79275d1"

[dependencies.twox-hash]
version = "1.6"
Expand Down
6 changes: 5 additions & 1 deletion tiny_skia/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,14 @@ impl Backend {
let clip_mask = (!physical_bounds.is_within(&clip_bounds))
.then_some(clip_mask as &_);

let font_system = self.text_pipeline.font_system.borrow();
self.vector_pipeline.draw(
handle,
*color,
(*bounds + translation) * scale_factor,
pixels,
clip_mask,
font_system.db(),
);
}
Primitive::Fill {
Expand Down Expand Up @@ -745,6 +747,8 @@ impl backend::Svg for Backend {
&self,
handle: &crate::core::svg::Handle,
) -> Size<u32> {
self.vector_pipeline.viewport_dimensions(handle)
let font_system = self.text_pipeline.font_system.borrow();
self.vector_pipeline
.viewport_dimensions(handle, font_system.db())
}
}
2 changes: 1 addition & 1 deletion tiny_skia/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::sync::Arc;

#[allow(missing_debug_implementations)]
pub struct Pipeline {
font_system: RefCell<cosmic_text::FontSystem>,
pub font_system: RefCell<cosmic_text::FontSystem>,
glyph_cache: GlyphCache,
measurement_cache: RefCell<Cache>,
render_cache: Cache,
Expand Down
32 changes: 23 additions & 9 deletions tiny_skia/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::core::svg::{Data, Handle};
use crate::core::{Color, Rectangle, Size};

use bytemuck::cast;
use cosmic_text::fontdb::Database;
use resvg::usvg::{self, TreeTextToPath};
use rustc_hash::{FxHashMap, FxHashSet};

Expand All @@ -20,10 +21,14 @@ impl Pipeline {
}
}

pub fn viewport_dimensions(&self, handle: &Handle) -> Size<u32> {
pub fn viewport_dimensions(
&self,
handle: &Handle,
fontdb: &Database,
) -> Size<u32> {
self.cache
.borrow_mut()
.viewport_dimensions(handle)
.viewport_dimensions(handle, fontdb)
.unwrap_or(Size::new(0, 0))
}

Expand All @@ -34,11 +39,13 @@ impl Pipeline {
bounds: Rectangle,
pixels: &mut tiny_skia::PixmapMut<'_>,
clip_mask: Option<&tiny_skia::Mask>,
fontdb: &Database,
) {
if let Some(image) = self.cache.borrow_mut().draw(
handle,
color,
Size::new(bounds.width as u32, bounds.height as u32),
fontdb,
) {
pixels.draw_pixmap(
bounds.x as i32,
Expand Down Expand Up @@ -72,7 +79,11 @@ struct RasterKey {
}

impl Cache {
fn load(&mut self, handle: &Handle) -> Option<&usvg::Tree> {
fn load(
&mut self,
handle: &Handle,
fontdb: &Database,
) -> Option<&usvg::Tree> {
use usvg::TreeParsing;

let id = handle.id();
Expand All @@ -95,9 +106,7 @@ impl Cache {

if let Some(svg) = &mut svg {
if svg.has_text_nodes() {
let mut fontdb = usvg::fontdb::Database::new();
fontdb.load_system_fonts();
svg.convert_text(&fontdb);
svg.convert_text(fontdb);
}
}

Expand All @@ -108,8 +117,12 @@ impl Cache {
self.trees.get(&id).unwrap().as_ref()
}

fn viewport_dimensions(&mut self, handle: &Handle) -> Option<Size<u32>> {
let tree = self.load(handle)?;
fn viewport_dimensions(
&mut self,
handle: &Handle,
fontdb: &Database,
) -> Option<Size<u32>> {
let tree = self.load(handle, fontdb)?;

Some(Size::new(
tree.size.width() as u32,
Expand All @@ -122,6 +135,7 @@ impl Cache {
handle: &Handle,
color: Option<Color>,
size: Size<u32>,
fontdb: &Database,
) -> Option<tiny_skia::PixmapRef<'_>> {
if size.width == 0 || size.height == 0 {
return None;
Expand All @@ -135,7 +149,7 @@ impl Cache {

#[allow(clippy::map_entry)]
if !self.rasters.contains_key(&key) {
let tree = self.load(handle)?;
let tree = self.load(handle, fontdb)?;

let mut image = tiny_skia::Pixmap::new(size.width, size.height)?;

Expand Down

0 comments on commit 31167fa

Please sign in to comment.