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

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: Style fixes; early exit for drawing transparent underlines. 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
« no previous file with comments | « Source/core/rendering/InlineTextBox.h ('k') | Source/core/rendering/RenderTheme.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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) {
leviw_travelin_and_unemployed 2014/06/17 19:09:56 How about extracting this whole if/else block into
huangs 2014/06/17 21:23:27 Done. The new function is InlineTextBox::paintC
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)
651 continue;
652 unsigned paintStart = underlinePaintStart(*it);
653 unsigned paintEnd = underlinePaintEnd(*it);
654 paintCompositionBackground(context, boxOrigin, styleToUse, f ont, it->backgroundColor, paintStart, paintEnd);
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 if (it->color == Color::transparent)
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; 793 continue;
782 794 paintCompositionUnderline(context, boxOrigin, *it);
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 } 795 }
793 } 796 }
794 } 797 }
795 798
796 if (shouldRotate) 799 if (shouldRotate)
797 context->concatCTM(rotation(boxRect, Counterclockwise)); 800 context->concatCTM(rotation(boxRect, Counterclockwise));
798 } 801 }
799 802
800 void InlineTextBox::selectionStartEnd(int& sPos, int& ePos) 803 void InlineTextBox::selectionStartEnd(int& sPos, int& ePos)
801 { 804 {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
867 870
868 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY); 871 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY);
869 FloatRect clipRect(localOrigin, FloatSize(m_logicalWidth, selHeight)); 872 FloatRect clipRect(localOrigin, FloatSize(m_logicalWidth, selHeight));
870 alignSelectionRectToDevicePixels(clipRect); 873 alignSelectionRectToDevicePixels(clipRect);
871 874
872 context->clip(clipRect); 875 context->clip(clipRect);
873 876
874 context->drawHighlightForText(font, textRun, localOrigin, selHeight, c, sPos , ePos); 877 context->drawHighlightForText(font, textRun, localOrigin, selHeight, c, sPos , ePos);
875 } 878 }
876 879
877 void InlineTextBox::paintCompositionBackground(GraphicsContext* context, const F loatPoint& boxOrigin, RenderStyle* style, const Font& font, int startPos, int en dPos) 880 unsigned InlineTextBox::underlinePaintStart(const CompositionUnderline& underlin e)
878 { 881 {
879 int offset = m_start; 882 return std::max(static_cast<unsigned>(m_start), underline.startOffset);
880 int sPos = max(startPos - offset, 0); 883 }
881 int ePos = min(endPos - offset, (int)m_len);
882 884
885 unsigned InlineTextBox::underlinePaintEnd(const CompositionUnderline& underline)
886 {
887 unsigned paintEnd = std::min(end() + 1, underline.endOffset); // end() point s at the last char, not past it.
888 if (m_truncation != cNoTruncation)
889 paintEnd = std::min(paintEnd, static_cast<unsigned>(m_start + m_truncati on));
890 return paintEnd;
891 }
892
893 void InlineTextBox::paintCompositionBackground(GraphicsContext* context, const F loatPoint& boxOrigin, RenderStyle* style, const Font& font, Color backgroundColo r, int startPos, int endPos)
894 {
895 int sPos = std::max(startPos - m_start, 0);
896 int ePos = std::min(endPos - m_start, static_cast<int>(m_len));
883 if (sPos >= ePos) 897 if (sPos >= ePos)
884 return; 898 return;
885 899
886 GraphicsContextStateSaver stateSaver(*context); 900 GraphicsContextStateSaver stateSaver(*context);
887 901
888 Color c = Color(225, 221, 85); 902 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 903
892 int deltaY = renderer().style()->isFlippedLinesWritingMode() ? selectionBott om() - logicalBottom() : logicalTop() - selectionTop(); 904 int deltaY = renderer().style()->isFlippedLinesWritingMode() ? selectionBott om() - logicalBottom() : logicalTop() - selectionTop();
893 int selHeight = selectionHeight(); 905 int selHeight = selectionHeight();
894 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY); 906 FloatPoint localOrigin(boxOrigin.x(), boxOrigin.y() - deltaY);
895 context->drawHighlightForText(font, constructTextRun(style, font), localOrig in, selHeight, c, sPos, ePos); 907 context->drawHighlightForText(font, constructTextRun(style, font), localOrig in, selHeight, backgroundColor, sPos, ePos);
896 } 908 }
897 909
898 static StrokeStyle textDecorationStyleToStrokeStyle(TextDecorationStyle decorati onStyle) 910 static StrokeStyle textDecorationStyleToStrokeStyle(TextDecorationStyle decorati onStyle)
899 { 911 {
900 StrokeStyle strokeStyle = SolidStroke; 912 StrokeStyle strokeStyle = SolidStroke;
901 switch (decorationStyle) { 913 switch (decorationStyle) {
902 case TextDecorationStyleSolid: 914 case TextDecorationStyleSolid:
903 strokeStyle = SolidStroke; 915 strokeStyle = SolidStroke;
904 break; 916 break;
905 case TextDecorationStyleDouble: 917 case TextDecorationStyleDouble:
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
1364 } 1376 }
1365 1377
1366 } 1378 }
1367 } 1379 }
1368 1380
1369 void InlineTextBox::paintCompositionUnderline(GraphicsContext* ctx, const FloatP oint& boxOrigin, const CompositionUnderline& underline) 1381 void InlineTextBox::paintCompositionUnderline(GraphicsContext* ctx, const FloatP oint& boxOrigin, const CompositionUnderline& underline)
1370 { 1382 {
1371 if (m_truncation == cFullTruncation) 1383 if (m_truncation == cFullTruncation)
1372 return; 1384 return;
1373 1385
1374 float start = 0; // start of line to draw, relative to tx 1386 unsigned paintStart = underlinePaintStart(underline);
1375 float width = m_logicalWidth; // how much line to draw 1387 unsigned paintEnd = underlinePaintEnd(underline);
1376 bool useWholeWidth = true; 1388
1377 unsigned paintStart = m_start; 1389 // start of line to draw, relative to tx
leviw_travelin_and_unemployed 2014/06/17 19:09:56 Can you update this comment? tx has been dead for
huangs 2014/06/17 21:23:27 Done.
1378 unsigned paintEnd = end() + 1; // end points at the last char, not past it 1390 float start = paintStart == static_cast<unsigned>(m_start) ? 0 :
1379 if (paintStart <= underline.startOffset) { 1391 toRenderText(renderer()).width(m_start, paintStart - m_start, textPos(), isLeftToRightDirection() ? LTR : RTL, isFirstLineStyle());
1380 paintStart = underline.startOffset; 1392 // how much line to draw
1381 useWholeWidth = false; 1393 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()); 1394 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 1395
1396 // Thick marked text underlines are 2px thick as long as there is room for t he 2px line under the baseline. 1396 // 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. 1397 // All other marked text underlines are 1px thick.
1398 // If there's not enough space the underline will touch or overlap character s. 1398 // If there's not enough space the underline will touch or overlap character s.
1399 int lineThickness = 1; 1399 int lineThickness = 1;
1400 int baseline = renderer().style(isFirstLineStyle())->fontMetrics().ascent(); 1400 int baseline = renderer().style(isFirstLineStyle())->fontMetrics().ascent();
1401 if (underline.thick && logicalHeight() - baseline >= 2) 1401 if (underline.thick && logicalHeight() - baseline >= 2)
1402 lineThickness = 2; 1402 lineThickness = 2;
1403 1403
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. 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.
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
1579 printedCharacters = fprintf(stderr, "\t%s %p", obj.renderName(), &obj); 1579 printedCharacters = fprintf(stderr, "\t%s %p", obj.renderName(), &obj);
1580 const int rendererCharacterOffset = 24; 1580 const int rendererCharacterOffset = 24;
1581 for (; printedCharacters < rendererCharacterOffset; printedCharacters++) 1581 for (; printedCharacters < rendererCharacterOffset; printedCharacters++)
1582 fputc(' ', stderr); 1582 fputc(' ', stderr);
1583 fprintf(stderr, "(%d,%d) \"%s\"\n", start(), start() + len(), value.utf8().d ata()); 1583 fprintf(stderr, "(%d,%d) \"%s\"\n", start(), start() + len(), value.utf8().d ata());
1584 } 1584 }
1585 1585
1586 #endif 1586 #endif
1587 1587
1588 } // namespace WebCore 1588 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/rendering/InlineTextBox.h ('k') | Source/core/rendering/RenderTheme.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698