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_canvas.h"
28 #include "rendering/render_object.h"
29 #include "rendering/render_text.h"
30 #include "rendering/break_lines.h"
31 #include "dom/dom2_range.h"
32 #include "xml/dom_nodeimpl.h"
33 #include "xml/dom_docimpl.h"
34 #include "xml/dom_position.h"
35 #include "render_arena.h"
37 #include "misc/loader.h"
39 #include "khtml_part.h"
45 using namespace khtml;
49 static bool inInlineTextBoxDetach;
52 void InlineTextBox::detach(RenderArena* renderArena)
55 inInlineTextBoxDetach = true;
59 inInlineTextBoxDetach = false;
62 // Recover the size left there for us by operator delete and free the memory.
63 renderArena->free(*(size_t *)this, this);
66 void* InlineTextBox::operator new(size_t sz, RenderArena* renderArena) throw()
68 return renderArena->allocate(sz);
71 void InlineTextBox::operator delete(void* ptr, size_t sz)
73 assert(inInlineTextBoxDetach);
75 // Stash size where detach can find it.
79 RenderText* InlineTextBox::textObject()
81 return static_cast<RenderText*>(m_object);
84 bool InlineTextBox::checkVerticalPoint(int _y, int _ty, int _h)
87 int bottomY = m_y + m_height;
88 if (root()->hasSelectedChildren()) {
89 topY = kMin(root()->selectionTop(), topY);
90 bottomY = kMax(bottomY, root()->bottomOverflow());
92 if ((_ty + topY >= _y + _h) || (_ty + bottomY <= _y))
97 bool InlineTextBox::isSelected(int startPos, int endPos) const
99 int sPos = kMax(startPos - m_start, 0);
100 int ePos = kMin(endPos - m_start, (int)m_len);
101 return (sPos < ePos);
104 RenderObject::SelectionState InlineTextBox::selectionState()
106 RenderObject::SelectionState state = object()->selectionState();
107 if (state == RenderObject::SelectionStart || state == RenderObject::SelectionEnd ||
108 state == RenderObject::SelectionBoth) {
109 int startPos, endPos;
110 object()->selectionStartEnd(startPos, endPos);
112 bool start = (state != RenderObject::SelectionEnd && startPos >= m_start && startPos < m_start + m_len);
113 bool end = (state != RenderObject::SelectionStart && endPos > m_start && endPos <= m_start + m_len);
115 state = RenderObject::SelectionBoth;
117 state = RenderObject::SelectionStart;
119 state = RenderObject::SelectionEnd;
120 else if ((state == RenderObject::SelectionEnd || startPos < m_start) &&
121 (state == RenderObject::SelectionStart || endPos > m_start + m_len))
122 state = RenderObject::SelectionInside;
127 QRect InlineTextBox::selectionRect(int tx, int ty, int startPos, int endPos)
129 int sPos = kMax(startPos - m_start, 0);
130 int ePos = kMin(endPos - m_start, (int)m_len);
135 RootInlineBox* rootBox = root();
136 int selStart = m_reversed ? m_x + m_width : m_x;
137 int selEnd = selStart;
138 int selTop = rootBox->selectionTop();
139 int selHeight = rootBox->selectionHeight();
141 // FIXME: For justified text, just return the entire text box's rect. At the moment there's still no easy
142 // way to get the width of a run including the justification padding.
143 if (sPos > 0 && !m_toAdd) {
144 // The selection begins in the middle of our run.
145 int w = textObject()->width(m_start, sPos, m_firstLine);
152 if (m_toAdd || (sPos == 0 && ePos == m_len)) {
156 selEnd = m_x + m_width;
159 // Our run is partially selected, and so we have to actually do a measurement.
160 int w = textObject()->width(sPos + m_start, ePos - sPos, m_firstLine);
162 selEnd = selStart - w;
164 selEnd = selStart + w;
167 int selLeft = m_reversed ? selEnd : selStart;
168 int selRight = m_reversed ? selStart : selEnd;
170 return QRect(selLeft + tx, selTop + ty, selRight - selLeft, selHeight);
173 void InlineTextBox::deleteLine(RenderArena* arena)
175 static_cast<RenderText*>(m_object)->removeTextBox(this);
179 void InlineTextBox::extractLine()
184 static_cast<RenderText*>(m_object)->extractTextBox(this);
187 void InlineTextBox::attachLine()
192 static_cast<RenderText*>(m_object)->attachTextBox(this);
195 int InlineTextBox::placeEllipsisBox(bool ltr, int blockEdge, int ellipsisWidth, bool& foundBox)
198 m_truncation = cFullTruncation;
202 int ellipsisX = ltr ? blockEdge - ellipsisWidth : blockEdge + ellipsisWidth;
204 // 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.
206 if (ellipsisX <= m_x) {
207 // Too far. Just set full truncation, but return -1 and let the ellipsis just be placed at the edge of the box.
208 m_truncation = cFullTruncation;
213 if (ellipsisX < m_x + m_width) {
215 return -1; // FIXME: Support LTR truncation when the last run is RTL someday.
219 int offset = offsetForPosition(ellipsisX, false);
221 // No characters should be rendered. Set ourselves to full truncation and place the ellipsis at the min of our start
222 // and the ellipsis edge.
223 m_truncation = cFullTruncation;
224 return kMin(ellipsisX, m_x);
227 // Set the truncation index on the text run. The ellipsis needs to be placed just after the last visible character.
228 m_truncation = offset + m_start;
229 return m_x + static_cast<RenderText*>(m_object)->width(m_start, offset, m_firstLine);
233 // FIXME: Support RTL truncation someday, including both modes (when the leftmost run on the line is either RTL or LTR)
238 void InlineTextBox::paintSelection(const Font *f, RenderText *text, QPainter *p, RenderStyle* style, int tx, int ty, int startPos, int endPos)
240 int offset = m_start;
241 int sPos = kMax(startPos - offset, 0);
242 int ePos = kMin(endPos - offset, (int)m_len);
247 // Macintosh-style text highlighting is to draw with a particular background color, not invert.
248 QColor textColor = style->color();
249 QColor c = object()->selectionColor(p);
253 // If the text color ends up being the same as the selection background, invert the selection
254 // background. This should basically never happen, since the selection has transparency.
256 c = QColor(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
259 p->setPen(c); // Don't draw text at all!
260 RootInlineBox* r = root();
262 int y = r->selectionTop();
263 int h = r->selectionHeight();
264 f->drawHighlightForText(p, x, y + ty, h,
265 text->str->s, text->str->l, m_start, m_len,
266 m_toAdd, m_reversed ? QPainter::RTL : QPainter::LTR, style->visuallyOrdered(), sPos, ePos, c);
271 void InlineTextBox::paintMarkedTextBackground(const Font *f, RenderText *text, QPainter *p, RenderStyle* style, int tx, int ty, int startPos, int endPos)
273 int offset = m_start;
274 int sPos = kMax(startPos - offset, 0);
275 int ePos = kMin(endPos - offset, (int)m_len);
282 QColor c = QColor(225, 221, 85);
284 p->setPen(c); // Don't draw text at all!
286 RootInlineBox* r = root();
288 int y = r->selectionTop();
289 int h = r->selectionHeight();
290 f->drawHighlightForText(p, x, y + ty, h, text->str->s, text->str->l, m_start, m_len,
291 m_toAdd, m_reversed ? QPainter::RTL : QPainter::LTR, style->visuallyOrdered(), sPos, ePos, c);
297 void InlineTextBox::paintDecoration( QPainter *pt, int _tx, int _ty, int deco)
302 if (m_truncation == cFullTruncation)
305 int width = (m_truncation == cNoTruncation) ?
306 m_width : static_cast<RenderText*>(m_object)->width(m_start, m_truncation - m_start, m_firstLine);
308 // Get the text decoration colors.
309 QColor underline, overline, linethrough;
310 object()->getTextDecorationColors(deco, underline, overline, linethrough, true);
312 // Use a special function for underlines to get the positioning exactly right.
313 if (deco & UNDERLINE) {
314 pt->setPen(underline);
315 pt->drawLineForText(_tx, _ty, m_baseline, width);
317 if (deco & OVERLINE) {
318 pt->setPen(overline);
319 pt->drawLineForText(_tx, _ty, 0, width);
321 if (deco & LINE_THROUGH) {
322 pt->setPen(linethrough);
323 pt->drawLineForText(_tx, _ty, 2*m_baseline/3, width);
327 void InlineTextBox::paintDecoration( QPainter *pt, int _tx, int _ty, int decoration)
332 int width = m_width - 1;
334 QColor underline, overline, linethrough;
335 object()->getTextDecorationColors(decoration, underline, overline, linethrough, true);
337 int underlineOffset = ( pt->fontMetrics().height() + m_baseline ) / 2;
338 if(underlineOffset <= m_baseline) underlineOffset = m_baseline+1;
340 if(deco & UNDERLINE){
341 pt->setPen(underline);
342 pt->drawLine(_tx, _ty + underlineOffset, _tx + width, _ty + underlineOffset );
344 if (deco & OVERLINE) {
345 pt->setPen(overline);
346 pt->drawLine(_tx, _ty, _tx + width, _ty );
348 if(deco & LINE_THROUGH) {
349 pt->setPen(linethrough);
350 pt->drawLine(_tx, _ty + 2*m_baseline/3, _tx + width, _ty + 2*m_baseline/3 );
352 // NO! Do NOT add BLINK! It is the most annouing feature of Netscape, and IE has a reason not to
357 void InlineTextBox::paintMarker(QPainter *pt, int _tx, int _ty, DocumentMarker marker)
362 if (m_truncation == cFullTruncation)
365 int start = 0; // start of line to draw, relative to _tx
366 int width = m_width; // how much line to draw
367 bool useWholeWidth = true;
368 ulong paintStart = m_start;
369 ulong paintEnd = end()+1; // end points at the last char, not past it
370 if (paintStart <= marker.startOffset) {
371 paintStart = marker.startOffset;
372 useWholeWidth = false;
373 start = static_cast<RenderText*>(m_object)->width(m_start, paintStart - m_start, m_firstLine);
375 if (paintEnd != marker.endOffset) { // end points at the last char, not past it
376 paintEnd = kMin(paintEnd, marker.endOffset);
377 useWholeWidth = false;
379 if (m_truncation != cNoTruncation) {
380 paintEnd = kMin(paintEnd, (ulong)m_truncation);
381 useWholeWidth = false;
383 if (!useWholeWidth) {
384 width = static_cast<RenderText*>(m_object)->width(paintStart, paintEnd - paintStart, m_firstLine);
387 // AppKit uses a more complicated formula to figure the offset from the baseline for the misspelling
388 // underline. Using "+2" made the marker draw outside the selection rect in Times-16, using "+1"
389 // leaves it a pixel too high with Times-24. The former is the lesser evil.
390 int underlineOffset = m_baseline + 1;
391 pt->drawLineForMisspelling(_tx + start, _ty + underlineOffset, width);
394 long InlineTextBox::caretMinOffset() const
399 long InlineTextBox::caretMaxOffset() const
401 return m_start + m_len;
404 unsigned long InlineTextBox::caretMaxRenderedOffset() const
406 return m_start + m_len;
409 #define LOCAL_WIDTH_BUF_SIZE 1024
411 int InlineTextBox::offsetForPosition(int _x, bool includePartialGlyphs)
413 RenderText* text = static_cast<RenderText*>(m_object);
414 const Font* f = text->htmlFont(m_firstLine);
415 return f->checkSelectionPoint(text->str->s, text->str->l, m_start, m_len, m_toAdd, _x - m_x, m_reversed, includePartialGlyphs);
418 // -------------------------------------------------------------------------------------
420 RenderText::RenderText(DOM::NodeImpl* node, DOMStringImpl *_str)
421 : RenderObject(node), m_linesDirty(false)
423 // init RenderObject attributes
424 setRenderText(); // our object inherits from RenderText
430 m_monospaceCharacterWidth = 0;
431 m_allAsciiChecked = false;
437 str = str->replace('\\', backslashAsCurrencySymbol());
440 KHTMLAssert(!str || !str->l || str->s);
442 m_firstTextBox = m_lastTextBox = 0;
444 m_selectionState = SelectionNone;
447 QConstString cstr(str->s, str->l);
448 kdDebug( 6040 ) << "RenderText ctr( "<< cstr.string().length() << " ) '" << cstr.string() << "'" << endl;
452 void RenderText::setStyle(RenderStyle *_style)
454 if ( style() != _style ) {
455 bool needToTransformText = (!style() && _style->textTransform() != TTNONE) ||
456 (style() && style()->textTransform() != _style->textTransform());
458 RenderObject::setStyle( _style );
460 if (needToTransformText) {
461 DOM::DOMStringImpl* textToTransform = originalString();
463 setText(textToTransform, true);
466 // setText also calls cacheWidths(), so there is no need to call it again in that case.
473 RenderText::~RenderText()
475 if(str) str->deref();
478 void RenderText::detach()
480 if (!documentBeingDestroyed()) {
481 if (firstTextBox()) {
483 RootInlineBox* next = firstTextBox()->root()->nextRootBox();
487 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
491 parent()->dirtyLinesFromChangedChild(this, false);
494 RenderObject::detach();
497 void RenderText::extractTextBox(InlineTextBox* box)
499 m_lastTextBox = box->prevTextBox();
500 if (box == m_firstTextBox)
502 if (box->prevTextBox())
503 box->prevTextBox()->setNextLineBox(0);
504 box->setPreviousLineBox(0);
505 for (InlineRunBox* curr = box; curr; curr = curr->nextLineBox())
506 curr->setExtracted();
509 void RenderText::attachTextBox(InlineTextBox* box)
512 m_lastTextBox->setNextLineBox(box);
513 box->setPreviousLineBox(m_lastTextBox);
516 m_firstTextBox = box;
517 InlineTextBox* last = box;
518 for (InlineTextBox* curr = box; curr; curr = curr->nextTextBox()) {
519 curr->setExtracted(false);
522 m_lastTextBox = last;
525 void RenderText::removeTextBox(InlineTextBox* box)
527 if (box == m_firstTextBox)
528 m_firstTextBox = box->nextTextBox();
529 if (box == m_lastTextBox)
530 m_lastTextBox = box->prevTextBox();
531 if (box->nextTextBox())
532 box->nextTextBox()->setPreviousLineBox(box->prevTextBox());
533 if (box->prevTextBox())
534 box->prevTextBox()->setNextLineBox(box->nextTextBox());
537 void RenderText::deleteTextBoxes()
539 if (firstTextBox()) {
540 RenderArena* arena = renderArena();
541 InlineTextBox *curr = firstTextBox(), *next = 0;
543 next = curr->nextTextBox();
547 m_firstTextBox = m_lastTextBox = 0;
551 bool RenderText::isTextFragment() const
556 DOM::DOMStringImpl* RenderText::originalString() const
558 return element() ? element()->string() : 0;
561 void RenderText::absoluteRects(QValueList<QRect>& rects, int _tx, int _ty)
563 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
564 rects.append(QRect(_tx + box->xPos(),
570 InlineTextBox* RenderText::findNextInlineTextBox(int offset, int &pos)
572 // The text runs point to parts of the rendertext's str string
573 // (they don't include '\n')
574 // Find the text run that includes the character at @p offset
575 // and return pos, which is the position of the char in the run.
580 InlineTextBox* s = m_firstTextBox;
582 while (offset > off && s->nextTextBox())
584 s = s->nextTextBox();
585 off = s->m_start + s->m_len;
587 // we are now in the correct text run
588 pos = (offset > off ? s->m_len : s->m_len - (off - offset) );
592 bool RenderText::nodeAtPoint(NodeInfo& info, int _x, int _y, int _tx, int _ty,
593 HitTestAction hitTestAction, bool inside)
597 if (style()->visibility() == HIDDEN)
600 for (InlineTextBox *s = m_firstTextBox; s; s = s->nextTextBox()) {
601 if((_y >=_ty + s->m_y) && (_y < _ty + s->m_y + s->height()) &&
602 (_x >= _tx + s->m_x) && (_x <_tx + s->m_x + s->m_width) ) {
608 if (inside && element()) {
609 if (info.innerNode() && info.innerNode()->renderer() &&
610 !info.innerNode()->renderer()->isInline()) {
611 // Within the same layer, inlines are ALWAYS fully above blocks. Change inner node.
612 info.setInnerNode(element());
614 // Clear everything else.
615 info.setInnerNonSharedNode(0);
616 info.setURLElement(0);
619 if (!info.innerNode())
620 info.setInnerNode(element());
622 if (!info.innerNonSharedNode())
623 info.setInnerNonSharedNode(element());
629 Position RenderText::positionForCoordinates(int _x, int _y, EAffinity *affinity)
632 *affinity = UPSTREAM;
634 if (!firstTextBox() || stringLength() == 0)
635 return Position(element(), 0);
638 containingBlock()->absolutePosition(absx, absy);
640 if (firstTextBox() && _y < absy + firstTextBox()->root()->bottomOverflow() && _x < absx + firstTextBox()->m_x) {
641 // at the y coordinate of the first line or above
642 // and the x coordinate is to the left than the first text box left edge
644 *affinity = DOWNSTREAM;
645 return Position(element(), firstTextBox()->m_start);
648 if (lastTextBox() && _y >= absy + lastTextBox()->root()->topOverflow() && _x >= absx + lastTextBox()->m_x + lastTextBox()->m_width) {
649 // at the y coordinate of the last line or below
650 // and the x coordinate is to the right than the last text box right edge
651 return Position(element(), lastTextBox()->m_start + lastTextBox()->m_len);
654 for (InlineTextBox *box = firstTextBox(); box; box = box->nextTextBox()) {
655 if (_y >= absy + box->root()->topOverflow() && _y < absy + box->root()->bottomOverflow()) {
656 if (_x < absx + box->m_x + box->m_width) {
657 // and the x coordinate is to the left of the right edge of this box
658 // check to see if position goes in this box
659 int offset = box->offsetForPosition(_x - absx);
662 *affinity = DOWNSTREAM;
663 return Position(element(), offset + box->m_start);
666 else if (!box->prevOnLine() && _x < absx + box->m_x) {
667 // box is first on line
668 // and the x coordinate is to the left of the first text box left edge
670 *affinity = DOWNSTREAM;
671 return Position(element(), box->m_start);
673 else if (!box->nextOnLine() && _x >= absx + box->m_x + box->m_width)
674 // box is last on line
675 // and the x coordinate is to the right of the last text box right edge
676 return Position(element(), box->m_start + box->m_len);
680 return Position(element(), 0);
683 static RenderObject *firstRendererOnNextLine(InlineBox *box)
688 RootInlineBox *root = box->root();
692 if (root->endsWithBreak())
695 RootInlineBox *nextRoot = root->nextRootBox();
699 InlineBox *firstChild = nextRoot->firstChild();
703 return firstChild->object();
706 QRect RenderText::caretRect(int offset, EAffinity affinity)
708 if (!firstTextBox() || stringLength() == 0) {
712 // Find the text box for the given offset
713 InlineTextBox *box = 0;
714 for (box = firstTextBox(); box; box = box->nextTextBox()) {
715 if (affinity == DOWNSTREAM && offset == box->m_start + box->m_len) {
716 // We're at the end of a line and affinity is downstream.
717 // Try to jump down to the next line.
718 if (box->nextTextBox()) {
719 // Use the next text box
720 box = box->nextTextBox();
721 offset = box->m_start;
724 // Look on the next line
725 RenderObject *object = firstRendererOnNextLine(box);
727 return object->caretRect(0, affinity);
730 if (offset <= box->m_start + box->m_len)
738 int height = box->root()->bottomOverflow() - box->root()->topOverflow();
739 int top = box->root()->topOverflow();
741 const QFontMetrics &fm = metrics(box->isFirstLineStyle());
742 QString string(str->s + box->m_start, box->m_len);
743 long pos = offset - box->m_start; // the number of characters we are into the string
744 int left = box->m_x + fm.boundingRect(string, pos).right();
749 left += fm.rightBearing(*(str->s + box->m_start + offset));
753 absolutePosition(absx,absy);
757 // FIXME: Need the +1 to match caret position of other programs on Macintosh.
758 // Would be better to somehow derive it once we understand exactly why it's needed.
761 RenderBlock *cb = containingBlock();
762 int availableWidth = cb->lineWidth(height);
763 if (style()->whiteSpace() == NORMAL)
764 left = kMin(left, absx + availableWidth - 1);
766 return QRect(left, top, 1, height);
769 void RenderText::posOfChar(int chr, int &x, int &y)
771 absolutePosition( x, y, false );
773 //if( chr > (int) str->l )
777 InlineTextBox * s = findNextInlineTextBox( chr, pos );
781 // s is the line containing the character
782 x += s->m_x; // this is the x of the beginning of the line, but it's good enough for now
788 simpleDifferenceBetweenColors(QColor c1, QColor c2)
790 // a distance could be computed by squaring the differences between components, but
791 // this is faster and so far seems good enough for our purposes.
792 return abs(c1.red() - c2.red()) + abs(c1.green() - c2.green()) + abs(c1.blue() - c2.blue());
796 correctedTextColor(QColor textColor, QColor backgroundColor)
798 // Adjust the text color if it is too close to the background color,
799 // by darkening or lightening it to move it further away.
801 int d = simpleDifferenceBetweenColors(textColor, backgroundColor);
802 // semi-arbitrarily chose 255 value here after a few tests;
807 int distanceFromWhite = simpleDifferenceBetweenColors(textColor, Qt::white);
808 int distanceFromBlack = simpleDifferenceBetweenColors(textColor, Qt::black);
810 if (distanceFromWhite < distanceFromBlack) {
811 return textColor.dark();
814 return textColor.light();
817 void RenderText::paint(PaintInfo& i, int tx, int ty)
819 if (i.phase != PaintActionForeground && i.phase != PaintActionSelection)
822 if (!shouldPaintWithinRoot(i))
825 if (style()->visibility() != VISIBLE || !firstTextBox())
828 // Selection paints all the way up to the previous line's bottomOverflow. We need to check
829 // for this when doing our dirty rect intersection tests.
830 int topY = firstTextBox()->yPos();
831 if (firstTextBox()->root()->hasSelectedChildren())
832 topY = kMin(topY, firstTextBox()->root()->selectionTop());
833 int bottomY = lastTextBox()->yPos() + lastTextBox()->height();
834 if (lastTextBox()->root()->hasSelectedChildren())
835 bottomY = kMax(bottomY, lastTextBox()->root()->bottomOverflow());
837 if (ty + topY >= i.r.y() + i.r.height())
840 if (ty + bottomY <= i.r.y())
844 RenderStyle* pseudoStyle = style(true);
845 if (pseudoStyle == style()) pseudoStyle = 0;
846 int d = style()->textDecorationsInEffect();
847 bool isPrinting = (p->device()->devType() == QInternal::Printer);
849 // Walk forward until we hit the first line that needs to be painted.
850 InlineTextBox* s = firstTextBox();
851 for (; s && !s->checkVerticalPoint(i.r.y(), ty, i.r.height()); s = s->nextTextBox());
854 // Now calculate startPos and endPos, for painting selection.
855 // We paint selection while endPos > 0
856 int endPos, startPos;
857 if (!isPrinting && (selectionState() != SelectionNone)) {
858 if (selectionState() == SelectionInside) {
859 //kdDebug(6040) << this << " SelectionInside -> 0 to end" << endl;
863 selectionStartEnd(startPos, endPos);
864 if(selectionState() == SelectionStart)
866 else if(selectionState() == SelectionEnd)
869 //kdDebug(6040) << this << " Selection from " << startPos << " to " << endPos << endl;
872 const Font *font = &style()->htmlFont();
875 // We will iterate over the text boxes in two passes. First we
876 // paint all the backgrounds, then the foregrounds.
878 InlineTextBox* startBox = s;
880 // Pass 1: paint backgrounds for selection or marked text, if we have any
882 bool haveSelection = startPos != endPos && !isPrinting && selectionState() != SelectionNone;
883 if (!haveSelection && i.phase == PaintActionSelection) {
884 // When only painting the selection, don't bother to paint if there is none.
888 Range markedTextRange = KWQ(document()->part())->markedTextRange();
889 bool haveMarkedText = markedTextRange.handle() != 0 && markedTextRange.startContainer() == node();
891 if ((haveSelection || haveMarkedText) && i.phase != PaintActionSelection && !isPrinting) {
894 // run until we find a text box that is outside the range, then we know we can stop
896 if (s->m_truncation == cFullTruncation)
899 RenderStyle* _style = pseudoStyle && s->m_firstLine ? pseudoStyle : style();
901 if (_style->font() != p->font())
902 p->setFont(_style->font());
904 font = &_style->htmlFont(); // Always update, since smallCaps is not stored in the QFont.
907 s->paintMarkedTextBackground(font, this, p, _style, tx, ty, markedTextRange.startOffset(), markedTextRange.endOffset());
910 s->paintSelection(font, this, p, _style, tx, ty, startPos, endPos);
912 } while (((s = s->nextTextBox()) != 0) && s->checkVerticalPoint(i.r.y(), ty, i.r.height()));
916 // Pass 2: now paint the foreground, including text and decorations
918 QValueList<DocumentMarker> markers = document()->markersForNode(node());
919 QValueListIterator <DocumentMarker> markerIt = markers.begin();
923 // run until we find a text box that is outside the range, then we know we can stop
926 if (ty+s->m_y+s->height() > i.r.y() + i.r.height()) {
927 RenderCanvas* canvasObj = canvas();
928 if (ty+s->m_y < canvasObj->truncatedAt())
929 canvasObj->setBestTruncatedAt(ty+s->m_y, this);
935 if (s->m_truncation == cFullTruncation)
938 RenderStyle* _style = pseudoStyle && s->m_firstLine ? pseudoStyle : style();
940 if (_style->font() != p->font())
941 p->setFont(_style->font());
943 font = &_style->htmlFont(); // Always update, since smallCaps is not stored in the QFont.
945 QColor textColor = _style->color();
946 if (_style->shouldCorrectTextColor()) {
947 textColor = correctedTextColor(textColor, _style->backgroundColor());
950 if(textColor != p->pen().color())
951 p->setPen(textColor);
953 // Set a text shadow if we have one.
954 // FIXME: Support multiple shadow effects. Need more from the CG API before
956 bool setShadow = false;
957 if (_style->textShadow()) {
958 p->setShadow(_style->textShadow()->x, _style->textShadow()->y,
959 _style->textShadow()->blur, _style->textShadow()->color);
964 bool paintSelectedTextOnly = (i.phase == PaintActionSelection);
965 bool paintSelectedTextSeparately = false; // Whether or not we have to do multiple paints. Only
966 // necessary when a custom ::selection foreground color is applied.
967 QColor selectionColor = p->pen().color();
968 ShadowData* selectionTextShadow = 0;
970 RenderStyle* pseudoStyle = getPseudoStyle(RenderStyle::SELECTION);
972 if (pseudoStyle->color() != selectionColor || pseudoStyle->textShadow()) {
973 if (!paintSelectedTextOnly)
974 paintSelectedTextSeparately = true;
975 if (pseudoStyle->color() != selectionColor)
976 selectionColor = pseudoStyle->color();
977 if (pseudoStyle->textShadow())
978 selectionTextShadow = pseudoStyle->textShadow();
983 if (!paintSelectedTextOnly && !paintSelectedTextSeparately) {
984 // paint all the text
986 // FIXME: Handle RTL direction, handle reversed strings. For now truncation can only be turned on
987 // for non-reversed LTR strings.
988 int endPoint = s->m_len;
989 if (s->m_truncation != cNoTruncation)
990 endPoint = s->m_truncation - s->m_start;
991 font->drawText(p, s->m_x + tx, s->m_y + ty + s->m_baseline,
992 str->s, str->l, s->m_start, endPoint,
993 s->m_toAdd, s->m_reversed ? QPainter::RTL : QPainter::LTR, style()->visuallyOrdered());
995 int offset = s->m_start;
996 int sPos = QMAX( startPos - offset, 0 );
997 int ePos = QMIN( endPos - offset, s->m_len );
998 if (paintSelectedTextSeparately) {
999 // paint only the text that is not selected
1002 font->drawText(p, s->m_x + tx, s->m_y + ty + s->m_baseline,
1003 str->s, str->l, s->m_start, s->m_len,
1004 s->m_toAdd, s->m_reversed ? QPainter::RTL : QPainter::LTR, style()->visuallyOrdered());
1007 font->drawText(p, s->m_x + tx, s->m_y + ty + s->m_baseline, str->s,
1008 str->l, s->m_start, s->m_len,
1009 s->m_toAdd, s->m_reversed ? QPainter::RTL : QPainter::LTR, style()->visuallyOrdered(), 0, sPos);
1011 if (ePos < s->m_start+s->m_len) {
1012 font->drawText(p, s->m_x + tx, s->m_y + ty + s->m_baseline, str->s,
1013 str->l, s->m_start, s->m_len,
1014 s->m_toAdd, s->m_reversed ? QPainter::RTL : QPainter::LTR, style()->visuallyOrdered(), ePos, -1);
1019 if ( sPos < ePos ) {
1020 // paint only the text that is selected
1021 if (selectionColor != p->pen().color())
1022 p->setPen(selectionColor);
1024 if (selectionTextShadow)
1025 p->setShadow(selectionTextShadow->x,
1026 selectionTextShadow->y,
1027 selectionTextShadow->blur,
1028 selectionTextShadow->color);
1029 font->drawText(p, s->m_x + tx, s->m_y + ty + s->m_baseline, str->s,
1030 str->l, s->m_start, s->m_len,
1031 s->m_toAdd, s->m_reversed ? QPainter::RTL : QPainter::LTR, style()->visuallyOrdered(), sPos, ePos);
1032 if (selectionTextShadow)
1038 // paint decorations
1040 if (d != TDNONE && i.phase != PaintActionSelection &&
1041 style()->htmlHacks()) {
1042 p->setPen(_style->color());
1043 s->paintDecoration(p, tx, ty, d);
1046 // Draw any doc markers that touch this run
1047 // Note s->end() points at the last char, not one past it like endOffset and ranges do
1049 if (i.phase != PaintActionSelection) {
1050 for ( ; markerIt != markers.end(); markerIt++) {
1051 DocumentMarker marker = *markerIt;
1053 if (marker.endOffset <= s->start()) {
1054 // marker is completely before this run. This might be a marker that sits before the
1055 // first run we draw, or markers that were within runs we skipped due to truncation.
1059 if (marker.startOffset <= s->end()) {
1060 // marker intersects this run. Paint it.
1061 s->paintMarker(p, tx, ty, marker);
1062 if (marker.endOffset > s->end()+1) {
1063 // marker also runs into the next run. Bail now, no more marker advancement.
1067 // marker is completely after this run, bail. A later run will paint it.
1078 int h = lineHeight( false ) + paddingTop() + paddingBottom() + borderTop() + borderBottom();
1079 QColor c2 = QColor("#0000ff");
1080 drawBorder(p, tx, ty, tx+1, ty + h,
1081 RenderObject::BSLeft, c2, c2, SOLID, 1, 1);
1082 drawBorder(p, tx + s->m_width, ty, tx + s->m_width + 1, ty + h,
1083 RenderObject::BSRight, c2, c2, SOLID, 1, 1);
1086 } while (((s = s->nextTextBox()) != 0) && s->checkVerticalPoint(i.r.y(), ty, i.r.height()));
1088 #error This file no longer works without Apple changes
1092 #ifdef APPLE_CHANGES
1094 bool RenderText::allAscii() const
1096 if (m_allAsciiChecked)
1098 m_allAsciiChecked = true;
1101 for (i = 0; i < str->l; i++){
1102 if (str->s[i].unicode() >= 0x7f){
1113 bool RenderText::shouldUseMonospaceCache(const Font *f) const
1115 return (f && f->isFixedPitch() && allAscii() && !style()->htmlFont().isSmallCaps());
1118 // We cache the width of the ' ' character for <pre> text. We could go futher
1119 // and cache a widths array for all styles, at the expense of increasing the size of the
1121 void RenderText::cacheWidths()
1123 const Font *f = htmlFont( false );
1125 if (shouldUseMonospaceCache(f)){
1128 f->floatCharacterWidths( &c, 1, 0, 1, 0, &fw);
1129 m_monospaceCharacterWidth = (int)fw;
1132 m_monospaceCharacterWidth = 0;
1136 inline int RenderText::widthFromCache(const Font *f, int start, int len) const
1138 if (m_monospaceCharacterWidth != 0){
1140 for (i = start; i < start+len; i++){
1141 int dir = str->s[i].direction();
1142 if (dir != QChar::DirNSM && dir != QChar::DirBN)
1143 w += m_monospaceCharacterWidth;
1148 return f->width(str->s, str->l, start, len);
1151 inline int RenderText::widthFromCache(const Font *f, int start, int len) const
1153 if (m_monospaceCharacterWidth != 0){
1154 return len * m_monospaceCharacterWidth;
1157 return f->width(str->s, str->l, start, len);
1163 void RenderText::trimmedMinMaxWidth(int& beginMinW, bool& beginWS,
1164 int& endMinW, bool& endWS,
1165 bool& hasBreakableChar, bool& hasBreak,
1166 int& beginMaxW, int& endMaxW,
1167 int& minW, int& maxW, bool& stripFrontSpaces)
1169 bool isPre = style()->whiteSpace() == PRE;
1171 stripFrontSpaces = false;
1174 if (len == 0 || (stripFrontSpaces && str->containsOnlyWhitespace())) {
1182 beginWS = stripFrontSpaces ? false : m_hasBeginWS;
1185 beginMinW = m_beginMinWidth;
1186 endMinW = m_endMinWidth;
1188 hasBreakableChar = m_hasBreakableChar;
1189 hasBreak = m_hasBreak;
1191 if (stripFrontSpaces && (str->s[0] == ' ' || (!isPre && str->s[0] == '\n'))) {
1192 const Font *f = htmlFont( false );
1193 QChar space[1]; space[0] = ' ';
1194 int spaceWidth = f->width(space, 1, 0);
1198 stripFrontSpaces = !isPre && m_hasEndWS;
1200 if (style()->whiteSpace() == NOWRAP)
1202 else if (minW > maxW)
1205 // Compute our max widths by scanning the string for newlines.
1207 const Font *f = htmlFont( false );
1208 bool firstLine = true;
1209 beginMaxW = endMaxW = maxW;
1210 for (int i = 0; i < len; i++)
1213 while (i+linelen < len && str->s[i+linelen] != '\n')
1219 endMaxW = f->width(str->s, str->l, i, linelen);
1221 endMaxW = widthFromCache(f, i, linelen);
1225 beginMaxW = endMaxW;
1229 else if (firstLine) {
1235 // A <pre> run that ends with a newline, as in, e.g.,
1236 // <pre>Some text\n\n<span>More text</pre>
1242 void RenderText::calcMinMaxWidth()
1244 KHTMLAssert( !minMaxKnown() );
1246 // ### calc Min and Max width...
1247 m_minWidth = m_beginMinWidth = m_endMinWidth = 0;
1253 int currMinWidth = 0;
1254 int currMaxWidth = 0;
1255 m_hasBreakableChar = m_hasBreak = m_hasBeginWS = m_hasEndWS = false;
1257 // ### not 100% correct for first-line
1258 const Font *f = htmlFont( false );
1259 int wordSpacing = style()->wordSpacing();
1261 bool ignoringSpaces = false;
1262 bool isSpace = false;
1263 bool isPre = style()->whiteSpace() == PRE;
1264 bool firstWord = true;
1265 bool firstLine = true;
1266 for(int i = 0; i < len; i++)
1268 const QChar c = str->s[i];
1270 bool previousCharacterIsSpace = isSpace;
1272 bool isNewline = false;
1285 if ((isSpace || isNewline) && i == 0)
1286 m_hasBeginWS = true;
1287 if ((isSpace || isNewline) && i == len-1)
1290 if (!ignoringSpaces && !isPre && previousCharacterIsSpace && isSpace)
1291 ignoringSpaces = true;
1293 if (ignoringSpaces && !isSpace)
1294 ignoringSpaces = false;
1296 if (ignoringSpaces || (i > 0 && c.unicode() == SOFT_HYPHEN)) // Ignore spaces and soft hyphens
1300 while (i+wordlen < len && str->s[i+wordlen] != '\n' && str->s[i+wordlen] != ' ' &&
1301 (i+wordlen == 0 || str->s[i+wordlen].unicode() != SOFT_HYPHEN) && // Skip soft hyphens
1302 (wordlen == 0 || !isBreakable( str->s, i+wordlen, str->l)))
1308 int w = f->width(str->s, str->l, i, wordlen);
1310 int w = widthFromCache(f, i, wordlen);
1315 bool isBreakableCharSpace = (i+wordlen < len) ? ((!isPre && str->s[i+wordlen] == '\n') ||
1316 str->s[i+wordlen] == ' ') : false;
1318 if (i+wordlen < len && style()->whiteSpace() == NORMAL)
1319 m_hasBreakableChar = true;
1321 // Add in wordspacing to our maxwidth, but not if this is the last word on a line or the
1322 // last word in the run.
1323 if (wordSpacing && isBreakableCharSpace && !containsOnlyWhitespace(i+wordlen, len-(i+wordlen)))
1324 currMaxWidth += wordSpacing;
1328 // If the first character in the run is breakable, then we consider ourselves to have a beginning
1329 // minimum width of 0, since a break could occur right before our run starts, preventing us from ever
1330 // being appended to a previous text run when considering the total minimum width of the containing block.
1331 bool hasBreak = isBreakable(str->s, i, str->l);
1333 m_hasBreakableChar = true;
1334 m_beginMinWidth = hasBreak ? 0 : w;
1338 if (currMinWidth > m_minWidth) m_minWidth = currMinWidth;
1344 // Nowrap can never be broken, so don't bother setting the
1345 // breakable character boolean. Pre can only be broken if we encounter a newline.
1346 if (style()->whiteSpace() == NORMAL || isNewline)
1347 m_hasBreakableChar = true;
1349 if (currMinWidth > m_minWidth) m_minWidth = currMinWidth;
1352 if (isNewline) // Only set if isPre was true and we saw a newline.
1356 m_beginMinWidth = currMaxWidth;
1359 if (currMaxWidth > m_maxWidth) m_maxWidth = currMaxWidth;
1364 currMaxWidth += f->width( str->s, str->l, i + wordlen );
1369 if(currMinWidth > m_minWidth) m_minWidth = currMinWidth;
1370 if(currMaxWidth > m_maxWidth) m_maxWidth = currMaxWidth;
1372 if (style()->whiteSpace() != NORMAL)
1373 m_minWidth = m_maxWidth;
1377 m_beginMinWidth = m_maxWidth;
1378 m_endMinWidth = currMaxWidth;
1382 //kdDebug( 6040 ) << "Text::calcMinMaxWidth(): min = " << m_minWidth << " max = " << m_maxWidth << endl;
1385 bool RenderText::containsOnlyWhitespace(unsigned int from, unsigned int len) const
1387 unsigned int currPos;
1388 for (currPos = from;
1389 currPos < from+len && (str->s[currPos] == '\n' || str->s[currPos].unicode() == ' ');
1391 return currPos >= (from+len);
1394 int RenderText::minXPos() const
1396 if (!m_firstTextBox) return 0;
1398 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
1399 retval = kMin(retval, (int)box->m_x);
1403 int RenderText::xPos() const
1405 return m_firstTextBox ? m_firstTextBox->m_x : 0;
1408 int RenderText::yPos() const
1410 return m_firstTextBox ? m_firstTextBox->m_y : 0;
1413 const QFont &RenderText::font()
1415 return style()->font();
1418 void RenderText::setSelectionState(SelectionState s)
1420 m_selectionState = s;
1421 if (s == SelectionStart || s == SelectionEnd || s == SelectionBoth) {
1422 int startPos, endPos;
1423 selectionStartEnd(startPos, endPos);
1424 if(selectionState() == SelectionStart)
1426 else if(selectionState() == SelectionEnd)
1429 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
1430 if (box->isSelected(startPos, endPos)) {
1431 RootInlineBox* line = box->root();
1433 line->setHasSelectedChildren(true);
1438 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
1439 RootInlineBox* line = box->root();
1441 line->setHasSelectedChildren(s == SelectionInside);
1445 containingBlock()->setSelectionState(s);
1448 void RenderText::setTextWithOffset(DOMStringImpl *text, uint offset, uint len, bool force)
1450 uint oldLen = str ? str->l : 0;
1451 uint newLen = text ? text->l : 0;
1452 int delta = newLen - oldLen;
1453 uint end = len ? offset+len-1 : offset;
1455 RootInlineBox* firstRootBox = 0;
1456 RootInlineBox* lastRootBox = 0;
1458 bool dirtiedLines = false;
1460 // Dirty all text boxes that include characters in between offset and offset+len.
1461 for (InlineTextBox* curr = firstTextBox(); curr; curr = curr->nextTextBox()) {
1462 // Text run is entirely before the affected range.
1463 if (curr->end() < offset)
1466 // Text run is entirely after the affected range.
1467 if (curr->start() > end) {
1468 curr->offsetRun(delta);
1469 RootInlineBox* root = curr->root();
1470 if (!firstRootBox) {
1471 firstRootBox = root;
1472 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.
1473 firstRootBox->markDirty();
1474 dirtiedLines = true;
1479 else if (curr->end() >= offset && curr->end() <= end) {
1480 curr->dirtyLineBoxes(); // Text run overlaps with the left end of the affected range.
1481 dirtiedLines = true;
1483 else if (curr->start() <= offset && curr->end() >= end) {
1484 curr->dirtyLineBoxes(); // Text run subsumes the affected range.
1485 dirtiedLines = true;
1487 else if (curr->start() <= end && curr->end() >= end) {
1488 curr->dirtyLineBoxes(); // Text run overlaps with right end of the affected range.
1489 dirtiedLines = true;
1493 // Now we have to walk all of the clean lines and adjust their cached line break information
1494 // to reflect our updated offsets.
1496 lastRootBox = lastRootBox->nextRootBox();
1498 RootInlineBox* prev = firstRootBox->prevRootBox();
1500 firstRootBox = prev;
1502 for (RootInlineBox* curr = firstRootBox; curr && curr != lastRootBox; curr = curr->nextRootBox()) {
1503 if (!curr->isDirty() && curr->lineBreakObj() == this && curr->lineBreakPos() > end)
1504 curr->setLineBreakPos(curr->lineBreakPos()+delta);
1507 m_linesDirty = dirtiedLines;
1508 setText(text, force);
1511 void RenderText::setText(DOMStringImpl *text, bool force)
1515 if (!force && str == text)
1522 str = str->replace('\\', backslashAsCurrencySymbol());
1524 switch(style()->textTransform()) {
1525 case CAPITALIZE: str = str->capitalize(); break;
1526 case UPPERCASE: str = str->upper(); break;
1527 case LOWERCASE: str = str->lower(); break;
1539 // ### what should happen if we change the text of a
1540 // RenderBR object ?
1541 KHTMLAssert(!isBR() || (str->l == 1 && (*str->s) == '\n'));
1542 KHTMLAssert(!str->l || str->s);
1544 setNeedsLayoutAndMinMaxRecalc();
1547 QConstString cstr(str->s, str->l);
1548 kdDebug( 6040 ) << "RenderText::setText( " << cstr.string().length() << " ) '" << cstr.string() << "'" << endl;
1552 int RenderText::height() const
1554 // FIXME: Why use line-height? Shouldn't we be adding in the height of the last text box? -dwh
1557 retval = lastTextBox()->m_y + lineHeight(false) - firstTextBox()->m_y;
1561 short RenderText::lineHeight(bool firstLine, bool) const
1563 // Always use the interior line height of the parent (e.g., if our parent is an inline block).
1564 return parent()->lineHeight(firstLine, true);
1567 short RenderText::baselinePosition( bool firstLine, bool ) const
1569 const QFontMetrics &fm = metrics( firstLine );
1570 return fm.ascent() +
1571 ( lineHeight( firstLine ) - fm.height() ) / 2;
1574 void RenderText::dirtyLineBoxes(bool fullLayout, bool)
1578 else if (!m_linesDirty) {
1579 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
1580 box->dirtyLineBoxes();
1582 m_linesDirty = false;
1585 InlineBox* RenderText::createInlineBox(bool, bool isRootLineBox, bool)
1587 KHTMLAssert(!isRootLineBox);
1588 InlineTextBox* textBox = new (renderArena()) InlineTextBox(this);
1589 if (!m_firstTextBox)
1590 m_firstTextBox = m_lastTextBox = textBox;
1592 m_lastTextBox->setNextLineBox(textBox);
1593 textBox->setPreviousLineBox(m_lastTextBox);
1594 m_lastTextBox = textBox;
1599 void RenderText::position(InlineBox* box, int from, int len, bool reverse)
1601 InlineTextBox *s = static_cast<InlineTextBox*>(box);
1603 // ### should not be needed!!!
1605 // We want the box to be destroyed.
1607 s->detach(renderArena());
1608 m_firstTextBox = m_lastTextBox = 0;
1612 reverse = reverse && !style()->visuallyOrdered();
1615 QChar *ch = str->s+from;
1616 QConstString cstr(ch, len);
1617 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;
1620 s->m_reversed = reverse;
1625 unsigned int RenderText::width(unsigned int from, unsigned int len, bool firstLine) const
1627 if(!str->s || from > str->l ) return 0;
1628 if ( from + len > str->l ) len = str->l - from;
1630 const Font *f = htmlFont( firstLine );
1631 return width( from, len, f );
1634 unsigned int RenderText::width(unsigned int from, unsigned int len, const Font *f) const
1636 if(!str->s || from > str->l ) return 0;
1637 if ( from + len > str->l ) len = str->l - from;
1640 if ( f == &style()->htmlFont() && from == 0 && len == str->l )
1643 else if (f == &style()->htmlFont())
1644 w = widthFromCache (f, from, len);
1647 w = f->width(str->s, str->l, from, len );
1649 //kdDebug( 6040 ) << "RenderText::width(" << from << ", " << len << ") = " << w << endl;
1653 int RenderText::width() const
1656 int minx = 100000000;
1659 for (InlineTextBox* s = firstTextBox(); s; s = s->nextTextBox()) {
1662 if(s->m_x + s->m_width > maxx)
1663 maxx = s->m_x + s->m_width;
1666 w = kMax(0, maxx-minx);
1671 QRect RenderText::getAbsoluteRepaintRect()
1673 RenderObject *cb = containingBlock();
1674 return cb->getAbsoluteRepaintRect();
1677 QRect RenderText::selectionRect()
1680 if (selectionState() == SelectionNone)
1682 RenderBlock* cb = containingBlock();
1686 // Now calculate startPos and endPos for painting selection.
1687 // We include a selection while endPos > 0
1688 int startPos, endPos;
1689 if (selectionState() == SelectionInside) {
1690 // We are fully selected.
1694 selectionStartEnd(startPos, endPos);
1695 if (selectionState() == SelectionStart)
1697 else if (selectionState() == SelectionEnd)
1701 if (startPos == endPos)
1705 cb->absolutePosition(absx, absy);
1706 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox()) {
1707 QRect r = box->selectionRect(absx, absy, startPos, endPos);
1712 rect = rect.unite(r);
1719 short RenderText::verticalPositionHint( bool firstLine ) const
1721 return parent()->verticalPositionHint( firstLine );
1724 const QFontMetrics &RenderText::metrics(bool firstLine) const
1726 return style(firstLine)->fontMetrics();
1729 const Font *RenderText::htmlFont(bool firstLine) const
1731 return &style(firstLine)->htmlFont();
1734 long RenderText::caretMinOffset() const
1736 if (!firstTextBox())
1738 // EDIT FIXME: it is *not* guaranteed that the first run contains the lowest offset
1739 // Either make this a linear search (slow),
1740 // or maintain an index (needs much mem),
1741 // or calculate and store it in bidi.cpp (needs calculation even if not needed)
1742 return firstTextBox()->m_start;
1745 long RenderText::caretMaxOffset() const
1747 if (!firstTextBox())
1749 // EDIT FIXME: it is *not* guaranteed that the last run contains the highest offset
1750 // Either make this a linear search (slow),
1751 // or maintain an index (needs much mem),
1752 // or calculate and store it in bidi.cpp (needs calculation even if not needed)
1753 return lastTextBox()->m_start + lastTextBox()->m_len;
1756 unsigned long RenderText::caretMaxRenderedOffset() const
1759 for (InlineTextBox* box = firstTextBox(); box; box = box->nextTextBox())
1764 InlineBox *RenderText::inlineBox(long offset, EAffinity affinity)
1766 for (InlineTextBox *box = firstTextBox(); box; box = box->nextTextBox()) {
1767 if (offset >= box->m_start && offset <= box->m_start + box->m_len) {
1768 if (affinity == DOWNSTREAM && box->nextTextBox() && offset == box->m_start + box->m_len)
1769 return box->nextTextBox();
1772 else if (offset < box->m_start) {
1773 // The offset we're looking for is before this node
1774 // this means the offset must be in content that is
1776 return box->prevTextBox() ? box->prevTextBox() : firstTextBox();
1783 RenderTextFragment::RenderTextFragment(DOM::NodeImpl* _node, DOM::DOMStringImpl* _str,
1784 int startOffset, int endOffset)
1785 :RenderText(_node, _str->substring(startOffset, endOffset)),
1786 m_start(startOffset), m_end(endOffset), m_generatedContentStr(0)
1789 RenderTextFragment::RenderTextFragment(DOM::NodeImpl* _node, DOM::DOMStringImpl* _str)
1790 :RenderText(_node, _str), m_start(0)
1792 m_generatedContentStr = _str;
1801 RenderTextFragment::~RenderTextFragment()
1803 if (m_generatedContentStr)
1804 m_generatedContentStr->deref();
1807 bool RenderTextFragment::isTextFragment() const
1812 DOM::DOMStringImpl* RenderTextFragment::originalString() const
1814 DOM::DOMStringImpl* result = 0;
1816 result = element()->string();
1818 result = contentString();
1819 if (result && (start() > 0 || start() < result->l))
1820 result = result->substring(start(), end());