1 --[[TEXT WIDGET v1.. by Wlourf 25/06/2010
2 This widget can drawn texts set in the "text_settings" table with some parameters
3 http://u-scripts.blogspot.com/2010/06/text-widget.html
5 The parameters (all optionals) are :
6 text - text to display, default = "Conky is good for you"
7 use conky_parse to display conky value ie text=conly_parse("${cpu cpu1}")
8 - coordinates below are relative to top left corner of the conky window
9 x - x coordinate of first letter (bottom-left), default = center of conky window
10 y - y coordinate of first letter (bottom-left), default = center of conky window
11 h_align - horizontal alignement of text relative to point (x,y), default="l"
12 available values are "l": left, "c" : center, "r" : right
13 v_align - vertical alignment of text relative to point (x,y), default="b"
14 available values "t" : top, "m" : middle, "b" : bottom
15 font_name - name of font to use, default = Free Sans
16 font_size - size of font to use, default = 14
17 italic - display text in italic (true/false), default=false
18 oblique - display text in oblique (true/false), default=false (I don' see the difference with italic!)
19 bold - display text in bold (true/false), default=false
20 angle - rotation of text in degrees, default = 0 (horizontal)
21 colour - table of colours for text, default = plain white {{1,0xFFFFFF,1}}
22 this table contains one or more tables with format {P,C,A}
23 P=position of gradient (0 = beginning of text, 1= end of text)
25 A=alpha (opacity) of color (0=invisible,1=opacity 100%)
27 for a plain color {{1,0x00FF00,0.5}}
28 for a gradient with two colours {{0,0x00FF00,0.5},{1,0x000033,1}}
29 or {{0.5,0x00FF00,1},{1,0x000033,1}} -with this one, gradient will start in the middle of the text
30 for a gradient with three colours {{0,0x00FF00,0.5},{0.5,0x000033,1},{1,0x440033,1}}
32 orientation - in case of gradient, "orientation" defines the starting point of the gradient, default="ww"
33 there are 8 available starting points : "nw","nn","ne","ee","se","ss","sw","ww"
34 (n for north, w for west ...)
35 theses 8 points are the 4 corners + the 4 middles of text's outline
36 so a gradient "nn" will go from "nn" to "ss" (top to bottom, parallele to text)
37 a gradient "nw" will go from "nw" to "se" (left-top corner to right-bottom corner)
38 radial - define a radial gradient (if present at the same time as "orientation", "orientation" will have no effect)
39 this parameter is a table with 6 numbers : {xa,ya,ra,xb,yb,rb}
40 they define two circle for the gradient :
41 xa, ya, xb and yb are relative to x and y values above
42 reflection_alpha - add a reflection effect (values from 0 to 1) default = 0 = no reflection
43 other values = starting opacity
44 reflection_scale - scale of the reflection (default = 1 = height of text)
45 reflection_length - length of reflection, define where the opacity will be set to zero
46 calues from 0 to 1, default =1
47 skew_x,skew_y - skew text around x or y axis
52 To call this script in the conkyrc, in before-TEXT section:
53 lua_load /path/to/the/lua/script/text.lua
54 lua_draw_hook_pre draw_text
56 v1.0 07/06/2010, Original release
57 v1.1 10/06/2010 Add "orientation" parameter
58 v1.2 15/06/2010 Add "h_align", "v_align" and "radial" parameters
59 v1.3 25/06/2010 Add "reflection_alpha", "reflection_length", "reflection_scale",
67 function conky_draw_text()
68 local col0,col1,col2=0xFFFFCC,0xCCFF99,0x99FF00
72 text=conky_parse("${time %I:%M%p %D}"),
75 font_name="Nokia Pure",
87 radial={400,-700,0,100,-1000,1024}
90 text=conky_parse("$sysname $kernel on $machine - $uptime"),
93 font_name="Nokia Pure",
99 colour={{0,col0,0.75},{1,colbg,0.75}},
102 text="Conky v"..conky_parse("$conky_version").." - Lance Colton",
105 font_name="Nokia Pure",
111 colour={{0,col0,0.75},{1,colbg,0.75}},
114 text=conky_parse("$freq MHz"),
115 font_name="Nokia Pure",
124 colour={{0,col0,1},{0.5,colbg,1}},
128 text=conky_parse("${battery_temp}").."\194\176",--yes i had to do the degree symbol like that...
133 font_name="Nokia Pure",
135 colour={{0,col0,1},{0.5,colbg,1}},
141 text=conky_parse("${battery_rate}mA"),
146 font_name="Nokia Pure",
148 colour={{0,col0,1},{0.5,colbg,1}},
154 text=conky_parse("${battery_short} ${battery_volts}mV"),
159 font_name="Nokia Pure",
161 colour={{0,col0,1},{0.5,colbg,1}},
167 text=conky_parse("${cell_radio_dbm}".."dBm"),
170 font_name="Nokia Pure",
172 colour={{0,col0,1},{0.5,colbg,1}},
174 reflection_alpha=0.0,
175 reflection_length=0.0,
178 text=conky_parse('${cpu}').."%",
183 font_name="Nokia Pure",
185 colour={{0,col0,1},{0.5,colbg,1}},
191 text=conky_parse('${memperc}').."% RAM",
195 font_name="Nokia Pure",
197 colour={{0,col0,1},{0.5,colbg,1}},
203 text=conky_parse('${fs_used /} / ${fs_size /} (${fs_free /})'),
207 font_name="Nokia Pure",
209 colour={{conky_parse('${fs_used_perc /}')/100,0x000000,1},{1,0xFFFFFF,1}},
219 font_name="Nokia Pure",
221 colour={{0,0x336633,1},{1,col0,1}},
228 text=conky_parse('${fs_used /home} / ${fs_size /home} (${fs_free /home})'),
232 font_name="Nokia Pure",
234 colour={{conky_parse('${fs_used_perc /home}')/100,0x000000,1},{1,0xFFFFFF,1}},
244 font_name="Nokia Pure",
246 colour={{0,0x336633,1},{1,col0,1}},
253 text=conky_parse('${fs_used /home/user/MyDocs} / ${fs_size /home/user/MyDocs} (${fs_free /home/user/MyDocs})'),
257 font_name="Nokia Pure",
259 colour={{conky_parse('${fs_used_perc /home/user/MyDocs}')/100-.1,0x000000,1},{1,0xFFFFFF,1}},
263 DrawMe=conky_parse("${if_mounted /home/user/MyDocs}1$endif")
270 font_name="Nokia Pure",
272 colour={{0,0x336633,1},{1,col0,1}},
277 DrawMe=conky_parse("${if_mounted /home/user/MyDocs}1$endif")
284 font_name="Nokia Pure",
286 colour={{0,0xcc6600,1},{1,col0,1}},
297 font_name="Nokia Pure",
299 colour={{0,0xFF0000,1},{1,col0,1}},
307 text=conky_parse('${fs_used /media/mmc1} / ${fs_size /media/mmc1} (${fs_free /media/mmc1})'),
311 font_name="Nokia Pure",
313 colour={{conky_parse('${fs_used_perc /media/mmc1}')/100-.1,0x000000,1},{1,0xFFFFFF,1}},
317 DrawMe=conky_parse("${if_mounted /media/mmc1}1$endif")
324 font_name="Nokia Pure",
326 colour={{0,0x336633,1},{1,col0,1}},
331 DrawMe=conky_parse("${if_mounted /media/mmc1}1$endif"),
335 text=conky_parse('${wireless_essid wlan0}'),
340 font_name="Nokia Pure",
342 colour={{0,col0,1},{0.5,colbg,1}},
346 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
354 font_name="Nokia Pure",
356 colour={{0,col0,1},{0.5,colbg,1}},
360 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
363 text=conky_parse('${wireless_link_qual_perc wlan0}')..'%',
368 font_name="Nokia Pure",
370 colour={{0,col0,1},{0.5,colbg,1}},
374 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
377 text='Up '..conky_parse('${upspeedf wlan0}') + conky_parse('${upspeedf gprs0}'),
385 colour={{0,0xcc0066,1}},
389 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
392 text=conky_parse('${downspeedf wlan0}') + conky_parse('${downspeedf gprs0}')..' Down',
400 colour={{0,0xcc0066,1}},
404 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
407 text='Up '..conky_parse('${upspeed gprs0}'),
415 colour={{0,0xcc0066,1}},
419 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
422 text=conky_parse('${downspeed gprs0}')..' Down',
430 colour={{0,0xcc0066,1}},
434 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
437 if conky_window == nil then return end
438 if tonumber(conky_parse("$updates"))<3 then return end
439 local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
440 for i,v in pairs(text_settings) do
441 cr = cairo_create (cs)
446 cairo_surface_destroy(cs)
449 function rgb_to_r_g_b2(tcolour)
450 local colour,alpha=tcolour[2],tcolour[3]
451 return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
454 function display_text(t)
455 if t.DrawMe~=nil and t.DrawMe ~= "1" then return end
456 local function set_pattern(te)
457 --this function set the pattern
459 cairo_set_source_rgba(cr,rgb_to_r_g_b2(t.colour[1]))
462 if t.radial==nil then
463 local pts=linear_orientation(t,te)
464 pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
466 pat = cairo_pattern_create_radial (t.radial[1],t.radial[2],t.radial[3],t.radial[4],t.radial[5],t.radial[6])
468 for i=1, #t.colour do
469 cairo_pattern_add_color_stop_rgba (pat, t.colour[i][1], rgb_to_r_g_b2(t.colour[i]))
471 cairo_set_source (cr, pat)
472 cairo_pattern_destroy(pat)
476 --set default values if needed
477 if t.text==nil then t.text="Conky is good for you !" end
478 if t.x==nil then t.x = conky_window.width/2 end
479 if t.y==nil then t.y = conky_window.height/2 end
480 if t.colour==nil then t.colour={{1,0xFFFFFF,1}} end
481 if t.font_name==nil then t.font_name="Free Sans" end
482 if t.font_size==nil then t.font_size=14 end
483 if t.angle==nil then t.angle=0 end
484 if t.italic==nil then t.italic=false end
485 if t.oblique==nil then t.oblique=false end
486 if t.bold==nil then t.bold=false end
487 if t.radial ~= nil then
489 print ("error in radial table")
493 if t.orientation==nil then t.orientation="ww" end
494 if t.h_align==nil then t.h_align="l" end
495 if t.v_align==nil then t.v_align="b" end
496 if t.reflection_alpha == nil then t.reflection_alpha=0 end
497 if t.reflection_length == nil then t.reflection_length=1 end
498 if t.reflection_scale == nil then t.reflection_scale=1 end
499 if t.rotx==nil then t.rotx=0 end
500 if t.roty==nil then t.roty=0 end
501 cairo_translate(cr,t.x,t.y)
502 cairo_rotate(cr,t.angle*math.pi/180)
504 local slant = CAIRO_FONT_SLANT_NORMAL
505 local weight = CAIRO_FONT_WEIGHT_NORMAL
506 if t.italic then slant = CAIRO_FONT_SLANT_ITALIC end
507 if t.oblique then slant = CAIRO_FONT_SLANT_OBLIQUE end
508 if t.bold then weight = CAIRO_FONT_WEIGHT_BOLD end
509 cairo_select_font_face(cr, t.font_name, slant,weight)
510 for i=1, #t.colour do
511 if #t.colour[i]~=3 then
512 print ("error in color table")
513 t.colour[i]={1,0xFFFFFF,1}
516 local matrix0 = cairo_matrix_t:create()
517 tolua.takeownership(matrix0)
518 local rotx,roty=t.rotx/t.font_size,t.roty/t.font_size
519 cairo_matrix_init (matrix0, 1,roty,rotx,1,0,0)
520 cairo_transform(cr,matrix0)
521 cairo_set_font_size(cr,t.font_size)
522 local te=cairo_text_extents_t:create()
523 tolua.takeownership(te)
524 cairo_text_extents (cr,t.text,te)
527 if t.h_align=="c" then
529 elseif t.h_align=="r" then
532 if t.v_align=="m" then
533 my=-te.height/2-te.y_bearing
534 elseif t.v_align=="t" then
537 cairo_move_to(cr,mx,my)
538 cairo_show_text(cr,t.text)
540 if t.reflection_alpha ~= 0 then
541 local matrix1 = cairo_matrix_t:create()
542 tolua.takeownership(matrix1)
543 cairo_set_font_size(cr,t.font_size)
544 cairo_matrix_init (matrix1,1,0,0,-1*t.reflection_scale,0,(te.height+te.y_bearing+my)*(1+t.reflection_scale))
545 cairo_set_font_size(cr,t.font_size)
547 local te=cairo_text_extents_t:create()
548 tolua.takeownership(te)
549 cairo_text_extents (cr,t.text,te)
550 cairo_transform(cr,matrix1)
552 cairo_move_to(cr,mx,my)
553 cairo_show_text(cr,t.text)
554 local pat2 = cairo_pattern_create_linear (0,
555 (te.y_bearing+te.height+my),
558 cairo_pattern_add_color_stop_rgba (pat2, 0,1,0,0,1-t.reflection_alpha)
559 cairo_pattern_add_color_stop_rgba (pat2, t.reflection_length,0,0,0,1)
560 cairo_set_line_width(cr,0)
561 local dy=te.x_bearing
562 if dy<0 then dy=dy*(-1) end
563 cairo_rectangle(cr,mx+te.x_bearing,te.y_bearing+te.height+my,te.width+dy,-te.height*1.05)
564 cairo_clip_preserve(cr)
565 cairo_set_operator(cr,CAIRO_OPERATOR_CLEAR)
568 cairo_pattern_destroy(pat2)
569 cairo_set_operator(cr,CAIRO_OPERATOR_OVER)
575 function linear_orientation(t,te)
576 local w,h=te.width,te.height
577 local xb,yb=te.x_bearing,te.y_bearing
579 if t.h_align=="c" then
581 elseif t.h_align=="r" then
584 if t.v_align=="m" then
586 elseif t.v_align=="t" then
590 if t.orientation=="nn" then
591 p={xb+w/2,yb,xb+w/2,yb+h}
592 elseif t.orientation=="ne" then
594 elseif t.orientation=="ww" then
596 elseif vorientation=="se" then
598 elseif t.orientation=="ss" then
599 p={xb+w/2,yb+h,xb+w/2,yb}
600 elseif vorientation=="ee" then
602 elseif t.orientation=="sw" then
604 elseif t.orientation=="nw" then