dc.c 27 KB

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