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|. Note |text_bounds_| is
58 // the 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 if (selection_rect.Contains(text_bounds_)) {
62 Paint(start_offset, end_offset, length, selection_style, node_id);
63 return;
64 }
65
66 // Adjust start/end offset when they are in the middle of a ligature. e.g.,
67 // when |start_offset| is between a ligature of "fi", it needs to be adjusted
68 // to before "f".
69 fragment_paint_info_.shape_result->ExpandRangeToIncludePartialGlyphs(
70 &start_offset, &end_offset);
71
72 // Because only a part of the text glyph can be selected, we need to draw
73 // the selection twice. First, draw the glyphs outside the selection area,
74 // with the original style.
75 FloatRect float_selection_rect(selection_rect);
76 {
77 GraphicsContextStateSaver state_saver(graphics_context_);
78 graphics_context_.ClipOut(float_selection_rect);
79 Paint(start_offset, end_offset, length, text_style, node_id);
80 }
81 // Then draw the glyphs inside the selection area, with the selection style.
82 {
83 GraphicsContextStateSaver state_saver(graphics_context_);
84 graphics_context_.Clip(float_selection_rect);
85 Paint(start_offset, end_offset, length, selection_style, node_id);
86 }
87 }
88
89 template <NGTextPainter::PaintInternalStep step>
PaintInternalFragment(unsigned from,unsigned to,DOMNodeId node_id)90 void NGTextPainter::PaintInternalFragment(
91 unsigned from,
92 unsigned to,
93 DOMNodeId node_id) {
94 DCHECK(from <= fragment_paint_info_.text.length());
95 DCHECK(to <= fragment_paint_info_.text.length());
96
97 fragment_paint_info_.from = from;
98 fragment_paint_info_.to = to;
99
100 if (step == kPaintEmphasisMark) {
101 graphics_context_.DrawEmphasisMarks(
102 font_, fragment_paint_info_, emphasis_mark_,
103 FloatPoint(text_origin_) + IntSize(0, emphasis_mark_offset_));
104 } else {
105 DCHECK(step == kPaintText);
106 graphics_context_.DrawText(font_, fragment_paint_info_,
107 FloatPoint(text_origin_), node_id);
108 // TODO(npm): Check that there are non-whitespace characters. See
109 // crbug.com/788444.
110 graphics_context_.GetPaintController().SetTextPainted();
111
112 if (!font_.ShouldSkipDrawing())
113 PaintTimingDetector::NotifyTextPaint(visual_rect_);
114 }
115 }
116
117 template <NGTextPainter::PaintInternalStep Step>
PaintInternal(unsigned start_offset,unsigned end_offset,unsigned truncation_point,DOMNodeId node_id)118 void NGTextPainter::PaintInternal(unsigned start_offset,
119 unsigned end_offset,
120 unsigned truncation_point,
121 DOMNodeId node_id) {
122 // TODO(layout-dev): We shouldn't be creating text fragments without text.
123 if (!fragment_paint_info_.shape_result)
124 return;
125
126 if (start_offset <= end_offset) {
127 PaintInternalFragment<Step>(start_offset, end_offset, node_id);
128 } else {
129 if (end_offset > 0) {
130 PaintInternalFragment<Step>(ellipsis_offset_, end_offset, node_id);
131 }
132 if (start_offset < truncation_point) {
133 PaintInternalFragment<Step>(start_offset, truncation_point, node_id);
134 }
135 }
136 }
137
ClipDecorationsStripe(float upper,float stripe_width,float dilation)138 void NGTextPainter::ClipDecorationsStripe(float upper,
139 float stripe_width,
140 float dilation) {
141 if (fragment_paint_info_.from >= fragment_paint_info_.to ||
142 !fragment_paint_info_.shape_result)
143 return;
144
145 Vector<Font::TextIntercept> text_intercepts;
146 font_.GetTextIntercepts(
147 fragment_paint_info_, graphics_context_.DeviceScaleFactor(),
148 graphics_context_.FillFlags(),
149 std::make_tuple(upper, upper + stripe_width), text_intercepts);
150
151 DecorationsStripeIntercepts(upper, stripe_width, dilation, text_intercepts);
152 }
153
PaintEmphasisMarkForCombinedText()154 void NGTextPainter::PaintEmphasisMarkForCombinedText() {}
155
156 } // namespace blink
157