Bug 1759686 - patch 4 - Extend ServoCSSParser::ParseFontShorthandForMatching to (optionally) return font size in addition to family/style. r=emilio

When running in a Worker, the canvas2d SetFontInternal method can't call GetFontStyleForServo
because it doesn't have a canvasElement or presShell to pass. But we can use the method
ServoCSSParser::ParseFontShorthandForMatching to parse the canvas 'font' property, if we
extend it so as to also return the size from the font shorthand.

(This is incomplete by itself; Emilio's following patch fixes it up, so the intention is to land them together.)

Depends on D144186

Differential Revision: https://phabricator.services.mozilla.com/D144187
This commit is contained in:
Jonathan Kew 2022-05-03 12:39:09 +00:00
Родитель 0cf6fb1bdd
Коммит 746059b94b
3 изменённых файлов: 53 добавлений и 5 удалений

Просмотреть файл

@ -59,9 +59,12 @@ bool ServoCSSParser::ParseTransformIntoMatrix(const nsACString& aValue,
/* static */
bool ServoCSSParser::ParseFontShorthandForMatching(
const nsACString& aValue, URLExtraData* aUrl, StyleFontFamilyList& aList,
StyleComputedFontStyleDescriptor& aStyle, float& aStretch, float& aWeight) {
StyleComputedFontStyleDescriptor& aStyle, float& aStretch, float& aWeight,
float* aSize) {
float dummySize;
return Servo_ParseFontShorthandForMatching(&aValue, aUrl, &aList, &aStyle,
&aStretch, &aWeight);
&aStretch, &aWeight,
aSize ? aSize : &dummySize);
}
/* static */

Просмотреть файл

@ -119,12 +119,13 @@ class ServoCSSParser {
* @param aStyle The parsed FontStyle. (output)
* @param aStretch The parsed FontStretch. (output)
* @param aWeight The parsed FontWeight. (output)
* @param aSize If non-null, returns the parsed font size. (output)
* @return Whether the value was successfully parsed.
*/
static bool ParseFontShorthandForMatching(
const nsACString& aValue, URLExtraData* aUrl, StyleFontFamilyList& aList,
StyleComputedFontStyleDescriptor& aStyle, float& aStretch,
float& aWeight);
StyleComputedFontStyleDescriptor& aStyle, float& aStretch, float& aWeight,
float* aSize = nullptr);
/**
* Get a URLExtraData from a document.

Просмотреть файл

@ -139,6 +139,7 @@ use style::values::animated::{Animate, Procedure, ToAnimatedZero};
use style::values::computed::font::{FontFamily, FontFamilyList, GenericFontFamily};
use style::values::computed::{self, Context, ToComputedValue};
use style::values::distance::ComputeSquaredDistance;
use style::values::specified::FontSizeKeyword;
use style::values::specified::gecko::IntersectionObserverRootMargin;
use style::values::specified::source_size_list::SourceSizeList;
use style::values::{specified, AtomIdent, CustomIdent, KeyframesName};
@ -6964,13 +6965,15 @@ pub unsafe extern "C" fn Servo_ParseFontShorthandForMatching(
style: &mut ComputedFontStyleDescriptor,
stretch: &mut f32,
weight: &mut f32,
size: &mut f32,
) -> bool {
use style::properties::shorthands::font;
use style::values::computed::font::FontWeight as ComputedFontWeight;
use style::values::generics::font::FontStyle as GenericFontStyle;
use style::values::specified::font::{
FontFamily, FontStretch, FontStyle, FontWeight, SpecifiedFontStyle,
FontFamily, FontSize, FontStretch, FontStyle, FontWeight, SpecifiedFontStyle,
};
use style::values::specified::LengthPercentage;
let string = value.as_str_unchecked();
let mut input = ParserInput::new(&string);
@ -7026,6 +7029,47 @@ pub unsafe extern "C" fn Servo_ParseFontShorthandForMatching(
FontWeight::System(_) => return false,
};
// XXX This is unfinished; see values::specified::FontSize::ToComputedValue
// for a more complete implementation (but we can't use it as-is).
*size = match font.font_size {
FontSize::Length(lp) => {
match lp {
LengthPercentage::Length(len) => {
if let Ok(len) = len.to_computed_pixel_length_without_context() {
len
} else {
return false;
}
},
LengthPercentage::Percentage(_) => return false,
// XXX We should support calc() here.
LengthPercentage::Calc(_) => return false,
}
},
// Map absolute-size keywords to sizes.
// XXX This doesn't account for language- and generic-based sizing
// differences.
// XXX Chrome resolves these differently in offscreen canvas; need to
// check whether that's a bug or is required behavior for some reason.
FontSize::Keyword(info) => {
match info.kw {
FontSizeKeyword::XXSmall => 9.0,
FontSizeKeyword::XSmall => 10.0,
FontSizeKeyword::Small => 13.0,
FontSizeKeyword::Medium => 16.0,
FontSizeKeyword::Large => 18.0,
FontSizeKeyword::XLarge => 24.0,
FontSizeKeyword::XXLarge => 32.0,
FontSizeKeyword::XXXLarge => 48.0,
FontSizeKeyword::None => unreachable!(),
}
}
// smaller, larger not currently supported
FontSize::Smaller => return false,
FontSize::Larger => return false,
FontSize::System(_) => return false,
};
true
}