nanovg.c 75 KB

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