- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / AbstractInlineTextBox.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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "core/rendering/AbstractInlineTextBox.h"
33
34 #include "core/editing/TextIterator.h"
35 #include "core/rendering/InlineTextBox.h"
36 #include "platform/text/TextBreakIterator.h"
37
38 namespace WebCore {
39
40 AbstractInlineTextBox::InlineToAbstractInlineTextBoxHashMap* AbstractInlineTextBox::gAbstractInlineTextBoxMap = 0;
41
42 PassRefPtr<AbstractInlineTextBox> AbstractInlineTextBox::getOrCreate(RenderText* renderText, InlineTextBox* inlineTextBox)
43 {
44     if (!inlineTextBox)
45         return 0;
46
47     if (!gAbstractInlineTextBoxMap)
48         gAbstractInlineTextBoxMap = new InlineToAbstractInlineTextBoxHashMap();
49
50     InlineToAbstractInlineTextBoxHashMap::const_iterator it = gAbstractInlineTextBoxMap->find(inlineTextBox);
51     if (it != gAbstractInlineTextBoxMap->end())
52         return it->value;
53
54     RefPtr<AbstractInlineTextBox> obj = adoptRef(new AbstractInlineTextBox(renderText, inlineTextBox));
55     gAbstractInlineTextBoxMap->set(inlineTextBox, obj);
56     return obj;
57 }
58
59 void AbstractInlineTextBox::willDestroy(InlineTextBox* inlineTextBox)
60 {
61     if (!gAbstractInlineTextBoxMap)
62         return;
63
64     InlineToAbstractInlineTextBoxHashMap::const_iterator it = gAbstractInlineTextBoxMap->find(inlineTextBox);
65     if (it != gAbstractInlineTextBoxMap->end()) {
66         it->value->detach();
67         gAbstractInlineTextBoxMap->remove(inlineTextBox);
68     }
69 }
70
71 void AbstractInlineTextBox::detach()
72 {
73     m_renderText = 0;
74     m_inlineTextBox = 0;
75 }
76
77 PassRefPtr<AbstractInlineTextBox> AbstractInlineTextBox::nextInlineTextBox() const
78 {
79     if (!m_inlineTextBox)
80         return 0;
81
82     return getOrCreate(m_renderText, m_inlineTextBox->nextTextBox());
83 }
84
85 LayoutRect AbstractInlineTextBox::bounds() const
86 {
87     if (!m_inlineTextBox || !m_renderText)
88         return LayoutRect();
89
90     FloatRect boundaries = m_inlineTextBox->calculateBoundaries();
91     return m_renderText->localToAbsoluteQuad(boundaries).enclosingBoundingBox();
92 }
93
94 unsigned AbstractInlineTextBox::start() const
95 {
96     if (!m_inlineTextBox)
97         return 0;
98
99     return m_inlineTextBox->start();
100 }
101
102 unsigned AbstractInlineTextBox::len() const
103 {
104     if (!m_inlineTextBox)
105         return 0;
106
107     return m_inlineTextBox->len();
108 }
109
110 AbstractInlineTextBox::Direction AbstractInlineTextBox::direction() const
111 {
112     if (!m_inlineTextBox || !m_renderText)
113         return LeftToRight;
114
115     if (m_renderText->style()->isHorizontalWritingMode())
116         return (m_inlineTextBox->direction() == RTL ? RightToLeft : LeftToRight);
117     return (m_inlineTextBox->direction() == RTL ? BottomToTop : TopToBottom);
118 }
119
120 void AbstractInlineTextBox::characterWidths(Vector<float>& widths) const
121 {
122     if (!m_inlineTextBox)
123         return;
124
125     m_inlineTextBox->characterWidths(widths);
126 }
127
128 void AbstractInlineTextBox::wordBoundaries(Vector<WordBoundaries>& words) const
129 {
130     if (!m_inlineTextBox)
131         return;
132
133     String text = this->text();
134     int len = text.length();
135     TextBreakIterator* iterator = wordBreakIterator(text, 0, len);
136     int pos = iterator->first();
137     while (pos >= 0 && pos < len) {
138         int next = iterator->next();
139         if (isWordTextBreak(iterator))
140             words.append(WordBoundaries(pos, next));
141         pos = next;
142     }
143 }
144
145 String AbstractInlineTextBox::text() const
146 {
147     if (!m_inlineTextBox || !m_renderText)
148         return String();
149
150     unsigned start = m_inlineTextBox->start();
151     unsigned len = m_inlineTextBox->len();
152     if (Node* node = m_renderText->node()) {
153         RefPtr<Range> range = Range::create(node->document());
154         range->setStart(node, start, IGNORE_EXCEPTION);
155         range->setEnd(node, start + len, IGNORE_EXCEPTION);
156         return plainText(range.get(), TextIteratorIgnoresStyleVisibility);
157     }
158
159     String result = m_renderText->text().substring(start, len).simplifyWhiteSpace(WTF::DoNotStripWhiteSpace);
160     if (m_inlineTextBox->nextTextBox() && m_inlineTextBox->nextTextBox()->start() > m_inlineTextBox->end() && result.length() && !result.right(1).containsOnlyWhitespace())
161         return result + " ";
162     return result;
163 }
164
165 } // namespace WebCore