You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nanovg.c 74KB

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