Skip to content

Commit 0d7c57c

Browse files
committed
Use RefCell yet again
1 parent e9190ec commit 0d7c57c

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/object/cid_font.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,10 @@ mod tests {
272272
let font_data = Arc::new(load_font("NotoSans-Regular.ttf"));
273273
let font = Font::new(font_data, 0, Location::default()).unwrap();
274274
sc.create_or_get_font_container(font.clone())
275+
.borrow_mut()
275276
.add_glyph(GlyphId::new(36));
276277
sc.create_or_get_font_container(font.clone())
278+
.borrow_mut()
277279
.add_glyph(GlyphId::new(37));
278280
check_snapshot("cid_font/noto_sans_two_glyphs", sc.finish().as_bytes());
279281
}
@@ -284,12 +286,16 @@ mod tests {
284286
let font_data = Arc::new(load_font("LatinModernRoman-Regular.otf"));
285287
let font = Font::new(font_data, 0, Location::default()).unwrap();
286288
sc.create_or_get_font_container(font.clone())
289+
.borrow_mut()
287290
.add_glyph(GlyphId::new(58));
288291
sc.create_or_get_font_container(font.clone())
292+
.borrow_mut()
289293
.add_glyph(GlyphId::new(54));
290294
sc.create_or_get_font_container(font.clone())
295+
.borrow_mut()
291296
.add_glyph(GlyphId::new(69));
292297
sc.create_or_get_font_container(font.clone())
298+
.borrow_mut()
293299
.add_glyph(GlyphId::new(71));
294300
check_snapshot("cid_font/latin_modern_four_glyphs", sc.finish().as_bytes());
295301
}

src/serialize.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use siphasher::sip128::{Hasher128, SipHasher13};
1515
use skrifa::instance::Location;
1616
use skrifa::GlyphId;
1717
use std::borrow::Cow;
18+
use std::cell::RefCell;
1819
use std::collections::HashMap;
1920
use std::hash::Hash;
2021
use std::sync::Arc;
@@ -84,7 +85,7 @@ pub trait RegisterableObject: Object + SipHashable {}
8485

8586
pub struct SerializerContext {
8687
font_cache: HashMap<Arc<FontInfo>, Font>,
87-
font_map: HashMap<Font, FontContainer>,
88+
font_map: HashMap<Font, RefCell<FontContainer>>,
8889
catalog_ref: Ref,
8990
page_tree_ref: Ref,
9091
page_labels_ref: Option<Ref>,
@@ -195,15 +196,15 @@ impl SerializerContext {
195196
}
196197
}
197198

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

203204
if font.is_type3_font() {
204-
FontContainer::Type3(Type3FontMapper::new(font.clone()))
205+
RefCell::new(FontContainer::Type3(Type3FontMapper::new(font.clone())))
205206
} else {
206-
FontContainer::CIDFont(CIDFont::new(font.clone()))
207+
RefCell::new(FontContainer::CIDFont(CIDFont::new(font.clone())))
207208
}
208209
})
209210
}
@@ -301,7 +302,7 @@ impl SerializerContext {
301302
// TODO: Make more efficient
302303
let fonts = std::mem::take(&mut self.font_map);
303304
for font_container in fonts.values() {
304-
match font_container {
305+
match &*font_container.borrow() {
305306
FontContainer::Type3(font_mapper) => {
306307
for t3_font in font_mapper.fonts() {
307308
let ref_ = self.add_font(t3_font.identifier());

src/stream.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cell::RefCell;
12
use crate::font::{Font, FontIdentifier};
23
use crate::graphics_state::GraphicsStates;
34
use crate::object::cid_font::CIDFont;
@@ -366,6 +367,8 @@ impl ContentBuilder {
366367
sb.content.begin_text();
367368
sb.content.set_text_rendering_mode(text_rendering_mode);
368369

370+
let font_container = sc.create_or_get_font_container(font.clone());
371+
369372
let spanned = TextSpanner::new(glyphs, text);
370373

371374
for fragment in spanned {
@@ -377,7 +380,7 @@ impl ContentBuilder {
377380
}
378381

379382
let segmented = GlyphGrouper::new(
380-
sc.create_or_get_font_container(font.clone()),
383+
font_container,
381384
fragment.glyphs(),
382385
text,
383386
);
@@ -926,13 +929,13 @@ impl GlyphGroup {
926929
}
927930

928931
pub struct GlyphGrouper<'a, 'b> {
929-
font_container: &'b mut FontContainer,
932+
font_container: &'b RefCell<FontContainer>,
930933
slice: &'a [Glyph],
931934
text: &'a str,
932935
}
933936

934937
impl<'a, 'b> GlyphGrouper<'a, 'b> {
935-
pub fn new(font_container: &'b mut FontContainer, slice: &'a [Glyph], text: &'a str) -> Self {
938+
pub fn new(font_container: &'b RefCell<FontContainer>, slice: &'a [Glyph], text: &'a str) -> Self {
936939
Self {
937940
font_container,
938941
slice,
@@ -952,9 +955,10 @@ impl<'a> Iterator for GlyphGrouper<'a, '_> {
952955
y_offset: f32,
953956
}
954957

955-
let mut func = |g: &Glyph| {
956-
self.font_container.add_glyph(g.glyph_id);
957-
let font_identifier = self.font_container.font_identifier(g.glyph_id).unwrap();
958+
let func = |g: &Glyph| {
959+
let mut font_container = self.font_container.borrow_mut();
960+
font_container.add_glyph(g.glyph_id);
961+
let font_identifier = font_container.font_identifier(g.glyph_id).unwrap();
958962

959963
GlyphProps {
960964
font_identifier,
@@ -990,9 +994,9 @@ impl<'a> Iterator for GlyphGrouper<'a, '_> {
990994
let glyphs = head
991995
.iter()
992996
.map(move |g| {
993-
let font_identifier = self.font_container.font_identifier(g.glyph_id).unwrap();
994-
let mut pdf_font = self
995-
.font_container
997+
let mut font_container = self.font_container.borrow_mut();
998+
let font_identifier = font_container.font_identifier(g.glyph_id).unwrap();
999+
let mut pdf_font = font_container
9961000
.get_from_identifier_mut(font_identifier.clone())
9971001
.unwrap();
9981002

0 commit comments

Comments
 (0)