From d099b3735de7315f258ff3c19bdaa5d8292d43a8 Mon Sep 17 00:00:00 2001 From: "pavlov%pavlov.net" Date: Sat, 4 Mar 2006 04:40:07 +0000 Subject: [PATCH] making windows font sizes scale based on the CTM. bug 329306. r=vlad --- gfx/thebes/public/gfxContext.h | 2 +- gfx/thebes/public/gfxMatrix.h | 11 +++- gfx/thebes/public/gfxWindowsFonts.h | 12 +++-- gfx/thebes/src/gfxContext.cpp | 6 +-- gfx/thebes/src/gfxWindowsFonts.cpp | 80 +++++++++++++++++------------ 5 files changed, 70 insertions(+), 41 deletions(-) diff --git a/gfx/thebes/public/gfxContext.h b/gfx/thebes/public/gfxContext.h index cc96ec173190..0326462a9cad 100644 --- a/gfx/thebes/public/gfxContext.h +++ b/gfx/thebes/public/gfxContext.h @@ -241,7 +241,7 @@ public: /** * Returns the current transformation matrix. */ - gfxMatrix CurrentMatrix() const; + const gfxMatrix& CurrentMatrix() const; /** * Converts a point from device to user coordinates using the inverse diff --git a/gfx/thebes/public/gfxMatrix.h b/gfx/thebes/public/gfxMatrix.h index e9d54ce88599..f130a64e9b41 100644 --- a/gfx/thebes/public/gfxMatrix.h +++ b/gfx/thebes/public/gfxMatrix.h @@ -87,6 +87,15 @@ public: mat = m; } + bool operator==(const gfxMatrix& m) const { + return ((mat.xx == m.mat.xx) && + (mat.yx == m.mat.yx) && + (mat.xy == m.mat.xy) && + (mat.yy == m.mat.yy) && + (mat.x0 == m.mat.x0) && + (mat.y0 == m.mat.y0)); + } + gfxMatrix& operator=(const cairo_matrix_t& m) { mat = m; return *this; @@ -107,7 +116,7 @@ public: } // conversion to other types - cairo_matrix_t ToCairoMatrix() const { + const cairo_matrix_t& ToCairoMatrix() const { return mat; } diff --git a/gfx/thebes/public/gfxWindowsFonts.h b/gfx/thebes/public/gfxWindowsFonts.h index 353c472d8894..91dfb9df3b7c 100644 --- a/gfx/thebes/public/gfxWindowsFonts.h +++ b/gfx/thebes/public/gfxWindowsFonts.h @@ -57,8 +57,7 @@ class gfxWindowsFont : public gfxFont { public: gfxWindowsFont(const nsAString &aName, const gfxFontGroup *aFontGroup, HDC aHWnd); - gfxWindowsFont(HFONT aFont, const gfxFontGroup *aFontGroup, PRBool aIsMLangFont); - + gfxWindowsFont(HFONT aFont, const gfxFontGroup *aFontGroup, PRBool aIsMLangFont, const gfxMatrix& aMatrix); virtual ~gfxWindowsFont(); virtual const gfxFont::Metrics& GetMetrics(); @@ -67,14 +66,17 @@ public: cairo_scaled_font_t *CairoScaledFont() { return mScaledFont; } SCRIPT_CACHE *ScriptCache() { return &mScriptCache; } HFONT GetHFONT() { return mFont; } - void UpdateFonts(cairo_t *cr); + const gfxMatrix& CurrentMatrix() const { return mCTM; } + void UpdateCTM(const gfxMatrix& aMatrix); protected: cairo_font_face_t *MakeCairoFontFace(); - cairo_scaled_font_t *MakeCairoScaledFont(cairo_t *cr); + cairo_scaled_font_t *MakeCairoScaledFont(); void FillLogFont(PRInt16 weight); private: + void Init(); + void Destroy(); void ComputeMetrics(); HFONT mFont; @@ -88,6 +90,8 @@ private: LOGFONTW mLogFont; PRPackedBool mIsMLangFont; + + gfxMatrix mCTM; }; diff --git a/gfx/thebes/src/gfxContext.cpp b/gfx/thebes/src/gfxContext.cpp index d6f6da59d320..d556024d7688 100644 --- a/gfx/thebes/src/gfxContext.cpp +++ b/gfx/thebes/src/gfxContext.cpp @@ -234,13 +234,13 @@ void gfxContext::Rotate(gfxFloat angle) } void gfxContext::Multiply(const gfxMatrix& matrix) { - cairo_matrix_t mat = matrix.ToCairoMatrix(); + const cairo_matrix_t& mat = matrix.ToCairoMatrix(); cairo_transform(mCairo, &mat); } void gfxContext::SetMatrix(const gfxMatrix& matrix) { - cairo_matrix_t mat = matrix.ToCairoMatrix(); + const cairo_matrix_t& mat = matrix.ToCairoMatrix(); cairo_set_matrix(mCairo, &mat); } @@ -249,7 +249,7 @@ void gfxContext::IdentityMatrix() cairo_identity_matrix(mCairo); } -gfxMatrix gfxContext::CurrentMatrix() const +const gfxMatrix& gfxContext::CurrentMatrix() const { cairo_matrix_t mat; cairo_get_matrix(mCairo, &mat); diff --git a/gfx/thebes/src/gfxWindowsFonts.cpp b/gfx/thebes/src/gfxWindowsFonts.cpp index ae604277653e..d886d9c7d5d3 100644 --- a/gfx/thebes/src/gfxWindowsFonts.cpp +++ b/gfx/thebes/src/gfxWindowsFonts.cpp @@ -64,28 +64,39 @@ gfxWindowsFont::gfxWindowsFont(const nsAString &aName, const gfxFontGroup *aFont mGroup = aFontGroup; mStyle = mGroup->GetStyle(); - mFontFace = MakeCairoFontFace(); - NS_ASSERTION(mFontFace, "Failed to make font face"); - - mScaledFont = MakeCairoScaledFont(nsnull); - NS_ASSERTION(mScaledFont, "Failed to make scaled font"); + Init(); } -gfxWindowsFont::gfxWindowsFont(HFONT aFont, const gfxFontGroup *aFontGroup, PRBool aIsMLangFont) - : mScriptCache(nsnull), mMetrics(nsnull), mIsMLangFont(aIsMLangFont) +gfxWindowsFont::gfxWindowsFont(HFONT aFont, + const gfxFontGroup *aFontGroup, + PRBool aIsMLangFont, + const gfxMatrix& aMatrix) + : mScriptCache(nsnull), mMetrics(nsnull), mIsMLangFont(aIsMLangFont), mCTM(aMatrix) { mFont = aFont; mGroup = aFontGroup; mStyle = mGroup->GetStyle(); - mFontFace = MakeCairoFontFace(); - NS_ASSERTION(mFontFace, "Failed to make font face"); - - mScaledFont = MakeCairoScaledFont(nsnull); - NS_ASSERTION(mScaledFont, "Failed to make scaled font"); + Init(); } gfxWindowsFont::~gfxWindowsFont() +{ + Destroy(); +} + +void +gfxWindowsFont::Init() +{ + mFontFace = MakeCairoFontFace(); + NS_ASSERTION(mFontFace, "Failed to make font face"); + + mScaledFont = MakeCairoScaledFont(); + NS_ASSERTION(mScaledFont, "Failed to make scaled font"); +} + +void +gfxWindowsFont::Destroy() { cairo_font_face_destroy(mFontFace); cairo_scaled_font_destroy(mScaledFont); @@ -107,6 +118,12 @@ gfxWindowsFont::~gfxWindowsFont() ScriptFreeCache(&mScriptCache); delete mMetrics; + + mFont = nsnull; + mScriptCache = nsnull; + mFontFace = nsnull; + mScaledFont = nsnull; + mMetrics = nsnull; } const gfxFont::Metrics& @@ -119,15 +136,16 @@ gfxWindowsFont::GetMetrics() } void -gfxWindowsFont::UpdateFonts(cairo_t *cr) +gfxWindowsFont::UpdateCTM(const gfxMatrix& aMatrix) { - cairo_font_face_destroy(mFontFace); - cairo_scaled_font_destroy(mScaledFont); - - mFontFace = MakeCairoFontFace(); - NS_ASSERTION(mFontFace, "Failed to make font face"); - mScaledFont = MakeCairoScaledFont(nsnull); - NS_ASSERTION(mScaledFont, "Failed to make scaled font"); + if (aMatrix.ToCairoMatrix().yy == mCTM.ToCairoMatrix().yy) + return; + + Destroy(); + + mCTM = aMatrix; + + Init(); } cairo_font_face_t * @@ -174,21 +192,16 @@ gfxWindowsFont::MakeCairoFontFace() } cairo_scaled_font_t * -gfxWindowsFont::MakeCairoScaledFont(cairo_t *cr) +gfxWindowsFont::MakeCairoScaledFont() { cairo_scaled_font_t *font = nsnull; - cairo_matrix_t sizeMatrix, ctm; - - if (cr) - cairo_get_matrix(cr, &ctm); - else - cairo_matrix_init_identity(&ctm); + cairo_matrix_t sizeMatrix; cairo_matrix_init_scale(&sizeMatrix, mStyle->size, mStyle->size); cairo_font_options_t *fontOptions = cairo_font_options_create(); - font = cairo_scaled_font_create(mFontFace, &sizeMatrix, &ctm, fontOptions); + font = cairo_scaled_font_create(mFontFace, &sizeMatrix, &mCTM.ToCairoMatrix(), fontOptions); cairo_font_options_destroy(fontOptions); return font; @@ -279,8 +292,10 @@ void gfxWindowsFont::FillLogFont(PRInt16 currentWeight) { #define CLIP_TURNOFF_FONTASSOCIATION 0x40 + + const double yScale = mCTM.ToCairoMatrix().yy; - mLogFont.lfHeight = -NSToCoordRound(mStyle->size * 32); + mLogFont.lfHeight = -NSToCoordRound(mStyle->size * yScale * 32); if (mLogFont.lfHeight == 0) mLogFont.lfHeight = -1; @@ -439,7 +454,7 @@ gfxWindowsTextRun::FindFallbackFont(HDC aDC, const PRUnichar *aString, PRUint32 HFONT retFont; rv = langFontLink->MapFont(aDC, actualCodePages, aString[0], &retFont); if (SUCCEEDED(rv)) - return new gfxWindowsFont(retFont, mGroup, PR_TRUE); + return new gfxWindowsFont(retFont, mGroup, PR_TRUE, aFont->CurrentMatrix()); } return nsnull; @@ -486,6 +501,7 @@ gfxWindowsTextRun::MeasureOrDrawFast(gfxContext *aContext, cairo_t *cr = aContext->GetCairo(); nsRefPtr currentFont = mGroup->GetFontAt(0); + currentFont->UpdateCTM(aContext->CurrentMatrix()); cairo_font_face_t *fontFace = currentFont->CairoFontFace(); cairo_scaled_font_t *scaledFont = currentFont->CairoScaledFont(); @@ -663,7 +679,7 @@ TRY_AGAIN_HOPE_FOR_THE_BEST: SaveDC(aDC); - //currentFont->UpdateFonts(cr); + currentFont->UpdateCTM(aContext->CurrentMatrix()); fontFace = currentFont->CairoFontFace(); scaledFont = currentFont->CairoScaledFont(); @@ -851,7 +867,7 @@ TRY_AGAIN_JUST_PLACE: /* Draw using ScriptTextOut() */ SaveDC(aDC); - gfxMatrix m = aContext->CurrentMatrix(); + const gfxMatrix& m = aContext->CurrentMatrix(); XFORM xform; double dm[6]; m.ToValues(&dm[0], &dm[1], &dm[2], &dm[3], &dm[4], &dm[5]);