DisplaySVGItem.cpp (2234B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // Main header first: 8 #include "DisplaySVGItem.h" 9 10 #include "mozilla/ISVGDisplayableFrame.h" 11 #include "mozilla/SVGUtils.h" 12 #include "nsLayoutUtils.h" 13 #include "nsPoint.h" 14 15 using namespace mozilla::gfx; 16 using namespace mozilla::image; 17 18 namespace mozilla { 19 20 void DisplaySVGItem::HitTest(nsDisplayListBuilder* aBuilder, 21 const nsRect& aRect, HitTestState* aState, 22 nsTArray<nsIFrame*>* aOutFrames) { 23 ISVGDisplayableFrame* svgFrame = do_QueryFrame(mFrame); 24 MOZ_ASSERT(svgFrame, "Unexpected frame type"); 25 26 nsPoint pointRelativeToReferenceFrame = aRect.Center(); 27 // ToReferenceFrame() includes mFrame->GetPosition(), our user 28 // space position. 29 nsPoint userSpacePtInAppUnits = pointRelativeToReferenceFrame - 30 (ToReferenceFrame() - mFrame->GetPosition()); 31 gfxPoint userSpacePt = 32 gfxPoint(userSpacePtInAppUnits.x, userSpacePtInAppUnits.y) / 33 AppUnitsPerCSSPixel(); 34 if (auto* target = svgFrame->GetFrameForPoint(userSpacePt)) { 35 aOutFrames->AppendElement(target); 36 } 37 } 38 39 void DisplaySVGItem::Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) { 40 ISVGDisplayableFrame* svgFrame = do_QueryFrame(mFrame); 41 MOZ_ASSERT(svgFrame, "Unexpected frame type"); 42 int32_t appUnitsPerDevPixel = mFrame->PresContext()->AppUnitsPerDevPixel(); 43 44 // ToReferenceFrame() includes our mRect offset, but painting takes 45 // account of that too. To avoid double counting, we subtract that 46 // here. 47 nsPoint offset = ToReferenceFrame() - mFrame->GetPosition(); 48 49 gfxPoint devPixelOffset = 50 nsLayoutUtils::PointToGfxPoint(offset, appUnitsPerDevPixel); 51 52 gfxMatrix tm = SVGUtils::GetCSSPxToDevPxMatrix(mFrame) * 53 gfxMatrix::Translation(devPixelOffset); 54 imgDrawingParams imgParams(aBuilder->GetImageDecodeFlags()); 55 svgFrame->PaintSVG(*aCtx, tm, imgParams); 56 } 57 58 } // namespace mozilla