Skip to content

Commit

Permalink
Return the svg document including the svg data and the range covered.
Browse files Browse the repository at this point in the history
Closes #108
  • Loading branch information
wjian23 committed Oct 7, 2023
1 parent 42916b2 commit 97b164a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/font2svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn process(args: Args) -> Result<(), Box<dyn std::error::Error>> {
buf.extend_from_slice(b"data:image/svg+xml;base64, ");

let mut enc = base64::write::EncoderWriter::new(buf, base64::STANDARD);
enc.write_all(img).unwrap();
enc.write_all(img.data).unwrap();
enc.finish().unwrap();
});
svg.end_element();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2057,7 +2057,7 @@ impl<'a> Face<'a> {
/// Also, a font can contain both: images and outlines. So when this method returns `None`
/// you should also try `outline_glyph()` afterwards.
#[inline]
pub fn glyph_svg_image(&self, glyph_id: GlyphId) -> Option<&'a [u8]> {
pub fn glyph_svg_image(&self, glyph_id: GlyphId) -> Option<svg::SvgDocument<'a>> {
self.tables.svg.and_then(|svg| svg.documents.find(glyph_id))
}

Expand Down
24 changes: 20 additions & 4 deletions src/tables/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
use crate::parser::{FromData, LazyArray16, NumFrom, Offset, Offset32, Stream};
use crate::GlyphId;

#[derive(Debug, Clone, Copy)]
/// An SvgDocument can include multi glyphs
pub struct SvgDocument<'a> {
/// The SVG document data.The detail of data can see https://learn.microsoft.com/en-us/typography/opentype/spec/svg#svg-document-list
pub data: &'a [u8],
/// The first glyph ID for the range covered by this record.
pub start_glyph_id: GlyphId,
/// The last glyph ID for the range covered by this record.
pub end_glyph_id: GlyphId,
}

#[derive(Clone, Copy)]
struct SvgDocumentRecord {
start_glyph_id: GlyphId,
Expand Down Expand Up @@ -39,16 +50,21 @@ impl<'a> SvgDocumentsList<'a> {
///
/// `index` is not a GlyphId. You should use [`find()`](SvgDocumentsList::find) instead.
#[inline]
pub fn get(&self, index: u16) -> Option<&'a [u8]> {
pub fn get(&self, index: u16) -> Option<SvgDocument<'a>> {
let record = self.records.get(index)?;
let offset = record.svg_doc_offset?.to_usize();
self.data
.get(offset..offset + usize::num_from(record.svg_doc_length))
.map(|data| SvgDocument {
data,
start_glyph_id: record.start_glyph_id,
end_glyph_id: record.end_glyph_id,
})
}

/// Returns a SVG document data by glyph ID.
#[inline]
pub fn find(&self, glyph_id: GlyphId) -> Option<&'a [u8]> {
pub fn find(&self, glyph_id: GlyphId) -> Option<SvgDocument<'a>> {
let index = self
.records
.into_iter()
Expand All @@ -74,7 +90,7 @@ impl core::fmt::Debug for SvgDocumentsList<'_> {
}

impl<'a> IntoIterator for SvgDocumentsList<'a> {
type Item = &'a [u8];
type Item = SvgDocument<'a>;
type IntoIter = SvgDocumentsListIter<'a>;

#[inline]
Expand All @@ -95,7 +111,7 @@ pub struct SvgDocumentsListIter<'a> {
}

impl<'a> Iterator for SvgDocumentsListIter<'a> {
type Item = &'a [u8];
type Item = SvgDocument<'a>;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
Expand Down

0 comments on commit 97b164a

Please sign in to comment.