Skip to content

Commit

Permalink
Use RefCell yet again
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Aug 28, 2024
1 parent e9190ec commit 0d7c57c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/object/cid_font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,10 @@ mod tests {
let font_data = Arc::new(load_font("NotoSans-Regular.ttf"));
let font = Font::new(font_data, 0, Location::default()).unwrap();
sc.create_or_get_font_container(font.clone())
.borrow_mut()
.add_glyph(GlyphId::new(36));
sc.create_or_get_font_container(font.clone())
.borrow_mut()
.add_glyph(GlyphId::new(37));
check_snapshot("cid_font/noto_sans_two_glyphs", sc.finish().as_bytes());
}
Expand All @@ -284,12 +286,16 @@ mod tests {
let font_data = Arc::new(load_font("LatinModernRoman-Regular.otf"));
let font = Font::new(font_data, 0, Location::default()).unwrap();
sc.create_or_get_font_container(font.clone())
.borrow_mut()
.add_glyph(GlyphId::new(58));
sc.create_or_get_font_container(font.clone())
.borrow_mut()
.add_glyph(GlyphId::new(54));
sc.create_or_get_font_container(font.clone())
.borrow_mut()
.add_glyph(GlyphId::new(69));
sc.create_or_get_font_container(font.clone())
.borrow_mut()
.add_glyph(GlyphId::new(71));
check_snapshot("cid_font/latin_modern_four_glyphs", sc.finish().as_bytes());
}
Expand Down
11 changes: 6 additions & 5 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use siphasher::sip128::{Hasher128, SipHasher13};
use skrifa::instance::Location;
use skrifa::GlyphId;
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
Expand Down Expand Up @@ -84,7 +85,7 @@ pub trait RegisterableObject: Object + SipHashable {}

pub struct SerializerContext {
font_cache: HashMap<Arc<FontInfo>, Font>,
font_map: HashMap<Font, FontContainer>,
font_map: HashMap<Font, RefCell<FontContainer>>,
catalog_ref: Ref,
page_tree_ref: Ref,
page_labels_ref: Option<Ref>,
Expand Down Expand Up @@ -195,15 +196,15 @@ impl SerializerContext {
}
}

pub fn create_or_get_font_container(&mut self, font: Font) -> &mut FontContainer {
pub fn create_or_get_font_container(&mut self, font: Font) -> &RefCell<FontContainer> {
self.font_map.entry(font.clone()).or_insert_with(|| {
self.font_cache
.insert(font.font_info().clone(), font.clone());

if font.is_type3_font() {
FontContainer::Type3(Type3FontMapper::new(font.clone()))
RefCell::new(FontContainer::Type3(Type3FontMapper::new(font.clone())))
} else {
FontContainer::CIDFont(CIDFont::new(font.clone()))
RefCell::new(FontContainer::CIDFont(CIDFont::new(font.clone())))
}
})
}
Expand Down Expand Up @@ -301,7 +302,7 @@ impl SerializerContext {
// TODO: Make more efficient
let fonts = std::mem::take(&mut self.font_map);
for font_container in fonts.values() {
match font_container {
match &*font_container.borrow() {
FontContainer::Type3(font_mapper) => {
for t3_font in font_mapper.fonts() {
let ref_ = self.add_font(t3_font.identifier());
Expand Down
22 changes: 13 additions & 9 deletions src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cell::RefCell;
use crate::font::{Font, FontIdentifier};
use crate::graphics_state::GraphicsStates;
use crate::object::cid_font::CIDFont;
Expand Down Expand Up @@ -366,6 +367,8 @@ impl ContentBuilder {
sb.content.begin_text();
sb.content.set_text_rendering_mode(text_rendering_mode);

let font_container = sc.create_or_get_font_container(font.clone());

let spanned = TextSpanner::new(glyphs, text);

for fragment in spanned {
Expand All @@ -377,7 +380,7 @@ impl ContentBuilder {
}

let segmented = GlyphGrouper::new(
sc.create_or_get_font_container(font.clone()),
font_container,
fragment.glyphs(),
text,
);
Expand Down Expand Up @@ -926,13 +929,13 @@ impl GlyphGroup {
}

pub struct GlyphGrouper<'a, 'b> {
font_container: &'b mut FontContainer,
font_container: &'b RefCell<FontContainer>,
slice: &'a [Glyph],
text: &'a str,
}

impl<'a, 'b> GlyphGrouper<'a, 'b> {
pub fn new(font_container: &'b mut FontContainer, slice: &'a [Glyph], text: &'a str) -> Self {
pub fn new(font_container: &'b RefCell<FontContainer>, slice: &'a [Glyph], text: &'a str) -> Self {
Self {
font_container,
slice,
Expand All @@ -952,9 +955,10 @@ impl<'a> Iterator for GlyphGrouper<'a, '_> {
y_offset: f32,
}

let mut func = |g: &Glyph| {
self.font_container.add_glyph(g.glyph_id);
let font_identifier = self.font_container.font_identifier(g.glyph_id).unwrap();
let func = |g: &Glyph| {
let mut font_container = self.font_container.borrow_mut();
font_container.add_glyph(g.glyph_id);
let font_identifier = font_container.font_identifier(g.glyph_id).unwrap();

GlyphProps {
font_identifier,
Expand Down Expand Up @@ -990,9 +994,9 @@ impl<'a> Iterator for GlyphGrouper<'a, '_> {
let glyphs = head
.iter()
.map(move |g| {
let font_identifier = self.font_container.font_identifier(g.glyph_id).unwrap();
let mut pdf_font = self
.font_container
let mut font_container = self.font_container.borrow_mut();
let font_identifier = font_container.font_identifier(g.glyph_id).unwrap();
let mut pdf_font = font_container
.get_from_identifier_mut(font_identifier.clone())
.unwrap();

Expand Down

0 comments on commit 0d7c57c

Please sign in to comment.