dc.c 24 KB

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