Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / html / HTMLAreaElement.cpp
blobceaffe91dbe8929d226ff1697c612c16351e4e31
1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2009, 2011 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include "config.h"
23 #include "core/html/HTMLAreaElement.h"
25 #include "core/HTMLNames.h"
26 #include "core/dom/ElementTraversal.h"
27 #include "core/html/HTMLImageElement.h"
28 #include "core/html/HTMLMapElement.h"
29 #include "core/layout/HitTestResult.h"
30 #include "core/layout/LayoutImage.h"
31 #include "core/layout/LayoutView.h"
32 #include "platform/LengthFunctions.h"
33 #include "platform/graphics/Path.h"
34 #include "platform/transforms/AffineTransform.h"
36 namespace blink {
38 using namespace HTMLNames;
40 inline HTMLAreaElement::HTMLAreaElement(Document& document)
41 : HTMLAnchorElement(areaTag, document)
42 , m_lastSize(-1, -1)
43 , m_shape(Unknown)
47 // An explicit empty destructor should be in HTMLAreaElement.cpp, because
48 // if an implicit destructor is used or an empty destructor is defined in
49 // HTMLAreaElement.h, when including HTMLAreaElement.h, msvc tries to expand
50 // the destructor and causes a compile error because of lack of blink::Path
51 // definition.
52 HTMLAreaElement::~HTMLAreaElement()
56 DEFINE_NODE_FACTORY(HTMLAreaElement)
58 void HTMLAreaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
60 if (name == shapeAttr) {
61 if (equalIgnoringCase(value, "default"))
62 m_shape = Default;
63 else if (equalIgnoringCase(value, "circle"))
64 m_shape = Circle;
65 else if (equalIgnoringCase(value, "poly"))
66 m_shape = Poly;
67 else if (equalIgnoringCase(value, "rect"))
68 m_shape = Rect;
69 invalidateCachedRegion();
70 } else if (name == coordsAttr) {
71 m_coords = parseHTMLAreaElementCoords(value.string());
72 invalidateCachedRegion();
73 } else if (name == altAttr || name == accesskeyAttr) {
74 // Do nothing.
75 } else {
76 HTMLAnchorElement::parseAttribute(name, value);
80 void HTMLAreaElement::invalidateCachedRegion()
82 m_lastSize = LayoutSize(-1, -1);
85 bool HTMLAreaElement::pointInArea(LayoutPoint location, const LayoutSize& containerSize)
87 if (m_lastSize != containerSize) {
88 m_region = adoptPtr(new Path(getRegion(containerSize)));
89 m_lastSize = containerSize;
92 return m_region->contains(FloatPoint(location));
95 Path HTMLAreaElement::computePath(LayoutObject* obj) const
97 if (!obj)
98 return Path();
100 // FIXME: This doesn't work correctly with transforms.
101 FloatPoint absPos = obj->localToAbsolute();
103 // Default should default to the size of the containing object.
104 LayoutSize size = m_lastSize;
105 if (m_shape == Default)
106 size = obj->absoluteClippedOverflowRect().size();
108 Path p = getRegion(size);
109 float zoomFactor = obj->style()->effectiveZoom();
110 if (zoomFactor != 1.0f) {
111 AffineTransform zoomTransform;
112 zoomTransform.scale(zoomFactor);
113 p.transform(zoomTransform);
116 p.translate(toFloatSize(absPos));
117 return p;
120 LayoutRect HTMLAreaElement::computeRect(LayoutObject* obj) const
122 return enclosingLayoutRect(computePath(obj).boundingRect());
125 Path HTMLAreaElement::getRegion(const LayoutSize& size) const
127 if (m_coords.isEmpty() && m_shape != Default)
128 return Path();
130 LayoutUnit width = size.width();
131 LayoutUnit height = size.height();
133 // If element omits the shape attribute, select shape based on number of coordinates.
134 Shape shape = m_shape;
135 if (shape == Unknown) {
136 if (m_coords.size() == 3)
137 shape = Circle;
138 else if (m_coords.size() == 4)
139 shape = Rect;
140 else if (m_coords.size() >= 6)
141 shape = Poly;
144 Path path;
145 switch (shape) {
146 case Poly:
147 if (m_coords.size() >= 6) {
148 int numPoints = m_coords.size() / 2;
149 path.moveTo(FloatPoint(minimumValueForLength(m_coords[0], width).toFloat(), minimumValueForLength(m_coords[1], height).toFloat()));
150 for (int i = 1; i < numPoints; ++i)
151 path.addLineTo(FloatPoint(minimumValueForLength(m_coords[i * 2], width).toFloat(), minimumValueForLength(m_coords[i * 2 + 1], height).toFloat()));
152 path.closeSubpath();
154 break;
155 case Circle:
156 if (m_coords.size() >= 3) {
157 Length radius = m_coords[2];
158 float r = std::min(minimumValueForLength(radius, width).toFloat(), minimumValueForLength(radius, height).toFloat());
159 path.addEllipse(FloatRect(minimumValueForLength(m_coords[0], width).toFloat() - r, minimumValueForLength(m_coords[1], height).toFloat() - r, 2 * r, 2 * r));
161 break;
162 case Rect:
163 if (m_coords.size() >= 4) {
164 float x0 = minimumValueForLength(m_coords[0], width).toFloat();
165 float y0 = minimumValueForLength(m_coords[1], height).toFloat();
166 float x1 = minimumValueForLength(m_coords[2], width).toFloat();
167 float y1 = minimumValueForLength(m_coords[3], height).toFloat();
168 path.addRect(FloatRect(x0, y0, x1 - x0, y1 - y0));
170 break;
171 case Default:
172 path.addRect(FloatRect(0, 0, width.toFloat(), height.toFloat()));
173 break;
174 case Unknown:
175 break;
178 return path;
181 HTMLImageElement* HTMLAreaElement::imageElement() const
183 if (HTMLMapElement* mapElement = Traversal<HTMLMapElement>::firstAncestor(*this))
184 return mapElement->imageElement();
185 return nullptr;
188 bool HTMLAreaElement::isKeyboardFocusable() const
190 return isFocusable();
193 bool HTMLAreaElement::isMouseFocusable() const
195 return isFocusable();
198 bool HTMLAreaElement::layoutObjectIsFocusable() const
200 HTMLImageElement* image = imageElement();
201 if (!image || !image->layoutObject() || image->layoutObject()->style()->visibility() != VISIBLE)
202 return false;
204 return supportsFocus() && Element::tabIndex() >= 0;
207 void HTMLAreaElement::setFocus(bool shouldBeFocused)
209 if (focused() == shouldBeFocused)
210 return;
212 HTMLAnchorElement::setFocus(shouldBeFocused);
214 HTMLImageElement* imageElement = this->imageElement();
215 if (!imageElement)
216 return;
218 LayoutObject* layoutObject = imageElement->layoutObject();
219 if (!layoutObject || !layoutObject->isImage())
220 return;
222 toLayoutImage(layoutObject)->areaElementFocusChanged(this);
225 void HTMLAreaElement::updateFocusAppearance(bool restorePreviousSelection)
227 if (!isFocusable())
228 return;
230 HTMLImageElement* imageElement = this->imageElement();
231 if (!imageElement)
232 return;
234 imageElement->updateFocusAppearance(restorePreviousSelection);