2 * Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "core/rendering/InlineBox.h"
23 #include "core/paint/BlockPainter.h"
24 #include "core/rendering/InlineFlowBox.h"
25 #include "core/rendering/PaintInfo.h"
26 #include "core/rendering/RenderBlockFlow.h"
27 #include "core/rendering/RenderObjectInlines.h"
28 #include "core/rendering/RootInlineBox.h"
29 #include "platform/Partitions.h"
30 #include "platform/fonts/FontMetrics.h"
38 struct SameSizeAsInlineBox {
39 virtual ~SameSizeAsInlineBox() { }
49 COMPILE_ASSERT(sizeof(InlineBox) == sizeof(SameSizeAsInlineBox), InlineBox_size_guard);
53 InlineBox::~InlineBox()
55 if (!m_hasBadParent && m_parent)
56 m_parent->setHasBadChildList();
61 void InlineBox::remove(MarkLineBoxes markLineBoxes)
64 parent()->removeChild(this, markLineBoxes);
67 void* InlineBox::operator new(size_t sz)
69 return partitionAlloc(Partitions::getRenderingPartition(), sz);
72 void InlineBox::operator delete(void* ptr)
78 const char* InlineBox::boxName() const
83 void InlineBox::showTreeForThis() const
85 renderer().showTreeForThis();
88 void InlineBox::showLineTreeForThis() const
90 renderer().containingBlock()->showLineTreeAndMark(this, "*");
93 void InlineBox::showLineTreeAndMark(const InlineBox* markedBox1, const char* markedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const RenderObject* obj, int depth) const
95 int printedCharacters = 0;
96 if (this == markedBox1)
97 printedCharacters += fprintf(stderr, "%s", markedLabel1);
98 if (this == markedBox2)
99 printedCharacters += fprintf(stderr, "%s", markedLabel2);
100 if (&renderer() == obj)
101 printedCharacters += fprintf(stderr, "*");
102 for (; printedCharacters < depth * 2; printedCharacters++)
105 showBox(printedCharacters);
108 void InlineBox::showBox(int printedCharacters) const
110 printedCharacters += fprintf(stderr, "%s\t%p", boxName(), this);
111 for (; printedCharacters < showTreeCharacterOffset; printedCharacters++)
113 fprintf(stderr, "\t%s %p {pos=%g,%g size=%g,%g} baseline=%i/%i\n",
114 renderer().renderName(), &renderer(), x(), y(), width(), height(),
115 baselinePosition(AlphabeticBaseline),
116 baselinePosition(IdeographicBaseline));
120 float InlineBox::logicalHeight() const
122 if (hasVirtualLogicalHeight())
123 return virtualLogicalHeight();
125 if (renderer().isText())
126 return m_bitfields.isText() ? renderer().style(isFirstLineStyle())->fontMetrics().height() : 0;
127 if (renderer().isBox() && parent())
128 return isHorizontal() ? toRenderBox(renderer()).height().toFloat() : toRenderBox(renderer()).width().toFloat();
130 ASSERT(isInlineFlowBox());
131 RenderBoxModelObject* flowObject = boxModelObject();
132 const FontMetrics& fontMetrics = renderer().style(isFirstLineStyle())->fontMetrics();
133 float result = fontMetrics.height();
135 result += flowObject->borderAndPaddingLogicalHeight();
139 int InlineBox::baselinePosition(FontBaseline baselineType) const
141 return boxModelObject()->baselinePosition(baselineType, m_bitfields.firstLine(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOnContainingLine);
144 LayoutUnit InlineBox::lineHeight() const
146 return boxModelObject()->lineHeight(m_bitfields.firstLine(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOnContainingLine);
149 int InlineBox::caretMinOffset() const
151 return renderer().caretMinOffset();
154 int InlineBox::caretMaxOffset() const
156 return renderer().caretMaxOffset();
159 void InlineBox::dirtyLineBoxes()
162 for (InlineFlowBox* curr = parent(); curr && !curr->isDirty(); curr = curr->parent())
166 void InlineBox::deleteLine()
168 if (!m_bitfields.extracted() && renderer().isBox())
169 toRenderBox(renderer()).setInlineBoxWrapper(0);
173 void InlineBox::extractLine()
175 m_bitfields.setExtracted(true);
176 if (renderer().isBox())
177 toRenderBox(renderer()).setInlineBoxWrapper(0);
180 void InlineBox::attachLine()
182 m_bitfields.setExtracted(false);
183 if (renderer().isBox())
184 toRenderBox(renderer()).setInlineBoxWrapper(this);
187 void InlineBox::adjustPosition(float dx, float dy)
189 m_topLeft.move(dx, dy);
191 if (renderer().isReplaced())
192 toRenderBox(renderer()).move(dx, dy);
195 void InlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit /* lineTop */, LayoutUnit /*lineBottom*/)
197 BlockPainter::paintInlineBox(*this, paintInfo, paintOffset);
200 bool InlineBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit /* lineTop */, LayoutUnit /*lineBottom*/)
202 // Hit test all phases of replaced elements atomically, as though the replaced element established its
203 // own stacking context. (See Appendix E.2, section 6.4 on inline block/table elements in the CSS2.1
205 LayoutPoint childPoint = accumulatedOffset;
206 if (parent()->renderer().style()->isFlippedBlocksWritingMode()) // Faster than calling containingBlock().
207 childPoint = renderer().containingBlock()->flipForWritingModeForChild(&toRenderBox(renderer()), childPoint);
209 return renderer().hitTest(request, result, locationInContainer, childPoint);
212 const RootInlineBox& InlineBox::root() const
215 return m_parent->root();
216 ASSERT(isRootInlineBox());
217 return static_cast<const RootInlineBox&>(*this);
220 RootInlineBox& InlineBox::root()
223 return m_parent->root();
224 ASSERT(isRootInlineBox());
225 return static_cast<RootInlineBox&>(*this);
228 bool InlineBox::nextOnLineExists() const
230 if (!m_bitfields.determinedIfNextOnLineExists()) {
231 m_bitfields.setDeterminedIfNextOnLineExists(true);
234 m_bitfields.setNextOnLineExists(false);
235 else if (nextOnLine())
236 m_bitfields.setNextOnLineExists(true);
238 m_bitfields.setNextOnLineExists(parent()->nextOnLineExists());
240 return m_bitfields.nextOnLineExists();
243 InlineBox* InlineBox::nextLeafChild() const
246 for (InlineBox* box = nextOnLine(); box && !leaf; box = box->nextOnLine())
247 leaf = box->isLeaf() ? box : toInlineFlowBox(box)->firstLeafChild();
248 if (!leaf && parent())
249 leaf = parent()->nextLeafChild();
253 InlineBox* InlineBox::prevLeafChild() const
256 for (InlineBox* box = prevOnLine(); box && !leaf; box = box->prevOnLine())
257 leaf = box->isLeaf() ? box : toInlineFlowBox(box)->lastLeafChild();
258 if (!leaf && parent())
259 leaf = parent()->prevLeafChild();
263 InlineBox* InlineBox::nextLeafChildIgnoringLineBreak() const
265 InlineBox* leaf = nextLeafChild();
266 if (leaf && leaf->isLineBreak())
271 InlineBox* InlineBox::prevLeafChildIgnoringLineBreak() const
273 InlineBox* leaf = prevLeafChild();
274 if (leaf && leaf->isLineBreak())
279 RenderObject::SelectionState InlineBox::selectionState() const
281 return renderer().selectionState();
284 bool InlineBox::canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth) const
286 // Non-replaced elements can always accommodate an ellipsis.
287 if (!renderer().isReplaced())
290 IntRect boxRect(left(), 0, m_logicalWidth, 10);
291 IntRect ellipsisRect(ltr ? blockEdge - ellipsisWidth : blockEdge, 0, ellipsisWidth, 10);
292 return !(boxRect.intersects(ellipsisRect));
295 float InlineBox::placeEllipsisBox(bool, float, float, float, float& truncatedWidth, bool&)
297 // Use -1 to mean "we didn't set the position."
298 truncatedWidth += logicalWidth();
302 void InlineBox::clearKnownToHaveNoOverflow()
304 m_bitfields.setKnownToHaveNoOverflow(false);
305 if (parent() && parent()->knownToHaveNoOverflow())
306 parent()->clearKnownToHaveNoOverflow();
309 FloatPoint InlineBox::locationIncludingFlipping()
311 if (!renderer().style()->isFlippedBlocksWritingMode())
312 return FloatPoint(x(), y());
313 RenderBlockFlow& block = root().block();
314 if (block.style()->isHorizontalWritingMode())
315 return FloatPoint(x(), block.height() - height() - y());
317 return FloatPoint(block.width() - width() - x(), y());
320 void InlineBox::flipForWritingMode(FloatRect& rect)
322 if (!renderer().style()->isFlippedBlocksWritingMode())
324 root().block().flipForWritingMode(rect);
327 FloatPoint InlineBox::flipForWritingMode(const FloatPoint& point)
329 if (!renderer().style()->isFlippedBlocksWritingMode())
331 return root().block().flipForWritingMode(point);
334 void InlineBox::flipForWritingMode(LayoutRect& rect)
336 if (!renderer().style()->isFlippedBlocksWritingMode())
338 root().block().flipForWritingMode(rect);
341 LayoutPoint InlineBox::flipForWritingMode(const LayoutPoint& point)
343 if (!renderer().style()->isFlippedBlocksWritingMode())
345 return root().block().flipForWritingMode(point);
352 void showTree(const blink::InlineBox* b)
355 b->showTreeForThis();
358 void showLineTree(const blink::InlineBox* b)
361 b->showLineTreeForThis();