| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964 |
- /*
- * File : dc.c
- * This file is part of RT-Thread GUI Engine
- * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- * 2010-09-20 richard modified rtgui_dc_draw_round_rect
- * 2010-09-27 Bernard fix draw_mono_bmp issue
- * 2011-04-25 Bernard fix fill polygon issue, which found by loveic
- */
- /* for sin/cos etc */
- #include <math.h>
- #include <rtgui/dc.h>
- #include <rtgui/rtgui_system.h>
- #include <rtgui/rtgui_server.h>
- #include <rtgui/widgets/window.h>
- #include <rtgui/widgets/title.h>
- #include <string.h> /* for strlen */
- #include <stdlib.h> /* fir qsort */
- static int _int_compare(const void *a, const void *b)
- {
- return (*(const int *) a) - (*(const int *) b);
- }
- void rtgui_dc_destory(struct rtgui_dc *dc)
- {
- if (dc == RT_NULL) return;
- dc->engine->fini(dc);
- rtgui_free(dc);
- }
- RTM_EXPORT(rtgui_dc_destory);
- void rtgui_dc_draw_line(struct rtgui_dc *dc, int x1, int y1, int x2, int y2)
- {
- if (dc == RT_NULL) return;
- if (y1 == y2)
- {
- rtgui_dc_draw_hline(dc, x1, x2, y1);
- }
- else if (x1 == x2)
- {
- rtgui_dc_draw_vline(dc, x1, y1, y2);
- }
- else
- {
- int dx, dy, sdx, sdy, dxabs, dyabs, x, y, px, py;
- register rt_base_t i;
- /* rtgui_rect_t rect; */
- dx = x2 - x1; /* the horizontal distance of the line */
- dy = y2 - y1; /* the vertical distance of the line */
- #define rtgui_sgn(x) ((x<0)?-1:((x>0)?1:0)) /* macro to return the sign of a number */
- dxabs = _UI_ABS(dx);
- dyabs = _UI_ABS(dy);
- sdx = rtgui_sgn(dx);
- sdy = rtgui_sgn(dy);
- x = dyabs >> 1;
- y = dxabs >> 1;
- px = x1;
- py = y1;
- if (dxabs >= dyabs) /* the line is more horizontal than vertical */
- {
- for (i = 0; i < dxabs; i++)
- {
- y += dyabs;
- if (y >= dxabs)
- {
- y -= dxabs;
- py += sdy;
- }
- px += sdx;
- /* draw this point */
- rtgui_dc_draw_point(dc, px, py);
- }
- }
- else /* the line is more vertical than horizontal */
- {
- for (i = 0; i < dyabs; i++)
- {
- x += dxabs;
- if (x >= dyabs)
- {
- x -= dyabs;
- px += sdx;
- }
- py += sdy;
- /* draw this point */
- rtgui_dc_draw_point(dc, px, py);
- }
- }
- }
- }
- RTM_EXPORT(rtgui_dc_draw_line);
- void rtgui_dc_draw_horizontal_line(struct rtgui_dc *dc, int x1, int x2, int y)
- {
- rtgui_color_t color;
- if (dc == RT_NULL) return ;
- /* save old color */
- color = RTGUI_DC_FC(dc);
- RTGUI_DC_FC(dc) = dark_grey;
- rtgui_dc_draw_hline(dc, x1, x2, y);
- y ++;
- RTGUI_DC_FC(dc) = high_light;
- rtgui_dc_draw_hline(dc, x1, x2, y);
- /* restore color */
- RTGUI_DC_FC(dc) = color;
- }
- RTM_EXPORT(rtgui_dc_draw_horizontal_line);
- void rtgui_dc_draw_vertical_line(struct rtgui_dc *dc, int x, int y1, int y2)
- {
- rtgui_color_t color;
- if (dc == RT_NULL) return ;
- /* save old color */
- color = RTGUI_DC_FC(dc);
- RTGUI_DC_FC(dc) = dark_grey;
- rtgui_dc_draw_vline(dc, x, y1, y2);
- x ++;
- RTGUI_DC_FC(dc) = high_light;
- rtgui_dc_draw_vline(dc, x, y1, y2);
- /* restore color */
- RTGUI_DC_FC(dc) = color;
- }
- RTM_EXPORT(rtgui_dc_draw_vertical_line);
- void rtgui_dc_draw_rect(struct rtgui_dc *dc, struct rtgui_rect *rect)
- {
- rtgui_dc_draw_hline(dc, rect->x1, rect->x2, rect->y1);
- rtgui_dc_draw_hline(dc, rect->x1, rect->x2, rect->y2 - 1);
- rtgui_dc_draw_vline(dc, rect->x1, rect->y1, rect->y2);
- rtgui_dc_draw_vline(dc, rect->x2 - 1, rect->y1, rect->y2);
- }
- RTM_EXPORT(rtgui_dc_draw_rect);
- void rtgui_dc_fill_rect_forecolor(struct rtgui_dc *dc, struct rtgui_rect *rect)
- {
- rtgui_color_t save_color;
- /*save the background color of dc*/
- save_color = RTGUI_DC_BC(dc);
- /*set the background color to fore color*/
- RTGUI_DC_BC(dc) = RTGUI_DC_FC(dc);
- dc->engine->fill_rect(dc, rect);
- /*restore the background color of dc*/
- RTGUI_DC_BC(dc) = save_color;
- }
- RTM_EXPORT(rtgui_dc_fill_rect_forecolor);
- void rtgui_dc_draw_round_rect(struct rtgui_dc *dc, struct rtgui_rect *rect, int r)
- {
- RT_ASSERT(((rect->x2 - rect->x1) / 2 >= r) && ((rect->y2 - rect->y1) / 2 >= r));
- if (r < 0)
- {
- return;
- }
- if (r == 0)
- {
- rtgui_dc_draw_rect(dc, rect);
- return;
- }
- if (((rect->x2 - rect->x1) / 2 >= r) && ((rect->y2 - rect->y1) / 2 >= r))
- {
- rtgui_dc_draw_arc(dc, rect->x1 + r, rect->y1 + r, r, 180, 270);
- rtgui_dc_draw_arc(dc, rect->x2 - r, rect->y1 + r, r, 270, 360);
- rtgui_dc_draw_arc(dc, rect->x1 + r, rect->y2 - r, r, 90, 180);
- rtgui_dc_draw_arc(dc, rect->x2 - r, rect->y2 - r, r, 0, 90);
- rtgui_dc_draw_hline(dc, rect->x1 + r, rect->x2 - r, rect->y1);
- rtgui_dc_draw_hline(dc, rect->x1 + r, rect->x2 - r, rect->y2);
- rtgui_dc_draw_vline(dc, rect->x1, rect->y1 + r, rect->y2 - r);
- rtgui_dc_draw_vline(dc, rect->x2, rect->y1 + r, rect->y2 - r);
- }
- }
- RTM_EXPORT(rtgui_dc_draw_round_rect);
- void rtgui_dc_fill_round_rect(struct rtgui_dc *dc, struct rtgui_rect *rect, int r)
- {
- struct rtgui_rect rect_temp;
- RT_ASSERT(((rect->x2 - rect->x1) / 2 >= r) && ((rect->y2 - rect->y1) / 2 >= r));
- if (((rect->x2 - rect->x1) / 2 >= r) && ((rect->y2 - rect->y1) / 2 >= r))
- {
- rect_temp.x1 = rect->x1 + r;
- rect_temp.y1 = rect->y1;
- rect_temp.x2 = rect->x2 - r;
- rect_temp.y2 = rect->y2;
- rtgui_dc_fill_rect_forecolor(dc, &rect_temp);//fill rect with foreground
- rect_temp.x1 = rect->x1;
- rect_temp.y1 = rect->y1 + r;
- rect_temp.x2 = rect->x1 + r;
- rect_temp.y2 = rect->y2 - r;
- rtgui_dc_fill_rect_forecolor(dc, &rect_temp);//fill rect with foreground
- rect_temp.x1 = rect->x2 - r;
- rect_temp.y1 = rect->y1 + r;
- rect_temp.x2 = rect->x2;
- rect_temp.y2 = rect->y2 - r;
- rtgui_dc_fill_rect_forecolor(dc, &rect_temp);//fill rect with foreground
- rtgui_dc_fill_circle(dc, rect->x1 + r, rect->y1 + r, r);
- rtgui_dc_fill_circle(dc, rect->x2 - r, rect->y2 - r, r);
- rtgui_dc_fill_circle(dc, rect->x2 - r, rect->y1 + r, r);
- rtgui_dc_fill_circle(dc, rect->x1 + r, rect->y2 - r, r);
- }
- }
- RTM_EXPORT(rtgui_dc_fill_round_rect);
- void rtgui_dc_draw_shaded_rect(struct rtgui_dc *dc, rtgui_rect_t *rect,
- rtgui_color_t c1, rtgui_color_t c2)
- {
- RT_ASSERT(dc != RT_NULL);
- RTGUI_DC_FC(dc) = c1;
- rtgui_dc_draw_vline(dc, rect->x1, rect->y1, rect->y2);
- rtgui_dc_draw_hline(dc, rect->x1 + 1, rect->x2, rect->y1);
- RTGUI_DC_FC(dc) = c2;
- rtgui_dc_draw_vline(dc, rect->x2 - 1, rect->y1, rect->y2);
- rtgui_dc_draw_hline(dc, rect->x1, rect->x2, rect->y2 - 1);
- }
- RTM_EXPORT(rtgui_dc_draw_shaded_rect);
- void rtgui_dc_fill_gradient_rectv(struct rtgui_dc *dc, rtgui_rect_t *rect,
- rtgui_color_t c1, rtgui_color_t c2)
- {
- int y, step;
- rtgui_color_t fc;
- RT_ASSERT(dc != RT_NULL);
- RT_ASSERT(rect != RT_NULL);
- step = rtgui_rect_height(*rect);
- fc = RTGUI_DC_FC(dc);
- for (y = rect->y1; y < rect->y2; y++)
- {
- RTGUI_DC_FC(dc) = RTGUI_ARGB(((int)RTGUI_RGB_A(c2) - RTGUI_RGB_A(c1)) * (y - rect->y1) / step + RTGUI_RGB_A(c1),
- ((int)RTGUI_RGB_R(c2) - RTGUI_RGB_R(c1)) * (y - rect->y1) / step + RTGUI_RGB_R(c1),
- ((int)RTGUI_RGB_G(c2) - RTGUI_RGB_G(c1)) * (y - rect->y1) / step + RTGUI_RGB_G(c1),
- ((int)RTGUI_RGB_B(c2) - RTGUI_RGB_B(c1)) * (y - rect->y1) / step + RTGUI_RGB_B(c1));
- rtgui_dc_draw_hline(dc, rect->x1, rect->x2, y);
- }
- RTGUI_DC_FC(dc) = fc;
- }
- RTM_EXPORT(rtgui_dc_fill_gradient_rectv);
- void rtgui_dc_draw_focus_rect(struct rtgui_dc *dc, rtgui_rect_t *rect)
- {
- int x, y;
- for (x = rect->x1; x < rect->x2 - 1; x += 2)
- {
- rtgui_dc_draw_point(dc, x, rect->y1);
- rtgui_dc_draw_point(dc, x, rect->y2 - 1);
- }
- for (y = rect->y1; y < rect->y2; y += 2)
- {
- rtgui_dc_draw_point(dc, rect->x1, y);
- rtgui_dc_draw_point(dc, rect->x2 - 1, y);
- }
- }
- RTM_EXPORT(rtgui_dc_draw_focus_rect);
- void rtgui_dc_draw_text(struct rtgui_dc *dc, const char *text, struct rtgui_rect *rect)
- {
- rt_uint32_t len;
- struct rtgui_font *font;
- RT_ASSERT(dc != RT_NULL);
- font = RTGUI_DC_FONT(dc);
- if (font == RT_NULL)
- {
- /* use system default font */
- font = rtgui_font_default();
- }
- len = strlen((const char *)text);
- if (len == 0)
- return;
- rtgui_font_draw(font, dc, text, len, rect);
- }
- RTM_EXPORT(rtgui_dc_draw_text);
- void rtgui_dc_draw_text_stroke(struct rtgui_dc *dc, const char *text, struct rtgui_rect *rect,
- rtgui_color_t color_stroke, rtgui_color_t color_core)
- {
- int x, y;
- rtgui_rect_t r;
- rtgui_color_t fc;
- RT_ASSERT(dc != RT_NULL);
- fc = RTGUI_DC_FC(dc);
- RTGUI_DC_FC(dc) = color_stroke;
- for (x = -1; x < 2; x++)
- {
- for (y = -1; y < 2; y++)
- {
- r = *rect;
- rtgui_rect_move(&r, x, y);
- rtgui_dc_draw_text(dc, text, &r);
- }
- }
- RTGUI_DC_FC(dc) = color_core;
- rtgui_dc_draw_text(dc, text, rect);
- RTGUI_DC_FC(dc) = fc;
- }
- RTM_EXPORT(rtgui_dc_draw_text_stroke);
- /*
- * draw a monochrome color bitmap data
- */
- void rtgui_dc_draw_mono_bmp(struct rtgui_dc *dc, int x, int y, int w, int h, const rt_uint8_t *data)
- {
- int i, j, k;
- /* get word bytes */
- w = (w + 7) / 8;
- /* draw mono bitmap data */
- for (i = 0; i < h; i ++)
- for (j = 0; j < w; j++)
- for (k = 0; k < 8; k++)
- if (((data[i * w + j] >> (7 - k)) & 0x01) != 0)
- rtgui_dc_draw_point(dc, x + 8 * j + k, y + i);
- }
- RTM_EXPORT(rtgui_dc_draw_mono_bmp);
- void rtgui_dc_draw_byte(struct rtgui_dc *dc, int x, int y, int h, const rt_uint8_t *data)
- {
- rtgui_dc_draw_mono_bmp(dc, x, y, 8, h, data);
- }
- RTM_EXPORT(rtgui_dc_draw_byte);
- void rtgui_dc_draw_word(struct rtgui_dc *dc, int x, int y, int h, const rt_uint8_t *data)
- {
- rtgui_dc_draw_mono_bmp(dc, x, y, 16, h, data);
- }
- RTM_EXPORT(rtgui_dc_draw_word);
- void rtgui_dc_draw_border(struct rtgui_dc *dc, rtgui_rect_t *rect, int flag)
- {
- rtgui_rect_t r;
- rtgui_color_t color;
- if (dc == RT_NULL) return ;
- /* save old color */
- color = RTGUI_DC_FC(dc);
- r = *rect;
- switch (flag)
- {
- case RTGUI_BORDER_RAISE:
- rtgui_dc_draw_shaded_rect(dc, &r, high_light, black);
- rtgui_rect_inflate(&r, -1);
- rtgui_dc_draw_shaded_rect(dc, &r, light_grey, dark_grey);
- break;
- case RTGUI_BORDER_SUNKEN:
- rtgui_dc_draw_shaded_rect(dc, &r, dark_grey, high_light);
- rtgui_rect_inflate(&r, -1);
- rtgui_dc_draw_shaded_rect(dc, &r, black, light_grey);
- break;
- case RTGUI_BORDER_BOX:
- rtgui_dc_draw_shaded_rect(dc, &r, dark_grey, high_light);
- rtgui_rect_inflate(&r, -1);
- rtgui_dc_draw_shaded_rect(dc, &r, high_light, dark_grey);
- break;
- case RTGUI_BORDER_STATIC:
- rtgui_dc_draw_shaded_rect(dc, &r, dark_grey, high_light);
- break;
- case RTGUI_BORDER_EXTRA:
- RTGUI_DC_FC(dc) = light_grey;
- rtgui_dc_draw_rect(dc, &r);
- break;
- case RTGUI_BORDER_SIMPLE:
- RTGUI_DC_FC(dc) = black;
- rtgui_dc_draw_rect(dc, &r);
- break;
- default:
- break;
- }
- /* restore color */
- RTGUI_DC_FC(dc) = color;
- }
- RTM_EXPORT(rtgui_dc_draw_border);
- void rtgui_dc_draw_polygon(struct rtgui_dc *dc, const int *vx, const int *vy, int count)
- {
- int i;
- const int *x1, *y1, *x2, *y2;
- /*
- * Sanity check
- */
- if (count < 3) return;
- /*
- * Pointer setup
- */
- x1 = x2 = vx;
- y1 = y2 = vy;
- x2++;
- y2++;
- /*
- * Draw
- */
- for (i = 1; i < count; i++)
- {
- rtgui_dc_draw_line(dc, *x1, *y1, *x2, *y2);
- x1 = x2;
- y1 = y2;
- x2++;
- y2++;
- }
- rtgui_dc_draw_line(dc, *x1, *y1, *vx, *vy);
- }
- RTM_EXPORT(rtgui_dc_draw_polygon);
- void rtgui_dc_fill_polygon(struct rtgui_dc *dc, const int *vx, const int *vy, int count)
- {
- int i;
- int y, xa, xb;
- int miny, maxy;
- int x1, y1;
- int x2, y2;
- int ind1, ind2;
- int ints;
- int *poly_ints = RT_NULL;
- /*
- * Sanity check number of edges
- */
- if (count < 3) return;
- /*
- * Allocate temp array, only grow array
- */
- poly_ints = (int *) rtgui_malloc(sizeof(int) * count);
- if (poly_ints == RT_NULL) return ; /* no memory, failed */
- /*
- * Determine Y maximal
- */
- miny = vy[0];
- maxy = vy[0];
- for (i = 1; (i < count); i++)
- {
- if (vy[i] < miny) miny = vy[i];
- else if (vy[i] > maxy) maxy = vy[i];
- }
- /*
- * Draw, scanning y
- */
- for (y = miny; (y <= maxy); y++)
- {
- ints = 0;
- for (i = 0; (i < count); i++)
- {
- if (!i)
- {
- ind1 = count - 1;
- ind2 = 0;
- }
- else
- {
- ind1 = i - 1;
- ind2 = i;
- }
- y1 = vy[ind1];
- y2 = vy[ind2];
- if (y1 < y2)
- {
- x1 = vx[ind1];
- x2 = vx[ind2];
- }
- else if (y1 > y2)
- {
- y2 = vy[ind1];
- y1 = vy[ind2];
- x2 = vx[ind1];
- x1 = vx[ind2];
- }
- else
- {
- continue;
- }
- if (((y >= y1) && (y < y2)) || ((y == maxy) && (y > y1) && (y <= y2)))
- {
- poly_ints[ints++] = ((65536 * (y - y1)) / (y2 - y1)) * (x2 - x1) + (65536 * x1);
- }
- }
- qsort(poly_ints, ints, sizeof(int), _int_compare);
- for (i = 0; (i < ints); i += 2)
- {
- xa = poly_ints[i] + 1;
- xa = (xa >> 16) + ((xa & 32768) >> 15);
- xb = poly_ints[i + 1] - 1;
- xb = (xb >> 16) + ((xb & 32768) >> 15);
- rtgui_dc_draw_hline(dc, xa, xb, y);
- }
- }
- /* release memory */
- rtgui_free(poly_ints);
- }
- RTM_EXPORT(rtgui_dc_fill_polygon);
- void rtgui_dc_draw_circle(struct rtgui_dc *dc, int x, int y, int r)
- {
- rtgui_dc_draw_ellipse(dc, x, y, r, r);
- }
- RTM_EXPORT(rtgui_dc_draw_circle);
- enum
- {
- QUARTER_BTM,
- QUARTER_BTM_LEFT,
- QUARTER_BTM_RIGHT,
- QUARTER_TOP,
- QUARTER_TOP_LEFT,
- QUARTER_TOP_RIGHT,
- QUARTER_FULL,
- };
- static void _fill_quarter_circle(struct rtgui_dc *dc,
- rt_int16_t ox, rt_int16_t oy,
- rt_int16_t rad, int quadrant)
- {
- /* Midpoint circle algorithm. */
- int dk, x, y;
- dk = 1 - rad;
- x = 0;
- y = rad;
- while (x <= y)
- {
- switch (quadrant)
- {
- case QUARTER_BTM:
- rtgui_dc_draw_hline(dc, ox - x, ox + x, oy + y);
- rtgui_dc_draw_hline(dc, ox - y, ox + y, oy + x);
- break;
- case QUARTER_BTM_LEFT:
- rtgui_dc_draw_hline(dc, ox, ox + x, oy + y);
- rtgui_dc_draw_hline(dc, ox, ox + y, oy + x);
- break;
- case QUARTER_BTM_RIGHT:
- rtgui_dc_draw_hline(dc, ox, ox - x, oy + y);
- rtgui_dc_draw_hline(dc, ox, ox - y, oy + x);
- break;
- case QUARTER_TOP_RIGHT:
- rtgui_dc_draw_hline(dc, ox, ox - x, oy - y);
- rtgui_dc_draw_hline(dc, ox, ox - y, oy - x);
- break;
- case QUARTER_TOP_LEFT:
- rtgui_dc_draw_hline(dc, ox, ox + x, oy - y);
- rtgui_dc_draw_hline(dc, ox, ox + y, oy - x);
- break;
- case QUARTER_TOP:
- rtgui_dc_draw_hline(dc, ox - x, ox + x, oy - y);
- rtgui_dc_draw_hline(dc, ox - y, ox + y, oy - x);
- break;
- case QUARTER_FULL:
- rtgui_dc_draw_hline(dc, ox - x, ox + x, oy + y);
- rtgui_dc_draw_hline(dc, ox - y, ox + y, oy + x);
- rtgui_dc_draw_hline(dc, ox - x, ox + x, oy - y);
- rtgui_dc_draw_hline(dc, ox - y, ox + y, oy - x);
- break;
- default:
- RT_ASSERT(0);
- };
- if (dk > 0)
- {
- y--;
- dk += 2 * (x - y) + 5;
- }
- else
- {
- dk += 2 * x + 3;
- }
- x++;
- }
- }
- void rtgui_dc_fill_circle(struct rtgui_dc *dc, rt_int16_t x, rt_int16_t y, rt_int16_t r)
- {
- /*
- * Sanity check radius
- */
- if (r < 0)
- return;
- /*
- * Special case for r=0 - draw a point
- */
- if (r == 0)
- {
- rtgui_dc_draw_point(dc, x, y);
- return;
- }
- _fill_quarter_circle(dc, x, y, r, QUARTER_FULL);
- }
- RTM_EXPORT(rtgui_dc_fill_circle);
- 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)
- {
- rt_int16_t cx = 0;
- rt_int16_t cy = r;
- rt_int16_t df = 1 - r;
- rt_int16_t d_e = 3;
- rt_int16_t d_se = -2 * r + 5;
- rt_int16_t xpcx, xmcx, xpcy, xmcy;
- rt_int16_t ypcy, ymcy, ypcx, ymcx;
- rt_uint8_t drawoct;
- int startoct, endoct, oct, stopval_start, stopval_end;
- double temp;
- stopval_start = 0;
- stopval_end = 0;
- temp = 0;
- /* Sanity check radius */
- if (r < 0) return ;
- /* Special case for r=0 - draw a point */
- if (r == 0)
- {
- rtgui_dc_draw_point(dc, x, y);
- return;
- }
- /*
- * Draw arc
- * Octant labelling
- *
- * \ 5 | 6 /
- * \ | /
- * 4 \ | / 7
- * \|/
- *------+------ +x
- * /|\
- * 3 / | \ 0
- * / | \
- * / 2 | 1 \
- * +y
- */
- drawoct = 0; // 0x00000000
- // whether or not to keep drawing a given octant.
- // For example: 0x00111100 means we're drawing in octants 2-5
- // 0 <= start & end < 360; note that sometimes start > end - if so, arc goes back through 0.
- while (start < 0) start += 360;
- while (end < 0) end += 360;
- /* Fixup angles */
- start = start % 360;
- end = end % 360;
- // now, we find which octants we're drawing in.
- startoct = start / 45;
- endoct = end / 45;
- oct = startoct - 1; // we increment as first step in loop
- //stopval_start, stopval_end; // what values of cx to stop at.
- do
- {
- oct = (oct + 1) % 8;
- if (oct == startoct)
- {
- // need to compute stopval_start for this octant. Look at picture above if this is unclear
- switch (oct)
- {
- case 0:
- case 3:
- temp = sin(start * M_PI / 180);
- break;
- case 1:
- case 6:
- temp = cos(start * M_PI / 180);
- break;
- case 2:
- case 5:
- temp = -cos(start * M_PI / 180);
- break;
- case 4:
- case 7:
- temp = -sin(start * M_PI / 180);
- break;
- }
- temp *= r;
- stopval_start = (int)temp; // always round down.
- // This isn't arbitrary, but requires graph paper to explain well.
- // The basic idea is that we're always changing drawoct after we draw, so we
- // stop immediately after we render the last sensible pixel at x = ((int)temp).
- // and whether to draw in this octant initially
- if (oct % 2) drawoct |= (1 << oct); // this is basically like saying drawoct[oct] = true, if drawoct were a bool array
- else drawoct &= 255 - (1 << oct); // this is basically like saying drawoct[oct] = false
- }
- if (oct == endoct)
- {
- // need to compute stopval_end for this octant
- switch (oct)
- {
- case 0:
- case 3:
- temp = sin(end * M_PI / 180);
- break;
- case 1:
- case 6:
- temp = cos(end * M_PI / 180);
- break;
- case 2:
- case 5:
- temp = -cos(end * M_PI / 180);
- break;
- case 4:
- case 7:
- temp = -sin(end * M_PI / 180);
- break;
- }
- temp *= r;
- stopval_end = (int)temp;
- // and whether to draw in this octant initially
- if (startoct == endoct)
- {
- // note: we start drawing, stop, then start again in this case
- // otherwise: we only draw in this octant, so initialize it to false, it will get set back to true
- if (start > end)
- {
- // unfortunately, if we're in the same octant and need to draw over the whole circle,
- // we need to set the rest to true, because the while loop will end at the bottom.
- drawoct = 255;
- }
- else
- {
- drawoct &= 255 - (1 << oct);
- }
- }
- else if (oct % 2) drawoct &= 255 - (1 << oct);
- else drawoct |= (1 << oct);
- }
- else if (oct != startoct) // already verified that it's != endoct
- {
- drawoct |= (1 << oct); // draw this entire segment
- }
- }
- while (oct != endoct);
- // so now we have what octants to draw and when to draw them. all that's left is the actual raster code.
- do
- {
- ypcy = y + cy;
- ymcy = y - cy;
- if (cx > 0)
- {
- xpcx = x + cx;
- xmcx = x - cx;
- // always check if we're drawing a certain octant before adding a pixel to that octant.
- if (drawoct & 4) rtgui_dc_draw_point(dc, xmcx, ypcy); // drawoct & 4 = 22; drawoct[2]
- if (drawoct & 2) rtgui_dc_draw_point(dc, xpcx, ypcy);
- if (drawoct & 32) rtgui_dc_draw_point(dc, xmcx, ymcy);
- if (drawoct & 64) rtgui_dc_draw_point(dc, xpcx, ymcy);
- }
- else
- {
- if (drawoct & 6) rtgui_dc_draw_point(dc, x, ypcy); // 4 + 2; drawoct[2] || drawoct[1]
- if (drawoct & 96) rtgui_dc_draw_point(dc, x, ymcy); // 32 + 64
- }
- xpcy = x + cy;
- xmcy = x - cy;
- if (cx > 0 && cx != cy)
- {
- ypcx = y + cx;
- ymcx = y - cx;
- if (drawoct & 8) rtgui_dc_draw_point(dc, xmcy, ypcx);
- if (drawoct & 1) rtgui_dc_draw_point(dc, xpcy, ypcx);
- if (drawoct & 16) rtgui_dc_draw_point(dc, xmcy, ymcx);
- if (drawoct & 128) rtgui_dc_draw_point(dc, xpcy, ymcx);
- }
- else if (cx == 0)
- {
- if (drawoct & 24) rtgui_dc_draw_point(dc, xmcy, y); // 8 + 16
- if (drawoct & 129) rtgui_dc_draw_point(dc, xpcy, y); // 1 + 128
- }
- /*
- * Update whether we're drawing an octant
- */
- if (stopval_start == cx)
- {
- // works like an on-off switch because start & end may be in the same octant.
- if (drawoct & (1 << startoct)) drawoct &= 255 - (1 << startoct);
- else drawoct |= (1 << startoct);
- }
- if (stopval_end == cx)
- {
- if (drawoct & (1 << endoct)) drawoct &= 255 - (1 << endoct);
- else drawoct |= (1 << endoct);
- }
- /*
- * Update pixels
- */
- if (df < 0)
- {
- df += d_e;
- d_e += 2;
- d_se += 2;
- }
- else
- {
- df += d_se;
- d_e += 2;
- d_se += 4;
- cy--;
- }
- cx++;
- }
- while (cx <= cy);
- }
- RTM_EXPORT(rtgui_dc_draw_arc);
- 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)
- {
- rt_int16_t start_x, start_y;
- rt_int16_t end_x, end_y;
- double temp;
- rt_int16_t temp_val = 0;
- /* Sanity check radius */
- if ((r1 < 0) || (r2 < 0)) return ;
- /* Special case for r=0 - draw a point */
- if ((r1 == 0) && (r2 == 0))
- {
- rtgui_dc_draw_point(dc, x, y);
- return;
- }
- while (start < 0) start += 360;
- while (end < 0) end += 360;
- rtgui_dc_draw_arc(dc, x, y, r1, start, end);
- rtgui_dc_draw_arc(dc, x, y, r2, start, end);
- temp = cos(start * M_PI / 180);
- temp_val = (int)(temp * r1);
- start_x = x + temp_val;
- temp_val = (int)(temp * r2);
- end_x = x + temp_val;
- temp = sin(start * M_PI / 180);
- temp_val = (int)(temp * r1);
- start_y = y + temp_val;
- temp_val = (int)(temp * r2);
- end_y = y + temp_val;
- rtgui_dc_draw_line(dc, start_x, start_y, end_x, end_y);
- temp = cos(end * M_PI / 180);
- temp_val = (int)(temp * r1);
- start_x = x + temp_val;
- temp_val = (int)(temp * r2);
- end_x = x + temp_val;
- temp = sin(end * M_PI / 180);
- temp_val = (int)(temp * r1);
- start_y = y + temp_val;
- temp_val = (int)(temp * r2);
- end_y = y + temp_val;
- rtgui_dc_draw_line(dc, start_x, start_y, end_x, end_y);
- }
- RTM_EXPORT(rtgui_dc_draw_annulus);
- 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)
- {
- int ix, iy;
- int h, i, j, k;
- int oh, oi, oj, ok;
- int xmh, xph, ypk, ymk;
- int xmi, xpi, ymj, ypj;
- int xmj, xpj, ymi, ypi;
- int xmk, xpk, ymh, yph;
- /*
- * Sanity check radii
- */
- if ((rx < 0) || (ry < 0)) return;
- /*
- * Special case for rx=0 - draw a vline
- */
- if (rx == 0)
- {
- rtgui_dc_draw_vline(dc, x, y - ry, y + ry);
- return;
- }
- /*
- * Special case for ry=0 - draw a hline
- */
- if (ry == 0)
- {
- rtgui_dc_draw_hline(dc, x - rx, x + rx, y);
- return;
- }
- /*
- * Init vars
- */
- oh = oi = oj = ok = 0xFFFF;
- if (rx > ry)
- {
- ix = 0;
- iy = rx * 64;
- do
- {
- h = (ix + 32) >> 6;
- i = (iy + 32) >> 6;
- j = (h * ry) / rx;
- k = (i * ry) / rx;
- if (((ok != k) && (oj != k)) || ((oj != j) && (ok != j)) || (k != j))
- {
- xph = x + h;
- xmh = x - h;
- if (k > 0)
- {
- ypk = y + k;
- ymk = y - k;
- rtgui_dc_draw_point(dc, xmh, ypk);
- rtgui_dc_draw_point(dc, xph, ypk);
- rtgui_dc_draw_point(dc, xmh, ymk);
- rtgui_dc_draw_point(dc, xph, ymk);
- }
- else
- {
- rtgui_dc_draw_point(dc, xmh, y);
- rtgui_dc_draw_point(dc, xph, y);
- }
- ok = k;
- xpi = x + i;
- xmi = x - i;
- if (j > 0)
- {
- ypj = y + j;
- ymj = y - j;
- rtgui_dc_draw_point(dc, xmi, ypj);
- rtgui_dc_draw_point(dc, xpi, ypj);
- rtgui_dc_draw_point(dc, xmi, ymj);
- rtgui_dc_draw_point(dc, xpi, ymj);
- }
- else
- {
- rtgui_dc_draw_point(dc, xmi, y);
- rtgui_dc_draw_point(dc, xpi, y);
- }
- oj = j;
- }
- ix = ix + iy / rx;
- iy = iy - ix / rx;
- }
- while (i > h);
- }
- else
- {
- ix = 0;
- iy = ry * 64;
- do
- {
- h = (ix + 32) >> 6;
- i = (iy + 32) >> 6;
- j = (h * rx) / ry;
- k = (i * rx) / ry;
- if (((oi != i) && (oh != i)) || ((oh != h) && (oi != h) && (i != h)))
- {
- xmj = x - j;
- xpj = x + j;
- if (i > 0)
- {
- ypi = y + i;
- ymi = y - i;
- rtgui_dc_draw_point(dc, xmj, ypi);
- rtgui_dc_draw_point(dc, xpj, ypi);
- rtgui_dc_draw_point(dc, xmj, ymi);
- rtgui_dc_draw_point(dc, xpj, ymi);
- }
- else
- {
- rtgui_dc_draw_point(dc, xmj, y);
- rtgui_dc_draw_point(dc, xpj, y);
- }
- oi = i;
- xmk = x - k;
- xpk = x + k;
- if (h > 0)
- {
- yph = y + h;
- ymh = y - h;
- rtgui_dc_draw_point(dc, xmk, yph);
- rtgui_dc_draw_point(dc, xpk, yph);
- rtgui_dc_draw_point(dc, xmk, ymh);
- rtgui_dc_draw_point(dc, xpk, ymh);
- }
- else
- {
- rtgui_dc_draw_point(dc, xmk, y);
- rtgui_dc_draw_point(dc, xpk, y);
- }
- oh = h;
- }
- ix = ix + iy / ry;
- iy = iy - ix / ry;
- }
- while (i > h);
- }
- }
- RTM_EXPORT(rtgui_dc_draw_ellipse);
- 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)
- {
- int ix, iy;
- int h, i, j, k;
- int oh, oi, oj, ok;
- int xmh, xph;
- int xmi, xpi;
- int xmj, xpj;
- int xmk, xpk;
- /*
- * Special case for rx=0 - draw a vline
- */
- if (rx == 0)
- {
- rtgui_dc_draw_vline(dc, x, y - ry, y + ry);
- return;
- }
- /* special case for ry=0 - draw a hline */
- if (ry == 0)
- {
- rtgui_dc_draw_hline(dc, x - rx, x + rx, y);
- return;
- }
- /*
- * Init vars
- */
- oh = oi = oj = ok = 0xFFFF;
- /*
- * Draw
- */
- if (rx > ry)
- {
- ix = 0;
- iy = rx * 64;
- do
- {
- h = (ix + 32) >> 6;
- i = (iy + 32) >> 6;
- j = (h * ry) / rx;
- k = (i * ry) / rx;
- if ((ok != k) && (oj != k))
- {
- xph = x + h;
- xmh = x - h;
- if (k > 0)
- {
- rtgui_dc_draw_hline(dc, xmh, xph, y + k);
- rtgui_dc_draw_hline(dc, xmh, xph, y - k);
- }
- else
- {
- rtgui_dc_draw_hline(dc, xmh, xph, y);
- }
- ok = k;
- }
- if ((oj != j) && (ok != j) && (k != j))
- {
- xmi = x - i;
- xpi = x + i;
- if (j > 0)
- {
- rtgui_dc_draw_hline(dc, xmi, xpi, y + j);
- rtgui_dc_draw_hline(dc, xmi, xpi, y - j);
- }
- else
- {
- rtgui_dc_draw_hline(dc, xmi, xpi, y);
- }
- oj = j;
- }
- ix = ix + iy / rx;
- iy = iy - ix / rx;
- }
- while (i > h);
- }
- else
- {
- ix = 0;
- iy = ry * 64;
- do
- {
- h = (ix + 32) >> 6;
- i = (iy + 32) >> 6;
- j = (h * rx) / ry;
- k = (i * rx) / ry;
- if ((oi != i) && (oh != i))
- {
- xmj = x - j;
- xpj = x + j;
- if (i > 0)
- {
- rtgui_dc_draw_hline(dc, xmj, xpj, y + i);
- rtgui_dc_draw_hline(dc, xmj, xpj, y - i);
- }
- else
- {
- rtgui_dc_draw_hline(dc, xmj, xpj, y);
- }
- oi = i;
- }
- if ((oh != h) && (oi != h) && (i != h))
- {
- xmk = x - k;
- xpk = x + k;
- if (h > 0)
- {
- rtgui_dc_draw_hline(dc, xmk, xpk, y + h);
- rtgui_dc_draw_hline(dc, xmk, xpk, y - h);
- }
- else
- {
- rtgui_dc_draw_hline(dc, xmk, xpk, y);
- }
- oh = h;
- }
- ix = ix + iy / ry;
- iy = iy - ix / ry;
- }
- while (i > h);
- }
- }
- RTM_EXPORT(rtgui_dc_fill_ellipse);
- void rtgui_dc_draw_pie(struct rtgui_dc *dc, rt_int16_t x, rt_int16_t y, rt_int16_t rad, rt_int16_t start, rt_int16_t end)
- {
- double angle, start_angle, end_angle;
- double deltaAngle;
- double dr;
- int numpoints, i;
- int *vx, *vy;
- /* Sanity check radii */
- if (rad < 0) return ;
- /*
- * Fixup angles
- */
- start = start % 360;
- end = end % 360;
- /*
- * Special case for rad=0 - draw a point
- */
- if (rad == 0)
- {
- rtgui_dc_draw_point(dc, x, y);
- return;
- }
- /*
- * Variable setup
- */
- dr = (double) rad;
- deltaAngle = 3.0 / dr;
- start_angle = (double) start * (2.0 * M_PI / 360.0);
- end_angle = (double) end * (2.0 * M_PI / 360.0);
- if (start > end)
- {
- end_angle += (2.0 * M_PI);
- }
- /* We will always have at least 2 points */
- numpoints = 2;
- /* Count points (rather than calculating it) */
- angle = start_angle;
- while (angle < end_angle)
- {
- angle += deltaAngle;
- numpoints++;
- }
- /* Allocate combined vertex array */
- vx = vy = (int *) rtgui_malloc(2 * sizeof(int) * numpoints);
- if (vx == RT_NULL) return ;
- /* Update point to start of vy */
- vy += numpoints;
- /* Center */
- vx[0] = x;
- vy[0] = y;
- /* First vertex */
- angle = start_angle;
- vx[1] = x + (int)(dr * cos(angle));
- vy[1] = y + (int)(dr * sin(angle));
- if (numpoints < 3)
- {
- rtgui_dc_draw_line(dc, vx[0], vy[0], vx[1], vy[1]);
- }
- else
- {
- /* Calculate other vertices */
- i = 2;
- angle = start_angle;
- while (angle < end_angle)
- {
- angle += deltaAngle;
- if (angle > end_angle)
- {
- angle = end_angle;
- }
- vx[i] = x + (int)(dr * cos(angle));
- vy[i] = y + (int)(dr * sin(angle));
- i++;
- }
- /* Draw */
- rtgui_dc_draw_polygon(dc, vx, vy, numpoints);
- }
- /* Free combined vertex array */
- rtgui_free(vx);
- return;
- }
- RTM_EXPORT(rtgui_dc_draw_pie);
- /*
- * Octant labelling
- *
- * \ 5 | 6 /
- * \ | /
- * 4 \ | / 7
- * \|/
- *------+------ +x
- * /|\
- * 3 / | \ 0
- * / | \
- * / 2 | 1 \
- * +y
- */
- static void _draw_octant(struct rtgui_dc *dc,
- rt_int16_t ox, rt_int16_t oy,
- rt_int16_t y1, rt_int16_t y2, rt_int16_t x, int oct)
- {
- switch (oct % 8)
- {
- case 0:
- rtgui_dc_draw_line(dc, ox + x, oy + y1, ox + x, oy + y2);
- break;
- case 1:
- /* Ugly hack to get the edge right. */
- y2 += 1;
- y1 += 1;
- x -= 1;
- rtgui_dc_draw_line(dc, ox + y1, oy + x, ox + y2, oy + x);
- break;
- case 2:
- y2 -= 1;
- y1 -= 1;
- x -= 1;
- rtgui_dc_draw_line(dc, ox - y2, oy + x, ox - y1, oy + x);
- break;
- case 3:
- x -= 1;
- rtgui_dc_draw_line(dc, ox - x, oy + y1, ox - x, oy + y2);
- break;
- case 4:
- x -= 1;
- rtgui_dc_draw_line(dc, ox - x, oy - y2, ox - x, oy - y1);
- break;
- case 5:
- y2 -= 1;
- y1 -= 1;
- rtgui_dc_draw_line(dc, ox - y2, oy - x, ox - y1, oy - x);
- break;
- case 6:
- y2 += 1;
- y1 += 1;
- rtgui_dc_draw_line(dc, ox + y1, oy - x, ox + y2, oy - x);
- break;
- case 7:
- rtgui_dc_draw_line(dc, ox + x, oy - y2, ox + x, oy - y1);
- break;
- };
- }
- static void _fill_small_pie(struct rtgui_dc *dc,
- rt_int16_t ox, rt_int16_t oy,
- rt_int16_t rad, rt_int16_t start, rt_int16_t end,
- int oct)
- {
- /* Midpoint circle algorithm. */
- int dk, x, y;
- /* Start X, end X, */
- rt_int16_t sx, ex, ty, my;
- enum {ST_NONE, ST_ARC, ST_TRI} st;
- RT_ASSERT(0 <= start && start <= 45);
- RT_ASSERT(0 <= end && end <= 45);
- if (start == end)
- return;
- RT_ASSERT(start < end);
- RT_ASSERT(rad > 0);
- /* cos(90 - start) == sin(start) */
- sx = rad * sin(start * M_PI / 180);
- ex = rad * sin(end * M_PI / 180);
- dk = 1 - rad;
- x = 0;
- y = rad;
- st = ST_NONE;
- my = ex;
- while (x <= y)
- {
- rt_int16_t lx;
- if (x < sx)
- {
- }
- else if (x == sx)
- {
- /* Start point. */
- st = ST_ARC;
- ty = y;
- }
- else if (x <= ex)
- {
- /* Between the pie. */
- RT_ASSERT(st == ST_ARC);
- }
- else /* x > ex */
- {
- /* End. */
- st = ST_TRI;
- my = y;
- break;
- }
- /* Drawing. */
- if (st == ST_ARC)
- {
- lx = y * sx / ty;
- /* Change from math coordinate to plot coordinate. */
- _draw_octant(dc, ox, oy, lx, x, y, oct);
- }
- /* Midpoint increment. */
- if (dk > 0)
- {
- y--;
- dk += 2 * (x - y) + 5;
- }
- else
- {
- dk += 2 * x + 3;
- }
- x++;
- }
- /* Draw bottom part. */
- for (; y >= 0; y--)
- {
- rt_int16_t lx, rx;
- lx = y * sx / ty;
- rx = y * ex / my;
- _draw_octant(dc, ox, oy, lx, rx, y, oct);
- }
- }
- void rtgui_dc_fill_pie(struct rtgui_dc *dc,
- rt_int16_t x, rt_int16_t y, rt_int16_t rad,
- rt_int16_t start, rt_int16_t end)
- {
- /* Sanity check radii */
- if (rad < 0)
- return;
- if (rad == 0)
- {
- rtgui_dc_draw_point(dc, x, y);
- return;
- }
- if (end - start >= 360)
- {
- rtgui_dc_fill_circle(dc, x, y, rad);
- return;
- }
- if (start == end)
- return;
- /*
- * Fixup angles
- */
- while (start < 0)
- {
- start += 360;
- end += 360;
- }
- while (start >= 360)
- {
- start -= 360;
- end -= 360;
- }
- if (end < start)
- end += 360;
- while (start / 45 != end / 45)
- {
- /* The start and end are not in the same piece. */
- if ((start / 45) % 2)
- {
- _fill_small_pie(dc, x, y, rad,
- 0, 45 - start % 45, start / 45);
- start += 45 - start % 45;
- }
- else
- {
- _fill_small_pie(dc, x, y, rad,
- start % 45, 45, start / 45);
- start += 45 - start % 45;
- }
- }
- if ((start / 45) % 2)
- {
- _fill_small_pie(dc, x, y, rad,
- 90 - end % 90, 90 - start % 90, start / 45);
- }
- else
- {
- _fill_small_pie(dc, x, y, rad,
- start % 45, end % 45, start / 45);
- }
- return;
- }
- RTM_EXPORT(rtgui_dc_fill_pie);
- /*
- * set gc of dc
- */
- void rtgui_dc_set_gc(struct rtgui_dc *dc, rtgui_gc_t *gc)
- {
- RT_ASSERT(dc != RT_NULL);
- switch (dc->type)
- {
- case RTGUI_DC_CLIENT:
- {
- rtgui_widget_t *owner;
- /* get owner */
- owner = RTGUI_CONTAINER_OF(dc, struct rtgui_widget, dc_type);
- owner->gc = *gc;
- break;
- }
- case RTGUI_DC_HW:
- {
- struct rtgui_dc_hw *dc_hw;
- dc_hw = (struct rtgui_dc_hw *) dc;
- RT_ASSERT(dc_hw->owner != RT_NULL);
- dc_hw->owner->gc = *gc;
- break;
- }
- case RTGUI_DC_BUFFER:
- {
- struct rtgui_dc_buffer *dc_buffer;
- dc_buffer = (struct rtgui_dc_buffer *)dc;
- dc_buffer->gc = *gc;
- break;
- }
- }
- }
- RTM_EXPORT(rtgui_dc_set_gc);
- /*
- * get gc of dc
- */
- rtgui_gc_t *rtgui_dc_get_gc(struct rtgui_dc *dc)
- {
- rtgui_gc_t *gc = RT_NULL;
- RT_ASSERT(dc != RT_NULL);
- switch (dc->type)
- {
- case RTGUI_DC_CLIENT:
- {
- rtgui_widget_t *owner;
- /* get owner */
- owner = RTGUI_CONTAINER_OF(dc, struct rtgui_widget, dc_type);
- gc = &owner->gc;
- break;
- }
- case RTGUI_DC_HW:
- {
- struct rtgui_dc_hw *dc_hw;
- dc_hw = (struct rtgui_dc_hw *) dc;
- RT_ASSERT(dc_hw->owner != RT_NULL);
- gc = &dc_hw->owner->gc;
- break;
- }
- case RTGUI_DC_BUFFER:
- {
- struct rtgui_dc_buffer *dc_buffer;
- dc_buffer = (struct rtgui_dc_buffer *)dc;
- gc = &dc_buffer->gc;
- break;
- }
- }
- return gc;
- }
- RTM_EXPORT(rtgui_dc_get_gc);
- /*
- * get visible status of dc
- */
- rt_bool_t rtgui_dc_get_visible(struct rtgui_dc *dc)
- {
- rt_bool_t result = RT_TRUE;
- RT_ASSERT(dc != RT_NULL);
- if (rtgui_graphic_driver_is_vmode())
- return RT_TRUE;
- switch (dc->type)
- {
- case RTGUI_DC_CLIENT:
- {
- rtgui_widget_t *owner;
- /* get owner */
- owner = RTGUI_CONTAINER_OF(dc, struct rtgui_widget, dc_type);
- if (!RTGUI_WIDGET_IS_DC_VISIBLE(owner)) result = RT_FALSE;
- break;
- }
- case RTGUI_DC_HW:
- {
- struct rtgui_dc_hw *dc_hw;
- dc_hw = (struct rtgui_dc_hw *) dc;
- if (!RTGUI_WIDGET_IS_DC_VISIBLE(dc_hw->owner)) result = RT_FALSE;
- break;
- }
- default:
- /* use default value */
- break;
- }
- return result;
- }
- RTM_EXPORT(rtgui_dc_get_visible);
- /*
- * get rect of dc
- */
- void rtgui_dc_get_rect(struct rtgui_dc *dc, rtgui_rect_t *rect)
- {
- RT_ASSERT(dc != RT_NULL);
- switch (dc->type)
- {
- case RTGUI_DC_CLIENT:
- {
- rtgui_widget_t *owner;
- /* get owner */
- owner = RTGUI_CONTAINER_OF(dc, struct rtgui_widget, dc_type);
- /* we should return the clipped rectangular information */
- rect->x1 = owner->clip.extents.x1 - owner->extent.x1;
- rect->y1 = owner->clip.extents.y1 - owner->extent.y1;
- rect->x2 = rect->x1 + owner->clip.extents.x2 - owner->clip.extents.x1;
- rect->y2 = rect->y1 + owner->clip.extents.y2 - owner->clip.extents.y1;
- // rtgui_widget_get_rect(owner, rect);
- break;
- }
- case RTGUI_DC_HW:
- {
- rtgui_widget_t *owner;
- struct rtgui_dc_hw *dc_hw;
- dc_hw = (struct rtgui_dc_hw *) dc;
- owner = dc_hw->owner;
- rtgui_widget_get_rect(owner, rect);
- if (owner->extent.x1 + rect->x2 > dc_hw->hw_driver->width)
- rect->x2 = dc_hw->hw_driver->width - owner->extent.x1;
- if (owner->extent.y1 + rect->y2 > dc_hw->hw_driver->height)
- rect->y2 = dc_hw->hw_driver->height - owner->extent.y1;
- break;
- }
- case RTGUI_DC_BUFFER:
- {
- struct rtgui_dc_buffer *dc_buffer;
- dc_buffer = (struct rtgui_dc_buffer *)dc;
- rtgui_rect_init(rect, 0, 0, dc_buffer->width, dc_buffer->height);
- break;
- }
- }
- return;
- }
- RTM_EXPORT(rtgui_dc_get_rect);
- rt_uint8_t rtgui_dc_get_pixel_format(struct rtgui_dc *dc)
- {
- rt_uint8_t pixel_fmt;
- RT_ASSERT(dc != RT_NULL);
- switch (dc->type)
- {
- case RTGUI_DC_CLIENT:
- case RTGUI_DC_HW:
- {
- struct rtgui_graphic_driver *hw_driver;
- hw_driver = rtgui_graphic_driver_get_default();
- pixel_fmt = hw_driver->pixel_format;
- break;
- }
- case RTGUI_DC_BUFFER:
- {
- struct rtgui_dc_buffer *dc_buffer;
- dc_buffer = (struct rtgui_dc_buffer *)dc;
- pixel_fmt = dc_buffer->pixel_format;
- break;
- }
- default:
- RT_ASSERT(0);
- }
- return pixel_fmt;
- }
- RTM_EXPORT(rtgui_dc_get_pixel_format);
- void rtgui_dc_logic_to_device(struct rtgui_dc *dc, struct rtgui_point *point)
- {
- switch (dc->type)
- {
- case RTGUI_DC_CLIENT:
- {
- rtgui_widget_t *owner;
- /* get owner */
- owner = RTGUI_CONTAINER_OF(dc, struct rtgui_widget, dc_type);
- point->x += owner->extent.x1;
- point->y += owner->extent.y1;
- break;
- }
- case RTGUI_DC_HW:
- {
- rtgui_widget_t *owner;
- struct rtgui_dc_hw *dc_hw;
- dc_hw = (struct rtgui_dc_hw *) dc;
- owner = dc_hw->owner;
- point->x += owner->extent.x1;
- point->y += owner->extent.y1;
- break;
- }
- case RTGUI_DC_BUFFER: /* no conversion */
- break;
- }
- }
- RTM_EXPORT(rtgui_dc_logic_to_device);
- void rtgui_dc_rect_to_device(struct rtgui_dc *dc, struct rtgui_rect *rect)
- {
- switch (dc->type)
- {
- case RTGUI_DC_CLIENT:
- {
- rtgui_widget_t *owner;
- /* get owner */
- owner = RTGUI_CONTAINER_OF(dc, struct rtgui_widget, dc_type);
- rtgui_rect_move(rect, owner->extent.x1, owner->extent.y1);
- break;
- }
- case RTGUI_DC_HW:
- {
- rtgui_widget_t *owner;
- struct rtgui_dc_hw *dc_hw;
- dc_hw = (struct rtgui_dc_hw *) dc;
- owner = dc_hw->owner;
- rtgui_rect_move(rect, owner->extent.x1, owner->extent.y1);
- break;
- }
- case RTGUI_DC_BUFFER: /* no conversion */
- break;
- }
- }
- RTM_EXPORT(rtgui_dc_rect_to_device);
- extern struct rt_mutex cursor_mutex;
- extern void rtgui_mouse_show_cursor(void);
- extern void rtgui_mouse_hide_cursor(void);
- struct rtgui_dc *rtgui_dc_begin_drawing(rtgui_widget_t *owner)
- {
- struct rtgui_dc *dc;
- struct rtgui_widget *widget, *parent;
- struct rtgui_win *win;
- RT_ASSERT(owner != RT_NULL);
- win = owner->toplevel;
- if (win == RT_NULL)
- return RT_NULL;
- parent = (struct rtgui_widget *)win;
- if (!(win->flag & RTGUI_WIN_FLAG_ACTIVATE) &&
- (win->outer_clip.extents.x1 == win->outer_clip.extents.x2 ||
- win->outer_clip.extents.y1 == win->outer_clip.extents.y2))
- return RT_NULL;
- if (!(win->flag & RTGUI_WIN_FLAG_ACTIVATE) &&
- (parent->clip.extents.x1 == parent->clip.extents.x2 ||
- parent->clip.extents.y1 == parent->clip.extents.y2))
- return RT_NULL;
- /* increase drawing count */
- if (win->drawing == 0)
- {
- memset(&(win->drawing_rect), 0x0, sizeof(struct rtgui_rect));
- }
- win->drawing ++;
- /* always drawing on the virtual mode */
- if (rtgui_graphic_driver_is_vmode() == RT_FALSE)
- {
- /* set the initial visible as true */
- RTGUI_WIDGET_DC_SET_VISIBLE(owner);
- /* check the visible of widget */
- widget = owner;
- while (widget != RT_NULL)
- {
- if (RTGUI_WIDGET_IS_HIDE(widget))
- {
- RTGUI_WIDGET_DC_SET_UNVISIBLE(owner);
- win->drawing --;
- return RT_NULL;
- }
- widget = widget->parent;
- }
- }
- rtgui_screen_lock(RT_WAITING_FOREVER);
- /* create client or hardware DC */
- if ((rtgui_region_is_flat(&owner->clip) == RT_EOK) &&
- rtgui_rect_is_equal(&(owner->extent), &(owner->clip.extents)) == RT_EOK)
- dc = rtgui_dc_hw_create(owner);
- else
- dc = rtgui_dc_client_create(owner);
- if (dc == RT_NULL)
- {
- /* restore drawing counter */
- win->drawing--;
- rtgui_screen_unlock();
- }
- else if (win->drawing == 1 && rtgui_graphic_driver_is_vmode() == RT_FALSE)
- {
- #ifdef RTGUI_USING_MOUSE_CURSOR
- rt_mutex_take(&cursor_mutex, RT_WAITING_FOREVER);
- rtgui_mouse_hide_cursor();
- #endif
- if (! RTGUI_IS_WINTITLE(win))
- {
- /* send draw begin to server */
- struct rtgui_event_update_begin eupdate;
- RTGUI_EVENT_UPDATE_BEGIN_INIT(&(eupdate));
- eupdate.rect = RTGUI_WIDGET(win)->extent;
- rtgui_server_post_event((struct rtgui_event *)&eupdate, sizeof(eupdate));
- }
- }
- return dc;
- }
- RTM_EXPORT(rtgui_dc_begin_drawing);
- void rtgui_dc_end_drawing(struct rtgui_dc *dc, rt_bool_t update)
- {
- struct rtgui_widget *owner;
- struct rtgui_win *win;
- RT_ASSERT(dc != RT_NULL);
- /* get owner */
- if (dc->type == RTGUI_DC_CLIENT)
- owner = RTGUI_CONTAINER_OF(dc, struct rtgui_widget, dc_type);
- else if (dc->type == RTGUI_DC_HW)
- owner = ((struct rtgui_dc_hw *)dc)->owner;
- else return ; /* bad DC type */
- /* get window */
- win = owner->toplevel;
- /* union drawing rect */
- rtgui_rect_union(&(owner->extent_visiable), &(win->drawing_rect));
- /* decrease drawing counter */
- win->drawing--;
- if (win->drawing == 0)
- {
- /* notify window to handle window update done */
- if (RTGUI_OBJECT(win)->event_handler)
- {
- struct rtgui_event_win_update_end ewin_update;
- RTGUI_EVENT_WIN_UPDATE_END_INIT(&(ewin_update));
- ewin_update.rect = win->drawing_rect;
- RTGUI_OBJECT(win)->event_handler(RTGUI_OBJECT(win), (struct rtgui_event *)&ewin_update);
- }
- if (rtgui_graphic_driver_is_vmode() == RT_FALSE && win->update == 0 && update)
- {
- #ifdef RTGUI_USING_MOUSE_CURSOR
- rt_mutex_release(&cursor_mutex);
- /* show cursor */
- rtgui_mouse_show_cursor();
- #endif
- if (RTGUI_IS_WINTITLE(win))
- {
- /* update screen */
- rtgui_graphic_driver_screen_update(rtgui_graphic_driver_get_default(),
- &(owner->extent));
- }
- else
- {
- /* send to server for window update */
- struct rtgui_event_update_end eupdate;
- RTGUI_EVENT_UPDATE_END_INIT(&(eupdate));
- eupdate.rect = owner->extent;
- rtgui_server_post_event((struct rtgui_event *)&eupdate, sizeof(eupdate));
- }
- }
- }
- dc->engine->fini(dc);
- rtgui_screen_unlock();
- }
- RTM_EXPORT(rtgui_dc_end_drawing);
|