Make computed style handle eStyleUnit_Chars. Bug 371043, r+sr=dbaron

This commit is contained in:
bzbarsky@mit.edu 2007-05-04 23:09:50 -07:00
parent 5a7814a82a
commit 64c3e5df8a
13 changed files with 98 additions and 37 deletions

View File

@ -1243,11 +1243,8 @@ static PRBool GetAbsoluteCoord(const nsStyleCoord& aStyle,
return PR_TRUE;
}
if (eStyleUnit_Chars == unit) {
SetFontFromStyle(aRenderingContext, aFrame->GetStyleContext());
nscoord fontWidth;
aRenderingContext->SetTextRunRTL(PR_FALSE);
aRenderingContext->GetWidth('M', fontWidth);
aResult = aStyle.GetIntValue() * fontWidth;
aResult = nsLayoutUtils::CharsToCoord(aStyle, aRenderingContext,
aFrame->GetStyleContext());
return PR_TRUE;
}
return PR_FALSE;
@ -2154,3 +2151,27 @@ nsLayoutUtils::DrawImage(nsIRenderingContext* aRenderingContext,
*/
#endif
}
void
nsLayoutUtils::SetFontFromStyle(nsIRenderingContext* aRC, nsStyleContext* aSC)
{
const nsStyleFont* font = aSC->GetStyleFont();
const nsStyleVisibility* visibility = aSC->GetStyleVisibility();
aRC->SetFont(font->mFont, visibility->mLangGroup);
}
nscoord
nsLayoutUtils::CharsToCoord(const nsStyleCoord& aStyle,
nsIRenderingContext* aRenderingContext,
nsStyleContext* aStyleContext)
{
NS_ASSERTION(aStyle.GetUnit() == eStyleUnit_Chars,
"Shouldn't have called this");
SetFontFromStyle(aRenderingContext, aStyleContext);
nscoord fontWidth;
aRenderingContext->SetTextRunRTL(PR_FALSE);
aRenderingContext->GetWidth('M', fontWidth);
return aStyle.GetIntValue() * fontWidth;
}

View File

@ -647,6 +647,22 @@ public:
const nsRect& aDestRect,
const nsRect& aDirtyRect,
const nsRect* aSourceRect = nsnull);
/**
* Set the font on aRC based on the style in aSC
*/
static void SetFontFromStyle(nsIRenderingContext* aRC, nsStyleContext* aSC);
/**
* Convert an eStyleUnit_Chars nsStyleCoord to an nscoord.
*
* @param aStyle the style coord
* @param aRenderingContext the rendering context to use for font measurement
* @param aStyleContext the style context to use for font infomation
*/
static nscoord CharsToCoord(const nsStyleCoord& aStyle,
nsIRenderingContext* aRenderingContext,
nsStyleContext* aStyleContext);
};
#endif // nsLayoutUtils_h__

View File

@ -46,7 +46,7 @@
#include "nsGkAtoms.h"
#include "nsIFontMetrics.h"
#include "nsIRenderingContext.h"
#include "nsTextTransformer.h"
#include "nsLayoutUtils.h"
#ifdef ACCESSIBILITY
#include "nsIServiceManager.h"
@ -144,7 +144,7 @@ BRFrame::Reflow(nsPresContext* aPresContext,
// We also do this in strict mode because BR should act like a
// normal inline frame. That line-height is used is important
// here for cases where the line-height is less that 1.
SetFontFromStyle(aReflowState.rendContext, mStyleContext);
nsLayoutUtils::SetFontFromStyle(aReflowState.rendContext, mStyleContext);
nsCOMPtr<nsIFontMetrics> fm;
aReflowState.rendContext->GetFontMetrics(*getter_AddRefs(fm));
if (fm) {

View File

@ -437,15 +437,6 @@ nsresult NS_NewSelectionImageService(nsISelectionImageService** aResult)
//end selection service
// a handy utility to set font
void SetFontFromStyle(nsIRenderingContext* aRC, nsStyleContext* aSC)
{
const nsStyleFont* font = aSC->GetStyleFont();
const nsStyleVisibility* visibility = aSC->GetStyleVisibility();
aRC->SetFont(font->mFont, visibility->mLangGroup);
}
void
nsWeakFrame::Init(nsIFrame* aFrame)
{
@ -3059,11 +3050,8 @@ AddCoord(const nsStyleCoord& aStyle,
*aPercent += aStyle.GetPercentValue();
break;
case eStyleUnit_Chars: {
SetFontFromStyle(aRenderingContext, aFrame->GetStyleContext());
nscoord fontWidth;
aRenderingContext->SetTextRunRTL(PR_FALSE);
aRenderingContext->GetWidth('M', fontWidth);
*aCoord += aStyle.GetIntValue() * fontWidth;
*aCoord += nsLayoutUtils::CharsToCoord(aStyle, aRenderingContext,
aFrame->GetStyleContext());
break;
}
default:

View File

@ -113,10 +113,6 @@
#define NS_FRAME_TRACE_REFLOW_OUT(_method, _status)
#endif
// handy utilities
// XXXldb Move to nsLayoutUtils!
void SetFontFromStyle(nsIRenderingContext* aRC, nsStyleContext* aSC);
//----------------------------------------------------------------------
struct nsBoxLayoutMetrics;

View File

@ -941,7 +941,7 @@ nsImageFrame::DisplayAltText(nsPresContext* aPresContext,
{
// Set font and color
aRenderingContext.SetColor(GetStyleColor()->mColor);
SetFontFromStyle(&aRenderingContext, mStyleContext);
nsLayoutUtils::SetFontFromStyle(&aRenderingContext, mStyleContext);
// Format the text to display within the formatting rect
nsIFontMetrics* fm;

View File

@ -568,7 +568,7 @@ nsInlineFrame::ReflowFrames(nsPresContext* aPresContext,
: aReflowState.mComputedBorderPadding.left;
}
SetFontFromStyle(aReflowState.rendContext, mStyleContext);
nsLayoutUtils::SetFontFromStyle(aReflowState.rendContext, mStyleContext);
nsCOMPtr<nsIFontMetrics> fm;
aReflowState.rendContext->GetFontMetrics(*getter_AddRefs(fm));

View File

@ -1615,7 +1615,7 @@ nsLineLayout::VerticalAlignFrames(PerSpanData* psd)
// Get the parent frame's font for all of the frames in this span
nsStyleContext* styleContext = spanFrame->GetStyleContext();
nsIRenderingContext* rc = mBlockReflowState->rendContext;
SetFontFromStyle(mBlockReflowState->rendContext, styleContext);
nsLayoutUtils::SetFontFromStyle(mBlockReflowState->rendContext, styleContext);
nsCOMPtr<nsIFontMetrics> fm;
rc->GetFontMetrics(*getter_AddRefs(fm));

View File

@ -893,7 +893,8 @@ nsTextStyle::nsTextStyle(nsPresContext* aPresContext,
PRUint8 originalDecorations = plainFont->decorations;
plainFont->decorations = NS_FONT_DECORATION_NONE;
mAveCharWidth = 0;
SetFontFromStyle(&aRenderingContext, sc); // some users of the struct expect this state
// Set the font: some users of the struct expect this state
nsLayoutUtils::SetFontFromStyle(&aRenderingContext, sc);
aRenderingContext.GetFontMetrics(mNormalFont);
mNormalFont->GetSpaceWidth(mSpaceWidth);
mNormalFont->GetAveCharWidth(mAveCharWidth);
@ -2140,7 +2141,7 @@ nsTextFrame::FillClusterBuffer(nsPresContext *aPresContext, const PRUnichar *aTe
NS_ENSURE_SUCCESS(rv, rv);
// Find the font metrics for this text
SetFontFromStyle(acx, mStyleContext);
nsLayoutUtils::SetFontFromStyle(acx, mStyleContext);
acx->GetHints(clusterHint);
clusterHint &= NS_RENDERING_HINT_TEXT_CLUSTERS;
@ -4131,7 +4132,7 @@ nsTextFrame::GetPositionHelper(const nsPoint& aPoint,
}
// Find the font metrics for this text
SetFontFromStyle(rendContext, mStyleContext);
nsLayoutUtils::SetFontFromStyle(rendContext, mStyleContext);
// Get the renderable form of the text
nsTextTransformer tx(PresContext());
@ -6173,7 +6174,7 @@ nsTextFrame::Reflow(nsPresContext* aPresContext,
#ifdef MOZ_MATHML
if (calcMathMLMetrics) {
SetFontFromStyle(aReflowState.rendContext, mStyleContext);
nsLayoutUtils::SetFontFromStyle(aReflowState.rendContext, mStyleContext);
nsBoundingMetrics bm;
rv = aReflowState.rendContext->GetBoundingMetrics(textBuffer.mBuffer, textLength, bm);
if (NS_SUCCEEDED(rv))
@ -6277,7 +6278,7 @@ nsTextFrame::TrimTrailingWhiteSpace(nsPresContext* aPresContext,
if (XP_IS_SPACE(ch)) {
// Get font metrics for a space so we can adjust the width by the
// right amount.
SetFontFromStyle(&aRC, mStyleContext);
nsLayoutUtils::SetFontFromStyle(&aRC, mStyleContext);
aRC.GetWidth(' ', dw);
// NOTE: Trailing whitespace includes word and letter spacing!

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<body>
<div id="source" style="font-size: 10px; -moz-column-gap: 2ch">
<span id="test"
style="background: green; color: green; display: inline-block;
height: 30px; padding: 0 2px">MM
</span>
</div>
</body>

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<body>
<div id="source" style="font-size: 10px; -moz-column-gap: 2ch">
<span id="test"
style="background: green; display: inline-block; height: 30px;
padding: 0 2px">
</span>
<script>
document.getElementById("test").style.width =
document.defaultView.getComputedStyle(document.getElementById("source"),
"").MozColumnGap;
</script>
</div>
</body>

View File

@ -210,6 +210,7 @@ fails == 368504-1.html 368504-1-ref.html # bug 368504
== 370629-2.html 370629-2-ref.html
== 370692-1.xhtml 370692-1-ref.xhtml
== 371041-1.html 371041-1-ref.html
== 371043-1.html 371043-1-ref.html
== 371925-1a.html 371925-1-ref.html
== 371925-1b.html 371925-1-ref.html
== 372553-1.html 372553-1-ref.html

View File

@ -68,6 +68,8 @@
#include "nsStyleSet.h"
#include "imgIRequest.h"
#include "nsInspectorCSSUtils.h"
#include "nsLayoutUtils.h"
#include "nsFrameManager.h"
#if defined(DEBUG_bzbarsky) || defined(DEBUG_caillon)
#define DEBUG_ComputedDOMStyle
@ -2747,11 +2749,24 @@ nsComputedDOMStyle::SetValueToCoord(nsROCSSPrimitiveValue* aValue,
aTable));
break;
case eStyleUnit_Chars:
// XXX we need a frame and a rendering context to calculate this, bug 281972, bug 282126.
aValue->SetAppUnits(0);
case eStyleUnit_Chars: {
// Get a rendering context
nsCOMPtr<nsIRenderingContext> cx;
nsIFrame* frame = mPresShell->FrameManager()->GetRootFrame();
if (frame) {
mPresShell->CreateRenderingContext(frame, getter_AddRefs(cx));
}
if (cx) {
nscoord val =
nsLayoutUtils::CharsToCoord(aCoord, cx, mStyleContextHolder);
aValue->SetAppUnits(PR_MAX(aMinAppUnits, val));
} else {
// Oh, well. Give up.
aValue->SetAppUnits(0);
}
break;
}
case eStyleUnit_Null:
aValue->SetIdent(nsGkAtoms::none);
break;