1 // Copyright 2019 Google LLC.
2
3 #include "include/core/SkBlurTypes.h"
4 #include "include/core/SkMaskFilter.h"
5 #include "include/effects/SkDashPathEffect.h"
6 #include "include/effects/SkDiscretePathEffect.h"
7 #include "modules/skparagraph/src/ParagraphPainterImpl.h"
8
9 #include <array>
10
11 namespace skia {
12 namespace textlayout {
13
DashPathEffect(SkScalar onLength,SkScalar offLength)14 ParagraphPainter::DashPathEffect::DashPathEffect(SkScalar onLength, SkScalar offLength)
15 : fOnLength(onLength), fOffLength(offLength) {}
16
DecorationStyle()17 ParagraphPainter::DecorationStyle::DecorationStyle()
18 : ParagraphPainter::DecorationStyle(SK_ColorTRANSPARENT, 0, std::nullopt) {}
19
DecorationStyle(SkColor color,SkScalar strokeWidth,std::optional<DashPathEffect> dashPathEffect)20 ParagraphPainter::DecorationStyle::DecorationStyle(
21 SkColor color, SkScalar strokeWidth,
22 std::optional<DashPathEffect> dashPathEffect)
23 : fColor(color), fStrokeWidth(strokeWidth), fDashPathEffect(dashPathEffect) {
24 fPaint.setStyle(SkPaint::kStroke_Style);
25 fPaint.setAntiAlias(true);
26 fPaint.setColor(fColor);
27 fPaint.setStrokeWidth(fStrokeWidth);
28
29 if (fDashPathEffect) {
30 const std::array<SkScalar, 4> intervals =
31 {fDashPathEffect->fOnLength, fDashPathEffect->fOffLength,
32 fDashPathEffect->fOnLength, fDashPathEffect->fOffLength};
33 fPaint.setPathEffect(SkPathEffect::MakeCompose(
34 SkDashPathEffect::Make(intervals.data(), intervals.size(), 0.0f),
35 SkDiscretePathEffect::Make(0, 0)));
36 }
37 }
38
CanvasParagraphPainter(SkCanvas * canvas)39 CanvasParagraphPainter::CanvasParagraphPainter(SkCanvas* canvas)
40 : fCanvas(canvas) {}
41
drawTextBlob(const sk_sp<SkTextBlob> & blob,SkScalar x,SkScalar y,const SkPaintOrID & paint)42 void CanvasParagraphPainter::drawTextBlob(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, const SkPaintOrID& paint) {
43 SkASSERT(std::holds_alternative<SkPaint>(paint));
44 fCanvas->drawTextBlob(blob, x, y, std::get<SkPaint>(paint));
45 }
46
drawTextShadow(const sk_sp<SkTextBlob> & blob,SkScalar x,SkScalar y,SkColor color,SkScalar blurSigma)47 void CanvasParagraphPainter::drawTextShadow(const sk_sp<SkTextBlob>& blob, SkScalar x, SkScalar y, SkColor color, SkScalar blurSigma) {
48 SkPaint paint;
49 paint.setColor(color);
50 if (blurSigma != 0.0) {
51 sk_sp<SkMaskFilter> filter = SkMaskFilter::MakeBlur(
52 kNormal_SkBlurStyle, blurSigma, false);
53 paint.setMaskFilter(filter);
54 }
55 fCanvas->drawTextBlob(blob, x, y, paint);
56 }
57
drawRect(const SkRect & rect,const SkPaintOrID & paint)58 void CanvasParagraphPainter::drawRect(const SkRect& rect, const SkPaintOrID& paint) {
59 SkASSERT(std::holds_alternative<SkPaint>(paint));
60 fCanvas->drawRect(rect, std::get<SkPaint>(paint));
61 }
62
drawRRect(const SkRRect & rrect,const SkColor color)63 void CanvasParagraphPainter::drawRRect(const SkRRect& rrect, const SkColor color) {
64 SkPaint paint;
65 paint.setColor(color);
66 paint.setAntiAlias(false);
67 fCanvas->drawRRect(rrect, paint);
68 }
69
drawFilledRect(const SkRect & rect,const DecorationStyle & decorStyle)70 void CanvasParagraphPainter::drawFilledRect(const SkRect& rect, const DecorationStyle& decorStyle) {
71 SkPaint p(decorStyle.skPaint());
72 p.setStroke(false);
73 fCanvas->drawRect(rect, p);
74 }
75
drawPath(const SkPath & path,const DecorationStyle & decorStyle)76 void CanvasParagraphPainter::drawPath(const SkPath& path, const DecorationStyle& decorStyle) {
77 fCanvas->drawPath(path, decorStyle.skPaint());
78 }
79
drawLine(SkScalar x0,SkScalar y0,SkScalar x1,SkScalar y1,const DecorationStyle & decorStyle)80 void CanvasParagraphPainter::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const DecorationStyle& decorStyle) {
81 fCanvas->drawLine(x0, y0, x1, y1, decorStyle.skPaint());
82 }
83
clipRect(const SkRect & rect)84 void CanvasParagraphPainter::clipRect(const SkRect& rect) {
85 fCanvas->clipRect(rect);
86 }
87
translate(SkScalar dx,SkScalar dy)88 void CanvasParagraphPainter::translate(SkScalar dx, SkScalar dy) {
89 fCanvas->translate(dx, dy);
90 }
91
save()92 void CanvasParagraphPainter::save() {
93 fCanvas->save();
94 }
95
restore()96 void CanvasParagraphPainter::restore() {
97 fCanvas->restore();
98 }
99
100 } // namespace textlayout
101 } // namespace skia
102