Skip to content

Commit

Permalink
Bug 1572805 - Use cbindgen for text-emphasis-style. r=boris
Browse files Browse the repository at this point in the history
I sent mozilla/cbindgen#377 since I got sick of
copy-pasting the private default constructor stuff :)

Differential Revision: https://phabricator.services.mozilla.com/D41419
  • Loading branch information
emilio committed Aug 10, 2019
1 parent 14db11b commit 7cab266
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 166 deletions.
6 changes: 3 additions & 3 deletions layout/generic/nsLineLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,7 @@ void nsLineLayout::AdjustLeadings(nsIFrame* spanFrame, PerSpanData* psd,
requiredStartLeading += leadings.mStart;
requiredEndLeading += leadings.mEnd;
}
if (aStyleText->HasTextEmphasis()) {
if (aStyleText->HasEffectiveTextEmphasis()) {
nscoord bsize = GetBSizeOfEmphasisMarks(spanFrame, aInflation);
LogicalSide side = aStyleText->TextEmphasisSide(mRootSpan->mWritingMode);
if (side == eLogicalSideBStart) {
Expand Down Expand Up @@ -2279,7 +2279,7 @@ void nsLineLayout::VerticalAlignFrames(PerSpanData* psd) {
fm, minimumLineBSize, lineWM.IsLineInverted());
nscoord blockEnd = blockStart + minimumLineBSize;

if (mStyleText->HasTextEmphasis()) {
if (mStyleText->HasEffectiveTextEmphasis()) {
nscoord fontMaxHeight = fm->MaxHeight();
nscoord emphasisHeight =
GetBSizeOfEmphasisMarks(spanFrame, inflation);
Expand Down Expand Up @@ -3303,7 +3303,7 @@ void nsLineLayout::RelativePositionFrames(PerSpanData* psd,
// (4) When there are text strokes
if (pfd->mRecomputeOverflow ||
frame->Style()->HasTextDecorationLines() ||
frame->StyleText()->HasTextEmphasis() ||
frame->StyleText()->HasEffectiveTextEmphasis() ||
frame->StyleText()->HasWebkitTextStroke()) {
nsTextFrame* f = static_cast<nsTextFrame*>(frame);
r = f->RecomputeOverflow(mBlockReflowInput->mFrame);
Expand Down
43 changes: 36 additions & 7 deletions layout/generic/nsTextFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,7 @@ already_AddRefed<gfxTextRun> BuildTextRunsScanner::BuildTextRunForFrames(
lastComputedStyle->IsTextCombined()) {
anyTextTransformStyle = true;
}
if (textStyle->HasTextEmphasis()) {
if (textStyle->HasEffectiveTextEmphasis()) {
anyTextEmphasis = true;
}
flags |= GetSpacingFlags(f);
Expand Down Expand Up @@ -5233,10 +5233,38 @@ struct EmphasisMarkInfo {

NS_DECLARE_FRAME_PROPERTY_DELETABLE(EmphasisMarkProperty, EmphasisMarkInfo)

static void ComputeTextEmphasisStyleString(const StyleTextEmphasisStyle& aStyle,
nsAString& aOut) {
MOZ_ASSERT(!aStyle.IsNone());
if (aStyle.IsString()) {
nsDependentCSubstring string = aStyle.AsString().AsString();
AppendUTF8toUTF16(string, aOut);
return;
}
const auto& keyword = aStyle.AsKeyword();
const bool fill = keyword.fill == StyleTextEmphasisFillMode::Filled;
switch (keyword.shape) {
case StyleTextEmphasisShapeKeyword::Dot:
return aOut.AppendLiteral(fill ? u"\u2022" : u"\u25e6");
case StyleTextEmphasisShapeKeyword::Circle:
return aOut.AppendLiteral(fill ? u"\u25cf" : u"\u25cb");
case StyleTextEmphasisShapeKeyword::DoubleCircle:
return aOut.AppendLiteral(fill ? u"\u25c9" : u"\u25ce");
case StyleTextEmphasisShapeKeyword::Triangle:
return aOut.AppendLiteral(fill ? u"\u25b2" : u"\u25b3");
case StyleTextEmphasisShapeKeyword::Sesame:
return aOut.AppendLiteral(fill ? u"\ufe45" : u"\ufe46");
default:
MOZ_ASSERT_UNREACHABLE("Unknown emphasis style shape");
}
}

static already_AddRefed<gfxTextRun> GenerateTextRunForEmphasisMarks(
nsTextFrame* aFrame, gfxFontGroup* aFontGroup,
ComputedStyle* aComputedStyle, const nsStyleText* aStyleText) {
const nsString& emphasisString = aStyleText->mTextEmphasisStyleString;
nsAutoString string;
ComputeTextEmphasisStyleString(aStyleText->mTextEmphasisStyle, string);

RefPtr<DrawTarget> dt = CreateReferenceDrawTarget(aFrame);
auto appUnitsPerDevUnit = aFrame->PresContext()->AppUnitsPerDevPixel();
gfx::ShapedTextFlags flags =
Expand All @@ -5245,9 +5273,9 @@ static already_AddRefed<gfxTextRun> GenerateTextRunForEmphasisMarks(
// The emphasis marks should always be rendered upright per spec.
flags = gfx::ShapedTextFlags::TEXT_ORIENT_VERTICAL_UPRIGHT;
}
return aFontGroup->MakeTextRun<char16_t>(
emphasisString.get(), emphasisString.Length(), dt, appUnitsPerDevUnit,
flags, nsTextFrameUtils::Flags(), nullptr);
return aFontGroup->MakeTextRun<char16_t>(string.get(), string.Length(), dt,
appUnitsPerDevUnit, flags,
nsTextFrameUtils::Flags(), nullptr);
}

static nsRubyFrame* FindFurthestInlineRubyAncestor(nsTextFrame* aFrame) {
Expand All @@ -5265,7 +5293,7 @@ static nsRubyFrame* FindFurthestInlineRubyAncestor(nsTextFrame* aFrame) {
nsRect nsTextFrame::UpdateTextEmphasis(WritingMode aWM,
PropertyProvider& aProvider) {
const nsStyleText* styleText = StyleText();
if (!styleText->HasTextEmphasis()) {
if (!styleText->HasEffectiveTextEmphasis()) {
DeleteProperty(EmphasisMarkProperty());
return nsRect();
}
Expand Down Expand Up @@ -7117,7 +7145,8 @@ void nsTextFrame::DrawText(Range aRange, const gfx::Point& aTextBaselinePt,
// Hide text decorations if we're currently hiding @font-face fallback text
const bool drawDecorations =
!aParams.provider->GetFontGroup()->ShouldSkipDrawing() &&
(decorations.HasDecorationLines() || StyleText()->HasTextEmphasis());
(decorations.HasDecorationLines() ||
StyleText()->HasEffectiveTextEmphasis());
if (drawDecorations) {
DrawTextRunAndDecorations(aRange, aTextBaselinePt, aParams, decorations);
} else {
Expand Down
1 change: 1 addition & 0 deletions layout/style/ServoBindings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ cbindgen-types = [
{ gecko = "StyleGenericTrackListValue", servo = "values::generics::grid::TrackListValue" },
{ gecko = "StyleGenericTrackList", servo = "values::generics::grid::TrackList" },
{ gecko = "StyleGenericGridTemplateComponent", servo = "values::generics::grid::GridTemplateComponent" },
{ gecko = "StyleTextEmphasisStyle", servo = "values::computed::text::TextEmphasisStyle" },
]

mapped-generic-types = [
Expand Down
18 changes: 0 additions & 18 deletions layout/style/nsStyleConsts.h
Original file line number Diff line number Diff line change
Expand Up @@ -730,24 +730,6 @@ enum class StyleWhiteSpace : uint8_t {
(NS_STYLE_TEXT_EMPHASIS_POSITION_UNDER | \
NS_STYLE_TEXT_EMPHASIS_POSITION_RIGHT)

// text-emphasis-style
// Note that filled and none here both have zero as their value. This is
// not an problem because:
// * In specified style, none is represented as eCSSUnit_None.
// * In computed style, 'filled' always has its shape computed, and thus
// the combined value is never zero.
#define NS_STYLE_TEXT_EMPHASIS_STYLE_NONE 0
#define NS_STYLE_TEXT_EMPHASIS_STYLE_FILL_MASK (1 << 3)
#define NS_STYLE_TEXT_EMPHASIS_STYLE_FILLED (0 << 3)
#define NS_STYLE_TEXT_EMPHASIS_STYLE_OPEN (1 << 3)
#define NS_STYLE_TEXT_EMPHASIS_STYLE_SHAPE_MASK 7
#define NS_STYLE_TEXT_EMPHASIS_STYLE_DOT 1
#define NS_STYLE_TEXT_EMPHASIS_STYLE_CIRCLE 2
#define NS_STYLE_TEXT_EMPHASIS_STYLE_DOUBLE_CIRCLE 3
#define NS_STYLE_TEXT_EMPHASIS_STYLE_TRIANGLE 4
#define NS_STYLE_TEXT_EMPHASIS_STYLE_SESAME 5
#define NS_STYLE_TEXT_EMPHASIS_STYLE_STRING 255

// text-rendering
enum class StyleTextRendering : uint8_t {
Auto,
Expand Down
12 changes: 5 additions & 7 deletions layout/style/nsStyleStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3371,7 +3371,6 @@ nsStyleText::nsStyleText(const Document& aDocument)
mTextCombineUpright(NS_STYLE_TEXT_COMBINE_UPRIGHT_NONE),
mControlCharacterVisibility(
nsLayoutUtils::ControlCharVisibilityDefault()),
mTextEmphasisStyle(NS_STYLE_TEXT_EMPHASIS_STYLE_NONE),
mTextRendering(StyleTextRendering::Auto),
mTextEmphasisColor(StyleColor::CurrentColor()),
mWebkitTextFillColor(StyleColor::CurrentColor()),
Expand All @@ -3384,7 +3383,8 @@ nsStyleText::nsStyleText(const Document& aDocument)
mTextIndent(LengthPercentage::Zero()),
mTextUnderlineOffset(LengthOrAuto::Auto()),
mTextDecorationSkipInk(StyleTextDecorationSkipInk::Auto),
mWebkitTextStrokeWidth(0) {
mWebkitTextStrokeWidth(0),
mTextEmphasisStyle(StyleTextEmphasisStyle::None()) {
MOZ_COUNT_CTOR(nsStyleText);
RefPtr<nsAtom> language = aDocument.GetContentLanguageAsAtomForStyle();
mTextEmphasisPosition =
Expand All @@ -3410,7 +3410,6 @@ nsStyleText::nsStyleText(const nsStyleText& aSource)
mTextCombineUpright(aSource.mTextCombineUpright),
mControlCharacterVisibility(aSource.mControlCharacterVisibility),
mTextEmphasisPosition(aSource.mTextEmphasisPosition),
mTextEmphasisStyle(aSource.mTextEmphasisStyle),
mTextRendering(aSource.mTextRendering),
mTextEmphasisColor(aSource.mTextEmphasisColor),
mWebkitTextFillColor(aSource.mWebkitTextFillColor),
Expand All @@ -3424,7 +3423,7 @@ nsStyleText::nsStyleText(const nsStyleText& aSource)
mTextDecorationSkipInk(aSource.mTextDecorationSkipInk),
mWebkitTextStrokeWidth(aSource.mWebkitTextStrokeWidth),
mTextShadow(aSource.mTextShadow),
mTextEmphasisStyleString(aSource.mTextEmphasisStyleString) {
mTextEmphasisStyle(aSource.mTextEmphasisStyle) {
MOZ_COUNT_CTOR(nsStyleText);
}

Expand Down Expand Up @@ -3463,8 +3462,8 @@ nsChangeHint nsStyleText::CalcDifference(const nsStyleText& aNewData) const {
return NS_STYLE_HINT_REFLOW;
}

if (HasTextEmphasis() != aNewData.HasTextEmphasis() ||
(HasTextEmphasis() &&
if (HasEffectiveTextEmphasis() != aNewData.HasEffectiveTextEmphasis() ||
(HasEffectiveTextEmphasis() &&
mTextEmphasisPosition != aNewData.mTextEmphasisPosition)) {
// Text emphasis position change could affect line height calculation.
return nsChangeHint_AllReflowHints | nsChangeHint_RepaintFrame;
Expand All @@ -3482,7 +3481,6 @@ nsChangeHint nsStyleText::CalcDifference(const nsStyleText& aNewData) const {

if (mTextShadow != aNewData.mTextShadow ||
mTextEmphasisStyle != aNewData.mTextEmphasisStyle ||
mTextEmphasisStyleString != aNewData.mTextEmphasisStyleString ||
mWebkitTextStrokeWidth != aNewData.mWebkitTextStrokeWidth) {
hint |= nsChangeHint_UpdateSubtreeOverflow | nsChangeHint_SchedulePaint |
nsChangeHint_RepaintFrame;
Expand Down
15 changes: 11 additions & 4 deletions layout/style/nsStyleStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,6 @@ struct MOZ_NEEDS_MEMMOVABLE_MEMBERS nsStyleText {
uint8_t
mControlCharacterVisibility; // NS_STYLE_CONTROL_CHARACTER_VISIBILITY_*
uint8_t mTextEmphasisPosition; // NS_STYLE_TEXT_EMPHASIS_POSITION_*
uint8_t mTextEmphasisStyle; // NS_STYLE_TEXT_EMPHASIS_STYLE_*
mozilla::StyleTextRendering mTextRendering;
mozilla::StyleColor mTextEmphasisColor;
mozilla::StyleColor mWebkitTextFillColor;
Expand All @@ -1164,8 +1163,7 @@ struct MOZ_NEEDS_MEMMOVABLE_MEMBERS nsStyleText {
nscoord mWebkitTextStrokeWidth; // coord

mozilla::StyleArcSlice<mozilla::StyleSimpleShadow> mTextShadow;

nsString mTextEmphasisStyleString;
mozilla::StyleTextEmphasisStyle mTextEmphasisStyle;

mozilla::StyleWordBreak EffectiveWordBreak() const {
if (mWordBreak == mozilla::StyleWordBreak::BreakWord) {
Expand Down Expand Up @@ -1225,7 +1223,16 @@ struct MOZ_NEEDS_MEMMOVABLE_MEMBERS nsStyleText {
owrap == mozilla::StyleOverflowWrap::Anywhere;
}

bool HasTextEmphasis() const { return !mTextEmphasisStyleString.IsEmpty(); }
bool HasEffectiveTextEmphasis() const {
if (mTextEmphasisStyle.IsNone()) {
return false;
}
if (mTextEmphasisStyle.IsString() &&
mTextEmphasisStyle.AsString().AsString().IsEmpty()) {
return false;
}
return true;
}

bool HasWebkitTextStroke() const { return mWebkitTextStrokeWidth > 0; }

Expand Down
84 changes: 1 addition & 83 deletions servo/components/style/properties/gecko.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2578,97 +2578,15 @@ fn static_assert() {


<%self:impl_trait style_struct_name="InheritedText"
skip_longhands="text-align text-emphasis-style
-webkit-text-stroke-width text-emphasis-position">
skip_longhands="text-align -webkit-text-stroke-width text-emphasis-position">

<% text_align_keyword = Keyword("text-align",
"start end left right center justify -moz-center -moz-left -moz-right char",
gecko_strip_moz_prefix=False) %>
${impl_keyword('text_align', 'mTextAlign', text_align_keyword)}

fn clear_text_emphasis_style_if_string(&mut self) {
if self.gecko.mTextEmphasisStyle == structs::NS_STYLE_TEXT_EMPHASIS_STYLE_STRING as u8 {
self.gecko.mTextEmphasisStyleString.truncate();
self.gecko.mTextEmphasisStyle = structs::NS_STYLE_TEXT_EMPHASIS_STYLE_NONE as u8;
}
}

${impl_simple_type_with_conversion("text_emphasis_position")}

pub fn set_text_emphasis_style(&mut self, v: values::computed::TextEmphasisStyle) {
use crate::values::computed::TextEmphasisStyle;
use crate::values::specified::text::{TextEmphasisFillMode, TextEmphasisShapeKeyword};

self.clear_text_emphasis_style_if_string();
let (te, s) = match v {
TextEmphasisStyle::None => (structs::NS_STYLE_TEXT_EMPHASIS_STYLE_NONE, ""),
TextEmphasisStyle::Keyword { fill, shape } => {
let gecko_fill = match fill {
TextEmphasisFillMode::Filled => structs::NS_STYLE_TEXT_EMPHASIS_STYLE_FILLED,
TextEmphasisFillMode::Open => structs::NS_STYLE_TEXT_EMPHASIS_STYLE_OPEN,
};

let gecko_shape = match shape {
TextEmphasisShapeKeyword::Dot => structs::NS_STYLE_TEXT_EMPHASIS_STYLE_DOT,
TextEmphasisShapeKeyword::Circle => structs::NS_STYLE_TEXT_EMPHASIS_STYLE_CIRCLE,
TextEmphasisShapeKeyword::DoubleCircle => structs::NS_STYLE_TEXT_EMPHASIS_STYLE_DOUBLE_CIRCLE,
TextEmphasisShapeKeyword::Triangle => structs::NS_STYLE_TEXT_EMPHASIS_STYLE_TRIANGLE,
TextEmphasisShapeKeyword::Sesame => structs::NS_STYLE_TEXT_EMPHASIS_STYLE_SESAME,
};

(gecko_shape | gecko_fill, shape.char(fill))
},
TextEmphasisStyle::String(ref s) => {
(structs::NS_STYLE_TEXT_EMPHASIS_STYLE_STRING, &**s)
},
};
self.gecko.mTextEmphasisStyleString.assign_str(s);
self.gecko.mTextEmphasisStyle = te as u8;
}

pub fn copy_text_emphasis_style_from(&mut self, other: &Self) {
self.clear_text_emphasis_style_if_string();
if other.gecko.mTextEmphasisStyle == structs::NS_STYLE_TEXT_EMPHASIS_STYLE_STRING as u8 {
self.gecko.mTextEmphasisStyleString
.assign(&*other.gecko.mTextEmphasisStyleString)
}
self.gecko.mTextEmphasisStyle = other.gecko.mTextEmphasisStyle;
}

pub fn reset_text_emphasis_style(&mut self, other: &Self) {
self.copy_text_emphasis_style_from(other)
}

pub fn clone_text_emphasis_style(&self) -> values::computed::TextEmphasisStyle {
use crate::values::computed::TextEmphasisStyle;
use crate::values::specified::text::{TextEmphasisFillMode, TextEmphasisShapeKeyword};

if self.gecko.mTextEmphasisStyle == structs::NS_STYLE_TEXT_EMPHASIS_STYLE_NONE as u8 {
return TextEmphasisStyle::None;
}

if self.gecko.mTextEmphasisStyle == structs::NS_STYLE_TEXT_EMPHASIS_STYLE_STRING as u8 {
return TextEmphasisStyle::String(self.gecko.mTextEmphasisStyleString.to_string().into());
}

let fill =
self.gecko.mTextEmphasisStyle & structs::NS_STYLE_TEXT_EMPHASIS_STYLE_OPEN as u8 == 0;

let fill = if fill { TextEmphasisFillMode::Filled } else { TextEmphasisFillMode::Open };

let shape =
match self.gecko.mTextEmphasisStyle as u32 & !structs::NS_STYLE_TEXT_EMPHASIS_STYLE_OPEN {
structs::NS_STYLE_TEXT_EMPHASIS_STYLE_DOT => TextEmphasisShapeKeyword::Dot,
structs::NS_STYLE_TEXT_EMPHASIS_STYLE_CIRCLE => TextEmphasisShapeKeyword::Circle,
structs::NS_STYLE_TEXT_EMPHASIS_STYLE_DOUBLE_CIRCLE => TextEmphasisShapeKeyword::DoubleCircle,
structs::NS_STYLE_TEXT_EMPHASIS_STYLE_TRIANGLE => TextEmphasisShapeKeyword::Triangle,
structs::NS_STYLE_TEXT_EMPHASIS_STYLE_SESAME => TextEmphasisShapeKeyword::Sesame,
_ => panic!("Unexpected value in style struct for text-emphasis-style property")
};

TextEmphasisStyle::Keyword { fill, shape }
}

${impl_non_negative_length('_webkit_text_stroke_width',
'mWebkitTextStrokeWidth')}

Expand Down
3 changes: 3 additions & 0 deletions servo/components/style/values/computed/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ impl TextDecorationsInEffect {
}

/// Computed value for the text-emphasis-style property
///
/// cbindgen:derive-tagged-enum-copy-constructor=true
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss, ToResolvedValue)]
#[allow(missing_docs)]
#[repr(C, u8)]
pub enum TextEmphasisStyle {
/// [ <fill> || <shape> ]
Keyword {
Expand Down
Loading

0 comments on commit 7cab266

Please sign in to comment.