Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / accessibility / AXInlineTextBox.cpp
1 /*
2  * Copyright (C) 2013, Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "core/accessibility/AXInlineTextBox.h"
31
32 #include "core/accessibility/AXObjectCache.h"
33 #include "core/dom/Range.h"
34 #include "core/rendering/RenderText.h"
35 #include "platform/LayoutUnit.h"
36
37
38 namespace blink {
39
40 using namespace HTMLNames;
41
42 AXInlineTextBox::AXInlineTextBox(PassRefPtr<AbstractInlineTextBox> inlineTextBox)
43     : m_inlineTextBox(inlineTextBox)
44 {
45     RenderText* renderText = m_inlineTextBox->renderText();
46     m_axObjectCache = renderText->document().axObjectCache();
47 }
48
49 AXInlineTextBox::~AXInlineTextBox()
50 {
51     if (m_axObjectCache && m_inlineTextBox)
52         m_axObjectCache->remove(m_inlineTextBox.get());
53 }
54
55 PassRefPtr<AXInlineTextBox> AXInlineTextBox::create(PassRefPtr<AbstractInlineTextBox> inlineTextBox)
56 {
57     return adoptRef(new AXInlineTextBox(inlineTextBox));
58 }
59
60 void AXInlineTextBox::init()
61 {
62 }
63
64 void AXInlineTextBox::detach()
65 {
66     m_inlineTextBox = nullptr;
67     m_axObjectCache = 0;
68     AXObject::detach();
69 }
70
71 LayoutRect AXInlineTextBox::elementRect() const
72 {
73     if (!m_inlineTextBox)
74         return LayoutRect();
75
76     return m_inlineTextBox->bounds();
77 }
78
79 bool AXInlineTextBox::computeAccessibilityIsIgnored() const
80 {
81     if (AXObject* parent = parentObject())
82         return parent->accessibilityIsIgnored();
83
84     return false;
85 }
86
87 void AXInlineTextBox::textCharacterOffsets(Vector<int>& offsets) const
88 {
89     if (!m_inlineTextBox)
90         return;
91
92     unsigned len = m_inlineTextBox->len();
93     Vector<float> widths;
94     m_inlineTextBox->characterWidths(widths);
95     ASSERT(widths.size() == len);
96     offsets.resize(len);
97
98     float widthSoFar = 0;
99     for (unsigned i = 0; i < len; i++) {
100         widthSoFar += widths[i];
101         offsets[i] = lroundf(widthSoFar);
102     }
103 }
104
105 void AXInlineTextBox::wordBoundaries(Vector<PlainTextRange>& words) const
106 {
107     if (!m_inlineTextBox)
108         return;
109
110     Vector<AbstractInlineTextBox::WordBoundaries> wordBoundaries;
111     m_inlineTextBox->wordBoundaries(wordBoundaries);
112     words.resize(wordBoundaries.size());
113     for (unsigned i = 0; i < wordBoundaries.size(); i++)
114         words[i] = PlainTextRange(wordBoundaries[i].startIndex, wordBoundaries[i].endIndex - wordBoundaries[i].startIndex);
115 }
116
117 String AXInlineTextBox::stringValue() const
118 {
119     if (!m_inlineTextBox)
120         return String();
121
122     return m_inlineTextBox->text();
123 }
124
125 AXObject* AXInlineTextBox::parentObject() const
126 {
127     if (!m_inlineTextBox || !m_axObjectCache)
128         return 0;
129
130     RenderText* renderText = m_inlineTextBox->renderText();
131     return m_axObjectCache->getOrCreate(renderText);
132 }
133
134 AccessibilityTextDirection AXInlineTextBox::textDirection() const
135 {
136     if (!m_inlineTextBox)
137         return AccessibilityTextDirectionLeftToRight;
138
139     switch (m_inlineTextBox->direction()) {
140     case AbstractInlineTextBox::LeftToRight:
141         return AccessibilityTextDirectionLeftToRight;
142     case AbstractInlineTextBox::RightToLeft:
143         return AccessibilityTextDirectionRightToLeft;
144     case AbstractInlineTextBox::TopToBottom:
145         return AccessibilityTextDirectionTopToBottom;
146     case AbstractInlineTextBox::BottomToTop:
147         return AccessibilityTextDirectionBottomToTop;
148     }
149
150     return AccessibilityTextDirectionLeftToRight;
151 }
152
153 } // namespace blink