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 using namespace std;
38
39 namespace WebCore {
40
41 using namespace HTMLNames;
42
AXInlineTextBox(PassRefPtr<AbstractInlineTextBox> inlineTextBox)43 AXInlineTextBox::AXInlineTextBox(PassRefPtr<AbstractInlineTextBox> inlineTextBox)
44 : m_inlineTextBox(inlineTextBox)
45 {
46 RenderText* renderText = m_inlineTextBox->renderText();
47 m_axObjectCache = renderText->document().axObjectCache();
48 }
49
~AXInlineTextBox()50 AXInlineTextBox::~AXInlineTextBox()
51 {
52 if (m_axObjectCache && m_inlineTextBox)
53 m_axObjectCache->remove(m_inlineTextBox.get());
54 }
55
create(PassRefPtr<AbstractInlineTextBox> inlineTextBox)56 PassRefPtr<AXInlineTextBox> AXInlineTextBox::create(PassRefPtr<AbstractInlineTextBox> inlineTextBox)
57 {
58 return adoptRef(new AXInlineTextBox(inlineTextBox));
59 }
60
init()61 void AXInlineTextBox::init()
62 {
63 }
64
detach()65 void AXInlineTextBox::detach()
66 {
67 m_inlineTextBox = 0;
68 m_axObjectCache = 0;
69 AXObject::detach();
70 }
71
elementRect() const72 LayoutRect AXInlineTextBox::elementRect() const
73 {
74 if (!m_inlineTextBox)
75 return LayoutRect();
76
77 return m_inlineTextBox->bounds();
78 }
79
computeAccessibilityIsIgnored() const80 bool AXInlineTextBox::computeAccessibilityIsIgnored() const
81 {
82 if (AXObject* parent = parentObject())
83 return parent->accessibilityIsIgnored();
84
85 return false;
86 }
87
textCharacterOffsets(Vector<int> & offsets) const88 void AXInlineTextBox::textCharacterOffsets(Vector<int>& offsets) const
89 {
90 if (!m_inlineTextBox)
91 return;
92
93 unsigned len = m_inlineTextBox->len();
94 Vector<float> widths;
95 m_inlineTextBox->characterWidths(widths);
96 ASSERT(widths.size() == len);
97 offsets.resize(len);
98
99 float widthSoFar = 0;
100 for (unsigned i = 0; i < len; i++) {
101 widthSoFar += widths[i];
102 offsets[i] = LayoutUnit::fromFloatRound(widthSoFar);
103 }
104 }
105
wordBoundaries(Vector<PlainTextRange> & words) const106 void AXInlineTextBox::wordBoundaries(Vector<PlainTextRange>& words) const
107 {
108 if (!m_inlineTextBox)
109 return;
110
111 Vector<AbstractInlineTextBox::WordBoundaries> wordBoundaries;
112 m_inlineTextBox->wordBoundaries(wordBoundaries);
113 words.resize(wordBoundaries.size());
114 for (unsigned i = 0; i < wordBoundaries.size(); i++)
115 words[i] = PlainTextRange(wordBoundaries[i].startIndex, wordBoundaries[i].endIndex - wordBoundaries[i].startIndex);
116 }
117
stringValue() const118 String AXInlineTextBox::stringValue() const
119 {
120 if (!m_inlineTextBox)
121 return String();
122
123 return m_inlineTextBox->text();
124 }
125
parentObject() const126 AXObject* AXInlineTextBox::parentObject() const
127 {
128 if (!m_inlineTextBox || !m_axObjectCache)
129 return 0;
130
131 RenderText* renderText = m_inlineTextBox->renderText();
132 return m_axObjectCache->getOrCreate(renderText);
133 }
134
textDirection() const135 AccessibilityTextDirection AXInlineTextBox::textDirection() const
136 {
137 if (!m_inlineTextBox)
138 return AccessibilityTextDirectionLeftToRight;
139
140 switch (m_inlineTextBox->direction()) {
141 case AbstractInlineTextBox::LeftToRight:
142 return AccessibilityTextDirectionLeftToRight;
143 case AbstractInlineTextBox::RightToLeft:
144 return AccessibilityTextDirectionRightToLeft;
145 case AbstractInlineTextBox::TopToBottom:
146 return AccessibilityTextDirectionTopToBottom;
147 case AbstractInlineTextBox::BottomToTop:
148 return AccessibilityTextDirectionBottomToTop;
149 }
150
151 return AccessibilityTextDirectionLeftToRight;
152 }
153
154 } // namespace WebCore
155