From 073d4da6250ae51fdbecec26639db3be8b6043e1 Mon Sep 17 00:00:00 2001 From: Daniel Brooks Date: Thu, 18 Jun 2009 03:35:11 -0500 Subject: [PATCH] Bug 495002 - nsPresContext ought to have CSSPixelsToDevPixels and DevPixelsToCSSPixels methods r=bz, sr=roc --- dom/base/nsGlobalWindow.cpp | 22 +++++-------- layout/analysis/pixel-conversion.js | 6 ++++ layout/analysis/simple-match.js | 39 +++++++++++++++++++++++ layout/base/nsPresContext.h | 15 +++++++++ layout/xul/base/src/nsXULPopupManager.cpp | 4 +-- 5 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 layout/analysis/pixel-conversion.js create mode 100644 layout/analysis/simple-match.js diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index dbc0b643631..5346a6d0104 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -3115,9 +3115,8 @@ nsGlobalWindow::DevToCSSIntPixels(PRInt32 px) mDocShell->GetPresContext(getter_AddRefs(presContext)); if (!presContext) return px; - - return nsPresContext::AppUnitsToIntCSSPixels( - presContext->DevPixelsToAppUnits(px)); + + return presContext->DevPixelsToIntCSSPixels(px); } PRInt32 @@ -3130,9 +3129,8 @@ nsGlobalWindow::CSSToDevIntPixels(PRInt32 px) mDocShell->GetPresContext(getter_AddRefs(presContext)); if (!presContext) return px; - - return presContext->AppUnitsToDevPixels( - nsPresContext::CSSPixelsToAppUnits(px)); + + return presContext->CSSPixelsToDevPixels(px); } nsIntSize @@ -3147,10 +3145,8 @@ nsGlobalWindow::DevToCSSIntPixels(nsIntSize px) return px; return nsIntSize( - nsPresContext::AppUnitsToIntCSSPixels( - presContext->DevPixelsToAppUnits(px.width)), - nsPresContext::AppUnitsToIntCSSPixels( - presContext->DevPixelsToAppUnits(px.height))); + presContext->DevPixelsToIntCSSPixels(px.width), + presContext->DevPixelsToIntCSSPixels(px.height)); } nsIntSize @@ -3165,10 +3161,8 @@ nsGlobalWindow::CSSToDevIntPixels(nsIntSize px) return px; return nsIntSize( - presContext->AppUnitsToDevPixels( - nsPresContext::CSSPixelsToAppUnits(px.width)), - presContext->AppUnitsToDevPixels( - nsPresContext::CSSPixelsToAppUnits(px.height))); + presContext->CSSPixelsToDevPixels(px.width), + presContext->CSSPixelsToDevPixels(px.height)); } diff --git a/layout/analysis/pixel-conversion.js b/layout/analysis/pixel-conversion.js new file mode 100644 index 00000000000..c34d173b55d --- /dev/null +++ b/layout/analysis/pixel-conversion.js @@ -0,0 +1,6 @@ +include("simple-match.js"); + +var patterns = {CSSPixelsToDevPixels: ['AppUnitsToDevPixels', 'CSSPixelsToAppUnits'], + DevPixelsToIntCSSPixels: ['AppUnitsToIntCSSPixels', 'DevPixelsToAppUnits'], + DevPixelsToFloatCSSPixels: ['AppUnitsToFloatCSSPixels', 'DevPixelsToAppUnits'], + }; diff --git a/layout/analysis/simple-match.js b/layout/analysis/simple-match.js new file mode 100644 index 00000000000..263962a3b55 --- /dev/null +++ b/layout/analysis/simple-match.js @@ -0,0 +1,39 @@ +// This executes a very simple search for all functions that call a +// given set of functions. It's intended to be the simplest possible +// way of refactoring a common pattern of function calls. Of course, +// it's still up to a human to decide if the replacement is truely +// suitable, but this gets the low-hanging fruit. + +// Expects the variable 'patterns' to hold an object with replacement +// function names as keys and function lists as values. Any function +// in the tested source that calls all of the functions named in the +// list will be listed in the output as being likely candidates to +// instead call the replacement function. + +include("unstable/lazy_types.js"); + +var matches = {}; + +function identity(x) x; + +function process_cp_pre_genericize(fndecl) +{ + var c = []; + function calls(t, stack) + { + try { + t.tree_check(CALL_EXPR); + var fn = callable_arg_function_decl(CALL_EXPR_FN(t)); + if (fn) + c.push(decl_name_string(fn)); + } + catch (e if e.TreeCheckError) { } + } + + walk_tree(DECL_SAVED_TREE(fndecl), calls); + + for (let [fnreplace, pattern] in patterns) + if (pattern.map(function(e){ return c.some(function(f) { return e == f; }); }).every(identity)) + if (fnreplace != (n = decl_name_string(fndecl))) + print(fnreplace +" could probably be used in "+ n); +} diff --git a/layout/base/nsPresContext.h b/layout/base/nsPresContext.h index 3a7b7ac1375..c5862beae38 100644 --- a/layout/base/nsPresContext.h +++ b/layout/base/nsPresContext.h @@ -568,6 +568,21 @@ public: { return NSAppUnitsToIntPixels(aAppUnits, float(mDeviceContext->AppUnitsPerDevPixel())); } + PRInt32 CSSPixelsToDevPixels(PRInt32 aPixels) + { return AppUnitsToDevPixels(CSSPixelsToAppUnits(aPixels)); } + + float CSSPixelsToDevPixels(float aPixels) + { + return NSAppUnitsToFloatPixels(CSSPixelsToAppUnits(aPixels), + float(mDeviceContext->AppUnitsPerDevPixel())); + } + + PRInt32 DevPixelsToIntCSSPixels(PRInt32 aPixels) + { return AppUnitsToIntCSSPixels(DevPixelsToAppUnits(aPixels)); } + + float DevPixelsToFloatCSSPixels(PRInt32 aPixels) + { return AppUnitsToFloatCSSPixels(DevPixelsToAppUnits(aPixels)); } + // If there is a remainder, it is rounded to nearest app units. nscoord GfxUnitsToAppUnits(gfxFloat aGfxUnits) const { return mDeviceContext->GfxUnitsToAppUnits(aGfxUnits); } diff --git a/layout/xul/base/src/nsXULPopupManager.cpp b/layout/xul/base/src/nsXULPopupManager.cpp index c7e66df11fa..77abf05bc7f 100644 --- a/layout/xul/base/src/nsXULPopupManager.cpp +++ b/layout/xul/base/src/nsXULPopupManager.cpp @@ -361,8 +361,8 @@ nsXULPopupManager::SetTriggerEvent(nsIDOMEvent* aEvent, nsIContent* aPopup) mouseEvent->GetClientY(&mCachedMousePoint.y); // convert to device pixels - mCachedMousePoint.x = presContext->AppUnitsToDevPixels(nsPresContext::CSSPixelsToAppUnits(mCachedMousePoint.x)); - mCachedMousePoint.y = presContext->AppUnitsToDevPixels(nsPresContext::CSSPixelsToAppUnits(mCachedMousePoint.y)); + mCachedMousePoint.x = presContext->CSSPixelsToDevPixels(mCachedMousePoint.x); + mCachedMousePoint.y = presContext->CSSPixelsToDevPixels(mCachedMousePoint.y); } else if (rootFrame) { nsPoint pnt =