Struct glifparser::anchor::Anchor
source · pub struct Anchor<PD: PointData> {
pub x: f32,
pub y: f32,
pub class: Option<String>,
pub data: PD,
pub atype: AnchorType,
}Fields§
§x: f32§y: f32§class: Option<String>§data: PD§atype: AnchorTypeImplementations§
source§impl<PD: PointData> Anchor<PD>
impl<PD: PointData> Anchor<PD>
sourcepub fn from_glif(
ga: &GlifAnchor,
pedantry: Pedantry
) -> Result<Self, GlifParserError>
pub fn from_glif(
ga: &GlifAnchor,
pedantry: Pedantry
) -> Result<Self, GlifParserError>
Examples found in repository?
src/glif/read.rs (line 160)
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
pub fn read_ufo_glif_pedantic<PD: PointData>(glif: &str, pedantry: Pedantry) -> Result<Glif<PD>, GlifParserError> {
let mut glif = xmltree::Element::parse(glif.as_bytes())?;
let mut ret = Glif::new();
if glif.name != "glyph" {
return Err(input_error!("Root element not <glyph>"))
}
if glif.attributes.get("format").ok_or(input_error!("no format in <glyph>"))? != "2" {
return Err(input_error!("<glyph> format not 2"))
}
ret.name = glif
.attributes
.get("name")
.ok_or(input_error!("<glyph> has no name"))?
.clone();
ret.components.root = ret.name.clone();
let advance = glif
.take_child("advance");
ret.width = if let Some(a) = advance {
let width = a.attributes
.get("width")
.ok_or(input_error!("<advance> has no width"))?;
let widthc = width.parse::<u64>();
match widthc {
Err(e) => if let Ok((f, false)) = width.parse::<f32>().map(|f|(f, f.is_subnormal())) {
const FLOATPOINTWARNING: &str = "Floating point value given as <advance> width — OpenType `hmtx` / `vmtx` will truncate it";
if !pedantry.should_mend() {
Err(input_error!(FLOATPOINTWARNING))?
}
log::warn!("{}, so we do too!", FLOATPOINTWARNING);
Some(f as u64)
} else {
log::trace!("<advance> parsing as int rose {:?}", e);
Err(input_error!("<advance> width neither int nor downgradable (not subnormal) float!"))?
},
Ok(i) => Some(i)
}
} else {
None
};
let mut unicodes = vec![];
while let Some(u) = glif.take_child("unicode") {
let unicodehex = u
.attributes
.get("hex")
.ok_or(input_error!("<unicode> has no hex"))?;
unicodes.push(
char::from_u32(
u32::from_str_radix(unicodehex, 16)
.or(Err(input_error!("<unicode> hex not int")))?
)
.ok_or(input_error!("<unicode> char conversion failed"))?,
);
}
ret.unicode = unicodes;
let mut anchors: Vec<Anchor<PD>> = Vec::new();
while let Some(anchor_el) = glif.take_child("anchor") {
let mut anchor = GlifAnchor::default();
anchor.x = anchor_el
.attributes
.get("x")
.ok_or(input_error!("<anchor> missing x"))?
.parse()
.or(Err(input_error!("<anchor> x not integer/float")))?;
anchor.y = anchor_el
.attributes
.get("y")
.ok_or(input_error!("<anchor> missing y"))?
.parse()
.or(Err(input_error!("<anchor> y not integer/float")))?;
anchor.class = anchor_el
.attributes
.get("name")
.map(|a|GlifStringLenOne::try_from(a.clone()))
.map_or(None, |r|r.ok());
anchors.push(Anchor::from_glif(&anchor, pedantry)?);
}
ret.anchors = anchors;
#[cfg(feature = "glifimage")] {
let mut images: Vec<GlifImage> = Vec::new();
while let Some(image_el) = glif.take_child("image") {
let filename = path::PathBuf::from(image_el
.attributes
.get("fileName")
.ok_or(input_error!("<image> missing fileName"))?);
let mut gimage = GlifImage::from_filename(filename)?;
load_matrix_and_identifier!(image_el, gimage, (xScale, xyScale, yxScale, yScale, xOffset, yOffset));
if let Some(color) = image_el.attributes.get("color") {
gimage.color = Some(color.parse()?);
}
images.push(gimage);
}
ret.images = images;
}
let mut guidelines: Vec<Guideline<PD>> = Vec::new();
while let Some(guideline_el) = glif.take_child("guideline") {
let gx = guideline_el
.attributes
.get("x")
.ok_or(input_error!("<guideline> missing x"))?
.parse()
.or(Err(input_error!("<guideline> x not float")))?;
let gy = guideline_el
.attributes
.get("y")
.ok_or(input_error!("<guideline> missing y"))?
.parse()
.or(Err(input_error!("<guideline> x not float")))?;
let angle: IntegerOrFloat = guideline_el
.attributes
.get("angle")
.ok_or(input_error!("<guideline> missing angle"))?
.as_str()
.try_into()
.or(Err(input_error!("<guideline> angle not float")))?;
let mut guideline = Guideline::from_x_y_angle(gx, gy, angle);
if let Some(color) = guideline_el.attributes.get("color") {
guideline.color = Some(color.parse()?);
}
guideline.name = guideline_el.attributes.get("name").map(|n|n.clone());
guideline.identifier = guideline_el.attributes.get("identifier").map(|i|i.clone());
guidelines.push(guideline);
}
ret.guidelines = guidelines;
if let Some(note_el) = glif.take_child("note") {
note_el.get_text().map(|t|ret.note=Some(t.into_owned()));
}
let mut goutline: GlifOutline = GlifOutline::new();
let outline_el = glif.take_child("outline");
if let Some(mut outline_elu) = outline_el {
while let Some(mut contour_el) = outline_elu.take_child("contour") {
let mut gcontour: GlifContour = Vec::new();
while let Some(point_el) = contour_el.take_child("point") {
let mut gpoint = GlifPoint::new();
gpoint.x = point_el
.attributes
.get("x")
.ok_or(input_error!("<point> missing x"))?
.parse()
.or(Err(input_error!("<point> x not float")))?;
gpoint.y = point_el
.attributes
.get("y")
.ok_or(input_error!("<point> missing y"))?
.parse()
.or(Err(input_error!("<point> y not float")))?;
match point_el.attributes.get("name") {
Some(n) => gpoint.name = Some(n.clone()),
None => {}
}
gpoint.ptype = point_el.attributes.get("type").as_ref().map(|s| s.as_str()).unwrap_or("offcurve").into();
match point_el.attributes.get("smooth") {
Some(s) => if s == "yes" {
if gpoint.ptype != PointType::OffCurve {
gpoint.smooth = true;
} else {
log::error!("Ignoring illogical `smooth=yes` on offcurve point");
}
},
_ => {}
}
if gpoint.ptype.is_valid() {
gcontour.push(gpoint);
} else {
Err(GlifInputError(format!("Shouldn't write <point type={}> to UFO .glif!", gpoint.ptype)))?;
}
}
if gcontour.len() > 0 {
goutline.push(gcontour);
}
}
while let Some(component_el) = outline_elu.take_child("component") {
let mut gcomponent = GlifComponent::new();
load_matrix_and_identifier!(component_el, gcomponent, (xScale, xyScale, yxScale, yScale, xOffset, yOffset));
gcomponent.base = component_el.attributes.get("base").ok_or(input_error!("<component> missing base"))?.clone();
ret.components.vec.push(gcomponent);
}
}
#[cfg(feature = "glifserde")]
if let Some(mut lib) = glif.take_child("lib") {
let mut plist_temp: Vec<u8> = vec![];
lib.name = String::from("plist");
match lib.write(&mut plist_temp).map(|()|plist::from_bytes(&plist_temp)) {
Ok(Ok(lib_p)) => ret.lib = Lib::Plist(lib_p),
Err(e) => {
log::error!("Failed to serialize .glif lib as XML? Error: {:?}", e);
ret.lib = Lib::Xml(lib)
},
Ok(Err(e)) => {
log::error!("Failed to deserialize .glif lib XML as plist? Error: {:?}", e);
}
}
}
#[cfg(not(feature = "glifserde"))]
if let Some(_) = glif.take_child("lib") {
log::warn!("Without glifserde, cannot decode plist!")
}
goutline.figure_type();
ret.order = goutline.otype.into();
let outline: Outline<PD> = goutline.try_into()?;
if outline.len() > 0 || ret.components.vec.len() > 0 {
ret.outline = Some(outline);
}
Ok(ret)
}Trait Implementations§
source§impl<'de, PD> Deserialize<'de> for Anchor<PD>where
PD: Deserialize<'de> + Default + PointData,
impl<'de, PD> Deserialize<'de> for Anchor<PD>where
PD: Deserialize<'de> + Default + PointData,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more