Update to latest spec changes for ctx.font setter and getter. (Bug 508452) r=bzbarsky

This commit is contained in:
L. David Baron 2010-07-24 12:17:38 -07:00
parent 11a03de1b8
commit 068f7e65f0
2 changed files with 65 additions and 1 deletions

View File

@ -74,6 +74,7 @@
#include "nsCSSParser.h"
#include "nsICSSStyleRule.h"
#include "mozilla/css/Declaration.h"
#include "nsComputedDOMStyle.h"
#include "nsStyleSet.h"
@ -2307,6 +2308,23 @@ nsCanvasRenderingContext2D::SetFont(const nsAString& font)
if (NS_FAILED(rv))
return rv;
css::Declaration *declaration = rule->GetDeclaration();
// The easiest way to see whether we got a syntax error or whether
// we got 'inherit' or 'initial' is to look at font-size-adjust,
// which the shorthand resets to either 'none' or
// '-moz-system-font'.
// We know the declaration is not !important, so we can use
// GetNormalBlock().
const nsCSSValue *fsaVal =
declaration->GetNormalBlock()->
ValueStorageFor(eCSSProperty_font_size_adjust);
if (!fsaVal || (fsaVal->GetUnit() != eCSSUnit_None &&
fsaVal->GetUnit() != eCSSUnit_System_Font)) {
// We got an all-property value or a syntax error. The spec says
// this value must be ignored.
return NS_OK;
}
rules.AppendObject(rule);
nsStyleSet* styleSet = presShell->StyleSet();
@ -2375,7 +2393,13 @@ nsCanvasRenderingContext2D::SetFont(const nsAString& font)
&style,
presShell->GetPresContext()->GetUserFontSet());
NS_ASSERTION(CurrentState().fontGroup, "Could not get font group");
CurrentState().font = font;
// The font getter is required to be reserialized based on what we
// parsed (including having line-height removed). (Older drafts of
// the spec required font sizes be converted to pixels, but that no
// longer seems to be required.)
declaration->GetValue(eCSSProperty_font, CurrentState().font);
return NS_OK;
}

View File

@ -18,11 +18,51 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=
var canvas = document.getElementById("display");
var cx = canvas.getContext("2d");
is(cx.font, "10px sans-serif", "initial font of canvas context");
cx.font = "italic 16px sans-serif";
is(cx.font, "italic 16px sans-serif", "valid font should round-trip");
cx.font = "bold 12px serif; background: green";
is(cx.font, "italic 16px sans-serif", "invalid font should be ignored");
cx.font = "bold 12px/3.0 serif";
is(cx.font, "bold 12px serif", "line-height should be dropped");
cx.font = "inherit";
is(cx.font, "bold 12px serif", "inherit should be ignored");
cx.font = "boold 18px sans-serif";
is(cx.font, "bold 12px serif", "syntax error should be ignored");
cx.font = "menu";
todo_is(cx.font, "menu", "system fonts should work");
function textmeas() {
return cx.measureText("hello").width;
}
cx.font = "66px serif";
var w_at_66 = textmeas();
cx.font = "20px serif";
var w_at_20 = textmeas();
ok(w_at_66 > w_at_20, "text should be wider at 66px than at 20px");
canvas.style.fontSize = "33px";
cx.font = "2em serif";
is(cx.font, "2em serif", "serialization of em");
is(textmeas(), w_at_66, "em should be relative to canvas font size");
canvas.style.fontSize = "16px";
is(cx.font, "2em serif", "serialization of em");
is(textmeas(), w_at_66,
"em should be relative to canvas font size at time of setting");
document.body.removeChild(canvas);
is(cx.font, "2em serif", "serialization of em");
is(textmeas(), w_at_66,
"em should be relative to canvas font size at time of setting");
canvas.style.fontSize = "33px";
cx.font = "2em serif";
is(cx.font, "2em serif", "serialization of em");
is(textmeas(), w_at_20,
"em should be relative to 10px when canvas not in document");
document.body.appendChild(canvas);
</script>
</pre>
</body>