1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/core/paint/ng/ng_text_painter.h"
6 
7 #include "third_party/blink/renderer/core/css/css_property_names.h"
8 #include "third_party/blink/renderer/core/frame/settings.h"
9 #include "third_party/blink/renderer/core/layout/ng/inline/ng_physical_text_fragment.h"
10 #include "third_party/blink/renderer/core/layout/ng/ng_unpositioned_float.h"
11 #include "third_party/blink/renderer/core/paint/box_painter.h"
12 #include "third_party/blink/renderer/core/paint/paint_info.h"
13 #include "third_party/blink/renderer/core/paint/paint_timing_detector.h"
14 #include "third_party/blink/renderer/core/style/computed_style.h"
15 #include "third_party/blink/renderer/core/style/shadow_list.h"
16 #include "third_party/blink/renderer/platform/fonts/font.h"
17 #include "third_party/blink/renderer/platform/graphics/graphics_context.h"
18 #include "third_party/blink/renderer/platform/graphics/graphics_context_state_saver.h"
19 #include "third_party/blink/renderer/platform/graphics/paint/paint_controller.h"
20 #include "third_party/blink/renderer/platform/wtf/assertions.h"
21 #include "third_party/blink/renderer/platform/wtf/text/character_names.h"
22 
23 namespace blink {
24 
Paint(unsigned start_offset,unsigned end_offset,unsigned length,const TextPaintStyle & text_style,DOMNodeId node_id)25 void NGTextPainter::Paint(unsigned start_offset,
26                           unsigned end_offset,
27                           unsigned length,
28                           const TextPaintStyle& text_style,
29                           DOMNodeId node_id) {
30   GraphicsContextStateSaver state_saver(graphics_context_, false);
31   UpdateGraphicsContext(text_style, state_saver);
32   // TODO(layout-dev): Handle combine text here or elsewhere.
33   PaintInternal<kPaintText>(start_offset, end_offset, length, node_id);
34 
35   if (!emphasis_mark_.IsEmpty()) {
36     if (text_style.emphasis_mark_color != text_style.fill_color)
37       graphics_context_.SetFillColor(text_style.emphasis_mark_color);
38     PaintInternal<kPaintEmphasisMark>(start_offset, end_offset, length,
39                                       node_id);
40   }
41 }
42 
43 // This function paints text twice with different styles in order to:
44 // 1. Paint glyphs inside of |selection_rect| using |selection_style|, and
45 //    outside using |text_style|.
46 // 2. Paint parts of a ligature glyph.
PaintSelectedText(unsigned start_offset,unsigned end_offset,unsigned length,const TextPaintStyle & text_style,const TextPaintStyle & selection_style,const PhysicalRect & selection_rect,DOMNodeId node_id)47 void NGTextPainter::PaintSelectedText(unsigned start_offset,
48                                       unsigned end_offset,
49                                       unsigned length,
50                                       const TextPaintStyle& text_style,
51                                       const TextPaintStyle& selection_style,
52                                       const PhysicalRect& selection_rect,
53                                       DOMNodeId node_id) {
54   if (!fragment_paint_info_.shape_result)
55     return;
56 
57   // Use fast path if all glyphs fit in |selection_rect|. |visual_rect_| is the
58   // ink bounds of all glyphs of this text fragment, including characters before
59   // |start_offset| or after |end_offset|. Computing exact bounds is expensive
60   // that this code only checks bounds of all glyphs.
61   IntRect snapped_selection_rect(PixelSnappedIntRect(selection_rect));
62   // Allowing 1px overflow is almost unnoticeable, while it can avoid two-pass
63   // painting in most small text.
64   snapped_selection_rect.Inflate(1);
65   if (snapped_selection_rect.Contains(visual_rect_)) {
66     Paint(start_offset, end_offset, length, selection_style, node_id);
67     return;
68   }
69 
70   // Adjust start/end offset when they are in the middle of a ligature. e.g.,
71   // when |start_offset| is between a ligature of "fi", it needs to be adjusted
72   // to before "f".
73   fragment_paint_info_.shape_result->ExpandRangeToIncludePartialGlyphs(
74       &start_offset, &end_offset);
75 
76   // Because only a part of the text glyph can be selected, we need to draw
77   // the selection twice. First, draw the glyphs outside the selection area,
78   // with the original style.
79   FloatRect float_selection_rect(selection_rect);
80   {
81     GraphicsContextStateSaver state_saver(graphics_context_);
82     graphics_context_.ClipOut(float_selection_rect);
83     Paint(start_offset, end_offset, length, text_style, node_id);
84   }
85   // Then draw the glyphs inside the selection area, with the selection style.
86   {
87     GraphicsContextStateSaver state_saver(graphics_context_);
88     graphics_context_.Clip(float_selection_rect);
89     Paint(start_offset, end_offset, length, selection_style, node_id);
90   }
91 }
92 
93 template <NGTextPainter::PaintInternalStep step>
PaintInternalFragment(unsigned from,unsigned to,DOMNodeId node_id)94 void NGTextPainter::PaintInternalFragment(
95     unsigned from,
96     unsigned to,
97     DOMNodeId node_id) {
98   DCHECK(from <= fragment_paint_info_.text.length());
99   DCHECK(to <= fragment_paint_info_.text.length());
100 
101   fragment_paint_info_.from = from;
102   fragment_paint_info_.to = to;
103 
104   if (step == kPaintEmphasisMark) {
105     graphics_context_.DrawEmphasisMarks(
106         font_, fragment_paint_info_, emphasis_mark_,
107         FloatPoint(text_origin_) + IntSize(0, emphasis_mark_offset_));
108   } else {
109     DCHECK(step == kPaintText);
110     graphics_context_.DrawText(font_, fragment_paint_info_,
111                                FloatPoint(text_origin_), node_id);
112     // TODO(npm): Check that there are non-whitespace characters. See
113     // crbug.com/788444.
114     graphics_context_.GetPaintController().SetTextPainted();
115 
116     if (!font_.ShouldSkipDrawing())
117       PaintTimingDetector::NotifyTextPaint(visual_rect_);
118   }
119 }
120 
121 template <NGTextPainter::PaintInternalStep Step>
PaintInternal(unsigned start_offset,unsigned end_offset,unsigned truncation_point,DOMNodeId node_id)122 void NGTextPainter::PaintInternal(unsigned start_offset,
123                                   unsigned end_offset,
124                                   unsigned truncation_point,
125                                   DOMNodeId node_id) {
126   // TODO(layout-dev): We shouldn't be creating text fragments without text.
127   if (!fragment_paint_info_.shape_result)
128     return;
129 
130   if (start_offset <= end_offset) {
131     PaintInternalFragment<Step>(start_offset, end_offset, node_id);
132   } else {
133     if (end_offset > 0) {
134       PaintInternalFragment<Step>(ellipsis_offset_, end_offset, node_id);
135     }
136     if (start_offset < truncation_point) {
137       PaintInternalFragment<Step>(start_offset, truncation_point, node_id);
138     }
139   }
140 }
141 
ClipDecorationsStripe(float upper,float stripe_width,float dilation)142 void NGTextPainter::ClipDecorationsStripe(float upper,
143                                           float stripe_width,
144                                           float dilation) {
145   if (fragment_paint_info_.from >= fragment_paint_info_.to ||
146       !fragment_paint_info_.shape_result)
147     return;
148 
149   Vector<Font::TextIntercept> text_intercepts;
150   font_.GetTextIntercepts(
151       fragment_paint_info_, graphics_context_.DeviceScaleFactor(),
152       graphics_context_.FillFlags(),
153       std::make_tuple(upper, upper + stripe_width), text_intercepts);
154 
155   DecorationsStripeIntercepts(upper, stripe_width, dilation, text_intercepts);
156 }
157 
PaintEmphasisMarkForCombinedText()158 void NGTextPainter::PaintEmphasisMarkForCombinedText() {}
159 
160 }  // namespace blink
161