Bug 537890. Part 4: Make points be a fixed number of CSS pixels. r=dbaron

This commit is contained in:
Robert O'Callahan 2010-08-13 21:58:01 +12:00
parent 21d0bce532
commit 973ebcc0ad
6 changed files with 87 additions and 44 deletions

View File

@ -454,17 +454,16 @@ inline float NSCoordScale(nscoord aCoord, PRInt32 aFromAPP, PRInt32 aToAPP)
/*
* Twips/unit conversions
*/
inline nscoord NSUnitsToTwips(float aValue, float aPointsPerUnit)
inline float NSUnitsToTwips(float aValue, float aPointsPerUnit)
{
return NSToCoordRoundWithClamp(aValue * aPointsPerUnit * TWIPS_PER_POINT_FLOAT);
return aValue * aPointsPerUnit * TWIPS_PER_POINT_FLOAT;
}
inline float NSTwipsToUnits(nscoord aTwips, float aUnitsPerPoint)
inline float NSTwipsToUnits(float aTwips, float aUnitsPerPoint)
{
return (aTwips * (aUnitsPerPoint / TWIPS_PER_POINT_FLOAT));
}
/// Unit conversion macros
//@{
#define NS_POINTS_TO_TWIPS(x) NSUnitsToTwips((x), 1.0f)

View File

@ -619,9 +619,9 @@ public:
AppUnitsToGfxUnits(aAppRect.width),
AppUnitsToGfxUnits(aAppRect.height)); }
nscoord TwipsToAppUnits(PRInt32 aTwips) const
{ return NSCoordSaturatingMultiply(mDeviceContext->AppUnitsPerInch(),
NS_TWIPS_TO_INCHES(aTwips)); }
nscoord TwipsToAppUnits(float aTwips) const
{ return NSToCoordRoundWithClamp(
mDeviceContext->AppUnitsPerInch() * NS_TWIPS_TO_INCHES(aTwips)); }
// Margin-specific version, since they often need TwipsToAppUnits
nsMargin TwipsToAppUnits(const nsIntMargin &marginInTwips) const

View File

@ -348,15 +348,15 @@ nsMathMLFrame::CalcLength(nsPresContext* aPresContext,
NS_ASSERTION(aCSSValue.IsLengthUnit(), "not a length unit");
if (aCSSValue.IsFixedLengthUnit()) {
return aPresContext->TwipsToAppUnits(aCSSValue.GetLengthTwips());
return aCSSValue.GetFixedLength(aPresContext);
}
if (aCSSValue.IsPixelLengthUnit()) {
return aCSSValue.GetPixelLength();
}
nsCSSUnit unit = aCSSValue.GetUnit();
if (eCSSUnit_Pixel == unit) {
return nsPresContext::CSSPixelsToAppUnits(aCSSValue.GetFloatValue());
}
else if (eCSSUnit_EM == unit) {
if (eCSSUnit_EM == unit) {
const nsStyleFont* font = aStyleContext->GetStyleFont();
return NSToCoordRound(aCSSValue.GetFloatValue() * (float)font->mFont.size);
}
@ -368,6 +368,8 @@ nsMathMLFrame::CalcLength(nsPresContext* aPresContext,
return NSToCoordRound(aCSSValue.GetFloatValue() * (float)xHeight);
}
// MathML doesn't specify other CSS units such as rem or ch
NS_ERROR("Unsupported unit");
return 0;
}

View File

@ -232,30 +232,51 @@ imgIRequest* nsCSSValue::GetImageValue() const
return mValue.mImage->mRequest;
}
nscoord nsCSSValue::GetLengthTwips() const
nscoord nsCSSValue::GetFixedLength(nsPresContext* aPresContext) const
{
NS_ASSERTION(IsFixedLengthUnit(), "not a fixed length unit");
if (IsFixedLengthUnit()) {
switch (mUnit) {
case eCSSUnit_Inch:
return NS_INCHES_TO_TWIPS(mValue.mFloat);
float twips;
switch (mUnit) {
case eCSSUnit_Inch:
twips = NS_INCHES_TO_TWIPS(mValue.mFloat);
break;
case eCSSUnit_Millimeter:
return NS_MILLIMETERS_TO_TWIPS(mValue.mFloat);
case eCSSUnit_Centimeter:
return NS_CENTIMETERS_TO_TWIPS(mValue.mFloat);
case eCSSUnit_Millimeter:
twips = NS_MILLIMETERS_TO_TWIPS(mValue.mFloat);
break;
case eCSSUnit_Point:
return NS_POINTS_TO_TWIPS(mValue.mFloat);
case eCSSUnit_Pica:
return NS_PICAS_TO_TWIPS(mValue.mFloat);
default:
NS_ERROR("should never get here");
break;
}
case eCSSUnit_Centimeter:
twips = NS_CENTIMETERS_TO_TWIPS(mValue.mFloat);
break;
case eCSSUnit_Pica:
twips = NS_PICAS_TO_TWIPS(mValue.mFloat);
break;
default:
NS_ERROR("should never get here");
return 0;
}
return aPresContext->TwipsToAppUnits(twips);
}
nscoord nsCSSValue::GetPixelLength() const
{
NS_ASSERTION(IsPixelLengthUnit(), "not a fixed length unit");
switch (mUnit) {
case eCSSUnit_Pixel:
return nsPresContext::CSSPixelsToAppUnits(mValue.mFloat);
case eCSSUnit_Point:
return nsPresContext::CSSPixelsToAppUnits(mValue.mFloat*4/3);
default:
NS_ERROR("should never get here");
return 0;
}
return 0;
}
void nsCSSValue::DoReset()

View File

@ -56,6 +56,7 @@
class imgIRequest;
class nsIDocument;
class nsIPrincipal;
class nsPresContext;
// Deletes a linked list iteratively to avoid blowing up the stack (bug 456196).
#define NS_CSS_DELETE_LIST_MEMBER(type_, ptr_, member_) \
@ -155,7 +156,6 @@ enum nsCSSUnit {
eCSSUnit_Centimeter = 208, // (float) 1/100 meter
// US Typographic
eCSSUnit_Point = 300, // (float) 1/72 inch
eCSSUnit_Pica = 301, // (float) 12 points == 1/6 inch
// Length units - relative
@ -166,7 +166,8 @@ enum nsCSSUnit {
eCSSUnit_RootEM = 803, // (float) == root element font size
// Screen relative measure
eCSSUnit_Pixel = 900, // (float) CSS pixel unit
eCSSUnit_Point = 900, // (float) 4/3 of a CSS pixel
eCSSUnit_Pixel = 901, // (float) CSS pixel unit
// Angular units
eCSSUnit_Degree = 1000, // (float) 360 per circle
@ -229,10 +230,29 @@ public:
nsCSSUnit GetUnit() const { return mUnit; }
PRBool IsLengthUnit() const
{ return eCSSUnit_Inch <= mUnit && mUnit <= eCSSUnit_Pixel; }
/**
* A "fixed" length unit is one that means a specific physical length
* which we try to match based on the physical characteristics of an
* output device.
*/
PRBool IsFixedLengthUnit() const
{ return eCSSUnit_Inch <= mUnit && mUnit <= eCSSUnit_Pica; }
/**
* What the spec calls relative length units is, for us, split
* between relative length units and pixel length units.
*
* A "relative" length unit is a multiple of some derived metric,
* such as a font em-size, which itself was controlled by an input CSS
* length. Relative length units should not be scaled by zooming, since
* the underlying CSS length would already have been scaled.
*/
PRBool IsRelativeLengthUnit() const
{ return eCSSUnit_EM <= mUnit && mUnit <= eCSSUnit_Pixel; }
{ return eCSSUnit_EM <= mUnit && mUnit <= eCSSUnit_RootEM; }
/**
* A "pixel" length unit is a some multiple of CSS pixels.
*/
PRBool IsPixelLengthUnit() const
{ return eCSSUnit_Point <= mUnit && mUnit <= eCSSUnit_Pixel; }
PRBool IsAngularUnit() const
{ return eCSSUnit_Degree <= mUnit && mUnit <= eCSSUnit_Radian; }
PRBool IsFrequencyUnit() const
@ -342,7 +362,8 @@ public:
// all over.
imgIRequest* GetImageValue() const;
nscoord GetLengthTwips() const;
nscoord GetFixedLength(nsPresContext* aPresContext) const;
nscoord GetPixelLength() const;
void Reset() // sets to null
{

View File

@ -238,13 +238,13 @@ static nscoord CalcLengthWith(const nsCSSValue& aValue,
NS_ASSERTION(aPresContext, "Must have prescontext");
if (aValue.IsFixedLengthUnit()) {
return aPresContext->TwipsToAppUnits(aValue.GetLengthTwips());
return aValue.GetFixedLength(aPresContext);
}
nsCSSUnit unit = aValue.GetUnit();
if (unit == eCSSUnit_Pixel) {
return nsPresContext::CSSPixelsToAppUnits(aValue.GetFloatValue());
if (aValue.IsPixelLengthUnit()) {
return aValue.GetPixelLength();
}
// Common code for all units other than pixels:
// Common code for all units other than pixel-based units and fixed-length
// units:
aCanStoreInRuleTree = PR_FALSE;
const nsStyleFont *styleFont =
aStyleFont ? aStyleFont : aStyleContext->GetStyleFont();
@ -253,7 +253,7 @@ static nscoord CalcLengthWith(const nsCSSValue& aValue,
// prefs into account?
aFontSize = styleFont->mFont.size;
}
switch (unit) {
switch (aValue.GetUnit()) {
case eCSSUnit_RootEM: {
nscoord rootFontSize;
@ -1414,7 +1414,7 @@ CheckFontCallback(const nsRuleDataStruct& aData,
const nsCSSValue& size = fontData.mSize;
const nsCSSValue& weight = fontData.mWeight;
const nsCSSValue& stretch = fontData.mStretch;
if ((size.IsRelativeLengthUnit() && size.GetUnit() != eCSSUnit_Pixel) ||
if (size.IsRelativeLengthUnit() ||
size.GetUnit() == eCSSUnit_Percent ||
(size.GetUnit() == eCSSUnit_Enumerated &&
(size.GetIntValue() == NS_STYLE_FONT_SIZE_SMALLER ||
@ -2884,7 +2884,7 @@ struct SetFontSizeCalcOps : public css::BasicCoordCalcOps,
size = CalcLengthWith(aValue, mParentSize, mParentFont,
nsnull, mPresContext, mAtRoot,
PR_TRUE, mCanStoreInRuleTree);
if (aValue.IsFixedLengthUnit() || aValue.GetUnit() == eCSSUnit_Pixel) {
if (!aValue.IsRelativeLengthUnit()) {
size = nsStyleFont::ZoomText(mPresContext, size);
}
}
@ -3650,8 +3650,8 @@ nsRuleNode::ComputeTextData(void* aStartStruct,
SetCoord(textData.mLineHeight, text->mLineHeight, parentText->mLineHeight,
SETCOORD_LEH | SETCOORD_FACTOR | SETCOORD_NORMAL,
aContext, mPresContext, canStoreInRuleTree);
if (textData.mLineHeight.IsFixedLengthUnit() ||
textData.mLineHeight.GetUnit() == eCSSUnit_Pixel) {
if (textData.mLineHeight.IsLengthUnit() &&
!textData.mLineHeight.IsRelativeLengthUnit()) {
nscoord lh = nsStyleFont::ZoomText(mPresContext,
text->mLineHeight.GetCoordValue());
nscoord minimumFontSize =