dc.c 27 KB

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