Skip to content

Commit

Permalink
Merge 50c6e72 into aca74c2
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyr authored Jan 4, 2022
2 parents aca74c2 + 50c6e72 commit 891a3f3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use std::borrow::Borrow;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::datastore::{DataStore, ImageStore};
use crate::fontinfo::FontInfo;
Expand Down Expand Up @@ -560,7 +559,7 @@ impl Font {

/// Returns a reference to the glyph with the given name _in the default
/// layer_.
pub fn get_glyph<K>(&self, key: &K) -> Option<&Arc<Glyph>>
pub fn get_glyph<K>(&self, key: &K) -> Option<&Glyph>
where
GlyphName: Borrow<K>,
K: Ord + ?Sized,
Expand Down
108 changes: 98 additions & 10 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

#[cfg(feature = "druid")]
use std::ops::Deref;

#[cfg(feature = "rayon")]
use rayon::prelude::*;

Expand Down Expand Up @@ -186,7 +189,10 @@ impl Default for LayerSet {
/// [UFO layer]: http://unifiedfontobject.org/versions/ufo3/glyphs/
#[derive(Debug, Clone, PartialEq)]
pub struct Layer {
#[cfg(feature = "druid")]
pub(crate) glyphs: BTreeMap<GlyphName, Arc<Glyph>>,
#[cfg(not(feature = "druid"))]
pub(crate) glyphs: BTreeMap<GlyphName, Glyph>,
pub(crate) name: LayerName,
pub(crate) path: PathBuf,
contents: BTreeMap<GlyphName, PathBuf>,
Expand Down Expand Up @@ -263,7 +269,14 @@ impl Layer {
Glyph::load_with_names(&glyph_path, names)
.map(|mut glyph| {
glyph.name = name.clone();
(name, Arc::new(glyph))
#[cfg(feature = "druid")]
{
(name, Arc::new(glyph))
}
#[cfg(not(feature = "druid"))]
{
(name, glyph)
}
})
.map_err(|source| Error::GlifLoad { path: glyph_path, source })
})
Expand Down Expand Up @@ -376,7 +389,24 @@ impl Layer {
}

/// Returns a reference to the glyph with the given name, if it exists.
pub fn get_glyph<K>(&self, glyph: &K) -> Option<&Arc<Glyph>>
pub fn get_glyph<K>(&self, glyph: &K) -> Option<&Glyph>
where
GlyphName: Borrow<K>,
K: Ord + ?Sized,
{
#[cfg(feature = "druid")]
{
return self.glyphs.get(glyph).map(|g| g.deref());
}
#[cfg(not(feature = "druid"))]
{
self.glyphs.get(glyph)
}
}

/// Returns a reference to the given glyph, behind an `Arc`, if it exists.
#[cfg(feature = "druid")]
pub fn get_glyph_raw<K>(&self, glyph: &K) -> Option<&Arc<Glyph>>
where
GlyphName: Borrow<K>,
K: Ord + ?Sized,
Expand All @@ -390,7 +420,14 @@ impl Layer {
GlyphName: Borrow<K>,
K: Ord + ?Sized,
{
self.glyphs.get_mut(glyph).map(Arc::make_mut)
#[cfg(feature = "druid")]
{
self.glyphs.get_mut(glyph).map(Arc::make_mut)
}
#[cfg(not(feature = "druid"))]
{
self.glyphs.get_mut(glyph)
}
}

/// Returns `true` if this layer contains a glyph with this `name`.
Expand All @@ -402,7 +439,11 @@ impl Layer {
///
/// If the glyph does not previously exist, the filename is calculated from
/// the glyph's name.
pub fn insert_glyph(&mut self, glyph: impl Into<Arc<Glyph>>) {
pub fn insert_glyph(
&mut self,
#[cfg(feature = "druid")] glyph: impl Into<Arc<Glyph>>,
#[cfg(not(feature = "druid"))] glyph: impl Into<Glyph>,
) {
let glyph = glyph.into();
if !self.contents.contains_key(&glyph.name) {
let path = crate::util::default_file_name_for_glyph_name(&glyph.name);
Expand All @@ -418,8 +459,26 @@ impl Layer {
}

/// Remove the named glyph from this layer and return it, if it exists.
pub fn remove_glyph(&mut self, name: &str) -> Option<Arc<Glyph>> {
///
/// **Note**: If the `druid` feature is enabled, this will not return the
/// removed `Glyph` if there are any other outstanding references to it,
/// although it will still be removed. In this case, consider using the
/// `remove_glyph_raw` method instead.
pub fn remove_glyph(&mut self, name: &str) -> Option<Glyph> {
self.contents.remove(name);
#[cfg(feature = "druid")]
{
self.glyphs.remove(name).and_then(|g| Arc::try_unwrap(g).ok())
}
#[cfg(not(feature = "druid"))]
{
self.glyphs.remove(name)
}
}

/// Remove the named glyph and return it, including the containing `Arc`.
#[cfg(feature = "druid")]
pub fn remove_glyph_raw(&mut self, name: &str) -> Option<Arc<Glyph>> {
self.glyphs.remove(name)
}

Expand All @@ -436,21 +495,50 @@ impl Layer {
} else if !self.glyphs.contains_key(old) {
Err(Error::MissingGlyph { glyph: old.into(), layer: self.name.to_string() })
} else {
let mut g = self.remove_glyph(old).unwrap();
Arc::make_mut(&mut g).name = new.into();
self.insert_glyph(g);
#[cfg(feature = "druid")]
{
let mut g = self.remove_glyph_raw(old).unwrap();
Arc::make_mut(&mut g).name = new.into();
self.insert_glyph(g);
}
#[cfg(not(feature = "druid"))]
{
let mut g = self.remove_glyph(old).unwrap();
g.name = new.into();
self.insert_glyph(g);
}
Ok(())
}
}

/// Returns an iterator over the glyphs in this layer.
pub fn iter(&self) -> impl Iterator<Item = &Arc<Glyph>> + '_ {
pub fn iter(&self) -> impl Iterator<Item = &Glyph> + '_ {
#[cfg(feature = "druid")]
{
self.glyphs.values().map(|g| g.deref())
}
#[cfg(not(feature = "druid"))]
{
self.glyphs.values()
}
}

/// Returns an iterator over the glyphs in this layer.
#[cfg(feature = "druid")]
pub fn iter_raw(&self) -> impl Iterator<Item = &Arc<Glyph>> + '_ {
self.glyphs.values()
}

/// Returns an iterator over the glyphs in this layer, mutably.
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Glyph> {
self.glyphs.values_mut().map(Arc::make_mut)
#[cfg(feature = "druid")]
{
self.glyphs.values_mut().map(Arc::make_mut)
}
#[cfg(not(feature = "druid"))]
{
self.glyphs.values_mut()
}
}

/// Returns the path to the .glif file of a given glyph `name`.
Expand Down

0 comments on commit 891a3f3

Please sign in to comment.