Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XML declaration custom single/double quote style support in glif and *.plist files #157

Merged
merged 16 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 7 additions & 23 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,13 @@ impl Font {
// we want to always set ourselves as the creator when serializing,
// but we also don't have mutable access to self.
if self.meta.creator == DEFAULT_METAINFO_CREATOR {
write::write_xml_to_file(&path.join(METAINFO_FILE), &self.meta, options.xml_options())?;
write::write_xml_to_file(&path.join(METAINFO_FILE), &self.meta, options)?;
} else {
write::write_xml_to_file(
&path.join(METAINFO_FILE),
&MetaInfo::default(),
options.xml_options(),
)?;
write::write_xml_to_file(&path.join(METAINFO_FILE), &MetaInfo::default(), options)?;
}

if let Some(font_info) = self.font_info.as_ref() {
write::write_xml_to_file(&path.join(FONTINFO_FILE), font_info, options.xml_options())?;
write::write_xml_to_file(&path.join(FONTINFO_FILE), font_info, options)?;
}

// Object libs are treated specially. The UFO v3 format won't allow us
Expand All @@ -277,25 +273,17 @@ impl Font {
}
if !lib.is_empty() {
crate::util::recursive_sort_plist_keys(&mut lib);
write::write_plist_value_to_file(
&path.join(LIB_FILE),
&lib.into(),
options.xml_options(),
)?;
write::write_plist_value_to_file(&path.join(LIB_FILE), &lib.into(), options)?;
}

if let Some(groups) = self.groups.as_ref() {
validate_groups(groups).map_err(Error::InvalidGroups)?;
write::write_xml_to_file(&path.join(GROUPS_FILE), groups, options.xml_options())?;
write::write_xml_to_file(&path.join(GROUPS_FILE), groups, options)?;
}

if let Some(kerning) = self.kerning.as_ref() {
let kerning_serializer = crate::kerning::KerningSerializer { kerning };
write::write_xml_to_file(
&path.join(KERNING_FILE),
&kerning_serializer,
options.xml_options(),
)?;
write::write_xml_to_file(&path.join(KERNING_FILE), &kerning_serializer, options)?;
}

if let Some(features) = self.features.as_ref() {
Expand All @@ -304,11 +292,7 @@ impl Font {

let contents: Vec<(&str, &PathBuf)> =
self.layers.iter().map(|l| (l.name.as_ref(), &l.path)).collect();
write::write_xml_to_file(
&path.join(LAYER_CONTENTS_FILE),
&contents,
options.xml_options(),
)?;
write::write_xml_to_file(&path.join(LAYER_CONTENTS_FILE), &contents, options)?;

for layer in self.layers.iter() {
let layer_path = path.join(&layer.path);
Expand Down
8 changes: 6 additions & 2 deletions src/glyph/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::io::{Cursor, Write};

use quick_xml::{
events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event},
events::{BytesEnd, BytesStart, BytesText, Event},
Error as XmlError, Writer,
};

Expand All @@ -14,6 +14,7 @@ use crate::{
};

use crate::error::{GlifWriteError, WriteError};
use crate::write::QuoteChar;

impl Glyph {
/// Serialize the glyph into an XML byte stream.
Expand All @@ -37,7 +38,10 @@ impl Glyph {
options.whitespace_char,
options.whitespace_count,
);
writer.write_event(Event::Decl(BytesDecl::new(b"1.0", Some(b"UTF-8"), None)))?;
match options.quote_style {
QuoteChar::Double => writer.write(b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")?,
QuoteChar::Single => writer.write(b"<?xml version='1.0' encoding='UTF-8'?>\n")?,
}
let mut start = BytesStart::borrowed_name(b"glyph");
start.push_attribute(("name", &*self.name));
start.push_attribute(("format", self.format.as_str()));
Expand Down
62 changes: 62 additions & 0 deletions src/glyph/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::parse::parse_glyph;
use super::*;
use crate::write::QuoteChar;
use std::path::PathBuf;

#[test]
Expand Down Expand Up @@ -94,6 +95,67 @@ fn serialize_with_custom_whitespace() {
);
}

#[test]
fn serialize_with_single_quote_style() {
let data = include_str!("../../testdata/small_lib.glif");
let glyph = parse_glyph(data.as_bytes()).unwrap();
let options = WriteOptions::default().quote_char(QuoteChar::Single);
let one_tab = glyph.encode_xml_with_options(&options).unwrap();
let one_tab = std::str::from_utf8(&one_tab).unwrap();
pretty_assertions::assert_eq!(
one_tab,
r#"<?xml version='1.0' encoding='UTF-8'?>
<glyph name="hello" format="2">
<advance width="1200"/>
<outline>
<contour>
<point x="2" y="30" type="line"/>
<point x="44" y="10" type="line"/>
</contour>
</outline>
<lib>
<dict>
<key>test.key</key>
<string>I am a creative professional :)</string>
</dict>
</lib>
<note>durp</note>
</glyph>
"#
);
}

#[test]
fn serialize_with_custom_whitespace_and_single_quote_style() {
let data = include_str!("../../testdata/small_lib.glif");
let glyph = parse_glyph(data.as_bytes()).unwrap();
let options = WriteOptions::default().whitespace(" ").quote_char(QuoteChar::Single);
let two_spaces = glyph.encode_xml_with_options(&options).unwrap();
let two_spaces = std::str::from_utf8(&two_spaces).unwrap();

pretty_assertions::assert_eq!(
two_spaces,
r#"<?xml version='1.0' encoding='UTF-8'?>
<glyph name="hello" format="2">
<advance width="1200"/>
<outline>
<contour>
<point x="2" y="30" type="line"/>
<point x="44" y="10" type="line"/>
</contour>
</outline>
<lib>
<dict>
<key>test.key</key>
<string>I am a creative professional :)</string>
</dict>
</lib>
<note>durp</note>
</glyph>
"#
);
}

#[test]
fn parse() {
let bytes = include_bytes!("../../testdata/sample_period.glif");
Expand Down
12 changes: 2 additions & 10 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,7 @@ impl Layer {

util::recursive_sort_plist_keys(&mut dict);

crate::write::write_plist_value_to_file(
&path.join(LAYER_INFO_FILE),
&dict.into(),
options.xml_options(),
)
crate::write::write_plist_value_to_file(&path.join(LAYER_INFO_FILE), &dict.into(), options)
}

/// Attempt to write this layer to the given path.
Expand All @@ -341,11 +337,7 @@ impl Layer {

pub fn save_with_options(&self, path: &Path, opts: &WriteOptions) -> Result<(), Error> {
fs::create_dir(&path)?;
crate::write::write_xml_to_file(
&path.join(CONTENTS_FILE),
&self.contents,
opts.xml_options(),
)?;
crate::write::write_xml_to_file(&path.join(CONTENTS_FILE), &self.contents, opts)?;

self.layerinfo_to_file_if_needed(path, opts)?;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub use guideline::{Guideline, Line};
pub use identifier::Identifier;
pub use layer::{Layer, LayerSet};
pub use shared_types::{Color, IntegerOrFloat, NonNegativeIntegerOrFloat, Plist};
pub use write::WriteOptions;
pub use write::{QuoteChar, WriteOptions};

#[allow(deprecated)]
pub use font::Ufo;
Loading