dc.c 24 KB

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