1
0

dc.c 27 KB

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