dc.c 25 KB

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