dc.c 27 KB

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