dc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * File : dc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtgui/dc.h>
  15. #include <rtgui/rtgui_system.h>
  16. #include <string.h> /* for strlen */
  17. #include <stdlib.h> /* fir qsort */
  18. /* for sin/cos etc */
  19. #include <math.h>
  20. #ifndef M_PI
  21. #define M_PI 3.14159265358979323846
  22. #endif
  23. void rtgui_dc_destory(struct rtgui_dc* dc)
  24. {
  25. if (dc == RT_NULL) return;
  26. dc->engine->fini(dc);
  27. rtgui_free(dc);
  28. }
  29. void rtgui_dc_draw_line (struct rtgui_dc* dc, int x1, int y1, int x2, int y2)
  30. {
  31. if (dc == RT_NULL) return;
  32. if (y1 == y2)
  33. {
  34. rtgui_dc_draw_hline(dc, x1, x2, y1);
  35. }
  36. else if (x1 == x2)
  37. {
  38. rtgui_dc_draw_vline(dc, x1, y1, y2);
  39. }
  40. else
  41. {
  42. int dx, dy, sdx, sdy, dxabs, dyabs, x, y, px, py;
  43. register rt_base_t i;
  44. /* rtgui_rect_t rect; */
  45. dx = x2 - x1; /* the horizontal distance of the line */
  46. dy = y2 - y1; /* the vertical distance of the line */
  47. #define rtgui_sgn(x) ((x<0)?-1:((x>0)?1:0)) /* macro to return the sign of a number */
  48. #define rtgui_abs(x) ((x)>=0? (x):-(x)) /* macro to return the absolute value */
  49. dxabs = rtgui_abs(dx);
  50. dyabs = rtgui_abs(dy);
  51. sdx = rtgui_sgn(dx);
  52. sdy = rtgui_sgn(dy);
  53. x = dyabs >> 1;
  54. y = dxabs >> 1;
  55. px = x1;
  56. py = y1;
  57. if(dxabs >= dyabs) /* the line is more horizontal than vertical */
  58. {
  59. for(i = 0; i < dxabs; i++)
  60. {
  61. y += dyabs;
  62. if(y >= dxabs)
  63. {
  64. y -= dxabs;
  65. py += sdy;
  66. }
  67. px += sdx;
  68. /* draw this point */
  69. rtgui_dc_draw_point(dc, px, py);
  70. }
  71. }
  72. else /* the line is more vertical than horizontal */
  73. {
  74. for(i = 0; i < dyabs; i++)
  75. {
  76. x += dxabs;
  77. if(x >= dyabs)
  78. {
  79. x -= dyabs;
  80. px += sdx;
  81. }
  82. py += sdy;
  83. /* draw this point */
  84. rtgui_dc_draw_point(dc, px, py);
  85. }
  86. }
  87. }
  88. }
  89. void rtgui_dc_draw_rect (struct rtgui_dc* dc, struct rtgui_rect* rect)
  90. {
  91. rtgui_dc_draw_hline(dc, rect->x1, rect->x2, rect->y1);
  92. rtgui_dc_draw_hline(dc, rect->x1, rect->x2, rect->y2 - 1);
  93. rtgui_dc_draw_vline(dc, rect->x1, rect->y1, rect->y2);
  94. rtgui_dc_draw_vline(dc, rect->x2 - 1, rect->y1, rect->y2);
  95. }
  96. void rtgui_dc_fill_rect_forecolor(struct rtgui_dc* dc, struct rtgui_rect* rect)
  97. {
  98. int i = 0;
  99. rtgui_dc_draw_rect(dc, rect);
  100. do
  101. {
  102. rtgui_dc_draw_hline(dc, rect->x1+1, rect->x2-1, rect->y1+i);
  103. i++;
  104. }while(!(rect->y1+i == rect->y2));
  105. }
  106. void rtgui_dc_draw_round_rect(struct rtgui_dc* dc, struct rtgui_rect* rect, int r)
  107. {
  108. RT_ASSERT(((rect->x2 - rect->x1)/2 >= r)&&((rect->y2-rect->y1)/2 >= r));
  109. if(((rect->x2 - rect->x1)/2 >= r)&&((rect->y2-rect->y1)/2 >= r))
  110. {
  111. rtgui_dc_draw_arc(dc, rect->x1 + r, rect->y1 + r, r, 180, 270);
  112. rtgui_dc_draw_arc(dc, rect->x2 - r, rect->y1 + r, r, 270, 360);
  113. rtgui_dc_draw_arc(dc, rect->x1 + r, rect->y2 - r, r, 90, 180);
  114. rtgui_dc_draw_arc(dc, rect->x2 - r, rect->y2 - r, r, 0, 90);
  115. rtgui_dc_draw_hline(dc, rect->x1 + r, rect->x2 - r, rect->y1);
  116. rtgui_dc_draw_hline(dc, rect->x1 + r, rect->x2 - r, rect->y2);
  117. rtgui_dc_draw_vline(dc, rect->x1, rect->y1 + r, rect->y2 - r);
  118. rtgui_dc_draw_vline(dc, rect->x2, rect->y1 + r, rect->y2 - r);
  119. }
  120. }
  121. void rtgui_dc_fill_round_rect(struct rtgui_dc* dc, struct rtgui_rect* rect, int r)
  122. {
  123. struct rtgui_rect rect_temp;
  124. RT_ASSERT(((rect->x2 - rect->x1)/2 >= r)&&((rect->y2-rect->y1)/2 >= r));
  125. if(((rect->x2 - rect->x1)/2 >= r)&&((rect->y2-rect->y1)/2 >= r))
  126. {
  127. rect_temp.x1 = rect->x1 + r;
  128. rect_temp.y1 = rect->y1;
  129. rect_temp.x2 = rect->x2 - r;
  130. rect_temp.y2 = rect->y2;
  131. rtgui_dc_fill_rect_forecolor(dc, &rect_temp);//fill rect with foreground
  132. rect_temp.x1 = rect->x1;
  133. rect_temp.y1 = rect->y1 + r;
  134. rect_temp.x2 = rect->x1 + r;
  135. rect_temp.y2 = rect->y2 - r;
  136. rtgui_dc_fill_rect_forecolor(dc, &rect_temp);//fill rect with foreground
  137. rect_temp.x1 = rect->x2 - r;
  138. rect_temp.y1 = rect->y1 + r;
  139. rect_temp.x2 = rect->x2;
  140. rect_temp.y2 = rect->y2 - r;
  141. rtgui_dc_fill_rect_forecolor(dc, &rect_temp);//fill rect with foreground
  142. rtgui_dc_fill_circle(dc, rect->x1 + r, rect->y1 + r, r);
  143. rtgui_dc_fill_circle(dc, rect->x2 - r, rect->y2 - r, r);
  144. rtgui_dc_fill_circle(dc, rect->x2 - r, rect->y1 + r, r);
  145. rtgui_dc_fill_circle(dc, rect->x1 + r, rect->y2 - r, r);
  146. }
  147. }
  148. void rtgui_dc_draw_text (struct rtgui_dc* dc, const char* text, struct rtgui_rect* rect)
  149. {
  150. rt_uint32_t len;
  151. struct rtgui_font *font;
  152. #ifdef RTGUI_USING_FONTHZ
  153. struct rtgui_font *gb2312_font;
  154. #endif
  155. struct rtgui_rect text_rect;
  156. RT_ASSERT(dc != RT_NULL);
  157. font = RTGUI_DC_FONT(dc);
  158. if (font == RT_NULL)
  159. {
  160. /* use system default font */
  161. font = rtgui_font_default();
  162. }
  163. #ifdef RTGUI_USING_FONTHZ
  164. gb2312_font = rtgui_font_refer("hz", font->height);
  165. if (gb2312_font == RT_NULL)
  166. {
  167. gb2312_font = rtgui_font_refer("hz", 16);
  168. }
  169. #endif
  170. /* text align */
  171. rtgui_font_get_metrics(font, text, &text_rect);
  172. rtgui_rect_moveto_align(rect, &text_rect, RTGUI_DC_TEXTALIGN(dc));
  173. #ifdef RTGUI_USING_FONTHZ
  174. while (*text)
  175. {
  176. len = 0;
  177. while (((rt_uint8_t)*(text + len)) < 0x80 && *(text + len)) len ++;
  178. if (len > 0)
  179. {
  180. rtgui_font_draw(font, dc, text, len, &text_rect);
  181. text += len;
  182. }
  183. len = 0;
  184. while (((rt_uint8_t)*(text + len)) > 0x80) len ++;
  185. if (len > 0)
  186. {
  187. rtgui_font_draw(gb2312_font, dc, text, len, &text_rect);
  188. text += len;
  189. }
  190. }
  191. rtgui_font_derefer(gb2312_font);
  192. #else
  193. len = strlen((const char*)text);
  194. rtgui_font_draw(font, dc, text, len, &text_rect);
  195. #endif
  196. }
  197. /*
  198. * draw a monochrome color bitmap data
  199. */
  200. void rtgui_dc_draw_mono_bmp(struct rtgui_dc* dc, int x, int y, int w, int h, const rt_uint8_t* data)
  201. {
  202. int word_bytes;
  203. int i, j, k;
  204. /* get word bytes */
  205. word_bytes = (w + 7)/8;
  206. /* draw mono bitmap data */
  207. for (i = 0; i < h; i ++)
  208. for (j = 0; j < word_bytes; j++)
  209. for (k = 0; k < 8; k++)
  210. if ( ((data[i*2 + j] >> (7-k)) & 0x01) != 0)
  211. rtgui_dc_draw_point(dc, x + 8*j + k, y + i);
  212. }
  213. void rtgui_dc_draw_byte(struct rtgui_dc*dc, int x, int y, int h, const rt_uint8_t* data)
  214. {
  215. int i, k;
  216. /* draw byte */
  217. for (i=0; i < h; i ++)
  218. {
  219. for (k=0; k < 8; k++)
  220. {
  221. if (((data[i] >> (7-k)) & 0x01) != 0)
  222. {
  223. rtgui_dc_draw_point(dc, x + k, y + i);
  224. }
  225. }
  226. }
  227. }
  228. void rtgui_dc_draw_word(struct rtgui_dc*dc, int x, int y, int h, const rt_uint8_t* data)
  229. {
  230. int i, j, k;
  231. /* draw word */
  232. for (i=0; i < h; i ++)
  233. {
  234. for (j=0; j < 2; j++)
  235. for (k=0; k < 8; k++)
  236. {
  237. if (((data[i * 2 + j] >> (7-k)) & 0x01) != 0)
  238. {
  239. rtgui_dc_draw_point(dc, x + 8*j + k, y + i);
  240. }
  241. }
  242. }
  243. }
  244. void rtgui_dc_draw_shaded_rect(struct rtgui_dc* dc, rtgui_rect_t* rect,
  245. rtgui_color_t c1, rtgui_color_t c2)
  246. {
  247. RT_ASSERT(dc != RT_NULL);
  248. RTGUI_DC_FC(dc) = c1;
  249. rtgui_dc_draw_vline(dc, rect->x1, rect->y1, rect->y2);
  250. rtgui_dc_draw_hline(dc, rect->x1 + 1, rect->x2, rect->y1);
  251. RTGUI_DC_FC(dc) = c2;
  252. rtgui_dc_draw_vline(dc, rect->x2, rect->y1, rect->y2);
  253. rtgui_dc_draw_hline(dc, rect->x1, rect->x2 + 1, rect->y2);
  254. }
  255. void rtgui_dc_draw_border(struct rtgui_dc* dc, rtgui_rect_t* rect, int flag)
  256. {
  257. rtgui_rect_t r;
  258. rtgui_color_t color;
  259. if (dc == RT_NULL) return ;
  260. /* save old color */
  261. color = RTGUI_DC_FC(dc);
  262. r = *rect;
  263. switch (flag)
  264. {
  265. case RTGUI_BORDER_RAISE:
  266. rtgui_dc_draw_shaded_rect(dc, &r, high_light, black);
  267. rtgui_rect_inflate(&r, -1);
  268. rtgui_dc_draw_shaded_rect(dc, &r, light_grey, dark_grey);
  269. break;
  270. case RTGUI_BORDER_SUNKEN:
  271. rtgui_dc_draw_shaded_rect(dc, &r, dark_grey, high_light);
  272. rtgui_rect_inflate(&r, -1);
  273. rtgui_dc_draw_shaded_rect(dc, &r, black, light_grey);
  274. break;
  275. case RTGUI_BORDER_BOX:
  276. rtgui_dc_draw_shaded_rect(dc, &r, dark_grey, high_light);
  277. rtgui_rect_inflate(&r, -1);
  278. rtgui_dc_draw_shaded_rect(dc, &r, high_light, dark_grey);
  279. break;
  280. case RTGUI_BORDER_STATIC:
  281. rtgui_dc_draw_shaded_rect(dc, &r, dark_grey, high_light);
  282. break;
  283. case RTGUI_BORDER_EXTRA:
  284. RTGUI_DC_FC(dc) = light_grey;
  285. rtgui_dc_draw_rect(dc, &r);
  286. break;
  287. case RTGUI_BORDER_SIMPLE:
  288. RTGUI_DC_FC(dc) = black;
  289. rtgui_dc_draw_rect(dc, &r);
  290. break;
  291. default:
  292. break;
  293. }
  294. /* restore color */
  295. RTGUI_DC_FC(dc) = color;
  296. }
  297. void rtgui_dc_draw_horizontal_line(struct rtgui_dc* dc, int x1, int x2, int y)
  298. {
  299. rtgui_color_t color;
  300. if (dc == RT_NULL) return ;
  301. /* save old color */
  302. color = RTGUI_DC_FC(dc);
  303. RTGUI_DC_FC(dc) = dark_grey;
  304. rtgui_dc_draw_hline(dc, x1, x2, y);
  305. y ++;
  306. RTGUI_DC_FC(dc) = high_light;
  307. rtgui_dc_draw_hline(dc, x1, x2, y);
  308. /* restore color */
  309. RTGUI_DC_FC(dc) = color;
  310. }
  311. void rtgui_dc_draw_vertical_line(struct rtgui_dc* dc, int x, int y1, int y2)
  312. {
  313. rtgui_color_t color;
  314. if (dc == RT_NULL) return ;
  315. /* save old color */
  316. color = RTGUI_DC_FC(dc);
  317. RTGUI_DC_FC(dc) = dark_grey;
  318. rtgui_dc_draw_hline(dc, x, y1, y2);
  319. x ++;
  320. RTGUI_DC_FC(dc) = high_light;
  321. rtgui_dc_draw_hline(dc, x, y1, y2);
  322. /* restore color */
  323. RTGUI_DC_FC(dc) = color;
  324. }
  325. void rtgui_dc_draw_polygon(struct rtgui_dc* dc, const int *vx, const int *vy, int count)
  326. {
  327. int i;
  328. const int *x1, *y1, *x2, *y2;
  329. /*
  330. * Sanity check
  331. */
  332. if (count < 3) return;
  333. /*
  334. * Pointer setup
  335. */
  336. x1 = x2 = vx;
  337. y1 = y2 = vy;
  338. x2++;
  339. y2++;
  340. /*
  341. * Draw
  342. */
  343. for (i = 1; i < count; i++)
  344. {
  345. rtgui_dc_draw_line(dc, *x1, *y1, *x2, *y2);
  346. x1 = x2;
  347. y1 = y2;
  348. x2++;
  349. y2++;
  350. }
  351. rtgui_dc_draw_line(dc, *x1, *y1, *vx, *vy);
  352. }
  353. static int _int_compare(const void *a, const void *b)
  354. {
  355. return (*(const int *) a) - (*(const int *) b);
  356. }
  357. void rtgui_dc_fill_polygon(struct rtgui_dc* dc, const int* vx, const int* vy, int count)
  358. {
  359. int i;
  360. int y, xa, xb;
  361. int miny, maxy;
  362. int x1, y1;
  363. int x2, y2;
  364. int ind1, ind2;
  365. int ints;
  366. int *poly_ints = RT_NULL;
  367. /*
  368. * Sanity check number of edges
  369. */
  370. if (count < 3) return;
  371. /*
  372. * Allocate temp array, only grow array
  373. */
  374. poly_ints = (int *) rt_malloc(sizeof(int) * count);
  375. if (poly_ints == RT_NULL) return ; /* no memory, failed */
  376. /*
  377. * Determine Y maximal
  378. */
  379. miny = vy[0];
  380. maxy = vy[0];
  381. for (i = 1; (i < count); i++)
  382. {
  383. if (vy[i] < miny) miny = vy[i];
  384. else if (vy[i] > maxy) maxy = vy[i];
  385. }
  386. /*
  387. * Draw, scanning y
  388. */
  389. for (y = miny; (y <= maxy); y++) {
  390. ints = 0;
  391. for (i = 0; (i < count); i++) {
  392. if (!i) {
  393. ind1 = count - 1;
  394. ind2 = 0;
  395. } else {
  396. ind1 = i - 1;
  397. ind2 = i;
  398. }
  399. y1 = vy[ind1];
  400. y2 = vy[ind2];
  401. if (y1 < y2) {
  402. x1 = vx[ind1];
  403. x2 = vx[ind2];
  404. } else if (y1 > y2) {
  405. y2 = vy[ind1];
  406. y1 = vy[ind2];
  407. x2 = vx[ind1];
  408. x1 = vx[ind2];
  409. } else {
  410. continue;
  411. }
  412. if ( ((y >= y1) && (y < y2)) || ((y == maxy) && (y > y1) && (y <= y2)) )
  413. {
  414. poly_ints[ints++] = ((65536 * (y - y1)) / (y2 - y1)) * (x2 - x1) + (65536 * x1);
  415. }
  416. }
  417. qsort(poly_ints, ints, sizeof(int), _int_compare);
  418. for (i = 0; (i < ints); i += 2)
  419. {
  420. xa = poly_ints[i] + 1;
  421. xa = (xa >> 16) + ((xa & 32768) >> 15);
  422. xb = poly_ints[i+1] - 1;
  423. xb = (xb >> 16) + ((xb & 32768) >> 15);
  424. rtgui_dc_draw_hline(dc, xa, xb, y);
  425. }
  426. }
  427. }
  428. void rtgui_dc_draw_circle(struct rtgui_dc* dc, int x, int y, int r)
  429. {
  430. rt_int16_t cx = 0;
  431. rt_int16_t cy = r;
  432. rt_int16_t df = 1 - r;
  433. rt_int16_t d_e = 3;
  434. rt_int16_t d_se = -2 * r + 5;
  435. rt_int16_t xpcx, xmcx, xpcy, xmcy;
  436. rt_int16_t ypcy, ymcy, ypcx, ymcx;
  437. /*
  438. * sanity check radius
  439. */
  440. if (r < 0) return ;
  441. /* special case for r=0 - draw a point */
  442. if (r == 0) rtgui_dc_draw_point(dc, x, y);
  443. /*
  444. * draw circle
  445. */
  446. do
  447. {
  448. ypcy = y + cy;
  449. ymcy = y - cy;
  450. if (cx > 0)
  451. {
  452. xpcx = x + cx;
  453. xmcx = x - cx;
  454. rtgui_dc_draw_point(dc, xmcx, ypcy);
  455. rtgui_dc_draw_point(dc, xpcx, ypcy);
  456. rtgui_dc_draw_point(dc, xmcx, ymcy);
  457. rtgui_dc_draw_point(dc, xpcx, ymcy);
  458. }
  459. else
  460. {
  461. rtgui_dc_draw_point(dc, x, ymcy);
  462. rtgui_dc_draw_point(dc, x, ypcy);
  463. }
  464. xpcy = x + cy;
  465. xmcy = x - cy;
  466. if ((cx > 0) && (cx != cy))
  467. {
  468. ypcx = y + cx;
  469. ymcx = y - cx;
  470. rtgui_dc_draw_point(dc, xmcy, ypcx);
  471. rtgui_dc_draw_point(dc, xpcy, ypcx);
  472. rtgui_dc_draw_point(dc, xmcy, ymcx);
  473. rtgui_dc_draw_point(dc, xpcy, ymcx);
  474. }
  475. else if (cx == 0)
  476. {
  477. rtgui_dc_draw_point(dc, xmcy, y);
  478. rtgui_dc_draw_point(dc, xpcy, y);
  479. }
  480. /*
  481. * Update
  482. */
  483. if (df < 0)
  484. {
  485. df += d_e;
  486. d_e += 2;
  487. d_se += 2;
  488. }
  489. else
  490. {
  491. df += d_se;
  492. d_e += 2;
  493. d_se += 4;
  494. cy--;
  495. }
  496. cx++;
  497. }while (cx <= cy);
  498. }
  499. void rtgui_dc_fill_circle(struct rtgui_dc* dc, rt_int16_t x, rt_int16_t y, rt_int16_t r)
  500. {
  501. rt_int16_t cx = 0;
  502. rt_int16_t cy = r;
  503. rt_int16_t ocx = (rt_int16_t) 0xffff;
  504. rt_int16_t ocy = (rt_int16_t) 0xffff;
  505. rt_int16_t df = 1 - r;
  506. rt_int16_t d_e = 3;
  507. rt_int16_t d_se = -2 * r + 5;
  508. rt_int16_t xpcx, xmcx, xpcy, xmcy;
  509. rt_int16_t ypcy, ymcy, ypcx, ymcx;
  510. /*
  511. * Sanity check radius
  512. */
  513. if (r < 0) return;
  514. /*
  515. * Special case for r=0 - draw a point
  516. */
  517. if (r == 0)
  518. {
  519. rtgui_dc_draw_point(dc, x, y);
  520. return ;
  521. }
  522. /*
  523. * Draw
  524. */
  525. do {
  526. xpcx = x + cx;
  527. xmcx = x - cx;
  528. xpcy = x + cy;
  529. xmcy = x - cy;
  530. if (ocy != cy) {
  531. if (cy > 0) {
  532. ypcy = y + cy;
  533. ymcy = y - cy;
  534. rtgui_dc_draw_hline(dc, xmcx, xpcx, ypcy);
  535. rtgui_dc_draw_hline(dc, xmcx, xpcx, ymcy);
  536. } else {
  537. rtgui_dc_draw_hline(dc, xmcx, xpcx, y);
  538. }
  539. ocy = cy;
  540. }
  541. if (ocx != cx) {
  542. if (cx != cy) {
  543. if (cx > 0) {
  544. ypcx = y + cx;
  545. ymcx = y - cx;
  546. rtgui_dc_draw_hline(dc, xmcy, xpcy, ymcx);
  547. rtgui_dc_draw_hline(dc, xmcy, xpcy, ypcx);
  548. } else {
  549. rtgui_dc_draw_hline(dc, xmcy, xpcy, y);
  550. }
  551. }
  552. ocx = cx;
  553. }
  554. /*
  555. * Update
  556. */
  557. if (df < 0) {
  558. df += d_e;
  559. d_e += 2;
  560. d_se += 2;
  561. } else {
  562. df += d_se;
  563. d_e += 2;
  564. d_se += 4;
  565. cy--;
  566. }
  567. cx++;
  568. } while (cx <= cy);
  569. }
  570. void rtgui_dc_draw_arc(struct rtgui_dc *dc, rt_int16_t x, rt_int16_t y, rt_int16_t r, rt_int16_t start, rt_int16_t end)
  571. {
  572. rt_int16_t cx = 0;
  573. rt_int16_t cy = r;
  574. rt_int16_t df = 1 - r;
  575. rt_int16_t d_e = 3;
  576. rt_int16_t d_se = -2 * r + 5;
  577. rt_int16_t xpcx, xmcx, xpcy, xmcy;
  578. rt_int16_t ypcy, ymcy, ypcx, ymcx;
  579. rt_uint8_t drawoct;
  580. int startoct, endoct, oct, stopval_start, stopval_end;
  581. double temp;
  582. stopval_start = 0;
  583. stopval_end = 0;
  584. temp = 0;
  585. /* Sanity check radius */
  586. if (r < 0) return ;
  587. /* Special case for r=0 - draw a point */
  588. if (r == 0)
  589. {
  590. rtgui_dc_draw_point(dc, x, y);
  591. return;
  592. }
  593. /*
  594. * Draw arc
  595. */
  596. // Octant labelling
  597. //
  598. // \ 5 | 6 /
  599. // \ | /
  600. // 4 \ | / 7
  601. // \|/
  602. //------+------ +x
  603. // /|\
  604. // 3 / | \ 0
  605. // / | \
  606. // / 2 | 1 \
  607. // +y
  608. drawoct = 0; // 0x00000000
  609. // whether or not to keep drawing a given octant.
  610. // For example: 0x00111100 means we're drawing in octants 2-5
  611. // 0 <= start & end < 360; note that sometimes start > end - if so, arc goes back through 0.
  612. while (start < 0) start += 360;
  613. while (end < 0) end += 360;
  614. /* Fixup angles */
  615. start = start % 360;
  616. end = end % 360;
  617. // now, we find which octants we're drawing in.
  618. startoct = start / 45;
  619. endoct = end / 45;
  620. oct = startoct - 1; // we increment as first step in loop
  621. //stopval_start, stopval_end; // what values of cx to stop at.
  622. do {
  623. oct = (oct + 1) % 8;
  624. if (oct == startoct)
  625. {
  626. // need to compute stopval_start for this octant. Look at picture above if this is unclear
  627. switch (oct)
  628. {
  629. case 0:
  630. case 3:
  631. temp = sin(start * M_PI / 180);
  632. break;
  633. case 1:
  634. case 6:
  635. temp = cos(start * M_PI / 180);
  636. break;
  637. case 2:
  638. case 5:
  639. temp = -cos(start * M_PI / 180);
  640. break;
  641. case 4:
  642. case 7:
  643. temp = -sin(start * M_PI / 180);
  644. break;
  645. }
  646. temp *= r;
  647. stopval_start = (int)temp; // always round down.
  648. // This isn't arbitrary, but requires graph paper to explain well.
  649. // The basic idea is that we're always changing drawoct after we draw, so we
  650. // stop immediately after we render the last sensible pixel at x = ((int)temp).
  651. // and whether to draw in this octant initially
  652. if (oct % 2) drawoct |= (1 << oct); // this is basically like saying drawoct[oct] = true, if drawoct were a bool array
  653. else drawoct &= 255 - (1 << oct); // this is basically like saying drawoct[oct] = false
  654. }
  655. if (oct == endoct)
  656. {
  657. // need to compute stopval_end for this octant
  658. switch (oct)
  659. {
  660. case 0:
  661. case 3:
  662. temp = sin(end * M_PI / 180);
  663. break;
  664. case 1:
  665. case 6:
  666. temp = cos(end * M_PI / 180);
  667. break;
  668. case 2:
  669. case 5:
  670. temp = -cos(end * M_PI / 180);
  671. break;
  672. case 4:
  673. case 7:
  674. temp = -sin(end * M_PI / 180);
  675. break;
  676. }
  677. temp *= r;
  678. stopval_end = (int)temp;
  679. // and whether to draw in this octant initially
  680. if (startoct == endoct)
  681. {
  682. // note: we start drawing, stop, then start again in this case
  683. // otherwise: we only draw in this octant, so initialize it to false, it will get set back to true
  684. if (start > end)
  685. {
  686. // unfortunately, if we're in the same octant and need to draw over the whole circle,
  687. // we need to set the rest to true, because the while loop will end at the bottom.
  688. drawoct = 255;
  689. }
  690. else
  691. {
  692. drawoct &= 255 - (1 << oct);
  693. }
  694. }
  695. else if (oct % 2) drawoct &= 255 - (1 << oct);
  696. else drawoct |= (1 << oct);
  697. } else if (oct != startoct) { // already verified that it's != endoct
  698. drawoct |= (1 << oct); // draw this entire segment
  699. }
  700. } while (oct != endoct);
  701. // so now we have what octants to draw and when to draw them. all that's left is the actual raster code.
  702. do
  703. {
  704. ypcy = y + cy;
  705. ymcy = y - cy;
  706. if (cx > 0)
  707. {
  708. xpcx = x + cx;
  709. xmcx = x - cx;
  710. // always check if we're drawing a certain octant before adding a pixel to that octant.
  711. if (drawoct & 4) rtgui_dc_draw_point(dc, xmcx, ypcy); // drawoct & 4 = 22; drawoct[2]
  712. if (drawoct & 2) rtgui_dc_draw_point(dc, xpcx, ypcy);
  713. if (drawoct & 32) rtgui_dc_draw_point(dc, xmcx, ymcy);
  714. if (drawoct & 64) rtgui_dc_draw_point(dc, xpcx, ymcy);
  715. }
  716. else
  717. {
  718. if (drawoct & 6) rtgui_dc_draw_point(dc, x, ypcy); // 4 + 2; drawoct[2] || drawoct[1]
  719. if (drawoct & 96) rtgui_dc_draw_point(dc, x, ymcy); // 32 + 64
  720. }
  721. xpcy = x + cy;
  722. xmcy = x - cy;
  723. if (cx > 0 && cx != cy)
  724. {
  725. ypcx = y + cx;
  726. ymcx = y - cx;
  727. if (drawoct & 8) rtgui_dc_draw_point(dc, xmcy, ypcx);
  728. if (drawoct & 1) rtgui_dc_draw_point(dc, xpcy, ypcx);
  729. if (drawoct & 16) rtgui_dc_draw_point(dc, xmcy, ymcx);
  730. if (drawoct & 128) rtgui_dc_draw_point(dc, xpcy, ymcx);
  731. }
  732. else if (cx == 0)
  733. {
  734. if (drawoct & 24) rtgui_dc_draw_point(dc, xmcy, y); // 8 + 16
  735. if (drawoct & 129) rtgui_dc_draw_point(dc, xpcy, y); // 1 + 128
  736. }
  737. /*
  738. * Update whether we're drawing an octant
  739. */
  740. if (stopval_start == cx)
  741. {
  742. // works like an on-off switch because start & end may be in the same octant.
  743. if (drawoct & (1 << startoct)) drawoct &= 255 - (1 << startoct);
  744. else drawoct |= (1 << startoct);
  745. }
  746. if (stopval_end == cx)
  747. {
  748. if (drawoct & (1 << endoct)) drawoct &= 255 - (1 << endoct);
  749. else drawoct |= (1 << endoct);
  750. }
  751. /*
  752. * Update pixels
  753. */
  754. if (df < 0)
  755. {
  756. df += d_e;
  757. d_e += 2;
  758. d_se += 2;
  759. }
  760. else
  761. {
  762. df += d_se;
  763. d_e += 2;
  764. d_se += 4;
  765. cy--;
  766. }
  767. cx++;
  768. } while (cx <= cy);
  769. }
  770. void rtgui_dc_draw_annulus(struct rtgui_dc *dc, rt_int16_t x, rt_int16_t y, rt_int16_t r1, rt_int16_t r2, rt_int16_t start, rt_int16_t end)
  771. {
  772. rt_int16_t start_x, start_y;
  773. rt_int16_t end_x, end_y;
  774. double temp;
  775. rt_int16_t temp_val = 0;
  776. /* Sanity check radius */
  777. if ((r1 < 0) || (r1 < 0)) return ;
  778. /* Special case for r=0 - draw a point */
  779. if ((r1 == 0) && (r2 == 0))
  780. {
  781. rtgui_dc_draw_point(dc, x, y);
  782. return;
  783. }
  784. while (start < 0) start += 360;
  785. while (end < 0) end += 360;
  786. rtgui_dc_draw_arc(dc, x, y, r1, start, end);
  787. rtgui_dc_draw_arc(dc, x, y, r2, start, end);
  788. temp = cos(start * M_PI / 180);
  789. temp_val = (int)(temp * r1);
  790. start_x = x + temp_val;
  791. temp_val = (int)(temp * r2);
  792. end_x = x + temp_val;
  793. temp = sin(start * M_PI / 180);
  794. temp_val = (int)(temp * r1);
  795. start_y = y + temp_val;
  796. temp_val = (int)(temp * r2);
  797. end_y = y + temp_val;
  798. rtgui_dc_draw_line(dc, start_x, start_y, end_x, end_y);
  799. temp = cos(end * M_PI / 180);
  800. temp_val = (int)(temp * r1);
  801. start_x = x + temp_val;
  802. temp_val = (int)(temp * r2);
  803. end_x = x + temp_val;
  804. temp = sin(end * M_PI / 180);
  805. temp_val = (int)(temp * r1);
  806. start_y = y + temp_val;
  807. temp_val = (int)(temp * r2);
  808. end_y = y + temp_val;
  809. rtgui_dc_draw_line(dc, start_x, start_y, end_x, end_y);
  810. }
  811. void rtgui_dc_draw_sector(struct rtgui_dc *dc, rt_int16_t x, rt_int16_t y, rt_int16_t r, rt_int16_t start, rt_int16_t end)
  812. {
  813. int start_x, start_y;
  814. int end_x, end_y;
  815. /* Sanity check radius */
  816. if (r < 0) return ;
  817. /* Special case for r=0 - draw a point */
  818. if (r == 0)
  819. {
  820. rtgui_dc_draw_point(dc, x, y);
  821. return;
  822. }
  823. while (start < 0) start += 360;
  824. while (end < 0) end += 360;
  825. /* Fixup angles */
  826. start = start % 360;
  827. end = end % 360;
  828. rtgui_dc_draw_arc(dc, x, y, r, start, end);
  829. start_x = x + r * cos(start * M_PI / 180);
  830. start_y = y + r * sin(start * M_PI / 180);
  831. end_x = x + r * cos(end * M_PI / 180);
  832. end_y = y + r * sin(end * M_PI / 180);
  833. rtgui_dc_draw_line(dc, x, y, start_x, start_y);
  834. rtgui_dc_draw_line(dc, x, y, end_x, end_y);
  835. }
  836. void rtgui_dc_fill_sector(struct rtgui_dc *dc, rt_int16_t x, rt_int16_t y, rt_int16_t r, rt_int16_t start, rt_int16_t end)
  837. {
  838. int start_x, start_y;
  839. int end_x, end_y;
  840. float start_f;
  841. /* Sanity check radius */
  842. if (r < 0) return ;
  843. /* Special case for r=0 - draw a point */
  844. if (r == 0)
  845. {
  846. rtgui_dc_draw_point(dc, x, y);
  847. return;
  848. }
  849. while (start < 0) start += 360;
  850. while (end < 0) end += 360;
  851. /* Fixup angles */
  852. start = start % 360;
  853. end = end % 360;
  854. end_x = x + r * cos(end * M_PI / 180);
  855. end_y = y + r * sin(end * M_PI / 180);
  856. do
  857. {
  858. start_x = x + r * cos(start * M_PI / 180);
  859. start_y = y + r * sin(start * M_PI / 180);
  860. start ++;
  861. rtgui_dc_draw_line(dc, x, y, start_x, start_y);
  862. }while(!((start_x == end_x) && (start_y == end_y)));
  863. }
  864. void rtgui_dc_draw_ellipse(struct rtgui_dc* dc, rt_int16_t x, rt_int16_t y, rt_int16_t rx, rt_int16_t ry)
  865. {
  866. int ix, iy;
  867. int h, i, j, k;
  868. int oh, oi, oj, ok;
  869. int xmh, xph, ypk, ymk;
  870. int xmi, xpi, ymj, ypj;
  871. int xmj, xpj, ymi, ypi;
  872. int xmk, xpk, ymh, yph;
  873. /*
  874. * Sanity check radii
  875. */
  876. if ((rx < 0) || (ry < 0)) return;
  877. /*
  878. * Special case for rx=0 - draw a vline
  879. */
  880. if (rx == 0)
  881. {
  882. rtgui_dc_draw_vline(dc, x, y - ry, y + ry);
  883. return;
  884. }
  885. /*
  886. * Special case for ry=0 - draw a hline
  887. */
  888. if (ry == 0)
  889. {
  890. rtgui_dc_draw_hline(dc, x - rx, x + rx, y);
  891. return;
  892. }
  893. /*
  894. * Init vars
  895. */
  896. oh = oi = oj = ok = 0xFFFF;
  897. if (rx > ry)
  898. {
  899. ix = 0;
  900. iy = rx * 64;
  901. do
  902. {
  903. h = (ix + 32) >> 6;
  904. i = (iy + 32) >> 6;
  905. j = (h * ry) / rx;
  906. k = (i * ry) / rx;
  907. if (((ok != k) && (oj != k)) || ((oj != j) && (ok != j)) || (k != j))
  908. {
  909. xph = x + h;
  910. xmh = x - h;
  911. if (k > 0)
  912. {
  913. ypk = y + k;
  914. ymk = y - k;
  915. rtgui_dc_draw_point(dc, xmh, ypk);
  916. rtgui_dc_draw_point(dc, xph, ypk);
  917. rtgui_dc_draw_point(dc, xmh, ymk);
  918. rtgui_dc_draw_point(dc, xph, ymk);
  919. }
  920. else
  921. {
  922. rtgui_dc_draw_point(dc, xmh, y);
  923. rtgui_dc_draw_point(dc, xph, y);
  924. }
  925. ok = k;
  926. xpi = x + i;
  927. xmi = x - i;
  928. if (j > 0)
  929. {
  930. ypj = y + j;
  931. ymj = y - j;
  932. rtgui_dc_draw_point(dc, xmi, ypj);
  933. rtgui_dc_draw_point(dc, xpi, ypj);
  934. rtgui_dc_draw_point(dc, xmi, ymj);
  935. rtgui_dc_draw_point(dc, xpi, ymj);
  936. }
  937. else
  938. {
  939. rtgui_dc_draw_point(dc, xmi, y);
  940. rtgui_dc_draw_point(dc, xpi, y);
  941. }
  942. oj = j;
  943. }
  944. ix = ix + iy / rx;
  945. iy = iy - ix / rx;
  946. } while (i > h);
  947. }
  948. else
  949. {
  950. ix = 0;
  951. iy = ry * 64;
  952. do
  953. {
  954. h = (ix + 32) >> 6;
  955. i = (iy + 32) >> 6;
  956. j = (h * rx) / ry;
  957. k = (i * rx) / ry;
  958. if (((oi != i) && (oh != i)) || ((oh != h) && (oi != h) && (i != h)))
  959. {
  960. xmj = x - j;
  961. xpj = x + j;
  962. if (i > 0)
  963. {
  964. ypi = y + i;
  965. ymi = y - i;
  966. rtgui_dc_draw_point(dc, xmj, ypi);
  967. rtgui_dc_draw_point(dc, xpj, ypi);
  968. rtgui_dc_draw_point(dc, xmj, ymi);
  969. rtgui_dc_draw_point(dc, xpj, ymi);
  970. }
  971. else
  972. {
  973. rtgui_dc_draw_point(dc, xmj, y);
  974. rtgui_dc_draw_point(dc, xpj, y);
  975. }
  976. oi = i;
  977. xmk = x - k;
  978. xpk = x + k;
  979. if (h > 0)
  980. {
  981. yph = y + h;
  982. ymh = y - h;
  983. rtgui_dc_draw_point(dc, xmk, yph);
  984. rtgui_dc_draw_point(dc, xpk, yph);
  985. rtgui_dc_draw_point(dc, xmk, ymh);
  986. rtgui_dc_draw_point(dc, xpk, ymh);
  987. }
  988. else
  989. {
  990. rtgui_dc_draw_point(dc, xmk, y);
  991. rtgui_dc_draw_point(dc, xpk, y);
  992. }
  993. oh = h;
  994. }
  995. ix = ix + iy / ry;
  996. iy = iy - ix / ry;
  997. } while (i > h);
  998. }
  999. }
  1000. void rtgui_dc_fill_ellipse(struct rtgui_dc *dc, rt_int16_t x, rt_int16_t y, rt_int16_t rx, rt_int16_t ry)
  1001. {
  1002. int ix, iy;
  1003. int h, i, j, k;
  1004. int oh, oi, oj, ok;
  1005. int xmh, xph;
  1006. int xmi, xpi;
  1007. int xmj, xpj;
  1008. int xmk, xpk;
  1009. /*
  1010. * Special case for rx=0 - draw a vline
  1011. */
  1012. if (rx == 0)
  1013. {
  1014. rtgui_dc_draw_vline(dc, x, y - ry, y + ry);
  1015. return;
  1016. }
  1017. /* special case for ry=0 - draw a hline */
  1018. if (ry == 0) {
  1019. rtgui_dc_draw_hline(dc, x - rx, x + rx, y);
  1020. return;
  1021. }
  1022. /*
  1023. * Init vars
  1024. */
  1025. oh = oi = oj = ok = 0xFFFF;
  1026. /*
  1027. * Draw
  1028. */
  1029. if (rx > ry) {
  1030. ix = 0;
  1031. iy = rx * 64;
  1032. do {
  1033. h = (ix + 32) >> 6;
  1034. i = (iy + 32) >> 6;
  1035. j = (h * ry) / rx;
  1036. k = (i * ry) / rx;
  1037. if ((ok != k) && (oj != k)) {
  1038. xph = x + h;
  1039. xmh = x - h;
  1040. if (k > 0) {
  1041. rtgui_dc_draw_hline(dc, xmh, xph, y + k);
  1042. rtgui_dc_draw_hline(dc, xmh, xph, y - k);
  1043. } else {
  1044. rtgui_dc_draw_hline(dc, xmh, xph, y);
  1045. }
  1046. ok = k;
  1047. }
  1048. if ((oj != j) && (ok != j) && (k != j)) {
  1049. xmi = x - i;
  1050. xpi = x + i;
  1051. if (j > 0) {
  1052. rtgui_dc_draw_hline(dc, xmi, xpi, y + j);
  1053. rtgui_dc_draw_hline(dc, xmi, xpi, y - j);
  1054. } else {
  1055. rtgui_dc_draw_hline(dc, xmi, xpi, y);
  1056. }
  1057. oj = j;
  1058. }
  1059. ix = ix + iy / rx;
  1060. iy = iy - ix / rx;
  1061. } while (i > h);
  1062. } else {
  1063. ix = 0;
  1064. iy = ry * 64;
  1065. do {
  1066. h = (ix + 32) >> 6;
  1067. i = (iy + 32) >> 6;
  1068. j = (h * rx) / ry;
  1069. k = (i * rx) / ry;
  1070. if ((oi != i) && (oh != i)) {
  1071. xmj = x - j;
  1072. xpj = x + j;
  1073. if (i > 0) {
  1074. rtgui_dc_draw_hline(dc, xmj, xpj, y + i);
  1075. rtgui_dc_draw_hline(dc, xmj, xpj, y - i);
  1076. } else {
  1077. rtgui_dc_draw_hline(dc, xmj, xpj, y);
  1078. }
  1079. oi = i;
  1080. }
  1081. if ((oh != h) && (oi != h) && (i != h)) {
  1082. xmk = x - k;
  1083. xpk = x + k;
  1084. if (h > 0) {
  1085. rtgui_dc_draw_hline(dc, xmk, xpk, y + h);
  1086. rtgui_dc_draw_hline(dc, xmk, xpk, y - h);
  1087. } else {
  1088. rtgui_dc_draw_hline(dc, xmk, xpk, y);
  1089. }
  1090. oh = h;
  1091. }
  1092. ix = ix + iy / ry;
  1093. iy = iy - ix / ry;
  1094. } while (i > h);
  1095. }
  1096. }
  1097. void rtgui_dc_draw_focus_rect(struct rtgui_dc* dc, rtgui_rect_t* rect)
  1098. {
  1099. int i;
  1100. for (i = rect->x1; i <= rect->x2; i += 2)
  1101. {
  1102. rtgui_dc_draw_point(dc, i, rect->y1);
  1103. rtgui_dc_draw_point(dc, i, rect->y2);
  1104. }
  1105. for (i = rect->y1; i <= rect->y2; i += 2)
  1106. {
  1107. rtgui_dc_draw_point(dc, rect->x1, i);
  1108. rtgui_dc_draw_point(dc, rect->x2, i);
  1109. }
  1110. }