dc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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 char* 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. #if 1
  537. void rtgui_dc_draw_circle(struct rtgui_dc *dc,
  538. int xCenter, int yCenter, int radius)
  539. {
  540. int x =0;
  541. int y = radius;
  542. int p = 1-radius;
  543. rtgui_dc_draw_point(dc,xCenter+x,yCenter+y);
  544. rtgui_dc_draw_point(dc,xCenter-x,yCenter+y);
  545. rtgui_dc_draw_point(dc,xCenter+x,yCenter-y);
  546. rtgui_dc_draw_point(dc,xCenter-x,yCenter-y);
  547. rtgui_dc_draw_point(dc,xCenter+y,yCenter+x);
  548. rtgui_dc_draw_point(dc,xCenter-y,yCenter+x);
  549. rtgui_dc_draw_point(dc,xCenter+y,yCenter-x);
  550. rtgui_dc_draw_point(dc,xCenter-y,yCenter-x);
  551. while(x<y){
  552. x++;
  553. if(p<0)
  554. p+=2*x+1;
  555. else{
  556. y--;
  557. p+=2*(x-y)+1;
  558. }
  559. rtgui_dc_draw_point(dc,xCenter+x,yCenter+y);
  560. rtgui_dc_draw_point(dc,xCenter-x,yCenter+y);
  561. rtgui_dc_draw_point(dc,xCenter+x,yCenter-y);
  562. rtgui_dc_draw_point(dc,xCenter-x,yCenter-y);
  563. rtgui_dc_draw_point(dc,xCenter+y,yCenter+x);
  564. rtgui_dc_draw_point(dc,xCenter-y,yCenter+x);
  565. rtgui_dc_draw_point(dc,xCenter+y,yCenter-x);
  566. rtgui_dc_draw_point(dc,xCenter-y,yCenter-x);
  567. }
  568. }
  569. #else
  570. void rtgui_dc_draw_circle(struct rtgui_dc* dc, int x, int y, int r)
  571. {
  572. rt_int16_t cx = 0;
  573. rt_int16_t cy = r;
  574. rt_int16_t df = 1 - r;
  575. rt_int16_t d_e = 3;
  576. rt_int16_t d_se = -2 * r + 5;
  577. rt_int16_t xpcx, xmcx, xpcy, xmcy;
  578. rt_int16_t ypcy, ymcy, ypcx, ymcx;
  579. /*
  580. * sanity check radius
  581. */
  582. if (r < 0) return ;
  583. /* special case for r=0 - draw a point */
  584. if (r == 0) rtgui_dc_draw_point(dc, x, y);
  585. /*
  586. * draw circle
  587. */
  588. do
  589. {
  590. ypcy = y + cy;
  591. ymcy = y - cy;
  592. if (cx > 0)
  593. {
  594. xpcx = x + cx;
  595. xmcx = x - cx;
  596. rtgui_dc_draw_point(dc, xmcx, ypcy);
  597. rtgui_dc_draw_point(dc, xpcx, ypcy);
  598. rtgui_dc_draw_point(dc, xmcx, ymcy);
  599. rtgui_dc_draw_point(dc, xpcx, ymcy);
  600. }
  601. else
  602. {
  603. rtgui_dc_draw_point(dc, x, ymcy);
  604. rtgui_dc_draw_point(dc, x, ypcy);
  605. }
  606. xpcy = x + cy;
  607. xmcy = x - cy;
  608. if ((cx > 0) && (cx != cy))
  609. {
  610. ypcx = y + cx;
  611. ymcx = y - cx;
  612. rtgui_dc_draw_point(dc, xmcy, ypcx);
  613. rtgui_dc_draw_point(dc, xpcy, ypcx);
  614. rtgui_dc_draw_point(dc, xmcy, ymcx);
  615. rtgui_dc_draw_point(dc, xpcy, ymcx);
  616. }
  617. else if (cx == 0)
  618. {
  619. rtgui_dc_draw_point(dc, xmcy, y);
  620. rtgui_dc_draw_point(dc, xpcy, y);
  621. }
  622. /*
  623. * Update
  624. */
  625. if (df < 0)
  626. {
  627. df += d_e;
  628. d_e += 2;
  629. d_se += 2;
  630. }
  631. else
  632. {
  633. df += d_se;
  634. d_e += 2;
  635. d_se += 4;
  636. cy--;
  637. }
  638. cx++;
  639. }while (cx <= cy);
  640. }
  641. #endif
  642. void rtgui_dc_fill_circle(struct rtgui_dc* dc, rt_int16_t x, rt_int16_t y, rt_int16_t r)
  643. {
  644. rt_int16_t cx = 0;
  645. rt_int16_t cy = r;
  646. rt_int16_t ocx = (rt_int16_t) 0xffff;
  647. rt_int16_t ocy = (rt_int16_t) 0xffff;
  648. rt_int16_t df = 1 - r;
  649. rt_int16_t d_e = 3;
  650. rt_int16_t d_se = -2 * r + 5;
  651. rt_int16_t xpcx, xmcx, xpcy, xmcy;
  652. rt_int16_t ypcy, ymcy, ypcx, ymcx;
  653. /*
  654. * Sanity check radius
  655. */
  656. if (r < 0) return;
  657. /*
  658. * Special case for r=0 - draw a point
  659. */
  660. if (r == 0)
  661. {
  662. rtgui_dc_draw_point(dc, x, y);
  663. return ;
  664. }
  665. /*
  666. * Draw
  667. */
  668. do {
  669. xpcx = x + cx;
  670. xmcx = x - cx;
  671. xpcy = x + cy;
  672. xmcy = x - cy;
  673. if (ocy != cy) {
  674. if (cy > 0) {
  675. ypcy = y + cy;
  676. ymcy = y - cy;
  677. rtgui_dc_draw_hline(dc, xmcx, xpcx, ypcy);
  678. rtgui_dc_draw_hline(dc, xmcx, xpcx, ymcy);
  679. } else {
  680. rtgui_dc_draw_hline(dc, xmcx, xpcx, y);
  681. }
  682. ocy = cy;
  683. }
  684. if (ocx != cx) {
  685. if (cx != cy) {
  686. if (cx > 0) {
  687. ypcx = y + cx;
  688. ymcx = y - cx;
  689. rtgui_dc_draw_hline(dc, xmcy, xpcy, ymcx);
  690. rtgui_dc_draw_hline(dc, xmcy, xpcy, ypcx);
  691. } else {
  692. rtgui_dc_draw_hline(dc, xmcy, xpcy, y);
  693. }
  694. }
  695. ocx = cx;
  696. }
  697. /*
  698. * Update
  699. */
  700. if (df < 0) {
  701. df += d_e;
  702. d_e += 2;
  703. d_se += 2;
  704. } else {
  705. df += d_se;
  706. d_e += 2;
  707. d_se += 4;
  708. cy--;
  709. }
  710. cx++;
  711. } while (cx <= cy);
  712. }
  713. 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)
  714. {
  715. rt_int16_t cx = 0;
  716. rt_int16_t cy = r;
  717. rt_int16_t df = 1 - r;
  718. rt_int16_t d_e = 3;
  719. rt_int16_t d_se = -2 * r + 5;
  720. rt_int16_t xpcx, xmcx, xpcy, xmcy;
  721. rt_int16_t ypcy, ymcy, ypcx, ymcx;
  722. rt_uint8_t drawoct;
  723. int startoct, endoct, oct, stopval_start, stopval_end;
  724. double temp;
  725. stopval_start = 0;
  726. stopval_end = 0;
  727. temp = 0;
  728. /* Sanity check radius */
  729. if (r < 0) return ;
  730. /* Special case for r=0 - draw a point */
  731. if (r == 0)
  732. {
  733. rtgui_dc_draw_point(dc, x, y);
  734. return;
  735. }
  736. /* Fixup angles */
  737. start = start % 360;
  738. end = end % 360;
  739. /*
  740. * Draw arc
  741. */
  742. // Octant labelling
  743. //
  744. // \ 5 | 6 /
  745. // \ | /
  746. // 4 \ | / 7
  747. // \|/
  748. //------+------ +x
  749. // /|\
  750. // 3 / | \ 0
  751. // / | \
  752. // / 2 | 1 \
  753. // +y
  754. drawoct = 0; // 0x00000000
  755. // whether or not to keep drawing a given octant.
  756. // For example: 0x00111100 means we're drawing in octants 2-5
  757. // 0 <= start & end < 360; note that sometimes start > end - if so, arc goes back through 0.
  758. while (start < 0) start += 360;
  759. while (end < 0) end += 360;
  760. start %= 360;
  761. end %= 360;
  762. // now, we find which octants we're drawing in.
  763. startoct = start / 45;
  764. endoct = end / 45;
  765. oct = startoct - 1; // we increment as first step in loop
  766. //stopval_start, stopval_end; // what values of cx to stop at.
  767. do {
  768. oct = (oct + 1) % 8;
  769. if (oct == startoct)
  770. {
  771. // need to compute stopval_start for this octant. Look at picture above if this is unclear
  772. switch (oct)
  773. {
  774. case 0:
  775. case 3:
  776. temp = sin(start * M_PI / 180);
  777. break;
  778. case 1:
  779. case 6:
  780. temp = cos(start * M_PI / 180);
  781. break;
  782. case 2:
  783. case 5:
  784. temp = -cos(start * M_PI / 180);
  785. break;
  786. case 4:
  787. case 7:
  788. temp = -sin(start * M_PI / 180);
  789. break;
  790. }
  791. temp *= r;
  792. stopval_start = (int)temp; // always round down.
  793. // This isn't arbitrary, but requires graph paper to explain well.
  794. // The basic idea is that we're always changing drawoct after we draw, so we
  795. // stop immediately after we render the last sensible pixel at x = ((int)temp).
  796. // and whether to draw in this octant initially
  797. if (oct % 2) drawoct |= (1 << oct); // this is basically like saying drawoct[oct] = true, if drawoct were a bool array
  798. else drawoct &= 255 - (1 << oct); // this is basically like saying drawoct[oct] = false
  799. }
  800. if (oct == endoct)
  801. {
  802. // need to compute stopval_end for this octant
  803. switch (oct)
  804. {
  805. case 0:
  806. case 3:
  807. temp = sin(end * M_PI / 180);
  808. break;
  809. case 1:
  810. case 6:
  811. temp = cos(end * M_PI / 180);
  812. break;
  813. case 2:
  814. case 5:
  815. temp = -cos(end * M_PI / 180);
  816. break;
  817. case 4:
  818. case 7:
  819. temp = -sin(end * M_PI / 180);
  820. break;
  821. }
  822. temp *= r;
  823. stopval_end = (int)temp;
  824. // and whether to draw in this octant initially
  825. if (startoct == endoct)
  826. {
  827. // note: we start drawing, stop, then start again in this case
  828. // otherwise: we only draw in this octant, so initialize it to false, it will get set back to true
  829. if (start > end)
  830. {
  831. // unfortunately, if we're in the same octant and need to draw over the whole circle,
  832. // we need to set the rest to true, because the while loop will end at the bottom.
  833. drawoct = 255;
  834. }
  835. else
  836. {
  837. drawoct &= 255 - (1 << oct);
  838. }
  839. }
  840. else if (oct % 2) drawoct &= 255 - (1 << oct);
  841. else drawoct |= (1 << oct);
  842. } else if (oct != startoct) { // already verified that it's != endoct
  843. drawoct |= (1 << oct); // draw this entire segment
  844. }
  845. } while (oct != endoct);
  846. // so now we have what octants to draw and when to draw them. all that's left is the actual raster code.
  847. do
  848. {
  849. ypcy = y + cy;
  850. ymcy = y - cy;
  851. if (cx > 0)
  852. {
  853. xpcx = x + cx;
  854. xmcx = x - cx;
  855. // always check if we're drawing a certain octant before adding a pixel to that octant.
  856. if (drawoct & 4) rtgui_dc_draw_point(dc, xmcx, ypcy); // drawoct & 4 = 22; drawoct[2]
  857. if (drawoct & 2) rtgui_dc_draw_point(dc, xpcx, ypcy);
  858. if (drawoct & 32) rtgui_dc_draw_point(dc, xmcx, ymcy);
  859. if (drawoct & 64) rtgui_dc_draw_point(dc, xpcx, ymcy);
  860. }
  861. else
  862. {
  863. if (drawoct & 6) rtgui_dc_draw_point(dc, x, ypcy); // 4 + 2; drawoct[2] || drawoct[1]
  864. if (drawoct & 96) rtgui_dc_draw_point(dc, x, ymcy); // 32 + 64
  865. }
  866. xpcy = x + cy;
  867. xmcy = x - cy;
  868. if (cx > 0 && cx != cy)
  869. {
  870. ypcx = y + cx;
  871. ymcx = y - cx;
  872. if (drawoct & 8) rtgui_dc_draw_point(dc, xmcy, ypcx);
  873. if (drawoct & 1) rtgui_dc_draw_point(dc, xpcy, ypcx);
  874. if (drawoct & 16) rtgui_dc_draw_point(dc, xmcy, ymcx);
  875. if (drawoct & 128) rtgui_dc_draw_point(dc, xpcy, ymcx);
  876. }
  877. else if (cx == 0)
  878. {
  879. if (drawoct & 24) rtgui_dc_draw_point(dc, xmcy, y); // 8 + 16
  880. if (drawoct & 129) rtgui_dc_draw_point(dc, xpcy, y); // 1 + 128
  881. }
  882. /*
  883. * Update whether we're drawing an octant
  884. */
  885. if (stopval_start == cx)
  886. {
  887. // works like an on-off switch because start & end may be in the same octant.
  888. if (drawoct & (1 << startoct)) drawoct &= 255 - (1 << startoct);
  889. else drawoct |= (1 << startoct);
  890. }
  891. if (stopval_end == cx)
  892. {
  893. if (drawoct & (1 << endoct)) drawoct &= 255 - (1 << endoct);
  894. else drawoct |= (1 << endoct);
  895. }
  896. /*
  897. * Update pixels
  898. */
  899. if (df < 0)
  900. {
  901. df += d_e;
  902. d_e += 2;
  903. d_se += 2;
  904. }
  905. else
  906. {
  907. df += d_se;
  908. d_e += 2;
  909. d_se += 4;
  910. cy--;
  911. }
  912. cx++;
  913. } while (cx <= cy);
  914. }
  915. 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)
  916. {
  917. int ix, iy;
  918. int h, i, j, k;
  919. int oh, oi, oj, ok;
  920. int xmh, xph, ypk, ymk;
  921. int xmi, xpi, ymj, ypj;
  922. int xmj, xpj, ymi, ypi;
  923. int xmk, xpk, ymh, yph;
  924. /*
  925. * Sanity check radii
  926. */
  927. if ((rx < 0) || (ry < 0)) return;
  928. /*
  929. * Special case for rx=0 - draw a vline
  930. */
  931. if (rx == 0)
  932. {
  933. rtgui_dc_draw_vline(dc, x, y - ry, y + ry);
  934. return;
  935. }
  936. /*
  937. * Special case for ry=0 - draw a hline
  938. */
  939. if (ry == 0)
  940. {
  941. rtgui_dc_draw_hline(dc, x - rx, x + rx, y);
  942. return;
  943. }
  944. /*
  945. * Init vars
  946. */
  947. oh = oi = oj = ok = 0xFFFF;
  948. if (rx > ry)
  949. {
  950. ix = 0;
  951. iy = rx * 64;
  952. do
  953. {
  954. h = (ix + 32) >> 6;
  955. i = (iy + 32) >> 6;
  956. j = (h * ry) / rx;
  957. k = (i * ry) / rx;
  958. if (((ok != k) && (oj != k)) || ((oj != j) && (ok != j)) || (k != j))
  959. {
  960. xph = x + h;
  961. xmh = x - h;
  962. if (k > 0)
  963. {
  964. ypk = y + k;
  965. ymk = y - k;
  966. rtgui_dc_draw_point(dc, xmh, ypk);
  967. rtgui_dc_draw_point(dc, xph, ypk);
  968. rtgui_dc_draw_point(dc, xmh, ymk);
  969. rtgui_dc_draw_point(dc, xph, ymk);
  970. }
  971. else
  972. {
  973. rtgui_dc_draw_point(dc, xmh, y);
  974. rtgui_dc_draw_point(dc, xph, y);
  975. }
  976. ok = k;
  977. xpi = x + i;
  978. xmi = x - i;
  979. if (j > 0)
  980. {
  981. ypj = y + j;
  982. ymj = y - j;
  983. rtgui_dc_draw_point(dc, xmi, ypj);
  984. rtgui_dc_draw_point(dc, xpi, ypj);
  985. rtgui_dc_draw_point(dc, xmi, ymj);
  986. rtgui_dc_draw_point(dc, xpi, ymj);
  987. }
  988. else
  989. {
  990. rtgui_dc_draw_point(dc, xmi, y);
  991. rtgui_dc_draw_point(dc, xpi, y);
  992. }
  993. oj = j;
  994. }
  995. ix = ix + iy / rx;
  996. iy = iy - ix / rx;
  997. } while (i > h);
  998. }
  999. else
  1000. {
  1001. ix = 0;
  1002. iy = ry * 64;
  1003. do
  1004. {
  1005. h = (ix + 32) >> 6;
  1006. i = (iy + 32) >> 6;
  1007. j = (h * rx) / ry;
  1008. k = (i * rx) / ry;
  1009. if (((oi != i) && (oh != i)) || ((oh != h) && (oi != h) && (i != h)))
  1010. {
  1011. xmj = x - j;
  1012. xpj = x + j;
  1013. if (i > 0)
  1014. {
  1015. ypi = y + i;
  1016. ymi = y - i;
  1017. rtgui_dc_draw_point(dc, xmj, ypi);
  1018. rtgui_dc_draw_point(dc, xpj, ypi);
  1019. rtgui_dc_draw_point(dc, xmj, ymi);
  1020. rtgui_dc_draw_point(dc, xpj, ymi);
  1021. }
  1022. else
  1023. {
  1024. rtgui_dc_draw_point(dc, xmj, y);
  1025. rtgui_dc_draw_point(dc, xpj, y);
  1026. }
  1027. oi = i;
  1028. xmk = x - k;
  1029. xpk = x + k;
  1030. if (h > 0)
  1031. {
  1032. yph = y + h;
  1033. ymh = y - h;
  1034. rtgui_dc_draw_point(dc, xmk, yph);
  1035. rtgui_dc_draw_point(dc, xpk, yph);
  1036. rtgui_dc_draw_point(dc, xmk, ymh);
  1037. rtgui_dc_draw_point(dc, xpk, ymh);
  1038. }
  1039. else
  1040. {
  1041. rtgui_dc_draw_point(dc, xmk, y);
  1042. rtgui_dc_draw_point(dc, xpk, y);
  1043. }
  1044. oh = h;
  1045. }
  1046. ix = ix + iy / ry;
  1047. iy = iy - ix / ry;
  1048. } while (i > h);
  1049. }
  1050. }
  1051. 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)
  1052. {
  1053. int ix, iy;
  1054. int h, i, j, k;
  1055. int oh, oi, oj, ok;
  1056. int xmh, xph;
  1057. int xmi, xpi;
  1058. int xmj, xpj;
  1059. int xmk, xpk;
  1060. /*
  1061. * Special case for rx=0 - draw a vline
  1062. */
  1063. if (rx == 0)
  1064. {
  1065. rtgui_dc_draw_vline(dc, x, y - ry, y + ry);
  1066. return;
  1067. }
  1068. /* special case for ry=0 - draw a hline */
  1069. if (ry == 0) {
  1070. rtgui_dc_draw_hline(dc, x - rx, x + rx, y);
  1071. return;
  1072. }
  1073. /*
  1074. * Init vars
  1075. */
  1076. oh = oi = oj = ok = 0xFFFF;
  1077. /*
  1078. * Draw
  1079. */
  1080. if (rx > ry) {
  1081. ix = 0;
  1082. iy = rx * 64;
  1083. do {
  1084. h = (ix + 32) >> 6;
  1085. i = (iy + 32) >> 6;
  1086. j = (h * ry) / rx;
  1087. k = (i * ry) / rx;
  1088. if ((ok != k) && (oj != k)) {
  1089. xph = x + h;
  1090. xmh = x - h;
  1091. if (k > 0) {
  1092. rtgui_dc_draw_hline(dc, xmh, xph, y + k);
  1093. rtgui_dc_draw_hline(dc, xmh, xph, y - k);
  1094. } else {
  1095. rtgui_dc_draw_hline(dc, xmh, xph, y);
  1096. }
  1097. ok = k;
  1098. }
  1099. if ((oj != j) && (ok != j) && (k != j)) {
  1100. xmi = x - i;
  1101. xpi = x + i;
  1102. if (j > 0) {
  1103. rtgui_dc_draw_hline(dc, xmi, xpi, y + j);
  1104. rtgui_dc_draw_hline(dc, xmi, xpi, y - j);
  1105. } else {
  1106. rtgui_dc_draw_hline(dc, xmi, xpi, y);
  1107. }
  1108. oj = j;
  1109. }
  1110. ix = ix + iy / rx;
  1111. iy = iy - ix / rx;
  1112. } while (i > h);
  1113. } else {
  1114. ix = 0;
  1115. iy = ry * 64;
  1116. do {
  1117. h = (ix + 32) >> 6;
  1118. i = (iy + 32) >> 6;
  1119. j = (h * rx) / ry;
  1120. k = (i * rx) / ry;
  1121. if ((oi != i) && (oh != i)) {
  1122. xmj = x - j;
  1123. xpj = x + j;
  1124. if (i > 0) {
  1125. rtgui_dc_draw_hline(dc, xmj, xpj, y + i);
  1126. rtgui_dc_draw_hline(dc, xmj, xpj, y - i);
  1127. } else {
  1128. rtgui_dc_draw_hline(dc, xmj, xpj, y);
  1129. }
  1130. oi = i;
  1131. }
  1132. if ((oh != h) && (oi != h) && (i != h)) {
  1133. xmk = x - k;
  1134. xpk = x + k;
  1135. if (h > 0) {
  1136. rtgui_dc_draw_hline(dc, xmk, xpk, y + h);
  1137. rtgui_dc_draw_hline(dc, xmk, xpk, y - h);
  1138. } else {
  1139. rtgui_dc_draw_hline(dc, xmk, xpk, y);
  1140. }
  1141. oh = h;
  1142. }
  1143. ix = ix + iy / ry;
  1144. iy = iy - ix / ry;
  1145. } while (i > h);
  1146. }
  1147. }
  1148. void rtgui_dc_draw_focus_rect(struct rtgui_dc* dc, rtgui_rect_t* rect)
  1149. {
  1150. int i;
  1151. for (i = rect->x1; i <= rect->x2; i += 2)
  1152. {
  1153. rtgui_dc_draw_point(dc, i, rect->y1);
  1154. rtgui_dc_draw_point(dc, i, rect->y2);
  1155. }
  1156. for (i = rect->y1; i <= rect->y2; i += 2)
  1157. {
  1158. rtgui_dc_draw_point(dc, rect->x1, i);
  1159. rtgui_dc_draw_point(dc, rect->x2, i);
  1160. }
  1161. }
  1162. void rtgui_dc_get_rect(struct rtgui_dc*dc, rtgui_rect_t* rect)
  1163. {
  1164. if (dc != RT_NULL && rect != RT_NULL)
  1165. {
  1166. dc->get_rect(dc, rect);
  1167. }
  1168. }