*/}}

nanovg.c 76 KB

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