summaryrefslogtreecommitdiff
path: root/libs/dgl/src/NanoVG.cpp
blob: a4c357bfb5080635a68c1fd7cb6ac27a9e2e1743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/*
 * DISTRHO Plugin Framework (DPF)
 * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
 *
 * Permission to use, copy, modify, and/or distribute this software for any purpose with
 * or without fee is hereby granted, provided that the above copyright notice and this
 * permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "../NanoVG.hpp"
#include "../Window.hpp"

// -----------------------------------------------------------------------
// Ignore some warnings if debugging

#if 0 //def DEBUG
# define NANOVG_GL3   0
# define NANOVG_GLES2 0
# define NANOVG_GLES3 0
# define NANOVG_GL_USE_UNIFORMBUFFER 0
# if defined(__clang__)
#  pragma clang diagnostic push
#  pragma clang diagnostic ignored "-Weverything"
# elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#  pragma GCC diagnostic push
#  pragma GCC diagnostic ignored "-Wall"
#  pragma GCC diagnostic ignored "-Wextra"
#  pragma GCC diagnostic ignored "-Wconversion"
#  pragma GCC diagnostic ignored "-Weffc++"
#  pragma GCC diagnostic ignored "-Wsign-conversion"
#  pragma GCC diagnostic ignored "-Wundef"
#  pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
# endif
#endif

// -----------------------------------------------------------------------
// Include NanoVG OpenGL implementation

#define NANOVG_GL2_IMPLEMENTATION 1
#include "nanovg/nanovg_gl.h"

#if defined(NANOVG_GL2)
# define nvgCreateGL nvgCreateGL2
# define nvgDeleteGL nvgDeleteGL2
#elif defined(NANOVG_GL3)
# define nvgCreateGL nvgCreateGL3
# define nvgDeleteGL nvgDeleteGL3
#elif defined(NANOVG_GLES2)
# define nvgCreateGL nvgCreateGLES2
# define nvgDeleteGL nvgDeleteGLES2
#elif defined(NANOVG_GLES3)
# define nvgCreateGL nvgCreateGLES3
# define nvgDeleteGL nvgDeleteGLES3
#endif

// -----------------------------------------------------------------------
// Restore normal state if debugging

#if 0//def DEBUG
# if defined(__clang__)
#  pragma clang diagnostic pop
# elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
#  pragma GCC diagnostic pop
# endif
#endif

// -----------------------------------------------------------------------

START_NAMESPACE_DGL

// -----------------------------------------------------------------------
// Paint

NanoVG::Paint::Paint() noexcept
    : radius(0.0f), feather(0.0f), innerColor(), outerColor(), imageId(0), repeat(REPEAT_NONE)
{
    std::memset(xform, 0, sizeof(float)*6);
    std::memset(extent, 0, sizeof(float)*2);
}

NanoVG::Paint::Paint(const NVGpaint& p) noexcept
    : radius(p.radius), feather(p.feather), innerColor(p.innerColor), outerColor(p.outerColor), imageId(p.image), repeat(static_cast<PatternRepeat>(p.repeat))
{
    std::memcpy(xform, p.xform, sizeof(float)*6);
    std::memcpy(extent, p.extent, sizeof(float)*2);
}

NanoVG::Paint::operator NVGpaint() const noexcept
{
    NVGpaint p;
    p.radius = radius;
    p.feather = feather;
    p.innerColor = innerColor;
    p.outerColor = outerColor;
    p.image = imageId;
    p.repeat = repeat;
    std::memcpy(p.xform, xform, sizeof(float)*6);
    std::memcpy(p.extent, extent, sizeof(float)*2);
    return p;
}

// -----------------------------------------------------------------------
// NanoImage

NanoImage::NanoImage(NVGcontext* const context, const int imageId) noexcept
    : fContext(context),
      fImageId(imageId),
      fSize(),
      leakDetector_NanoImage()
{
    _updateSize();
}

NanoImage::~NanoImage()
{
    if (fContext != nullptr && fImageId != 0)
        nvgDeleteImage(fContext, fImageId);
}

Size<uint> NanoImage::getSize() const noexcept
{
    return fSize;
}

void NanoImage::updateImage(const uchar* const data)
{
    DISTRHO_SAFE_ASSERT_RETURN(data != nullptr,);

    if (fContext != nullptr && fImageId != 0)
    {
        nvgUpdateImage(fContext, fImageId, data);
        _updateSize();
    }
}

void NanoImage::_updateSize()
{
    int w=0, h=0;

    if (fContext != nullptr && fImageId != 0)
    {
        nvgImageSize(fContext, fImageId, &w, &h);

        if (w < 0) w = 0;
        if (h < 0) h = 0;
    }

    fSize.setSize(static_cast<uint>(w), static_cast<uint>(h));
}

// -----------------------------------------------------------------------
// NanoVG

NanoVG::NanoVG()
    : fContext(nvgCreateGL(512, 512, NVG_ANTIALIAS)),
      fInFrame(false),
      leakDetector_NanoVG()
{
    DISTRHO_SAFE_ASSERT_RETURN(fContext != nullptr,);
}

NanoVG::NanoVG(const int textAtlasWidth, const int textAtlasHeight)
    : fContext(nvgCreateGL(textAtlasWidth, textAtlasHeight, NVG_ANTIALIAS)),
      fInFrame(false),
      leakDetector_NanoVG()
{
    DISTRHO_SAFE_ASSERT_RETURN(fContext != nullptr,);
}

NanoVG::~NanoVG()
{
    DISTRHO_SAFE_ASSERT(! fInFrame);

    if (fContext != nullptr)
        nvgDeleteGL(fContext);
}

// -----------------------------------------------------------------------

void NanoVG::beginFrame(const uint width, const uint height, const float scaleFactor, const Alpha alpha)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(scaleFactor > 0.0f,);
    DISTRHO_SAFE_ASSERT_RETURN(! fInFrame,);

    fInFrame = true;
    nvgBeginFrame(fContext, static_cast<int>(width), static_cast<int>(height), scaleFactor, static_cast<NVGalpha>(alpha));
}

void NanoVG::beginFrame(Widget* const widget)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(widget != nullptr,);
    DISTRHO_SAFE_ASSERT_RETURN(! fInFrame,);

    Window& window(widget->getParentWindow());

    fInFrame = true;
    nvgBeginFrame(fContext, static_cast<int>(window.getWidth()), static_cast<int>(window.getHeight()), 1.0f, NVG_PREMULTIPLIED_ALPHA);
}

void NanoVG::endFrame()
{
    DISTRHO_SAFE_ASSERT_RETURN(fInFrame,);

    if (fContext != nullptr)
        nvgEndFrame(fContext);

    fInFrame = false;
}

// -----------------------------------------------------------------------
// State Handling

void NanoVG::save()
{
    if (fContext != nullptr)
        nvgSave(fContext);
}

void NanoVG::restore()
{
    if (fContext != nullptr)
        nvgRestore(fContext);
}

void NanoVG::reset()
{
    if (fContext != nullptr)
        nvgReset(fContext);
}

// -----------------------------------------------------------------------
// Render styles

void NanoVG::strokeColor(const Color& color)
{
    if (fContext != nullptr)
        nvgStrokeColor(fContext, color);
}

void NanoVG::strokeColor(const int red, const int green, const int blue, const int alpha)
{
    if (fContext != nullptr)
    {
        DISTRHO_SAFE_ASSERT_RETURN(red   >= 0 && red   <= 255,);
        DISTRHO_SAFE_ASSERT_RETURN(green >= 0 && green <= 255,);
        DISTRHO_SAFE_ASSERT_RETURN(blue  >= 0 && blue  <= 255,);
        DISTRHO_SAFE_ASSERT_RETURN(alpha >= 0 && alpha <= 255,);

        nvgStrokeColor(fContext, nvgRGBA(static_cast<uchar>(red),
                                         static_cast<uchar>(green),
                                         static_cast<uchar>(blue),
                                         static_cast<uchar>(alpha)));
    }
}

void NanoVG::strokeColor(const float red, const float green, const float blue, const float alpha)
{
    if (fContext != nullptr)
        nvgStrokeColor(fContext, nvgRGBAf(red, green, blue, alpha));
}

void NanoVG::strokePaint(const Paint& paint)
{
    if (fContext != nullptr)
        nvgStrokePaint(fContext, paint);
}

void NanoVG::fillColor(const Color& color)
{
    if (fContext != nullptr)
        nvgFillColor(fContext, color);
}

void NanoVG::fillColor(const int red, const int green, const int blue, const int alpha)
{
    if (fContext != nullptr)
    {
        DISTRHO_SAFE_ASSERT_RETURN(red   >= 0 && red   <= 255,);
        DISTRHO_SAFE_ASSERT_RETURN(green >= 0 && green <= 255,);
        DISTRHO_SAFE_ASSERT_RETURN(blue  >= 0 && blue  <= 255,);
        DISTRHO_SAFE_ASSERT_RETURN(alpha >= 0 && alpha <= 255,);

        nvgFillColor(fContext, nvgRGBA(static_cast<uchar>(red),
                                       static_cast<uchar>(green),
                                       static_cast<uchar>(blue),
                                       static_cast<uchar>(alpha)));
    }
}

void NanoVG::fillColor(const float red, const float green, const float blue, const float alpha)
{
    if (fContext != nullptr)
        nvgFillColor(fContext, nvgRGBAf(red, green, blue, alpha));
}

void NanoVG::fillPaint(const Paint& paint)
{
    if (fContext != nullptr)
        nvgFillPaint(fContext, paint);
}

void NanoVG::miterLimit(float limit)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(limit > 0.0f,);

    nvgMiterLimit(fContext, limit);
}

void NanoVG::strokeWidth(float size)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(size > 0.0f,);

    nvgStrokeWidth(fContext, size);
}

void NanoVG::lineCap(NanoVG::LineCap cap)
{
    if (fContext != nullptr)
        nvgLineCap(fContext, cap);
}

void NanoVG::lineJoin(NanoVG::LineCap join)
{
    if (fContext != nullptr)
        nvgLineJoin(fContext, join);
}

// -----------------------------------------------------------------------
// Transforms

void NanoVG::resetTransform()
{
    if (fContext != nullptr)
        nvgResetTransform(fContext);
}

void NanoVG::transform(float a, float b, float c, float d, float e, float f)
{
    if (fContext != nullptr)
        nvgTransform(fContext, a, b, c, d, e, f);
}

void NanoVG::translate(float x, float y)
{
    if (fContext != nullptr)
        nvgTranslate(fContext, x, y);
}

void NanoVG::rotate(float angle)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(angle > 0.0f,);

    nvgRotate(fContext, angle);
}

void NanoVG::skewX(float angle)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(angle > 0.0f,);

    nvgSkewX(fContext, angle);
}

void NanoVG::skewY(float angle)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(angle > 0.0f,);

    nvgSkewY(fContext, angle);
}

void NanoVG::scale(float x, float y)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(x > 0.0f,);
    DISTRHO_SAFE_ASSERT_RETURN(y > 0.0f,);

    nvgScale(fContext, x, y);
}

void NanoVG::currentTransform(float xform[6])
{
    if (fContext != nullptr)
        nvgCurrentTransform(fContext, xform);
}

void NanoVG::transformIdentity(float dst[6])
{
    nvgTransformIdentity(dst);
}

void NanoVG::transformTranslate(float dst[6], float tx, float ty)
{
    nvgTransformTranslate(dst, tx, ty);
}

void NanoVG::transformScale(float dst[6], float sx, float sy)
{
    nvgTransformScale(dst, sx, sy);
}

void NanoVG::transformRotate(float dst[6], float a)
{
    nvgTransformRotate(dst, a);
}

void NanoVG::transformSkewX(float dst[6], float a)
{
    nvgTransformSkewX(dst, a);
}

void NanoVG::transformSkewY(float dst[6], float a)
{
    nvgTransformSkewY(dst, a);
}

void NanoVG::transformMultiply(float dst[6], const float src[6])
{
    nvgTransformMultiply(dst, src);
}

void NanoVG::transformPremultiply(float dst[6], const float src[6])
{
    nvgTransformPremultiply(dst, src);
}

int NanoVG::transformInverse(float dst[6], const float src[6])
{
    return nvgTransformInverse(dst, src);
}

void NanoVG::transformPoint(float& dstx, float& dsty, const float xform[6], float srcx, float srcy)
{
    nvgTransformPoint(&dstx, &dsty, xform, srcx, srcy);
}

float NanoVG::degToRad(float deg)
{
    return nvgDegToRad(deg);
}

float NanoVG::radToDeg(float rad)
{
    return nvgRadToDeg(rad);
}

// -----------------------------------------------------------------------
// Images

NanoImage* NanoVG::createImage(const char* filename)
{
    if (fContext == nullptr) return nullptr;
    DISTRHO_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr);

    if (const int imageId = nvgCreateImage(fContext, filename))
        return new NanoImage(fContext, imageId);

    return nullptr;
}

NanoImage* NanoVG::createImageMem(uchar* data, int ndata)
{
    if (fContext == nullptr) return nullptr;
    DISTRHO_SAFE_ASSERT_RETURN(data != nullptr, nullptr);
    DISTRHO_SAFE_ASSERT_RETURN(ndata > 0, nullptr);

    if (const int imageId = nvgCreateImageMem(fContext, data, ndata))
        return new NanoImage(fContext, imageId);

    return nullptr;
}

NanoImage* NanoVG::createImageRGBA(uint w, uint h, const uchar* data)
{
    if (fContext == nullptr) return nullptr;
    DISTRHO_SAFE_ASSERT_RETURN(data != nullptr, nullptr);

    if (const int imageId = nvgCreateImageRGBA(fContext, static_cast<int>(w), static_cast<int>(h), data))
        return new NanoImage(fContext, imageId);

    return nullptr;
}

// -----------------------------------------------------------------------
// Paints

NanoVG::Paint NanoVG::linearGradient(float sx, float sy, float ex, float ey, const Color& icol, const Color& ocol)
{
    if (fContext == nullptr) return Paint();
    return nvgLinearGradient(fContext, sx, sy, ex, ey, icol, ocol);
}

NanoVG::Paint NanoVG::boxGradient(float x, float y, float w, float h, float r, float f, const Color& icol, const Color& ocol)
{
    if (fContext == nullptr) return Paint();
    return nvgBoxGradient(fContext, x, y, w, h, r, f, icol, ocol);
}

NanoVG::Paint NanoVG::radialGradient(float cx, float cy, float inr, float outr, const Color& icol, const Color& ocol)
{
    if (fContext == nullptr) return Paint();
    return nvgRadialGradient(fContext, cx, cy, inr, outr, icol, ocol);
}

NanoVG::Paint NanoVG::imagePattern(float ox, float oy, float ex, float ey, float angle, const NanoImage* image, NanoVG::PatternRepeat repeat)
{
    if (fContext == nullptr) return Paint();
    DISTRHO_SAFE_ASSERT_RETURN(image != nullptr, Paint());

    return nvgImagePattern(fContext, ox, oy, ex, ey, angle, image->fImageId, repeat);
}

// -----------------------------------------------------------------------
// Scissoring

void NanoVG::scissor(float x, float y, float w, float h)
{
    if (fContext != nullptr)
        nvgScissor(fContext, x, y, w, h);
}

void NanoVG::resetScissor()
{
    if (fContext != nullptr)
        nvgResetScissor(fContext);
}

// -----------------------------------------------------------------------
// Paths

void NanoVG::beginPath()
{
    if (fContext != nullptr)
        nvgBeginPath(fContext);
}

void NanoVG::moveTo(float x, float y)
{
    if (fContext != nullptr)
        nvgMoveTo(fContext, x, y);
}

void NanoVG::lineTo(float x, float y)
{
    if (fContext != nullptr)
        nvgLineTo(fContext, x, y);
}

void NanoVG::bezierTo(float c1x, float c1y, float c2x, float c2y, float x, float y)
{
    if (fContext != nullptr)
        nvgBezierTo(fContext, c1x, c1y, c2x, c2y, x, y);
}

void NanoVG::arcTo(float x1, float y1, float x2, float y2, float radius)
{
    if (fContext != nullptr)
        nvgArcTo(fContext, x1, y1, x2, y2, radius);
}

void NanoVG::closePath()
{
    if (fContext != nullptr)
        nvgClosePath(fContext);
}

void NanoVG::pathWinding(NanoVG::Winding dir)
{
    if (fContext != nullptr)
        nvgPathWinding(fContext, dir);
}

void NanoVG::arc(float cx, float cy, float r, float a0, float a1, NanoVG::Winding dir)
{
    if (fContext != nullptr)
        nvgArc(fContext, cx, cy, r, a0, a1, dir);
}

void NanoVG::rect(float x, float y, float w, float h)
{
    if (fContext != nullptr)
        nvgRect(fContext, x, y, w, h);
}

void NanoVG::roundedRect(float x, float y, float w, float h, float r)
{
    if (fContext != nullptr)
        nvgRoundedRect(fContext, x, y, w, h, r);
}

void NanoVG::ellipse(float cx, float cy, float rx, float ry)
{
    if (fContext != nullptr)
        nvgEllipse(fContext, cx, cy, rx, ry);
}

void NanoVG::circle(float cx, float cy, float r)
{
    if (fContext != nullptr)
        nvgCircle(fContext, cx, cy, r);
}

void NanoVG::fill()
{
    if (fContext != nullptr)
        nvgFill(fContext);
}

void NanoVG::stroke()
{
    if (fContext != nullptr)
        nvgStroke(fContext);
}

// -----------------------------------------------------------------------
// Text

NanoVG::FontId NanoVG::createFont(const char* name, const char* filename)
{
    if (fContext == nullptr) return -1;
    DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', -1);
    DISTRHO_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', -1);

    return nvgCreateFont(fContext, name, filename);
}

NanoVG::FontId NanoVG::createFontMem(const char* name, const uchar* data, int ndata, bool freeData)
{
    if (fContext == nullptr) return -1;
    DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', -1);
    DISTRHO_SAFE_ASSERT_RETURN(data != nullptr, -1);

    return nvgCreateFontMem(fContext, name, const_cast<uchar*>(data), ndata, freeData);
}

NanoVG::FontId NanoVG::findFont(const char* name)
{
    if (fContext == nullptr) return -1;
    DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', -1);

    return nvgFindFont(fContext, name);
}

void NanoVG::fontSize(float size)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(size > 0.0f,);

    nvgFontSize(fContext, size);
}

void NanoVG::fontBlur(float blur)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(blur >= 0.0f,);

    nvgFontBlur(fContext, blur);
}

void NanoVG::textLetterSpacing(float spacing)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(spacing >= 0.0f,);

    nvgTextLetterSpacing(fContext, spacing);
}

void NanoVG::textLineHeight(float lineHeight)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(lineHeight > 0.0f,);

    nvgTextLineHeight(fContext, lineHeight);
}

void NanoVG::textAlign(NanoVG::Align align)
{
    if (fContext != nullptr)
        nvgTextAlign(fContext, align);
}

void NanoVG::textAlign(int align)
{
    if (fContext != nullptr)
        nvgTextAlign(fContext, align);
}

void NanoVG::fontFaceId(FontId font)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(font >= 0,);

    nvgFontFaceId(fContext, font);
}

void NanoVG::fontFace(const char* font)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(font != nullptr && font[0] != '\0',);

    nvgFontFace(fContext, font);
}

float NanoVG::text(float x, float y, const char* string, const char* end)
{
    if (fContext == nullptr) return 0.0f;
    DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0', 0.0f);

    return nvgText(fContext, x, y, string, end);
}

void NanoVG::textBox(float x, float y, float breakRowWidth, const char* string, const char* end)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0',);

    nvgTextBox(fContext, x, y, breakRowWidth, string, end);
}

float NanoVG::textBounds(float x, float y, const char* string, const char* end, Rectangle<float>& bounds)
{
    if (fContext == nullptr) return 0.0f;
    DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0', 0.0f);

    float b[4];
    const float ret = nvgTextBounds(fContext, x, y, string, end, b);
    bounds = Rectangle<float>(b[0], b[1], b[2], b[3]);
    return ret;
}

void NanoVG::textBoxBounds(float x, float y, float breakRowWidth, const char* string, const char* end, float* bounds)
{
    if (fContext == nullptr) return;
    DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0',);

    nvgTextBoxBounds(fContext, x, y, breakRowWidth, string, end, bounds);
}

int NanoVG::textGlyphPositions(float x, float y, const char* string, const char* end, NanoVG::GlyphPosition* positions, int maxPositions)
{
    if (fContext == nullptr) return 0;
    DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0', 0);

    return nvgTextGlyphPositions(fContext, x, y, string, end, (NVGglyphPosition*)positions, maxPositions);
}

void NanoVG::textMetrics(float* ascender, float* descender, float* lineh)
{
    if (fContext != nullptr)
        nvgTextMetrics(fContext, ascender, descender, lineh);
}

int NanoVG::textBreakLines(const char* string, const char* end, float breakRowWidth, NanoVG::TextRow* rows, int maxRows)
{
    if (fContext != nullptr)
        return nvgTextBreakLines(fContext, string, end, breakRowWidth, (NVGtextRow*)rows, maxRows);
    return 0;
}

// -----------------------------------------------------------------------

END_NAMESPACE_DGL

extern "C" {
#include "nanovg/nanovg.c"
}

// -----------------------------------------------------------------------