nanovg.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799
  1. //
  2. // Copyright (c) 2013 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <stdio.h>
  19. #include <math.h>
  20. #include "nanovg.h"
  21. #include <bx/macros.h>
  22. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4701) // error C4701: potentially uninitialized local variable 'cint' used
  23. // -Wunused-function and 4505 must be file scope, can't be disabled between push/pop.
  24. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-function");
  25. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505) // error C4505: '' : unreferenced local function has been removed
  26. BX_PRAGMA_DIAGNOSTIC_PUSH();
  27. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-parameter");
  28. BX_PRAGMA_DIAGNOSTIC_IGNORED_GCC("-Wunused-result");
  29. #define FONTSTASH_IMPLEMENTATION
  30. #include "fontstash.h"
  31. BX_PRAGMA_DIAGNOSTIC_POP();
  32. BX_PRAGMA_DIAGNOSTIC_PUSH();
  33. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4127) // warning C4127: conditional expression is constant
  34. #define LODEPNG_NO_COMPILE_ENCODER
  35. #define LODEPNG_NO_COMPILE_DISK
  36. #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
  37. #define LODEPNG_NO_COMPILE_ERROR_TEXT
  38. #define LODEPNG_NO_COMPILE_ALLOCATORS
  39. #define LODEPNG_NO_COMPILE_CPP
  40. #include <lodepng/lodepng.cpp>
  41. BX_PRAGMA_DIAGNOSTIC_POP();
  42. void* lodepng_malloc(size_t _size)
  43. {
  44. return ::malloc(_size);
  45. }
  46. void* lodepng_realloc(void* _ptr, size_t _size)
  47. {
  48. return ::realloc(_ptr, _size);
  49. }
  50. void lodepng_free(void* _ptr)
  51. {
  52. ::free(_ptr);
  53. }
  54. BX_PRAGMA_DIAGNOSTIC_PUSH();
  55. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wmissing-field-initializers");
  56. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow");
  57. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wint-to-pointer-cast")
  58. #define STBI_MALLOC(_size) lodepng_malloc(_size)
  59. #define STBI_REALLOC(_ptr, _size) lodepng_realloc(_ptr, _size)
  60. #define STBI_FREE(_ptr) lodepng_free(_ptr)
  61. #define STB_IMAGE_IMPLEMENTATION
  62. #include <stb/stb_image.c>
  63. BX_PRAGMA_DIAGNOSTIC_POP();
  64. #ifdef _MSC_VER
  65. #pragma warning(disable: 4100) // unreferenced formal parameter
  66. #pragma warning(disable: 4127) // conditional expression is constant
  67. #pragma warning(disable: 4204) // nonstandard extension used : non-constant aggregate initializer
  68. #pragma warning(disable: 4706) // assignment within conditional expression
  69. #endif
  70. #define NVG_INIT_FONTIMAGE_SIZE 512
  71. #define NVG_MAX_FONTIMAGE_SIZE 2048
  72. #define NVG_MAX_FONTIMAGES 4
  73. #define NVG_INIT_COMMANDS_SIZE 256
  74. #define NVG_INIT_POINTS_SIZE 128
  75. #define NVG_INIT_PATHS_SIZE 16
  76. #define NVG_INIT_VERTS_SIZE 256
  77. #define NVG_MAX_STATES 32
  78. #define NVG_KAPPA90 0.5522847493f // Length proportional to radius of a cubic bezier handle for 90deg arcs.
  79. #define NVG_COUNTOF(arr) (sizeof(arr) / sizeof(0[arr]))
  80. enum NVGcommands {
  81. NVG_MOVETO = 0,
  82. NVG_LINETO = 1,
  83. NVG_BEZIERTO = 2,
  84. NVG_CLOSE = 3,
  85. NVG_WINDING = 4,
  86. };
  87. enum NVGpointFlags
  88. {
  89. NVG_PT_CORNER = 0x01,
  90. NVG_PT_LEFT = 0x02,
  91. NVG_PT_BEVEL = 0x04,
  92. NVG_PR_INNERBEVEL = 0x08,
  93. };
  94. struct NVGstate {
  95. NVGpaint fill;
  96. NVGpaint stroke;
  97. float strokeWidth;
  98. float miterLimit;
  99. int lineJoin;
  100. int lineCap;
  101. float alpha;
  102. float xform[6];
  103. NVGscissor scissor;
  104. float fontSize;
  105. float letterSpacing;
  106. float lineHeight;
  107. float fontBlur;
  108. int textAlign;
  109. int fontId;
  110. };
  111. typedef struct NVGstate NVGstate;
  112. struct NVGpoint {
  113. float x,y;
  114. float dx, dy;
  115. float len;
  116. float dmx, dmy;
  117. unsigned char flags;
  118. };
  119. typedef struct NVGpoint NVGpoint;
  120. struct NVGpathCache {
  121. NVGpoint* points;
  122. int npoints;
  123. int cpoints;
  124. NVGpath* paths;
  125. int npaths;
  126. int cpaths;
  127. NVGvertex* verts;
  128. int nverts;
  129. int cverts;
  130. float bounds[4];
  131. };
  132. typedef struct NVGpathCache NVGpathCache;
  133. struct NVGcontext {
  134. NVGparams params;
  135. float* commands;
  136. int ccommands;
  137. int ncommands;
  138. float commandx, commandy;
  139. NVGstate states[NVG_MAX_STATES];
  140. int nstates;
  141. NVGpathCache* cache;
  142. float tessTol;
  143. float distTol;
  144. float fringeWidth;
  145. float devicePxRatio;
  146. struct FONScontext* fs;
  147. int fontImages[NVG_MAX_FONTIMAGES];
  148. int fontImageIdx;
  149. int drawCallCount;
  150. int fillTriCount;
  151. int strokeTriCount;
  152. int textTriCount;
  153. };
  154. static float nvg__sqrtf(float a) { return sqrtf(a); }
  155. static float nvg__modf(float a, float b) { return fmodf(a, b); }
  156. static float nvg__sinf(float a) { return sinf(a); }
  157. static float nvg__cosf(float a) { return cosf(a); }
  158. static float nvg__tanf(float a) { return tanf(a); }
  159. static float nvg__atan2f(float a,float b) { return atan2f(a, b); }
  160. static float nvg__acosf(float a) { return acosf(a); }
  161. static int nvg__mini(int a, int b) { return a < b ? a : b; }
  162. static int nvg__maxi(int a, int b) { return a > b ? a : b; }
  163. static int nvg__clampi(int a, int mn, int mx) { return a < mn ? mn : (a > mx ? mx : a); }
  164. static float nvg__minf(float a, float b) { return a < b ? a : b; }
  165. static float nvg__maxf(float a, float b) { return a > b ? a : b; }
  166. static float nvg__absf(float a) { return a >= 0.0f ? a : -a; }
  167. static float nvg__signf(float a) { return a >= 0.0f ? 1.0f : -1.0f; }
  168. static float nvg__clampf(float a, float mn, float mx) { return a < mn ? mn : (a > mx ? mx : a); }
  169. static float nvg__cross(float dx0, float dy0, float dx1, float dy1) { return dx1*dy0 - dx0*dy1; }
  170. static float nvg__normalize(float *x, float* y)
  171. {
  172. float d = nvg__sqrtf((*x)*(*x) + (*y)*(*y));
  173. if (d > 1e-6f) {
  174. float id = 1.0f / d;
  175. *x *= id;
  176. *y *= id;
  177. }
  178. return d;
  179. }
  180. static void nvg__deletePathCache(NVGpathCache* c)
  181. {
  182. if (c == NULL) return;
  183. if (c->points != NULL) free(c->points);
  184. if (c->paths != NULL) free(c->paths);
  185. if (c->verts != NULL) free(c->verts);
  186. free(c);
  187. }
  188. static NVGpathCache* nvg__allocPathCache(void)
  189. {
  190. NVGpathCache* c = (NVGpathCache*)malloc(sizeof(NVGpathCache));
  191. if (c == NULL) goto error;
  192. memset(c, 0, sizeof(NVGpathCache));
  193. c->points = (NVGpoint*)malloc(sizeof(NVGpoint)*NVG_INIT_POINTS_SIZE);
  194. if (!c->points) goto error;
  195. c->npoints = 0;
  196. c->cpoints = NVG_INIT_POINTS_SIZE;
  197. c->paths = (NVGpath*)malloc(sizeof(NVGpath)*NVG_INIT_PATHS_SIZE);
  198. if (!c->paths) goto error;
  199. c->npaths = 0;
  200. c->cpaths = NVG_INIT_PATHS_SIZE;
  201. c->verts = (NVGvertex*)malloc(sizeof(NVGvertex)*NVG_INIT_VERTS_SIZE);
  202. if (!c->verts) goto error;
  203. c->nverts = 0;
  204. c->cverts = NVG_INIT_VERTS_SIZE;
  205. return c;
  206. error:
  207. nvg__deletePathCache(c);
  208. return NULL;
  209. }
  210. static void nvg__setDevicePixelRatio(NVGcontext* ctx, float ratio)
  211. {
  212. ctx->tessTol = 0.25f / ratio;
  213. ctx->distTol = 0.01f / ratio;
  214. ctx->fringeWidth = 1.0f / ratio;
  215. ctx->devicePxRatio = ratio;
  216. }
  217. NVGcontext* nvgCreateInternal(NVGparams* params)
  218. {
  219. FONSparams fontParams;
  220. NVGcontext* ctx = (NVGcontext*)malloc(sizeof(NVGcontext));
  221. int i;
  222. if (ctx == NULL) goto error;
  223. memset(ctx, 0, sizeof(NVGcontext));
  224. ctx->params = *params;
  225. for (i = 0; i < NVG_MAX_FONTIMAGES; i++)
  226. ctx->fontImages[i] = 0;
  227. ctx->commands = (float*)malloc(sizeof(float)*NVG_INIT_COMMANDS_SIZE);
  228. if (!ctx->commands) goto error;
  229. ctx->ncommands = 0;
  230. ctx->ccommands = NVG_INIT_COMMANDS_SIZE;
  231. ctx->cache = nvg__allocPathCache();
  232. if (ctx->cache == NULL) goto error;
  233. nvgSave(ctx);
  234. nvgReset(ctx);
  235. nvg__setDevicePixelRatio(ctx, 1.0f);
  236. if (ctx->params.renderCreate(ctx->params.userPtr) == 0) goto error;
  237. // Init font rendering
  238. memset(&fontParams, 0, sizeof(fontParams));
  239. fontParams.width = NVG_INIT_FONTIMAGE_SIZE;
  240. fontParams.height = NVG_INIT_FONTIMAGE_SIZE;
  241. fontParams.flags = FONS_ZERO_TOPLEFT;
  242. fontParams.renderCreate = NULL;
  243. fontParams.renderUpdate = NULL;
  244. fontParams.renderDraw = NULL;
  245. fontParams.renderDelete = NULL;
  246. fontParams.userPtr = NULL;
  247. ctx->fs = fonsCreateInternal(&fontParams);
  248. if (ctx->fs == NULL) goto error;
  249. // Create font texture
  250. ctx->fontImages[0] = ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_ALPHA, fontParams.width, fontParams.height, 0, NULL);
  251. if (ctx->fontImages[0] == 0) goto error;
  252. ctx->fontImageIdx = 0;
  253. return ctx;
  254. error:
  255. nvgDeleteInternal(ctx);
  256. return 0;
  257. }
  258. NVGparams* nvgInternalParams(NVGcontext* ctx)
  259. {
  260. return &ctx->params;
  261. }
  262. void nvgDeleteInternal(NVGcontext* ctx)
  263. {
  264. int i;
  265. if (ctx == NULL) return;
  266. if (ctx->commands != NULL) free(ctx->commands);
  267. if (ctx->cache != NULL) nvg__deletePathCache(ctx->cache);
  268. if (ctx->fs)
  269. fonsDeleteInternal(ctx->fs);
  270. for (i = 0; i < NVG_MAX_FONTIMAGES; i++) {
  271. if (ctx->fontImages[i] != 0) {
  272. nvgDeleteImage(ctx, ctx->fontImages[i]);
  273. ctx->fontImages[i] = 0;
  274. }
  275. }
  276. if (ctx->params.renderDelete != NULL)
  277. ctx->params.renderDelete(ctx->params.userPtr);
  278. free(ctx);
  279. }
  280. void nvgBeginFrame(NVGcontext* ctx, int windowWidth, int windowHeight, float devicePixelRatio)
  281. {
  282. /* printf("Tris: draws:%d fill:%d stroke:%d text:%d TOT:%d\n",
  283. ctx->drawCallCount, ctx->fillTriCount, ctx->strokeTriCount, ctx->textTriCount,
  284. ctx->fillTriCount+ctx->strokeTriCount+ctx->textTriCount);*/
  285. ctx->nstates = 0;
  286. nvgSave(ctx);
  287. nvgReset(ctx);
  288. nvg__setDevicePixelRatio(ctx, devicePixelRatio);
  289. ctx->params.renderViewport(ctx->params.userPtr, windowWidth, windowHeight);
  290. ctx->drawCallCount = 0;
  291. ctx->fillTriCount = 0;
  292. ctx->strokeTriCount = 0;
  293. ctx->textTriCount = 0;
  294. }
  295. void nvgCancelFrame(NVGcontext* ctx)
  296. {
  297. ctx->params.renderCancel(ctx->params.userPtr);
  298. }
  299. void nvgEndFrame(NVGcontext* ctx)
  300. {
  301. ctx->params.renderFlush(ctx->params.userPtr);
  302. if (ctx->fontImageIdx != 0) {
  303. int fontImage = ctx->fontImages[ctx->fontImageIdx];
  304. int i, j, iw, ih;
  305. // delete images that smaller than current one
  306. if (fontImage == 0)
  307. return;
  308. nvgImageSize(ctx, fontImage, &iw, &ih);
  309. for (i = j = 0; i < ctx->fontImageIdx; i++) {
  310. if (ctx->fontImages[i] != 0) {
  311. int nw, nh;
  312. nvgImageSize(ctx, ctx->fontImages[i], &nw, &nh);
  313. if (nw < iw || nh < ih)
  314. nvgDeleteImage(ctx, ctx->fontImages[i]);
  315. else
  316. ctx->fontImages[j++] = ctx->fontImages[i];
  317. }
  318. }
  319. // make current font image to first
  320. ctx->fontImages[j++] = ctx->fontImages[0];
  321. ctx->fontImages[0] = fontImage;
  322. ctx->fontImageIdx = 0;
  323. // clear all images after j
  324. for (i = j; i < NVG_MAX_FONTIMAGES; i++)
  325. ctx->fontImages[i] = 0;
  326. }
  327. }
  328. NVGcolor nvgRGB(unsigned char r, unsigned char g, unsigned char b)
  329. {
  330. return nvgRGBA(r,g,b,255);
  331. }
  332. NVGcolor nvgRGBf(float r, float g, float b)
  333. {
  334. return nvgRGBAf(r,g,b,1.0f);
  335. }
  336. NVGcolor nvgRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
  337. {
  338. NVGcolor color;
  339. // Use longer initialization to suppress warning.
  340. color.r = r / 255.0f;
  341. color.g = g / 255.0f;
  342. color.b = b / 255.0f;
  343. color.a = a / 255.0f;
  344. return color;
  345. }
  346. NVGcolor nvgRGBAf(float r, float g, float b, float a)
  347. {
  348. NVGcolor color;
  349. // Use longer initialization to suppress warning.
  350. color.r = r;
  351. color.g = g;
  352. color.b = b;
  353. color.a = a;
  354. return color;
  355. }
  356. NVGcolor nvgTransRGBA(NVGcolor c, unsigned char a)
  357. {
  358. c.a = a / 255.0f;
  359. return c;
  360. }
  361. NVGcolor nvgTransRGBAf(NVGcolor c, float a)
  362. {
  363. c.a = a;
  364. return c;
  365. }
  366. NVGcolor nvgLerpRGBA(NVGcolor c0, NVGcolor c1, float u)
  367. {
  368. int i;
  369. float oneminu;
  370. NVGcolor cint;
  371. u = nvg__clampf(u, 0.0f, 1.0f);
  372. oneminu = 1.0f - u;
  373. for( i = 0; i <4; i++ )
  374. {
  375. cint.rgba[i] = c0.rgba[i] * oneminu + c1.rgba[i] * u;
  376. }
  377. return cint;
  378. }
  379. NVGcolor nvgHSL(float h, float s, float l)
  380. {
  381. return nvgHSLA(h,s,l,255);
  382. }
  383. static float nvg__hue(float h, float m1, float m2)
  384. {
  385. if (h < 0) h += 1;
  386. if (h > 1) h -= 1;
  387. if (h < 1.0f/6.0f)
  388. return m1 + (m2 - m1) * h * 6.0f;
  389. else if (h < 3.0f/6.0f)
  390. return m2;
  391. else if (h < 4.0f/6.0f)
  392. return m1 + (m2 - m1) * (2.0f/3.0f - h) * 6.0f;
  393. return m1;
  394. }
  395. NVGcolor nvgHSLA(float h, float s, float l, unsigned char a)
  396. {
  397. float m1, m2;
  398. NVGcolor col;
  399. h = nvg__modf(h, 1.0f);
  400. if (h < 0.0f) h += 1.0f;
  401. s = nvg__clampf(s, 0.0f, 1.0f);
  402. l = nvg__clampf(l, 0.0f, 1.0f);
  403. m2 = l <= 0.5f ? (l * (1 + s)) : (l + s - l * s);
  404. m1 = 2 * l - m2;
  405. col.r = nvg__clampf(nvg__hue(h + 1.0f/3.0f, m1, m2), 0.0f, 1.0f);
  406. col.g = nvg__clampf(nvg__hue(h, m1, m2), 0.0f, 1.0f);
  407. col.b = nvg__clampf(nvg__hue(h - 1.0f/3.0f, m1, m2), 0.0f, 1.0f);
  408. col.a = a/255.0f;
  409. return col;
  410. }
  411. static NVGstate* nvg__getState(NVGcontext* ctx)
  412. {
  413. return &ctx->states[ctx->nstates-1];
  414. }
  415. void nvgTransformIdentity(float* t)
  416. {
  417. t[0] = 1.0f; t[1] = 0.0f;
  418. t[2] = 0.0f; t[3] = 1.0f;
  419. t[4] = 0.0f; t[5] = 0.0f;
  420. }
  421. void nvgTransformTranslate(float* t, float tx, float ty)
  422. {
  423. t[0] = 1.0f; t[1] = 0.0f;
  424. t[2] = 0.0f; t[3] = 1.0f;
  425. t[4] = tx; t[5] = ty;
  426. }
  427. void nvgTransformScale(float* t, float sx, float sy)
  428. {
  429. t[0] = sx; t[1] = 0.0f;
  430. t[2] = 0.0f; t[3] = sy;
  431. t[4] = 0.0f; t[5] = 0.0f;
  432. }
  433. void nvgTransformRotate(float* t, float a)
  434. {
  435. float cs = nvg__cosf(a), sn = nvg__sinf(a);
  436. t[0] = cs; t[1] = sn;
  437. t[2] = -sn; t[3] = cs;
  438. t[4] = 0.0f; t[5] = 0.0f;
  439. }
  440. void nvgTransformSkewX(float* t, float a)
  441. {
  442. t[0] = 1.0f; t[1] = 0.0f;
  443. t[2] = nvg__tanf(a); t[3] = 1.0f;
  444. t[4] = 0.0f; t[5] = 0.0f;
  445. }
  446. void nvgTransformSkewY(float* t, float a)
  447. {
  448. t[0] = 1.0f; t[1] = nvg__tanf(a);
  449. t[2] = 0.0f; t[3] = 1.0f;
  450. t[4] = 0.0f; t[5] = 0.0f;
  451. }
  452. void nvgTransformMultiply(float* t, const float* s)
  453. {
  454. float t0 = t[0] * s[0] + t[1] * s[2];
  455. float t2 = t[2] * s[0] + t[3] * s[2];
  456. float t4 = t[4] * s[0] + t[5] * s[2] + s[4];
  457. t[1] = t[0] * s[1] + t[1] * s[3];
  458. t[3] = t[2] * s[1] + t[3] * s[3];
  459. t[5] = t[4] * s[1] + t[5] * s[3] + s[5];
  460. t[0] = t0;
  461. t[2] = t2;
  462. t[4] = t4;
  463. }
  464. void nvgTransformPremultiply(float* t, const float* s)
  465. {
  466. float s2[6];
  467. memcpy(s2, s, sizeof(float)*6);
  468. nvgTransformMultiply(s2, t);
  469. memcpy(t, s2, sizeof(float)*6);
  470. }
  471. int nvgTransformInverse(float* inv, const float* t)
  472. {
  473. double invdet, det = (double)t[0] * t[3] - (double)t[2] * t[1];
  474. if (det > -1e-6 && det < 1e-6) {
  475. nvgTransformIdentity(inv);
  476. return 0;
  477. }
  478. invdet = 1.0 / det;
  479. inv[0] = (float)(t[3] * invdet);
  480. inv[2] = (float)(-t[2] * invdet);
  481. inv[4] = (float)(((double)t[2] * t[5] - (double)t[3] * t[4]) * invdet);
  482. inv[1] = (float)(-t[1] * invdet);
  483. inv[3] = (float)(t[0] * invdet);
  484. inv[5] = (float)(((double)t[1] * t[4] - (double)t[0] * t[5]) * invdet);
  485. return 1;
  486. }
  487. void nvgTransformPoint(float* dx, float* dy, const float* t, float sx, float sy)
  488. {
  489. *dx = sx*t[0] + sy*t[2] + t[4];
  490. *dy = sx*t[1] + sy*t[3] + t[5];
  491. }
  492. float nvgDegToRad(float deg)
  493. {
  494. return deg / 180.0f * NVG_PI;
  495. }
  496. float nvgRadToDeg(float rad)
  497. {
  498. return rad / NVG_PI * 180.0f;
  499. }
  500. static void nvg__setPaintColor(NVGpaint* p, NVGcolor color)
  501. {
  502. memset(p, 0, sizeof(*p));
  503. nvgTransformIdentity(p->xform);
  504. p->radius = 0.0f;
  505. p->feather = 1.0f;
  506. p->innerColor = color;
  507. p->outerColor = color;
  508. }
  509. // State handling
  510. void nvgSave(NVGcontext* ctx)
  511. {
  512. if (ctx->nstates >= NVG_MAX_STATES)
  513. return;
  514. if (ctx->nstates > 0)
  515. memcpy(&ctx->states[ctx->nstates], &ctx->states[ctx->nstates-1], sizeof(NVGstate));
  516. ctx->nstates++;
  517. }
  518. void nvgRestore(NVGcontext* ctx)
  519. {
  520. if (ctx->nstates <= 1)
  521. return;
  522. ctx->nstates--;
  523. }
  524. void nvgReset(NVGcontext* ctx)
  525. {
  526. NVGstate* state = nvg__getState(ctx);
  527. memset(state, 0, sizeof(*state));
  528. nvg__setPaintColor(&state->fill, nvgRGBA(255,255,255,255));
  529. nvg__setPaintColor(&state->stroke, nvgRGBA(0,0,0,255));
  530. state->strokeWidth = 1.0f;
  531. state->miterLimit = 10.0f;
  532. state->lineCap = NVG_BUTT;
  533. state->lineJoin = NVG_MITER;
  534. state->alpha = 1.0f;
  535. nvgTransformIdentity(state->xform);
  536. state->scissor.extent[0] = -1.0f;
  537. state->scissor.extent[1] = -1.0f;
  538. state->fontSize = 16.0f;
  539. state->letterSpacing = 0.0f;
  540. state->lineHeight = 1.0f;
  541. state->fontBlur = 0.0f;
  542. state->textAlign = NVG_ALIGN_LEFT | NVG_ALIGN_BASELINE;
  543. state->fontId = 0;
  544. }
  545. // State setting
  546. void nvgStrokeWidth(NVGcontext* ctx, float width)
  547. {
  548. NVGstate* state = nvg__getState(ctx);
  549. state->strokeWidth = width;
  550. }
  551. void nvgMiterLimit(NVGcontext* ctx, float limit)
  552. {
  553. NVGstate* state = nvg__getState(ctx);
  554. state->miterLimit = limit;
  555. }
  556. void nvgLineCap(NVGcontext* ctx, int cap)
  557. {
  558. NVGstate* state = nvg__getState(ctx);
  559. state->lineCap = cap;
  560. }
  561. void nvgLineJoin(NVGcontext* ctx, int join)
  562. {
  563. NVGstate* state = nvg__getState(ctx);
  564. state->lineJoin = join;
  565. }
  566. void nvgGlobalAlpha(NVGcontext* ctx, float alpha)
  567. {
  568. NVGstate* state = nvg__getState(ctx);
  569. state->alpha = alpha;
  570. }
  571. void nvgTransform(NVGcontext* ctx, float a, float b, float c, float d, float e, float f)
  572. {
  573. NVGstate* state = nvg__getState(ctx);
  574. float t[6] = { a, b, c, d, e, f };
  575. nvgTransformPremultiply(state->xform, t);
  576. }
  577. void nvgResetTransform(NVGcontext* ctx)
  578. {
  579. NVGstate* state = nvg__getState(ctx);
  580. nvgTransformIdentity(state->xform);
  581. }
  582. void nvgTranslate(NVGcontext* ctx, float x, float y)
  583. {
  584. NVGstate* state = nvg__getState(ctx);
  585. float t[6];
  586. nvgTransformTranslate(t, x,y);
  587. nvgTransformPremultiply(state->xform, t);
  588. }
  589. void nvgRotate(NVGcontext* ctx, float angle)
  590. {
  591. NVGstate* state = nvg__getState(ctx);
  592. float t[6];
  593. nvgTransformRotate(t, angle);
  594. nvgTransformPremultiply(state->xform, t);
  595. }
  596. void nvgSkewX(NVGcontext* ctx, float angle)
  597. {
  598. NVGstate* state = nvg__getState(ctx);
  599. float t[6];
  600. nvgTransformSkewX(t, angle);
  601. nvgTransformPremultiply(state->xform, t);
  602. }
  603. void nvgSkewY(NVGcontext* ctx, float angle)
  604. {
  605. NVGstate* state = nvg__getState(ctx);
  606. float t[6];
  607. nvgTransformSkewY(t, angle);
  608. nvgTransformPremultiply(state->xform, t);
  609. }
  610. void nvgScale(NVGcontext* ctx, float x, float y)
  611. {
  612. NVGstate* state = nvg__getState(ctx);
  613. float t[6];
  614. nvgTransformScale(t, x,y);
  615. nvgTransformPremultiply(state->xform, t);
  616. }
  617. void nvgCurrentTransform(NVGcontext* ctx, float* xform)
  618. {
  619. NVGstate* state = nvg__getState(ctx);
  620. if (xform == NULL) return;
  621. memcpy(xform, state->xform, sizeof(float)*6);
  622. }
  623. void nvgStrokeColor(NVGcontext* ctx, NVGcolor color)
  624. {
  625. NVGstate* state = nvg__getState(ctx);
  626. nvg__setPaintColor(&state->stroke, color);
  627. }
  628. void nvgStrokePaint(NVGcontext* ctx, NVGpaint paint)
  629. {
  630. NVGstate* state = nvg__getState(ctx);
  631. state->stroke = paint;
  632. nvgTransformMultiply(state->stroke.xform, state->xform);
  633. }
  634. void nvgFillColor(NVGcontext* ctx, NVGcolor color)
  635. {
  636. NVGstate* state = nvg__getState(ctx);
  637. nvg__setPaintColor(&state->fill, color);
  638. }
  639. void nvgFillPaint(NVGcontext* ctx, NVGpaint paint)
  640. {
  641. NVGstate* state = nvg__getState(ctx);
  642. state->fill = paint;
  643. nvgTransformMultiply(state->fill.xform, state->xform);
  644. }
  645. int nvgCreateImage(NVGcontext* ctx, const char* filename, int imageFlags)
  646. {
  647. int w, h, n, image;
  648. unsigned char* img;
  649. stbi_set_unpremultiply_on_load(1);
  650. stbi_convert_iphone_png_to_rgb(1);
  651. img = stbi_load(filename, &w, &h, &n, 4);
  652. if (img == NULL) {
  653. // printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
  654. return 0;
  655. }
  656. image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
  657. stbi_image_free(img);
  658. return image;
  659. }
  660. int nvgCreateImageMem(NVGcontext* ctx, int imageFlags, unsigned char* data, int ndata)
  661. {
  662. int w, h, n, image;
  663. unsigned char* img = stbi_load_from_memory(data, ndata, &w, &h, &n, 4);
  664. if (img == NULL) {
  665. // printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
  666. return 0;
  667. }
  668. image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
  669. stbi_image_free(img);
  670. return image;
  671. }
  672. int nvgCreateImageRGBA(NVGcontext* ctx, int w, int h, int imageFlags, const unsigned char* data)
  673. {
  674. return ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_RGBA, w, h, imageFlags, data);
  675. }
  676. void nvgUpdateImage(NVGcontext* ctx, int image, const unsigned char* data)
  677. {
  678. int w, h;
  679. ctx->params.renderGetTextureSize(ctx->params.userPtr, image, &w, &h);
  680. ctx->params.renderUpdateTexture(ctx->params.userPtr, image, 0,0, w,h, data);
  681. }
  682. void nvgImageSize(NVGcontext* ctx, int image, int* w, int* h)
  683. {
  684. ctx->params.renderGetTextureSize(ctx->params.userPtr, image, w, h);
  685. }
  686. void nvgDeleteImage(NVGcontext* ctx, int image)
  687. {
  688. ctx->params.renderDeleteTexture(ctx->params.userPtr, image);
  689. }
  690. NVGpaint nvgLinearGradient(NVGcontext* ctx,
  691. float sx, float sy, float ex, float ey,
  692. NVGcolor icol, NVGcolor ocol)
  693. {
  694. NVGpaint p;
  695. float dx, dy, d;
  696. const float large = 1e5;
  697. NVG_NOTUSED(ctx);
  698. memset(&p, 0, sizeof(p));
  699. // Calculate transform aligned to the line
  700. dx = ex - sx;
  701. dy = ey - sy;
  702. d = sqrtf(dx*dx + dy*dy);
  703. if (d > 0.0001f) {
  704. dx /= d;
  705. dy /= d;
  706. } else {
  707. dx = 0;
  708. dy = 1;
  709. }
  710. p.xform[0] = dy; p.xform[1] = -dx;
  711. p.xform[2] = dx; p.xform[3] = dy;
  712. p.xform[4] = sx - dx*large; p.xform[5] = sy - dy*large;
  713. p.extent[0] = large;
  714. p.extent[1] = large + d*0.5f;
  715. p.radius = 0.0f;
  716. p.feather = nvg__maxf(1.0f, d);
  717. p.innerColor = icol;
  718. p.outerColor = ocol;
  719. return p;
  720. }
  721. NVGpaint nvgRadialGradient(NVGcontext* ctx,
  722. float cx, float cy, float inr, float outr,
  723. NVGcolor icol, NVGcolor ocol)
  724. {
  725. NVGpaint p;
  726. float r = (inr+outr)*0.5f;
  727. float f = (outr-inr);
  728. NVG_NOTUSED(ctx);
  729. memset(&p, 0, sizeof(p));
  730. nvgTransformIdentity(p.xform);
  731. p.xform[4] = cx;
  732. p.xform[5] = cy;
  733. p.extent[0] = r;
  734. p.extent[1] = r;
  735. p.radius = r;
  736. p.feather = nvg__maxf(1.0f, f);
  737. p.innerColor = icol;
  738. p.outerColor = ocol;
  739. return p;
  740. }
  741. NVGpaint nvgBoxGradient(NVGcontext* ctx,
  742. float x, float y, float w, float h, float r, float f,
  743. NVGcolor icol, NVGcolor ocol)
  744. {
  745. NVGpaint p;
  746. NVG_NOTUSED(ctx);
  747. memset(&p, 0, sizeof(p));
  748. nvgTransformIdentity(p.xform);
  749. p.xform[4] = x+w*0.5f;
  750. p.xform[5] = y+h*0.5f;
  751. p.extent[0] = w*0.5f;
  752. p.extent[1] = h*0.5f;
  753. p.radius = r;
  754. p.feather = nvg__maxf(1.0f, f);
  755. p.innerColor = icol;
  756. p.outerColor = ocol;
  757. return p;
  758. }
  759. NVGpaint nvgImagePattern(NVGcontext* ctx,
  760. float cx, float cy, float w, float h, float angle,
  761. int image, float alpha)
  762. {
  763. NVGpaint p;
  764. NVG_NOTUSED(ctx);
  765. memset(&p, 0, sizeof(p));
  766. nvgTransformRotate(p.xform, angle);
  767. p.xform[4] = cx;
  768. p.xform[5] = cy;
  769. p.extent[0] = w;
  770. p.extent[1] = h;
  771. p.image = image;
  772. p.innerColor = p.outerColor = nvgRGBAf(1,1,1,alpha);
  773. return p;
  774. }
  775. // Scissoring
  776. void nvgScissor(NVGcontext* ctx, float x, float y, float w, float h)
  777. {
  778. NVGstate* state = nvg__getState(ctx);
  779. w = nvg__maxf(0.0f, w);
  780. h = nvg__maxf(0.0f, h);
  781. nvgTransformIdentity(state->scissor.xform);
  782. state->scissor.xform[4] = x+w*0.5f;
  783. state->scissor.xform[5] = y+h*0.5f;
  784. nvgTransformMultiply(state->scissor.xform, state->xform);
  785. state->scissor.extent[0] = w*0.5f;
  786. state->scissor.extent[1] = h*0.5f;
  787. }
  788. static void nvg__isectRects(float* dst,
  789. float ax, float ay, float aw, float ah,
  790. float bx, float by, float bw, float bh)
  791. {
  792. float minx = nvg__maxf(ax, bx);
  793. float miny = nvg__maxf(ay, by);
  794. float maxx = nvg__minf(ax+aw, bx+bw);
  795. float maxy = nvg__minf(ay+ah, by+bh);
  796. dst[0] = minx;
  797. dst[1] = miny;
  798. dst[2] = nvg__maxf(0.0f, maxx - minx);
  799. dst[3] = nvg__maxf(0.0f, maxy - miny);
  800. }
  801. void nvgIntersectScissor(NVGcontext* ctx, float x, float y, float w, float h)
  802. {
  803. NVGstate* state = nvg__getState(ctx);
  804. float pxform[6], invxorm[6];
  805. float rect[4];
  806. float ex, ey, tex, tey;
  807. // If no previous scissor has been set, set the scissor as current scissor.
  808. if (state->scissor.extent[0] < 0) {
  809. nvgScissor(ctx, x, y, w, h);
  810. return;
  811. }
  812. // Transform the current scissor rect into current transform space.
  813. // If there is difference in rotation, this will be approximation.
  814. memcpy(pxform, state->scissor.xform, sizeof(float)*6);
  815. ex = state->scissor.extent[0];
  816. ey = state->scissor.extent[1];
  817. nvgTransformInverse(invxorm, state->xform);
  818. nvgTransformMultiply(pxform, invxorm);
  819. tex = ex*nvg__absf(pxform[0]) + ey*nvg__absf(pxform[2]);
  820. tey = ex*nvg__absf(pxform[1]) + ey*nvg__absf(pxform[3]);
  821. // Intersect rects.
  822. nvg__isectRects(rect, pxform[4]-tex,pxform[5]-tey,tex*2,tey*2, x,y,w,h);
  823. nvgScissor(ctx, rect[0], rect[1], rect[2], rect[3]);
  824. }
  825. void nvgResetScissor(NVGcontext* ctx)
  826. {
  827. NVGstate* state = nvg__getState(ctx);
  828. memset(state->scissor.xform, 0, sizeof(state->scissor.xform));
  829. state->scissor.extent[0] = -1.0f;
  830. state->scissor.extent[1] = -1.0f;
  831. }
  832. static int nvg__ptEquals(float x1, float y1, float x2, float y2, float tol)
  833. {
  834. float dx = x2 - x1;
  835. float dy = y2 - y1;
  836. return dx*dx + dy*dy < tol*tol;
  837. }
  838. static float nvg__distPtSeg(float x, float y, float px, float py, float qx, float qy)
  839. {
  840. float pqx, pqy, dx, dy, d, t;
  841. pqx = qx-px;
  842. pqy = qy-py;
  843. dx = x-px;
  844. dy = y-py;
  845. d = pqx*pqx + pqy*pqy;
  846. t = pqx*dx + pqy*dy;
  847. if (d > 0) t /= d;
  848. if (t < 0) t = 0;
  849. else if (t > 1) t = 1;
  850. dx = px + t*pqx - x;
  851. dy = py + t*pqy - y;
  852. return dx*dx + dy*dy;
  853. }
  854. static void nvg__appendCommands(NVGcontext* ctx, float* vals, int nvals)
  855. {
  856. NVGstate* state = nvg__getState(ctx);
  857. int i;
  858. if (ctx->ncommands+nvals > ctx->ccommands) {
  859. float* commands;
  860. int ccommands = ctx->ncommands+nvals + ctx->ccommands/2;
  861. commands = (float*)realloc(ctx->commands, sizeof(float)*ccommands);
  862. if (commands == NULL) return;
  863. ctx->commands = commands;
  864. ctx->ccommands = ccommands;
  865. }
  866. if ((int)vals[0] != NVG_CLOSE && (int)vals[0] != NVG_WINDING) {
  867. ctx->commandx = vals[nvals-2];
  868. ctx->commandy = vals[nvals-1];
  869. }
  870. // transform commands
  871. i = 0;
  872. while (i < nvals) {
  873. int cmd = (int)vals[i];
  874. switch (cmd) {
  875. case NVG_MOVETO:
  876. nvgTransformPoint(&vals[i+1],&vals[i+2], state->xform, vals[i+1],vals[i+2]);
  877. i += 3;
  878. break;
  879. case NVG_LINETO:
  880. nvgTransformPoint(&vals[i+1],&vals[i+2], state->xform, vals[i+1],vals[i+2]);
  881. i += 3;
  882. break;
  883. case NVG_BEZIERTO:
  884. nvgTransformPoint(&vals[i+1],&vals[i+2], state->xform, vals[i+1],vals[i+2]);
  885. nvgTransformPoint(&vals[i+3],&vals[i+4], state->xform, vals[i+3],vals[i+4]);
  886. nvgTransformPoint(&vals[i+5],&vals[i+6], state->xform, vals[i+5],vals[i+6]);
  887. i += 7;
  888. break;
  889. case NVG_CLOSE:
  890. i++;
  891. break;
  892. case NVG_WINDING:
  893. i += 2;
  894. break;
  895. default:
  896. i++;
  897. }
  898. }
  899. memcpy(&ctx->commands[ctx->ncommands], vals, nvals*sizeof(float));
  900. ctx->ncommands += nvals;
  901. }
  902. static void nvg__clearPathCache(NVGcontext* ctx)
  903. {
  904. ctx->cache->npoints = 0;
  905. ctx->cache->npaths = 0;
  906. }
  907. static NVGpath* nvg__lastPath(NVGcontext* ctx)
  908. {
  909. if (ctx->cache->npaths > 0)
  910. return &ctx->cache->paths[ctx->cache->npaths-1];
  911. return NULL;
  912. }
  913. static void nvg__addPath(NVGcontext* ctx)
  914. {
  915. NVGpath* path;
  916. if (ctx->cache->npaths+1 > ctx->cache->cpaths) {
  917. NVGpath* paths;
  918. int cpaths = ctx->cache->npaths+1 + ctx->cache->cpaths/2;
  919. paths = (NVGpath*)realloc(ctx->cache->paths, sizeof(NVGpath)*cpaths);
  920. if (paths == NULL) return;
  921. ctx->cache->paths = paths;
  922. ctx->cache->cpaths = cpaths;
  923. }
  924. path = &ctx->cache->paths[ctx->cache->npaths];
  925. memset(path, 0, sizeof(*path));
  926. path->first = ctx->cache->npoints;
  927. path->winding = NVG_CCW;
  928. ctx->cache->npaths++;
  929. }
  930. static NVGpoint* nvg__lastPoint(NVGcontext* ctx)
  931. {
  932. if (ctx->cache->npoints > 0)
  933. return &ctx->cache->points[ctx->cache->npoints-1];
  934. return NULL;
  935. }
  936. static void nvg__addPoint(NVGcontext* ctx, float x, float y, int flags)
  937. {
  938. NVGpath* path = nvg__lastPath(ctx);
  939. NVGpoint* pt;
  940. if (path == NULL) return;
  941. if (path->count > 0 && ctx->cache->npoints > 0) {
  942. pt = nvg__lastPoint(ctx);
  943. if (nvg__ptEquals(pt->x,pt->y, x,y, ctx->distTol)) {
  944. pt->flags |= flags;
  945. return;
  946. }
  947. }
  948. if (ctx->cache->npoints+1 > ctx->cache->cpoints) {
  949. NVGpoint* points;
  950. int cpoints = ctx->cache->npoints+1 + ctx->cache->cpoints/2;
  951. points = (NVGpoint*)realloc(ctx->cache->points, sizeof(NVGpoint)*cpoints);
  952. if (points == NULL) return;
  953. ctx->cache->points = points;
  954. ctx->cache->cpoints = cpoints;
  955. }
  956. pt = &ctx->cache->points[ctx->cache->npoints];
  957. memset(pt, 0, sizeof(*pt));
  958. pt->x = x;
  959. pt->y = y;
  960. pt->flags = (unsigned char)flags;
  961. ctx->cache->npoints++;
  962. path->count++;
  963. }
  964. static void nvg__closePath(NVGcontext* ctx)
  965. {
  966. NVGpath* path = nvg__lastPath(ctx);
  967. if (path == NULL) return;
  968. path->closed = 1;
  969. }
  970. static void nvg__pathWinding(NVGcontext* ctx, int winding)
  971. {
  972. NVGpath* path = nvg__lastPath(ctx);
  973. if (path == NULL) return;
  974. path->winding = winding;
  975. }
  976. static float nvg__getAverageScale(float *t)
  977. {
  978. float sx = sqrtf(t[0]*t[0] + t[2]*t[2]);
  979. float sy = sqrtf(t[1]*t[1] + t[3]*t[3]);
  980. return (sx + sy) * 0.5f;
  981. }
  982. static NVGvertex* nvg__allocTempVerts(NVGcontext* ctx, int nverts)
  983. {
  984. if (nverts > ctx->cache->cverts) {
  985. NVGvertex* verts;
  986. int cverts = (nverts + 0xff) & ~0xff; // Round up to prevent allocations when things change just slightly.
  987. verts = (NVGvertex*)realloc(ctx->cache->verts, sizeof(NVGvertex)*cverts);
  988. if (verts == NULL) return NULL;
  989. ctx->cache->verts = verts;
  990. ctx->cache->cverts = cverts;
  991. }
  992. return ctx->cache->verts;
  993. }
  994. static float nvg__triarea2(float ax, float ay, float bx, float by, float cx, float cy)
  995. {
  996. float abx = bx - ax;
  997. float aby = by - ay;
  998. float acx = cx - ax;
  999. float acy = cy - ay;
  1000. return acx*aby - abx*acy;
  1001. }
  1002. static float nvg__polyArea(NVGpoint* pts, int npts)
  1003. {
  1004. int i;
  1005. float area = 0;
  1006. for (i = 2; i < npts; i++) {
  1007. NVGpoint* a = &pts[0];
  1008. NVGpoint* b = &pts[i-1];
  1009. NVGpoint* c = &pts[i];
  1010. area += nvg__triarea2(a->x,a->y, b->x,b->y, c->x,c->y);
  1011. }
  1012. return area * 0.5f;
  1013. }
  1014. static void nvg__polyReverse(NVGpoint* pts, int npts)
  1015. {
  1016. NVGpoint tmp;
  1017. int i = 0, j = npts-1;
  1018. while (i < j) {
  1019. tmp = pts[i];
  1020. pts[i] = pts[j];
  1021. pts[j] = tmp;
  1022. i++;
  1023. j--;
  1024. }
  1025. }
  1026. static void nvg__vset(NVGvertex* vtx, float x, float y, float u, float v)
  1027. {
  1028. vtx->x = x;
  1029. vtx->y = y;
  1030. vtx->u = u;
  1031. vtx->v = v;
  1032. }
  1033. static void nvg__tesselateBezier(NVGcontext* ctx,
  1034. float x1, float y1, float x2, float y2,
  1035. float x3, float y3, float x4, float y4,
  1036. int level, int type)
  1037. {
  1038. float x12,y12,x23,y23,x34,y34,x123,y123,x234,y234,x1234,y1234;
  1039. float dx,dy,d2,d3;
  1040. if (level > 10) return;
  1041. x12 = (x1+x2)*0.5f;
  1042. y12 = (y1+y2)*0.5f;
  1043. x23 = (x2+x3)*0.5f;
  1044. y23 = (y2+y3)*0.5f;
  1045. x34 = (x3+x4)*0.5f;
  1046. y34 = (y3+y4)*0.5f;
  1047. x123 = (x12+x23)*0.5f;
  1048. y123 = (y12+y23)*0.5f;
  1049. dx = x4 - x1;
  1050. dy = y4 - y1;
  1051. d2 = nvg__absf(((x2 - x4) * dy - (y2 - y4) * dx));
  1052. d3 = nvg__absf(((x3 - x4) * dy - (y3 - y4) * dx));
  1053. if ((d2 + d3)*(d2 + d3) < ctx->tessTol * (dx*dx + dy*dy)) {
  1054. nvg__addPoint(ctx, x4, y4, type);
  1055. return;
  1056. }
  1057. /* if (nvg__absf(x1+x3-x2-x2) + nvg__absf(y1+y3-y2-y2) + nvg__absf(x2+x4-x3-x3) + nvg__absf(y2+y4-y3-y3) < ctx->tessTol) {
  1058. nvg__addPoint(ctx, x4, y4, type);
  1059. return;
  1060. }*/
  1061. x234 = (x23+x34)*0.5f;
  1062. y234 = (y23+y34)*0.5f;
  1063. x1234 = (x123+x234)*0.5f;
  1064. y1234 = (y123+y234)*0.5f;
  1065. nvg__tesselateBezier(ctx, x1,y1, x12,y12, x123,y123, x1234,y1234, level+1, 0);
  1066. nvg__tesselateBezier(ctx, x1234,y1234, x234,y234, x34,y34, x4,y4, level+1, type);
  1067. }
  1068. static void nvg__flattenPaths(NVGcontext* ctx)
  1069. {
  1070. NVGpathCache* cache = ctx->cache;
  1071. // NVGstate* state = nvg__getState(ctx);
  1072. NVGpoint* last;
  1073. NVGpoint* p0;
  1074. NVGpoint* p1;
  1075. NVGpoint* pts;
  1076. NVGpath* path;
  1077. int i, j;
  1078. float* cp1;
  1079. float* cp2;
  1080. float* p;
  1081. float area;
  1082. if (cache->npaths > 0)
  1083. return;
  1084. // Flatten
  1085. i = 0;
  1086. while (i < ctx->ncommands) {
  1087. int cmd = (int)ctx->commands[i];
  1088. switch (cmd) {
  1089. case NVG_MOVETO:
  1090. nvg__addPath(ctx);
  1091. p = &ctx->commands[i+1];
  1092. nvg__addPoint(ctx, p[0], p[1], NVG_PT_CORNER);
  1093. i += 3;
  1094. break;
  1095. case NVG_LINETO:
  1096. p = &ctx->commands[i+1];
  1097. nvg__addPoint(ctx, p[0], p[1], NVG_PT_CORNER);
  1098. i += 3;
  1099. break;
  1100. case NVG_BEZIERTO:
  1101. last = nvg__lastPoint(ctx);
  1102. if (last != NULL) {
  1103. cp1 = &ctx->commands[i+1];
  1104. cp2 = &ctx->commands[i+3];
  1105. p = &ctx->commands[i+5];
  1106. nvg__tesselateBezier(ctx, last->x,last->y, cp1[0],cp1[1], cp2[0],cp2[1], p[0],p[1], 0, NVG_PT_CORNER);
  1107. }
  1108. i += 7;
  1109. break;
  1110. case NVG_CLOSE:
  1111. nvg__closePath(ctx);
  1112. i++;
  1113. break;
  1114. case NVG_WINDING:
  1115. nvg__pathWinding(ctx, (int)ctx->commands[i+1]);
  1116. i += 2;
  1117. break;
  1118. default:
  1119. i++;
  1120. }
  1121. }
  1122. cache->bounds[0] = cache->bounds[1] = 1e6f;
  1123. cache->bounds[2] = cache->bounds[3] = -1e6f;
  1124. // Calculate the direction and length of line segments.
  1125. for (j = 0; j < cache->npaths; j++) {
  1126. path = &cache->paths[j];
  1127. pts = &cache->points[path->first];
  1128. // If the first and last points are the same, remove the last, mark as closed path.
  1129. p0 = &pts[path->count-1];
  1130. p1 = &pts[0];
  1131. if (nvg__ptEquals(p0->x,p0->y, p1->x,p1->y, ctx->distTol)) {
  1132. path->count--;
  1133. p0 = &pts[path->count-1];
  1134. path->closed = 1;
  1135. }
  1136. // Enforce winding.
  1137. if (path->count > 2) {
  1138. area = nvg__polyArea(pts, path->count);
  1139. if (path->winding == NVG_CCW && area < 0.0f)
  1140. nvg__polyReverse(pts, path->count);
  1141. if (path->winding == NVG_CW && area > 0.0f)
  1142. nvg__polyReverse(pts, path->count);
  1143. }
  1144. for(i = 0; i < path->count; i++) {
  1145. // Calculate segment direction and length
  1146. p0->dx = p1->x - p0->x;
  1147. p0->dy = p1->y - p0->y;
  1148. p0->len = nvg__normalize(&p0->dx, &p0->dy);
  1149. // Update bounds
  1150. cache->bounds[0] = nvg__minf(cache->bounds[0], p0->x);
  1151. cache->bounds[1] = nvg__minf(cache->bounds[1], p0->y);
  1152. cache->bounds[2] = nvg__maxf(cache->bounds[2], p0->x);
  1153. cache->bounds[3] = nvg__maxf(cache->bounds[3], p0->y);
  1154. // Advance
  1155. p0 = p1++;
  1156. }
  1157. }
  1158. }
  1159. static int nvg__curveDivs(float r, float arc, float tol)
  1160. {
  1161. float da = acosf(r / (r + tol)) * 2.0f;
  1162. return nvg__maxi(2, (int)ceilf(arc / da));
  1163. }
  1164. static void nvg__chooseBevel(int bevel, NVGpoint* p0, NVGpoint* p1, float w,
  1165. float* x0, float* y0, float* x1, float* y1)
  1166. {
  1167. if (bevel) {
  1168. *x0 = p1->x + p0->dy * w;
  1169. *y0 = p1->y - p0->dx * w;
  1170. *x1 = p1->x + p1->dy * w;
  1171. *y1 = p1->y - p1->dx * w;
  1172. } else {
  1173. *x0 = p1->x + p1->dmx * w;
  1174. *y0 = p1->y + p1->dmy * w;
  1175. *x1 = p1->x + p1->dmx * w;
  1176. *y1 = p1->y + p1->dmy * w;
  1177. }
  1178. }
  1179. static NVGvertex* nvg__roundJoin(NVGvertex* dst, NVGpoint* p0, NVGpoint* p1,
  1180. float lw, float rw, float lu, float ru, int ncap, float fringe)
  1181. {
  1182. int i, n;
  1183. float dlx0 = p0->dy;
  1184. float dly0 = -p0->dx;
  1185. float dlx1 = p1->dy;
  1186. float dly1 = -p1->dx;
  1187. NVG_NOTUSED(fringe);
  1188. if (p1->flags & NVG_PT_LEFT) {
  1189. float lx0,ly0,lx1,ly1,a0,a1;
  1190. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, lw, &lx0,&ly0, &lx1,&ly1);
  1191. a0 = atan2f(-dly0, -dlx0);
  1192. a1 = atan2f(-dly1, -dlx1);
  1193. if (a1 > a0) a1 -= NVG_PI*2;
  1194. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1195. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1196. n = nvg__clampi((int)ceilf(((a0 - a1) / NVG_PI) * ncap), 2, ncap);
  1197. for (i = 0; i < n; i++) {
  1198. float u = i/(float)(n-1);
  1199. float a = a0 + u*(a1-a0);
  1200. float rx = p1->x + cosf(a) * rw;
  1201. float ry = p1->y + sinf(a) * rw;
  1202. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1203. nvg__vset(dst, rx, ry, ru,1); dst++;
  1204. }
  1205. nvg__vset(dst, lx1, ly1, lu,1); dst++;
  1206. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1207. } else {
  1208. float rx0,ry0,rx1,ry1,a0,a1;
  1209. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, -rw, &rx0,&ry0, &rx1,&ry1);
  1210. a0 = atan2f(dly0, dlx0);
  1211. a1 = atan2f(dly1, dlx1);
  1212. if (a1 < a0) a1 += NVG_PI*2;
  1213. nvg__vset(dst, p1->x + dlx0*rw, p1->y + dly0*rw, lu,1); dst++;
  1214. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1215. n = nvg__clampi((int)ceilf(((a1 - a0) / NVG_PI) * ncap), 2, ncap);
  1216. for (i = 0; i < n; i++) {
  1217. float u = i/(float)(n-1);
  1218. float a = a0 + u*(a1-a0);
  1219. float lx = p1->x + cosf(a) * lw;
  1220. float ly = p1->y + sinf(a) * lw;
  1221. nvg__vset(dst, lx, ly, lu,1); dst++;
  1222. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1223. }
  1224. nvg__vset(dst, p1->x + dlx1*rw, p1->y + dly1*rw, lu,1); dst++;
  1225. nvg__vset(dst, rx1, ry1, ru,1); dst++;
  1226. }
  1227. return dst;
  1228. }
  1229. static NVGvertex* nvg__bevelJoin(NVGvertex* dst, NVGpoint* p0, NVGpoint* p1,
  1230. float lw, float rw, float lu, float ru, float fringe)
  1231. {
  1232. float rx0,ry0,rx1,ry1;
  1233. float lx0,ly0,lx1,ly1;
  1234. float dlx0 = p0->dy;
  1235. float dly0 = -p0->dx;
  1236. float dlx1 = p1->dy;
  1237. float dly1 = -p1->dx;
  1238. NVG_NOTUSED(fringe);
  1239. if (p1->flags & NVG_PT_LEFT) {
  1240. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, lw, &lx0,&ly0, &lx1,&ly1);
  1241. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1242. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1243. if (p1->flags & NVG_PT_BEVEL) {
  1244. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1245. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1246. nvg__vset(dst, lx1, ly1, lu,1); dst++;
  1247. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1248. } else {
  1249. rx0 = p1->x - p1->dmx * rw;
  1250. ry0 = p1->y - p1->dmy * rw;
  1251. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1252. nvg__vset(dst, p1->x - dlx0*rw, p1->y - dly0*rw, ru,1); dst++;
  1253. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1254. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1255. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1256. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1257. }
  1258. nvg__vset(dst, lx1, ly1, lu,1); dst++;
  1259. nvg__vset(dst, p1->x - dlx1*rw, p1->y - dly1*rw, ru,1); dst++;
  1260. } else {
  1261. nvg__chooseBevel(p1->flags & NVG_PR_INNERBEVEL, p0, p1, -rw, &rx0,&ry0, &rx1,&ry1);
  1262. nvg__vset(dst, p1->x + dlx0*lw, p1->y + dly0*lw, lu,1); dst++;
  1263. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1264. if (p1->flags & NVG_PT_BEVEL) {
  1265. nvg__vset(dst, p1->x + dlx0*lw, p1->y + dly0*lw, lu,1); dst++;
  1266. nvg__vset(dst, rx0, ry0, ru,1); dst++;
  1267. nvg__vset(dst, p1->x + dlx1*lw, p1->y + dly1*lw, lu,1); dst++;
  1268. nvg__vset(dst, rx1, ry1, ru,1); dst++;
  1269. } else {
  1270. lx0 = p1->x + p1->dmx * lw;
  1271. ly0 = p1->y + p1->dmy * lw;
  1272. nvg__vset(dst, p1->x + dlx0*lw, p1->y + dly0*lw, lu,1); dst++;
  1273. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1274. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1275. nvg__vset(dst, lx0, ly0, lu,1); dst++;
  1276. nvg__vset(dst, p1->x + dlx1*lw, p1->y + dly1*lw, lu,1); dst++;
  1277. nvg__vset(dst, p1->x, p1->y, 0.5f,1); dst++;
  1278. }
  1279. nvg__vset(dst, p1->x + dlx1*lw, p1->y + dly1*lw, lu,1); dst++;
  1280. nvg__vset(dst, rx1, ry1, ru,1); dst++;
  1281. }
  1282. return dst;
  1283. }
  1284. static NVGvertex* nvg__buttCapStart(NVGvertex* dst, NVGpoint* p,
  1285. float dx, float dy, float w, float d, float aa)
  1286. {
  1287. float px = p->x - dx*d;
  1288. float py = p->y - dy*d;
  1289. float dlx = dy;
  1290. float dly = -dx;
  1291. nvg__vset(dst, px + dlx*w - dx*aa, py + dly*w - dy*aa, 0,0); dst++;
  1292. nvg__vset(dst, px - dlx*w - dx*aa, py - dly*w - dy*aa, 1,0); dst++;
  1293. nvg__vset(dst, px + dlx*w, py + dly*w, 0,1); dst++;
  1294. nvg__vset(dst, px - dlx*w, py - dly*w, 1,1); dst++;
  1295. return dst;
  1296. }
  1297. static NVGvertex* nvg__buttCapEnd(NVGvertex* dst, NVGpoint* p,
  1298. float dx, float dy, float w, float d, float aa)
  1299. {
  1300. float px = p->x + dx*d;
  1301. float py = p->y + dy*d;
  1302. float dlx = dy;
  1303. float dly = -dx;
  1304. nvg__vset(dst, px + dlx*w, py + dly*w, 0,1); dst++;
  1305. nvg__vset(dst, px - dlx*w, py - dly*w, 1,1); dst++;
  1306. nvg__vset(dst, px + dlx*w + dx*aa, py + dly*w + dy*aa, 0,0); dst++;
  1307. nvg__vset(dst, px - dlx*w + dx*aa, py - dly*w + dy*aa, 1,0); dst++;
  1308. return dst;
  1309. }
  1310. static NVGvertex* nvg__roundCapStart(NVGvertex* dst, NVGpoint* p,
  1311. float dx, float dy, float w, int ncap, float aa)
  1312. {
  1313. int i;
  1314. float px = p->x;
  1315. float py = p->y;
  1316. float dlx = dy;
  1317. float dly = -dx;
  1318. NVG_NOTUSED(aa);
  1319. for (i = 0; i < ncap; i++) {
  1320. float a = i/(float)(ncap-1)*NVG_PI;
  1321. float ax = cosf(a) * w, ay = sinf(a) * w;
  1322. nvg__vset(dst, px - dlx*ax - dx*ay, py - dly*ax - dy*ay, 0,1); dst++;
  1323. nvg__vset(dst, px, py, 0.5f,1); dst++;
  1324. }
  1325. nvg__vset(dst, px + dlx*w, py + dly*w, 0,1); dst++;
  1326. nvg__vset(dst, px - dlx*w, py - dly*w, 1,1); dst++;
  1327. return dst;
  1328. }
  1329. static NVGvertex* nvg__roundCapEnd(NVGvertex* dst, NVGpoint* p,
  1330. float dx, float dy, float w, int ncap, float aa)
  1331. {
  1332. int i;
  1333. float px = p->x;
  1334. float py = p->y;
  1335. float dlx = dy;
  1336. float dly = -dx;
  1337. NVG_NOTUSED(aa);
  1338. nvg__vset(dst, px + dlx*w, py + dly*w, 0,1); dst++;
  1339. nvg__vset(dst, px - dlx*w, py - dly*w, 1,1); dst++;
  1340. for (i = 0; i < ncap; i++) {
  1341. float a = i/(float)(ncap-1)*NVG_PI;
  1342. float ax = cosf(a) * w, ay = sinf(a) * w;
  1343. nvg__vset(dst, px, py, 0.5f,1); dst++;
  1344. nvg__vset(dst, px - dlx*ax + dx*ay, py - dly*ax + dy*ay, 0,1); dst++;
  1345. }
  1346. return dst;
  1347. }
  1348. static void nvg__calculateJoins(NVGcontext* ctx, float w, int lineJoin, float miterLimit)
  1349. {
  1350. NVGpathCache* cache = ctx->cache;
  1351. int i, j;
  1352. float iw = 0.0f;
  1353. if (w > 0.0f) iw = 1.0f / w;
  1354. // Calculate which joins needs extra vertices to append, and gather vertex count.
  1355. for (i = 0; i < cache->npaths; i++) {
  1356. NVGpath* path = &cache->paths[i];
  1357. NVGpoint* pts = &cache->points[path->first];
  1358. NVGpoint* p0 = &pts[path->count-1];
  1359. NVGpoint* p1 = &pts[0];
  1360. int nleft = 0;
  1361. path->nbevel = 0;
  1362. for (j = 0; j < path->count; j++) {
  1363. float dlx0, dly0, dlx1, dly1, dmr2, cross, limit;
  1364. dlx0 = p0->dy;
  1365. dly0 = -p0->dx;
  1366. dlx1 = p1->dy;
  1367. dly1 = -p1->dx;
  1368. // Calculate extrusions
  1369. p1->dmx = (dlx0 + dlx1) * 0.5f;
  1370. p1->dmy = (dly0 + dly1) * 0.5f;
  1371. dmr2 = p1->dmx*p1->dmx + p1->dmy*p1->dmy;
  1372. if (dmr2 > 0.000001f) {
  1373. float scale = 1.0f / dmr2;
  1374. if (scale > 600.0f) {
  1375. scale = 600.0f;
  1376. }
  1377. p1->dmx *= scale;
  1378. p1->dmy *= scale;
  1379. }
  1380. // Clear flags, but keep the corner.
  1381. p1->flags = (p1->flags & NVG_PT_CORNER) ? NVG_PT_CORNER : 0;
  1382. // Keep track of left turns.
  1383. cross = p1->dx * p0->dy - p0->dx * p1->dy;
  1384. if (cross > 0.0f) {
  1385. nleft++;
  1386. p1->flags |= NVG_PT_LEFT;
  1387. }
  1388. // Calculate if we should use bevel or miter for inner join.
  1389. limit = nvg__maxf(1.01f, nvg__minf(p0->len, p1->len) * iw);
  1390. if ((dmr2 * limit*limit) < 1.0f)
  1391. p1->flags |= NVG_PR_INNERBEVEL;
  1392. // Check to see if the corner needs to be beveled.
  1393. if (p1->flags & NVG_PT_CORNER) {
  1394. if ((dmr2 * miterLimit*miterLimit) < 1.0f || lineJoin == NVG_BEVEL || lineJoin == NVG_ROUND) {
  1395. p1->flags |= NVG_PT_BEVEL;
  1396. }
  1397. }
  1398. if ((p1->flags & (NVG_PT_BEVEL | NVG_PR_INNERBEVEL)) != 0)
  1399. path->nbevel++;
  1400. p0 = p1++;
  1401. }
  1402. path->convex = (nleft == path->count) ? 1 : 0;
  1403. }
  1404. }
  1405. static int nvg__expandStroke(NVGcontext* ctx, float w, int lineCap, int lineJoin, float miterLimit)
  1406. {
  1407. NVGpathCache* cache = ctx->cache;
  1408. NVGvertex* verts;
  1409. NVGvertex* dst;
  1410. int cverts, i, j;
  1411. float aa = ctx->fringeWidth;
  1412. int ncap = nvg__curveDivs(w, NVG_PI, ctx->tessTol); // Calculate divisions per half circle.
  1413. nvg__calculateJoins(ctx, w, lineJoin, miterLimit);
  1414. // Calculate max vertex usage.
  1415. cverts = 0;
  1416. for (i = 0; i < cache->npaths; i++) {
  1417. NVGpath* path = &cache->paths[i];
  1418. int loop = (path->closed == 0) ? 0 : 1;
  1419. if (lineJoin == NVG_ROUND)
  1420. cverts += (path->count + path->nbevel*(ncap+2) + 1) * 2; // plus one for loop
  1421. else
  1422. cverts += (path->count + path->nbevel*5 + 1) * 2; // plus one for loop
  1423. if (loop == 0) {
  1424. // space for caps
  1425. if (lineCap == NVG_ROUND) {
  1426. cverts += (ncap*2 + 2)*2;
  1427. } else {
  1428. cverts += (3+3)*2;
  1429. }
  1430. }
  1431. }
  1432. verts = nvg__allocTempVerts(ctx, cverts);
  1433. if (verts == NULL) return 0;
  1434. for (i = 0; i < cache->npaths; i++) {
  1435. NVGpath* path = &cache->paths[i];
  1436. NVGpoint* pts = &cache->points[path->first];
  1437. NVGpoint* p0;
  1438. NVGpoint* p1;
  1439. int s, e, loop;
  1440. float dx, dy;
  1441. path->fill = 0;
  1442. path->nfill = 0;
  1443. // Calculate fringe or stroke
  1444. loop = (path->closed == 0) ? 0 : 1;
  1445. dst = verts;
  1446. path->stroke = dst;
  1447. if (loop) {
  1448. // Looping
  1449. p0 = &pts[path->count-1];
  1450. p1 = &pts[0];
  1451. s = 0;
  1452. e = path->count;
  1453. } else {
  1454. // Add cap
  1455. p0 = &pts[0];
  1456. p1 = &pts[1];
  1457. s = 1;
  1458. e = path->count-1;
  1459. }
  1460. if (loop == 0) {
  1461. // Add cap
  1462. dx = p1->x - p0->x;
  1463. dy = p1->y - p0->y;
  1464. nvg__normalize(&dx, &dy);
  1465. if (lineCap == NVG_BUTT)
  1466. dst = nvg__buttCapStart(dst, p0, dx, dy, w, -aa*0.5f, aa);
  1467. else if (lineCap == NVG_BUTT || lineCap == NVG_SQUARE)
  1468. dst = nvg__buttCapStart(dst, p0, dx, dy, w, w-aa, aa);
  1469. else if (lineCap == NVG_ROUND)
  1470. dst = nvg__roundCapStart(dst, p0, dx, dy, w, ncap, aa);
  1471. }
  1472. for (j = s; j < e; ++j) {
  1473. if ((p1->flags & (NVG_PT_BEVEL | NVG_PR_INNERBEVEL)) != 0) {
  1474. if (lineJoin == NVG_ROUND) {
  1475. dst = nvg__roundJoin(dst, p0, p1, w, w, 0, 1, ncap, aa);
  1476. } else {
  1477. dst = nvg__bevelJoin(dst, p0, p1, w, w, 0, 1, aa);
  1478. }
  1479. } else {
  1480. nvg__vset(dst, p1->x + (p1->dmx * w), p1->y + (p1->dmy * w), 0,1); dst++;
  1481. nvg__vset(dst, p1->x - (p1->dmx * w), p1->y - (p1->dmy * w), 1,1); dst++;
  1482. }
  1483. p0 = p1++;
  1484. }
  1485. if (loop) {
  1486. // Loop it
  1487. nvg__vset(dst, verts[0].x, verts[0].y, 0,1); dst++;
  1488. nvg__vset(dst, verts[1].x, verts[1].y, 1,1); dst++;
  1489. } else {
  1490. // Add cap
  1491. dx = p1->x - p0->x;
  1492. dy = p1->y - p0->y;
  1493. nvg__normalize(&dx, &dy);
  1494. if (lineCap == NVG_BUTT)
  1495. dst = nvg__buttCapEnd(dst, p1, dx, dy, w, -aa*0.5f, aa);
  1496. else if (lineCap == NVG_BUTT || lineCap == NVG_SQUARE)
  1497. dst = nvg__buttCapEnd(dst, p1, dx, dy, w, w-aa, aa);
  1498. else if (lineCap == NVG_ROUND)
  1499. dst = nvg__roundCapEnd(dst, p1, dx, dy, w, ncap, aa);
  1500. }
  1501. path->nstroke = (int)(dst - verts);
  1502. verts = dst;
  1503. }
  1504. return 1;
  1505. }
  1506. static int nvg__expandFill(NVGcontext* ctx, float w, int lineJoin, float miterLimit)
  1507. {
  1508. NVGpathCache* cache = ctx->cache;
  1509. NVGvertex* verts;
  1510. NVGvertex* dst;
  1511. int cverts, convex, i, j;
  1512. float aa = ctx->fringeWidth;
  1513. int fringe = w > 0.0f;
  1514. nvg__calculateJoins(ctx, w, lineJoin, miterLimit);
  1515. // Calculate max vertex usage.
  1516. cverts = 0;
  1517. for (i = 0; i < cache->npaths; i++) {
  1518. NVGpath* path = &cache->paths[i];
  1519. cverts += path->count + path->nbevel + 1;
  1520. if (fringe)
  1521. cverts += (path->count + path->nbevel*5 + 1) * 2; // plus one for loop
  1522. }
  1523. verts = nvg__allocTempVerts(ctx, cverts);
  1524. if (verts == NULL) return 0;
  1525. convex = cache->npaths == 1 && cache->paths[0].convex;
  1526. for (i = 0; i < cache->npaths; i++) {
  1527. NVGpath* path = &cache->paths[i];
  1528. NVGpoint* pts = &cache->points[path->first];
  1529. NVGpoint* p0;
  1530. NVGpoint* p1;
  1531. float rw, lw, woff;
  1532. float ru, lu;
  1533. // Calculate shape vertices.
  1534. woff = 0.5f*aa;
  1535. dst = verts;
  1536. path->fill = dst;
  1537. if (fringe) {
  1538. // Looping
  1539. p0 = &pts[path->count-1];
  1540. p1 = &pts[0];
  1541. for (j = 0; j < path->count; ++j) {
  1542. if (p1->flags & NVG_PT_BEVEL) {
  1543. float dlx0 = p0->dy;
  1544. float dly0 = -p0->dx;
  1545. float dlx1 = p1->dy;
  1546. float dly1 = -p1->dx;
  1547. if (p1->flags & NVG_PT_LEFT) {
  1548. float lx = p1->x + p1->dmx * woff;
  1549. float ly = p1->y + p1->dmy * woff;
  1550. nvg__vset(dst, lx, ly, 0.5f,1); dst++;
  1551. } else {
  1552. float lx0 = p1->x + dlx0 * woff;
  1553. float ly0 = p1->y + dly0 * woff;
  1554. float lx1 = p1->x + dlx1 * woff;
  1555. float ly1 = p1->y + dly1 * woff;
  1556. nvg__vset(dst, lx0, ly0, 0.5f,1); dst++;
  1557. nvg__vset(dst, lx1, ly1, 0.5f,1); dst++;
  1558. }
  1559. } else {
  1560. nvg__vset(dst, p1->x + (p1->dmx * woff), p1->y + (p1->dmy * woff), 0.5f,1); dst++;
  1561. }
  1562. p0 = p1++;
  1563. }
  1564. } else {
  1565. for (j = 0; j < path->count; ++j) {
  1566. nvg__vset(dst, pts[j].x, pts[j].y, 0.5f,1);
  1567. dst++;
  1568. }
  1569. }
  1570. path->nfill = (int)(dst - verts);
  1571. verts = dst;
  1572. // Calculate fringe
  1573. if (fringe) {
  1574. lw = w + woff;
  1575. rw = w - woff;
  1576. lu = 0;
  1577. ru = 1;
  1578. dst = verts;
  1579. path->stroke = dst;
  1580. // Create only half a fringe for convex shapes so that
  1581. // the shape can be rendered without stenciling.
  1582. if (convex) {
  1583. lw = woff; // This should generate the same vertex as fill inset above.
  1584. lu = 0.5f; // Set outline fade at middle.
  1585. }
  1586. // Looping
  1587. p0 = &pts[path->count-1];
  1588. p1 = &pts[0];
  1589. for (j = 0; j < path->count; ++j) {
  1590. if ((p1->flags & (NVG_PT_BEVEL | NVG_PR_INNERBEVEL)) != 0) {
  1591. dst = nvg__bevelJoin(dst, p0, p1, lw, rw, lu, ru, ctx->fringeWidth);
  1592. } else {
  1593. nvg__vset(dst, p1->x + (p1->dmx * lw), p1->y + (p1->dmy * lw), lu,1); dst++;
  1594. nvg__vset(dst, p1->x - (p1->dmx * rw), p1->y - (p1->dmy * rw), ru,1); dst++;
  1595. }
  1596. p0 = p1++;
  1597. }
  1598. // Loop it
  1599. nvg__vset(dst, verts[0].x, verts[0].y, lu,1); dst++;
  1600. nvg__vset(dst, verts[1].x, verts[1].y, ru,1); dst++;
  1601. path->nstroke = (int)(dst - verts);
  1602. verts = dst;
  1603. } else {
  1604. path->stroke = NULL;
  1605. path->nstroke = 0;
  1606. }
  1607. }
  1608. return 1;
  1609. }
  1610. // Draw
  1611. void nvgBeginPath(NVGcontext* ctx)
  1612. {
  1613. ctx->ncommands = 0;
  1614. nvg__clearPathCache(ctx);
  1615. }
  1616. void nvgMoveTo(NVGcontext* ctx, float x, float y)
  1617. {
  1618. float vals[] = { NVG_MOVETO, x, y };
  1619. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1620. }
  1621. void nvgLineTo(NVGcontext* ctx, float x, float y)
  1622. {
  1623. float vals[] = { NVG_LINETO, x, y };
  1624. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1625. }
  1626. void nvgBezierTo(NVGcontext* ctx, float c1x, float c1y, float c2x, float c2y, float x, float y)
  1627. {
  1628. float vals[] = { NVG_BEZIERTO, c1x, c1y, c2x, c2y, x, y };
  1629. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1630. }
  1631. void nvgQuadTo(NVGcontext* ctx, float cx, float cy, float x, float y)
  1632. {
  1633. float x0 = ctx->commandx;
  1634. float y0 = ctx->commandy;
  1635. float vals[] = { NVG_BEZIERTO,
  1636. x0 + 2.0f/3.0f*(cx - x0), y0 + 2.0f/3.0f*(cy - y0),
  1637. x + 2.0f/3.0f*(cx - x), y + 2.0f/3.0f*(cy - y),
  1638. x, y };
  1639. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1640. }
  1641. void nvgArcTo(NVGcontext* ctx, float x1, float y1, float x2, float y2, float radius)
  1642. {
  1643. float x0 = ctx->commandx;
  1644. float y0 = ctx->commandy;
  1645. float dx0,dy0, dx1,dy1, a, d, cx,cy, a0,a1;
  1646. int dir;
  1647. if (ctx->ncommands == 0) {
  1648. return;
  1649. }
  1650. // Handle degenerate cases.
  1651. if (nvg__ptEquals(x0,y0, x1,y1, ctx->distTol) ||
  1652. nvg__ptEquals(x1,y1, x2,y2, ctx->distTol) ||
  1653. nvg__distPtSeg(x1,y1, x0,y0, x2,y2) < ctx->distTol*ctx->distTol ||
  1654. radius < ctx->distTol) {
  1655. nvgLineTo(ctx, x1,y1);
  1656. return;
  1657. }
  1658. // Calculate tangential circle to lines (x0,y0)-(x1,y1) and (x1,y1)-(x2,y2).
  1659. dx0 = x0-x1;
  1660. dy0 = y0-y1;
  1661. dx1 = x2-x1;
  1662. dy1 = y2-y1;
  1663. nvg__normalize(&dx0,&dy0);
  1664. nvg__normalize(&dx1,&dy1);
  1665. a = nvg__acosf(dx0*dx1 + dy0*dy1);
  1666. d = radius / nvg__tanf(a/2.0f);
  1667. // printf("a=%f° d=%f\n", a/NVG_PI*180.0f, d);
  1668. if (d > 10000.0f) {
  1669. nvgLineTo(ctx, x1,y1);
  1670. return;
  1671. }
  1672. if (nvg__cross(dx0,dy0, dx1,dy1) > 0.0f) {
  1673. cx = x1 + dx0*d + dy0*radius;
  1674. cy = y1 + dy0*d + -dx0*radius;
  1675. a0 = nvg__atan2f(dx0, -dy0);
  1676. a1 = nvg__atan2f(-dx1, dy1);
  1677. dir = NVG_CW;
  1678. // printf("CW c=(%f, %f) a0=%f° a1=%f°\n", cx, cy, a0/NVG_PI*180.0f, a1/NVG_PI*180.0f);
  1679. } else {
  1680. cx = x1 + dx0*d + -dy0*radius;
  1681. cy = y1 + dy0*d + dx0*radius;
  1682. a0 = nvg__atan2f(-dx0, dy0);
  1683. a1 = nvg__atan2f(dx1, -dy1);
  1684. dir = NVG_CCW;
  1685. // printf("CCW c=(%f, %f) a0=%f° a1=%f°\n", cx, cy, a0/NVG_PI*180.0f, a1/NVG_PI*180.0f);
  1686. }
  1687. nvgArc(ctx, cx, cy, radius, a0, a1, dir);
  1688. }
  1689. void nvgClosePath(NVGcontext* ctx)
  1690. {
  1691. float vals[] = { NVG_CLOSE };
  1692. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1693. }
  1694. void nvgPathWinding(NVGcontext* ctx, int dir)
  1695. {
  1696. float vals[] = { NVG_WINDING, (float)dir };
  1697. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1698. }
  1699. void nvgArc(NVGcontext* ctx, float cx, float cy, float r, float a0, float a1, int dir)
  1700. {
  1701. float a = 0, da = 0, hda = 0, kappa = 0;
  1702. float dx = 0, dy = 0, x = 0, y = 0, tanx = 0, tany = 0;
  1703. float px = 0, py = 0, ptanx = 0, ptany = 0;
  1704. float vals[3 + 5*7 + 100];
  1705. int i, ndivs, nvals;
  1706. int move = ctx->ncommands > 0 ? NVG_LINETO : NVG_MOVETO;
  1707. // Clamp angles
  1708. da = a1 - a0;
  1709. if (dir == NVG_CW) {
  1710. if (nvg__absf(da) >= NVG_PI*2) {
  1711. da = NVG_PI*2;
  1712. } else {
  1713. while (da < 0.0f) da += NVG_PI*2;
  1714. }
  1715. } else {
  1716. if (nvg__absf(da) >= NVG_PI*2) {
  1717. da = -NVG_PI*2;
  1718. } else {
  1719. while (da > 0.0f) da -= NVG_PI*2;
  1720. }
  1721. }
  1722. // Split arc into max 90 degree segments.
  1723. ndivs = nvg__maxi(1, nvg__mini((int)(nvg__absf(da) / (NVG_PI*0.5f) + 0.5f), 5));
  1724. hda = (da / (float)ndivs) / 2.0f;
  1725. kappa = nvg__absf(4.0f / 3.0f * (1.0f - nvg__cosf(hda)) / nvg__sinf(hda));
  1726. if (dir == NVG_CCW)
  1727. kappa = -kappa;
  1728. nvals = 0;
  1729. for (i = 0; i <= ndivs; i++) {
  1730. a = a0 + da * (i/(float)ndivs);
  1731. dx = nvg__cosf(a);
  1732. dy = nvg__sinf(a);
  1733. x = cx + dx*r;
  1734. y = cy + dy*r;
  1735. tanx = -dy*r*kappa;
  1736. tany = dx*r*kappa;
  1737. if (i == 0) {
  1738. vals[nvals++] = (float)move;
  1739. vals[nvals++] = x;
  1740. vals[nvals++] = y;
  1741. } else {
  1742. vals[nvals++] = NVG_BEZIERTO;
  1743. vals[nvals++] = px+ptanx;
  1744. vals[nvals++] = py+ptany;
  1745. vals[nvals++] = x-tanx;
  1746. vals[nvals++] = y-tany;
  1747. vals[nvals++] = x;
  1748. vals[nvals++] = y;
  1749. }
  1750. px = x;
  1751. py = y;
  1752. ptanx = tanx;
  1753. ptany = tany;
  1754. }
  1755. nvg__appendCommands(ctx, vals, nvals);
  1756. }
  1757. void nvgRect(NVGcontext* ctx, float x, float y, float w, float h)
  1758. {
  1759. float vals[] = {
  1760. NVG_MOVETO, x,y,
  1761. NVG_LINETO, x,y+h,
  1762. NVG_LINETO, x+w,y+h,
  1763. NVG_LINETO, x+w,y,
  1764. NVG_CLOSE
  1765. };
  1766. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1767. }
  1768. void nvgRoundedRect(NVGcontext* ctx, float x, float y, float w, float h, float r)
  1769. {
  1770. if (r < 0.1f) {
  1771. nvgRect(ctx, x,y,w,h);
  1772. return;
  1773. }
  1774. else {
  1775. float rx = nvg__minf(r, nvg__absf(w)*0.5f) * nvg__signf(w), ry = nvg__minf(r, nvg__absf(h)*0.5f) * nvg__signf(h);
  1776. float vals[] = {
  1777. NVG_MOVETO, x, y+ry,
  1778. NVG_LINETO, x, y+h-ry,
  1779. NVG_BEZIERTO, x, y+h-ry*(1-NVG_KAPPA90), x+rx*(1-NVG_KAPPA90), y+h, x+rx, y+h,
  1780. NVG_LINETO, x+w-rx, y+h,
  1781. NVG_BEZIERTO, x+w-rx*(1-NVG_KAPPA90), y+h, x+w, y+h-ry*(1-NVG_KAPPA90), x+w, y+h-ry,
  1782. NVG_LINETO, x+w, y+ry,
  1783. NVG_BEZIERTO, x+w, y+ry*(1-NVG_KAPPA90), x+w-rx*(1-NVG_KAPPA90), y, x+w-rx, y,
  1784. NVG_LINETO, x+rx, y,
  1785. NVG_BEZIERTO, x+rx*(1-NVG_KAPPA90), y, x, y+ry*(1-NVG_KAPPA90), x, y+ry,
  1786. NVG_CLOSE
  1787. };
  1788. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1789. }
  1790. }
  1791. void nvgEllipse(NVGcontext* ctx, float cx, float cy, float rx, float ry)
  1792. {
  1793. float vals[] = {
  1794. NVG_MOVETO, cx-rx, cy,
  1795. NVG_BEZIERTO, cx-rx, cy+ry*NVG_KAPPA90, cx-rx*NVG_KAPPA90, cy+ry, cx, cy+ry,
  1796. NVG_BEZIERTO, cx+rx*NVG_KAPPA90, cy+ry, cx+rx, cy+ry*NVG_KAPPA90, cx+rx, cy,
  1797. NVG_BEZIERTO, cx+rx, cy-ry*NVG_KAPPA90, cx+rx*NVG_KAPPA90, cy-ry, cx, cy-ry,
  1798. NVG_BEZIERTO, cx-rx*NVG_KAPPA90, cy-ry, cx-rx, cy-ry*NVG_KAPPA90, cx-rx, cy,
  1799. NVG_CLOSE
  1800. };
  1801. nvg__appendCommands(ctx, vals, NVG_COUNTOF(vals));
  1802. }
  1803. void nvgCircle(NVGcontext* ctx, float cx, float cy, float r)
  1804. {
  1805. nvgEllipse(ctx, cx,cy, r,r);
  1806. }
  1807. void nvgDebugDumpPathCache(NVGcontext* ctx)
  1808. {
  1809. const NVGpath* path;
  1810. int i, j;
  1811. printf("Dumping %d cached paths\n", ctx->cache->npaths);
  1812. for (i = 0; i < ctx->cache->npaths; i++) {
  1813. path = &ctx->cache->paths[i];
  1814. printf(" - Path %d\n", i);
  1815. if (path->nfill) {
  1816. printf(" - fill: %d\n", path->nfill);
  1817. for (j = 0; j < path->nfill; j++)
  1818. printf("%f\t%f\n", path->fill[j].x, path->fill[j].y);
  1819. }
  1820. if (path->nstroke) {
  1821. printf(" - stroke: %d\n", path->nstroke);
  1822. for (j = 0; j < path->nstroke; j++)
  1823. printf("%f\t%f\n", path->stroke[j].x, path->stroke[j].y);
  1824. }
  1825. }
  1826. }
  1827. void nvgFill(NVGcontext* ctx)
  1828. {
  1829. NVGstate* state = nvg__getState(ctx);
  1830. const NVGpath* path;
  1831. NVGpaint fillPaint = state->fill;
  1832. int i;
  1833. nvg__flattenPaths(ctx);
  1834. if (ctx->params.edgeAntiAlias)
  1835. nvg__expandFill(ctx, ctx->fringeWidth, NVG_MITER, 2.4f);
  1836. else
  1837. nvg__expandFill(ctx, 0.0f, NVG_MITER, 2.4f);
  1838. // Apply global alpha
  1839. fillPaint.innerColor.a *= state->alpha;
  1840. fillPaint.outerColor.a *= state->alpha;
  1841. ctx->params.renderFill(ctx->params.userPtr, &fillPaint, &state->scissor, ctx->fringeWidth,
  1842. ctx->cache->bounds, ctx->cache->paths, ctx->cache->npaths);
  1843. // Count triangles
  1844. for (i = 0; i < ctx->cache->npaths; i++) {
  1845. path = &ctx->cache->paths[i];
  1846. ctx->fillTriCount += path->nfill-2;
  1847. ctx->fillTriCount += path->nstroke-2;
  1848. ctx->drawCallCount += 2;
  1849. }
  1850. }
  1851. void nvgStroke(NVGcontext* ctx)
  1852. {
  1853. NVGstate* state = nvg__getState(ctx);
  1854. float scale = nvg__getAverageScale(state->xform);
  1855. float strokeWidth = nvg__clampf(state->strokeWidth * scale, 0.0f, 200.0f);
  1856. NVGpaint strokePaint = state->stroke;
  1857. const NVGpath* path;
  1858. int i;
  1859. if (strokeWidth < ctx->fringeWidth) {
  1860. // If the stroke width is less than pixel size, use alpha to emulate coverage.
  1861. // Since coverage is area, scale by alpha*alpha.
  1862. float alpha = nvg__clampf(strokeWidth / ctx->fringeWidth, 0.0f, 1.0f);
  1863. strokePaint.innerColor.a *= alpha*alpha;
  1864. strokePaint.outerColor.a *= alpha*alpha;
  1865. strokeWidth = ctx->fringeWidth;
  1866. }
  1867. // Apply global alpha
  1868. strokePaint.innerColor.a *= state->alpha;
  1869. strokePaint.outerColor.a *= state->alpha;
  1870. nvg__flattenPaths(ctx);
  1871. if (ctx->params.edgeAntiAlias)
  1872. nvg__expandStroke(ctx, strokeWidth*0.5f + ctx->fringeWidth*0.5f, state->lineCap, state->lineJoin, state->miterLimit);
  1873. else
  1874. nvg__expandStroke(ctx, strokeWidth*0.5f, state->lineCap, state->lineJoin, state->miterLimit);
  1875. ctx->params.renderStroke(ctx->params.userPtr, &strokePaint, &state->scissor, ctx->fringeWidth,
  1876. strokeWidth, ctx->cache->paths, ctx->cache->npaths);
  1877. // Count triangles
  1878. for (i = 0; i < ctx->cache->npaths; i++) {
  1879. path = &ctx->cache->paths[i];
  1880. ctx->strokeTriCount += path->nstroke-2;
  1881. ctx->drawCallCount++;
  1882. }
  1883. }
  1884. // Add fonts
  1885. int nvgCreateFont(NVGcontext* ctx, const char* name, const char* path)
  1886. {
  1887. return fonsAddFont(ctx->fs, name, path);
  1888. }
  1889. int nvgCreateFontMem(NVGcontext* ctx, const char* name, unsigned char* data, int ndata, int freeData)
  1890. {
  1891. return fonsAddFontMem(ctx->fs, name, data, ndata, freeData);
  1892. }
  1893. int nvgFindFont(NVGcontext* ctx, const char* name)
  1894. {
  1895. if (name == NULL) return -1;
  1896. return fonsGetFontByName(ctx->fs, name);
  1897. }
  1898. // State setting
  1899. void nvgFontSize(NVGcontext* ctx, float size)
  1900. {
  1901. NVGstate* state = nvg__getState(ctx);
  1902. state->fontSize = size;
  1903. }
  1904. void nvgFontBlur(NVGcontext* ctx, float blur)
  1905. {
  1906. NVGstate* state = nvg__getState(ctx);
  1907. state->fontBlur = blur;
  1908. }
  1909. void nvgTextLetterSpacing(NVGcontext* ctx, float spacing)
  1910. {
  1911. NVGstate* state = nvg__getState(ctx);
  1912. state->letterSpacing = spacing;
  1913. }
  1914. void nvgTextLineHeight(NVGcontext* ctx, float lineHeight)
  1915. {
  1916. NVGstate* state = nvg__getState(ctx);
  1917. state->lineHeight = lineHeight;
  1918. }
  1919. void nvgTextAlign(NVGcontext* ctx, int align)
  1920. {
  1921. NVGstate* state = nvg__getState(ctx);
  1922. state->textAlign = align;
  1923. }
  1924. void nvgFontFaceId(NVGcontext* ctx, int font)
  1925. {
  1926. NVGstate* state = nvg__getState(ctx);
  1927. state->fontId = font;
  1928. }
  1929. void nvgFontFace(NVGcontext* ctx, const char* font)
  1930. {
  1931. NVGstate* state = nvg__getState(ctx);
  1932. state->fontId = fonsGetFontByName(ctx->fs, font);
  1933. }
  1934. static float nvg__quantize(float a, float d)
  1935. {
  1936. return ((int)(a / d + 0.5f)) * d;
  1937. }
  1938. static float nvg__getFontScale(NVGstate* state)
  1939. {
  1940. return nvg__minf(nvg__quantize(nvg__getAverageScale(state->xform), 0.01f), 4.0f);
  1941. }
  1942. static void nvg__flushTextTexture(NVGcontext* ctx)
  1943. {
  1944. int dirty[4];
  1945. if (fonsValidateTexture(ctx->fs, dirty)) {
  1946. int fontImage = ctx->fontImages[ctx->fontImageIdx];
  1947. // Update texture
  1948. if (fontImage != 0) {
  1949. int iw, ih;
  1950. const unsigned char* data = fonsGetTextureData(ctx->fs, &iw, &ih);
  1951. int x = dirty[0];
  1952. int y = dirty[1];
  1953. int w = dirty[2] - dirty[0];
  1954. int h = dirty[3] - dirty[1];
  1955. ctx->params.renderUpdateTexture(ctx->params.userPtr, fontImage, x,y, w,h, data);
  1956. }
  1957. }
  1958. }
  1959. static int nvg__allocTextAtlas(NVGcontext* ctx)
  1960. {
  1961. int iw, ih;
  1962. nvg__flushTextTexture(ctx);
  1963. if (ctx->fontImageIdx >= NVG_MAX_FONTIMAGES-1)
  1964. return 0;
  1965. // if next fontImage already have a texture
  1966. if (ctx->fontImages[ctx->fontImageIdx+1] != 0)
  1967. nvgImageSize(ctx, ctx->fontImages[ctx->fontImageIdx+1], &iw, &ih);
  1968. else { // calculate the new font image size and create it.
  1969. nvgImageSize(ctx, ctx->fontImages[ctx->fontImageIdx], &iw, &ih);
  1970. if (iw > ih)
  1971. ih *= 2;
  1972. else
  1973. iw *= 2;
  1974. if (iw > NVG_MAX_FONTIMAGE_SIZE || ih > NVG_MAX_FONTIMAGE_SIZE)
  1975. iw = ih = NVG_MAX_FONTIMAGE_SIZE;
  1976. ctx->fontImages[ctx->fontImageIdx+1] = ctx->params.renderCreateTexture(ctx->params.userPtr, NVG_TEXTURE_ALPHA, iw, ih, 0, NULL);
  1977. }
  1978. ++ctx->fontImageIdx;
  1979. fonsResetAtlas(ctx->fs, iw, ih);
  1980. return 1;
  1981. }
  1982. static void nvg__renderText(NVGcontext* ctx, NVGvertex* verts, int nverts)
  1983. {
  1984. NVGstate* state = nvg__getState(ctx);
  1985. NVGpaint paint = state->fill;
  1986. // Render triangles.
  1987. paint.image = ctx->fontImages[ctx->fontImageIdx];
  1988. // Apply global alpha
  1989. paint.innerColor.a *= state->alpha;
  1990. paint.outerColor.a *= state->alpha;
  1991. ctx->params.renderTriangles(ctx->params.userPtr, &paint, &state->scissor, verts, nverts);
  1992. ctx->drawCallCount++;
  1993. ctx->textTriCount += nverts/3;
  1994. }
  1995. float nvgText(NVGcontext* ctx, float x, float y, const char* string, const char* end)
  1996. {
  1997. NVGstate* state = nvg__getState(ctx);
  1998. FONStextIter iter, prevIter;
  1999. FONSquad q;
  2000. NVGvertex* verts;
  2001. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2002. float invscale = 1.0f / scale;
  2003. int cverts = 0;
  2004. int nverts = 0;
  2005. if (end == NULL)
  2006. end = string + strlen(string);
  2007. if (state->fontId == FONS_INVALID) return x;
  2008. fonsSetSize(ctx->fs, state->fontSize*scale);
  2009. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2010. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2011. fonsSetAlign(ctx->fs, state->textAlign);
  2012. fonsSetFont(ctx->fs, state->fontId);
  2013. cverts = nvg__maxi(2, (int)(end - string)) * 6; // conservative estimate.
  2014. verts = nvg__allocTempVerts(ctx, cverts);
  2015. if (verts == NULL) return x;
  2016. fonsTextIterInit(ctx->fs, &iter, x*scale, y*scale, string, end);
  2017. prevIter = iter;
  2018. while (fonsTextIterNext(ctx->fs, &iter, &q)) {
  2019. float c[4*2];
  2020. if (iter.prevGlyphIndex == -1) { // can not retrieve glyph?
  2021. if (!nvg__allocTextAtlas(ctx))
  2022. break; // no memory :(
  2023. if (nverts != 0) {
  2024. nvg__renderText(ctx, verts, nverts);
  2025. nverts = 0;
  2026. }
  2027. iter = prevIter;
  2028. fonsTextIterNext(ctx->fs, &iter, &q); // try again
  2029. if (iter.prevGlyphIndex == -1) // still can not find glyph?
  2030. break;
  2031. }
  2032. prevIter = iter;
  2033. // Transform corners.
  2034. nvgTransformPoint(&c[0],&c[1], state->xform, q.x0*invscale, q.y0*invscale);
  2035. nvgTransformPoint(&c[2],&c[3], state->xform, q.x1*invscale, q.y0*invscale);
  2036. nvgTransformPoint(&c[4],&c[5], state->xform, q.x1*invscale, q.y1*invscale);
  2037. nvgTransformPoint(&c[6],&c[7], state->xform, q.x0*invscale, q.y1*invscale);
  2038. // Create triangles
  2039. if (nverts+6 <= cverts) {
  2040. nvg__vset(&verts[nverts], c[0], c[1], q.s0, q.t0); nverts++;
  2041. nvg__vset(&verts[nverts], c[4], c[5], q.s1, q.t1); nverts++;
  2042. nvg__vset(&verts[nverts], c[2], c[3], q.s1, q.t0); nverts++;
  2043. nvg__vset(&verts[nverts], c[0], c[1], q.s0, q.t0); nverts++;
  2044. nvg__vset(&verts[nverts], c[6], c[7], q.s0, q.t1); nverts++;
  2045. nvg__vset(&verts[nverts], c[4], c[5], q.s1, q.t1); nverts++;
  2046. }
  2047. }
  2048. // TODO: add back-end bit to do this just once per frame.
  2049. nvg__flushTextTexture(ctx);
  2050. nvg__renderText(ctx, verts, nverts);
  2051. return iter.x;
  2052. }
  2053. void nvgTextBox(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end)
  2054. {
  2055. NVGstate* state = nvg__getState(ctx);
  2056. NVGtextRow rows[2];
  2057. int nrows = 0, i;
  2058. int oldAlign = state->textAlign;
  2059. int haling = state->textAlign & (NVG_ALIGN_LEFT | NVG_ALIGN_CENTER | NVG_ALIGN_RIGHT);
  2060. int valign = state->textAlign & (NVG_ALIGN_TOP | NVG_ALIGN_MIDDLE | NVG_ALIGN_BOTTOM | NVG_ALIGN_BASELINE);
  2061. float lineh = 0;
  2062. if (state->fontId == FONS_INVALID) return;
  2063. nvgTextMetrics(ctx, NULL, NULL, &lineh);
  2064. state->textAlign = NVG_ALIGN_LEFT | valign;
  2065. while ((nrows = nvgTextBreakLines(ctx, string, end, breakRowWidth, rows, 2))) {
  2066. for (i = 0; i < nrows; i++) {
  2067. NVGtextRow* row = &rows[i];
  2068. if (haling & NVG_ALIGN_LEFT)
  2069. nvgText(ctx, x, y, row->start, row->end);
  2070. else if (haling & NVG_ALIGN_CENTER)
  2071. nvgText(ctx, x + breakRowWidth*0.5f - row->width*0.5f, y, row->start, row->end);
  2072. else if (haling & NVG_ALIGN_RIGHT)
  2073. nvgText(ctx, x + breakRowWidth - row->width, y, row->start, row->end);
  2074. y += lineh * state->lineHeight;
  2075. }
  2076. string = rows[nrows-1].next;
  2077. }
  2078. state->textAlign = oldAlign;
  2079. }
  2080. int nvgTextGlyphPositions(NVGcontext* ctx, float x, float y, const char* string, const char* end, NVGglyphPosition* positions, int maxPositions)
  2081. {
  2082. NVGstate* state = nvg__getState(ctx);
  2083. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2084. float invscale = 1.0f / scale;
  2085. FONStextIter iter, prevIter;
  2086. FONSquad q;
  2087. int npos = 0;
  2088. if (state->fontId == FONS_INVALID) return 0;
  2089. if (end == NULL)
  2090. end = string + strlen(string);
  2091. if (string == end)
  2092. return 0;
  2093. fonsSetSize(ctx->fs, state->fontSize*scale);
  2094. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2095. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2096. fonsSetAlign(ctx->fs, state->textAlign);
  2097. fonsSetFont(ctx->fs, state->fontId);
  2098. fonsTextIterInit(ctx->fs, &iter, x*scale, y*scale, string, end);
  2099. prevIter = iter;
  2100. while (fonsTextIterNext(ctx->fs, &iter, &q)) {
  2101. if (iter.prevGlyphIndex < 0 && nvg__allocTextAtlas(ctx)) { // can not retrieve glyph?
  2102. iter = prevIter;
  2103. fonsTextIterNext(ctx->fs, &iter, &q); // try again
  2104. }
  2105. prevIter = iter;
  2106. positions[npos].str = iter.str;
  2107. positions[npos].x = iter.x * invscale;
  2108. positions[npos].minx = nvg__minf(iter.x, q.x0) * invscale;
  2109. positions[npos].maxx = nvg__maxf(iter.nextx, q.x1) * invscale;
  2110. npos++;
  2111. if (npos >= maxPositions)
  2112. break;
  2113. }
  2114. return npos;
  2115. }
  2116. enum NVGcodepointType {
  2117. NVG_SPACE,
  2118. NVG_NEWLINE,
  2119. NVG_CHAR,
  2120. };
  2121. int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows)
  2122. {
  2123. NVGstate* state = nvg__getState(ctx);
  2124. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2125. float invscale = 1.0f / scale;
  2126. FONStextIter iter, prevIter;
  2127. FONSquad q;
  2128. int nrows = 0;
  2129. float rowStartX = 0;
  2130. float rowWidth = 0;
  2131. float rowMinX = 0;
  2132. float rowMaxX = 0;
  2133. const char* rowStart = NULL;
  2134. const char* rowEnd = NULL;
  2135. const char* wordStart = NULL;
  2136. float wordStartX = 0;
  2137. float wordMinX = 0;
  2138. const char* breakEnd = NULL;
  2139. float breakWidth = 0;
  2140. float breakMaxX = 0;
  2141. int type = NVG_SPACE, ptype = NVG_SPACE;
  2142. unsigned int pcodepoint = 0;
  2143. if (maxRows == 0) return 0;
  2144. if (state->fontId == FONS_INVALID) return 0;
  2145. if (end == NULL)
  2146. end = string + strlen(string);
  2147. if (string == end) return 0;
  2148. fonsSetSize(ctx->fs, state->fontSize*scale);
  2149. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2150. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2151. fonsSetAlign(ctx->fs, state->textAlign);
  2152. fonsSetFont(ctx->fs, state->fontId);
  2153. breakRowWidth *= scale;
  2154. fonsTextIterInit(ctx->fs, &iter, 0, 0, string, end);
  2155. prevIter = iter;
  2156. while (fonsTextIterNext(ctx->fs, &iter, &q)) {
  2157. if (iter.prevGlyphIndex < 0 && nvg__allocTextAtlas(ctx)) { // can not retrieve glyph?
  2158. iter = prevIter;
  2159. fonsTextIterNext(ctx->fs, &iter, &q); // try again
  2160. }
  2161. prevIter = iter;
  2162. switch (iter.codepoint) {
  2163. case 9: // \t
  2164. case 11: // \v
  2165. case 12: // \f
  2166. case 32: // space
  2167. case 0x00a0: // NBSP
  2168. type = NVG_SPACE;
  2169. break;
  2170. case 10: // \n
  2171. type = pcodepoint == 13 ? NVG_SPACE : NVG_NEWLINE;
  2172. break;
  2173. case 13: // \r
  2174. type = pcodepoint == 10 ? NVG_SPACE : NVG_NEWLINE;
  2175. break;
  2176. case 0x0085: // NEL
  2177. type = NVG_NEWLINE;
  2178. break;
  2179. default:
  2180. type = NVG_CHAR;
  2181. break;
  2182. }
  2183. if (type == NVG_NEWLINE) {
  2184. // Always handle new lines.
  2185. rows[nrows].start = rowStart != NULL ? rowStart : iter.str;
  2186. rows[nrows].end = rowEnd != NULL ? rowEnd : iter.str;
  2187. rows[nrows].width = rowWidth * invscale;
  2188. rows[nrows].minx = rowMinX * invscale;
  2189. rows[nrows].maxx = rowMaxX * invscale;
  2190. rows[nrows].next = iter.next;
  2191. nrows++;
  2192. if (nrows >= maxRows)
  2193. return nrows;
  2194. // Set null break point
  2195. breakEnd = rowStart;
  2196. breakWidth = 0.0;
  2197. breakMaxX = 0.0;
  2198. // Indicate to skip the white space at the beginning of the row.
  2199. rowStart = NULL;
  2200. rowEnd = NULL;
  2201. rowWidth = 0;
  2202. rowMinX = rowMaxX = 0;
  2203. } else {
  2204. if (rowStart == NULL) {
  2205. // Skip white space until the beginning of the line
  2206. if (type == NVG_CHAR) {
  2207. // The current char is the row so far
  2208. rowStartX = iter.x;
  2209. rowStart = iter.str;
  2210. rowEnd = iter.next;
  2211. rowWidth = iter.nextx - rowStartX; // q.x1 - rowStartX;
  2212. rowMinX = q.x0 - rowStartX;
  2213. rowMaxX = q.x1 - rowStartX;
  2214. wordStart = iter.str;
  2215. wordStartX = iter.x;
  2216. wordMinX = q.x0 - rowStartX;
  2217. // Set null break point
  2218. breakEnd = rowStart;
  2219. breakWidth = 0.0;
  2220. breakMaxX = 0.0;
  2221. }
  2222. } else {
  2223. float nextWidth = iter.nextx - rowStartX;
  2224. // track last non-white space character
  2225. if (type == NVG_CHAR) {
  2226. rowEnd = iter.next;
  2227. rowWidth = iter.nextx - rowStartX;
  2228. rowMaxX = q.x1 - rowStartX;
  2229. }
  2230. // track last end of a word
  2231. if (ptype == NVG_CHAR && type == NVG_SPACE) {
  2232. breakEnd = iter.str;
  2233. breakWidth = rowWidth;
  2234. breakMaxX = rowMaxX;
  2235. }
  2236. // track last beginning of a word
  2237. if (ptype == NVG_SPACE && type == NVG_CHAR) {
  2238. wordStart = iter.str;
  2239. wordStartX = iter.x;
  2240. wordMinX = q.x0 - rowStartX;
  2241. }
  2242. // Break to new line when a character is beyond break width.
  2243. if (type == NVG_CHAR && nextWidth > breakRowWidth) {
  2244. // The run length is too long, need to break to new line.
  2245. if (breakEnd == rowStart) {
  2246. // The current word is longer than the row length, just break it from here.
  2247. rows[nrows].start = rowStart;
  2248. rows[nrows].end = iter.str;
  2249. rows[nrows].width = rowWidth * invscale;
  2250. rows[nrows].minx = rowMinX * invscale;
  2251. rows[nrows].maxx = rowMaxX * invscale;
  2252. rows[nrows].next = iter.str;
  2253. nrows++;
  2254. if (nrows >= maxRows)
  2255. return nrows;
  2256. rowStartX = iter.x;
  2257. rowStart = iter.str;
  2258. rowEnd = iter.next;
  2259. rowWidth = iter.nextx - rowStartX;
  2260. rowMinX = q.x0 - rowStartX;
  2261. rowMaxX = q.x1 - rowStartX;
  2262. wordStart = iter.str;
  2263. wordStartX = iter.x;
  2264. wordMinX = q.x0 - rowStartX;
  2265. } else {
  2266. // Break the line from the end of the last word, and start new line from the beginning of the new.
  2267. rows[nrows].start = rowStart;
  2268. rows[nrows].end = breakEnd;
  2269. rows[nrows].width = breakWidth * invscale;
  2270. rows[nrows].minx = rowMinX * invscale;
  2271. rows[nrows].maxx = breakMaxX * invscale;
  2272. rows[nrows].next = wordStart;
  2273. nrows++;
  2274. if (nrows >= maxRows)
  2275. return nrows;
  2276. rowStartX = wordStartX;
  2277. rowStart = wordStart;
  2278. rowEnd = iter.next;
  2279. rowWidth = iter.nextx - rowStartX;
  2280. rowMinX = wordMinX;
  2281. rowMaxX = q.x1 - rowStartX;
  2282. // No change to the word start
  2283. }
  2284. // Set null break point
  2285. breakEnd = rowStart;
  2286. breakWidth = 0.0;
  2287. breakMaxX = 0.0;
  2288. }
  2289. }
  2290. }
  2291. pcodepoint = iter.codepoint;
  2292. ptype = type;
  2293. }
  2294. // Break the line from the end of the last word, and start new line from the beginning of the new.
  2295. if (rowStart != NULL) {
  2296. rows[nrows].start = rowStart;
  2297. rows[nrows].end = rowEnd;
  2298. rows[nrows].width = rowWidth * invscale;
  2299. rows[nrows].minx = rowMinX * invscale;
  2300. rows[nrows].maxx = rowMaxX * invscale;
  2301. rows[nrows].next = end;
  2302. nrows++;
  2303. }
  2304. return nrows;
  2305. }
  2306. float nvgTextBounds(NVGcontext* ctx, float x, float y, const char* string, const char* end, float* bounds)
  2307. {
  2308. NVGstate* state = nvg__getState(ctx);
  2309. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2310. float invscale = 1.0f / scale;
  2311. float width;
  2312. if (state->fontId == FONS_INVALID) return 0;
  2313. fonsSetSize(ctx->fs, state->fontSize*scale);
  2314. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2315. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2316. fonsSetAlign(ctx->fs, state->textAlign);
  2317. fonsSetFont(ctx->fs, state->fontId);
  2318. width = fonsTextBounds(ctx->fs, x*scale, y*scale, string, end, bounds);
  2319. if (bounds != NULL) {
  2320. // Use line bounds for height.
  2321. fonsLineBounds(ctx->fs, y*scale, &bounds[1], &bounds[3]);
  2322. bounds[0] *= invscale;
  2323. bounds[1] *= invscale;
  2324. bounds[2] *= invscale;
  2325. bounds[3] *= invscale;
  2326. }
  2327. return width * invscale;
  2328. }
  2329. void nvgTextBoxBounds(NVGcontext* ctx, float x, float y, float breakRowWidth, const char* string, const char* end, float* bounds)
  2330. {
  2331. NVGstate* state = nvg__getState(ctx);
  2332. NVGtextRow rows[2];
  2333. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2334. float invscale = 1.0f / scale;
  2335. int nrows = 0, i;
  2336. int oldAlign = state->textAlign;
  2337. int haling = state->textAlign & (NVG_ALIGN_LEFT | NVG_ALIGN_CENTER | NVG_ALIGN_RIGHT);
  2338. int valign = state->textAlign & (NVG_ALIGN_TOP | NVG_ALIGN_MIDDLE | NVG_ALIGN_BOTTOM | NVG_ALIGN_BASELINE);
  2339. float lineh = 0, rminy = 0, rmaxy = 0;
  2340. float minx, miny, maxx, maxy;
  2341. if (state->fontId == FONS_INVALID) {
  2342. if (bounds != NULL)
  2343. bounds[0] = bounds[1] = bounds[2] = bounds[3] = 0.0f;
  2344. return;
  2345. }
  2346. nvgTextMetrics(ctx, NULL, NULL, &lineh);
  2347. state->textAlign = NVG_ALIGN_LEFT | valign;
  2348. minx = maxx = x;
  2349. miny = maxy = y;
  2350. fonsSetSize(ctx->fs, state->fontSize*scale);
  2351. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2352. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2353. fonsSetAlign(ctx->fs, state->textAlign);
  2354. fonsSetFont(ctx->fs, state->fontId);
  2355. fonsLineBounds(ctx->fs, 0, &rminy, &rmaxy);
  2356. rminy *= invscale;
  2357. rmaxy *= invscale;
  2358. while ((nrows = nvgTextBreakLines(ctx, string, end, breakRowWidth, rows, 2))) {
  2359. for (i = 0; i < nrows; i++) {
  2360. NVGtextRow* row = &rows[i];
  2361. float rminx, rmaxx, dx = 0;
  2362. // Horizontal bounds
  2363. if (haling & NVG_ALIGN_LEFT)
  2364. dx = 0;
  2365. else if (haling & NVG_ALIGN_CENTER)
  2366. dx = breakRowWidth*0.5f - row->width*0.5f;
  2367. else if (haling & NVG_ALIGN_RIGHT)
  2368. dx = breakRowWidth - row->width;
  2369. rminx = x + row->minx + dx;
  2370. rmaxx = x + row->maxx + dx;
  2371. minx = nvg__minf(minx, rminx);
  2372. maxx = nvg__maxf(maxx, rmaxx);
  2373. // Vertical bounds.
  2374. miny = nvg__minf(miny, y + rminy);
  2375. maxy = nvg__maxf(maxy, y + rmaxy);
  2376. y += lineh * state->lineHeight;
  2377. }
  2378. string = rows[nrows-1].next;
  2379. }
  2380. state->textAlign = oldAlign;
  2381. if (bounds != NULL) {
  2382. bounds[0] = minx;
  2383. bounds[1] = miny;
  2384. bounds[2] = maxx;
  2385. bounds[3] = maxy;
  2386. }
  2387. }
  2388. void nvgTextMetrics(NVGcontext* ctx, float* ascender, float* descender, float* lineh)
  2389. {
  2390. NVGstate* state = nvg__getState(ctx);
  2391. float scale = nvg__getFontScale(state) * ctx->devicePxRatio;
  2392. float invscale = 1.0f / scale;
  2393. if (state->fontId == FONS_INVALID) return;
  2394. fonsSetSize(ctx->fs, state->fontSize*scale);
  2395. fonsSetSpacing(ctx->fs, state->letterSpacing*scale);
  2396. fonsSetBlur(ctx->fs, state->fontBlur*scale);
  2397. fonsSetAlign(ctx->fs, state->textAlign);
  2398. fonsSetFont(ctx->fs, state->fontId);
  2399. fonsVertMetrics(ctx->fs, ascender, descender, lineh);
  2400. if (ascender != NULL)
  2401. *ascender *= invscale;
  2402. if (descender != NULL)
  2403. *descender *= invscale;
  2404. if (lineh != NULL)
  2405. *lineh *= invscale;
  2406. }
  2407. // vim: ft=c nu noet ts=4