CairoG2D/MQL5/Experts/CairoG2D/Article 11/Demo11_Stroke.mq5
SandroBegashvil SandroBegashvil a4ff995bf2
2026-10-14 13:05:31 +04:00
524 lines
18 KiB
MQL5
//+-------------------------------------------------------------------+
//| Demo11_Stroke.mq5 |
//| Copyright 2026, Sandro Begashvili |
//| https://www.mql5.com/en/users/sandrobegashvil |
//| Telegram: https://t.me/MrMQL5Developer |
//| Cairo-Style Graphics in MQL5 (Part 11) - the only demo |
//| |
//| THE LIBRARY LEARNS TO DRAW A LINE. |
//| |
//| Ten files after this part. The new one, Stroke.mqh, turns a path |
//| into ANOTHER path: the outline of a line of some width, with |
//| caps at its ends, joins at its bends and gaps where a dash |
//| pattern says there should be gaps. |
//| |
//| Nothing rasterizes it. The outline is ordinary geometry, filled |
//| with the non-zero rule by the same Fill() every part since 3 has |
//| used - which is the whole design, stated as one sentence. |
//| |
//| Panels: |
//| |
//| A. one segment at five widths, from a hairline up B. the three |
//| caps, against a ruled line that shows where the |
//| geometry actually ends |
//| C. the three joins, at a sharp angle and a shallow one D. the |
//| miter limit: the same spike, allowed and refused E. dashes - |
//| four patterns, and one offset animated by input F. a closed |
//| shape stroked and filled, and the stroke outline |
//| shown on its own so the overlapping pieces are visible |
//| G. a price line: 240 points, round joins, under a transform |
//| |
//| Attach to any chart. Everything is removed again on deinit. |
//+-------------------------------------------------------------------+
#property copyright "Copyright 2026, Sandro Begashvili"
#property link "https://www.mql5.com/en/users/sandrobegashvil"
#property description "Part 11: a real stroke engine - width, caps, joins, dashes"
#property description "Free to use and extend - please credit the author on mql5.com"
#property version "1.00"
#property strict
#include <CairoG2D/Context.mqh>
#include <CairoG2D/DemoTheme.mqh>
#define UI_X 5
#define UI_Y 5
#define UI_W 950
#define UI_H 820
#define C_BACK DEMO_BG
#define C_CARD DEMO_PANEL
#define C_LINE DEMO_BORDER
#define C_INK DEMO_INK
#define C_BLUE DEMO_BLUE
#define C_GREEN DEMO_GREEN
#define C_AMBER DEMO_AMBER
#define C_RED DEMO_RED
#define C_DEEP DEMO_INK_SOFT
#define C_MUTED DEMO_GREY
input bool InpShowCaptions = true; // Draw the explanatory captions
input bool InpSetLightChart = true; // Switch the chart to Black On White
input double InpDashOffset = 0.0; // Panel E: dash offset, in pixels
CCairoSurface g_surface;
CCairoContext g_ctx;
//+-------------------------------------------------------------------+
//| A panel frame - and from this part on, its border is a STROKE. |
//| |
//| Part 10's version of this function drew four thin rectangles, |
//| because the library could only fill. One Rect() and one Stroke() |
//| replace all of it. |
//+-------------------------------------------------------------------+
void Frame(const double x, const double y, const double w, const double h)
{
g_ctx.Save();
g_ctx.SetLineWidth(1.0);
g_ctx.SetLineJoin(CAIRO_JOIN_MITER);
//--- the half-pixel inset is the oldest trick in 2D drawing: a
//--- 1 px line centered on an integer coordinate straddles two
//--- pixel columns at half coverage each, and looks like a 2 px
//--- grey line. Centered on x.5 it lands inside one column.
g_ctx.Rect(x + 0.5, y + 0.5, w - 1.0, h - 1.0);
g_ctx.FillStroke(C_CARD, C_LINE);
g_ctx.Restore();
}
//+-------------------------------------------------------------------+
//| A straight segment, in the current coordinates. |
//+-------------------------------------------------------------------+
void Segment(const double x0, const double y0,
const double x1, const double y1)
{
g_ctx.MoveTo(x0, y0);
g_ctx.LineTo(x1, y1);
}
//+-------------------------------------------------------------------+
//| A. ONE SEGMENT, FIVE WIDTHS. |
//| |
//| The same two verbs each time; only SetLineWidth changes. The |
//| hairline at the top is 0.5 px wide, which the rasterizer renders |
//| as a half-covered row rather than refusing - coverage was never |
//| limited to whole pixels. |
//+-------------------------------------------------------------------+
void PanelWidths(const double ox, const double oy)
{
Frame(ox, oy, 460, 200);
double w[5] = {0.5, 1.0, 2.0, 5.0, 12.0};
g_ctx.SetSourceColor(C_BLUE);
g_ctx.SetLineCap(CAIRO_CAP_BUTT);
for(int i = 0; i < 5; i++)
{
const double y = oy + 32 + i * 34;
g_ctx.SetLineWidth(w[i]);
Segment(ox + 30, y, ox + 300, y);
g_ctx.Stroke();
//--- a tick at each end, so the butt cap's exact stop is visible
g_ctx.SetSourceColor(C_MUTED);
g_ctx.SetLineWidth(1.0);
Segment(ox + 300.5, y - 14, ox + 300.5, y + 14);
g_ctx.Stroke();
g_ctx.SetSourceColor(C_BLUE);
}
}
//+-------------------------------------------------------------------+
//| B. THE THREE CAPS. |
//| |
//| All three segments stop at exactly the same x. The grey rule |
//| marks it, so the difference between stopping there, covering half |
//| a width past it, and rounding off is a measurement rather than an |
//| impression. |
//+-------------------------------------------------------------------+
void PanelCaps(const double ox, const double oy)
{
Frame(ox, oy, 460, 200);
ENUM_CAIRO_CAP caps[3] = {CAIRO_CAP_BUTT, CAIRO_CAP_ROUND,
CAIRO_CAP_SQUARE
};
uint col[3] = {C_BLUE, C_GREEN, C_AMBER};
const double x0 = ox + 70;
const double x1 = ox + 330;
//--- the two rules the caps are measured against
g_ctx.SetSourceColor(C_MUTED);
g_ctx.SetLineWidth(1.0);
Segment(x0 + 0.5, oy + 24, x0 + 0.5, oy + 176);
Segment(x1 + 0.5, oy + 24, x1 + 0.5, oy + 176);
g_ctx.Stroke();
for(int i = 0; i < 3; i++)
{
const double y = oy + 54 + i * 46;
g_ctx.SetLineWidth(18.0);
g_ctx.SetLineCap(caps[i]);
g_ctx.SetSourceColor(col[i]);
Segment(x0, y, x1, y);
g_ctx.Stroke();
//--- the path itself, drawn as a hairline on top, so the cap is
//--- visibly an ADDITION to the geometry rather than part of it
g_ctx.SetLineWidth(1.0);
g_ctx.SetLineCap(CAIRO_CAP_BUTT);
g_ctx.SetSourceColor(C_BACK);
Segment(x0, y, x1, y);
g_ctx.Stroke();
}
}
//+-------------------------------------------------------------------+
//| C. THE THREE JOINS. |
//| |
//| A sharp bend on the left, a shallow one on the right. The miter |
//| is the longest at a sharp angle and indistinguishable from the |
//| others at a shallow one, which is why the limit matters only |
//| where the geometry is tight. |
//+-------------------------------------------------------------------+
void Chevron(const double x, const double y, const double spread)
{
g_ctx.MoveTo(x, y + 40);
g_ctx.LineTo(x + spread, y);
g_ctx.LineTo(x + spread * 2, y + 40);
}
//+-------------------------------------------------------------------+
//| Three joins, two angles. |
//+-------------------------------------------------------------------+
void PanelJoins(const double ox, const double oy)
{
Frame(ox, oy, 460, 200);
ENUM_CAIRO_JOIN joins[3] = {CAIRO_JOIN_MITER, CAIRO_JOIN_ROUND,
CAIRO_JOIN_BEVEL
};
uint col[3] = {C_BLUE, C_GREEN, C_AMBER};
g_ctx.SetLineWidth(16.0);
g_ctx.SetLineCap(CAIRO_CAP_BUTT);
g_ctx.SetMiterLimit(10.0);
for(int i = 0; i < 3; i++)
{
g_ctx.SetLineJoin(joins[i]);
g_ctx.SetSourceColor(col[i]);
//--- sharp
Chevron(ox + 10 + i * 60, oy + 100, 22.0);
g_ctx.Stroke();
//--- shallow
Chevron(ox + 200 + i * 60, oy + 100, 60.0);
g_ctx.Stroke();
}
}
//+-------------------------------------------------------------------+
//| D. THE MITER LIMIT. |
//| |
//| One very sharp bend, drawn twice. On the left the limit is high |
//| enough that the spike is allowed; on the right it is not, and the |
//| join falls back to a bevel. Nothing about the path changed. |
//+-------------------------------------------------------------------+
void PanelMiterLimit(const double ox, const double oy)
{
Frame(ox, oy, 460, 200);
double limit[2] = {12.0, 2.0};
uint col[2] = {C_BLUE, C_RED};
g_ctx.SetLineWidth(14.0);
g_ctx.SetLineJoin(CAIRO_JOIN_MITER);
g_ctx.SetLineCap(CAIRO_CAP_BUTT);
for(int i = 0; i < 2; i++)
{
g_ctx.SetMiterLimit(limit[i]);
g_ctx.SetSourceColor(col[i]);
const double x = ox + 60 + i * 230;
g_ctx.MoveTo(x, oy + 170);
g_ctx.LineTo(x + 86, oy + 44);
g_ctx.LineTo(x + 100, oy + 170);
g_ctx.Stroke();
}
}
//+-------------------------------------------------------------------+
//| E. DASHES. |
//| |
//| Four patterns, including an odd-length one - { 14 } means |
//| fourteen on and fourteen off, because the stroker doubles an odd |
//| pattern so that on and off still alternate on the second pass. |
//| The last line takes the offset from an input, which is how a |
//| marching-ants selection is animated. |
//+-------------------------------------------------------------------+
void PanelDashes(const double ox, const double oy)
{
Frame(ox, oy, 460, 200);
g_ctx.SetLineWidth(4.0);
g_ctx.SetLineCap(CAIRO_CAP_BUTT);
g_ctx.SetSourceColor(C_BLUE);
double p1[2] = {14.0, 8.0};
double p2[4] = {2.0, 6.0, 12.0, 6.0};
double p3[1] = {14.0};
g_ctx.SetDash(p1, 2);
Segment(ox + 30, oy + 44, ox + 420, oy + 44);
g_ctx.Stroke();
g_ctx.SetDash(p2, 4);
Segment(ox + 30, oy + 84, ox + 420, oy + 84);
g_ctx.Stroke();
g_ctx.SetDash(p3, 1);
Segment(ox + 30, oy + 124, ox + 420, oy + 124);
g_ctx.Stroke();
//--- the same pattern, shifted along the line
g_ctx.SetSourceColor(C_GREEN);
g_ctx.SetDash(p1, 2, InpDashOffset);
Segment(ox + 30, oy + 164, ox + 420, oy + 164);
g_ctx.Stroke();
g_ctx.ClearDash();
}
//+-------------------------------------------------------------------+
//| F. WHAT A STROKE ACTUALLY IS. |
//| |
//| Left: a rounded rectangle, filled and stroked, which is what a |
//| widget looks like. Right: the SAME stroke, but the outline the |
//| stroker produced is drawn as a hairline instead of being filled - |
//| so the overlapping quads, wedges and discs are visible as the |
//| separate closed contours they are. |
//+-------------------------------------------------------------------+
void PanelOutline(const double ox, const double oy)
{
Frame(ox, oy, 460, 200);
//--- the finished look
g_ctx.SetLineWidth(6.0);
g_ctx.SetLineJoin(CAIRO_JOIN_ROUND);
g_ctx.RoundRect(ox + 30, oy + 40, 170, 120, 18.0);
g_ctx.FillStroke(C_CARD, C_BLUE);
//--- the same geometry, stroked into a path we can look at
CCairoPath src;
CCairoPath outline;
CCairoStroker *st = g_ctx.Stroker();
src.AddRoundRect(ox + 260, oy + 40, 170, 120, 18.0);
st.SetWidth(6.0);
st.SetJoin(CAIRO_JOIN_ROUND);
st.SetCap(CAIRO_CAP_BUTT);
st.ClearDash();
if(st.Stroke(src, outline))
{
//--- the outline is a PATH. Stroking it with a hairline shows
//--- every contour the stroker emitted, instead of the welded
//--- result the non-zero rule would produce.
g_ctx.Save();
g_ctx.NewPath();
g_ctx.SetLineWidth(1.0);
g_ctx.SetSourceColor(C_RED);
CCairoStroker thin;
CCairoPath hairline;
thin.SetWidth(1.0);
thin.SetCap(CAIRO_CAP_BUTT);
thin.SetJoin(CAIRO_JOIN_MITER);
if(thin.Stroke(outline, hairline))
g_ctx.Rasterizer().Fill(hairline, C_RED, g_surface.m_px,
g_surface.Width(), g_surface.Height(),
CAIRO_NONZERO, g_ctx.ClipObject());
g_ctx.Restore();
PrintFormat("Cairo5 Part 11, panel F: the outline of a rounded "
"rectangle is %d points", outline.PointCount());
}
}
//+-------------------------------------------------------------------+
//| G. A PRICE LINE. |
//| |
//| 240 points, round joins so no spike can appear at a sharp tick, |
//| drawn under a transform that the line code knows nothing about. |
//| This is the shape of every chart overlay the library will ever be |
//| asked for. |
//+-------------------------------------------------------------------+
void PanelPriceLine(const double ox, const double oy)
{
Frame(ox, oy, 930, 140);
const int n = 240;
double xs[];
double ys[];
ArrayResize(xs, n);
ArrayResize(ys, n);
double v = 0.0;
for(int i = 0; i < n; i++)
{
//--- a deterministic walk, so the figure in the article and the
//--- panel on the chart are the same picture every run
v += MathSin(i * 0.21) * 3.1 + MathSin(i * 0.037) * 5.0;
xs[i] = i * 3.8;
ys[i] = -v * 0.32 + 30;
}
g_ctx.Save();
g_ctx.Translate(ox + 20, oy + 76);
g_ctx.SetLineWidth(2.0);
g_ctx.SetLineJoin(CAIRO_JOIN_ROUND);
g_ctx.SetLineCap(CAIRO_CAP_ROUND);
g_ctx.SetSourceColor(C_BLUE);
g_ctx.Polyline(xs, ys, n);
g_ctx.Stroke();
//--- a dashed zero line through the same coordinates
double zp[2] = {6.0, 5.0};
g_ctx.SetSourceColor(C_MUTED);
g_ctx.SetLineWidth(1.0);
g_ctx.SetDash(zp, 2);
Segment(0.0, 0.0, (n - 1) * 3.8, 0.0);
g_ctx.Stroke();
g_ctx.Restore();
}
//+-------------------------------------------------------------------+
//| Every panel, in order. |
//+-------------------------------------------------------------------+
void Render()
{
g_ctx.ClearAll(C_BACK);
g_ctx.ResetState();
PanelWidths(10, 36);
PanelCaps(480, 36);
PanelJoins(10, 246);
PanelMiterLimit(480, 246);
PanelDashes(10, 456);
PanelOutline(480, 456);
PanelPriceLine(10, 666);
g_surface.Flush();
}
//+-------------------------------------------------------------------+
//| The captions. Chart labels, not drawing. |
//+-------------------------------------------------------------------+
void Captions()
{
DemoLabel("ttl", UI_X + 12, UI_Y + 6,
"Cairo5 Part 11 - a real stroke engine",
(color)C_INK, 11);
DemoLabel("a", UI_X + 20, UI_Y + 42, "A. one segment, five widths");
DemoLabel("b", UI_X + 500, UI_Y + 42, "B. butt, round, square");
DemoLabel("c", UI_X + 20, UI_Y + 252, "C. miter, round, bevel");
DemoLabel("d", UI_X + 500, UI_Y + 252, "D. the miter limit: 12, then 2");
DemoLabel("e", UI_X + 20, UI_Y + 462, "E. dash patterns, and an offset");
DemoLabel("f", UI_X + 500, UI_Y + 462, "F. the stroke outline itself");
DemoLabel("g", UI_X + 20, UI_Y + 672, "G. 240 points, round joins");
}
//+-------------------------------------------------------------------+
//| Create the surface, hand it to the context, draw once. |
//+-------------------------------------------------------------------+
int OnInit()
{
if(InpSetLightChart)
DemoUseLightChart();
if(!g_surface.Create("cairo_demo11", UI_X, UI_Y, UI_W, UI_H))
{
Print("Cairo5: failed to create the surface");
return INIT_FAILED;
}
if(!g_ctx.SetTarget(GetPointer(g_surface)))
{
Print("Cairo5: failed to set the context target");
return INIT_FAILED;
}
const ulong t0 = GetMicrosecondCount();
Render();
const ulong t1 = GetMicrosecondCount();
if(InpShowCaptions)
Captions();
PrintFormat("Cairo5 Part 11: one frame in %d us, %d x %d pixels, "
"1 chart object", (int)(t1 - t0), UI_W, UI_H);
ChartRedraw();
return INIT_SUCCEEDED;
}
//+-------------------------------------------------------------------+
//| Give everything back. |
//+-------------------------------------------------------------------+
void OnDeinit(const int reason)
{
g_surface.Destroy();
DemoLabelsClear();
ChartRedraw();
}
//+-------------------------------------------------------------------+
//| The panel is static; nothing is redrawn on a tick. |
//+-------------------------------------------------------------------+
void OnTick()
{
}
//+------------------------------------------------------------------+