2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/rendering/AbstractInlineTextBox.h"
34 #include "core/editing/TextIterator.h"
35 #include "platform/text/TextBreakIterator.h"
39 AbstractInlineTextBox::InlineToAbstractInlineTextBoxHashMap* AbstractInlineTextBox::gAbstractInlineTextBoxMap = 0;
41 PassRefPtr<AbstractInlineTextBox> AbstractInlineTextBox::getOrCreate(RenderText* renderText, InlineTextBox* inlineTextBox)
46 if (!gAbstractInlineTextBoxMap)
47 gAbstractInlineTextBoxMap = new InlineToAbstractInlineTextBoxHashMap();
49 InlineToAbstractInlineTextBoxHashMap::const_iterator it = gAbstractInlineTextBoxMap->find(inlineTextBox);
50 if (it != gAbstractInlineTextBoxMap->end())
53 RefPtr<AbstractInlineTextBox> obj = adoptRef(new AbstractInlineTextBox(renderText, inlineTextBox));
54 gAbstractInlineTextBoxMap->set(inlineTextBox, obj);
58 void AbstractInlineTextBox::willDestroy(InlineTextBox* inlineTextBox)
60 if (!gAbstractInlineTextBoxMap)
63 InlineToAbstractInlineTextBoxHashMap::const_iterator it = gAbstractInlineTextBoxMap->find(inlineTextBox);
64 if (it != gAbstractInlineTextBoxMap->end()) {
66 gAbstractInlineTextBoxMap->remove(inlineTextBox);
70 void AbstractInlineTextBox::detach()
76 PassRefPtr<AbstractInlineTextBox> AbstractInlineTextBox::nextInlineTextBox() const
81 return getOrCreate(m_renderText, m_inlineTextBox->nextTextBox());
84 LayoutRect AbstractInlineTextBox::bounds() const
86 if (!m_inlineTextBox || !m_renderText)
89 FloatRect boundaries = m_inlineTextBox->calculateBoundaries();
90 return m_renderText->localToAbsoluteQuad(boundaries).enclosingBoundingBox();
93 unsigned AbstractInlineTextBox::start() const
98 return m_inlineTextBox->start();
101 unsigned AbstractInlineTextBox::len() const
103 if (!m_inlineTextBox)
106 return m_inlineTextBox->len();
109 AbstractInlineTextBox::Direction AbstractInlineTextBox::direction() const
111 if (!m_inlineTextBox || !m_renderText)
114 if (m_renderText->style()->isHorizontalWritingMode())
115 return (m_inlineTextBox->direction() == RTL ? RightToLeft : LeftToRight);
116 return (m_inlineTextBox->direction() == RTL ? BottomToTop : TopToBottom);
119 void AbstractInlineTextBox::characterWidths(Vector<float>& widths) const
121 if (!m_inlineTextBox)
124 m_inlineTextBox->characterWidths(widths);
127 void AbstractInlineTextBox::wordBoundaries(Vector<WordBoundaries>& words) const
129 if (!m_inlineTextBox)
132 String text = this->text();
133 int len = text.length();
134 TextBreakIterator* iterator = wordBreakIterator(text, 0, len);
135 int pos = iterator->first();
136 while (pos >= 0 && pos < len) {
137 int next = iterator->next();
138 if (isWordTextBreak(iterator))
139 words.append(WordBoundaries(pos, next));
144 String AbstractInlineTextBox::text() const
146 if (!m_inlineTextBox || !m_renderText)
149 unsigned start = m_inlineTextBox->start();
150 unsigned len = m_inlineTextBox->len();
151 if (Node* node = m_renderText->node()) {
152 RefPtrWillBeRawPtr<Range> range = Range::create(node->document());
153 range->setStart(node, start, IGNORE_EXCEPTION);
154 range->setEnd(node, start + len, IGNORE_EXCEPTION);
155 return plainText(range.get(), TextIteratorIgnoresStyleVisibility);
158 String result = m_renderText->text().substring(start, len).simplifyWhiteSpace(WTF::DoNotStripWhiteSpace);
159 if (m_inlineTextBox->nextTextBox() && m_inlineTextBox->nextTextBox()->start() > m_inlineTextBox->end() && result.length() && !result.right(1).containsOnlyWhitespace())
164 } // namespace WebCore