dc.c 25 KB

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