text.rs (4862B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 //! Computed types for text properties. 6 7 use crate::derives::*; 8 use crate::values::computed::length::{Length, LengthPercentage}; 9 use crate::values::generics::text::{ 10 GenericHyphenateLimitChars, GenericInitialLetter, GenericTextDecorationInset, 11 GenericTextDecorationLength, GenericTextIndent, 12 }; 13 use crate::values::generics::NumberOrAuto; 14 use crate::values::specified::text as specified; 15 use crate::values::specified::text::{TextEmphasisFillMode, TextEmphasisShapeKeyword}; 16 use crate::values::{CSSFloat, CSSInteger}; 17 use crate::Zero; 18 use std::fmt::{self, Write}; 19 use style_traits::{CssString, CssWriter, ToCss, ToTyped, TypedValue}; 20 21 pub use crate::values::specified::text::{ 22 HyphenateCharacter, LineBreak, MozControlCharacterVisibility, OverflowWrap, RubyPosition, 23 TextAlignLast, TextAutospace, TextDecorationLine, TextDecorationSkipInk, TextEmphasisPosition, 24 TextJustify, TextOverflow, TextTransform, TextUnderlinePosition, WordBreak, 25 }; 26 27 /// A computed value for the `initial-letter` property. 28 pub type InitialLetter = GenericInitialLetter<CSSFloat, CSSInteger>; 29 30 /// Implements type for `text-decoration-thickness` property. 31 pub type TextDecorationLength = GenericTextDecorationLength<LengthPercentage>; 32 33 /// Implements type for `text-decoration-inset` property. 34 pub type TextDecorationInset = GenericTextDecorationInset<Length>; 35 36 /// The computed value of `text-align`. 37 pub type TextAlign = specified::TextAlignKeyword; 38 39 /// The computed value of `text-indent`. 40 pub type TextIndent = GenericTextIndent<LengthPercentage>; 41 42 /// A computed value for the `hyphenate-character` property. 43 pub type HyphenateLimitChars = GenericHyphenateLimitChars<CSSInteger>; 44 45 impl HyphenateLimitChars { 46 /// Return the `auto` value, which has all three component values as `auto`. 47 #[inline] 48 pub fn auto() -> Self { 49 Self { 50 total_word_length: NumberOrAuto::Auto, 51 pre_hyphen_length: NumberOrAuto::Auto, 52 post_hyphen_length: NumberOrAuto::Auto, 53 } 54 } 55 } 56 57 /// A computed value for the `letter-spacing` property. 58 #[repr(transparent)] 59 #[derive( 60 Animate, 61 Clone, 62 ComputeSquaredDistance, 63 Copy, 64 Debug, 65 MallocSizeOf, 66 PartialEq, 67 ToAnimatedValue, 68 ToAnimatedZero, 69 ToResolvedValue, 70 )] 71 pub struct GenericLetterSpacing<L>(pub L); 72 /// This is generic just to make the #[derive()] code do the right thing for lengths. 73 pub type LetterSpacing = GenericLetterSpacing<LengthPercentage>; 74 75 impl LetterSpacing { 76 /// Return the `normal` computed value, which is just zero. 77 #[inline] 78 pub fn normal() -> Self { 79 Self(LengthPercentage::zero()) 80 } 81 } 82 83 impl ToCss for LetterSpacing { 84 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result 85 where 86 W: Write, 87 { 88 // https://drafts.csswg.org/css-text/#propdef-letter-spacing 89 // 90 // For legacy reasons, a computed letter-spacing of zero yields a 91 // resolved value (getComputedStyle() return value) of normal. 92 if self.0.is_zero() { 93 return dest.write_str("normal"); 94 } 95 self.0.to_css(dest) 96 } 97 } 98 99 impl ToTyped for LetterSpacing { 100 // XXX The specification does not currently define how this property should 101 // be reified into Typed OM. The current behavior follows existing WPT 102 // coverage (letter-spacing.html). We may file a spec issue once more data 103 // is collected to update the Property-specific Rules section to align with 104 // observed test expectations. 105 fn to_typed(&self) -> Option<TypedValue> { 106 if self.0.is_zero() { 107 return Some(TypedValue::Keyword(CssString::from("normal"))); 108 } 109 // XXX According to the test, should return TypedValue::Numeric with 110 // unit "px" or "percent" once that variant is available. Tracked in 111 // bug 1990419. 112 None 113 } 114 } 115 116 /// A computed value for the `word-spacing` property. 117 pub type WordSpacing = LengthPercentage; 118 119 impl WordSpacing { 120 /// Return the `normal` computed value, which is just zero. 121 #[inline] 122 pub fn normal() -> Self { 123 LengthPercentage::zero() 124 } 125 } 126 127 /// Computed value for the text-emphasis-style property 128 #[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss, ToResolvedValue, ToTyped)] 129 #[allow(missing_docs)] 130 #[repr(C, u8)] 131 pub enum TextEmphasisStyle { 132 /// [ <fill> || <shape> ] 133 Keyword { 134 #[css(skip_if = "TextEmphasisFillMode::is_filled")] 135 fill: TextEmphasisFillMode, 136 shape: TextEmphasisShapeKeyword, 137 }, 138 /// `none` 139 None, 140 /// `<string>` (of which only the first grapheme cluster will be used). 141 String(crate::OwnedStr), 142 }