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. return rtgui_dc_draw_rect(dc, rect);
  150. }
  151. if(((rect->x2 - rect->x1)/2 >= r)&&((rect->y2-rect->y1)/2 >= r))
  152. {
  153. rtgui_dc_draw_arc(dc, rect->x1 + r, rect->y1 + r, r, 180, 270);
  154. rtgui_dc_draw_arc(dc, rect->x2 - r, rect->y1 + r, r, 270, 360);
  155. rtgui_dc_draw_arc(dc, rect->x1 + r, rect->y2 - r, r, 90, 180);
  156. rtgui_dc_draw_arc(dc, rect->x2 - r, rect->y2 - r, r, 0, 90);
  157. rtgui_dc_draw_hline(dc, rect->x1 + r, rect->x2 - r, rect->y1);
  158. rtgui_dc_draw_hline(dc, rect->x1 + r, rect->x2 - r, rect->y2);
  159. rtgui_dc_draw_vline(dc, rect->x1, rect->y1 + r, rect->y2 - r);
  160. rtgui_dc_draw_vline(dc, rect->x2, rect->y1 + r, rect->y2 - r);
  161. }
  162. }
  163. void rtgui_dc_fill_round_rect(struct rtgui_dc* dc, struct rtgui_rect* rect, int r)
  164. {
  165. struct rtgui_rect rect_temp;
  166. RT_ASSERT(((rect->x2 - rect->x1)/2 >= r)&&((rect->y2-rect->y1)/2 >= r));
  167. if(((rect->x2 - rect->x1)/2 >= r)&&((rect->y2-rect->y1)/2 >= r))
  168. {
  169. rect_temp.x1 = rect->x1 + r;
  170. rect_temp.y1 = rect->y1;
  171. rect_temp.x2 = rect->x2 - r;
  172. rect_temp.y2 = rect->y2;
  173. rtgui_dc_fill_rect_forecolor(dc, &rect_temp);//fill rect with foreground
  174. rect_temp.x1 = rect->x1;
  175. rect_temp.y1 = rect->y1 + r;
  176. rect_temp.x2 = rect->x1 + r;
  177. rect_temp.y2 = rect->y2 - r;
  178. rtgui_dc_fill_rect_forecolor(dc, &rect_temp);//fill rect with foreground
  179. rect_temp.x1 = rect->x2 - r;
  180. rect_temp.y1 = rect->y1 + r;
  181. rect_temp.x2 = rect->x2;
  182. rect_temp.y2 = rect->y2 - r;
  183. rtgui_dc_fill_rect_forecolor(dc, &rect_temp);//fill rect with foreground
  184. rtgui_dc_fill_circle(dc, rect->x1 + r, rect->y1 + r, r);
  185. rtgui_dc_fill_circle(dc, rect->x2 - r, rect->y2 - r, r);
  186. rtgui_dc_fill_circle(dc, rect->x2 - r, rect->y1 + r, r);
  187. rtgui_dc_fill_circle(dc, rect->x1 + r, rect->y2 - r, r);
  188. }
  189. }
  190. void rtgui_dc_draw_shaded_rect(struct rtgui_dc* dc, rtgui_rect_t* rect,
  191. rtgui_color_t c1, rtgui_color_t c2)
  192. {
  193. RT_ASSERT(dc != RT_NULL);
  194. RTGUI_DC_FC(dc) = c1;
  195. rtgui_dc_draw_vline(dc, rect->x1, rect->y1, rect->y2);
  196. rtgui_dc_draw_hline(dc, rect->x1 + 1, rect->x2, rect->y1);
  197. RTGUI_DC_FC(dc) = c2;
  198. rtgui_dc_draw_vline(dc, rect->x2, rect->y1, rect->y2);
  199. rtgui_dc_draw_hline(dc, rect->x1, rect->x2 + 1, rect->y2);
  200. }
  201. void rtgui_dc_draw_focus_rect(struct rtgui_dc* dc, rtgui_rect_t* rect)
  202. {
  203. int i;
  204. for (i = rect->x1; i <= rect->x2; i += 2)
  205. {
  206. rtgui_dc_draw_point(dc, i, rect->y1);
  207. rtgui_dc_draw_point(dc, i, rect->y2);
  208. }
  209. for (i = rect->y1; i <= rect->y2; i += 2)
  210. {
  211. rtgui_dc_draw_point(dc, rect->x1, i);
  212. rtgui_dc_draw_point(dc, rect->x2, i);
  213. }
  214. }
  215. void rtgui_dc_draw_text (struct rtgui_dc* dc, const char* text, struct rtgui_rect* rect)
  216. {
  217. rt_uint32_t len;
  218. struct rtgui_font *font;
  219. struct rtgui_rect text_rect;
  220. RT_ASSERT(dc != RT_NULL);
  221. font = RTGUI_DC_FONT(dc);
  222. if (font == RT_NULL)
  223. {
  224. /* use system default font */
  225. font = rtgui_font_default();
  226. }
  227. /* text align */
  228. rtgui_font_get_metrics(font, text, &text_rect);
  229. rtgui_rect_moveto_align(rect, &text_rect, RTGUI_DC_TEXTALIGN(dc));
  230. len = strlen((const char*)text);
  231. rtgui_font_draw(font, dc, text, len, &text_rect);
  232. }
  233. /*
  234. * draw a monochrome color bitmap data
  235. */
  236. void rtgui_dc_draw_mono_bmp(struct rtgui_dc* dc, int x, int y, int w, int h, const rt_uint8_t* data)
  237. {
  238. int i, j, k;
  239. /* get word bytes */
  240. w = (w + 7)/8;
  241. /* draw mono bitmap data */
  242. for (i = 0; i < h; i ++)
  243. for (j = 0; j < w; j++)
  244. for (k = 0; k < 8; k++)
  245. if ( ((data[i*w + j] >> (7-k)) & 0x01) != 0)
  246. rtgui_dc_draw_point(dc, x + 8*j + k, y + i);
  247. }
  248. void rtgui_dc_draw_byte(struct rtgui_dc*dc, int x, int y, int h, const rt_uint8_t* data)
  249. {
  250. rtgui_dc_draw_mono_bmp(dc, x, y, 8, h, data);
  251. }
  252. void rtgui_dc_draw_word(struct rtgui_dc*dc, int x, int y, int h, const rt_uint8_t* data)
  253. {
  254. rtgui_dc_draw_mono_bmp(dc, x, y, 16, h, data);
  255. }
  256. void rtgui_dc_draw_border(struct rtgui_dc* dc, rtgui_rect_t* rect, int flag)
  257. {
  258. rtgui_rect_t r;
  259. rtgui_color_t color;
  260. if (dc == RT_NULL) return ;
  261. /* save old color */
  262. color = RTGUI_DC_FC(dc);
  263. r = *rect;
  264. switch (flag)
  265. {
  266. case RTGUI_BORDER_RAISE:
  267. rtgui_dc_draw_shaded_rect(dc, &r, high_light, black);
  268. rtgui_rect_inflate(&r, -1);
  269. rtgui_dc_draw_shaded_rect(dc, &r, light_grey, dark_grey);
  270. break;
  271. case RTGUI_BORDER_SUNKEN:
  272. rtgui_dc_draw_shaded_rect(dc, &r, dark_grey, high_light);
  273. rtgui_rect_inflate(&r, -1);
  274. rtgui_dc_draw_shaded_rect(dc, &r, black, light_grey);
  275. break;
  276. case RTGUI_BORDER_BOX:
  277. rtgui_dc_draw_shaded_rect(dc, &r, dark_grey, high_light);
  278. rtgui_rect_inflate(&r, -1);
  279. rtgui_dc_draw_shaded_rect(dc, &r, high_light, dark_grey);
  280. break;
  281. case RTGUI_BORDER_STATIC:
  282. rtgui_dc_draw_shaded_rect(dc, &r, dark_grey, high_light);
  283. break;
  284. case RTGUI_BORDER_EXTRA:
  285. RTGUI_DC_FC(dc) = light_grey;
  286. rtgui_dc_draw_rect(dc, &r);
  287. break;
  288. case RTGUI_BORDER_SIMPLE:
  289. RTGUI_DC_FC(dc) = black;
  290. rtgui_dc_draw_rect(dc, &r);
  291. break;
  292. default:
  293. break;
  294. }
  295. /* restore color */
  296. RTGUI_DC_FC(dc) = color;
  297. }
  298. void rtgui_dc_draw_polygon(struct rtgui_dc* dc, const int *vx, const int *vy, int count)
  299. {
  300. int i;
  301. const int *x1, *y1, *x2, *y2;
  302. /*
  303. * Sanity check
  304. */
  305. if (count < 3) return;
  306. /*
  307. * Pointer setup
  308. */
  309. x1 = x2 = vx;
  310. y1 = y2 = vy;
  311. x2++;
  312. y2++;
  313. /*
  314. * Draw
  315. */
  316. for (i = 1; i < count; i++)
  317. {
  318. rtgui_dc_draw_line(dc, *x1, *y1, *x2, *y2);
  319. x1 = x2;
  320. y1 = y2;
  321. x2++;
  322. y2++;
  323. }
  324. rtgui_dc_draw_line(dc, *x1, *y1, *vx, *vy);
  325. }
  326. void rtgui_dc_draw_regular_polygon(struct rtgui_dc* dc, int x, int y, int r, int count, rt_uint16_t angle)
  327. {
  328. int i, temp_val;
  329. double temp;
  330. float angle_interval;
  331. int *xx;
  332. int *x_head;
  333. int *yy;
  334. int *y_head;
  335. /*
  336. * Sanity check
  337. */
  338. if (count < 3) return;
  339. angle_interval = 360.0 / count;
  340. /*
  341. * Pointer setup
  342. */
  343. x_head = xx = (int *)rt_malloc(sizeof(int) * count);
  344. y_head = yy = (int *)rt_malloc(sizeof(int) * count);
  345. for(i = 0; i < count; i++)
  346. {
  347. temp = cos(((angle_interval * i) + angle) * M_PI / 180);
  348. temp *= r;
  349. temp_val = (int)temp;
  350. *xx = temp_val + x;
  351. temp = sin(((angle_interval * i) + angle) * M_PI / 180);
  352. temp *= r;
  353. temp_val = (int)temp;
  354. *yy = temp_val + y;
  355. xx++;
  356. yy++;
  357. }
  358. rtgui_dc_draw_polygon(dc, (const int *)x_head, (const int *)y_head, count);
  359. rt_free(x_head);
  360. rt_free(y_head);
  361. }
  362. void rtgui_dc_fill_polygon(struct rtgui_dc* dc, const int* vx, const int* vy, int count)
  363. {
  364. int i;
  365. int y, xa, xb;
  366. int miny, maxy;
  367. int x1, y1;
  368. int x2, y2;
  369. int ind1, ind2;
  370. int ints;
  371. int *poly_ints = RT_NULL;
  372. /*
  373. * Sanity check number of edges
  374. */
  375. if (count < 3) return;
  376. /*
  377. * Allocate temp array, only grow array
  378. */
  379. poly_ints = (int *) rt_malloc(sizeof(int) * count);
  380. if (poly_ints == RT_NULL) return ; /* no memory, failed */
  381. /*
  382. * Determine Y maximal
  383. */
  384. miny = vy[0];
  385. maxy = vy[0];
  386. for (i = 1; (i < count); i++)
  387. {
  388. if (vy[i] < miny) miny = vy[i];
  389. else if (vy[i] > maxy) maxy = vy[i];
  390. }
  391. /*
  392. * Draw, scanning y
  393. */
  394. for (y = miny; (y <= maxy); y++) {
  395. ints = 0;
  396. for (i = 0; (i < count); i++) {
  397. if (!i) {
  398. ind1 = count - 1;
  399. ind2 = 0;
  400. } else {
  401. ind1 = i - 1;
  402. ind2 = i;
  403. }
  404. y1 = vy[ind1];
  405. y2 = vy[ind2];
  406. if (y1 < y2) {
  407. x1 = vx[ind1];
  408. x2 = vx[ind2];
  409. } else if (y1 > y2) {
  410. y2 = vy[ind1];
  411. y1 = vy[ind2];
  412. x2 = vx[ind1];
  413. x1 = vx[ind2];
  414. } else {
  415. continue;
  416. }
  417. if ( ((y >= y1) && (y < y2)) || ((y == maxy) && (y > y1) && (y <= y2)) )
  418. {
  419. poly_ints[ints++] = ((65536 * (y - y1)) / (y2 - y1)) * (x2 - x1) + (65536 * x1);
  420. }
  421. }
  422. qsort(poly_ints, ints, sizeof(int), _int_compare);
  423. for (i = 0; (i < ints); i += 2)
  424. {
  425. xa = poly_ints[i] + 1;
  426. xa = (xa >> 16) + ((xa & 32768) >> 15);
  427. xb = poly_ints[i+1] - 1;
  428. xb = (xb >> 16) + ((xb & 32768) >> 15);
  429. rtgui_dc_draw_hline(dc, xa, xb, y);
  430. }
  431. }
  432. }
  433. void rtgui_dc_draw_circle(struct rtgui_dc* dc, int x, int y, int r)
  434. {
  435. rt_int16_t cx = 0;
  436. rt_int16_t cy = r;
  437. rt_int16_t df = 1 - r;
  438. rt_int16_t d_e = 3;
  439. rt_int16_t d_se = -2 * r + 5;
  440. rt_int16_t xpcx, xmcx, xpcy, xmcy;
  441. rt_int16_t ypcy, ymcy, ypcx, ymcx;
  442. /*
  443. * sanity check radius
  444. */
  445. if (r < 0) return ;
  446. /* special case for r=0 - draw a point */
  447. if (r == 0) rtgui_dc_draw_point(dc, x, y);
  448. /*
  449. * draw circle
  450. */
  451. do
  452. {
  453. ypcy = y + cy;
  454. ymcy = y - cy;
  455. if (cx > 0)
  456. {
  457. xpcx = x + cx;
  458. xmcx = x - cx;
  459. rtgui_dc_draw_point(dc, xmcx, ypcy);
  460. rtgui_dc_draw_point(dc, xpcx, ypcy);
  461. rtgui_dc_draw_point(dc, xmcx, ymcy);
  462. rtgui_dc_draw_point(dc, xpcx, ymcy);
  463. }
  464. else
  465. {
  466. rtgui_dc_draw_point(dc, x, ymcy);
  467. rtgui_dc_draw_point(dc, x, ypcy);
  468. }
  469. xpcy = x + cy;
  470. xmcy = x - cy;
  471. if ((cx > 0) && (cx != cy))
  472. {
  473. ypcx = y + cx;
  474. ymcx = y - cx;
  475. rtgui_dc_draw_point(dc, xmcy, ypcx);
  476. rtgui_dc_draw_point(dc, xpcy, ypcx);
  477. rtgui_dc_draw_point(dc, xmcy, ymcx);
  478. rtgui_dc_draw_point(dc, xpcy, ymcx);
  479. }
  480. else if (cx == 0)
  481. {
  482. rtgui_dc_draw_point(dc, xmcy, y);
  483. rtgui_dc_draw_point(dc, xpcy, y);
  484. }
  485. /*
  486. * Update
  487. */
  488. if (df < 0)
  489. {
  490. df += d_e;
  491. d_e += 2;
  492. d_se += 2;
  493. }
  494. else
  495. {
  496. df += d_se;
  497. d_e += 2;
  498. d_se += 4;
  499. cy--;
  500. }
  501. cx++;
  502. }while (cx <= cy);
  503. }
  504. void rtgui_dc_fill_circle(struct rtgui_dc* dc, rt_int16_t x, rt_int16_t y, rt_int16_t r)
  505. {
  506. rt_int16_t cx = 0;
  507. rt_int16_t cy = r;
  508. rt_int16_t ocx = (rt_int16_t) 0xffff;
  509. rt_int16_t ocy = (rt_int16_t) 0xffff;
  510. rt_int16_t df = 1 - r;
  511. rt_int16_t d_e = 3;
  512. rt_int16_t d_se = -2 * r + 5;
  513. rt_int16_t xpcx, xmcx, xpcy, xmcy;
  514. rt_int16_t ypcy, ymcy, ypcx, ymcx;
  515. /*
  516. * Sanity check radius
  517. */
  518. if (r < 0) return;
  519. /*
  520. * Special case for r=0 - draw a point
  521. */
  522. if (r == 0)
  523. {
  524. rtgui_dc_draw_point(dc, x, y);
  525. return ;
  526. }
  527. /*
  528. * Draw
  529. */
  530. do {
  531. xpcx = x + cx;
  532. xmcx = x - cx;
  533. xpcy = x + cy;
  534. xmcy = x - cy;
  535. if (ocy != cy) {
  536. if (cy > 0) {
  537. ypcy = y + cy;
  538. ymcy = y - cy;
  539. rtgui_dc_draw_hline(dc, xmcx, xpcx, ypcy);
  540. rtgui_dc_draw_hline(dc, xmcx, xpcx, ymcy);
  541. } else {
  542. rtgui_dc_draw_hline(dc, xmcx, xpcx, y);
  543. }
  544. ocy = cy;
  545. }
  546. if (ocx != cx) {
  547. if (cx != cy) {
  548. if (cx > 0) {
  549. ypcx = y + cx;
  550. ymcx = y - cx;
  551. rtgui_dc_draw_hline(dc, xmcy, xpcy, ymcx);
  552. rtgui_dc_draw_hline(dc, xmcy, xpcy, ypcx);
  553. } else {
  554. rtgui_dc_draw_hline(dc, xmcy, xpcy, y);
  555. }
  556. }
  557. ocx = cx;
  558. }
  559. /*
  560. * Update
  561. */
  562. if (df < 0) {
  563. df += d_e;
  564. d_e += 2;
  565. d_se += 2;
  566. } else {
  567. df += d_se;
  568. d_e += 2;
  569. d_se += 4;
  570. cy--;
  571. }
  572. cx++;
  573. } while (cx <= cy);
  574. }
  575. 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)
  576. {
  577. rt_int16_t cx = 0;
  578. rt_int16_t cy = r;
  579. rt_int16_t df = 1 - r;
  580. rt_int16_t d_e = 3;
  581. rt_int16_t d_se = -2 * r + 5;
  582. rt_int16_t xpcx, xmcx, xpcy, xmcy;
  583. rt_int16_t ypcy, ymcy, ypcx, ymcx;
  584. rt_uint8_t drawoct;
  585. int startoct, endoct, oct, stopval_start, stopval_end;
  586. double temp;
  587. stopval_start = 0;
  588. stopval_end = 0;
  589. temp = 0;
  590. /* Sanity check radius */
  591. if (r < 0) return ;
  592. /* Special case for r=0 - draw a point */
  593. if (r == 0)
  594. {
  595. rtgui_dc_draw_point(dc, x, y);
  596. return;
  597. }
  598. /*
  599. * Draw arc
  600. */
  601. // Octant labelling
  602. //
  603. // \ 5 | 6 /
  604. // \ | /
  605. // 4 \ | / 7
  606. // \|/
  607. //------+------ +x
  608. // /|\
  609. // 3 / | \ 0
  610. // / | \
  611. // / 2 | 1 \
  612. // +y
  613. drawoct = 0; // 0x00000000
  614. // whether or not to keep drawing a given octant.
  615. // For example: 0x00111100 means we're drawing in octants 2-5
  616. // 0 <= start & end < 360; note that sometimes start > end - if so, arc goes back through 0.
  617. while (start < 0) start += 360;
  618. while (end < 0) end += 360;
  619. /* Fixup angles */
  620. start = start % 360;
  621. end = end % 360;
  622. // now, we find which octants we're drawing in.
  623. startoct = start / 45;
  624. endoct = end / 45;
  625. oct = startoct - 1; // we increment as first step in loop
  626. //stopval_start, stopval_end; // what values of cx to stop at.
  627. do {
  628. oct = (oct + 1) % 8;
  629. if (oct == startoct)
  630. {
  631. // need to compute stopval_start for this octant. Look at picture above if this is unclear
  632. switch (oct)
  633. {
  634. case 0:
  635. case 3:
  636. temp = sin(start * M_PI / 180);
  637. break;
  638. case 1:
  639. case 6:
  640. temp = cos(start * M_PI / 180);
  641. break;
  642. case 2:
  643. case 5:
  644. temp = -cos(start * M_PI / 180);
  645. break;
  646. case 4:
  647. case 7:
  648. temp = -sin(start * M_PI / 180);
  649. break;
  650. }
  651. temp *= r;
  652. stopval_start = (int)temp; // always round down.
  653. // This isn't arbitrary, but requires graph paper to explain well.
  654. // The basic idea is that we're always changing drawoct after we draw, so we
  655. // stop immediately after we render the last sensible pixel at x = ((int)temp).
  656. // and whether to draw in this octant initially
  657. if (oct % 2) drawoct |= (1 << oct); // this is basically like saying drawoct[oct] = true, if drawoct were a bool array
  658. else drawoct &= 255 - (1 << oct); // this is basically like saying drawoct[oct] = false
  659. }
  660. if (oct == endoct)
  661. {
  662. // need to compute stopval_end for this octant
  663. switch (oct)
  664. {
  665. case 0:
  666. case 3:
  667. temp = sin(end * M_PI / 180);
  668. break;
  669. case 1:
  670. case 6:
  671. temp = cos(end * M_PI / 180);
  672. break;
  673. case 2:
  674. case 5:
  675. temp = -cos(end * M_PI / 180);
  676. break;
  677. case 4:
  678. case 7:
  679. temp = -sin(end * M_PI / 180);
  680. break;
  681. }
  682. temp *= r;
  683. stopval_end = (int)temp;
  684. // and whether to draw in this octant initially
  685. if (startoct == endoct)
  686. {
  687. // note: we start drawing, stop, then start again in this case
  688. // otherwise: we only draw in this octant, so initialize it to false, it will get set back to true
  689. if (start > end)
  690. {
  691. // unfortunately, if we're in the same octant and need to draw over the whole circle,
  692. // we need to set the rest to true, because the while loop will end at the bottom.
  693. drawoct = 255;
  694. }
  695. else
  696. {
  697. drawoct &= 255 - (1 << oct);
  698. }
  699. }
  700. else if (oct % 2) drawoct &= 255 - (1 << oct);
  701. else drawoct |= (1 << oct);
  702. } else if (oct != startoct) { // already verified that it's != endoct
  703. drawoct |= (1 << oct); // draw this entire segment
  704. }
  705. } while (oct != endoct);
  706. // so now we have what octants to draw and when to draw them. all that's left is the actual raster code.
  707. do
  708. {
  709. ypcy = y + cy;
  710. ymcy = y - cy;
  711. if (cx > 0)
  712. {
  713. xpcx = x + cx;
  714. xmcx = x - cx;
  715. // always check if we're drawing a certain octant before adding a pixel to that octant.
  716. if (drawoct & 4) rtgui_dc_draw_point(dc, xmcx, ypcy); // drawoct & 4 = 22; drawoct[2]
  717. if (drawoct & 2) rtgui_dc_draw_point(dc, xpcx, ypcy);
  718. if (drawoct & 32) rtgui_dc_draw_point(dc, xmcx, ymcy);
  719. if (drawoct & 64) rtgui_dc_draw_point(dc, xpcx, ymcy);
  720. }
  721. else
  722. {
  723. if (drawoct & 6) rtgui_dc_draw_point(dc, x, ypcy); // 4 + 2; drawoct[2] || drawoct[1]
  724. if (drawoct & 96) rtgui_dc_draw_point(dc, x, ymcy); // 32 + 64
  725. }
  726. xpcy = x + cy;
  727. xmcy = x - cy;
  728. if (cx > 0 && cx != cy)
  729. {
  730. ypcx = y + cx;
  731. ymcx = y - cx;
  732. if (drawoct & 8) rtgui_dc_draw_point(dc, xmcy, ypcx);
  733. if (drawoct & 1) rtgui_dc_draw_point(dc, xpcy, ypcx);
  734. if (drawoct & 16) rtgui_dc_draw_point(dc, xmcy, ymcx);
  735. if (drawoct & 128) rtgui_dc_draw_point(dc, xpcy, ymcx);
  736. }
  737. else if (cx == 0)
  738. {
  739. if (drawoct & 24) rtgui_dc_draw_point(dc, xmcy, y); // 8 + 16
  740. if (drawoct & 129) rtgui_dc_draw_point(dc, xpcy, y); // 1 + 128
  741. }
  742. /*
  743. * Update whether we're drawing an octant
  744. */
  745. if (stopval_start == cx)
  746. {
  747. // works like an on-off switch because start & end may be in the same octant.
  748. if (drawoct & (1 << startoct)) drawoct &= 255 - (1 << startoct);
  749. else drawoct |= (1 << startoct);
  750. }
  751. if (stopval_end == cx)
  752. {
  753. if (drawoct & (1 << endoct)) drawoct &= 255 - (1 << endoct);
  754. else drawoct |= (1 << endoct);
  755. }
  756. /*
  757. * Update pixels
  758. */
  759. if (df < 0)
  760. {
  761. df += d_e;
  762. d_e += 2;
  763. d_se += 2;
  764. }
  765. else
  766. {
  767. df += d_se;
  768. d_e += 2;
  769. d_se += 4;
  770. cy--;
  771. }
  772. cx++;
  773. } while (cx <= cy);
  774. }
  775. 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)
  776. {
  777. rt_int16_t start_x, start_y;
  778. rt_int16_t end_x, end_y;
  779. double temp;
  780. rt_int16_t temp_val = 0;
  781. /* Sanity check radius */
  782. if ((r1 < 0) || (r1 < 0)) return ;
  783. /* Special case for r=0 - draw a point */
  784. if ((r1 == 0) && (r2 == 0))
  785. {
  786. rtgui_dc_draw_point(dc, x, y);
  787. return;
  788. }
  789. while (start < 0) start += 360;
  790. while (end < 0) end += 360;
  791. rtgui_dc_draw_arc(dc, x, y, r1, start, end);
  792. rtgui_dc_draw_arc(dc, x, y, r2, start, end);
  793. temp = cos(start * M_PI / 180);
  794. temp_val = (int)(temp * r1);
  795. start_x = x + temp_val;
  796. temp_val = (int)(temp * r2);
  797. end_x = x + temp_val;
  798. temp = sin(start * M_PI / 180);
  799. temp_val = (int)(temp * r1);
  800. start_y = y + temp_val;
  801. temp_val = (int)(temp * r2);
  802. end_y = y + temp_val;
  803. rtgui_dc_draw_line(dc, start_x, start_y, end_x, end_y);
  804. temp = cos(end * M_PI / 180);
  805. temp_val = (int)(temp * r1);
  806. start_x = x + temp_val;
  807. temp_val = (int)(temp * r2);
  808. end_x = x + temp_val;
  809. temp = sin(end * M_PI / 180);
  810. temp_val = (int)(temp * r1);
  811. start_y = y + temp_val;
  812. temp_val = (int)(temp * r2);
  813. end_y = y + temp_val;
  814. rtgui_dc_draw_line(dc, start_x, start_y, end_x, end_y);
  815. }
  816. 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)
  817. {
  818. int start_x, start_y;
  819. int end_x, end_y;
  820. /* Sanity check radius */
  821. if (r < 0) return ;
  822. /* Special case for r=0 - draw a point */
  823. if (r == 0)
  824. {
  825. rtgui_dc_draw_point(dc, x, y);
  826. return;
  827. }
  828. while (start < 0) start += 360;
  829. while (end < 0) end += 360;
  830. /* Fixup angles */
  831. start = start % 360;
  832. end = end % 360;
  833. rtgui_dc_draw_arc(dc, x, y, r, start, end);
  834. start_x = x + r * cos(start * M_PI / 180);
  835. start_y = y + r * sin(start * M_PI / 180);
  836. end_x = x + r * cos(end * M_PI / 180);
  837. end_y = y + r * sin(end * M_PI / 180);
  838. rtgui_dc_draw_line(dc, x, y, start_x, start_y);
  839. rtgui_dc_draw_line(dc, x, y, end_x, end_y);
  840. }
  841. 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)
  842. {
  843. int start_x, start_y;
  844. int end_x, end_y;
  845. float start_f;
  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. }