13#include "IGraphicsNanoVG.h"
16#if defined IGRAPHICS_GL
18 #if defined IGRAPHICS_GL2
19 #define NANOVG_GL2_IMPLEMENTATION
20 #elif defined IGRAPHICS_GL3
21 #include <OpenGL/gl3.h>
22 #define NANOVG_GL3_IMPLEMENTATION
23 #elif defined IGRAPHICS_GLES2
24 #include <libGLESv2/angle_gl.h>
25 #define NANOVG_GLES2_IMPLEMENTATION
26 #elif defined IGRAPHICS_GLES3
27 #include <libGLESv2/angle_gl.h>
28 #define NANOVG_GLES3_IMPLEMENTATION
31 #if defined IGRAPHICS_GLES2
32 #include <libGLESv2/angle_gl.h>
33 #define NANOVG_GLES2_IMPLEMENTATION
34 #elif defined IGRAPHICS_GLES3
35 #include <libGLESv2/angle_gl.h>
36 #define NANOVG_GLES3_IMPLEMENTATION
38 #error Define either IGRAPHICS_GLES2 or IGRAPHICS_GLES3 for IGRAPHICS_NANOVG with OS_IOS
41 #pragma comment(lib, "opengl32.lib")
42 #if defined IGRAPHICS_GL2
43 #define NANOVG_GL2_IMPLEMENTATION
44 #elif defined IGRAPHICS_GL3
45 #define NANOVG_GL3_IMPLEMENTATION
47 #error Define either IGRAPHICS_GL2 or IGRAPHICS_GL3 when using IGRAPHICS_GL and IGRAPHICS_NANOVG with OS_WIN
49 #elif defined OS_LINUX
50 #if defined IGRAPHICS_GL2
51 #define NANOVG_GL2_IMPLEMENTATION
52 #elif defined IGRAPHICS_GL3
53 #define NANOVG_GL3_IMPLEMENTATION
55 #error "Define either IGRAPHICS_GL2 or IGRAPHICS_GL3 when using IGRAPHICS_NANOVG with OS_LINUX"
58 #if defined IGRAPHICS_GLES2
59 #define NANOVG_GLES2_IMPLEMENTATION
60 #elif defined IGRAPHICS_GLES3
61 #define NANOVG_GLES3_IMPLEMENTATION
63 #error Define either IGRAPHICS_GLES2 or IGRAPHICS_GLES3 when using IGRAPHICS_GL and IGRAPHICS_NANOVG with OS_WEB
66 #include "nanovg_gl.h"
67 #include "nanovg_gl_utils.h"
68#elif defined IGRAPHICS_METAL
69 #include "nanovg_mtl.h"
72 #import <Metal/Metal.h>
75 #error you must define either IGRAPHICS_GL2, IGRAPHICS_GLES2 etc or IGRAPHICS_METAL when using IGRAPHICS_NANOVG
82using namespace igraphics;
84#pragma mark - Private Classes and Structs
89 Bitmap(NVGcontext* pContext,
const char* path,
double sourceScale,
int nvgImageID,
bool shared =
false);
90 Bitmap(
IGraphicsNanoVG* pGraphics, NVGcontext* pContext,
int width,
int height,
float scale,
float drawScale);
91 Bitmap(NVGcontext* pContext,
int width,
int height,
const uint8_t* pData,
float scale,
float drawScale);
93 NVGframebuffer* GetFBO()
const {
return mFBO; }
97 NVGframebuffer* mFBO =
nullptr;
98 bool mSharedTexture =
false;
101IGraphicsNanoVG::Bitmap::Bitmap(NVGcontext* pContext,
const char* path,
double sourceScale,
int nvgImageID,
bool shared)
103 assert(nvgImageID > 0);
106 mSharedTexture = shared;
108 nvgImageSize(mVG, nvgImageID, &w, &h);
110 SetBitmap(nvgImageID, w, h, sourceScale, 1.f);
113IGraphicsNanoVG::Bitmap::Bitmap(
IGraphicsNanoVG* pGraphics, NVGcontext* pContext,
int width,
int height,
float scale,
float drawScale)
115 assert(width > 0 && height > 0);
116 mGraphics = pGraphics;
118 mFBO = nvgCreateFramebuffer(pContext, width, height, 0);
120 nvgBindFramebuffer(mFBO);
122#ifdef IGRAPHICS_METAL
123 mnvgClearWithColor(mVG, nvgRGBAf(0, 0, 0, 0));
125 glViewport(0, 0, width, height);
126 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
127 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
129 nvgBeginFrame(mVG, width, height, 1.f);
132 SetBitmap(mFBO->image, width, height, scale, drawScale);
135IGraphicsNanoVG::Bitmap::Bitmap(NVGcontext* pContext,
int width,
int height,
const uint8_t* pData,
float scale,
float drawScale)
137 int idx = nvgCreateImageRGBA(pContext, width, height, 0, pData);
139 SetBitmap(idx, width, height, scale, drawScale);
142IGraphicsNanoVG::Bitmap::~Bitmap()
147 mGraphics->DeleteFBO(mFBO);
149 nvgDeleteImage(mVG, GetBitmap());
154static StaticStorage<IFontData> sFontCache;
156extern std::map<std::string, MTLTexturePtr> gTextureMap;
159static void nvgReadPixels(NVGcontext* pContext,
int image,
int x,
int y,
int width,
int height,
void* pData)
161#if defined(IGRAPHICS_GL)
162 glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pData);
163#elif defined(IGRAPHICS_METAL)
164 mnvgReadPixels(pContext, image, x, y, width, height, pData);
168#pragma mark - Utilities
171BEGIN_IGRAPHICS_NAMESPACE
173NVGcolor NanoVGColor(
const IColor& color,
const IBlend* pBlend)
176 c.r = (float) color.R / 255.0f;
177 c.g = (
float) color.G / 255.0f;
178 c.b = (float) color.B / 255.0f;
183void NanoVGRect(NVGcontext* pContext,
const IRECT& r)
185 nvgRect(pContext, r.L, r.T, r.
W(), r.
H());
188void NanoVGSetBlendMode(NVGcontext* pContext,
const IBlend* pBlend)
192 nvgGlobalCompositeOperation(pContext, NVG_SOURCE_OVER);
196 switch (pBlend->mMethod)
198 case EBlend::SrcOver: nvgGlobalCompositeOperation(pContext, NVG_SOURCE_OVER);
break;
199 case EBlend::SrcIn: nvgGlobalCompositeOperation(pContext, NVG_SOURCE_IN);
break;
200 case EBlend::SrcOut: nvgGlobalCompositeOperation(pContext, NVG_SOURCE_OUT);
break;
201 case EBlend::SrcAtop: nvgGlobalCompositeOperation(pContext, NVG_ATOP);
break;
202 case EBlend::DstOver: nvgGlobalCompositeOperation(pContext, NVG_DESTINATION_OVER);
break;
203 case EBlend::DstIn: nvgGlobalCompositeOperation(pContext, NVG_DESTINATION_IN);
break;
204 case EBlend::DstOut: nvgGlobalCompositeOperation(pContext, NVG_DESTINATION_OUT);
break;
205 case EBlend::DstAtop: nvgGlobalCompositeOperation(pContext, NVG_DESTINATION_ATOP);
break;
206 case EBlend::Add: nvgGlobalCompositeBlendFunc(pContext, NVG_SRC_ALPHA, NVG_DST_ALPHA);
break;
207 case EBlend::XOR: nvgGlobalCompositeOperation(pContext, NVG_XOR);
break;
211NVGpaint NanoVGPaint(NVGcontext* pContext,
const IPattern& pattern,
const IBlend* pBlend)
213 assert(pattern.
NStops() > 0);
217 NVGcolor icol = NanoVGColor(pattern.
GetStop(0).mColor, pBlend);
218 NVGcolor ocol = NanoVGColor(pattern.
GetStop(pattern.
NStops() - 1).mColor, pBlend);
224 if (pattern.mType == EPatternType::Radial)
226 return nvgRadialGradient(pContext, s[0], s[1], inverse.mXX * pattern.
GetStop(0).mOffset, inverse.mXX, icol, ocol);
232 return nvgLinearGradient(pContext, s[0], s[1], e[0], e[1], icol, ocol);
236END_IGRAPHICS_NAMESPACE
241IGraphicsNanoVG::IGraphicsNanoVG(
IGEditorDelegate& dlg,
int w,
int h,
int fps,
float scale)
244 DBGMSG(
"IGraphics NanoVG @ %i FPS\n", fps);
245 StaticStorage<IFontData>::Accessor storage(sFontCache);
249IGraphicsNanoVG::~IGraphicsNanoVG()
251 StaticStorage<IFontData>::Accessor storage(sFontCache);
258#if defined IGRAPHICS_METAL
259 return "NanoVG | Metal";
262 return "NanoVG | WebGL";
264 #if defined IGRAPHICS_GL2
265 return "NanoVG | GL2";
266 #elif defined IGRAPHICS_GL3
267 return "NanoVG | GL3";
268 #elif defined IGRAPHICS_GLES2
269 return "NanoVG | GLES2";
270 #elif defined IGRAPHICS_GLES3
271 return "NanoVG | GLES3";
281 return (strstr(extLower,
"png") !=
nullptr) || (strstr(extLower,
"jpg") !=
nullptr) || (strstr(extLower,
"jpeg") !=
nullptr);
286 if (targetScale == 0)
290 StaticStorage<APIBitmap>::Accessor storage(mBitmapCache);
291 APIBitmap* pAPIBitmap = storage.Find(name, targetScale);
296 const char* ext = name + strlen(name) - 1;
297 while (ext >= name && *ext !=
'.') --ext;
300 WDL_String fullPathOrResourceID;
302 EResourceLocation resourceFound =
SearchImageResource(name, ext, fullPathOrResourceID, targetScale, sourceScale);
306 if(resourceFound == EResourceLocation::kNotFound || !bitmapTypeSupported)
308 assert(0 &&
"Bitmap not found");
312 pAPIBitmap =
LoadAPIBitmap(fullPathOrResourceID.Get(), sourceScale, resourceFound, ext);
314 storage.Add(pAPIBitmap, name, sourceScale);
316 assert(pAPIBitmap &&
"Bitmap not loaded");
319 return IBitmap(pAPIBitmap, nStates, framesAreHorizontal, name);
325 int nvgImageFlags = 0;
328#if defined OS_IOS && defined IGRAPHICS_METAL
329 if (location == EResourceLocation::kPreloadedTexture)
331 idx = mnvgCreateImageFromHandle(mVG, gTextureMap[fileNameOrResID], nvgImageFlags);
337 if (location == EResourceLocation::kWinBinary)
339 const void* pResData =
nullptr;
347 idx = nvgCreateImageMem(mVG, nvgImageFlags, (
unsigned char*) pResData, size);
352 if (location == EResourceLocation::kAbsolutePath)
355 idx = nvgCreateImage(mVG, fileNameOrResID, nvgImageFlags);
358 return new Bitmap(mVG, fileNameOrResID, scale, idx, location == EResourceLocation::kPreloadedTexture);
363 StaticStorage<APIBitmap>::Accessor storage(mBitmapCache);
364 APIBitmap* pBitmap = storage.Find(name, scale);
369 int nvgImageFlags = 0;
373 idx = nvgCreateImageMem(mVG, nvgImageFlags, (
unsigned char*)pData, dataSize);
376 pBitmap =
new Bitmap(mVG, name, scale, idx,
false);
378 storage.Add(pBitmap, name, scale);
391 APIBitmap* pAPIBitmap =
new Bitmap(
this, mVG, width, height, scale, drawScale);
395 nvgBindFramebuffer(mMainFrameBuffer);
404 const APIBitmap* pBitmap = layer->GetAPIBitmap();
409 if (data.GetSize() >= size)
419 const APIBitmap* pBitmap = layer->GetAPIBitmap();
422 int size = width * height * 4;
424 if (mask.GetSize() >= size)
426 if (!shadow.mDrawForeground)
429 nvgGlobalCompositeBlendFunc(mVG, NVG_ZERO, NVG_ZERO);
431 nvgFillColor(mVG, NanoVGColor(COLOR_TRANSPARENT));
436 IRECT bounds(layer->Bounds());
440 IBitmap tempLayerBitmap(shadowBitmap, 1,
false);
441 IBitmap maskBitmap(&maskRawBitmap, 1,
false);
442 ILayer shadowLayer(shadowBitmap, layer->Bounds(),
nullptr,
IRECT());
447 DrawBitmap(maskBitmap, bounds, 0, 0,
nullptr);
448 IBlend blend1(EBlend::SrcIn, 1.0);
453 IBlend blend2(EBlend::DstOver, shadow.mOpacity);
454 bounds.
Translate(shadow.mXOffset, shadow.mYOffset);
455 DrawBitmap(tempLayerBitmap, bounds, 0, 0, &blend2);
463#if defined IGRAPHICS_METAL
464 mVG = nvgCreateContext(pContext, NVG_ANTIALIAS | NVG_TRIPLE_BUFFER);
466 mVG = nvgCreateContext(NVG_ANTIALIAS );
470 DBGMSG(
"Could not init nanovg.\n");
478 StaticStorage<APIBitmap>::Accessor storage(mBitmapCache);
481 if(mMainFrameBuffer !=
nullptr)
482 nvgDeleteFramebuffer(mMainFrameBuffer);
484 mMainFrameBuffer =
nullptr;
487 nvgDeleteContext(mVG);
496 if (mMainFrameBuffer !=
nullptr)
498 nvgDeleteFramebuffer(mMainFrameBuffer);
499 mMainFrameBuffer =
nullptr;
504#if defined OS_LINUX && defined IGRAPHICS_GL
510 while (glGetError() != GL_NO_ERROR) {}
514 mMainFrameBuffer = nvgCreateFramebuffer(mVG, w, h, 0);
516 if (mMainFrameBuffer ==
nullptr)
517 DBGMSG(
"Could not init FBO (w=%d h=%d).\n", w, h);
528 glClearColor(0.f, 0.f, 0.f, 0.f);
529 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
530 #if defined OS_MAC || defined OS_IOS
531 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &mInitialFBO);
535 nvgBindFramebuffer(mMainFrameBuffer);
546 if (mMainFrameBuffer ==
nullptr)
553 nvgBindFramebuffer(
nullptr);
559 nvgResetTransform(mVG);
560 nvgTranslate(mVG, mXTranslation, mYTranslation);
563 nvgFillPaint(mVG, img);
567#if (defined OS_MAC || defined OS_IOS) && defined IGRAPHICS_GL
568 glBindFramebuffer(GL_FRAMEBUFFER, mInitialFBO);
587 nvgTransformScale(imgPaint.xform, scale, scale);
589 imgPaint.xform[4] = dest.L - srcX;
590 imgPaint.xform[5] = dest.T - srcY;
591 imgPaint.extent[0] = bitmap.
W() * bitmap.
GetScale();
592 imgPaint.extent[1] = bitmap.
H() * bitmap.
GetScale();
593 imgPaint.image = pAPIBitmap->
GetBitmap();
594 imgPaint.radius = imgPaint.feather = 0.f;
595 imgPaint.innerColor = imgPaint.outerColor = nvgRGBAf(1, 1, 1,
BlendWeight(pBlend));
600 nvgRect(mVG, dest.L, dest.T, dest.
W(), dest.
H());
601 nvgFillPaint(mVG, imgPaint);
602 NanoVGSetBlendMode(mVG, pBlend);
604 nvgGlobalCompositeOperation(mVG, NVG_SOURCE_OVER);
620 nvgArc(mVG, cx, cy, r, DegToRad(a1 - 90.f), DegToRad(a2 - 90.f), winding == EWinding::CW ? NVG_CW : NVG_CCW);
625 nvgMoveTo(mVG, x, y);
630 nvgLineTo(mVG, x, y);
635 nvgBezierTo(mVG, c1x, c1y, c2x, c2y, x2, y2);
640 nvgQuadTo(mVG, cx, cy, x2, y2);
645 nvgPathWinding(mVG, clockwise ? NVG_CW : NVG_CCW);
653void IGraphicsNanoVG::PrepareAndMeasureText(
const IText& text,
const char* str,
IRECT& r,
double& x,
double & y)
const
657 assert(nvgFindFont(mVG, text.mFont) != -1 &&
"No font found - did you forget to load it?");
660 nvgFontSize(mVG, text.mSize);
661 nvgFontFace(mVG, text.mFont);
667 case EAlign::Near: align = NVG_ALIGN_LEFT; x = r.L;
break;
668 case EAlign::Center: align = NVG_ALIGN_CENTER; x = r.
MW();
break;
669 case EAlign::Far: align = NVG_ALIGN_RIGHT; x = r.R;
break;
672 switch (text.mVAlign)
674 case EVAlign::Top: align |= NVG_ALIGN_TOP; y = r.T;
break;
675 case EVAlign::Middle: align |= NVG_ALIGN_MIDDLE; y = r.
MH();
break;
676 case EVAlign::Bottom: align |= NVG_ALIGN_BOTTOM; y = r.B;
break;
679 nvgTextAlign(mVG, align);
680 nvgTextBounds(mVG, x, y, str, NULL, fbounds);
682 r =
IRECT(fbounds[0], fbounds[1], fbounds[2], fbounds[3]);
689 PrepareAndMeasureText(text, str, bounds, x, y);
697 IRECT measured = bounds;
700 PrepareAndMeasureText(text, str, measured, x, y);
702 DoTextRotation(text, bounds, measured);
703 nvgFillColor(mVG, NanoVGColor(text.mFGColor, pBlend));
704 NanoVGSetBlendMode(mVG, pBlend);
705 nvgText(mVG, x, y, str, NULL);
706 nvgGlobalCompositeOperation(mVG, NVG_SOURCE_OVER);
713 switch (options.mCapOption)
715 case ELineCap::Butt: nvgLineCap(mVG, NVG_BUTT);
break;
716 case ELineCap::Round: nvgLineCap(mVG, NVG_ROUND);
break;
717 case ELineCap::Square: nvgLineCap(mVG, NVG_SQUARE);
break;
720 switch (options.mJoinOption)
722 case ELineJoin::Miter: nvgLineJoin(mVG, NVG_MITER);
break;
723 case ELineJoin::Round: nvgLineJoin(mVG, NVG_ROUND);
break;
724 case ELineJoin::Bevel: nvgLineJoin(mVG, NVG_BEVEL);
break;
727 nvgMiterLimit(mVG, options.mMiterLimit);
728 nvgStrokeWidth(mVG, thickness);
731 if (pattern.mType == EPatternType::Solid)
732 nvgStrokeColor(mVG, NanoVGColor(pattern.
GetStop(0).mColor, pBlend));
734 nvgStrokePaint(mVG, NanoVGPaint(mVG, pattern, pBlend));
736 nvgPathWinding(mVG, NVG_CCW);
737 NanoVGSetBlendMode(mVG, pBlend);
739 nvgGlobalCompositeOperation(mVG, NVG_SOURCE_OVER);
741 if (!options.mPreserve)
747 switch(options.mFillRule)
752 case EFillRule::Winding:
753 nvgPathWinding(mVG, NVG_CCW);
755 case EFillRule::EvenOdd:
756 nvgPathWinding(mVG, NVG_CW);
758 case EFillRule::Preserve:
764 if (pattern.mType == EPatternType::Solid)
765 nvgFillColor(mVG, NanoVGColor(pattern.
GetStop(0).mColor, pBlend));
767 nvgFillPaint(mVG, NanoVGPaint(mVG, pattern, pBlend));
769 NanoVGSetBlendMode(mVG, pBlend);
771 nvgGlobalCompositeOperation(mVG, NVG_SOURCE_OVER);
773 if (!options.mPreserve)
779 StaticStorage<IFontData>::Accessor storage(sFontCache);
780 IFontData* cached = storage.Find(fontID);
784 nvgCreateFontFaceMem(mVG, fontID, cached->Get(), cached->GetSize(), cached->GetFaceIdx(), 0);
788 IFontDataPtr data = font->GetFontData();
790 if (data->IsValid() && nvgCreateFontFaceMem(mVG, fontID, data->Get(), data->GetSize(), data->GetFaceIdx(), 0) != -1)
792 storage.Add(data.release(), fontID);
799void IGraphicsNanoVG::UpdateLayer()
807 nvgBindFramebuffer(mMainFrameBuffer);
815 glViewport(0, 0, mLayers.top()->Bounds().W() * scale, mLayers.top()->Bounds().H() * scale);
817 nvgBindFramebuffer(
dynamic_cast<const Bitmap*
>(mLayers.top()->GetAPIBitmap())->GetFBO());
822void IGraphicsNanoVG::PathTransformSetMatrix(
const IMatrix& m)
824 double xTranslate = 0.0;
825 double yTranslate = 0.0;
827 if (!mLayers.empty())
829 IRECT bounds = mLayers.top()->Bounds();
831 xTranslate = -bounds.L;
832 yTranslate = -bounds.T;
835 nvgResetTransform(mVG);
837 nvgTranslate(mVG, xTranslate, yTranslate);
838 nvgTransform(mVG, m.mXX, m.mYX, m.mXY, m.mYY, m.mTX, m.mTY);
841void IGraphicsNanoVG::SetClipRegion(
const IRECT& r)
843 nvgScissor(mVG, r.L, r.T, r.
W(), r.
H());
848 const float xd = x1 - x2;
849 const float yd = y1 - y2;
850 const float len = std::sqrt(xd * xd + yd * yd);
852 const float segs = std::round(len / dashLen);
853 const float incr = 1.f / segs;
860 for (
int i = 1; i < static_cast<int>(segs); i+=2)
862 float progress = incr *
static_cast<float>(i);
864 float xe = x1 + progress * (x2 - x1);
865 float ye = y1 + progress * (y2 - y1);
871 xs = x1 + progress * (x2 - x1);
872 ys = y1 + progress * (y2 - y1);
882 const int xsegs =
static_cast<int>(std::ceil(bounds.
W() / (dashLen * 2.f)));
883 const int ysegs =
static_cast<int>(std::ceil(bounds.
H() / (dashLen * 2.f)));
893 for(
int j = 0; j < 2; j++)
895 for (
int i = 0; i < xsegs; i++)
897 x2 =
Clip(x1 + dashLen, bounds.L, bounds.R);
899 x1 =
Clip(x2 + dashLen, bounds.L, bounds.R);
905 for (
int i = 0; i < ysegs; i++)
907 y2 =
Clip(y1 + dashLen, bounds.T, bounds.B);
909 y1 =
Clip(y2 + dashLen, bounds.T, bounds.B);
921void IGraphicsNanoVG::DeleteFBO(NVGframebuffer* pBuffer)
924 nvgDeleteFramebuffer(pBuffer);
927 WDL_MutexLock lock(&mFBOMutex);
928 mFBOStack.push(pBuffer);
932void IGraphicsNanoVG::ClearFBOStack()
934 WDL_MutexLock lock(&mFBOMutex);
935 while (!mFBOStack.empty())
937 nvgDeleteFramebuffer(mFBOStack.top());
944 NVGpaint shadowPaint = nvgBoxGradient(mVG, innerBounds.L + xyDrop, innerBounds.T + xyDrop, innerBounds.
W(), innerBounds.
H(), roundness, blur, NanoVGColor(COLOR_BLACK_DROP_SHADOW, pBlend), NanoVGColor(COLOR_TRANSPARENT,
nullptr));
946 nvgRect(mVG, outerBounds.L, outerBounds.T, outerBounds.
W(), outerBounds.
H());
947 nvgFillPaint(mVG, shadowPaint);
948 nvgGlobalCompositeOperation(mVG, NVG_SOURCE_OVER);
956 nvgFontSize(mVG, text.mSize);
957 nvgFontFace(mVG, text.mFont);
959 float x = 0.0, y = 0.0;
960 const float width = bounds.
W();
962 float yOffsetScale = 0.0;
966 case EAlign::Near: align = NVG_ALIGN_LEFT; x = bounds.L;
break;
967 case EAlign::Center: align = NVG_ALIGN_CENTER; x = bounds.
MW();
break;
968 case EAlign::Far: align = NVG_ALIGN_RIGHT; x = bounds.R;
break;
971 switch (text.mVAlign)
975 align |= NVG_ALIGN_TOP;
980 case EVAlign::Middle:
982 align |= NVG_ALIGN_MIDDLE;
987 case EVAlign::Bottom:
989 align |= NVG_ALIGN_BOTTOM;
996 nvgTextAlign(mVG, align);
1004 nvgTextMetrics(mVG, NULL, NULL, &lineHeight);
1005 nvgFillColor(mVG, NanoVGColor(text.mFGColor, pBlend));
1007 for (
auto run : {0, 1})
1010 end = str + strlen(str);
1011 while ((nRows = nvgTextBreakLines(mVG, start, end, width, rows, 3))) {
1012 for (
int i = 0; i < nRows; i++) {
1019 NVGtextRow* row = &rows[i];
1020 nvgText(mVG, x, y - ((lines*lineHeight)*yOffsetScale), row->start, row->end);
1024 start = rows[nRows-1].next;
const void * LoadWinResource(const char *resID, const char *type, int &sizeInBytes, void *pHInstance)
Load a resource from the binary (windows only).
A Text entry widget drawn by IGraphics to optionally override platform text entries.
A base class interface for a bitmap abstraction around the different drawing back end bitmap represen...
BitmapData GetBitmap() const
void SetBitmap(BitmapData pBitmap, int w, int h, float scale, float drawScale)
Used to initialise the members after construction.
float GetDrawScale() const
User-facing bitmap abstraction that you use to manage bitmap data, independant of draw class/platform...
APIBitmap * GetAPIBitmap() const
An editor delegate base class that uses IGraphics for the UI.
The lowest level base class of an IGraphics context.
ILayer * PopLayer()
Pop a layer off the stack.
void PathTransformTranslate(float x, float y)
Apply a translation transform to the current path.
void PathRect(const IRECT &bounds)
Add a rectangle to the current path.
virtual float GetBackingPixelScale() const
void DoMeasureTextRotation(const IText &text, const IRECT &bounds, IRECT &rect) const
Measures text bounds accounting for rotation.
int WindowWidth() const
Gets the width of the graphics context including draw scaling.
EResourceLocation SearchImageResource(const char *fileName, const char *type, WDL_String &result, int targetScale, int &sourceScale)
Search for a bitmap image resource matching the target scale.
void PathTransformRestore()
Restore the affine transform of the current path, to the previously saved state.
void PushLayer(ILayer *pLayer)
Push a layer on to the stack.
int GetRoundedScreenScale() const
Gets the screen/display scaling factor, rounded up.
void RemoveAllControls()
Removes all regular IControls from the control list, as well as special controls (frees memory).
virtual void * GetWinModuleHandle()
float GetScreenScale() const
Gets the screen/display scaling factor, e.g.
void PathTransformSave()
Save the current affine transform of the current path.
float GetDrawScale() const
Gets the graphics context scaling factor.
int WindowHeight() const
Gets the height of the graphics context including draw scaling.
virtual void BeginFrame()
Called at the beginning of drawing.
IGraphics draw class using NanoVG
const char * GetDrawingAPIStr() override
void ApplyShadowMask(ILayerPtr &layer, RawBitmapData &mask, const IShadow &shadow) override
Implemented by a graphics backend to apply a calculated shadow mask to a layer, according to the shad...
void DrawResize() override
Called to update the drawing surface after a resize.
void PathClear() override
Clear the stack of path drawing commands.
IBitmap LoadBitmap(const char *name, int nStates, bool framesAreHorizontal, int targetScale) override
Load a bitmap image from disk or from windows resource.
void PathFill(const IPattern &pattern, const IFillOptions &options, const IBlend *pBlend) override
Fill the current current path.
void GetLayerBitmapData(const ILayerPtr &layer, RawBitmapData &data) override
Get the contents of a layer as Raw RGBA bitmap data NOTE: you should only call this within IControl::...
void DrawDottedRect(const IColor &color, const IRECT &bounds, const IBlend *pBlend, float thickness, float dashLen) override
Draw a dotted rectangle to the graphics context.
void PathSetWinding(bool clockwise) override
NanoVG only.
APIBitmap * LoadAPIBitmap(const char *fileNameOrResID, int scale, EResourceLocation location, const char *ext) override
Drawing API method to load a bitmap, called internally.
void PathStroke(const IPattern &pattern, float thickness, const IStrokeOptions &options, const IBlend *pBlend) override
Stroke the current current path.
void PathArc(float cx, float cy, float r, float a1, float a2, EWinding winding) override
Add an arc to the current path.
void PathLineTo(float x, float y) override
Add a line to the current path from the current point to the specified location.
void OnViewDestroyed() override
Called after a platform view is destroyed, so that drawing classes can e.g.
void DrawMultiLineText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend) override
Draw some multi-line text to the graphics context in a specific rectangle (NanoVG only)
void DrawDottedLine(const IColor &color, float x1, float y1, float x2, float y2, const IBlend *pBlend, float thickness, float dashLen) override
Draw a dotted line to the graphics context.
void PathCubicBezierTo(float c1x, float c1y, float c2x, float c2y, float x2, float y2) override
Add a cubic bezier to the current path from the current point to the specified location.
IColor GetPoint(int x, int y) override
Get the color at an X, Y location in the graphics context.
bool LoadAPIFont(const char *fontID, const PlatformFontPtr &font) override
Drawing API method to load a font from a PlatformFontPtr, called internally.
void PathClose() override
Close the path that is being specified.
void DoDrawText(const IText &text, const char *str, const IRECT &bounds, const IBlend *pBlend) override
Internal method to draw text.
void DrawFastDropShadow(const IRECT &innerBounds, const IRECT &outerBounds, float xyDrop=5.f, float roundness=0.f, float blur=10.f, IBlend *pBlend=nullptr) override
NanoVG only.
void PathMoveTo(float x, float y) override
Move the current point in the current path.
void OnViewInitialized(void *pContext) override
Called after platform view initialization, so that drawing classes can e.g.
void BeginFrame() override
Called at the beginning of drawing.
void EndFrame() override
Called by some drawing API classes to finally blit the draw bitmap onto the screen or perform other c...
APIBitmap * CreateAPIBitmap(int width, int height, float scale, double drawScale, bool cacheable=false) override
Creates a new API bitmap, either in memory or as a GPU texture.
void PathQuadraticBezierTo(float cx, float cy, float x2, float y2) override
Add a quadratic bezier to the current path from the current point to the specified location.
bool BitmapExtSupported(const char *ext) override
Checks a file extension and reports whether this drawing API supports loading that extension.
float DoMeasureText(const IText &text, const char *str, IRECT &bounds) const override
Internal method to measure text dimensions.
void DrawBitmap(const IBitmap &bitmap, const IRECT &dest, int srcX, int srcY, const IBlend *pBlend) override
Draw a bitmap (raster) image to the graphics context.
An abstraction that is used to store a temporary raster image/framebuffer.
std::unique_ptr< ILayer > ILayerPtr
ILayerPtr is a managed pointer for transferring the ownership of layers.
float BlendWeight(const IBlend *pBlend)
Helper function to extract the blend weight value from an IBlend ptr if it is valid.
Used to manage stroke behaviour for path based drawing back ends.
BEGIN_IPLUG_NAMESPACE T Clip(T x, T lo, T hi)
Clips the value x between lo and hi.
static void ToLower(char *cDest, const char *cSrc)
Converts a C string to lowercase.
Used to manage composite/blend operations, independent of draw class/platform.
Used to manage color data, independent of draw class/platform.
Used to manage fill behaviour.
Used to store transformation matrices.
IMatrix & Invert()
Changes the matrix to be the inverse of its original value.
void TransformPoint(double &x, double &y, double x0, double y0) const
Transforms a point using this matrix.
Used to store pattern information for gradients.
const IColorStop & GetStop(int idx) const
Get the IColorStop at a particular index (will crash if out of bounds)
Used to manage a rectangular area, independent of draw class/platform.
void Translate(float x, float y)
Translate this rectangle.
Used to specify properties of a drop-shadow to a layer.
IText is used to manage font and text/text entry style for a piece of text on the UI,...