dc.c 26 KB

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