Bug 1069761 - Move AppendSerializedFontSrc to nsStyleUtil. r=jdaggett

This commit is contained in:
Cameron McCormack 2014-09-22 10:32:58 +10:00
parent c4d3b0059d
commit a2eb916c20
3 changed files with 59 additions and 54 deletions

View File

@ -1397,59 +1397,6 @@ DOMCI_DATA(CSSNameSpaceRule, css::NameSpaceRule)
// nsCSSFontFaceStyleDecl and related routines
//
// A src: descriptor is represented as an array value; each entry in
// the array can be eCSSUnit_URL, eCSSUnit_Local_Font, or
// eCSSUnit_Font_Format. Blocks of eCSSUnit_Font_Format may appear
// only after one of the first two. (css3-fonts only contemplates
// annotating URLs with formats, but we handle the general case.)
static void
AppendSerializedFontSrc(const nsCSSValue& src, nsAString & aResult)
{
NS_PRECONDITION(src.GetUnit() == eCSSUnit_Array,
"improper value unit for src:");
const nsCSSValue::Array& sources = *src.GetArrayValue();
size_t i = 0;
while (i < sources.Count()) {
nsAutoString formats;
if (sources[i].GetUnit() == eCSSUnit_URL) {
aResult.AppendLiteral("url(");
nsDependentString url(sources[i].GetOriginalURLValue());
nsStyleUtil::AppendEscapedCSSString(url, aResult);
aResult.Append(')');
} else if (sources[i].GetUnit() == eCSSUnit_Local_Font) {
aResult.AppendLiteral("local(");
nsDependentString local(sources[i].GetStringBufferValue());
nsStyleUtil::AppendEscapedCSSString(local, aResult);
aResult.Append(')');
} else {
NS_NOTREACHED("entry in src: descriptor with improper unit");
i++;
continue;
}
i++;
formats.Truncate();
while (i < sources.Count() &&
sources[i].GetUnit() == eCSSUnit_Font_Format) {
formats.Append('"');
formats.Append(sources[i].GetStringBufferValue());
formats.AppendLiteral("\", ");
i++;
}
if (formats.Length() > 0) {
formats.Truncate(formats.Length() - 2); // remove the last comma
aResult.AppendLiteral(" format(");
aResult.Append(formats);
aResult.Append(')');
}
aResult.AppendLiteral(", ");
}
aResult.Truncate(aResult.Length() - 2); // remove the last comma-space
}
// Mapping from nsCSSFontDesc codes to nsCSSFontFaceStyleDecl fields.
nsCSSValue nsCSSFontFaceStyleDecl::* const
nsCSSFontFaceStyleDecl::Fields[] = {
@ -1531,7 +1478,7 @@ nsCSSFontFaceStyleDecl::GetPropertyValue(nsCSSFontDesc aFontDescID,
return NS_OK;
case eCSSFontDesc_Src:
AppendSerializedFontSrc(val, aResult);
nsStyleUtil::AppendSerializedFontSrc(val, aResult);
return NS_OK;
case eCSSFontDesc_UnicodeRange:

View File

@ -507,6 +507,61 @@ nsStyleUtil::AppendUnicodeRange(const nsCSSValue& aValue, nsAString& aResult)
CopyASCIItoUTF16(buf, aResult);
}
/* static */ void
nsStyleUtil::AppendSerializedFontSrc(const nsCSSValue& aValue,
nsAString& aResult)
{
// A src: descriptor is represented as an array value; each entry in
// the array can be eCSSUnit_URL, eCSSUnit_Local_Font, or
// eCSSUnit_Font_Format. Blocks of eCSSUnit_Font_Format may appear
// only after one of the first two. (css3-fonts only contemplates
// annotating URLs with formats, but we handle the general case.)
NS_PRECONDITION(aValue.GetUnit() == eCSSUnit_Array,
"improper value unit for src:");
const nsCSSValue::Array& sources = *aValue.GetArrayValue();
size_t i = 0;
while (i < sources.Count()) {
nsAutoString formats;
if (sources[i].GetUnit() == eCSSUnit_URL) {
aResult.AppendLiteral("url(");
nsDependentString url(sources[i].GetOriginalURLValue());
nsStyleUtil::AppendEscapedCSSString(url, aResult);
aResult.Append(')');
} else if (sources[i].GetUnit() == eCSSUnit_Local_Font) {
aResult.AppendLiteral("local(");
nsDependentString local(sources[i].GetStringBufferValue());
nsStyleUtil::AppendEscapedCSSString(local, aResult);
aResult.Append(')');
} else {
NS_NOTREACHED("entry in src: descriptor with improper unit");
i++;
continue;
}
i++;
formats.Truncate();
while (i < sources.Count() &&
sources[i].GetUnit() == eCSSUnit_Font_Format) {
formats.Append('"');
formats.Append(sources[i].GetStringBufferValue());
formats.AppendLiteral("\", ");
i++;
}
if (formats.Length() > 0) {
formats.Truncate(formats.Length() - 2); // remove the last comma
aResult.AppendLiteral(" format(");
aResult.Append(formats);
aResult.Append(')');
}
aResult.AppendLiteral(", ");
}
aResult.Truncate(aResult.Length() - 2); // remove the last comma-space
}
/* static */ float
nsStyleUtil::ColorComponentToFloat(uint8_t aAlpha)
{

View File

@ -71,6 +71,9 @@ public:
aResult.AppendFloat(aNumber);
}
static void AppendSerializedFontSrc(const nsCSSValue& aValue,
nsAString& aResult);
// convert bitmask value to keyword name for a functional alternate
static void GetFunctionalAlternatesName(int32_t aFeature,
nsAString& aFeatureName);