dc.c 28 KB

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