nanovg.cpp 75 KB

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