dc.c 28 KB

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