dc.c 25 KB

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