dc.c 28 KB

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