2 * This file is part of the DOM implementation for KDE.
4 * (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 2000 Dirk Mueller (mueller@kde.org)
6 * Copyright (C) 2004 Apple Computer, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 //#define DEBUG_LAYOUT
27 #include "rendering/render_text.h"
29 #include "rendering/render_canvas.h"
30 #include "rendering/render_object.h"
31 #include "rendering/break_lines.h"
32 #include "xml/dom2_rangeimpl.h"
33 #include "xml/dom_nodeimpl.h"
34 #include "xml/dom_docimpl.h"
35 #include "xml/dom_position.h"
36 #include "render_arena.h"
38 #include "misc/loader.h"
40 #include "khtml_part.h"
46 #include <unicode/ubrk.h>
47 #include <unicode/uloc.h>
48 #include <unicode/utypes.h>
49 #include <unicode/parseerr.h>
51 using namespace khtml;
55 static bool inInlineTextBoxDetach;
58 void InlineTextBox::detach(RenderArena* renderArena)
61 inInlineTextBoxDetach = true;
65 inInlineTextBoxDetach = false;
68 // Recover the size left there for us by operator delete and free the memory.
69 renderArena->free(*(size_t *)this, this);
72 void* InlineTextBox::operator new(size_t sz, RenderArena* renderArena) throw()
74 return renderArena->allocate(sz);
77 void InlineTextBox::operator delete(void* ptr, size_t sz)
79 assert(inInlineTextBoxDetach);
81 // Stash size where detach can find it.
85 RenderText* InlineTextBox::textObject()
87 return static_cast<RenderText*>(m_object);
90 bool InlineTextBox::checkVerticalPoint(int _y, int _ty, int _h)
93 int bottomY = m_y + m_height;
94 if (root()->hasSelectedChildren()) {
95 topY = kMin(root()->selectionTop(), topY);
96 bottomY = kMax(bottomY, root()->bottomOverflow());
98 if ((_ty + topY >= _y + _h) || (_ty + bottomY <= _y))
103 bool InlineTextBox::isSelected(int startPos, int endPos) const
105 int sPos = kMax(startPos - m_start, 0);
106 int ePos = kMin(endPos - m_start, (int)m_len);
107 return (sPos < ePos);
110 RenderObject::SelectionState InlineTextBox::selectionState()
112 RenderObject::SelectionState state = object()->selectionState();
113 if (state == RenderObject::SelectionStart || state == RenderObject::SelectionEnd ||
114 state == RenderObject::SelectionBoth) {
115 int startPos, endPos;
116 object()->selectionStartEnd(startPos, endPos);
118 bool start = (state != RenderObject::SelectionEnd && startPos >= m_start && startPos < m_start + m_len);
119 bool end = (state != RenderObject::SelectionStart && endPos > m_start && endPos <= m_start + m_len);
121 state = RenderObject::SelectionBoth;
123 state = RenderObject::SelectionStart;
125 state = RenderObject::SelectionEnd;
126 else if ((state == RenderObject::SelectionEnd || startPos < m_start) &&
127 (state == RenderObject::SelectionStart || endPos > m_start + m_len))
128 state = RenderObject::SelectionInside;
133 QRect InlineTextBox::selectionRect(int tx, int ty, int startPos, int endPos)
135 int sPos = kMax(startPos - m_start, 0);
136 int ePos = kMin(endPos - m_start, (int)m_len);
141 RootInlineBox* rootBox = root();
142 int selStart = m_reversed ? m_x + m_width : m_x;
143 int selEnd = selStart;
144 int selTop = rootBox->selectionTop();
145 int selHeight = rootBox->selectionHeight();
147 // FIXME: For justified text, just return the entire text box's rect. At the moment there's still no easy
148 // way to get the width of a run including the justification padding.
149 if (sPos > 0 && !m_toAdd) {
150 // The selection begins in the middle of our run.
151 int w = textObject()->width(m_start, sPos, m_firstLine);
158 if (m_toAdd || (sPos == 0 && ePos == m_len)) {
162 selEnd = m_x + m_width;
165 // Our run is partially selected, and so we have to actually do a measurement.
166 int w = textObject()->width(sPos + m_start, ePos - sPos, m_firstLine);
168 selEnd = selStart - w;
170 selEnd = selStart + w;
173 int selLeft = m_reversed ? selEnd : selStart;
174 int selRight = m_reversed ? selStart : selEnd;
176 return QRect(selLeft + tx, selTop + ty, selRight - selLeft, selHeight);
179 void InlineTextBox::deleteLine(RenderArena* arena)
181 static_cast<RenderText*>(m_object)->removeTextBox(this);
185 void InlineTextBox::extractLine()
190 static_cast<RenderText*>(m_object)->extractTextBox(this);
193 void InlineTextBox::attachLine()
198 static_cast<RenderText*>(m_object)->attachTextBox(this);
201 int InlineTextBox::placeEllipsisBox(bool ltr, int blockEdge, int ellipsisWidth, bool& foundBox)
204 m_truncation = cFullTruncation;
208 int ellipsisX = ltr ? blockEdge - ellipsisWidth : blockEdge + ellipsisWidth;
210 // For LTR, if the left edge of the ellipsis is to the left of our text run, then we are the run that will get truncated.
212 if (ellipsisX <= m_x) {
213 // Too far. Just set full truncation, but return -1 and let the ellipsis just be placed at the edge of the box.
214 m_truncation = cFullTruncation;
219 if (ellipsisX < m_x + m_width) {
221 return -1; // FIXME: Support LTR truncation when the last run is RTL someday.
225 int offset = offsetForPosition(ellipsisX, false);
227 // No characters should be rendered. Set ourselves to full truncation and place the ellipsis at the min of our start
228 // and the ellipsis edge.
229 m_truncation = cFullTruncation;
230 return kMin(ellipsisX, m_x);
233 // Set the truncation index on the text run. The ellipsis needs to be placed just after the last visible character.
234 m_truncation = offset + m_start;
235 return m_x + static_cast<RenderText*>(m_object)->width(m_start, offset, m_firstLine);
239 // FIXME: Support RTL truncation someday, including both modes (when the leftmost run on the line is either RTL or LTR)
245 simpleDifferenceBetweenColors(QColor c1, QColor c2)
247 // a distance could be computed by squaring the differences between components, but
248 // this is faster and so far seems good enough for our purposes.
249 return abs(c1.red() - c2.red()) + abs(c1.green() - c2.green()) + abs(c1.blue() - c2.blue());
253 correctedTextColor(QColor textColor, QColor backgroundColor)
255 // Adjust the text color if it is too close to the background color,
256 // by darkening or lightening it to move it further away.
258 int d = simpleDifferenceBetweenColors(textColor, backgroundColor);
259 // semi-arbitrarily chose 255 value here after a few tests;
264 int distanceFromWhite = simpleDifferenceBetweenColors(textColor, Qt::white);
265 int distanceFromBlack = simpleDifferenceBetweenColors(textColor, Qt::black);
267 if (distanceFromWhite < distanceFromBlack) {
268 return textColor.dark();
271 return textColor.light();
274 bool InlineTextBox::nodeAtPoint(RenderObject::NodeInfo& i, int x, int y, int tx, int ty)
276 if (object()->isBR())
279 QRect rect(tx + m_x, ty + m_y, m_width, m_height);
280 if (m_truncation != cFullTruncation &&
281 object()->style()->visibility() == VISIBLE && rect.contains(x, y)) {
282 object()->setInnerNode(i);
288 void InlineTextBox::paint(RenderObject::PaintInfo& i, int tx, int ty)
290 if (object()->isBR() || !object()->shouldPaintWithinRoot(i) || object()->style()->visibility() != VISIBLE ||
291 m_truncation == cFullTruncation || i.phase == PaintActionOutline)
296 if ((xPos >= i.r.x() + i.r.width()) || (xPos + w <= i.r.x()))
299 bool isPrinting = (i.p->device()->devType() == QInternal::Printer);
301 // Determine whether or not we're selected.
302 bool haveSelection = !isPrinting && selectionState() != RenderObject::SelectionNone;
303 if (!haveSelection && i.phase == PaintActionSelection)
304 // When only painting the selection, don't bother to paint if there is none.
307 // Determine whether or not we have marked text.
308 RangeImpl *markedTextRange = KWQ(object()->document()->part())->markedTextRange();
310 bool haveMarkedText = markedTextRange && markedTextRange->startContainer(exception) == object()->node();
311 bool markedTextUsesUnderlines = KWQ(object()->document()->part())->markedTextUsesUnderlines();
314 RenderStyle* styleToUse = object()->style(m_firstLine);
315 int d = styleToUse->textDecorationsInEffect();
316 if (styleToUse->font() != i.p->font())
317 i.p->setFont(styleToUse->font());
318 const Font *font = &styleToUse->htmlFont();
320 // 1. Paint backgrounds behind text if needed. Examples of such backgrounds include selection
322 if ((haveSelection || haveMarkedText) && !markedTextUsesUnderlines && i.phase != PaintActionSelection && !isPrinting) {
324 paintMarkedTextBackground(i.p, tx, ty, styleToUse, font, markedTextRange->startOffset(exception), markedTextRange->endOffset(exception));
327 paintSelection(i.p, tx, ty, styleToUse, font);
330 // 2. Now paint the foreground, including text and decorations like underline/overline (in quirks mode only).
331 if (m_len <= 0) return;
332 QValueList<DocumentMarker> markers = object()->document()->markersForNode(object()->node());
333 QValueListIterator <DocumentMarker> markerIt = markers.begin();
335 QValueList<KWQKHTMLPart::MarkedTextUnderline> underlines;
336 if (haveMarkedText && markedTextUsesUnderlines) {
337 underlines = KWQ(object()->document()->part())->markedTextUnderlines();
339 QValueListIterator<KWQKHTMLPart::MarkedTextUnderline> underlineIt = underlines.begin();
341 QColor textColor = styleToUse->color();
343 // Make the text color legible against a white background
344 if (styleToUse->forceBackgroundsToWhite())
345 textColor = correctedTextColor(textColor, Qt::white);
347 if (textColor != i.p->pen().color())
348 i.p->setPen(textColor);
350 // Set a text shadow if we have one.
351 // FIXME: Support multiple shadow effects. Need more from the CG API before
353 bool setShadow = false;
354 if (styleToUse->textShadow()) {
355 i.p->setShadow(styleToUse->textShadow()->x, styleToUse->textShadow()->y,
356 styleToUse->textShadow()->blur, styleToUse->textShadow()->color);
360 bool paintSelectedTextOnly = (i.phase == PaintActionSelection);
361 bool paintSelectedTextSeparately = false; // Whether or not we have to do multiple paints. Only
362 // necessary when a custom ::selection foreground color is applied.
363 QColor selectionColor = i.p->pen().color();
364 ShadowData* selectionTextShadow = 0;
366 RenderStyle* pseudoStyle = object()->getPseudoStyle(RenderStyle::SELECTION);
368 if (pseudoStyle->color() != selectionColor || pseudoStyle->textShadow()) {
369 if (!paintSelectedTextOnly)
370 paintSelectedTextSeparately = true;
371 if (pseudoStyle->color() != selectionColor)
372 selectionColor = pseudoStyle->color();
373 if (pseudoStyle->textShadow())
374 selectionTextShadow = pseudoStyle->textShadow();
379 if (!paintSelectedTextOnly && !paintSelectedTextSeparately) {
380 // paint all the text
381 // FIXME: Handle RTL direction, handle reversed strings. For now truncation can only be turned on
382 // for non-reversed LTR strings.
383 int endPoint = m_len;
384 if (m_truncation != cNoTruncation)
385 endPoint = m_truncation - m_start;
386 font->drawText(i.p, m_x + tx, m_y + ty + m_baseline,
387 textObject()->string()->s, textObject()->string()->l, m_start, endPoint,
388 m_toAdd, m_reversed ? QPainter::RTL : QPainter::LTR, styleToUse->visuallyOrdered());
391 selectionStartEnd(sPos, ePos);
392 if (paintSelectedTextSeparately) {
393 // paint only the text that is not selected
395 font->drawText(i.p, m_x + tx, m_y + ty + m_baseline,
396 textObject()->string()->s, textObject()->string()->l, m_start, m_len,
397 m_toAdd, m_reversed ? QPainter::RTL : QPainter::LTR, styleToUse->visuallyOrdered());
400 font->drawText(i.p, m_x + tx, m_y + ty + m_baseline, textObject()->string()->s,
401 textObject()->string()->l, m_start, m_len,
402 m_toAdd, m_reversed ? QPainter::RTL : QPainter::LTR, styleToUse->visuallyOrdered(), 0, sPos);
404 if (ePos < m_start + m_len) {
405 font->drawText(i.p, m_x + tx, m_y + ty + m_baseline, textObject()->string()->s,
406 textObject()->string()->l, m_start, m_len,
407 m_toAdd, m_reversed ? QPainter::RTL : QPainter::LTR, styleToUse->visuallyOrdered(), ePos, -1);
413 // paint only the text that is selected
414 if (selectionColor != i.p->pen().color())
415 i.p->setPen(selectionColor);
417 if (selectionTextShadow)
418 i.p->setShadow(selectionTextShadow->x,
419 selectionTextShadow->y,
420 selectionTextShadow->blur,
421 selectionTextShadow->color);
422 font->drawText(i.p, m_x + tx, m_y + ty + m_baseline, textObject()->string()->s,
423 textObject()->string()->l, m_start, m_len,
424 m_toAdd, m_reversed ? QPainter::RTL : QPainter::LTR, styleToUse->visuallyOrdered(), sPos, ePos);
425 if (selectionTextShadow)
431 if (d != TDNONE && i.phase != PaintActionSelection && styleToUse->htmlHacks()) {
432 i.p->setPen(styleToUse->color());
433 paintDecoration(i.p, tx, ty, d);
436 // Draw any doc markers that touch this run
437 // Note end() points at the last char, not one past it like endOffset and ranges do
438 if (i.phase != PaintActionSelection) {
439 for ( ; markerIt != markers.end(); markerIt++) {
440 DocumentMarker marker = *markerIt;
442 if (marker.endOffset <= start())
443 // marker is completely before this run. This might be a marker that sits before the
444 // first run we draw, or markers that were within runs we skipped due to truncation.
447 if (marker.startOffset <= end()) {
448 // marker intersects this run. Paint it.
449 paintMarker(i.p, tx, ty, marker);
450 if (marker.endOffset > end() + 1)
451 // marker also runs into the next run. Bail now, no more marker advancement.
454 // marker is completely after this run, bail. A later run will paint it.
459 for ( ; underlineIt != underlines.end(); underlineIt++) {
460 KWQKHTMLPart::MarkedTextUnderline underline = *underlineIt;
462 if (underline.endOffset <= start())
463 // underline is completely before this run. This might be an underlinethat sits
464 // before the first run we draw, or underlines that were within runs we skipped
465 // due to truncation.
468 if (underline.startOffset <= end()) {
469 // underline intersects this run. Paint it.
470 paintMarkedTextUnderline(i.p, tx, ty, underline);
471 if (underline.endOffset > end() + 1)
472 // underline also runs into the next run. Bail now, no more marker advancement.
475 // underline is completely after this run, bail. A later run will paint it.
487 void InlineTextBox::selectionStartEnd(int& sPos, int& ePos)
489 int startPos, endPos;
490 if (object()->selectionState() == RenderObject::SelectionInside) {
492 endPos = textObject()->string()->l;
494 textObject()->selectionStartEnd(startPos, endPos);
495 if (object()->selectionState() == RenderObject::SelectionStart)
496 endPos = textObject()->string()->l;
497 else if (object()->selectionState() == RenderObject::SelectionEnd)
501 sPos = kMax(startPos - m_start, 0);
502 ePos = kMin(endPos - m_start, (int)m_len);
505 void InlineTextBox::paintSelection(QPainter* p, int tx, int ty, RenderStyle* style, const Font* f)
507 // See if we have a selection to paint at all.
509 selectionStartEnd(sPos, ePos);
513 // Macintosh-style text highlighting is to draw with a particular background color, not invert.
514 QColor textColor = style->color();
515 QColor c = object()->selectionColor(p);
519 // If the text color ends up being the same as the selection background, invert the selection
520 // background. This should basically never happen, since the selection has transparency.
522 c = QColor(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
525 p->setPen(c); // Don't draw text at all!
526 RootInlineBox* r = root();
528 int y = r->selectionTop();
529 int h = r->selectionHeight();
530 f->drawHighlightForText(p, x, y + ty, h,
531 textObject()->str->s, textObject()->str->l, m_start, m_len,
532 m_toAdd, m_reversed ? QPainter::RTL : QPainter::LTR, style->visuallyOrdered(), sPos, ePos, c);
536 void InlineTextBox::paintMarkedTextBackground(QPainter* p, int tx, int ty, RenderStyle* style, const Font* f, int startPos, int endPos)
538 int offset = m_start;
539 int sPos = kMax(startPos - offset, 0);
540 int ePos = kMin(endPos - offset, (int)m_len);
547 QColor c = QColor(225, 221, 85);
549 p->setPen(c); // Don't draw text at all!
551 RootInlineBox* r = root();
553 int y = r->selectionTop();
554 int h = r->selectionHeight();
555 f->drawHighlightForText(p, x, y + ty, h, textObject()->str->s, textObject()->str->l, m_start, m_len,
556 m_toAdd, m_reversed ? QPainter::RTL : QPainter::LTR, style->visuallyOrdered(), sPos, ePos, c);
560 void InlineTextBox::paintDecoration( QPainter *pt, int _tx, int _ty, int deco)
565 if (m_truncation == cFullTruncation)
568 int width = (m_truncation == cNoTruncation) ?
569 m_width : static_cast<RenderText*>(m_object)->width(m_start, m_truncation - m_start, m_firstLine);
571 // Get the text decoration colors.
572 QColor underline, overline, linethrough;
573 object()->getTextDecorationColors(deco, underline, overline, linethrough, true);
575 // Use a special function for underlines to get the positioning exactly right.
576 if (deco & UNDERLINE) {
577 pt->setPen(underline);
578 pt->drawLineForText(_tx, _ty, m_baseline, width);
580 if (deco & OVERLINE) {
581 pt->setPen(overline);
582 pt->drawLineForText(_tx, _ty, 0, width);
584 if (deco & LINE_THROUGH) {
585 pt->setPen(linethrough);
586 pt->drawLineForText(_tx, _ty, 2*m_baseline/3, width);
590 void InlineTextBox::paintMarker(QPainter *pt, int _tx, int _ty, DocumentMarker marker)
595 if (m_truncation == cFullTruncation)
598 int start = 0; // start of line to draw, relative to _tx
599 int width = m_width; // how much line to draw
600 bool useWholeWidth = true;
601 ulong paintStart = m_start;
602 ulong paintEnd = end()+1; // end points at the last char, not past it
603 if (paintStart <= marker.startOffset) {
604 paintStart = marker.startOffset;
605 useWholeWidth = false;
606 start = static_cast<RenderText*>(m_object)->width(m_start, paintStart - m_start, m_firstLine);
608 if (paintEnd != marker.endOffset) { // end points at the last char, not past it
609 paintEnd = kMin(paintEnd, marker.endOffset);
610 useWholeWidth = false;
612 if (m_truncation != cNoTruncation) {
613 paintEnd = kMin(paintEnd, (ulong)m_truncation);
614 useWholeWidth = false;
616 if (!useWholeWidth) {
617 width = static_cast<RenderText*>(m_object)->width(paintStart, paintEnd - paintStart, m_firstLine);
620 // IMPORTANT: The misspelling underline is not considered when calculating the text bounds, so we have to
621 // make sure to fit within those bounds. This means the top pixel(s) of the underline will overlap the
622 // bottom pixel(s) of the glyphs in smaller font sizes. The alternatives are to increase the line spacing (bad!!)
623 // or decrease the underline thickness. The overlap is actually the most useful, and matches what AppKit does.
624 // So, we generally place the underline at the bottom of the text, but in larger fonts that's not so good so
625 // we pin to two pixels under the baseline.
626 int lineThickness = pt->misspellingLineThickness();
627 int descent = m_height - m_baseline;
629 if (descent <= (2 + lineThickness)) {
630 // place the underline at the very bottom of the text in small/medium fonts
631 underlineOffset = m_height - lineThickness;
633 // in larger fonts, tho, place the underline up near the baseline to prevent big gap
634 underlineOffset = m_baseline + 2;
636 pt->drawLineForMisspelling(_tx + start, _ty + underlineOffset, width);
639 void InlineTextBox::paintMarkedTextUnderline(QPainter *pt, int _tx, int _ty, KWQKHTMLPart::MarkedTextUnderline underline)
644 if (m_truncation == cFullTruncation)
647 int start = 0; // start of line to draw, relative to _tx
648 int width = m_width; // how much line to draw
649 bool useWholeWidth = true;
650 ulong paintStart = m_start;
651 ulong paintEnd = end()+1; // end points at the last char, not past it
652 if (paintStart <= underline.startOffset) {
653 paintStart = underline.startOffset;
654 useWholeWidth = false;
655 start = static_cast<RenderText*>(m_object)->width(m_start, paintStart - m_start, m_firstLine);
657 if (paintEnd != underline.endOffset) { // end points at the last char, not past it
658 paintEnd = kMin(paintEnd, (ulong)underline.endOffset);
659 useWholeWidth = false;
661 if (m_truncation != cNoTruncation) {
662 paintEnd = kMin(paintEnd, (ulong)m_truncation);
663 useWholeWidth = false;
665 if (!useWholeWidth) {
666 width = static_cast<RenderText*>(m_object)->width(paintStart, paintEnd - paintStart, m_firstLine);
669 int underlineOffset = m_height - 3;
670 pt->setPen(QPen(underline.color, underline.thick ? 2 : 0));
671 pt->drawLineForText(_tx + start, _ty, underlineOffset, width);
674 long InlineTextBox::caretMinOffset() const
679 long InlineTextBox::caretMaxOffset() const
681 return m_start + m_len;
684 unsigned long InlineTextBox::caretMaxRenderedOffset() const
686 return m_start + m_len;
689 static UBreakIterator *getCharacterBreakIterator(const DOMStringImpl *i)
691 // The locale is currently ignored when determining character cluster breaks. This may change
692 // in the future (according to Deborah Goldsmith).
693 static bool createdIterator = false;
694 static UBreakIterator *iterator;
696 if (!createdIterator) {
697 status = U_ZERO_ERROR;
698 iterator = ubrk_open(UBRK_CHARACTER, "en_us", NULL, 0, &status);
699 createdIterator = true;
704 status = U_ZERO_ERROR;
705 ubrk_setText(iterator, reinterpret_cast<const UChar *>(i->s), i->l, &status);
706 if (status != U_ZERO_ERROR) {
712 long RenderText::previousOffset (long current) const
714 UBreakIterator *iterator = getCharacterBreakIterator(str);
716 return ubrk_preceding(iterator, current);
721 long RenderText::nextOffset (long current) const
723 UBreakIterator *iterator = getCharacterBreakIterator(str);
725 return ubrk_following(iterator, current);
730 int InlineTextBox::offsetForPosition(int _x, bool includePartialGlyphs) const
732 RenderText* text = static_cast<RenderText*>(m_object);
733 const Font* f = text->htmlFont(m_firstLine);
734 return f->checkSelectionPoint(text->str->s, text->str->l, m_start, m_len, m_toAdd, _x - m_x, m_reversed, includePartialGlyphs);
737 int InlineTextBox::positionForOffset(int offset) const
739 RenderText *text = static_cast<RenderText *>(m_object);
740 const QFontMetrics &fm = text->metrics(m_firstLine);
744 long len = m_start + m_len - offset;
745 QString string(text->str->s + offset, len);
746 left = m_x + fm.boundingRect(string, len).right();
748 long len = offset - m_start;
749 QString string(text->str->s + m_start, len);
750 left = m_x + fm.boundingRect(string, len).right();
752 // FIXME: Do we need to add rightBearing here?
756 // -------------------------------------------------------------------------------------
758 RenderText::RenderText(DOM::NodeImpl* node, DOMStringImpl *_str)
759 : RenderObject(node), m_linesDirty(false)
761 // init RenderObject attributes
762 setRenderText(); // our object inherits from RenderText
768 m_monospaceCharacterWidth = 0;
769 m_allAsciiChecked = false;
775 str = str->replace('\\', backslashAsCurrencySymbol());
778 KHTMLAssert(!str || !str->l || str->s);
780 m_firstTextBox = m_lastTextBox = 0;
782 m_selectionState = SelectionNone;
785 QConstString cstr(str->s, str->l);
786 kdDebug( 6040 ) << "RenderText ctr( "<< cstr.string().length() << " ) '" << cstr.string() << "'" << endl;
790 void RenderText::setStyle(RenderStyle *_style)
792 if ( style() != _style ) {
793 bool needToTransformText = (!style() && _style->textTransform() != TTNONE) ||
794 (style() && style()->textTransform() != _style->textTransform());
796 RenderObject::setStyle( _style );
798 if (needToTransformText) {
799 DOM::DOMStringImpl* textToTransform = originalString();
801 setText(textToTransform, true);
804 // setText also calls cacheWidths(), so there is no need to call it again in that case.
811 RenderText::~RenderText()
813 if(str) str->deref();
816 void RenderText::detach()
818 if (!documentBeingDestroyed()) {
819 if (firstTextBox()) {
821 RootInlineBox* next = firstTextBox()->root()->nextRootBox();
825 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
829 parent()->dirtyLinesFromChangedChild(this);
832 RenderObject::detach();
835 void RenderText::extractTextBox(InlineTextBox* box)
837 m_lastTextBox = box->prevTextBox();
838 if (box == m_firstTextBox)
840 if (box->prevTextBox())
841 box->prevTextBox()->setNextLineBox(0);
842 box->setPreviousLineBox(0);
843 for (InlineRunBox* curr = box; curr; curr = curr->nextLineBox())
844 curr->setExtracted();
847 void RenderText::attachTextBox(InlineTextBox* box)
850 m_lastTextBox->setNextLineBox(box);
851 box->setPreviousLineBox(m_lastTextBox);
854 m_firstTextBox = box;
855 InlineTextBox* last = box;
856 for (InlineTextBox* curr = box; curr; curr = curr->nextTextBox()) {
857 curr->setExtracted(false);
860 m_lastTextBox = last;
863 void RenderText::removeTextBox(InlineTextBox* box)
865 if (box == m_firstTextBox)
866 m_firstTextBox = box->nextTextBox();
867 if (box == m_lastTextBox)
868 m_lastTextBox = box->prevTextBox();
869 if (box->nextTextBox())
870 box->nextTextBox()->setPreviousLineBox(box->prevTextBox());
871 if (box->prevTextBox())
872 box->prevTextBox()->setNextLineBox(box->nextTextBox());
875 void RenderText::deleteTextBoxes()
877 if (firstTextBox()) {
878 RenderArena* arena = renderArena();
879 InlineTextBox *curr = firstTextBox(), *next = 0;
881 next = curr->nextTextBox();
885 m_firstTextBox = m_lastTextBox = 0;
889 bool RenderText::isTextFragment() const
894 DOM::DOMStringImpl* RenderText::originalString() const
896 return element() ? element()->string() : 0;
899 void RenderText::absoluteRects(QValueList<QRect>& rects, int _tx, int _ty)
901 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
902 rects.append(QRect(_tx + box->xPos(),
908 InlineTextBox* RenderText::findNextInlineTextBox(int offset, int &pos) const
910 // The text runs point to parts of the rendertext's str string
911 // (they don't include '\n')
912 // Find the text run that includes the character at @p offset
913 // and return pos, which is the position of the char in the run.
918 InlineTextBox* s = m_firstTextBox;
920 while (offset > off && s->nextTextBox())
922 s = s->nextTextBox();
923 off = s->m_start + s->m_len;
925 // we are now in the correct text run
926 pos = (offset > off ? s->m_len : s->m_len - (off - offset) );
930 VisiblePosition RenderText::positionForCoordinates(int _x, int _y)
932 if (!firstTextBox() || stringLength() == 0)
933 return VisiblePosition(element(), 0, DOWNSTREAM);
936 containingBlock()->absolutePosition(absx, absy);
938 if (firstTextBox() && _y < absy + firstTextBox()->root()->bottomOverflow() && _x < absx + firstTextBox()->m_x) {
939 // at the y coordinate of the first line or above
940 // and the x coordinate is to the left than the first text box left edge
941 return VisiblePosition(element(), firstTextBox()->m_start, DOWNSTREAM);
944 if (lastTextBox() && _y >= absy + lastTextBox()->root()->topOverflow() && _x >= absx + lastTextBox()->m_x + lastTextBox()->m_width) {
945 // at the y coordinate of the last line or below
946 // and the x coordinate is to the right than the last text box right edge
947 return VisiblePosition(element(), lastTextBox()->m_start + lastTextBox()->m_len, DOWNSTREAM);
950 for (InlineTextBox *box = firstTextBox(); box; box = box->nextTextBox()) {
951 if (_y >= absy + box->root()->topOverflow() && _y < absy + box->root()->bottomOverflow()) {
952 if (_x < absx + box->m_x + box->m_width) {
953 // and the x coordinate is to the left of the right edge of this box
954 // check to see if position goes in this box
955 int offset = box->offsetForPosition(_x - absx);
957 EAffinity affinity = offset >= box->m_len && !box->nextOnLine() ? UPSTREAM : DOWNSTREAM;
958 VisiblePosition result = VisiblePosition(element(), offset + box->m_start, affinity);
959 setAffinityUsingLinePosition(result);
963 else if (!box->prevOnLine() && _x < absx + box->m_x) {
964 // box is first on line
965 // and the x coordinate is to the left of the first text box left edge
966 return VisiblePosition(element(), box->m_start, DOWNSTREAM);
968 else if (!box->nextOnLine() && _x >= absx + box->m_x + box->m_width) {
969 // box is last on line
970 // and the x coordinate is to the right of the last text box right edge
971 VisiblePosition result = VisiblePosition(element(), box->m_start + box->m_len, UPSTREAM);
972 setAffinityUsingLinePosition(result);
978 return VisiblePosition(element(), 0, DOWNSTREAM);
981 static RenderObject *firstRendererOnNextLine(InlineBox *box)
986 RootInlineBox *root = box->root();
990 if (root->endsWithBreak())
993 RootInlineBox *nextRoot = root->nextRootBox();
997 InlineBox *firstChild = nextRoot->firstChild();
1001 return firstChild->object();
1004 static RenderObject *lastRendererOnPrevLine(InlineBox *box)
1009 RootInlineBox *root = box->root();
1013 if (root->endsWithBreak())
1016 RootInlineBox *prevRoot = root->prevRootBox();
1020 InlineBox *lastChild = prevRoot->lastChild();
1024 return lastChild->object();
1027 QRect RenderText::caretRect(int offset, EAffinity affinity, int *extraWidthToEndOfLine)
1029 if (!firstTextBox() || stringLength() == 0) {
1033 // Find the text box for the given offset
1034 InlineTextBox *box = 0;
1035 for (box = firstTextBox(); box; box = box->nextTextBox()) {
1036 if ((offset >= box->m_start) && (offset <= box->m_start + box->m_len)) {
1037 // Check if downstream affinity would make us move to the next line.
1038 InlineTextBox *nextBox = box->nextTextBox();
1039 if (offset == box->m_start + box->m_len && affinity == DOWNSTREAM && nextBox && !box->nextOnLine()) {
1040 // We're at the end of a line broken on a word boundary and affinity is downstream.
1041 // Try to jump down to the next line.
1043 // Use the next text box
1045 offset = box->m_start;
1047 // Look on the next line
1048 RenderObject *object = firstRendererOnNextLine(box);
1050 return object->caretRect(0, affinity);
1053 InlineTextBox *prevBox = box->prevTextBox();
1054 if (offset == box->m_start && affinity == UPSTREAM && prevBox && !box->prevOnLine()) {
1057 offset = box->m_start + box->m_len;
1059 RenderObject *object = lastRendererOnPrevLine(box);
1061 return object->caretRect(0, affinity);
1073 int height = box->root()->bottomOverflow() - box->root()->topOverflow();
1074 int top = box->root()->topOverflow();
1076 int left = box->positionForOffset(offset);
1078 // FIXME: should we use the width of the root inline box or the
1079 // width of the containing block for this?
1080 if (extraWidthToEndOfLine)
1081 *extraWidthToEndOfLine = (box->root()->width() + box->root()->xPos()) - (left + 1);
1084 absolutePosition(absx,absy);
1088 // FIXME: Need the +1 to match caret position of other programs on Macintosh.
1089 // Would be better to somehow derive it once we understand exactly why it's needed.
1092 RenderBlock *cb = containingBlock();
1093 int availableWidth = cb->lineWidth(top);
1094 if (style()->whiteSpace() == NORMAL)
1095 left = kMin(left, absx + box->m_x + availableWidth - 1);
1097 return QRect(left, top, 1, height);
1100 void RenderText::posOfChar(int chr, int &x, int &y)
1102 absolutePosition( x, y, false );
1104 //if( chr > (int) str->l )
1108 InlineTextBox * s = findNextInlineTextBox( chr, pos );
1112 // s is the line containing the character
1113 x += s->m_x; // this is the x of the beginning of the line, but it's good enough for now
1118 #ifdef APPLE_CHANGES
1120 bool RenderText::allAscii() const
1122 if (m_allAsciiChecked)
1124 m_allAsciiChecked = true;
1127 for (i = 0; i < str->l; i++){
1128 if (str->s[i].unicode() >= 0x7f){
1139 bool RenderText::shouldUseMonospaceCache(const Font *f) const
1141 return (f && f->isFixedPitch() && allAscii() && !style()->htmlFont().isSmallCaps());
1144 // We cache the width of the ' ' character for <pre> text. We could go futher
1145 // and cache a widths array for all styles, at the expense of increasing the size of the
1147 void RenderText::cacheWidths()
1149 const Font *f = htmlFont( false );
1151 if (shouldUseMonospaceCache(f)){
1154 f->floatCharacterWidths( &c, 1, 0, 1, 0, &fw);
1155 m_monospaceCharacterWidth = (int)fw;
1158 m_monospaceCharacterWidth = 0;
1162 inline int RenderText::widthFromCache(const Font *f, int start, int len) const
1164 if (m_monospaceCharacterWidth != 0){
1166 for (i = start; i < start+len; i++){
1167 int dir = str->s[i].direction();
1168 if (dir != QChar::DirNSM && dir != QChar::DirBN)
1169 w += m_monospaceCharacterWidth;
1174 return f->width(str->s, str->l, start, len);
1177 inline int RenderText::widthFromCache(const Font *f, int start, int len) const
1179 if (m_monospaceCharacterWidth != 0){
1180 return len * m_monospaceCharacterWidth;
1183 return f->width(str->s, str->l, start, len);
1189 void RenderText::trimmedMinMaxWidth(int& beginMinW, bool& beginWS,
1190 int& endMinW, bool& endWS,
1191 bool& hasBreakableChar, bool& hasBreak,
1192 int& beginMaxW, int& endMaxW,
1193 int& minW, int& maxW, bool& stripFrontSpaces)
1195 bool isPre = style()->whiteSpace() == PRE;
1197 stripFrontSpaces = false;
1200 if (len == 0 || (stripFrontSpaces && str->containsOnlyWhitespace())) {
1208 beginWS = stripFrontSpaces ? false : m_hasBeginWS;
1211 beginMinW = m_beginMinWidth;
1212 endMinW = m_endMinWidth;
1214 hasBreakableChar = m_hasBreakableChar;
1215 hasBreak = m_hasBreak;
1217 if (stripFrontSpaces && (str->s[0] == ' ' || (!isPre && str->s[0] == '\n'))) {
1218 const Font *f = htmlFont( false );
1219 QChar space[1]; space[0] = ' ';
1220 int spaceWidth = f->width(space, 1, 0);
1224 stripFrontSpaces = !isPre && m_hasEndWS;
1226 if (style()->whiteSpace() == NOWRAP)
1228 else if (minW > maxW)
1231 // Compute our max widths by scanning the string for newlines.
1233 const Font *f = htmlFont( false );
1234 bool firstLine = true;
1235 beginMaxW = endMaxW = maxW;
1236 for (int i = 0; i < len; i++)
1239 while (i+linelen < len && str->s[i+linelen] != '\n')
1245 endMaxW = f->width(str->s, str->l, i, linelen);
1247 endMaxW = widthFromCache(f, i, linelen);
1251 beginMaxW = endMaxW;
1255 else if (firstLine) {
1261 // A <pre> run that ends with a newline, as in, e.g.,
1262 // <pre>Some text\n\n<span>More text</pre>
1268 void RenderText::calcMinMaxWidth()
1270 KHTMLAssert( !minMaxKnown() );
1272 // ### calc Min and Max width...
1273 m_minWidth = m_beginMinWidth = m_endMinWidth = 0;
1279 int currMinWidth = 0;
1280 int currMaxWidth = 0;
1281 m_hasBreakableChar = m_hasBreak = m_hasBeginWS = m_hasEndWS = false;
1283 // ### not 100% correct for first-line
1284 const Font *f = htmlFont( false );
1285 int wordSpacing = style()->wordSpacing();
1287 bool ignoringSpaces = false;
1288 bool isSpace = false;
1289 bool isPre = style()->whiteSpace() == PRE;
1290 bool firstWord = true;
1291 bool firstLine = true;
1292 for(int i = 0; i < len; i++)
1294 const QChar c = str->s[i];
1296 bool previousCharacterIsSpace = isSpace;
1298 bool isNewline = false;
1311 if ((isSpace || isNewline) && i == 0)
1312 m_hasBeginWS = true;
1313 if ((isSpace || isNewline) && i == len-1)
1316 if (!ignoringSpaces && !isPre && previousCharacterIsSpace && isSpace)
1317 ignoringSpaces = true;
1319 if (ignoringSpaces && !isSpace)
1320 ignoringSpaces = false;
1322 if (ignoringSpaces || (i > 0 && c.unicode() == SOFT_HYPHEN)) // Ignore spaces and soft hyphens
1326 while (i+wordlen < len && str->s[i+wordlen] != '\n' && str->s[i+wordlen] != ' ' &&
1327 (i+wordlen == 0 || str->s[i+wordlen].unicode() != SOFT_HYPHEN) && // Skip soft hyphens
1328 (wordlen == 0 || !isBreakable( str->s, i+wordlen, str->l)))
1334 int w = f->width(str->s, str->l, i, wordlen);
1336 int w = widthFromCache(f, i, wordlen);
1341 bool isBreakableCharSpace = (i+wordlen < len) ? ((!isPre && str->s[i+wordlen] == '\n') ||
1342 str->s[i+wordlen] == ' ') : false;
1344 if (i+wordlen < len && style()->whiteSpace() == NORMAL)
1345 m_hasBreakableChar = true;
1347 // Add in wordspacing to our maxwidth, but not if this is the last word on a line or the
1348 // last word in the run.
1349 if (wordSpacing && isBreakableCharSpace && !containsOnlyWhitespace(i+wordlen, len-(i+wordlen)))
1350 currMaxWidth += wordSpacing;
1354 // If the first character in the run is breakable, then we consider ourselves to have a beginning
1355 // minimum width of 0, since a break could occur right before our run starts, preventing us from ever
1356 // being appended to a previous text run when considering the total minimum width of the containing block.
1357 bool hasBreak = isBreakable(str->s, i, str->l);
1359 m_hasBreakableChar = true;
1360 m_beginMinWidth = hasBreak ? 0 : w;
1364 if (currMinWidth > m_minWidth) m_minWidth = currMinWidth;
1370 // Nowrap can never be broken, so don't bother setting the
1371 // breakable character boolean. Pre can only be broken if we encounter a newline.
1372 if (style()->whiteSpace() == NORMAL || isNewline)
1373 m_hasBreakableChar = true;
1375 if (currMinWidth > m_minWidth) m_minWidth = currMinWidth;
1378 if (isNewline) // Only set if isPre was true and we saw a newline.
1382 m_beginMinWidth = currMaxWidth;
1385 if (currMaxWidth > m_maxWidth) m_maxWidth = currMaxWidth;
1390 currMaxWidth += f->width( str->s, str->l, i + wordlen );
1395 if(currMinWidth > m_minWidth) m_minWidth = currMinWidth;
1396 if(currMaxWidth > m_maxWidth) m_maxWidth = currMaxWidth;
1398 if (style()->whiteSpace() != NORMAL)
1399 m_minWidth = m_maxWidth;
1403 m_beginMinWidth = m_maxWidth;
1404 m_endMinWidth = currMaxWidth;
1408 //kdDebug( 6040 ) << "Text::calcMinMaxWidth(): min = " << m_minWidth << " max = " << m_maxWidth << endl;
1411 bool RenderText::containsOnlyWhitespace(unsigned int from, unsigned int len) const
1413 unsigned int currPos;
1414 for (currPos = from;
1415 currPos < from+len && (str->s[currPos] == '\n' || str->s[currPos].unicode() == ' ');
1417 return currPos >= (from+len);
1420 int RenderText::minXPos() const
1422 if (!m_firstTextBox) return 0;
1424 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
1425 retval = kMin(retval, (int)box->m_x);
1429 int RenderText::xPos() const
1431 return m_firstTextBox ? m_firstTextBox->m_x : 0;
1434 int RenderText::yPos() const
1436 return m_firstTextBox ? m_firstTextBox->m_y : 0;
1439 const QFont &RenderText::font()
1441 return style()->font();
1444 void RenderText::setSelectionState(SelectionState s)
1448 m_selectionState = s;
1449 if (s == SelectionStart || s == SelectionEnd || s == SelectionBoth) {
1450 int startPos, endPos;
1451 selectionStartEnd(startPos, endPos);
1452 if(selectionState() == SelectionStart) {
1455 // to handle selection from end of text to end of line
1456 if (startPos != 0 && startPos == endPos) {
1457 startPos = endPos - 1;
1459 } else if(selectionState() == SelectionEnd)
1462 for (box = firstTextBox(); box; box = box->nextTextBox()) {
1463 if (box->isSelected(startPos, endPos)) {
1464 RootInlineBox* line = box->root();
1466 line->setHasSelectedChildren(true);
1471 for (box = firstTextBox(); box; box = box->nextTextBox()) {
1472 RootInlineBox* line = box->root();
1474 line->setHasSelectedChildren(s == SelectionInside);
1478 containingBlock()->setSelectionState(s);
1481 void RenderText::setTextWithOffset(DOMStringImpl *text, uint offset, uint len, bool force)
1483 uint oldLen = str ? str->l : 0;
1484 uint newLen = text ? text->l : 0;
1485 int delta = newLen - oldLen;
1486 uint end = len ? offset+len-1 : offset;
1488 RootInlineBox* firstRootBox = 0;
1489 RootInlineBox* lastRootBox = 0;
1491 bool dirtiedLines = false;
1493 // Dirty all text boxes that include characters in between offset and offset+len.
1494 for (InlineTextBox* curr = firstTextBox(); curr; curr = curr->nextTextBox()) {
1495 // Text run is entirely before the affected range.
1496 if (curr->end() < offset)
1499 // Text run is entirely after the affected range.
1500 if (curr->start() > end) {
1501 curr->offsetRun(delta);
1502 RootInlineBox* root = curr->root();
1503 if (!firstRootBox) {
1504 firstRootBox = root;
1505 if (!dirtiedLines) { // The affected area was in between two runs. Go ahead and mark the root box of the run after the affected area as dirty.
1506 firstRootBox->markDirty();
1507 dirtiedLines = true;
1512 else if (curr->end() >= offset && curr->end() <= end) {
1513 curr->dirtyLineBoxes(); // Text run overlaps with the left end of the affected range.
1514 dirtiedLines = true;
1516 else if (curr->start() <= offset && curr->end() >= end) {
1517 curr->dirtyLineBoxes(); // Text run subsumes the affected range.
1518 dirtiedLines = true;
1520 else if (curr->start() <= end && curr->end() >= end) {
1521 curr->dirtyLineBoxes(); // Text run overlaps with right end of the affected range.
1522 dirtiedLines = true;
1526 // Now we have to walk all of the clean lines and adjust their cached line break information
1527 // to reflect our updated offsets.
1529 lastRootBox = lastRootBox->nextRootBox();
1531 RootInlineBox* prev = firstRootBox->prevRootBox();
1533 firstRootBox = prev;
1535 for (RootInlineBox* curr = firstRootBox; curr && curr != lastRootBox; curr = curr->nextRootBox()) {
1536 if (curr->lineBreakObj() == this && curr->lineBreakPos() > end)
1537 curr->setLineBreakPos(curr->lineBreakPos()+delta);
1540 m_linesDirty = dirtiedLines;
1541 setText(text, force);
1544 void RenderText::setText(DOMStringImpl *text, bool force)
1548 if (!force && str == text)
1553 #ifdef APPLE_CHANGES
1554 m_allAsciiChecked = false;
1559 str = str->replace('\\', backslashAsCurrencySymbol());
1561 switch(style()->textTransform()) {
1562 case CAPITALIZE: str = str->capitalize(); break;
1563 case UPPERCASE: str = str->upper(); break;
1564 case LOWERCASE: str = str->lower(); break;
1576 // ### what should happen if we change the text of a
1577 // RenderBR object ?
1578 KHTMLAssert(!isBR() || (str->l == 1 && (*str->s) == '\n'));
1579 KHTMLAssert(!str->l || str->s);
1581 setNeedsLayoutAndMinMaxRecalc();
1584 QConstString cstr(str->s, str->l);
1585 kdDebug( 6040 ) << "RenderText::setText( " << cstr.string().length() << " ) '" << cstr.string() << "'" << endl;
1589 int RenderText::height() const
1591 // FIXME: Why use line-height? Shouldn't we be adding in the height of the last text box? -dwh
1594 retval = lastTextBox()->m_y + lineHeight(false) - firstTextBox()->m_y;
1598 short RenderText::lineHeight(bool firstLine, bool) const
1600 // Always use the interior line height of the parent (e.g., if our parent is an inline block).
1601 return parent()->lineHeight(firstLine, true);
1604 short RenderText::baselinePosition( bool firstLine, bool ) const
1606 const QFontMetrics &fm = metrics( firstLine );
1607 return fm.ascent() +
1608 ( lineHeight( firstLine ) - fm.height() ) / 2;
1611 void RenderText::dirtyLineBoxes(bool fullLayout, bool)
1615 else if (!m_linesDirty) {
1616 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
1617 box->dirtyLineBoxes();
1619 m_linesDirty = false;
1622 InlineBox* RenderText::createInlineBox(bool, bool isRootLineBox, bool)
1624 KHTMLAssert(!isRootLineBox);
1625 InlineTextBox* textBox = new (renderArena()) InlineTextBox(this);
1626 if (!m_firstTextBox)
1627 m_firstTextBox = m_lastTextBox = textBox;
1629 m_lastTextBox->setNextLineBox(textBox);
1630 textBox->setPreviousLineBox(m_lastTextBox);
1631 m_lastTextBox = textBox;
1636 void RenderText::position(InlineBox* box, int from, int len, bool reverse)
1638 InlineTextBox *s = static_cast<InlineTextBox*>(box);
1640 // ### should not be needed!!!
1642 // We want the box to be destroyed.
1644 s->detach(renderArena());
1645 m_firstTextBox = m_lastTextBox = 0;
1649 reverse = reverse && !style()->visuallyOrdered();
1652 QChar *ch = str->s+from;
1653 QConstString cstr(ch, len);
1654 qDebug("setting run text to *%s*, len=%d, w)=%d" , cstr.string().latin1(), len, width );//" << y << ")" << " height=" << lineHeight(false) << " fontHeight=" << metrics(false).height() << " ascent =" << metrics(false).ascent() << endl;
1657 s->m_reversed = reverse;
1662 unsigned int RenderText::width(unsigned int from, unsigned int len, bool firstLine) const
1664 if(!str->s || from > str->l ) return 0;
1665 if ( from + len > str->l ) len = str->l - from;
1667 const Font *f = htmlFont( firstLine );
1668 return width( from, len, f );
1671 unsigned int RenderText::width(unsigned int from, unsigned int len, const Font *f) const
1673 if(!str->s || from > str->l ) return 0;
1674 if ( from + len > str->l ) len = str->l - from;
1677 if ( f == &style()->htmlFont() && from == 0 && len == str->l )
1680 else if (f == &style()->htmlFont())
1681 w = widthFromCache (f, from, len);
1684 w = f->width(str->s, str->l, from, len );
1686 //kdDebug( 6040 ) << "RenderText::width(" << from << ", " << len << ") = " << w << endl;
1690 int RenderText::width() const
1693 int minx = 100000000;
1696 for (InlineTextBox* s = firstTextBox(); s; s = s->nextTextBox()) {
1699 if(s->m_x + s->m_width > maxx)
1700 maxx = s->m_x + s->m_width;
1703 w = kMax(0, maxx-minx);
1708 QRect RenderText::getAbsoluteRepaintRect()
1710 RenderObject *cb = containingBlock();
1711 return cb->getAbsoluteRepaintRect();
1714 QRect RenderText::selectionRect()
1717 if (selectionState() == SelectionNone)
1719 RenderBlock* cb = containingBlock();
1723 // Now calculate startPos and endPos for painting selection.
1724 // We include a selection while endPos > 0
1725 int startPos, endPos;
1726 if (selectionState() == SelectionInside) {
1727 // We are fully selected.
1731 selectionStartEnd(startPos, endPos);
1732 if (selectionState() == SelectionStart)
1734 else if (selectionState() == SelectionEnd)
1738 if (startPos == endPos)
1742 cb->absolutePosition(absx, absy);
1743 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
1744 QRect r = box->selectionRect(absx, absy, startPos, endPos);
1749 rect = rect.unite(r);
1756 short RenderText::verticalPositionHint( bool firstLine ) const
1758 return parent()->verticalPositionHint( firstLine );
1761 const QFontMetrics &RenderText::metrics(bool firstLine) const
1763 return style(firstLine)->fontMetrics();
1766 const Font *RenderText::htmlFont(bool firstLine) const
1768 return &style(firstLine)->htmlFont();
1771 long RenderText::caretMinOffset() const
1773 InlineTextBox *box = firstTextBox();
1776 int minOffset = box->m_start;
1777 for (box = box->nextTextBox(); box; box = box->nextTextBox())
1778 minOffset = kMin(minOffset, box->m_start);
1782 long RenderText::caretMaxOffset() const
1784 InlineTextBox* box = lastTextBox();
1787 int maxOffset = box->m_start + box->m_len;
1788 for (box = box->prevTextBox(); box; box = box->prevTextBox())
1789 maxOffset = kMax(maxOffset,box->m_start + box->m_len);
1793 unsigned long RenderText::caretMaxRenderedOffset() const
1796 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
1801 InlineBox *RenderText::inlineBox(long offset, EAffinity affinity)
1803 for (InlineTextBox *box = firstTextBox(); box; box = box->nextTextBox()) {
1804 if (offset >= box->m_start && offset <= box->m_start + box->m_len) {
1805 if (affinity == DOWNSTREAM && box->nextTextBox() && offset == box->m_start + box->m_len)
1806 return box->nextTextBox();
1809 else if (offset < box->m_start) {
1810 // The offset we're looking for is before this node
1811 // this means the offset must be in content that is
1813 return box->prevTextBox() ? box->prevTextBox() : firstTextBox();
1820 RenderTextFragment::RenderTextFragment(DOM::NodeImpl* _node, DOM::DOMStringImpl* _str,
1821 int startOffset, int endOffset)
1822 :RenderText(_node, _str->substring(startOffset, endOffset)),
1823 m_start(startOffset), m_end(endOffset), m_generatedContentStr(0)
1826 RenderTextFragment::RenderTextFragment(DOM::NodeImpl* _node, DOM::DOMStringImpl* _str)
1827 :RenderText(_node, _str), m_start(0)
1829 m_generatedContentStr = _str;
1838 RenderTextFragment::~RenderTextFragment()
1840 if (m_generatedContentStr)
1841 m_generatedContentStr->deref();
1844 bool RenderTextFragment::isTextFragment() const
1849 DOM::DOMStringImpl* RenderTextFragment::originalString() const
1851 DOM::DOMStringImpl* result = 0;
1853 result = element()->string();
1855 result = contentString();
1856 if (result && (start() > 0 || start() < result->l))
1857 result = result->substring(start(), end());