nuklear_rawfb.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2016-2017 Patrick Rudolph <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. /*
  23. * ==============================================================
  24. *
  25. * API
  26. *
  27. * ===============================================================
  28. */
  29. #ifndef NK_RAWFB_H_
  30. #define NK_RAWFB_H_
  31. struct rawfb_context;
  32. typedef enum rawfb_pixel_layout {
  33. PIXEL_LAYOUT_XRGB_8888,
  34. PIXEL_LAYOUT_RGBX_8888
  35. }
  36. rawfb_pl;
  37. /* All functions are thread-safe */
  38. NK_API struct rawfb_context *nk_rawfb_init(void *fb, void *tex_mem, const unsigned int w, const unsigned int h, const unsigned int pitch, const rawfb_pl pl);
  39. NK_API void nk_rawfb_render(const struct rawfb_context *rawfb, const struct nk_color clear, const unsigned char enable_clear);
  40. NK_API void nk_rawfb_shutdown(struct rawfb_context *rawfb);
  41. NK_API void nk_rawfb_resize_fb(struct rawfb_context *rawfb, void *fb, const unsigned int w, const unsigned int h, const unsigned int pitch, const rawfb_pl pl);
  42. #endif
  43. /*
  44. * ==============================================================
  45. *
  46. * IMPLEMENTATION
  47. *
  48. * ===============================================================
  49. */
  50. #ifdef NK_RAWFB_IMPLEMENTATION
  51. struct rawfb_image {
  52. void *pixels;
  53. int w, h, pitch;
  54. rawfb_pl pl;
  55. enum nk_font_atlas_format format;
  56. };
  57. struct rawfb_context {
  58. struct nk_context ctx;
  59. struct nk_rect scissors;
  60. struct rawfb_image fb;
  61. struct rawfb_image font_tex;
  62. struct nk_font_atlas atlas;
  63. };
  64. #ifndef MIN
  65. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  66. #endif
  67. #ifndef MAX
  68. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  69. #endif
  70. static unsigned int
  71. nk_rawfb_color2int(const struct nk_color c, rawfb_pl pl)
  72. {
  73. unsigned int res = 0;
  74. switch (pl) {
  75. case PIXEL_LAYOUT_RGBX_8888:
  76. res |= c.r << 24;
  77. res |= c.g << 16;
  78. res |= c.b << 8;
  79. res |= c.a;
  80. break;
  81. case PIXEL_LAYOUT_XRGB_8888:
  82. res |= c.a << 24;
  83. res |= c.r << 16;
  84. res |= c.g << 8;
  85. res |= c.b;
  86. break;
  87. default:
  88. perror("nk_rawfb_color2int(): Unsupported pixel layout.\n");
  89. break;
  90. }
  91. return (res);
  92. }
  93. static struct nk_color
  94. nk_rawfb_int2color(const unsigned int i, rawfb_pl pl)
  95. {
  96. struct nk_color col = {0,0,0,0};
  97. switch (pl) {
  98. case PIXEL_LAYOUT_RGBX_8888:
  99. col.r = (i >> 24) & 0xff;
  100. col.g = (i >> 16) & 0xff;
  101. col.b = (i >> 8) & 0xff;
  102. col.a = i & 0xff;
  103. break;
  104. case PIXEL_LAYOUT_XRGB_8888:
  105. col.a = (i >> 24) & 0xff;
  106. col.r = (i >> 16) & 0xff;
  107. col.g = (i >> 8) & 0xff;
  108. col.b = i & 0xff;
  109. break;
  110. default:
  111. perror("nk_rawfb_int2color(): Unsupported pixel layout.\n");
  112. break;
  113. }
  114. return col;
  115. }
  116. static void
  117. nk_rawfb_ctx_setpixel(const struct rawfb_context *rawfb,
  118. const short x0, const short y0, const struct nk_color col)
  119. {
  120. unsigned int c = nk_rawfb_color2int(col, rawfb->fb.pl);
  121. unsigned char *pixels = rawfb->fb.pixels;
  122. unsigned int *ptr;
  123. pixels += y0 * rawfb->fb.pitch;
  124. ptr = (unsigned int *)pixels + x0;
  125. if (y0 < rawfb->scissors.h && y0 >= rawfb->scissors.y &&
  126. x0 >= rawfb->scissors.x && x0 < rawfb->scissors.w)
  127. *ptr = c;
  128. }
  129. static void
  130. nk_rawfb_line_horizontal(const struct rawfb_context *rawfb,
  131. const short x0, const short y, const short x1, const struct nk_color col)
  132. {
  133. /* This function is called the most. Try to optimize it a bit...
  134. * It does not check for scissors or image borders.
  135. * The caller has to make sure it does no exceed bounds. */
  136. unsigned int i, n;
  137. unsigned int c[16];
  138. unsigned char *pixels = rawfb->fb.pixels;
  139. unsigned int *ptr;
  140. pixels += y * rawfb->fb.pitch;
  141. ptr = (unsigned int *)pixels + x0;
  142. n = x1 - x0;
  143. for (i = 0; i < sizeof(c) / sizeof(c[0]); i++)
  144. c[i] = nk_rawfb_color2int(col, rawfb->fb.pl);
  145. while (n > 16) {
  146. memcpy((void *)ptr, c, sizeof(c));
  147. n -= 16; ptr += 16;
  148. } for (i = 0; i < n; i++)
  149. ptr[i] = c[i];
  150. }
  151. static void
  152. nk_rawfb_img_setpixel(const struct rawfb_image *img,
  153. const int x0, const int y0, const struct nk_color col)
  154. {
  155. unsigned int c = nk_rawfb_color2int(col, img->pl);
  156. unsigned char *ptr;
  157. unsigned int *pixel;
  158. NK_ASSERT(img);
  159. if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
  160. ptr = (unsigned char *)img->pixels + (img->pitch * y0);
  161. pixel = (unsigned int *)ptr;
  162. if (img->format == NK_FONT_ATLAS_ALPHA8) {
  163. ptr[x0] = col.a;
  164. } else {
  165. pixel[x0] = c;
  166. }
  167. }
  168. }
  169. static struct nk_color
  170. nk_rawfb_img_getpixel(const struct rawfb_image *img, const int x0, const int y0)
  171. {
  172. struct nk_color col = {0, 0, 0, 0};
  173. unsigned char *ptr;
  174. unsigned int pixel;
  175. NK_ASSERT(img);
  176. if (y0 < img->h && y0 >= 0 && x0 >= 0 && x0 < img->w) {
  177. ptr = (unsigned char *)img->pixels + (img->pitch * y0);
  178. if (img->format == NK_FONT_ATLAS_ALPHA8) {
  179. col.a = ptr[x0];
  180. col.b = col.g = col.r = 0xff;
  181. } else {
  182. pixel = ((unsigned int *)ptr)[x0];
  183. col = nk_rawfb_int2color(pixel, img->pl);
  184. }
  185. } return col;
  186. }
  187. static void
  188. nk_rawfb_img_blendpixel(const struct rawfb_image *img,
  189. const int x0, const int y0, struct nk_color col)
  190. {
  191. struct nk_color col2;
  192. unsigned char inv_a;
  193. if (col.a == 0)
  194. return;
  195. inv_a = 0xff - col.a;
  196. col2 = nk_rawfb_img_getpixel(img, x0, y0);
  197. col.r = (col.r * col.a + col2.r * inv_a) >> 8;
  198. col.g = (col.g * col.a + col2.g * inv_a) >> 8;
  199. col.b = (col.b * col.a + col2.b * inv_a) >> 8;
  200. nk_rawfb_img_setpixel(img, x0, y0, col);
  201. }
  202. static void
  203. nk_rawfb_scissor(struct rawfb_context *rawfb,
  204. const float x,
  205. const float y,
  206. const float w,
  207. const float h)
  208. {
  209. rawfb->scissors.x = MIN(MAX(x, 0), rawfb->fb.w);
  210. rawfb->scissors.y = MIN(MAX(y, 0), rawfb->fb.h);
  211. rawfb->scissors.w = MIN(MAX(w + x, 0), rawfb->fb.w);
  212. rawfb->scissors.h = MIN(MAX(h + y, 0), rawfb->fb.h);
  213. }
  214. static void
  215. nk_rawfb_stroke_line(const struct rawfb_context *rawfb,
  216. short x0, short y0, short x1, short y1,
  217. const unsigned int line_thickness, const struct nk_color col)
  218. {
  219. short tmp;
  220. int dy, dx, stepx, stepy;
  221. NK_UNUSED(line_thickness);
  222. dy = y1 - y0;
  223. dx = x1 - x0;
  224. /* fast path */
  225. if (dy == 0) {
  226. if (dx == 0 || y0 >= rawfb->scissors.h || y0 < rawfb->scissors.y)
  227. return;
  228. if (dx < 0) {
  229. /* swap x0 and x1 */
  230. tmp = x1;
  231. x1 = x0;
  232. x0 = tmp;
  233. }
  234. x1 = MIN(rawfb->scissors.w, x1);
  235. x0 = MIN(rawfb->scissors.w, x0);
  236. x1 = MAX(rawfb->scissors.x, x1);
  237. x0 = MAX(rawfb->scissors.x, x0);
  238. nk_rawfb_line_horizontal(rawfb, x0, y0, x1, col);
  239. return;
  240. }
  241. if (dy < 0) {
  242. dy = -dy;
  243. stepy = -1;
  244. } else stepy = 1;
  245. if (dx < 0) {
  246. dx = -dx;
  247. stepx = -1;
  248. } else stepx = 1;
  249. dy <<= 1;
  250. dx <<= 1;
  251. nk_rawfb_ctx_setpixel(rawfb, x0, y0, col);
  252. if (dx > dy) {
  253. int fraction = dy - (dx >> 1);
  254. while (x0 != x1) {
  255. if (fraction >= 0) {
  256. y0 += stepy;
  257. fraction -= dx;
  258. }
  259. x0 += stepx;
  260. fraction += dy;
  261. nk_rawfb_ctx_setpixel(rawfb, x0, y0, col);
  262. }
  263. } else {
  264. int fraction = dx - (dy >> 1);
  265. while (y0 != y1) {
  266. if (fraction >= 0) {
  267. x0 += stepx;
  268. fraction -= dy;
  269. }
  270. y0 += stepy;
  271. fraction += dx;
  272. nk_rawfb_ctx_setpixel(rawfb, x0, y0, col);
  273. }
  274. }
  275. }
  276. static void
  277. nk_rawfb_fill_polygon(const struct rawfb_context *rawfb,
  278. const struct nk_vec2i *pnts, int count, const struct nk_color col)
  279. {
  280. int i = 0;
  281. #define MAX_POINTS 64
  282. int left = 10000, top = 10000, bottom = 0, right = 0;
  283. int nodes, nodeX[MAX_POINTS], pixelX, pixelY, j, swap ;
  284. if (count == 0) return;
  285. if (count > MAX_POINTS)
  286. count = MAX_POINTS;
  287. /* Get polygon dimensions */
  288. for (i = 0; i < count; i++) {
  289. if (left > pnts[i].x)
  290. left = pnts[i].x;
  291. if (right < pnts[i].x)
  292. right = pnts[i].x;
  293. if (top > pnts[i].y)
  294. top = pnts[i].y;
  295. if (bottom < pnts[i].y)
  296. bottom = pnts[i].y;
  297. } bottom++; right++;
  298. /* Polygon scanline algorithm released under public-domain by Darel Rex Finley, 2007 */
  299. /* Loop through the rows of the image. */
  300. for (pixelY = top; pixelY < bottom; pixelY ++) {
  301. nodes = 0; /* Build a list of nodes. */
  302. j = count - 1;
  303. for (i = 0; i < count; i++) {
  304. if (((pnts[i].y < pixelY) && (pnts[j].y >= pixelY)) ||
  305. ((pnts[j].y < pixelY) && (pnts[i].y >= pixelY))) {
  306. nodeX[nodes++]= (int)((float)pnts[i].x
  307. + ((float)pixelY - (float)pnts[i].y) / ((float)pnts[j].y - (float)pnts[i].y)
  308. * ((float)pnts[j].x - (float)pnts[i].x));
  309. } j = i;
  310. }
  311. /* Sort the nodes, via a simple “Bubble” sort. */
  312. i = 0;
  313. while (i < nodes - 1) {
  314. if (nodeX[i] > nodeX[i+1]) {
  315. swap = nodeX[i];
  316. nodeX[i] = nodeX[i+1];
  317. nodeX[i+1] = swap;
  318. if (i) i--;
  319. } else i++;
  320. }
  321. /* Fill the pixels between node pairs. */
  322. for (i = 0; i < nodes; i += 2) {
  323. if (nodeX[i+0] >= right) break;
  324. if (nodeX[i+1] > left) {
  325. if (nodeX[i+0] < left) nodeX[i+0] = left ;
  326. if (nodeX[i+1] > right) nodeX[i+1] = right;
  327. for (pixelX = nodeX[i]; pixelX < nodeX[i + 1]; pixelX++)
  328. nk_rawfb_ctx_setpixel(rawfb, pixelX, pixelY, col);
  329. }
  330. }
  331. }
  332. #undef MAX_POINTS
  333. }
  334. static void
  335. nk_rawfb_stroke_arc(const struct rawfb_context *rawfb,
  336. short x0, short y0, short w, short h, const short s,
  337. const short line_thickness, const struct nk_color col)
  338. {
  339. /* Bresenham's ellipses - modified to draw one quarter */
  340. const int a2 = (w * w) / 4;
  341. const int b2 = (h * h) / 4;
  342. const int fa2 = 4 * a2, fb2 = 4 * b2;
  343. int x, y, sigma;
  344. NK_UNUSED(line_thickness);
  345. if (s != 0 && s != 90 && s != 180 && s != 270) return;
  346. if (w < 1 || h < 1) return;
  347. /* Convert upper left to center */
  348. h = (h + 1) / 2;
  349. w = (w + 1) / 2;
  350. x0 += w; y0 += h;
  351. /* First half */
  352. for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
  353. if (s == 180)
  354. nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
  355. else if (s == 270)
  356. nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
  357. else if (s == 0)
  358. nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
  359. else if (s == 90)
  360. nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
  361. if (sigma >= 0) {
  362. sigma += fa2 * (1 - y);
  363. y--;
  364. } sigma += b2 * ((4 * x) + 6);
  365. }
  366. /* Second half */
  367. for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
  368. if (s == 180)
  369. nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
  370. else if (s == 270)
  371. nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
  372. else if (s == 0)
  373. nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
  374. else if (s == 90)
  375. nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
  376. if (sigma >= 0) {
  377. sigma += fb2 * (1 - x);
  378. x--;
  379. } sigma += a2 * ((4 * y) + 6);
  380. }
  381. }
  382. static void
  383. nk_rawfb_fill_arc(const struct rawfb_context *rawfb, short x0, short y0,
  384. short w, short h, const short s, const struct nk_color col)
  385. {
  386. /* Bresenham's ellipses - modified to fill one quarter */
  387. const int a2 = (w * w) / 4;
  388. const int b2 = (h * h) / 4;
  389. const int fa2 = 4 * a2, fb2 = 4 * b2;
  390. int x, y, sigma;
  391. struct nk_vec2i pnts[3];
  392. if (w < 1 || h < 1) return;
  393. if (s != 0 && s != 90 && s != 180 && s != 270)
  394. return;
  395. /* Convert upper left to center */
  396. h = (h + 1) / 2;
  397. w = (w + 1) / 2;
  398. x0 += w;
  399. y0 += h;
  400. pnts[0].x = x0;
  401. pnts[0].y = y0;
  402. pnts[2].x = x0;
  403. pnts[2].y = y0;
  404. /* First half */
  405. for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
  406. if (s == 180) {
  407. pnts[1].x = x0 + x; pnts[1].y = y0 + y;
  408. } else if (s == 270) {
  409. pnts[1].x = x0 - x; pnts[1].y = y0 + y;
  410. } else if (s == 0) {
  411. pnts[1].x = x0 + x; pnts[1].y = y0 - y;
  412. } else if (s == 90) {
  413. pnts[1].x = x0 - x; pnts[1].y = y0 - y;
  414. }
  415. nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
  416. pnts[2] = pnts[1];
  417. if (sigma >= 0) {
  418. sigma += fa2 * (1 - y);
  419. y--;
  420. } sigma += b2 * ((4 * x) + 6);
  421. }
  422. /* Second half */
  423. for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
  424. if (s == 180) {
  425. pnts[1].x = x0 + x; pnts[1].y = y0 + y;
  426. } else if (s == 270) {
  427. pnts[1].x = x0 - x; pnts[1].y = y0 + y;
  428. } else if (s == 0) {
  429. pnts[1].x = x0 + x; pnts[1].y = y0 - y;
  430. } else if (s == 90) {
  431. pnts[1].x = x0 - x; pnts[1].y = y0 - y;
  432. }
  433. nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
  434. pnts[2] = pnts[1];
  435. if (sigma >= 0) {
  436. sigma += fb2 * (1 - x);
  437. x--;
  438. } sigma += a2 * ((4 * y) + 6);
  439. }
  440. }
  441. static void
  442. nk_rawfb_stroke_rect(const struct rawfb_context *rawfb,
  443. const short x, const short y, const short w, const short h,
  444. const short r, const short line_thickness, const struct nk_color col)
  445. {
  446. if (r == 0) {
  447. nk_rawfb_stroke_line(rawfb, x, y, x + w, y, line_thickness, col);
  448. nk_rawfb_stroke_line(rawfb, x, y + h, x + w, y + h, line_thickness, col);
  449. nk_rawfb_stroke_line(rawfb, x, y, x, y + h, line_thickness, col);
  450. nk_rawfb_stroke_line(rawfb, x + w, y, x + w, y + h, line_thickness, col);
  451. } else {
  452. const short xc = x + r;
  453. const short yc = y + r;
  454. const short wc = (short)(w - 2 * r);
  455. const short hc = (short)(h - 2 * r);
  456. nk_rawfb_stroke_line(rawfb, xc, y, xc + wc, y, line_thickness, col);
  457. nk_rawfb_stroke_line(rawfb, x + w, yc, x + w, yc + hc, line_thickness, col);
  458. nk_rawfb_stroke_line(rawfb, xc, y + h, xc + wc, y + h, line_thickness, col);
  459. nk_rawfb_stroke_line(rawfb, x, yc, x, yc + hc, line_thickness, col);
  460. nk_rawfb_stroke_arc(rawfb, xc + wc - r, y,
  461. (unsigned)r*2, (unsigned)r*2, 0 , line_thickness, col);
  462. nk_rawfb_stroke_arc(rawfb, x, y,
  463. (unsigned)r*2, (unsigned)r*2, 90 , line_thickness, col);
  464. nk_rawfb_stroke_arc(rawfb, x, yc + hc - r,
  465. (unsigned)r*2, (unsigned)r*2, 270 , line_thickness, col);
  466. nk_rawfb_stroke_arc(rawfb, xc + wc - r, yc + hc - r,
  467. (unsigned)r*2, (unsigned)r*2, 180 , line_thickness, col);
  468. }
  469. }
  470. static void
  471. nk_rawfb_fill_rect(const struct rawfb_context *rawfb,
  472. const short x, const short y, const short w, const short h,
  473. const short r, const struct nk_color col)
  474. {
  475. int i;
  476. if (r == 0) {
  477. for (i = 0; i < h; i++)
  478. nk_rawfb_stroke_line(rawfb, x, y + i, x + w, y + i, 1, col);
  479. } else {
  480. const short xc = x + r;
  481. const short yc = y + r;
  482. const short wc = (short)(w - 2 * r);
  483. const short hc = (short)(h - 2 * r);
  484. struct nk_vec2i pnts[12];
  485. pnts[0].x = x;
  486. pnts[0].y = yc;
  487. pnts[1].x = xc;
  488. pnts[1].y = yc;
  489. pnts[2].x = xc;
  490. pnts[2].y = y;
  491. pnts[3].x = xc + wc;
  492. pnts[3].y = y;
  493. pnts[4].x = xc + wc;
  494. pnts[4].y = yc;
  495. pnts[5].x = x + w;
  496. pnts[5].y = yc;
  497. pnts[6].x = x + w;
  498. pnts[6].y = yc + hc;
  499. pnts[7].x = xc + wc;
  500. pnts[7].y = yc + hc;
  501. pnts[8].x = xc + wc;
  502. pnts[8].y = y + h;
  503. pnts[9].x = xc;
  504. pnts[9].y = y + h;
  505. pnts[10].x = xc;
  506. pnts[10].y = yc + hc;
  507. pnts[11].x = x;
  508. pnts[11].y = yc + hc;
  509. nk_rawfb_fill_polygon(rawfb, pnts, 12, col);
  510. nk_rawfb_fill_arc(rawfb, xc + wc - r, y,
  511. (unsigned)r*2, (unsigned)r*2, 0 , col);
  512. nk_rawfb_fill_arc(rawfb, x, y,
  513. (unsigned)r*2, (unsigned)r*2, 90 , col);
  514. nk_rawfb_fill_arc(rawfb, x, yc + hc - r,
  515. (unsigned)r*2, (unsigned)r*2, 270 , col);
  516. nk_rawfb_fill_arc(rawfb, xc + wc - r, yc + hc - r,
  517. (unsigned)r*2, (unsigned)r*2, 180 , col);
  518. }
  519. }
  520. NK_API void
  521. nk_rawfb_draw_rect_multi_color(const struct rawfb_context *rawfb,
  522. const short x, const short y, const short w, const short h, struct nk_color tl,
  523. struct nk_color tr, struct nk_color br, struct nk_color bl)
  524. {
  525. int i, j;
  526. struct nk_color *edge_buf;
  527. struct nk_color *edge_t;
  528. struct nk_color *edge_b;
  529. struct nk_color *edge_l;
  530. struct nk_color *edge_r;
  531. struct nk_color pixel;
  532. edge_buf = malloc(((2*w) + (2*h)) * sizeof(struct nk_color));
  533. if (edge_buf == NULL)
  534. return;
  535. edge_t = edge_buf;
  536. edge_b = edge_buf + w;
  537. edge_l = edge_buf + (w*2);
  538. edge_r = edge_buf + (w*2) + h;
  539. /* Top and bottom edge gradients */
  540. for (i=0; i<w; i++)
  541. {
  542. edge_t[i].r = (((((float)tr.r - tl.r)/(w-1))*i) + 0.5) + tl.r;
  543. edge_t[i].g = (((((float)tr.g - tl.g)/(w-1))*i) + 0.5) + tl.g;
  544. edge_t[i].b = (((((float)tr.b - tl.b)/(w-1))*i) + 0.5) + tl.b;
  545. edge_t[i].a = (((((float)tr.a - tl.a)/(w-1))*i) + 0.5) + tl.a;
  546. edge_b[i].r = (((((float)br.r - bl.r)/(w-1))*i) + 0.5) + bl.r;
  547. edge_b[i].g = (((((float)br.g - bl.g)/(w-1))*i) + 0.5) + bl.g;
  548. edge_b[i].b = (((((float)br.b - bl.b)/(w-1))*i) + 0.5) + bl.b;
  549. edge_b[i].a = (((((float)br.a - bl.a)/(w-1))*i) + 0.5) + bl.a;
  550. }
  551. /* Left and right edge gradients */
  552. for (i=0; i<h; i++)
  553. {
  554. edge_l[i].r = (((((float)bl.r - tl.r)/(h-1))*i) + 0.5) + tl.r;
  555. edge_l[i].g = (((((float)bl.g - tl.g)/(h-1))*i) + 0.5) + tl.g;
  556. edge_l[i].b = (((((float)bl.b - tl.b)/(h-1))*i) + 0.5) + tl.b;
  557. edge_l[i].a = (((((float)bl.a - tl.a)/(h-1))*i) + 0.5) + tl.a;
  558. edge_r[i].r = (((((float)br.r - tr.r)/(h-1))*i) + 0.5) + tr.r;
  559. edge_r[i].g = (((((float)br.g - tr.g)/(h-1))*i) + 0.5) + tr.g;
  560. edge_r[i].b = (((((float)br.b - tr.b)/(h-1))*i) + 0.5) + tr.b;
  561. edge_r[i].a = (((((float)br.a - tr.a)/(h-1))*i) + 0.5) + tr.a;
  562. }
  563. for (i=0; i<h; i++) {
  564. for (j=0; j<w; j++) {
  565. if (i==0) {
  566. nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_t[j]);
  567. } else if (i==h-1) {
  568. nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_b[j]);
  569. } else {
  570. if (j==0) {
  571. nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_l[i]);
  572. } else if (j==w-1) {
  573. nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, edge_r[i]);
  574. } else {
  575. pixel.r = (((((float)edge_r[i].r - edge_l[i].r)/(w-1))*j) + 0.5) + edge_l[i].r;
  576. pixel.g = (((((float)edge_r[i].g - edge_l[i].g)/(w-1))*j) + 0.5) + edge_l[i].g;
  577. pixel.b = (((((float)edge_r[i].b - edge_l[i].b)/(w-1))*j) + 0.5) + edge_l[i].b;
  578. pixel.a = (((((float)edge_r[i].a - edge_l[i].a)/(w-1))*j) + 0.5) + edge_l[i].a;
  579. nk_rawfb_img_blendpixel(&rawfb->fb, x+j, y+i, pixel);
  580. }
  581. }
  582. }
  583. }
  584. free(edge_buf);
  585. }
  586. static void
  587. nk_rawfb_fill_triangle(const struct rawfb_context *rawfb,
  588. const short x0, const short y0, const short x1, const short y1,
  589. const short x2, const short y2, const struct nk_color col)
  590. {
  591. struct nk_vec2i pnts[3];
  592. pnts[0].x = x0;
  593. pnts[0].y = y0;
  594. pnts[1].x = x1;
  595. pnts[1].y = y1;
  596. pnts[2].x = x2;
  597. pnts[2].y = y2;
  598. nk_rawfb_fill_polygon(rawfb, pnts, 3, col);
  599. }
  600. static void
  601. nk_rawfb_stroke_triangle(const struct rawfb_context *rawfb,
  602. const short x0, const short y0, const short x1, const short y1,
  603. const short x2, const short y2, const unsigned short line_thickness,
  604. const struct nk_color col)
  605. {
  606. nk_rawfb_stroke_line(rawfb, x0, y0, x1, y1, line_thickness, col);
  607. nk_rawfb_stroke_line(rawfb, x1, y1, x2, y2, line_thickness, col);
  608. nk_rawfb_stroke_line(rawfb, x2, y2, x0, y0, line_thickness, col);
  609. }
  610. static void
  611. nk_rawfb_stroke_polygon(const struct rawfb_context *rawfb,
  612. const struct nk_vec2i *pnts, const int count,
  613. const unsigned short line_thickness, const struct nk_color col)
  614. {
  615. int i;
  616. for (i = 1; i < count; ++i)
  617. nk_rawfb_stroke_line(rawfb, pnts[i-1].x, pnts[i-1].y, pnts[i].x,
  618. pnts[i].y, line_thickness, col);
  619. nk_rawfb_stroke_line(rawfb, pnts[count-1].x, pnts[count-1].y,
  620. pnts[0].x, pnts[0].y, line_thickness, col);
  621. }
  622. static void
  623. nk_rawfb_stroke_polyline(const struct rawfb_context *rawfb,
  624. const struct nk_vec2i *pnts, const int count,
  625. const unsigned short line_thickness, const struct nk_color col)
  626. {
  627. int i;
  628. for (i = 0; i < count-1; ++i)
  629. nk_rawfb_stroke_line(rawfb, pnts[i].x, pnts[i].y,
  630. pnts[i+1].x, pnts[i+1].y, line_thickness, col);
  631. }
  632. static void
  633. nk_rawfb_fill_circle(const struct rawfb_context *rawfb,
  634. short x0, short y0, short w, short h, const struct nk_color col)
  635. {
  636. /* Bresenham's ellipses */
  637. const int a2 = (w * w) / 4;
  638. const int b2 = (h * h) / 4;
  639. const int fa2 = 4 * a2, fb2 = 4 * b2;
  640. int x, y, sigma;
  641. /* Convert upper left to center */
  642. h = (h + 1) / 2;
  643. w = (w + 1) / 2;
  644. x0 += w;
  645. y0 += h;
  646. /* First half */
  647. for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
  648. nk_rawfb_stroke_line(rawfb, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
  649. nk_rawfb_stroke_line(rawfb, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
  650. if (sigma >= 0) {
  651. sigma += fa2 * (1 - y);
  652. y--;
  653. } sigma += b2 * ((4 * x) + 6);
  654. }
  655. /* Second half */
  656. for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
  657. nk_rawfb_stroke_line(rawfb, x0 - x, y0 + y, x0 + x, y0 + y, 1, col);
  658. nk_rawfb_stroke_line(rawfb, x0 - x, y0 - y, x0 + x, y0 - y, 1, col);
  659. if (sigma >= 0) {
  660. sigma += fb2 * (1 - x);
  661. x--;
  662. } sigma += a2 * ((4 * y) + 6);
  663. }
  664. }
  665. static void
  666. nk_rawfb_stroke_circle(const struct rawfb_context *rawfb,
  667. short x0, short y0, short w, short h, const short line_thickness,
  668. const struct nk_color col)
  669. {
  670. /* Bresenham's ellipses */
  671. const int a2 = (w * w) / 4;
  672. const int b2 = (h * h) / 4;
  673. const int fa2 = 4 * a2, fb2 = 4 * b2;
  674. int x, y, sigma;
  675. NK_UNUSED(line_thickness);
  676. /* Convert upper left to center */
  677. h = (h + 1) / 2;
  678. w = (w + 1) / 2;
  679. x0 += w;
  680. y0 += h;
  681. /* First half */
  682. for (x = 0, y = h, sigma = 2*b2+a2*(1-2*h); b2*x <= a2*y; x++) {
  683. nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
  684. nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
  685. nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
  686. nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
  687. if (sigma >= 0) {
  688. sigma += fa2 * (1 - y);
  689. y--;
  690. } sigma += b2 * ((4 * x) + 6);
  691. }
  692. /* Second half */
  693. for (x = w, y = 0, sigma = 2*a2+b2*(1-2*w); a2*y <= b2*x; y++) {
  694. nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 + y, col);
  695. nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 + y, col);
  696. nk_rawfb_ctx_setpixel(rawfb, x0 + x, y0 - y, col);
  697. nk_rawfb_ctx_setpixel(rawfb, x0 - x, y0 - y, col);
  698. if (sigma >= 0) {
  699. sigma += fb2 * (1 - x);
  700. x--;
  701. } sigma += a2 * ((4 * y) + 6);
  702. }
  703. }
  704. static void
  705. nk_rawfb_stroke_curve(const struct rawfb_context *rawfb,
  706. const struct nk_vec2i p1, const struct nk_vec2i p2,
  707. const struct nk_vec2i p3, const struct nk_vec2i p4,
  708. const unsigned int num_segments, const unsigned short line_thickness,
  709. const struct nk_color col)
  710. {
  711. unsigned int i_step, segments;
  712. float t_step;
  713. struct nk_vec2i last = p1;
  714. segments = MAX(num_segments, 1);
  715. t_step = 1.0f/(float)segments;
  716. for (i_step = 1; i_step <= segments; ++i_step) {
  717. float t = t_step * (float)i_step;
  718. float u = 1.0f - t;
  719. float w1 = u*u*u;
  720. float w2 = 3*u*u*t;
  721. float w3 = 3*u*t*t;
  722. float w4 = t * t *t;
  723. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  724. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  725. nk_rawfb_stroke_line(rawfb, last.x, last.y,
  726. (short)x, (short)y, line_thickness,col);
  727. last.x = (short)x; last.y = (short)y;
  728. }
  729. }
  730. static void
  731. nk_rawfb_clear(const struct rawfb_context *rawfb, const struct nk_color col)
  732. {
  733. nk_rawfb_fill_rect(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h, 0, col);
  734. }
  735. NK_API struct rawfb_context*
  736. nk_rawfb_init(void *fb, void *tex_mem, const unsigned int w, const unsigned int h,
  737. const unsigned int pitch, const rawfb_pl pl)
  738. {
  739. const void *tex;
  740. struct rawfb_context *rawfb;
  741. rawfb = malloc(sizeof(struct rawfb_context));
  742. if (!rawfb)
  743. return NULL;
  744. memset(rawfb, 0, sizeof(struct rawfb_context));
  745. rawfb->font_tex.pixels = tex_mem;
  746. rawfb->font_tex.format = NK_FONT_ATLAS_ALPHA8;
  747. rawfb->font_tex.w = rawfb->font_tex.h = 0;
  748. rawfb->fb.pixels = fb;
  749. rawfb->fb.w= w;
  750. rawfb->fb.h = h;
  751. rawfb->fb.pl = pl;
  752. if (pl == PIXEL_LAYOUT_RGBX_8888 || pl == PIXEL_LAYOUT_XRGB_8888) {
  753. rawfb->fb.format = NK_FONT_ATLAS_RGBA32;
  754. rawfb->fb.pitch = pitch;
  755. }
  756. else {
  757. perror("nk_rawfb_init(): Unsupported pixel layout.\n");
  758. free(rawfb);
  759. return NULL;
  760. }
  761. if (0 == nk_init_default(&rawfb->ctx, 0)) {
  762. free(rawfb);
  763. return NULL;
  764. }
  765. nk_font_atlas_init_default(&rawfb->atlas);
  766. nk_font_atlas_begin(&rawfb->atlas);
  767. tex = nk_font_atlas_bake(&rawfb->atlas, &rawfb->font_tex.w, &rawfb->font_tex.h, rawfb->font_tex.format);
  768. if (!tex) {
  769. free(rawfb);
  770. return NULL;
  771. }
  772. switch(rawfb->font_tex.format) {
  773. case NK_FONT_ATLAS_ALPHA8:
  774. rawfb->font_tex.pitch = rawfb->font_tex.w * 1;
  775. break;
  776. case NK_FONT_ATLAS_RGBA32:
  777. rawfb->font_tex.pitch = rawfb->font_tex.w * 4;
  778. break;
  779. };
  780. /* Store the font texture in tex scratch memory */
  781. memcpy(rawfb->font_tex.pixels, tex, rawfb->font_tex.pitch * rawfb->font_tex.h);
  782. nk_font_atlas_end(&rawfb->atlas, nk_handle_ptr(NULL), NULL);
  783. if (rawfb->atlas.default_font)
  784. nk_style_set_font(&rawfb->ctx, &rawfb->atlas.default_font->handle);
  785. nk_style_load_all_cursors(&rawfb->ctx, rawfb->atlas.cursors);
  786. nk_rawfb_scissor(rawfb, 0, 0, rawfb->fb.w, rawfb->fb.h);
  787. return rawfb;
  788. }
  789. static void
  790. nk_rawfb_stretch_image(const struct rawfb_image *dst,
  791. const struct rawfb_image *src, const struct nk_rect *dst_rect,
  792. const struct nk_rect *src_rect, const struct nk_rect *dst_scissors,
  793. const struct nk_color *fg)
  794. {
  795. short i, j;
  796. struct nk_color col;
  797. float xinc = src_rect->w / dst_rect->w;
  798. float yinc = src_rect->h / dst_rect->h;
  799. float xoff = src_rect->x, yoff = src_rect->y;
  800. /* Simple nearest filtering rescaling */
  801. /* TODO: use bilinear filter */
  802. for (j = 0; j < (short)dst_rect->h; j++) {
  803. for (i = 0; i < (short)dst_rect->w; i++) {
  804. if (dst_scissors) {
  805. if (i + (int)(dst_rect->x + 0.5f) < dst_scissors->x || i + (int)(dst_rect->x + 0.5f) >= dst_scissors->w)
  806. continue;
  807. if (j + (int)(dst_rect->y + 0.5f) < dst_scissors->y || j + (int)(dst_rect->y + 0.5f) >= dst_scissors->h)
  808. continue;
  809. }
  810. col = nk_rawfb_img_getpixel(src, (int)xoff, (int) yoff);
  811. if (col.r || col.g || col.b)
  812. {
  813. col.r = fg->r;
  814. col.g = fg->g;
  815. col.b = fg->b;
  816. }
  817. nk_rawfb_img_blendpixel(dst, i + (int)(dst_rect->x + 0.5f), j + (int)(dst_rect->y + 0.5f), col);
  818. xoff += xinc;
  819. }
  820. xoff = src_rect->x;
  821. yoff += yinc;
  822. }
  823. }
  824. static void
  825. nk_rawfb_font_query_font_glyph(nk_handle handle, const float height,
  826. struct nk_user_font_glyph *glyph, const nk_rune codepoint,
  827. const nk_rune next_codepoint)
  828. {
  829. float scale;
  830. const struct nk_font_glyph *g;
  831. struct nk_font *font;
  832. NK_ASSERT(glyph);
  833. NK_UNUSED(next_codepoint);
  834. font = (struct nk_font*)handle.ptr;
  835. NK_ASSERT(font);
  836. NK_ASSERT(font->glyphs);
  837. if (!font || !glyph)
  838. return;
  839. scale = height/font->info.height;
  840. g = nk_font_find_glyph(font, codepoint);
  841. glyph->width = (g->x1 - g->x0) * scale;
  842. glyph->height = (g->y1 - g->y0) * scale;
  843. glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
  844. glyph->xadvance = (g->xadvance * scale);
  845. glyph->uv[0] = nk_vec2(g->u0, g->v0);
  846. glyph->uv[1] = nk_vec2(g->u1, g->v1);
  847. }
  848. NK_API void
  849. nk_rawfb_draw_text(const struct rawfb_context *rawfb,
  850. const struct nk_user_font *font, const struct nk_rect rect,
  851. const char *text, const int len, const float font_height,
  852. const struct nk_color fg)
  853. {
  854. float x = 0;
  855. int text_len = 0;
  856. nk_rune unicode = 0;
  857. nk_rune next = 0;
  858. int glyph_len = 0;
  859. int next_glyph_len = 0;
  860. struct nk_user_font_glyph g;
  861. if (!len || !text) return;
  862. x = 0;
  863. glyph_len = nk_utf_decode(text, &unicode, len);
  864. if (!glyph_len) return;
  865. /* draw every glyph image */
  866. while (text_len < len && glyph_len) {
  867. struct nk_rect src_rect;
  868. struct nk_rect dst_rect;
  869. float char_width = 0;
  870. if (unicode == NK_UTF_INVALID) break;
  871. /* query currently drawn glyph information */
  872. next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
  873. nk_rawfb_font_query_font_glyph(font->userdata, font_height, &g, unicode,
  874. (next == NK_UTF_INVALID) ? '\0' : next);
  875. /* calculate and draw glyph drawing rectangle and image */
  876. char_width = g.xadvance;
  877. src_rect.x = g.uv[0].x * rawfb->font_tex.w;
  878. src_rect.y = g.uv[0].y * rawfb->font_tex.h;
  879. src_rect.w = g.uv[1].x * rawfb->font_tex.w - g.uv[0].x * rawfb->font_tex.w;
  880. src_rect.h = g.uv[1].y * rawfb->font_tex.h - g.uv[0].y * rawfb->font_tex.h;
  881. dst_rect.x = x + g.offset.x + rect.x;
  882. dst_rect.y = g.offset.y + rect.y;
  883. dst_rect.w = ceilf(g.width);
  884. dst_rect.h = ceilf(g.height);
  885. /* Use software rescaling to blit glyph from font_text to framebuffer */
  886. nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, &fg);
  887. /* offset next glyph */
  888. text_len += glyph_len;
  889. x += char_width;
  890. glyph_len = next_glyph_len;
  891. unicode = next;
  892. }
  893. }
  894. NK_API void
  895. nk_rawfb_drawimage(const struct rawfb_context *rawfb,
  896. const int x, const int y, const int w, const int h,
  897. const struct nk_image *img, const struct nk_color *col)
  898. {
  899. struct nk_rect src_rect;
  900. struct nk_rect dst_rect;
  901. src_rect.x = img->region[0];
  902. src_rect.y = img->region[1];
  903. src_rect.w = img->region[2];
  904. src_rect.h = img->region[3];
  905. dst_rect.x = x;
  906. dst_rect.y = y;
  907. dst_rect.w = w;
  908. dst_rect.h = h;
  909. nk_rawfb_stretch_image(&rawfb->fb, &rawfb->font_tex, &dst_rect, &src_rect, &rawfb->scissors, col);
  910. }
  911. NK_API void
  912. nk_rawfb_shutdown(struct rawfb_context *rawfb)
  913. {
  914. if (rawfb) {
  915. nk_free(&rawfb->ctx);
  916. memset(rawfb, 0, sizeof(struct rawfb_context));
  917. free(rawfb);
  918. }
  919. }
  920. NK_API void
  921. nk_rawfb_resize_fb(struct rawfb_context *rawfb,
  922. void *fb,
  923. const unsigned int w,
  924. const unsigned int h,
  925. const unsigned int pitch,
  926. const rawfb_pl pl)
  927. {
  928. rawfb->fb.w = w;
  929. rawfb->fb.h = h;
  930. rawfb->fb.pixels = fb;
  931. rawfb->fb.pitch = pitch;
  932. rawfb->fb.pl = pl;
  933. }
  934. NK_API void
  935. nk_rawfb_render(const struct rawfb_context *rawfb,
  936. const struct nk_color clear,
  937. const unsigned char enable_clear)
  938. {
  939. const struct nk_command *cmd;
  940. if (enable_clear)
  941. nk_rawfb_clear(rawfb, clear);
  942. nk_foreach(cmd, (struct nk_context*)&rawfb->ctx) {
  943. switch (cmd->type) {
  944. case NK_COMMAND_NOP: break;
  945. case NK_COMMAND_SCISSOR: {
  946. const struct nk_command_scissor *s =(const struct nk_command_scissor*)cmd;
  947. nk_rawfb_scissor((struct rawfb_context *)rawfb, s->x, s->y, s->w, s->h);
  948. } break;
  949. case NK_COMMAND_LINE: {
  950. const struct nk_command_line *l = (const struct nk_command_line *)cmd;
  951. nk_rawfb_stroke_line(rawfb, l->begin.x, l->begin.y, l->end.x,
  952. l->end.y, l->line_thickness, l->color);
  953. } break;
  954. case NK_COMMAND_RECT: {
  955. const struct nk_command_rect *r = (const struct nk_command_rect *)cmd;
  956. nk_rawfb_stroke_rect(rawfb, r->x, r->y, r->w, r->h,
  957. (unsigned short)r->rounding, r->line_thickness, r->color);
  958. } break;
  959. case NK_COMMAND_RECT_FILLED: {
  960. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled *)cmd;
  961. nk_rawfb_fill_rect(rawfb, r->x, r->y, r->w, r->h,
  962. (unsigned short)r->rounding, r->color);
  963. } break;
  964. case NK_COMMAND_CIRCLE: {
  965. const struct nk_command_circle *c = (const struct nk_command_circle *)cmd;
  966. nk_rawfb_stroke_circle(rawfb, c->x, c->y, c->w, c->h, c->line_thickness, c->color);
  967. } break;
  968. case NK_COMMAND_CIRCLE_FILLED: {
  969. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  970. nk_rawfb_fill_circle(rawfb, c->x, c->y, c->w, c->h, c->color);
  971. } break;
  972. case NK_COMMAND_TRIANGLE: {
  973. const struct nk_command_triangle*t = (const struct nk_command_triangle*)cmd;
  974. nk_rawfb_stroke_triangle(rawfb, t->a.x, t->a.y, t->b.x, t->b.y,
  975. t->c.x, t->c.y, t->line_thickness, t->color);
  976. } break;
  977. case NK_COMMAND_TRIANGLE_FILLED: {
  978. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled *)cmd;
  979. nk_rawfb_fill_triangle(rawfb, t->a.x, t->a.y, t->b.x, t->b.y,
  980. t->c.x, t->c.y, t->color);
  981. } break;
  982. case NK_COMMAND_POLYGON: {
  983. const struct nk_command_polygon *p =(const struct nk_command_polygon*)cmd;
  984. nk_rawfb_stroke_polygon(rawfb, p->points, p->point_count, p->line_thickness,p->color);
  985. } break;
  986. case NK_COMMAND_POLYGON_FILLED: {
  987. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled *)cmd;
  988. nk_rawfb_fill_polygon(rawfb, p->points, p->point_count, p->color);
  989. } break;
  990. case NK_COMMAND_POLYLINE: {
  991. const struct nk_command_polyline *p = (const struct nk_command_polyline *)cmd;
  992. nk_rawfb_stroke_polyline(rawfb, p->points, p->point_count, p->line_thickness, p->color);
  993. } break;
  994. case NK_COMMAND_TEXT: {
  995. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  996. nk_rawfb_draw_text(rawfb, t->font, nk_rect(t->x, t->y, t->w, t->h),
  997. t->string, t->length, t->height, t->foreground);
  998. } break;
  999. case NK_COMMAND_CURVE: {
  1000. const struct nk_command_curve *q = (const struct nk_command_curve *)cmd;
  1001. nk_rawfb_stroke_curve(rawfb, q->begin, q->ctrl[0], q->ctrl[1],
  1002. q->end, 22, q->line_thickness, q->color);
  1003. } break;
  1004. case NK_COMMAND_RECT_MULTI_COLOR: {
  1005. const struct nk_command_rect_multi_color *q = (const struct nk_command_rect_multi_color *)cmd;
  1006. nk_rawfb_draw_rect_multi_color(rawfb, q->x, q->y, q->w, q->h, q->left, q->top, q->right, q->bottom);
  1007. } break;
  1008. case NK_COMMAND_IMAGE: {
  1009. const struct nk_command_image *q = (const struct nk_command_image *)cmd;
  1010. nk_rawfb_drawimage(rawfb, q->x, q->y, q->w, q->h, &q->img, &q->col);
  1011. } break;
  1012. case NK_COMMAND_ARC: {
  1013. assert(0 && "NK_COMMAND_ARC not implemented\n");
  1014. } break;
  1015. case NK_COMMAND_ARC_FILLED: {
  1016. assert(0 && "NK_COMMAND_ARC_FILLED not implemented\n");
  1017. } break;
  1018. default: break;
  1019. }
  1020. } nk_clear((struct nk_context*)&rawfb->ctx);
  1021. }
  1022. #endif