Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: Source/core/rendering/InlineTextBox.cpp

Issue 313233002: Adding backgroundColor to WebCompositionUnderline and using it for InlineTextBox drawing. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Removing over-eager cleanups; adding CompositionUnderlineRangeFilter::operator->(). Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * (C) 1999 Lars Knoll (knoll@kde.org) 2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Dirk Mueller (mueller@kde.org) 3 * (C) 2000 Dirk Mueller (mueller@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All r ights reserved. 4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All r ights reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
11 * This library is distributed in the hope that it will be useful, 11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details. 14 * Library General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU Library General Public License 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 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, 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA. 19 * Boston, MA 02110-1301, USA.
20 * 20 *
21 */ 21 */
22 22
23 #include "config.h" 23 #include "config.h"
24 #include "core/rendering/InlineTextBox.h" 24 #include "core/rendering/InlineTextBox.h"
25 25
26 #include "core/dom/Document.h" 26 #include "core/dom/Document.h"
27 #include "core/dom/DocumentMarkerController.h" 27 #include "core/dom/DocumentMarkerController.h"
28 #include "core/dom/RenderedDocumentMarker.h" 28 #include "core/dom/RenderedDocumentMarker.h"
29 #include "core/dom/Text.h" 29 #include "core/dom/Text.h"
30 #include "core/editing/CompositionUnderline.h"
31 #include "core/editing/CompositionUnderlineRangeFilter.h"
30 #include "core/editing/Editor.h" 32 #include "core/editing/Editor.h"
31 #include "core/editing/InputMethodController.h" 33 #include "core/editing/InputMethodController.h"
32 #include "core/frame/LocalFrame.h" 34 #include "core/frame/LocalFrame.h"
33 #include "core/page/Page.h" 35 #include "core/page/Page.h"
34 #include "core/frame/Settings.h" 36 #include "core/frame/Settings.h"
35 #include "core/rendering/AbstractInlineTextBox.h" 37 #include "core/rendering/AbstractInlineTextBox.h"
36 #include "core/rendering/EllipsisBox.h" 38 #include "core/rendering/EllipsisBox.h"
37 #include "core/rendering/HitTestResult.h" 39 #include "core/rendering/HitTestResult.h"
38 #include "core/rendering/PaintInfo.h" 40 #include "core/rendering/PaintInfo.h"
39 #include "core/rendering/RenderBR.h" 41 #include "core/rendering/RenderBR.h"
40 #include "core/rendering/RenderBlock.h" 42 #include "core/rendering/RenderBlock.h"
41 #include "core/rendering/RenderCombineText.h" 43 #include "core/rendering/RenderCombineText.h"
42 #include "core/rendering/RenderRubyRun.h" 44 #include "core/rendering/RenderRubyRun.h"
43 #include "core/rendering/RenderRubyText.h" 45 #include "core/rendering/RenderRubyText.h"
44 #include "core/rendering/RenderTheme.h" 46 #include "core/rendering/RenderTheme.h"
45 #include "core/rendering/style/ShadowList.h" 47 #include "core/rendering/style/ShadowList.h"
46 #include "core/rendering/svg/SVGTextRunRenderingContext.h" 48 #include "core/rendering/svg/SVGTextRunRenderingContext.h"
47 #include "platform/fonts/FontCache.h" 49 #include "platform/fonts/FontCache.h"
48 #include "platform/fonts/WidthIterator.h" 50 #include "platform/fonts/WidthIterator.h"
49 #include "platform/graphics/DrawLooperBuilder.h" 51 #include "platform/graphics/DrawLooperBuilder.h"
50 #include "platform/graphics/GraphicsContextStateSaver.h" 52 #include "platform/graphics/GraphicsContextStateSaver.h"
51 #include "wtf/Vector.h" 53 #include "wtf/Vector.h"
52 #include "wtf/text/CString.h" 54 #include "wtf/text/CString.h"
53 #include "wtf/text/StringBuilder.h" 55 #include "wtf/text/StringBuilder.h"
54 56
57 #include <algorithm>
58
55 using namespace std; 59 using namespace std;
56 60
57 namespace WebCore { 61 namespace WebCore {
58 62
59 struct SameSizeAsInlineTextBox : public InlineBox { 63 struct SameSizeAsInlineTextBox : public InlineBox {
60 unsigned variables[1]; 64 unsigned variables[1];
61 unsigned short variables2[2]; 65 unsigned short variables2[2];
62 void* pointers[2]; 66 void* pointers[2];
63 }; 67 };
64 68
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 632
629 // Set our font. 633 // Set our font.
630 const Font& font = styleToUse->font(); 634 const Font& font = styleToUse->font();
631 635
632 FloatPoint textOrigin = FloatPoint(boxOrigin.x(), boxOrigin.y() + font.fontM etrics().ascent()); 636 FloatPoint textOrigin = FloatPoint(boxOrigin.x(), boxOrigin.y() + font.fontM etrics().ascent());
633 637
634 if (combinedText) 638 if (combinedText)
635 combinedText->adjustTextOrigin(textOrigin, boxRect); 639 combinedText->adjustTextOrigin(textOrigin, boxRect);
636 640
637 // 1. Paint backgrounds behind text if needed. Examples of such backgrounds include selection 641 // 1. Paint backgrounds behind text if needed. Examples of such backgrounds include selection
638 // and composition underlines. 642 // and composition highlights.
639 if (paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseT extClip && !isPrinting) { 643 if (paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseT extClip && !isPrinting) {
644 if (containsComposition) {
645 if (useCustomUnderlines) {
646 // Paint custom background highlights for compositions.
647 const Vector<CompositionUnderline>& underlines = renderer().fram e()->inputMethodController().customCompositionUnderlines();
648 CompositionUnderlineRangeFilter filter(underlines, start(), end( ));
649 for (CompositionUnderlineRangeFilter::ConstIterator it = filter. begin(); it != filter.end(); ++it) {
650 if (it->backgroundColor != Color::transparent) {
tkent 2014/06/13 04:31:43 We prefer early continue. if (it->backgroundColor
huangs 2014/06/13 17:22:24 Done, also applied to the other loop near line 791
651 unsigned paintStart = underlinePaintStart(*it);
652 unsigned paintEnd = underlinePaintEnd(*it);
653 paintCompositionBackground(context, boxOrigin, styleToUs e, font, it->backgroundColor, paintStart, paintEnd);
654 }
655 }
640 656
641 if (containsComposition && !useCustomUnderlines) { 657 } else {
642 paintCompositionBackground(context, boxOrigin, styleToUse, font, 658 paintCompositionBackground(context, boxOrigin, styleToUse, font, RenderTheme::theme().platformDefaultCompositionBackgroundColor(),
643 renderer().frame()->inputMethodController().compositionStart(), 659 renderer().frame()->inputMethodController().compositionStart (),
644 renderer().frame()->inputMethodController().compositionEnd()); 660 renderer().frame()->inputMethodController().compositionEnd() );
661 }
645 } 662 }
646 663
647 paintDocumentMarkers(context, boxOrigin, styleToUse, font, true); 664 paintDocumentMarkers(context, boxOrigin, styleToUse, font, true);
648 665
649 if (haveSelection && !useCustomUnderlines) 666 if (haveSelection && !useCustomUnderlines)
650 paintSelection(context, boxOrigin, styleToUse, font, selectionFillCo lor); 667 paintSelection(context, boxOrigin, styleToUse, font, selectionFillCo lor);
651 } 668 }
652 669
653 // 2. Now paint the foreground, including text and decorations like underlin e/overline (in quirks mode only). 670 // 2. Now paint the foreground, including text and decorations like underlin e/overline (in quirks mode only).
654 int length = m_len; 671 int length = m_len;
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 if (combinedText) 777 if (combinedText)
761 context->concatCTM(rotation(boxRect, Clockwise)); 778 context->concatCTM(rotation(boxRect, Clockwise));
762 paintDecoration(context, boxOrigin, textDecorations, textShadow); 779 paintDecoration(context, boxOrigin, textDecorations, textShadow);
763 if (combinedText) 780 if (combinedText)
764 context->concatCTM(rotation(boxRect, Counterclockwise)); 781 context->concatCTM(rotation(boxRect, Counterclockwise));
765 } 782 }
766 783
767 if (paintInfo.phase == PaintPhaseForeground) { 784 if (paintInfo.phase == PaintPhaseForeground) {
768 paintDocumentMarkers(context, boxOrigin, styleToUse, font, false); 785 paintDocumentMarkers(context, boxOrigin, styleToUse, font, false);
769 786
787 // Paint custom underlines for compositions.
770 if (useCustomUnderlines) { 788 if (useCustomUnderlines) {
771 const Vector<CompositionUnderline>& underlines = renderer().frame()- >inputMethodController().customCompositionUnderlines(); 789 const Vector<CompositionUnderline>& underlines = renderer().frame()- >inputMethodController().customCompositionUnderlines();
772 size_t numUnderlines = underlines.size(); 790 CompositionUnderlineRangeFilter filter(underlines, start(), end());
773 791 for (CompositionUnderlineRangeFilter::ConstIterator it = filter.begi n(); it != filter.end(); ++it) {
774 for (size_t index = 0; index < numUnderlines; ++index) { 792 paintCompositionUnderline(context, boxOrigin, *it);
775 const CompositionUnderline& underline = underlines[index];
776
777 if (underline.endOffset <= start())
778 // underline is completely before this run. This might be a n underline that sits
779 // before the first run we draw, or underlines that were wit hin runs we skipped
780 // due to truncation.
781 continue;
782
783 if (underline.startOffset <= end()) {
784 // underline intersects this run. Paint it.
785 paintCompositionUnderline(context, boxOrigin, underline);
786 if (underline.endOffset > end() + 1)
787 // underline also runs into the next run. Bail now, no m ore marker advancement.
788 break;
789 } else
790 // underline is completely after this run, bail. A later ru n will paint it.
791 break;
792 } 793 }
793 } 794 }
794 } 795 }
795 796
796 if (shouldRotate) 797 if (shouldRotate)
797 context->concatCTM(rotation(boxRect, Counterclockwise)); 798 context->concatCTM(rotation(boxRect, Counterclockwise));
798 } 799 }
799 800
800 void InlineTextBox::selectionStartEnd(int& sPos, int& ePos) 801 void InlineTextBox::selectionStartEnd(int& sPos, int& ePos)
801 { 802 {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
867 868
868 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY); 869 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY);
869 FloatRect clipRect(localOrigin, FloatSize(m_logicalWidth, selHeight)); 870 FloatRect clipRect(localOrigin, FloatSize(m_logicalWidth, selHeight));
870 alignSelectionRectToDevicePixels(clipRect); 871 alignSelectionRectToDevicePixels(clipRect);
871 872
872 context->clip(clipRect); 873 context->clip(clipRect);
873 874
874 context->drawHighlightForText(font, textRun, localOrigin, selHeight, c, sPos , ePos); 875 context->drawHighlightForText(font, textRun, localOrigin, selHeight, c, sPos , ePos);
875 } 876 }
876 877
877 void InlineTextBox::paintCompositionBackground(GraphicsContext* context, const F loatPoint& boxOrigin, RenderStyle* style, const Font& font, int startPos, int en dPos) 878 unsigned InlineTextBox::underlinePaintStart(const CompositionUnderline& underlin e)
878 { 879 {
879 int offset = m_start; 880 return std::max(static_cast<unsigned>(m_start), underline.startOffset);
880 int sPos = max(startPos - offset, 0); 881 }
881 int ePos = min(endPos - offset, (int)m_len);
882 882
883 unsigned InlineTextBox::underlinePaintEnd(const CompositionUnderline& underline)
884 {
885 unsigned paintEnd = std::min(end() + 1, underline.endOffset); // end() point s at the last char, not past it.
886 if (m_truncation != cNoTruncation)
887 paintEnd = std::min(paintEnd, static_cast<unsigned>(m_start + m_truncati on));
888 return paintEnd;
889 }
890
891 void InlineTextBox::paintCompositionBackground(GraphicsContext* context, const F loatPoint& boxOrigin, RenderStyle* style, const Font& font, Color backgroundColo r, int startPos, int endPos)
892 {
893 int sPos = std::max(startPos - m_start, 0);
894 int ePos = std::min(endPos - m_start, static_cast<int>(m_len));
883 if (sPos >= ePos) 895 if (sPos >= ePos)
884 return; 896 return;
885 897
886 GraphicsContextStateSaver stateSaver(*context); 898 GraphicsContextStateSaver stateSaver(*context);
887 899
888 Color c = Color(225, 221, 85); 900 updateGraphicsContext(context, backgroundColor, backgroundColor, 0); // Don' t draw text at all!
889
890 updateGraphicsContext(context, c, c, 0); // Don't draw text at all!
891 901
892 int deltaY = renderer().style()->isFlippedLinesWritingMode() ? selectionBott om() - logicalBottom() : logicalTop() - selectionTop(); 902 int deltaY = renderer().style()->isFlippedLinesWritingMode() ? selectionBott om() - logicalBottom() : logicalTop() - selectionTop();
893 int selHeight = selectionHeight(); 903 int selHeight = selectionHeight();
894 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY); 904 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY);
895 context->drawHighlightForText(font, constructTextRun(style, font), localOrig in, selHeight, c, sPos, ePos); 905 context->drawHighlightForText(font, constructTextRun(style, font), localOrig in, selHeight, backgroundColor, sPos, ePos);
896 } 906 }
897 907
898 static StrokeStyle textDecorationStyleToStrokeStyle(TextDecorationStyle decorati onStyle) 908 static StrokeStyle textDecorationStyleToStrokeStyle(TextDecorationStyle decorati onStyle)
899 { 909 {
900 StrokeStyle strokeStyle = SolidStroke; 910 StrokeStyle strokeStyle = SolidStroke;
901 switch (decorationStyle) { 911 switch (decorationStyle) {
902 case TextDecorationStyleSolid: 912 case TextDecorationStyleSolid:
903 strokeStyle = SolidStroke; 913 strokeStyle = SolidStroke;
904 break; 914 break;
905 case TextDecorationStyleDouble: 915 case TextDecorationStyleDouble:
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
1137 1147
1138 RenderStyle* styleToUse = renderer().style(isFirstLineStyle()); 1148 RenderStyle* styleToUse = renderer().style(isFirstLineStyle());
1139 int baseline = styleToUse->fontMetrics().ascent(); 1149 int baseline = styleToUse->fontMetrics().ascent();
1140 1150
1141 size_t shadowCount = shadowList ? shadowList->shadows().size() : 0; 1151 size_t shadowCount = shadowList ? shadowList->shadows().size() : 0;
1142 // Set the thick of the line to be 10% (or something else ?)of the computed font size and not less than 1px. 1152 // Set the thick of the line to be 10% (or something else ?)of the computed font size and not less than 1px.
1143 // Using computedFontSize should take care of zoom as well. 1153 // Using computedFontSize should take care of zoom as well.
1144 1154
1145 // Update Underline thickness, in case we have Faulty Font Metrics calculati ng underline thickness by old method. 1155 // Update Underline thickness, in case we have Faulty Font Metrics calculati ng underline thickness by old method.
1146 float textDecorationThickness = styleToUse->fontMetrics().underlineThickness (); 1156 float textDecorationThickness = styleToUse->fontMetrics().underlineThickness ();
1147 int fontHeightInt = (int)(styleToUse->fontMetrics().floatHeight() + 0.5); 1157 int fontHeightInt = (int)(styleToUse->fontMetrics().floatHeight() + 0.5);
yosin_UTC9 2014/06/13 04:08:26 super nit: If you have chance to update this patch
huangs 2014/06/13 17:22:24 Done.
1148 if ((textDecorationThickness == 0.f) || (textDecorationThickness >= (fontHei ghtInt >> 1))) 1158 if ((textDecorationThickness == 0.f) || (textDecorationThickness >= (fontHei ghtInt >> 1)))
1149 textDecorationThickness = std::max(1.f, styleToUse->computedFontSize() / 10.f); 1159 textDecorationThickness = std::max(1.f, styleToUse->computedFontSize() / 10.f);
1150 1160
1151 context->setStrokeThickness(textDecorationThickness); 1161 context->setStrokeThickness(textDecorationThickness);
1152 1162
1153 bool antialiasDecoration = shouldSetDecorationAntialias(overline.style, unde rline.style, linethrough.style) 1163 bool antialiasDecoration = shouldSetDecorationAntialias(overline.style, unde rline.style, linethrough.style)
1154 && RenderBoxModelObject::shouldAntialiasLines(context); 1164 && RenderBoxModelObject::shouldAntialiasLines(context);
1155 1165
1156 float extraOffset = 0; 1166 float extraOffset = 0;
1157 if (!linesAreOpaque && shadowCount > 1) { 1167 if (!linesAreOpaque && shadowCount > 1) {
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
1364 } 1374 }
1365 1375
1366 } 1376 }
1367 } 1377 }
1368 1378
1369 void InlineTextBox::paintCompositionUnderline(GraphicsContext* ctx, const FloatP oint& boxOrigin, const CompositionUnderline& underline) 1379 void InlineTextBox::paintCompositionUnderline(GraphicsContext* ctx, const FloatP oint& boxOrigin, const CompositionUnderline& underline)
1370 { 1380 {
1371 if (m_truncation == cFullTruncation) 1381 if (m_truncation == cFullTruncation)
1372 return; 1382 return;
1373 1383
1374 float start = 0; // start of line to draw, relative to tx 1384 unsigned paintStart = underlinePaintStart(underline);
1375 float width = m_logicalWidth; // how much line to draw 1385 unsigned paintEnd = underlinePaintEnd(underline);
1376 bool useWholeWidth = true; 1386
1377 unsigned paintStart = m_start; 1387 // start of line to draw, relative to tx
1378 unsigned paintEnd = end() + 1; // end points at the last char, not past it 1388 float start = paintStart == static_cast<unsigned>(m_start) ? 0 :
1379 if (paintStart <= underline.startOffset) { 1389 toRenderText(renderer()).width(m_start, paintStart - m_start, textPos(), isLeftToRightDirection() ? LTR : RTL, isFirstLineStyle());
1380 paintStart = underline.startOffset; 1390 // how much line to draw
1381 useWholeWidth = false; 1391 float width = (paintStart == static_cast<unsigned>(m_start) && paintEnd == s tatic_cast<unsigned>(end()) + 1) ? m_logicalWidth :
1382 start = toRenderText(renderer()).width(m_start, paintStart - m_start, te xtPos(), isLeftToRightDirection() ? LTR : RTL, isFirstLineStyle()); 1392 toRenderText(renderer()).width(paintStart, paintEnd - paintStart, textPo s() + start, isLeftToRightDirection() ? LTR : RTL, isFirstLineStyle());
1383 }
1384 if (paintEnd != underline.endOffset) { // end points at the last char, not past it
1385 paintEnd = min(paintEnd, (unsigned)underline.endOffset);
1386 useWholeWidth = false;
1387 }
1388 if (m_truncation != cNoTruncation) {
1389 paintEnd = min(paintEnd, (unsigned)m_start + m_truncation);
1390 useWholeWidth = false;
1391 }
1392 if (!useWholeWidth) {
1393 width = toRenderText(renderer()).width(paintStart, paintEnd - paintStart , textPos() + start, isLeftToRightDirection() ? LTR : RTL, isFirstLineStyle());
1394 }
1395 1393
1396 // Thick marked text underlines are 2px thick as long as there is room for t he 2px line under the baseline. 1394 // Thick marked text underlines are 2px thick as long as there is room for t he 2px line under the baseline.
1397 // All other marked text underlines are 1px thick. 1395 // All other marked text underlines are 1px thick.
1398 // If there's not enough space the underline will touch or overlap character s. 1396 // If there's not enough space the underline will touch or overlap character s.
1399 int lineThickness = 1; 1397 int lineThickness = 1;
1400 int baseline = renderer().style(isFirstLineStyle())->fontMetrics().ascent(); 1398 int baseline = renderer().style(isFirstLineStyle())->fontMetrics().ascent();
1401 if (underline.thick && logicalHeight() - baseline >= 2) 1399 if (underline.thick && logicalHeight() - baseline >= 2)
1402 lineThickness = 2; 1400 lineThickness = 2;
1403 1401
1404 // We need to have some space between underlines of subsequent clauses, beca use some input methods do not use different underline styles for those. 1402 // We need to have some space between underlines of subsequent clauses, beca use some input methods do not use different underline styles for those.
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
1579 printedCharacters = fprintf(stderr, "\t%s %p", obj.renderName(), &obj); 1577 printedCharacters = fprintf(stderr, "\t%s %p", obj.renderName(), &obj);
1580 const int rendererCharacterOffset = 24; 1578 const int rendererCharacterOffset = 24;
1581 for (; printedCharacters < rendererCharacterOffset; printedCharacters++) 1579 for (; printedCharacters < rendererCharacterOffset; printedCharacters++)
1582 fputc(' ', stderr); 1580 fputc(' ', stderr);
1583 fprintf(stderr, "(%d,%d) \"%s\"\n", start(), start() + len(), value.utf8().d ata()); 1581 fprintf(stderr, "(%d,%d) \"%s\"\n", start(), start() + len(), value.utf8().d ata());
1584 } 1582 }
1585 1583
1586 #endif 1584 #endif
1587 1585
1588 } // namespace WebCore 1586 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698