1use cast::{f64, u16};
7use cssparser::{Parser, Token};
8
9use crate::error::*;
10use crate::length::*;
11use crate::parse_identifiers;
12use crate::parsers::{Parse, finite_f32};
13use crate::properties::ComputedValues;
14use crate::property_defs::{FontStretch, FontStyle, FontVariant};
15
16#[derive(Debug, Clone, PartialEq)]
27pub enum Font {
28 Caption,
29 Icon,
30 Menu,
31 MessageBox,
32 SmallCaption,
33 StatusBar,
34 Spec(FontSpec),
35}
36
37#[derive(Debug, Default, Clone, PartialEq)]
39pub struct FontSpec {
40 pub style: FontStyle,
41 pub variant: FontVariant,
42 pub weight: FontWeight,
43 pub stretch: FontStretch,
44 pub size: FontSize,
45 pub line_height: LineHeight,
46 pub family: FontFamily,
47}
48
49impl Parse for Font {
50 fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Font, ParseError<'i>> {
51 if let Ok(f) = parse_font_spec_identifiers(parser) {
52 return Ok(f);
53 }
54
55 let mut nb_normals = 0;
58 let mut style = None;
59 let mut variant_caps = None;
60 let mut weight = None;
61 let mut stretch = None;
62 let size;
63
64 loop {
65 if parser
69 .try_parse(|input| input.expect_ident_matching("normal"))
70 .is_ok()
71 {
72 nb_normals += 1;
73 continue;
74 }
75 if style.is_none()
76 && let Ok(value) = parser.try_parse(FontStyle::parse)
77 {
78 style = Some(value);
79 continue;
80 }
81
82 if weight.is_none()
83 && let Ok(value) = parser.try_parse(FontWeight::parse)
84 {
85 weight = Some(value);
86 continue;
87 }
88
89 if variant_caps.is_none()
90 && let Ok(value) = parser.try_parse(FontVariant::parse)
91 {
92 variant_caps = Some(value);
93 continue;
94 }
95
96 if stretch.is_none()
97 && let Ok(value) = parser.try_parse(FontStretch::parse)
98 {
99 stretch = Some(value);
100 continue;
101 }
102
103 size = FontSize::parse(parser)?;
104 break;
105 }
106
107 let line_height = if parser.try_parse(|input| input.expect_delim('/')).is_ok() {
108 Some(LineHeight::parse(parser)?)
109 } else {
110 None
111 };
112
113 #[inline]
114 fn count<T>(opt: &Option<T>) -> u8 {
115 if opt.is_some() { 1 } else { 0 }
116 }
117
118 if (count(&style) + count(&weight) + count(&variant_caps) + count(&stretch) + nb_normals)
119 > 4
120 {
121 return Err(parser.new_custom_error(ValueErrorKind::parse_error(
122 "invalid syntax for 'font' property",
123 )));
124 }
125
126 let family = FontFamily::parse(parser)?;
127
128 Ok(Font::Spec(FontSpec {
129 style: style.unwrap_or_default(),
130 variant: variant_caps.unwrap_or_default(),
131 weight: weight.unwrap_or_default(),
132 stretch: stretch.unwrap_or_default(),
133 size,
134 line_height: line_height.unwrap_or_default(),
135 family,
136 }))
137 }
138}
139
140impl Font {
141 pub fn to_font_spec(&self) -> FontSpec {
142 match *self {
143 Font::Caption
144 | Font::Icon
145 | Font::Menu
146 | Font::MessageBox
147 | Font::SmallCaption
148 | Font::StatusBar => {
149 FontSpec::default()
151 }
152
153 Font::Spec(ref spec) => spec.clone(),
154 }
155 }
156}
157
158#[rustfmt::skip]
160fn parse_font_spec_identifiers<'i>(parser: &mut Parser<'i, '_>) -> Result<Font, ParseError<'i>> {
161 Ok(parser.try_parse(|p| {
162 parse_identifiers! {
163 p,
164 "caption" => Font::Caption,
165 "icon" => Font::Icon,
166 "menu" => Font::Menu,
167 "message-box" => Font::MessageBox,
168 "small-caption" => Font::SmallCaption,
169 "status-bar" => Font::StatusBar,
170 }
171 })?)
172}
173
174#[allow(clippy::upper_case_acronyms)]
182#[derive(Debug, Clone, PartialEq)]
183pub enum FontSize {
184 Smaller,
185 Larger,
186 XXSmall,
187 XSmall,
188 Small,
189 Medium,
190 Large,
191 XLarge,
192 XXLarge,
193 Value(Length<Both>),
194}
195
196impl FontSize {
197 pub fn value(&self) -> Length<Both> {
198 match self {
199 FontSize::Value(s) => *s,
200 _ => unreachable!(),
201 }
202 }
203
204 pub fn compute(&self, v: &ComputedValues) -> Self {
205 let compute_points = |p| 12.0 * 1.2f64.powf(p) / POINTS_PER_INCH;
206
207 let parent = v.font_size().value();
208
209 assert!(
211 parent.unit != LengthUnit::Percent
212 && parent.unit != LengthUnit::Em
213 && parent.unit != LengthUnit::Ex
214 );
215
216 use FontSize::*;
217
218 #[rustfmt::skip]
219 let new_size = match self {
220 Smaller => Length::<Both>::new(parent.length / 1.2, parent.unit),
221 Larger => Length::<Both>::new(parent.length * 1.2, parent.unit),
222 XXSmall => Length::<Both>::new(compute_points(-3.0), LengthUnit::In),
223 XSmall => Length::<Both>::new(compute_points(-2.0), LengthUnit::In),
224 Small => Length::<Both>::new(compute_points(-1.0), LengthUnit::In),
225 Medium => Length::<Both>::new(compute_points(0.0), LengthUnit::In),
226 Large => Length::<Both>::new(compute_points(1.0), LengthUnit::In),
227 XLarge => Length::<Both>::new(compute_points(2.0), LengthUnit::In),
228 XXLarge => Length::<Both>::new(compute_points(3.0), LengthUnit::In),
229
230 Value(s) if s.unit == LengthUnit::Percent => {
231 Length::<Both>::new(parent.length * s.length, parent.unit)
232 }
233
234 Value(s) if s.unit == LengthUnit::Em => {
235 Length::<Both>::new(parent.length * s.length, parent.unit)
236 }
237
238 Value(s) if s.unit == LengthUnit::Ex => {
239 Length::<Both>::new(parent.length * s.length / 2.0, parent.unit)
242 }
243
244 Value(s) => *s,
245 };
246
247 FontSize::Value(new_size)
248 }
249
250 pub fn to_user(&self, params: &NormalizeParams) -> f64 {
251 self.value().to_user(params)
252 }
253}
254
255impl Parse for FontSize {
256 #[rustfmt::skip]
257 fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<FontSize, ParseError<'i>> {
258 parser
259 .try_parse(|p| Length::<Both>::parse(p))
260 .map(FontSize::Value)
261 .or_else(|_| {
262 Ok(parse_identifiers!(
263 parser,
264 "smaller" => FontSize::Smaller,
265 "larger" => FontSize::Larger,
266 "xx-small" => FontSize::XXSmall,
267 "x-small" => FontSize::XSmall,
268 "small" => FontSize::Small,
269 "medium" => FontSize::Medium,
270 "large" => FontSize::Large,
271 "x-large" => FontSize::XLarge,
272 "xx-large" => FontSize::XXLarge,
273 )?)
274 })
275 }
276}
277
278#[derive(Debug, Copy, Clone, PartialEq)]
284pub enum FontWeight {
285 Normal,
286 Bold,
287 Bolder,
288 Lighter,
289 Weight(u16),
290}
291
292impl Parse for FontWeight {
293 fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<FontWeight, ParseError<'i>> {
294 parser
295 .try_parse(|p| {
296 Ok(parse_identifiers!(
297 p,
298 "normal" => FontWeight::Normal,
299 "bold" => FontWeight::Bold,
300 "bolder" => FontWeight::Bolder,
301 "lighter" => FontWeight::Lighter,
302 )?)
303 })
304 .or_else(|_: ParseError<'_>| {
305 let loc = parser.current_source_location();
306 let i = parser.expect_integer()?;
307 if (1..=1000).contains(&i) {
308 Ok(FontWeight::Weight(u16(i).unwrap()))
309 } else {
310 Err(loc.new_custom_error(ValueErrorKind::value_error(
311 "value must be between 1 and 1000 inclusive",
312 )))
313 }
314 })
315 }
316}
317
318impl FontWeight {
319 #[rustfmt::skip]
320 pub fn compute(&self, v: &Self) -> Self {
321 use FontWeight::*;
322
323 match *self {
329 Bolder => match v.numeric_weight() {
330 w if ( 1..100).contains(&w) => Weight(400),
331 w if (100..350).contains(&w) => Weight(400),
332 w if (350..550).contains(&w) => Weight(700),
333 w if (550..750).contains(&w) => Weight(900),
334 w if (750..900).contains(&w) => Weight(900),
335 w if 900 <= w => Weight(w),
336
337 _ => unreachable!(),
338 }
339
340 Lighter => match v.numeric_weight() {
341 w if ( 1..100).contains(&w) => Weight(w),
342 w if (100..350).contains(&w) => Weight(100),
343 w if (350..550).contains(&w) => Weight(100),
344 w if (550..750).contains(&w) => Weight(400),
345 w if (750..900).contains(&w) => Weight(700),
346 w if 900 <= w => Weight(700),
347
348 _ => unreachable!(),
349 }
350
351 _ => *self,
352 }
353 }
354
355 pub fn numeric_weight(self) -> u16 {
357 use FontWeight::*;
358
359 match self {
362 Normal => 400,
363 Bold => 700,
364 Bolder | Lighter => unreachable!(),
365 Weight(w) => w,
366 }
367 }
368}
369
370#[derive(Debug, Clone, PartialEq)]
376pub enum LetterSpacing {
377 Normal,
378 Value(Length<Horizontal>),
379}
380
381impl LetterSpacing {
382 pub fn value(&self) -> Length<Horizontal> {
383 match self {
384 LetterSpacing::Value(s) => *s,
385 _ => unreachable!(),
386 }
387 }
388
389 pub fn compute(&self) -> Self {
390 let spacing = match self {
391 LetterSpacing::Normal => Length::<Horizontal>::new(0.0, LengthUnit::Px),
392 LetterSpacing::Value(s) => *s,
393 };
394
395 LetterSpacing::Value(spacing)
396 }
397
398 pub fn to_user(&self, params: &NormalizeParams) -> f64 {
399 self.value().to_user(params)
400 }
401}
402
403impl Parse for LetterSpacing {
404 fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<LetterSpacing, ParseError<'i>> {
405 parser
406 .try_parse(|p| Length::<Horizontal>::parse(p))
407 .map(LetterSpacing::Value)
408 .or_else(|_| {
409 Ok(parse_identifiers!(
410 parser,
411 "normal" => LetterSpacing::Normal,
412 )?)
413 })
414 }
415}
416
417#[derive(Debug, Clone, PartialEq)]
421pub enum LineHeight {
422 Normal,
423 Number(f32),
424 Length(Length<Both>),
425 Percentage(f32),
426}
427
428impl LineHeight {
429 pub fn value(&self) -> Length<Both> {
430 match self {
431 LineHeight::Length(l) => *l,
432 _ => unreachable!(),
433 }
434 }
435
436 pub fn compute(&self, values: &ComputedValues) -> Self {
437 let font_size = values.font_size().value();
438
439 match *self {
440 LineHeight::Normal => LineHeight::Length(font_size),
441
442 LineHeight::Number(f) | LineHeight::Percentage(f) => {
443 LineHeight::Length(Length::new(font_size.length * f64(f), font_size.unit))
444 }
445
446 LineHeight::Length(l) => LineHeight::Length(l),
447 }
448 }
449
450 pub fn to_user(&self, params: &NormalizeParams) -> f64 {
451 self.value().to_user(params)
452 }
453}
454
455impl Parse for LineHeight {
456 fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<LineHeight, ParseError<'i>> {
457 let state = parser.state();
458 let loc = parser.current_source_location();
459
460 let token = parser.next()?.clone();
461
462 match token {
463 Token::Ident(ref cow) => {
464 if cow.eq_ignore_ascii_case("normal") {
465 Ok(LineHeight::Normal)
466 } else {
467 Err(parser
468 .new_basic_unexpected_token_error(token.clone())
469 .into())
470 }
471 }
472
473 Token::Number { value, .. } => Ok(LineHeight::Number(
474 finite_f32(value).map_err(|e| loc.new_custom_error(e))?,
475 )),
476
477 Token::Percentage { unit_value, .. } => Ok(LineHeight::Percentage(unit_value)),
478
479 Token::Dimension { .. } => {
480 parser.reset(&state);
481 Ok(LineHeight::Length(Length::<Both>::parse(parser)?))
482 }
483
484 _ => Err(parser.new_basic_unexpected_token_error(token).into()),
485 }
486 }
487}
488
489#[derive(Debug, Clone, PartialEq)]
497pub struct FontFamily(pub String);
498
499impl Parse for FontFamily {
500 fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<FontFamily, ParseError<'i>> {
501 let loc = parser.current_source_location();
502
503 let fonts = parser.parse_comma_separated(|parser| {
504 if let Ok(cow) = parser.try_parse(|p| p.expect_string_cloned()) {
505 if cow.is_empty() {
506 return Err(loc.new_custom_error(ValueErrorKind::value_error(
507 "empty string is not a valid font family name",
508 )));
509 }
510
511 return Ok(cow);
512 }
513
514 let first_ident = parser.expect_ident()?.clone();
515 let mut value = first_ident.as_ref().to_owned();
516
517 while let Ok(cow) = parser.try_parse(|p| p.expect_ident_cloned()) {
518 value.push(' ');
519 value.push_str(&cow);
520 }
521 Ok(cssparser::CowRcStr::from(value))
522 })?;
523
524 Ok(FontFamily(fonts.join(",")))
525 }
526}
527
528impl FontFamily {
529 pub fn as_str(&self) -> &str {
530 &self.0
531 }
532}
533
534#[derive(Debug, Copy, Clone, PartialEq)]
545pub enum GlyphOrientationVertical {
546 Auto,
547 Angle0,
548 Angle90,
549}
550
551impl Parse for GlyphOrientationVertical {
552 fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<GlyphOrientationVertical, ParseError<'i>> {
553 let loc = parser.current_source_location();
554
555 if parser
556 .try_parse(|p| p.expect_ident_matching("auto"))
557 .is_ok()
558 {
559 return Ok(GlyphOrientationVertical::Auto);
560 }
561
562 let token = parser.next()?.clone();
563
564 match token {
569 Token::Number {
570 int_value: Some(0), ..
571 } => Ok(GlyphOrientationVertical::Angle0),
572
573 Token::Number {
574 int_value: Some(90),
575 ..
576 } => Ok(GlyphOrientationVertical::Angle90),
577
578 Token::Dimension {
579 int_value: Some(0),
580 unit,
581 ..
582 } if unit.eq_ignore_ascii_case("deg") => Ok(GlyphOrientationVertical::Angle0),
583
584 Token::Dimension {
585 int_value: Some(90),
586 unit,
587 ..
588 } if unit.eq_ignore_ascii_case("deg") => Ok(GlyphOrientationVertical::Angle90),
589 _ => Err(loc.new_unexpected_token_error(token.clone())),
590 }
591 }
592}
593
594#[cfg(test)]
595mod tests {
596 use super::*;
597
598 use crate::properties::{ParsedProperty, SpecifiedValue, SpecifiedValues};
599
600 #[test]
601 fn parses_font_shorthand() {
602 assert_eq!(
603 Font::parse_str("small-caption").unwrap(),
604 Font::SmallCaption,
605 );
606
607 assert_eq!(
608 Font::parse_str("italic bold 12px sans").unwrap(),
609 Font::Spec(FontSpec {
610 style: FontStyle::Italic,
611 variant: Default::default(),
612 weight: FontWeight::Bold,
613 stretch: Default::default(),
614 size: FontSize::Value(Length::new(12.0, LengthUnit::Px)),
615 line_height: Default::default(),
616 family: FontFamily("sans".to_string()),
617 }),
618 );
619
620 assert_eq!(
621 Font::parse_str("bold 14cm/2 serif").unwrap(),
622 Font::Spec(FontSpec {
623 style: Default::default(),
624 variant: Default::default(),
625 weight: FontWeight::Bold,
626 stretch: Default::default(),
627 size: FontSize::Value(Length::new(14.0, LengthUnit::Cm)),
628 line_height: LineHeight::Number(2.0),
629 family: FontFamily("serif".to_string()),
630 }),
631 );
632
633 assert_eq!(
634 Font::parse_str("small-caps condensed 12pt serif").unwrap(),
635 Font::Spec(FontSpec {
636 style: Default::default(),
637 variant: FontVariant::SmallCaps,
638 weight: FontWeight::Normal,
639 stretch: FontStretch::Condensed,
640 size: FontSize::Value(Length::new(12.0, LengthUnit::Pt)),
641 line_height: Default::default(),
642 family: FontFamily("serif".to_string()),
643 }),
644 );
645 }
646
647 #[test]
648 fn parses_font_shorthand_with_normal_values() {
649 let expected_font = Font::Spec(FontSpec {
650 style: Default::default(),
651 variant: Default::default(),
652 weight: Default::default(),
653 stretch: Default::default(),
654 size: FontSize::Value(Length::new(12.0, LengthUnit::Pt)),
655 line_height: Default::default(),
656 family: FontFamily("serif".to_string()),
657 });
658
659 assert_eq!(Font::parse_str("normal 12pt serif").unwrap(), expected_font,);
662 assert_eq!(
663 Font::parse_str("normal normal 12pt serif").unwrap(),
664 expected_font,
665 );
666 assert_eq!(
667 Font::parse_str("normal normal normal 12pt serif").unwrap(),
668 expected_font,
669 );
670 assert_eq!(
671 Font::parse_str("normal normal normal normal 12pt serif").unwrap(),
672 expected_font,
673 );
674
675 assert!(Font::parse_str("normal normal normal normal normal 12pt serif").is_err());
677
678 assert_eq!(
680 Font::parse_str("normal bold normal 12pt serif").unwrap(),
681 Font::Spec(FontSpec {
682 style: Default::default(),
683 variant: Default::default(),
684 weight: FontWeight::Bold,
685 stretch: Default::default(),
686 size: FontSize::Value(Length::new(12.0, LengthUnit::Pt)),
687 line_height: Default::default(),
688 family: FontFamily("serif".to_string()),
689 }),
690 );
691 }
692
693 #[test]
694 fn detects_invalid_invalid_font_size() {
695 assert!(FontSize::parse_str("furlong").is_err());
696 }
697
698 #[test]
699 fn computes_parent_relative_font_size() {
700 let mut specified = SpecifiedValues::default();
701 specified.set_parsed_property(&ParsedProperty::FontSize(SpecifiedValue::Specified(
702 FontSize::parse_str("10px").unwrap(),
703 )));
704
705 let mut values = ComputedValues::default();
706 specified.to_computed_values(&mut values);
707
708 assert_eq!(
709 FontSize::parse_str("150%").unwrap().compute(&values),
710 FontSize::parse_str("15px").unwrap()
711 );
712
713 assert_eq!(
714 FontSize::parse_str("1.5em").unwrap().compute(&values),
715 FontSize::parse_str("15px").unwrap()
716 );
717
718 assert_eq!(
719 FontSize::parse_str("1ex").unwrap().compute(&values),
720 FontSize::parse_str("5px").unwrap()
721 );
722
723 let smaller = FontSize::parse_str("smaller").unwrap().compute(&values);
724 if let FontSize::Value(v) = smaller {
725 assert!(v.length < 10.0);
726 assert_eq!(v.unit, LengthUnit::Px);
727 } else {
728 unreachable!();
729 }
730
731 let larger = FontSize::parse_str("larger").unwrap().compute(&values);
732 if let FontSize::Value(v) = larger {
733 assert!(v.length > 10.0);
734 assert_eq!(v.unit, LengthUnit::Px);
735 } else {
736 unreachable!();
737 }
738 }
739
740 #[test]
741 fn parses_font_weight() {
742 assert_eq!(
743 <FontWeight as Parse>::parse_str("normal").unwrap(),
744 FontWeight::Normal
745 );
746 assert_eq!(
747 <FontWeight as Parse>::parse_str("bold").unwrap(),
748 FontWeight::Bold
749 );
750 assert_eq!(
751 <FontWeight as Parse>::parse_str("100").unwrap(),
752 FontWeight::Weight(100)
753 );
754 }
755
756 #[test]
757 fn detects_invalid_font_weight() {
758 assert!(<FontWeight as Parse>::parse_str("").is_err());
759 assert!(<FontWeight as Parse>::parse_str("strange").is_err());
760 assert!(<FontWeight as Parse>::parse_str("0").is_err());
761 assert!(<FontWeight as Parse>::parse_str("-1").is_err());
762 assert!(<FontWeight as Parse>::parse_str("1001").is_err());
763 assert!(<FontWeight as Parse>::parse_str("3.14").is_err());
764 }
765
766 #[test]
767 fn parses_letter_spacing() {
768 assert_eq!(
769 <LetterSpacing as Parse>::parse_str("normal").unwrap(),
770 LetterSpacing::Normal
771 );
772 assert_eq!(
773 <LetterSpacing as Parse>::parse_str("10em").unwrap(),
774 LetterSpacing::Value(Length::<Horizontal>::new(10.0, LengthUnit::Em,))
775 );
776 }
777
778 #[test]
779 fn computes_letter_spacing() {
780 assert_eq!(
781 <LetterSpacing as Parse>::parse_str("normal")
782 .map(|s| s.compute())
783 .unwrap(),
784 LetterSpacing::Value(Length::<Horizontal>::new(0.0, LengthUnit::Px,))
785 );
786 assert_eq!(
787 <LetterSpacing as Parse>::parse_str("10em")
788 .map(|s| s.compute())
789 .unwrap(),
790 LetterSpacing::Value(Length::<Horizontal>::new(10.0, LengthUnit::Em,))
791 );
792 }
793
794 #[test]
795 fn detects_invalid_invalid_letter_spacing() {
796 assert!(LetterSpacing::parse_str("furlong").is_err());
797 }
798
799 #[test]
800 fn parses_font_family() {
801 assert_eq!(
802 <FontFamily as Parse>::parse_str("'Hello world'").unwrap(),
803 FontFamily("Hello world".to_owned())
804 );
805
806 assert_eq!(
807 <FontFamily as Parse>::parse_str("\"Hello world\"").unwrap(),
808 FontFamily("Hello world".to_owned())
809 );
810
811 assert_eq!(
812 <FontFamily as Parse>::parse_str("\"Hello world with spaces\"").unwrap(),
813 FontFamily("Hello world with spaces".to_owned())
814 );
815
816 assert_eq!(
817 <FontFamily as Parse>::parse_str(" Hello world ").unwrap(),
818 FontFamily("Hello world".to_owned())
819 );
820
821 assert_eq!(
822 <FontFamily as Parse>::parse_str("Plonk").unwrap(),
823 FontFamily("Plonk".to_owned())
824 );
825 }
826
827 #[test]
828 fn parses_multiple_font_family() {
829 assert_eq!(
830 <FontFamily as Parse>::parse_str("serif,monospace,\"Hello world\", with spaces ")
831 .unwrap(),
832 FontFamily("serif,monospace,Hello world,with spaces".to_owned())
833 );
834 }
835
836 #[test]
837 fn detects_invalid_font_family() {
838 assert!(<FontFamily as Parse>::parse_str("").is_err());
839 assert!(<FontFamily as Parse>::parse_str("''").is_err());
840 assert!(<FontFamily as Parse>::parse_str("42").is_err());
841 }
842
843 #[test]
844 fn parses_line_height() {
845 assert_eq!(
846 <LineHeight as Parse>::parse_str("normal").unwrap(),
847 LineHeight::Normal
848 );
849
850 assert_eq!(
851 <LineHeight as Parse>::parse_str("2").unwrap(),
852 LineHeight::Number(2.0)
853 );
854
855 assert_eq!(
856 <LineHeight as Parse>::parse_str("2cm").unwrap(),
857 LineHeight::Length(Length::new(2.0, LengthUnit::Cm))
858 );
859
860 assert_eq!(
861 <LineHeight as Parse>::parse_str("150%").unwrap(),
862 LineHeight::Percentage(1.5)
863 );
864 }
865
866 #[test]
867 fn detects_invalid_line_height() {
868 assert!(<LineHeight as Parse>::parse_str("").is_err());
869 assert!(<LineHeight as Parse>::parse_str("florp").is_err());
870 assert!(<LineHeight as Parse>::parse_str("3florp").is_err());
871 }
872
873 #[test]
874 fn computes_line_height() {
875 let mut specified = SpecifiedValues::default();
876 specified.set_parsed_property(&ParsedProperty::FontSize(SpecifiedValue::Specified(
877 FontSize::parse_str("10px").unwrap(),
878 )));
879
880 let mut values = ComputedValues::default();
881 specified.to_computed_values(&mut values);
882
883 assert_eq!(
884 LineHeight::Normal.compute(&values),
885 LineHeight::Length(Length::new(10.0, LengthUnit::Px)),
886 );
887
888 assert_eq!(
889 LineHeight::Number(2.0).compute(&values),
890 LineHeight::Length(Length::new(20.0, LengthUnit::Px)),
891 );
892
893 assert_eq!(
894 LineHeight::Length(Length::new(3.0, LengthUnit::Cm)).compute(&values),
895 LineHeight::Length(Length::new(3.0, LengthUnit::Cm)),
896 );
897
898 assert_eq!(
899 LineHeight::parse_str("150%").unwrap().compute(&values),
900 LineHeight::Length(Length::new(15.0, LengthUnit::Px)),
901 );
902 }
903
904 #[test]
905 fn parses_glyph_orientation_vertical() {
906 assert_eq!(
907 <GlyphOrientationVertical as Parse>::parse_str("auto").unwrap(),
908 GlyphOrientationVertical::Auto
909 );
910 assert_eq!(
911 <GlyphOrientationVertical as Parse>::parse_str("0").unwrap(),
912 GlyphOrientationVertical::Angle0
913 );
914 assert_eq!(
915 <GlyphOrientationVertical as Parse>::parse_str("0deg").unwrap(),
916 GlyphOrientationVertical::Angle0
917 );
918 assert_eq!(
919 <GlyphOrientationVertical as Parse>::parse_str("90").unwrap(),
920 GlyphOrientationVertical::Angle90
921 );
922 assert_eq!(
923 <GlyphOrientationVertical as Parse>::parse_str("90deg").unwrap(),
924 GlyphOrientationVertical::Angle90
925 );
926 }
927
928 #[test]
929 fn detects_invalid_glyph_orientation_vertical() {
930 assert!(<GlyphOrientationVertical as Parse>::parse_str("").is_err());
931 assert!(<GlyphOrientationVertical as Parse>::parse_str("0.0").is_err());
932 assert!(<GlyphOrientationVertical as Parse>::parse_str("90.0").is_err());
933 assert!(<GlyphOrientationVertical as Parse>::parse_str("0.0deg").is_err());
934 assert!(<GlyphOrientationVertical as Parse>::parse_str("90.0deg").is_err());
935 assert!(<GlyphOrientationVertical as Parse>::parse_str("0rad").is_err());
936 assert!(<GlyphOrientationVertical as Parse>::parse_str("0.0rad").is_err());
937 }
938}