Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions
dashing.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkFont.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPathBuilder.h"
#include "include/core/SkPathEffect.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRect.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "include/effects/SkDashPathEffect.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"
#include <math.h>
#include <initializer_list>

Go to the source code of this file.

Classes

class  DashingGM
 
class  Dashing2GM
 
class  Dashing3GM
 
class  Dashing4GM
 
class  Dashing5GM
 

Functions

static void drawline (SkCanvas *canvas, int on, int off, const SkPaint &paint, SkScalar finalX=SkIntToScalar(600), SkScalar finalY=SkIntToScalar(0), SkScalar phase=SkIntToScalar(0), SkScalar startX=SkIntToScalar(0), SkScalar startY=SkIntToScalar(0))
 
static void show_giant_dash (SkCanvas *canvas)
 
static void show_zero_len_dash (SkCanvas *canvas)
 
static SkPath make_unit_star (int n)
 
static SkPath make_path_line (const SkRect &bounds)
 
static SkPath make_path_rect (const SkRect &bounds)
 
static SkPath make_path_oval (const SkRect &bounds)
 
static SkPath make_path_star (const SkRect &bounds)
 
 DEF_SIMPLE_GM (longpathdash, canvas, 612, 612)
 
 DEF_SIMPLE_GM (longlinedash, canvas, 512, 512)
 
 DEF_SIMPLE_GM (dashbigrects, canvas, 256, 256)
 
 DEF_SIMPLE_GM (longwavyline, canvas, 512, 512)
 
 DEF_SIMPLE_GM (dashtextcaps, canvas, 512, 512)
 
 DEF_SIMPLE_GM (dash_line_zero_off_interval, canvas, 160, 330)
 
 DEF_SIMPLE_GM (thin_aa_dash_lines, canvas, 330, 110)
 
 DEF_SIMPLE_GM (path_effect_empty_result, canvas, 100, 100)
 

Function Documentation

◆ DEF_SIMPLE_GM() [1/8]

DEF_SIMPLE_GM ( dash_line_zero_off_interval  ,
canvas  ,
160  ,
330   
)

Definition at line 585 of file dashing.cpp.

585 {
586 static constexpr SkScalar kIntervals[] = {5.f, 0.f, 2.f, 0.f};
587 SkPaint dashPaint;
588 dashPaint.setPathEffect(SkDashPathEffect::Make(kIntervals, std::size(kIntervals), 0.f));
589 SkASSERT(dashPaint.getPathEffect());
590 dashPaint.setStroke(true);
591 dashPaint.setStrokeWidth(20.f);
592 static constexpr struct {
593 SkPoint fA, fB;
594 } kLines[] = {{{0.5f, 0.5f}, {30.5f, 0.5f}}, // horizontal
595 {{0.5f, 0.5f}, {0.5f, 30.5f}}, // vertical
596 {{0.5f, 0.5f}, {0.5f, 0.5f}}, // point
597 {{0.5f, 0.5f}, {25.5f, 25.5f}}}; // diagonal
598 SkScalar pad = 5.f + dashPaint.getStrokeWidth();
599 canvas->translate(pad / 2.f, pad / 2.f);
600 canvas->save();
601 SkScalar h = 0.f;
602 for (const auto& line : kLines) {
603 h = std::max(h, SkScalarAbs(line.fA.fY - line.fB.fY));
604 }
605 for (const auto& line : kLines) {
606 SkScalar w = SkScalarAbs(line.fA.fX - line.fB.fX);
608 dashPaint.setStrokeCap(cap);
609 for (auto aa : {false, true}) {
610 dashPaint.setAntiAlias(aa);
611 canvas->drawLine(line.fA, line.fB, dashPaint);
612 canvas->translate(0.f, pad + h);
613 }
614 }
615 canvas->restore();
616 canvas->translate(pad + w, 0.f);
617 canvas->save();
618 }
619}
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkScalarAbs(x)
Definition SkScalar.h:39
static sk_sp< SkPathEffect > Make(const SkScalar intervals[], int count, SkScalar phase)
@ kRound_Cap
adds circle
Definition SkPaint.h:335
@ kButt_Cap
no stroke extension
Definition SkPaint.h:334
@ kSquare_Cap
adds square
Definition SkPaint.h:336
SkPathEffect * getPathEffect() const
Definition SkPaint.h:506
void setAntiAlias(bool aa)
Definition SkPaint.h:170
void setStrokeCap(Cap cap)
Definition SkPaint.cpp:179
void setPathEffect(sk_sp< SkPathEffect > pathEffect)
SkScalar getStrokeWidth() const
Definition SkPaint.h:300
void setStroke(bool)
Definition SkPaint.cpp:115
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
float SkScalar
Definition extension.cpp:12
SkScalar w
SkScalar h

◆ DEF_SIMPLE_GM() [2/8]

DEF_SIMPLE_GM ( dashbigrects  ,
canvas  ,
256  ,
256   
)

Definition at line 514 of file dashing.cpp.

514 {
515 SkRandom rand;
516
517 constexpr int kHalfStrokeWidth = 8;
518 constexpr int kOnOffInterval = 2*kHalfStrokeWidth;
519
520 canvas->clear(SkColors::kBlack);
521
522 SkPaint p;
523 p.setAntiAlias(true);
524 p.setStroke(true);
525 p.setStrokeWidth(2*kHalfStrokeWidth);
526 p.setStrokeCap(SkPaint::kButt_Cap);
527
528 constexpr SkScalar intervals[] = { kOnOffInterval, kOnOffInterval };
529 p.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), 0));
530
531 constexpr float gWidthHeights[] = {
532 1000000000.0f * kOnOffInterval + kOnOffInterval/2.0f,
533 1000000.0f * kOnOffInterval + kOnOffInterval/2.0f,
534 1000.0f * kOnOffInterval + kOnOffInterval/2.0f,
535 100.0f * kOnOffInterval + kOnOffInterval/2.0f,
536 10.0f * kOnOffInterval + kOnOffInterval/2.0f,
537 9.0f * kOnOffInterval + kOnOffInterval/2.0f,
538 8.0f * kOnOffInterval + kOnOffInterval/2.0f,
539 7.0f * kOnOffInterval + kOnOffInterval/2.0f,
540 6.0f * kOnOffInterval + kOnOffInterval/2.0f,
541 5.0f * kOnOffInterval + kOnOffInterval/2.0f,
542 4.0f * kOnOffInterval + kOnOffInterval/2.0f,
543 };
544
545 for (size_t i = 0; i < std::size(gWidthHeights); ++i) {
546 p.setColor(ToolUtils::color_to_565(rand.nextU() | (0xFF << 24)));
547
548 int offset = 2 * i * kHalfStrokeWidth + kHalfStrokeWidth;
549 canvas->drawRect(SkRect::MakeXYWH(offset, offset, gWidthHeights[i], gWidthHeights[i]), p);
550 }
551}
uint32_t nextU()
Definition SkRandom.h:42
constexpr SkColor4f kBlack
Definition SkColor.h:435
SkColor color_to_565(SkColor color)
Point offset
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659

◆ DEF_SIMPLE_GM() [3/8]

DEF_SIMPLE_GM ( dashtextcaps  ,
canvas  ,
512  ,
512   
)

Definition at line 568 of file dashing.cpp.

568 {
569 SkPaint p;
570 p.setAntiAlias(true);
571 p.setStroke(true);
572 p.setStrokeWidth(10);
573 p.setStrokeCap(SkPaint::kRound_Cap);
574 p.setStrokeJoin(SkPaint::kRound_Join);
575 p.setARGB(0xff, 0xbb, 0x00, 0x00);
576
578
579 const SkScalar intervals[] = { 12, 12 };
580 p.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), 0));
581 canvas->drawString("Sausages", 10, 90, font, p);
582 canvas->drawLine(8, 120, 456, 120, p);
583}
@ kRound_Join
adds circle
Definition SkPaint.h:360
sk_sp< SkTypeface > DefaultPortableTypeface()
font
Font Metadata and Metrics.

◆ DEF_SIMPLE_GM() [4/8]

DEF_SIMPLE_GM ( longlinedash  ,
canvas  ,
512  ,
512   
)

Definition at line 503 of file dashing.cpp.

503 {
504 SkPaint p;
505 p.setAntiAlias(true);
506 p.setStroke(true);
507 p.setStrokeWidth(80);
508
509 const SkScalar intervals[] = { 2, 2 };
510 p.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), 0));
511 canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p);
512}

◆ DEF_SIMPLE_GM() [5/8]

DEF_SIMPLE_GM ( longpathdash  ,
canvas  ,
612  ,
612   
)

Definition at line 475 of file dashing.cpp.

475 {
477 for (int x = 32; x < 256; x += 16) {
478 for (SkScalar a = 0; a < 3.141592f * 2; a += 0.03141592f) {
479 SkPoint pts[2] = {
480 { 256 + (float) sin(a) * x,
481 256 + (float) cos(a) * x },
482 { 256 + (float) sin(a + 3.141592 / 3) * (x + 64),
483 256 + (float) cos(a + 3.141592 / 3) * (x + 64) }
484 };
485 lines.moveTo(pts[0]);
486 for (SkScalar i = 0; i < 1; i += 0.05f) {
487 lines.lineTo(pts[0].fX * (1 - i) + pts[1].fX * i,
488 pts[0].fY * (1 - i) + pts[1].fY * i);
489 }
490 }
491 }
492 SkPaint p;
493 p.setAntiAlias(true);
494 p.setStroke(true);
495 p.setStrokeWidth(1);
496 const SkScalar intervals[] = { 1, 1 };
497 p.setPathEffect(SkDashPathEffect::Make(intervals, std::size(intervals), 0));
498
499 canvas->translate(50, 50);
500 canvas->drawPath(lines, p);
501}
struct MyStruct a[10]
double x
float fX
x-axis value
float fY
y-axis value

◆ DEF_SIMPLE_GM() [6/8]

DEF_SIMPLE_GM ( longwavyline  ,
canvas  ,
512  ,
512   
)

Definition at line 553 of file dashing.cpp.

553 {
554 SkPaint p;
555 p.setAntiAlias(true);
556 p.setStroke(true);
557 p.setStrokeWidth(2);
558
559 SkPath wavy;
560 wavy.moveTo(-10000, 100);
561 for (SkScalar i = -10000; i < 10000; i += 20) {
562 wavy.quadTo(i + 5, 95, i + 10, 100);
563 wavy.quadTo(i + 15, 105, i + 20, 100);
564 }
565 canvas->drawPath(wavy, p);
566}
SkPath & moveTo(SkScalar x, SkScalar y)
Definition SkPath.cpp:678
SkPath & quadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2)
Definition SkPath.cpp:736

◆ DEF_SIMPLE_GM() [7/8]

DEF_SIMPLE_GM ( path_effect_empty_result  ,
canvas  ,
100  ,
100   
)

Definition at line 644 of file dashing.cpp.

644 {
645 SkPaint p;
646 p.setStroke(true);
647 p.setStrokeWidth(1);
648
649 SkPath path;
650 float r = 70;
651 float l = 70;
652 float t = 70;
653 float b = 70;
654 path.moveTo(l, t);
655 path.lineTo(r, t);
656 path.lineTo(r, b);
657 path.lineTo(l, b);
658 path.close();
659
660 float dashes[] = {2.f, 2.f};
661 p.setPathEffect(SkDashPathEffect::Make(dashes, 2, 0.f));
662
663 canvas->drawPath(path, p);
664}
static bool b
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switches.h:57

◆ DEF_SIMPLE_GM() [8/8]

DEF_SIMPLE_GM ( thin_aa_dash_lines  ,
canvas  ,
330  ,
110   
)

Definition at line 621 of file dashing.cpp.

621 {
623 static constexpr SkScalar kScale = 100.f;
624 static constexpr SkScalar kIntervals[] = {10/kScale, 5/kScale};
625 paint.setPathEffect(SkDashPathEffect::Make(kIntervals, std::size(kIntervals), 0.f));
626 paint.setAntiAlias(true);
627 paint.setStrokeWidth(0.25f/kScale);
628 // substep moves the subpixel offset every iteration.
629 static constexpr SkScalar kSubstep = 0.05f/kScale;
630 // We will draw a grid of horiz/vertical lines that pass through each other's off intervals.
631 static constexpr SkScalar kStep = kIntervals[0] + kIntervals[1];
632 canvas->scale(kScale, kScale);
633 canvas->translate(kIntervals[1], kIntervals[1]);
635 paint.setStrokeCap(c);
636 for (SkScalar x = -.5f*kIntervals[1]; x < 105/kScale; x += (kStep + kSubstep)) {
637 canvas->drawLine({x, 0}, {x, 100/kScale}, paint);
638 canvas->drawLine({0, x}, {100/kScale, x}, paint);
639 }
640 canvas->translate(110/kScale, 0);
641 }
642}
const Paint & paint
static constexpr int kScale

◆ drawline()

static void drawline ( SkCanvas canvas,
int  on,
int  off,
const SkPaint paint,
SkScalar  finalX = SkIntToScalar(600),
SkScalar  finalY = SkIntToScalar(0),
SkScalar  phase = SkIntToScalar(0),
SkScalar  startX = SkIntToScalar(0),
SkScalar  startY = SkIntToScalar(0) 
)
static

Definition at line 30 of file dashing.cpp.

33 {
35
36 const SkScalar intervals[] = {
37 SkIntToScalar(on),
38 SkIntToScalar(off),
39 };
40
41 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
42 canvas->drawLine(startX, startY, finalX, finalY, p);
43}
#define SkIntToScalar(x)
Definition SkScalar.h:57
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)

◆ make_path_line()

static SkPath make_path_line ( const SkRect bounds)
static

Definition at line 122 of file dashing.cpp.

122 {
123 return SkPathBuilder().moveTo(bounds.left(), bounds.top())
124 .lineTo(bounds.right(), bounds.bottom())
125 .detach();
126}
SkPathBuilder & lineTo(SkPoint pt)
SkPathBuilder & moveTo(SkPoint pt)
Optional< SkRect > bounds
Definition SkRecords.h:189

◆ make_path_oval()

static SkPath make_path_oval ( const SkRect bounds)
static

Definition at line 132 of file dashing.cpp.

132 {
133 return SkPath::Oval(bounds);
134}
static SkPath Oval(const SkRect &, SkPathDirection=SkPathDirection::kCW)
Definition SkPath.cpp:3522

◆ make_path_rect()

static SkPath make_path_rect ( const SkRect bounds)
static

Definition at line 128 of file dashing.cpp.

128 {
129 return SkPath::Rect(bounds);
130}
static SkPath Rect(const SkRect &, SkPathDirection=SkPathDirection::kCW, unsigned startIndex=0)
Definition SkPath.cpp:3518

◆ make_path_star()

static SkPath make_path_star ( const SkRect bounds)
static

Definition at line 136 of file dashing.cpp.

136 {
139 return path.makeTransform(matrix);
140}
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition SkMatrix.h:157
@ kCenter_ScaleToFit
scales and aligns to center
Definition SkMatrix.h:139
static SkPath make_unit_star(int n)
Definition dashing.cpp:109
unsigned useCenter Optional< SkMatrix > matrix
Definition SkRecords.h:258

◆ make_unit_star()

static SkPath make_unit_star ( int  n)
static

Definition at line 109 of file dashing.cpp.

109 {
110 SkScalar rad = -SK_ScalarPI / 2;
111 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
112
114 b.moveTo(0, -SK_Scalar1);
115 for (int i = 1; i < n; i++) {
116 rad += drad;
117 b.lineTo(SkScalarCos(rad), SkScalarSin(rad));
118 }
119 return b.close().detach();
120}
#define SkScalarSin(radians)
Definition SkScalar.h:45
#define SK_Scalar1
Definition SkScalar.h:18
#define SkScalarCos(radians)
Definition SkScalar.h:46
#define SK_ScalarPI
Definition SkScalar.h:21
SkPathBuilder & close()

◆ show_giant_dash()

static void show_giant_dash ( SkCanvas canvas)
static

Definition at line 48 of file dashing.cpp.

48 {
50
51 drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000));
52}
static void drawline(SkCanvas *canvas, int on, int off, const SkPaint &paint, SkScalar finalX=SkIntToScalar(600), SkScalar finalY=SkIntToScalar(0), SkScalar phase=SkIntToScalar(0), SkScalar startX=SkIntToScalar(0), SkScalar startY=SkIntToScalar(0))
Definition dashing.cpp:30

◆ show_zero_len_dash()

static void show_zero_len_dash ( SkCanvas canvas)
static

Definition at line 54 of file dashing.cpp.

54 {
56
57 drawline(canvas, 2, 2, paint, SkIntToScalar(0));
58 paint.setStroke(true);
59 paint.setStrokeWidth(SkIntToScalar(2));
60 canvas->translate(0, SkIntToScalar(20));
61 drawline(canvas, 4, 4, paint, SkIntToScalar(0));
62}
void translate(SkScalar dx, SkScalar dy)