2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2004-2014 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "InlineTextBox.h"
26 #include "BreakLines.h"
27 #include "DashArray.h"
29 #include "DocumentMarkerController.h"
31 #include "EllipsisBox.h"
33 #include "GraphicsContext.h"
34 #include "HitTestResult.h"
35 #include "ImageBuffer.h"
36 #include "InlineTextBoxStyle.h"
38 #include "PaintInfo.h"
39 #include "RenderBlock.h"
40 #include "RenderCombineText.h"
41 #include "RenderLineBreak.h"
42 #include "RenderRubyRun.h"
43 #include "RenderRubyText.h"
44 #include "RenderTheme.h"
45 #include "RenderView.h"
46 #include "RenderedDocumentMarker.h"
48 #include "TextDecorationPainter.h"
49 #include "TextPaintStyle.h"
50 #include "TextPainter.h"
52 #include <wtf/text/CString.h>
53 #include <wtf/text/TextStream.h>
57 struct SameSizeAsInlineTextBox : public InlineBox {
58 unsigned variables[1];
59 unsigned short variables2[2];
63 COMPILE_ASSERT(sizeof(InlineTextBox) == sizeof(SameSizeAsInlineTextBox), InlineTextBox_should_stay_small);
65 typedef WTF::HashMap<const InlineTextBox*, LayoutRect> InlineTextBoxOverflowMap;
66 static InlineTextBoxOverflowMap* gTextBoxesWithOverflow;
68 InlineTextBox::~InlineTextBox()
70 if (!knownToHaveNoOverflow() && gTextBoxesWithOverflow)
71 gTextBoxesWithOverflow->remove(this);
74 void InlineTextBox::markDirty(bool dirty)
80 InlineBox::markDirty(dirty);
83 LayoutRect InlineTextBox::logicalOverflowRect() const
85 if (knownToHaveNoOverflow() || !gTextBoxesWithOverflow)
86 return enclosingIntRect(logicalFrameRect());
87 return gTextBoxesWithOverflow->get(this);
90 void InlineTextBox::setLogicalOverflowRect(const LayoutRect& rect)
92 ASSERT(!knownToHaveNoOverflow());
93 if (!gTextBoxesWithOverflow)
94 gTextBoxesWithOverflow = new InlineTextBoxOverflowMap;
95 gTextBoxesWithOverflow->add(this, rect);
98 int InlineTextBox::baselinePosition(FontBaseline baselineType) const
102 if (&parent()->renderer() == renderer().parent())
103 return parent()->baselinePosition(baselineType);
104 return downcast<RenderBoxModelObject>(*renderer().parent()).baselinePosition(baselineType, isFirstLine(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOnContainingLine);
107 LayoutUnit InlineTextBox::lineHeight() const
109 if (!renderer().parent())
111 if (&parent()->renderer() == renderer().parent())
112 return parent()->lineHeight();
113 return downcast<RenderBoxModelObject>(*renderer().parent()).lineHeight(isFirstLine(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOnContainingLine);
116 LayoutUnit InlineTextBox::selectionTop() const
118 return root().selectionTop();
121 LayoutUnit InlineTextBox::selectionBottom() const
123 return root().selectionBottom();
126 LayoutUnit InlineTextBox::selectionHeight() const
128 return root().selectionHeight();
131 bool InlineTextBox::isSelected(unsigned startPos, unsigned endPos) const
133 int sPos = clampedOffset(startPos);
134 int ePos = clampedOffset(endPos);
135 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=160786
136 // We should only be checking if sPos >= ePos here, because those are the
137 // indices used to actually generate the selection rect. Allowing us past this guard
138 // on any other condition creates zero-width selection rects.
139 return sPos < ePos || (startPos == endPos && startPos >= start() && startPos <= (start() + len()));
142 RenderObject::SelectionState InlineTextBox::selectionState()
144 RenderObject::SelectionState state = renderer().selectionState();
145 if (state == RenderObject::SelectionStart || state == RenderObject::SelectionEnd || state == RenderObject::SelectionBoth) {
146 unsigned startPos, endPos;
147 renderer().selectionStartEnd(startPos, endPos);
148 // The position after a hard line break is considered to be past its end.
149 ASSERT(start() + len() >= (isLineBreak() ? 1 : 0));
150 unsigned lastSelectable = start() + len() - (isLineBreak() ? 1 : 0);
152 bool start = (state != RenderObject::SelectionEnd && startPos >= m_start && startPos < m_start + m_len);
153 bool end = (state != RenderObject::SelectionStart && endPos > m_start && endPos <= lastSelectable);
155 state = RenderObject::SelectionBoth;
157 state = RenderObject::SelectionStart;
159 state = RenderObject::SelectionEnd;
160 else if ((state == RenderObject::SelectionEnd || startPos < m_start) &&
161 (state == RenderObject::SelectionStart || endPos > lastSelectable))
162 state = RenderObject::SelectionInside;
163 else if (state == RenderObject::SelectionBoth)
164 state = RenderObject::SelectionNone;
167 // If there are ellipsis following, make sure their selection is updated.
168 if (m_truncation != cNoTruncation && root().ellipsisBox()) {
169 EllipsisBox* ellipsis = root().ellipsisBox();
170 if (state != RenderObject::SelectionNone) {
171 unsigned selectionStart;
172 unsigned selectionEnd;
173 std::tie(selectionStart, selectionEnd) = selectionStartEnd();
174 // The ellipsis should be considered to be selected if the end of
175 // the selection is past the beginning of the truncation and the
176 // beginning of the selection is before or at the beginning of the
178 ellipsis->setSelectionState(selectionEnd >= m_truncation && selectionStart <= m_truncation ?
179 RenderObject::SelectionInside : RenderObject::SelectionNone);
181 ellipsis->setSelectionState(RenderObject::SelectionNone);
187 static const FontCascade& fontToUse(const RenderStyle& style, const RenderText& renderer)
189 if (style.hasTextCombine() && is<RenderCombineText>(renderer)) {
190 const auto& textCombineRenderer = downcast<RenderCombineText>(renderer);
191 if (textCombineRenderer.isCombined())
192 return textCombineRenderer.textCombineFont();
194 return style.fontCascade();
197 LayoutRect InlineTextBox::localSelectionRect(unsigned startPos, unsigned endPos) const
199 unsigned sPos = clampedOffset(startPos);
200 unsigned ePos = clampedOffset(endPos);
202 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=160786
203 // We should only be checking if sPos >= ePos here, because those are the
204 // indices used to actually generate the selection rect. Allowing us past this guard
205 // on any other condition creates zero-width selection rects.
206 if (sPos >= ePos && !(startPos == endPos && startPos >= start() && startPos <= (start() + len())))
209 LayoutUnit selectionTop = this->selectionTop();
210 LayoutUnit selectionHeight = this->selectionHeight();
211 const RenderStyle& lineStyle = this->lineStyle();
212 const FontCascade& font = fontToUse(lineStyle, renderer());
214 String hyphenatedString;
215 bool respectHyphen = ePos == m_len && hasHyphen();
217 hyphenatedString = hyphenatedStringForTextRun(lineStyle);
218 TextRun textRun = constructTextRun(lineStyle, hyphenatedString);
220 LayoutRect selectionRect = LayoutRect(LayoutPoint(logicalLeft(), selectionTop), LayoutSize(m_logicalWidth, selectionHeight));
221 // Avoid computing the font width when the entire line box is selected as an optimization.
222 if (sPos || ePos != m_len)
223 font.adjustSelectionRectForText(textRun, selectionRect, sPos, ePos);
224 IntRect snappedSelectionRect = enclosingIntRect(selectionRect);
225 LayoutUnit logicalWidth = snappedSelectionRect.width();
226 if (snappedSelectionRect.x() > logicalRight())
228 else if (snappedSelectionRect.maxX() > logicalRight())
229 logicalWidth = logicalRight() - snappedSelectionRect.x();
231 LayoutPoint topPoint = isHorizontal() ? LayoutPoint(snappedSelectionRect.x(), selectionTop) : LayoutPoint(selectionTop, snappedSelectionRect.x());
232 LayoutUnit width = isHorizontal() ? logicalWidth : selectionHeight;
233 LayoutUnit height = isHorizontal() ? selectionHeight : logicalWidth;
235 return LayoutRect(topPoint, LayoutSize(width, height));
238 void InlineTextBox::deleteLine()
240 renderer().removeTextBox(*this);
244 void InlineTextBox::extractLine()
249 renderer().extractTextBox(*this);
252 void InlineTextBox::attachLine()
257 renderer().attachTextBox(*this);
260 float InlineTextBox::placeEllipsisBox(bool flowIsLTR, float visibleLeftEdge, float visibleRightEdge, float ellipsisWidth, float &truncatedWidth, bool& foundBox)
263 m_truncation = cFullTruncation;
267 // For LTR this is the left edge of the box, for RTL, the right edge in parent coordinates.
268 float ellipsisX = flowIsLTR ? visibleRightEdge - ellipsisWidth : visibleLeftEdge + ellipsisWidth;
270 // Criteria for full truncation:
271 // LTR: the left edge of the ellipsis is to the left of our text run.
272 // RTL: the right edge of the ellipsis is to the right of our text run.
273 bool ltrFullTruncation = flowIsLTR && ellipsisX <= left();
274 bool rtlFullTruncation = !flowIsLTR && ellipsisX >= left() + logicalWidth();
275 if (ltrFullTruncation || rtlFullTruncation) {
276 // Too far. Just set full truncation, but return -1 and let the ellipsis just be placed at the edge of the box.
277 m_truncation = cFullTruncation;
282 bool ltrEllipsisWithinBox = flowIsLTR && (ellipsisX < right());
283 bool rtlEllipsisWithinBox = !flowIsLTR && (ellipsisX > left());
284 if (ltrEllipsisWithinBox || rtlEllipsisWithinBox) {
287 // The inline box may have different directionality than it's parent. Since truncation
288 // behavior depends both on both the parent and the inline block's directionality, we
289 // must keep track of these separately.
290 bool ltr = isLeftToRightDirection();
291 if (ltr != flowIsLTR) {
292 // Width in pixels of the visible portion of the box, excluding the ellipsis.
293 int visibleBoxWidth = visibleRightEdge - visibleLeftEdge - ellipsisWidth;
294 ellipsisX = ltr ? left() + visibleBoxWidth : right() - visibleBoxWidth;
297 int offset = offsetForPosition(ellipsisX, false);
299 // No characters should be rendered. Set ourselves to full truncation and place the ellipsis at the min of our start
300 // and the ellipsis edge.
301 m_truncation = cFullTruncation;
302 truncatedWidth += ellipsisWidth;
303 return flowIsLTR ? std::min(ellipsisX, x()) : std::max(ellipsisX, right() - ellipsisWidth);
306 // Set the truncation index on the text run.
307 m_truncation = offset;
309 // If we got here that means that we were only partially truncated and we need to return the pixel offset at which
310 // to place the ellipsis.
311 float widthOfVisibleText = renderer().width(m_start, offset, textPos(), isFirstLine());
313 // The ellipsis needs to be placed just after the last visible character.
314 // Where "after" is defined by the flow directionality, not the inline
315 // box directionality.
316 // e.g. In the case of an LTR inline box truncated in an RTL flow then we can
317 // have a situation such as |Hello| -> |...He|
318 truncatedWidth += widthOfVisibleText + ellipsisWidth;
320 return left() + widthOfVisibleText;
322 return right() - widthOfVisibleText - ellipsisWidth;
324 truncatedWidth += logicalWidth();
330 bool InlineTextBox::isLineBreak() const
332 return renderer().style().preserveNewline() && len() == 1 && (*renderer().text())[start()] == '\n';
335 bool InlineTextBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit /* lineTop */, LayoutUnit /*lineBottom*/,
336 HitTestAction /*hitTestAction*/)
338 if (!visibleToHitTesting())
344 if (m_truncation == cFullTruncation)
347 FloatRect rect(locationIncludingFlipping(), size());
348 // Make sure truncated text is ignored while hittesting.
349 if (m_truncation != cNoTruncation) {
350 LayoutUnit widthOfVisibleText = renderer().width(m_start, m_truncation, textPos(), isFirstLine());
353 renderer().style().isLeftToRightDirection() ? rect.setWidth(widthOfVisibleText) : rect.shiftXEdgeTo(right() - widthOfVisibleText);
355 rect.setHeight(widthOfVisibleText);
358 rect.moveBy(accumulatedOffset);
360 if (locationInContainer.intersects(rect)) {
361 renderer().updateHitTestResult(result, flipForWritingMode(locationInContainer.point() - toLayoutSize(accumulatedOffset)));
362 if (result.addNodeToListBasedTestResult(renderer().textNode(), request, locationInContainer, rect) == HitTestProgress::Stop)
368 static inline bool emphasisPositionHasNeitherLeftNorRight(TextEmphasisPosition emphasisPosition)
370 return !(emphasisPosition & TextEmphasisPositionLeft) && !(emphasisPosition & TextEmphasisPositionRight);
373 bool InlineTextBox::emphasisMarkExistsAndIsAbove(const RenderStyle& style, bool& above) const
375 // This function returns true if there are text emphasis marks and they are suppressed by ruby text.
376 if (style.textEmphasisMark() == TextEmphasisMarkNone)
379 TextEmphasisPosition emphasisPosition = style.textEmphasisPosition();
380 ASSERT(!((emphasisPosition & TextEmphasisPositionOver) && (emphasisPosition & TextEmphasisPositionUnder)));
381 ASSERT(!((emphasisPosition & TextEmphasisPositionLeft) && (emphasisPosition & TextEmphasisPositionRight)));
383 if (emphasisPositionHasNeitherLeftNorRight(emphasisPosition))
384 above = emphasisPosition & TextEmphasisPositionOver;
385 else if (style.isHorizontalWritingMode())
386 above = emphasisPosition & TextEmphasisPositionOver;
388 above = emphasisPosition & TextEmphasisPositionRight;
390 if ((style.isHorizontalWritingMode() && (emphasisPosition & TextEmphasisPositionUnder))
391 || (!style.isHorizontalWritingMode() && (emphasisPosition & TextEmphasisPositionLeft)))
392 return true; // Ruby text is always over, so it cannot suppress emphasis marks under.
394 RenderBlock* containingBlock = renderer().containingBlock();
395 if (!containingBlock->isRubyBase())
396 return true; // This text is not inside a ruby base, so it does not have ruby text over it.
398 if (!is<RenderRubyRun>(*containingBlock->parent()))
399 return true; // Cannot get the ruby text.
401 RenderRubyText* rubyText = downcast<RenderRubyRun>(*containingBlock->parent()).rubyText();
403 // The emphasis marks over are suppressed only if there is a ruby text box and it not empty.
404 return !rubyText || !rubyText->hasLines();
407 void InlineTextBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit /*lineTop*/, LayoutUnit /*lineBottom*/)
409 if (isLineBreak() || !paintInfo.shouldPaintWithinRoot(renderer()) || renderer().style().visibility() != VISIBLE
410 || m_truncation == cFullTruncation || paintInfo.phase == PaintPhaseOutline || !m_len)
413 ASSERT(paintInfo.phase != PaintPhaseSelfOutline && paintInfo.phase != PaintPhaseChildOutlines);
415 LayoutUnit logicalLeftSide = logicalLeftVisualOverflow();
416 LayoutUnit logicalRightSide = logicalRightVisualOverflow();
417 LayoutUnit logicalStart = logicalLeftSide + (isHorizontal() ? paintOffset.x() : paintOffset.y());
418 LayoutUnit logicalExtent = logicalRightSide - logicalLeftSide;
420 LayoutUnit paintEnd = isHorizontal() ? paintInfo.rect.maxX() : paintInfo.rect.maxY();
421 LayoutUnit paintStart = isHorizontal() ? paintInfo.rect.x() : paintInfo.rect.y();
423 FloatPoint localPaintOffset(paintOffset);
425 if (logicalStart >= paintEnd || logicalStart + logicalExtent <= paintStart)
428 bool isPrinting = renderer().document().printing();
430 // Determine whether or not we're selected.
431 bool haveSelection = !isPrinting && paintInfo.phase != PaintPhaseTextClip && selectionState() != RenderObject::SelectionNone;
432 if (!haveSelection && paintInfo.phase == PaintPhaseSelection)
433 // When only painting the selection, don't bother to paint if there is none.
436 if (m_truncation != cNoTruncation) {
437 if (renderer().containingBlock()->style().isLeftToRightDirection() != isLeftToRightDirection()) {
438 // Make the visible fragment of text hug the edge closest to the rest of the run by moving the origin
439 // at which we start drawing text.
440 // e.g. In the case of LTR text truncated in an RTL Context, the correct behavior is:
441 // |Hello|CBA| -> |...He|CBA|
442 // In order to draw the fragment "He" aligned to the right edge of it's box, we need to start drawing
443 // farther to the right.
444 // NOTE: WebKit's behavior differs from that of IE which appears to just overlay the ellipsis on top of the
445 // truncated string i.e. |Hello|CBA| -> |...lo|CBA|
446 LayoutUnit widthOfVisibleText = renderer().width(m_start, m_truncation, textPos(), isFirstLine());
447 LayoutUnit widthOfHiddenText = m_logicalWidth - widthOfVisibleText;
448 LayoutSize truncationOffset(isLeftToRightDirection() ? widthOfHiddenText : -widthOfHiddenText, 0);
449 localPaintOffset.move(isHorizontal() ? truncationOffset : truncationOffset.transposedSize());
453 GraphicsContext& context = paintInfo.context();
455 const RenderStyle& lineStyle = this->lineStyle();
457 localPaintOffset.move(0, lineStyle.isHorizontalWritingMode() ? 0 : -logicalHeight());
459 FloatPoint boxOrigin = locationIncludingFlipping();
460 boxOrigin.moveBy(localPaintOffset);
461 FloatRect boxRect(boxOrigin, FloatSize(logicalWidth(), logicalHeight()));
463 RenderCombineText* combinedText = lineStyle.hasTextCombine() && is<RenderCombineText>(renderer()) && downcast<RenderCombineText>(renderer()).isCombined() ? &downcast<RenderCombineText>(renderer()) : nullptr;
465 bool shouldRotate = !isHorizontal() && !combinedText;
467 context.concatCTM(rotation(boxRect, Clockwise));
469 // Determine whether or not we have composition underlines to draw.
470 bool containsComposition = renderer().textNode() && renderer().frame().editor().compositionNode() == renderer().textNode();
471 bool useCustomUnderlines = containsComposition && renderer().frame().editor().compositionUsesCustomUnderlines();
473 // Determine the text colors and selection colors.
474 TextPaintStyle textPaintStyle = computeTextPaintStyle(renderer().frame(), lineStyle, paintInfo);
476 bool paintSelectedTextOnly = false;
477 bool paintSelectedTextSeparately = false;
478 bool paintNonSelectedTextOnly = false;
479 const ShadowData* selectionShadow = nullptr;
481 // Text with custom underlines does not have selection background painted, so selection paint style is not appropriate for it.
482 TextPaintStyle selectionPaintStyle = haveSelection && !useCustomUnderlines ? computeTextSelectionPaintStyle(textPaintStyle, renderer(), lineStyle, paintInfo, paintSelectedTextOnly, paintSelectedTextSeparately, paintNonSelectedTextOnly, selectionShadow) : textPaintStyle;
485 const FontCascade& font = fontToUse(lineStyle, renderer());
486 // 1. Paint backgrounds behind text if needed. Examples of such backgrounds include selection
487 // and composition underlines.
488 if (paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseTextClip && !isPrinting) {
489 if (containsComposition && !useCustomUnderlines)
490 paintCompositionBackground(context, boxOrigin, lineStyle, font,
491 renderer().frame().editor().compositionStart(),
492 renderer().frame().editor().compositionEnd());
494 paintDocumentMarkers(context, boxOrigin, lineStyle, font, true);
496 if (haveSelection && !useCustomUnderlines)
497 paintSelection(context, boxOrigin, lineStyle, font, selectionPaintStyle.fillColor);
500 // FIXME: Right now, InlineTextBoxes never call addRelevantUnpaintedObject() even though they might
501 // legitimately be unpainted if they are waiting on a slow-loading web font. We should fix that, and
502 // when we do, we will have to account for the fact the InlineTextBoxes do not always have unique
503 // renderers and Page currently relies on each unpainted object having a unique renderer.
504 if (paintInfo.phase == PaintPhaseForeground)
505 renderer().page().addRelevantRepaintedObject(&renderer(), IntRect(boxOrigin.x(), boxOrigin.y(), logicalWidth(), logicalHeight()));
507 // 2. Now paint the foreground, including text and decorations like underline/overline (in quirks mode only).
508 String alternateStringToRender;
510 alternateStringToRender = combinedText->combinedStringForRendering();
511 else if (hasHyphen())
512 alternateStringToRender = hyphenatedStringForTextRun(lineStyle);
514 TextRun textRun = constructTextRun(lineStyle, alternateStringToRender);
515 unsigned length = textRun.length();
517 unsigned selectionStart = 0;
518 unsigned selectionEnd = 0;
519 if (haveSelection && (paintSelectedTextOnly || paintSelectedTextSeparately))
520 std::tie(selectionStart, selectionEnd) = selectionStartEnd();
522 if (m_truncation != cNoTruncation) {
523 selectionStart = std::min(selectionStart, static_cast<unsigned>(m_truncation));
524 selectionEnd = std::min(selectionEnd, static_cast<unsigned>(m_truncation));
525 length = m_truncation;
528 float emphasisMarkOffset = 0;
529 bool emphasisMarkAbove;
530 bool hasTextEmphasis = emphasisMarkExistsAndIsAbove(lineStyle, emphasisMarkAbove);
531 const AtomicString& emphasisMark = hasTextEmphasis ? lineStyle.textEmphasisMarkString() : nullAtom();
532 if (!emphasisMark.isEmpty())
533 emphasisMarkOffset = emphasisMarkAbove ? -font.fontMetrics().ascent() - font.emphasisMarkDescent(emphasisMark) : font.fontMetrics().descent() + font.emphasisMarkAscent(emphasisMark);
535 const ShadowData* textShadow = (paintInfo.forceTextColor()) ? nullptr : lineStyle.textShadow();
537 FloatPoint textOrigin(boxOrigin.x(), boxOrigin.y() + font.fontMetrics().ascent());
539 if (auto newOrigin = combinedText->computeTextOrigin(boxRect))
540 textOrigin = newOrigin.value();
544 textOrigin.setY(roundToDevicePixel(LayoutUnit(textOrigin.y()), renderer().document().deviceScaleFactor()));
546 textOrigin.setX(roundToDevicePixel(LayoutUnit(textOrigin.x()), renderer().document().deviceScaleFactor()));
548 TextPainter textPainter(context);
549 textPainter.setFont(font);
550 textPainter.setStyle(textPaintStyle);
551 textPainter.setSelectionStyle(selectionPaintStyle);
552 textPainter.setIsHorizontal(isHorizontal());
553 textPainter.setShadow(textShadow);
554 textPainter.setSelectionShadow(selectionShadow);
555 textPainter.setEmphasisMark(emphasisMark, emphasisMarkOffset, combinedText);
557 auto draggedContentRanges = renderer().draggedContentRangesBetweenOffsets(m_start, m_start + m_len);
558 if (!draggedContentRanges.isEmpty() && !paintSelectedTextOnly && !paintNonSelectedTextOnly) {
559 // FIXME: Painting with text effects ranges currently only works if we're not also painting the selection.
560 // In the future, we may want to support this capability, but in the meantime, this isn't required by anything.
561 unsigned currentEnd = 0;
562 for (size_t index = 0; index < draggedContentRanges.size(); ++index) {
563 unsigned previousEnd = index ? std::min(draggedContentRanges[index - 1].second, length) : 0;
564 unsigned currentStart = draggedContentRanges[index].first - m_start;
565 currentEnd = std::min(draggedContentRanges[index].second - m_start, length);
567 if (previousEnd < currentStart)
568 textPainter.paintRange(textRun, boxRect, textOrigin, previousEnd, currentStart);
570 if (currentStart < currentEnd) {
572 context.setAlpha(0.25);
573 textPainter.paintRange(textRun, boxRect, textOrigin, currentStart, currentEnd);
577 if (currentEnd < length)
578 textPainter.paintRange(textRun, boxRect, textOrigin, currentEnd, length);
580 textPainter.paint(textRun, length, boxRect, textOrigin, selectionStart, selectionEnd, paintSelectedTextOnly, paintSelectedTextSeparately, paintNonSelectedTextOnly);
583 TextDecoration textDecorations = lineStyle.textDecorationsInEffect();
584 if (textDecorations != TextDecorationNone && paintInfo.phase != PaintPhaseSelection) {
585 FloatRect textDecorationSelectionClipOutRect;
586 if ((paintInfo.paintBehavior & PaintBehaviorExcludeSelection) && selectionStart < selectionEnd && selectionEnd <= length) {
587 textDecorationSelectionClipOutRect = logicalOverflowRect();
588 textDecorationSelectionClipOutRect.moveBy(localPaintOffset);
589 float logicalWidthBeforeRange;
590 float logicalWidthAfterRange;
591 float logicalSelectionWidth = font.widthOfTextRange(textRun, selectionStart, selectionEnd, nullptr, &logicalWidthBeforeRange, &logicalWidthAfterRange);
592 // FIXME: Do we need to handle vertical bottom to top text?
593 if (!isHorizontal()) {
594 textDecorationSelectionClipOutRect.move(0, logicalWidthBeforeRange);
595 textDecorationSelectionClipOutRect.setHeight(logicalSelectionWidth);
596 } else if (direction() == RTL) {
597 textDecorationSelectionClipOutRect.move(logicalWidthAfterRange, 0);
598 textDecorationSelectionClipOutRect.setWidth(logicalSelectionWidth);
600 textDecorationSelectionClipOutRect.move(logicalWidthBeforeRange, 0);
601 textDecorationSelectionClipOutRect.setWidth(logicalSelectionWidth);
604 paintDecoration(context, font, combinedText, textRun, textOrigin, boxRect, textDecorations, textPaintStyle, textShadow, textDecorationSelectionClipOutRect);
607 if (paintInfo.phase == PaintPhaseForeground) {
608 paintDocumentMarkers(context, boxOrigin, lineStyle, font, false);
610 if (useCustomUnderlines) {
611 const Vector<CompositionUnderline>& underlines = renderer().frame().editor().customCompositionUnderlines();
612 size_t numUnderlines = underlines.size();
614 for (size_t index = 0; index < numUnderlines; ++index) {
615 const CompositionUnderline& underline = underlines[index];
617 if (underline.endOffset <= start())
618 // underline is completely before this run. This might be an underline that sits
619 // before the first run we draw, or underlines that were within runs we skipped
620 // due to truncation.
623 if (underline.startOffset <= end()) {
624 // underline intersects this run. Paint it.
625 paintCompositionUnderline(context, boxOrigin, underline);
626 if (underline.endOffset > end() + 1)
627 // underline also runs into the next run. Bail now, no more marker advancement.
630 // underline is completely after this run, bail. A later run will paint it.
637 context.concatCTM(rotation(boxRect, Counterclockwise));
640 unsigned InlineTextBox::clampedOffset(unsigned x) const
642 return std::max(std::min(x, start() + len()), start()) - start();
645 std::pair<unsigned, unsigned> InlineTextBox::selectionStartEnd() const
647 auto selectionState = renderer().selectionState();
648 if (selectionState == RenderObject::SelectionInside)
653 renderer().selectionStartEnd(start, end);
654 if (selectionState == RenderObject::SelectionStart)
655 end = renderer().textLength();
656 else if (selectionState == RenderObject::SelectionEnd)
658 return { clampedOffset(start), clampedOffset(end) };
661 void InlineTextBox::paintSelection(GraphicsContext& context, const FloatPoint& boxOrigin, const RenderStyle& style, const FontCascade& font, const Color& textColor)
663 #if ENABLE(TEXT_SELECTION)
664 if (context.paintingDisabled())
667 // See if we have a selection to paint at all.
668 unsigned selectionStart;
669 unsigned selectionEnd;
670 std::tie(selectionStart, selectionEnd) = selectionStartEnd();
671 if (selectionStart >= selectionEnd)
674 Color c = renderer().selectionBackgroundColor();
675 if (!c.isValid() || c.alpha() == 0)
678 // If the text color ends up being the same as the selection background, invert the selection
681 c = Color(0xff - c.red(), 0xff - c.green(), 0xff - c.blue());
683 GraphicsContextStateSaver stateSaver(context);
684 updateGraphicsContext(context, TextPaintStyle(c)); // Don't draw text at all!
686 // If the text is truncated, let the thing being painted in the truncation
687 // draw its own highlight.
689 unsigned length = m_truncation != cNoTruncation ? m_truncation : len();
691 String hyphenatedString;
692 bool respectHyphen = selectionEnd == length && hasHyphen();
694 hyphenatedString = hyphenatedStringForTextRun(style, length);
695 TextRun textRun = constructTextRun(style, hyphenatedString, std::optional<unsigned>(length));
697 selectionEnd = textRun.length();
699 const RootInlineBox& rootBox = root();
700 LayoutUnit selectionBottom = rootBox.selectionBottom();
701 LayoutUnit selectionTop = rootBox.selectionTopAdjustedForPrecedingBlock();
703 LayoutUnit deltaY = renderer().style().isFlippedLinesWritingMode() ? selectionBottom - logicalBottom() : logicalTop() - selectionTop;
704 LayoutUnit selectionHeight = std::max<LayoutUnit>(0, selectionBottom - selectionTop);
706 LayoutRect selectionRect = LayoutRect(boxOrigin.x(), boxOrigin.y() - deltaY, m_logicalWidth, selectionHeight);
707 font.adjustSelectionRectForText(textRun, selectionRect, selectionStart, selectionEnd);
708 context.fillRect(snapRectToDevicePixelsWithWritingDirection(selectionRect, renderer().document().deviceScaleFactor(), textRun.ltr()), c);
710 UNUSED_PARAM(context);
711 UNUSED_PARAM(boxOrigin);
714 UNUSED_PARAM(textColor);
718 void InlineTextBox::paintCompositionBackground(GraphicsContext& context, const FloatPoint& boxOrigin, const RenderStyle& style, const FontCascade& font, unsigned startPos, unsigned endPos)
720 unsigned selectionStart = clampedOffset(startPos);
721 unsigned selectionEnd = clampedOffset(endPos);
722 if (selectionStart >= selectionEnd)
725 GraphicsContextStateSaver stateSaver(context);
726 Color compositionColor = Color::compositionFill;
727 updateGraphicsContext(context, TextPaintStyle(compositionColor)); // Don't draw text at all!
729 LayoutUnit deltaY = renderer().style().isFlippedLinesWritingMode() ? selectionBottom() - logicalBottom() : logicalTop() - selectionTop();
730 LayoutRect selectionRect = LayoutRect(boxOrigin.x(), boxOrigin.y() - deltaY, 0, selectionHeight());
731 TextRun textRun = constructTextRun(style);
732 font.adjustSelectionRectForText(textRun, selectionRect, selectionStart, selectionEnd);
733 context.fillRect(snapRectToDevicePixelsWithWritingDirection(selectionRect, renderer().document().deviceScaleFactor(), textRun.ltr()), compositionColor);
736 static inline void mirrorRTLSegment(float logicalWidth, TextDirection direction, float& start, float width)
738 if (direction == LTR)
740 start = logicalWidth - width - start;
743 void InlineTextBox::paintDecoration(GraphicsContext& context, const FontCascade& font, RenderCombineText* combinedText, const TextRun& textRun, const FloatPoint& textOrigin,
744 const FloatRect& boxRect, TextDecoration decoration, TextPaintStyle textPaintStyle, const ShadowData* shadow, const FloatRect& clipOutRect)
746 if (m_truncation == cFullTruncation)
749 updateGraphicsContext(context, textPaintStyle);
751 context.concatCTM(rotation(boxRect, Clockwise));
754 float width = m_logicalWidth;
755 if (m_truncation != cNoTruncation) {
756 width = renderer().width(m_start, m_truncation, textPos(), isFirstLine());
757 mirrorRTLSegment(m_logicalWidth, direction(), start, width);
760 TextDecorationPainter decorationPainter(context, decoration, renderer(), isFirstLine());
761 decorationPainter.setInlineTextBox(this);
762 decorationPainter.setFont(font);
763 decorationPainter.setWidth(width);
764 decorationPainter.setBaseline(lineStyle().fontMetrics().ascent());
765 decorationPainter.setIsHorizontal(isHorizontal());
766 decorationPainter.addTextShadow(shadow);
768 FloatPoint localOrigin = boxRect.location();
769 localOrigin.move(start, 0);
771 if (!clipOutRect.isEmpty()) {
773 context.clipOut(clipOutRect);
776 decorationPainter.paintTextDecoration(textRun, textOrigin, localOrigin);
778 if (!clipOutRect.isEmpty())
782 context.concatCTM(rotation(boxRect, Counterclockwise));
785 static GraphicsContext::DocumentMarkerLineStyle lineStyleForMarkerType(DocumentMarker::MarkerType markerType)
787 switch (markerType) {
788 case DocumentMarker::Spelling:
789 return GraphicsContext::DocumentMarkerSpellingLineStyle;
790 case DocumentMarker::Grammar:
791 return GraphicsContext::DocumentMarkerGrammarLineStyle;
792 case DocumentMarker::CorrectionIndicator:
793 return GraphicsContext::DocumentMarkerAutocorrectionReplacementLineStyle;
794 case DocumentMarker::DictationAlternatives:
795 return GraphicsContext::DocumentMarkerDictationAlternativesLineStyle;
797 case DocumentMarker::DictationPhraseWithAlternatives:
798 // FIXME: Rename TextCheckingDictationPhraseWithAlternativesLineStyle and remove the PLATFORM(IOS)-guard.
799 return GraphicsContext::TextCheckingDictationPhraseWithAlternativesLineStyle;
802 ASSERT_NOT_REACHED();
803 return GraphicsContext::DocumentMarkerSpellingLineStyle;
807 void InlineTextBox::paintDocumentMarker(GraphicsContext& context, const FloatPoint& boxOrigin, RenderedDocumentMarker& marker, const RenderStyle& style, const FontCascade& font, bool grammar)
809 // Never print spelling/grammar markers (5327887)
810 if (renderer().document().printing())
813 if (m_truncation == cFullTruncation)
816 float start = 0; // start of line to draw, relative to tx
817 float width = m_logicalWidth; // how much line to draw
819 // Determine whether we need to measure text
820 bool markerSpansWholeBox = true;
821 if (m_start <= marker.startOffset())
822 markerSpansWholeBox = false;
823 if ((end() + 1) != marker.endOffset()) // end points at the last char, not past it
824 markerSpansWholeBox = false;
825 if (m_truncation != cNoTruncation)
826 markerSpansWholeBox = false;
828 bool isDictationMarker = marker.type() == DocumentMarker::DictationAlternatives;
829 if (!markerSpansWholeBox || grammar || isDictationMarker) {
830 unsigned startPosition = clampedOffset(marker.startOffset());
831 unsigned endPosition = clampedOffset(marker.endOffset());
833 if (m_truncation != cNoTruncation)
834 endPosition = std::min(endPosition, static_cast<unsigned>(m_truncation));
836 // Calculate start & width
837 int deltaY = renderer().style().isFlippedLinesWritingMode() ? selectionBottom() - logicalBottom() : logicalTop() - selectionTop();
838 int selHeight = selectionHeight();
839 FloatPoint startPoint(boxOrigin.x(), boxOrigin.y() - deltaY);
840 TextRun run = constructTextRun(style);
842 LayoutRect selectionRect = LayoutRect(startPoint, FloatSize(0, selHeight));
843 font.adjustSelectionRectForText(run, selectionRect, startPosition, endPosition);
844 IntRect markerRect = enclosingIntRect(selectionRect);
845 start = markerRect.x() - startPoint.x();
846 width = markerRect.width();
849 // IMPORTANT: The misspelling underline is not considered when calculating the text bounds, so we have to
850 // make sure to fit within those bounds. This means the top pixel(s) of the underline will overlap the
851 // bottom pixel(s) of the glyphs in smaller font sizes. The alternatives are to increase the line spacing (bad!!)
852 // or decrease the underline thickness. The overlap is actually the most useful, and matches what AppKit does.
853 // So, we generally place the underline at the bottom of the text, but in larger fonts that's not so good so
854 // we pin to two pixels under the baseline.
855 int lineThickness = cMisspellingLineThickness;
856 int baseline = lineStyle().fontMetrics().ascent();
857 int descent = logicalHeight() - baseline;
859 if (descent <= (2 + lineThickness)) {
860 // Place the underline at the very bottom of the text in small/medium fonts.
861 underlineOffset = logicalHeight() - lineThickness;
863 // In larger fonts, though, place the underline up near the baseline to prevent a big gap.
864 underlineOffset = baseline + 2;
866 context.drawLineForDocumentMarker(FloatPoint(boxOrigin.x() + start, boxOrigin.y() + underlineOffset), width, lineStyleForMarkerType(marker.type()));
869 void InlineTextBox::paintTextMatchMarker(GraphicsContext& context, const FloatPoint& boxOrigin, RenderedDocumentMarker& marker, const RenderStyle& style, const FontCascade& font)
871 if (!renderer().frame().editor().markedTextMatchesAreHighlighted())
874 Color color = marker.isActiveMatch() ? renderer().theme().platformActiveTextSearchHighlightColor() : renderer().theme().platformInactiveTextSearchHighlightColor();
875 GraphicsContextStateSaver stateSaver(context);
876 updateGraphicsContext(context, TextPaintStyle(color)); // Don't draw text at all!
878 // Use same y positioning and height as for selection, so that when the selection and this highlight are on
879 // the same word there are no pieces sticking out.
880 LayoutUnit deltaY = renderer().style().isFlippedLinesWritingMode() ? selectionBottom() - logicalBottom() : logicalTop() - selectionTop();
881 LayoutRect selectionRect = LayoutRect(boxOrigin.x(), boxOrigin.y() - deltaY, 0, this->selectionHeight());
883 unsigned sPos = clampedOffset(marker.startOffset());
884 unsigned ePos = clampedOffset(marker.endOffset());
885 TextRun run = constructTextRun(style);
886 font.adjustSelectionRectForText(run, selectionRect, sPos, ePos);
888 if (selectionRect.isEmpty())
891 context.fillRect(snapRectToDevicePixelsWithWritingDirection(selectionRect, renderer().document().deviceScaleFactor(), run.ltr()), color);
894 void InlineTextBox::paintDocumentMarkers(GraphicsContext& context, const FloatPoint& boxOrigin, const RenderStyle& style, const FontCascade& font, bool background)
896 if (!renderer().textNode())
899 Vector<RenderedDocumentMarker*> markers = renderer().document().markers().markersFor(renderer().textNode());
901 // Give any document markers that touch this run a chance to draw before the text has been drawn.
902 // Note end() points at the last char, not one past it like endOffset and ranges do.
903 for (auto* marker : markers) {
904 // Paint either the background markers or the foreground markers, but not both
905 switch (marker->type()) {
906 case DocumentMarker::Grammar:
907 case DocumentMarker::Spelling:
908 case DocumentMarker::CorrectionIndicator:
909 case DocumentMarker::Replacement:
910 case DocumentMarker::DictationAlternatives:
912 // FIXME: Remove the PLATFORM(IOS)-guard.
913 case DocumentMarker::DictationPhraseWithAlternatives:
918 case DocumentMarker::TextMatch:
919 #if ENABLE(TELEPHONE_NUMBER_DETECTION)
920 case DocumentMarker::TelephoneNumber:
929 if (marker->endOffset() <= start())
930 // marker is completely before this run. This might be a marker that sits before the
931 // first run we draw, or markers that were within runs we skipped due to truncation.
934 if (marker->startOffset() > end())
935 // marker is completely after this run, bail. A later run will paint it.
938 // marker intersects this run. Paint it.
939 switch (marker->type()) {
940 case DocumentMarker::Spelling:
941 case DocumentMarker::CorrectionIndicator:
942 case DocumentMarker::DictationAlternatives:
943 paintDocumentMarker(context, boxOrigin, *marker, style, font, false);
945 case DocumentMarker::Grammar:
946 paintDocumentMarker(context, boxOrigin, *marker, style, font, true);
949 // FIXME: See <rdar://problem/8933352>. Also, remove the PLATFORM(IOS)-guard.
950 case DocumentMarker::DictationPhraseWithAlternatives:
951 paintDocumentMarker(context, boxOrigin, *marker, style, font, true);
954 case DocumentMarker::TextMatch:
955 paintTextMatchMarker(context, boxOrigin, *marker, style, font);
957 case DocumentMarker::Replacement:
959 #if ENABLE(TELEPHONE_NUMBER_DETECTION)
960 case DocumentMarker::TelephoneNumber:
964 ASSERT_NOT_REACHED();
970 void InlineTextBox::paintCompositionUnderline(GraphicsContext& context, const FloatPoint& boxOrigin, const CompositionUnderline& underline)
972 if (m_truncation == cFullTruncation)
975 float start = 0; // start of line to draw, relative to tx
976 float width = m_logicalWidth; // how much line to draw
977 bool useWholeWidth = true;
978 unsigned paintStart = m_start;
979 unsigned paintEnd = end() + 1; // end points at the last char, not past it
980 if (paintStart <= underline.startOffset) {
981 paintStart = underline.startOffset;
982 useWholeWidth = false;
983 start = renderer().width(m_start, paintStart - m_start, textPos(), isFirstLine());
985 if (paintEnd != underline.endOffset) { // end points at the last char, not past it
986 paintEnd = std::min(paintEnd, (unsigned)underline.endOffset);
987 useWholeWidth = false;
989 if (m_truncation != cNoTruncation) {
990 paintEnd = std::min(paintEnd, (unsigned)m_start + m_truncation);
991 useWholeWidth = false;
993 if (!useWholeWidth) {
994 width = renderer().width(paintStart, paintEnd - paintStart, textPos() + start, isFirstLine());
995 mirrorRTLSegment(m_logicalWidth, direction(), start, width);
998 // Thick marked text underlines are 2px thick as long as there is room for the 2px line under the baseline.
999 // All other marked text underlines are 1px thick.
1000 // If there's not enough space the underline will touch or overlap characters.
1001 int lineThickness = 1;
1002 int baseline = lineStyle().fontMetrics().ascent();
1003 if (underline.thick && logicalHeight() - baseline >= 2)
1006 // We need to have some space between underlines of subsequent clauses, because some input methods do not use different underline styles for those.
1007 // We make each line shorter, which has a harmless side effect of shortening the first and last clauses, too.
1011 context.setStrokeColor(underline.compositionUnderlineColor == CompositionUnderlineColor::TextColor ? renderer().style().visitedDependentColor(CSSPropertyWebkitTextFillColor) : underline.color);
1012 context.setStrokeThickness(lineThickness);
1013 context.drawLineForText(FloatPoint(boxOrigin.x() + start, boxOrigin.y() + logicalHeight() - lineThickness), width, renderer().document().printing());
1016 int InlineTextBox::caretMinOffset() const
1021 int InlineTextBox::caretMaxOffset() const
1023 return m_start + m_len;
1026 float InlineTextBox::textPos() const
1028 // When computing the width of a text run, RenderBlock::computeInlineDirectionPositionsForLine() doesn't include the actual offset
1029 // from the containing block edge in its measurement. textPos() should be consistent so the text are rendered in the same width.
1030 if (logicalLeft() == 0)
1032 return logicalLeft() - root().logicalLeft();
1035 int InlineTextBox::offsetForPosition(float lineOffset, bool includePartialGlyphs) const
1040 if (lineOffset - logicalLeft() > logicalWidth())
1041 return isLeftToRightDirection() ? len() : 0;
1042 if (lineOffset - logicalLeft() < 0)
1043 return isLeftToRightDirection() ? 0 : len();
1045 const RenderStyle& lineStyle = this->lineStyle();
1046 const FontCascade& font = fontToUse(lineStyle, renderer());
1047 return font.offsetForPosition(constructTextRun(lineStyle), lineOffset - logicalLeft(), includePartialGlyphs);
1050 float InlineTextBox::positionForOffset(unsigned offset) const
1052 ASSERT(offset >= m_start);
1053 ASSERT(offset <= m_start + len());
1056 return logicalLeft();
1058 const RenderStyle& lineStyle = this->lineStyle();
1059 const FontCascade& font = fontToUse(lineStyle, renderer());
1060 unsigned from = !isLeftToRightDirection() ? clampedOffset(offset) : 0;
1061 unsigned to = !isLeftToRightDirection() ? m_len : clampedOffset(offset);
1062 // FIXME: Do we need to add rightBearing here?
1063 LayoutRect selectionRect = LayoutRect(logicalLeft(), 0, 0, 0);
1064 TextRun run = constructTextRun(lineStyle);
1065 font.adjustSelectionRectForText(run, selectionRect, from, to);
1066 return snapRectToDevicePixelsWithWritingDirection(selectionRect, renderer().document().deviceScaleFactor(), run.ltr()).maxX();
1069 StringView InlineTextBox::substringToRender(std::optional<unsigned> overridingLength) const
1071 return StringView(renderer().text()).substring(start(), overridingLength.value_or(len()));
1074 String InlineTextBox::hyphenatedStringForTextRun(const RenderStyle& style, std::optional<unsigned> alternateLength) const
1076 ASSERT(hasHyphen());
1077 return makeString(substringToRender(alternateLength), style.hyphenString());
1080 TextRun InlineTextBox::constructTextRun(const RenderStyle& style, StringView alternateStringToRender, std::optional<unsigned> alternateLength) const
1082 if (alternateStringToRender.isNull())
1083 return constructTextRun(style, substringToRender(alternateLength), renderer().textLength() - start());
1084 return constructTextRun(style, alternateStringToRender, alternateStringToRender.length());
1087 TextRun InlineTextBox::constructTextRun(const RenderStyle& style, StringView string, unsigned maximumLength) const
1089 ASSERT(maximumLength >= string.length());
1091 TextRun run(string, textPos(), expansion(), expansionBehavior(), direction(), dirOverride() || style.rtlOrdering() == VisualOrder, !renderer().canUseSimpleFontCodePath());
1092 run.setTabSize(!style.collapseWhiteSpace(), style.tabSize());
1094 // Propagate the maximum length of the characters buffer to the TextRun, even when we're only processing a substring.
1095 run.setCharactersLength(maximumLength);
1096 ASSERT(run.charactersLength() >= run.length());
1100 ExpansionBehavior InlineTextBox::expansionBehavior() const
1102 ExpansionBehavior leadingBehavior;
1103 if (forceLeadingExpansion())
1104 leadingBehavior = ForceLeadingExpansion;
1105 else if (canHaveLeadingExpansion())
1106 leadingBehavior = AllowLeadingExpansion;
1108 leadingBehavior = ForbidLeadingExpansion;
1110 ExpansionBehavior trailingBehavior;
1111 if (forceTrailingExpansion())
1112 trailingBehavior = ForceTrailingExpansion;
1113 else if (expansion() && nextLeafChild() && !nextLeafChild()->isLineBreak())
1114 trailingBehavior = AllowTrailingExpansion;
1116 trailingBehavior = ForbidTrailingExpansion;
1118 return leadingBehavior | trailingBehavior;
1121 #if ENABLE(TREE_DEBUGGING)
1123 const char* InlineTextBox::boxName() const
1125 return "InlineTextBox";
1128 void InlineTextBox::outputLineBox(TextStream& stream, bool mark, int depth) const
1130 stream << "-------- " << (isDirty() ? "D" : "-") << "-";
1132 int printedCharacters = 0;
1135 ++printedCharacters;
1137 while (++printedCharacters <= depth * 2)
1140 String value = renderer().text();
1141 value = value.substring(start(), len());
1142 value.replaceWithLiteral('\\', "\\\\");
1143 value.replaceWithLiteral('\n', "\\n");
1144 stream << boxName() << " " << FloatRect(x(), y(), width(), height()) << " (" << this << ") renderer->(" << &renderer() << ") run(" << start() << ", " << start() + len() << ") \"" << value.utf8().data() << "\"";
1150 } // namespace WebCore