Skip to content

Instantly share code, notes, and snippets.

@kenchris
Created January 23, 2012 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenchris/1662979 to your computer and use it in GitHub Desktop.
Save kenchris/1662979 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2011 Google Inc. All rights reserved.
* Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GestureTapHighlighter.h"
#include "Element.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "GraphicsTypes.h"
#include "Node.h"
#include "Page.h"
#include "RenderBoxModelObject.h"
#include "RenderInline.h"
#include "RenderObject.h"
#include "RenderLayer.h"
namespace WebCore {
namespace{
inline LayoutSize frameToMainFrameOffset(Frame* frame)
{
LayoutPoint mainFramePoint = frame->page()->mainFrame()->view()->rootViewToContents(frame->view()->contentsToRootView(LayoutPoint()));
return toLayoutSize(mainFramePoint);
}
} // anonymous namespace
namespace GestureTapHighlighter {
void drawHighlight(GraphicsContext* context, Node* node, const Color& color)
{
RenderObject* renderer = node->renderer();
Frame* containingFrame = node->document()->frame();
if (!renderer || !containingFrame || !context)
return;
LayoutSize mainFrameOffset = frameToMainFrameOffset(containingFrame);
// Don't translate the context if the frame is rendered in page coordinates.
FrameView* view = containingFrame->page()->mainFrame()->view();
if (!view->delegatesScrolling()) {
FloatRect visibleRect = view->visibleContentRect();
context->translate(-visibleRect.x(), -visibleRect.y());
}
if (renderer->isBox() || renderer->isRenderInline()) {
LayoutRect contentBox;
LayoutRect paddingBox;
LayoutRect borderBox;
if (renderer->isBox()) {
RenderBox* renderBox = toRenderBox(renderer);
contentBox = renderBox->contentBoxRect();
paddingBox = LayoutRect(contentBox.x() - renderBox->paddingLeft(), contentBox.y() - renderBox->paddingTop(),
contentBox.width() + renderBox->paddingLeft() + renderBox->paddingRight(), contentBox.height() + renderBox->paddingTop() + renderBox->paddingBottom());
borderBox = LayoutRect(paddingBox.x() - renderBox->borderLeft(), paddingBox.y() - renderBox->borderTop(),
paddingBox.width() + renderBox->borderLeft() + renderBox->borderRight(), paddingBox.height() + renderBox->borderTop() + renderBox->borderBottom());
} else {
RenderInline* renderInline = toRenderInline(renderer);
// RenderInline's bounding box includes paddings and borders, excludes margins.
borderBox = renderInline->linesBoundingBox();
}
RoundedRect rect(borderBox);
rect.expandRadii(10);
rect.inflateWithRadii(5);
AffineTransform trans;
LayoutPoint referencePoint;
while (renderer) {
RenderObject* next = renderer->container();
if (!next)
break;
LayoutSize containerOffset = renderer->offsetFromContainer(next, referencePoint);
TransformationMatrix t;
renderer->getTransformFromContainer(next, containerOffset, t);
trans *= t.toAffineTransform();
referencePoint.move(containerOffset);
renderer = next;
}
rect.move(mainFrameOffset);
context->concatCTM(trans);
context->fillRoundedRect(rect, color, ColorSpaceDeviceRGB);
}
}
} // namespace GestureTapHighlighter
} // namespace WebCore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment