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 "core/rendering/InlineTextBox.h"
36 #include "platform/text/TextBreakIterator.h"
40 AbstractInlineTextBox::InlineToAbstractInlineTextBoxHashMap* AbstractInlineTextBox::gAbstractInlineTextBoxMap = 0;
42 PassRefPtr<AbstractInlineTextBox> AbstractInlineTextBox::getOrCreate(RenderText* renderText, InlineTextBox* inlineTextBox)
47 if (!gAbstractInlineTextBoxMap)
48 gAbstractInlineTextBoxMap = new InlineToAbstractInlineTextBoxHashMap();
50 InlineToAbstractInlineTextBoxHashMap::const_iterator it = gAbstractInlineTextBoxMap->find(inlineTextBox);
51 if (it != gAbstractInlineTextBoxMap->end())
54 RefPtr<AbstractInlineTextBox> obj = adoptRef(new AbstractInlineTextBox(renderText, inlineTextBox));
55 gAbstractInlineTextBoxMap->set(inlineTextBox, obj);
59 void AbstractInlineTextBox::willDestroy(InlineTextBox* inlineTextBox)
61 if (!gAbstractInlineTextBoxMap)
64 InlineToAbstractInlineTextBoxHashMap::const_iterator it = gAbstractInlineTextBoxMap->find(inlineTextBox);
65 if (it != gAbstractInlineTextBoxMap->end()) {
67 gAbstractInlineTextBoxMap->remove(inlineTextBox);
71 void AbstractInlineTextBox::detach()
77 PassRefPtr<AbstractInlineTextBox> AbstractInlineTextBox::nextInlineTextBox() const
82 return getOrCreate(m_renderText, m_inlineTextBox->nextTextBox());
85 LayoutRect AbstractInlineTextBox::bounds() const
87 if (!m_inlineTextBox || !m_renderText)
90 FloatRect boundaries = m_inlineTextBox->calculateBoundaries();
91 return m_renderText->localToAbsoluteQuad(boundaries).enclosingBoundingBox();
94 unsigned AbstractInlineTextBox::start() const
99 return m_inlineTextBox->start();
102 unsigned AbstractInlineTextBox::len() const
104 if (!m_inlineTextBox)
107 return m_inlineTextBox->len();
110 AbstractInlineTextBox::Direction AbstractInlineTextBox::direction() const
112 if (!m_inlineTextBox || !m_renderText)
115 if (m_renderText->style()->isHorizontalWritingMode())
116 return (m_inlineTextBox->direction() == RTL ? RightToLeft : LeftToRight);
117 return (m_inlineTextBox->direction() == RTL ? BottomToTop : TopToBottom);
120 void AbstractInlineTextBox::characterWidths(Vector<float>& widths) const
122 if (!m_inlineTextBox)
125 m_inlineTextBox->characterWidths(widths);
128 void AbstractInlineTextBox::wordBoundaries(Vector<WordBoundaries>& words) const
130 if (!m_inlineTextBox)
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));
145 String AbstractInlineTextBox::text() const
147 if (!m_inlineTextBox || !m_renderText)
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);
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())
165 } // namespace WebCore