nothing interesting
[monky] / lua_scripts / text.lua
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
4
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)
24               C=hexadecimal colour 
25               A=alpha (opacity) of color (0=invisible,1=opacity 100%)
26               Examples :
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}}
31                           and so on ...
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
48                           
49
50 Needs conky 1.8.0 
51
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
55  
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", 
60                     "skew_x" et "skew_y"
61
62
63 ]]
64
65 require 'cairo'
66
67 function conky_draw_text()
68         local col0,col1,col2=0xFFFFCC,0xCCFF99,0x99FF00
69         local colbg=0x99CCFF
70     local text_settings={
71                 {
72                                 text=conky_parse("${time %I:%M%p %D}"),
73                                 font_size=24,
74                                 bold=true,
75                                 font_name="Droid Sans Mono",
76                                 h_align="l",
77                                 v_align="t",
78                                 x=5,
79                                 y=5,
80                                 reflection_alpha=0,
81                                 reflection_length=0,
82  colour={
83          {0.98,    0xFF0000,1},
84          {0.99,    0xFFCC00,1},
85          {1.00,    0xFF0000,1},
86          },
87  radial={400,-700,0,100,-1000,1024}
88                 }, 
89                 {
90                                 text=conky_parse("$sysname $kernel on $machine - $uptime"),
91                                 font_size=16,
92                                 bold=true,
93                                 font_name="Droid Sans Mono",
94                                 h_align="r",
95                                 x=800,
96                                 y=15,
97                                 reflection_alpha=0,
98                                 reflection_length=0,
99                                 colour={{0,col0,0.75},{1,colbg,0.75}},
100
101                 }, 
102                 {
103                                 text=conky_parse("$freq MHz"),
104                                 font_name="Droid Sans Mono",
105                                 font_size=16,
106                                 h_align="l",
107                                 v_align="t",
108                                 bold=true,
109                                 x=20,
110                                 y=60,
111                                 reflection_alpha=0,
112                                 reflection_length=0,
113                                 colour={{0,col0,1},{0.5,colbg,1}},
114                                 orientation="nn",
115                 },
116                 {
117                     text=conky_parse("${battery_temp}").."\194\176",--yes i had to do the degree symbol like that...
118                         x=794,
119                     y=69,
120                     v_align="m",
121                     h_align="r",
122                     font_name="Droid Sans Mono",
123                     font_size=14,
124                         colour={{0,col0,1},{0.5,colbg,1}},
125                                 orientation="nn",
126                 reflection_alpha=0,
127                 reflection_length=0,                    
128         }, 
129         {
130                     text=conky_parse("${battery_short} ${battery_volts}mV"),
131                         x=794,
132                     y=45,
133                     v_align="m",
134                     h_align="r",
135                     font_name="Droid Sans Mono",
136                     font_size=14,
137                         colour={{0,col0,1},{0.5,colbg,1}},
138                                 orientation="nn",
139                 reflection_alpha=0,
140                 reflection_length=0,                    
141         }, 
142         {
143                     text=conky_parse("${cell_radio_dbm}".."dBm"),
144                         x=510,
145                     y=50,
146                     font_name="Droid Sans Mono",
147                     font_size=14,
148                         colour={{0,col0,1},{0.5,colbg,1}},
149                                 orientation="nn",
150                 reflection_alpha=0.0,
151                 reflection_length=0.0,                  
152         },
153         {
154                     text=conky_parse('${cpu}').."%",
155                         x=223,
156                     y=65,
157                     v_align="t",
158                     h_align="l",
159                     font_name="Droid Sans Mono",
160                     font_size=18,
161                         colour={{0,col0,1},{0.5,colbg,1}},
162                                 orientation="ww",
163                 reflection_alpha=0,
164                 reflection_length=0,                    
165         },  
166         {
167                     text=conky_parse('${memperc}').."%",
168                     x=223,
169                     y=91,
170                     v_align="t",
171                     font_name="Droid Sans Mono",
172                     font_size=18,
173                         colour={{0,col0,1},{0.5,colbg,1}},
174                                 orientation="nn",
175                                 reflection_alpha=0,
176                                 reflection_length=0,
177         },
178         {
179                     text=conky_parse('${fs_used /} / ${fs_size /} (${fs_free /})'),
180                     x=5,
181                     y=115,
182                     v_align="t",
183                     font_name="Droid Sans Mono",
184                     font_size=16,
185                         colour={{conky_parse('${fs_used_perc /}')/100,0x000000,1},{1,0xFFFFFF,1}},
186                                 orientation="ww",
187                                 reflection_alpha=0,
188                                 reflection_length=0,
189         },
190         {
191                     text="/",
192                     x=220,
193                     y=114,
194                     v_align="t",
195                     font_name="Droid Sans Mono",
196                     font_size=18,
197                         colour={{0,0x336633,1},{1,col0,1}},
198                         bold=true,
199                                 orientation="ww",
200                                 reflection_alpha=0,
201                                 reflection_length=0,
202         },
203         {
204                     text=conky_parse('${fs_used /home} / ${fs_size /home} (${fs_free /home})'),
205                     x=5,
206                     y=135,
207                     v_align="t",
208                     font_name="Droid Sans Mono",
209                     font_size=16,
210                         colour={{conky_parse('${fs_used_perc /home}')/100,0x000000,1},{1,0xFFFFFF,1}},
211                                 orientation="ww",
212                                 reflection_alpha=0,
213                                 reflection_length=0,
214         },
215         {
216                     text="/home",
217                     x=220,
218                     y=134,
219                     v_align="t",
220                     font_name="Droid Sans Mono",
221                     font_size=18,
222                         colour={{0,0x336633,1},{1,col0,1}},
223                         bold=true,
224                                 orientation="ww",
225                                 reflection_alpha=0,
226                                 reflection_length=0,
227         },
228         {
229                     text=conky_parse('${fs_used /home/user/MyDocs} / ${fs_size /home/user/MyDocs} (${fs_free /home/user/MyDocs})'),
230                     x=5,
231                     y=155,
232                     v_align="t",
233                     font_name="Droid Sans Mono",
234                     font_size=16,
235                         colour={{conky_parse('${fs_used_perc /home/user/MyDocs}')/100-.1,0x000000,1},{1,0xFFFFFF,1}},
236                                 orientation="ww",
237                                 reflection_alpha=0,
238                                 reflection_length=0,
239                                 DrawMe=conky_parse("${if_mounted /home/user/MyDocs}1$endif")
240         },
241         {
242                     text="MyDocs",
243                     x=220,
244                     y=154,
245                     v_align="t",
246                     font_name="Droid Sans Mono",
247                     font_size=18,
248                         colour={{0,0x336633,1},{1,col0,1}},
249                         bold=true,
250                                 orientation="ww",
251                                 reflection_alpha=0,
252                                 reflection_length=0,
253                                 DrawMe=conky_parse("${if_mounted /home/user/MyDocs}1$endif")
254         },
255         {
256                     text=conky_parse('${fs_used /media/mmc1} / ${fs_size /media/mmc1} (${fs_free /media/mmc1})'),
257                     x=5,
258                     y=175,
259                     v_align="t",
260                     font_name="Droid Sans Mono",
261                     font_size=16,
262                         colour={{conky_parse('${fs_used_perc /media/mmc1}')/100-.1,0x000000,1},{1,0xFFFFFF,1}},
263                                 orientation="ww",
264                                 reflection_alpha=0,
265                                 reflection_length=0,
266                                 DrawMe=conky_parse("${if_mounted /media/mmc1}1$endif")
267         },
268         {
269                     text="SDCard",
270                     x=220,
271                     y=174,
272                     v_align="t",
273                     font_name="Droid Sans Mono",
274                     font_size=18,
275                         colour={{0,0x336633,1},{1,col0,1}},
276                         bold=true,
277                                 orientation="ww",
278                                 reflection_alpha=0,
279                                 reflection_length=0,
280                                 DrawMe=conky_parse("${if_mounted /media/mmc1}1$endif"),
281         },
282         {
283                     text=conky_parse('${wireless_essid wlan0}'),
284                         x=400,
285                     y=201,
286                     h_align="c",
287                     v_align="m",
288                     font_name="Droid Sans Mono",
289                     font_size=18,
290                         colour={{0,col0,1},{0.5,colbg,1}},
291                                 orientation="nn",
292                 reflection_alpha=0,
293                 reflection_length=0,
294                 DrawMe=conky_parse("${if_empty ${wireless_essid wlan0}}${else}1$endif"),
295         },
296         {
297                     text="GPRS",
298                         x=400,
299                     y=201,
300                     h_align="c",
301                     v_align="m",
302                     font_name="Droid Sans Mono",
303                     font_size=18,
304                         colour={{0,col0,1},{0.5,colbg,1}},
305                                 orientation="nn",
306                 reflection_alpha=0,
307                 reflection_length=0,
308                 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
309         },
310         {
311                     text=conky_parse('${wireless_link_qual_perc wlan0}')..'%',
312                         x=400,
313                     y=228,
314                     h_align="c",
315                     v_align="l",
316                     font_name="Droid Sans Mono",
317                     font_size=14,
318                         colour={{0,col0,1},{0.5,colbg,1}},
319                                 orientation="nn",
320                 reflection_alpha=0,
321                 reflection_length=0,
322                 DrawMe=conky_parse("${if_empty ${wireless_essid wlan0}}${else}1$endif"),
323         },
324         {
325                     text='Up '..conky_parse('${upspeed wlan0}'),
326                         x=225,
327                     y=223,
328                     h_align="l",
329                     v_align="m",
330                     font_name="LEDFont",
331                     font_size=22,
332                     bold=true,
333                         colour={{0,0xcc0066,1}},
334                                 orientation="ww",
335                 reflection_alpha=0,
336                 reflection_length=0,
337                 DrawMe=conky_parse("${if_empty ${wireless_essid wlan0}}${else}1$endif"),
338         },
339         {
340                     text=conky_parse('${downspeed wlan0}')..' Down',
341                         x=650,
342                     y=223,
343                     h_align="r",
344                     v_align="m",
345                     font_name="LEDFont",
346                     font_size=22,
347                     bold=true,
348                         colour={{0,0xcc0066,1}},
349                                 orientation="ww",
350                 reflection_alpha=0,
351                 reflection_length=0,
352                 DrawMe=conky_parse("${if_empty ${wireless_essid wlan0}}${else}1$endif"),
353         },
354         {
355                     text='Up '..conky_parse('${upspeed gprs0}'),
356                         x=225,
357                     y=223,
358                     h_align="l",
359                     v_align="m",
360                     font_name="LEDFont",
361                     font_size=22,
362                     bold=true,
363                         colour={{0,0xcc0066,1}},
364                                 orientation="ww",
365                 reflection_alpha=0,
366                 reflection_length=0,
367                 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
368         },
369         {
370                     text=conky_parse('${downspeed gprs0}')..' Down',
371                         x=650,
372                     y=223,
373                     h_align="r",
374                     v_align="m",
375                     font_name="LEDFont",
376                     font_size=22,
377                     bold=true,
378                         colour={{0,0xcc0066,1}},
379                                 orientation="ww",
380                 reflection_alpha=0,
381                 reflection_length=0,
382                 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
383         },  
384     }
385     if conky_window == nil then return end
386     if tonumber(conky_parse("$updates"))<3 then return end
387         local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
388     for i,v in pairs(text_settings) do  
389         cr = cairo_create (cs)
390                         display_text(v)
391             cairo_destroy(cr)
392             cr = nil
393     end
394         cairo_surface_destroy(cs)
395 end
396
397 function rgb_to_r_g_b2(tcolour)
398     local colour,alpha=tcolour[2],tcolour[3]
399     return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
400 end
401
402 function display_text(t)
403         if t.DrawMe~=nil and t.DrawMe ~= "1" then return end
404         local function set_pattern(te)
405                 --this function set the pattern
406                 if #t.colour==1 then 
407                     cairo_set_source_rgba(cr,rgb_to_r_g_b2(t.colour[1]))
408                 else
409                         local pat
410                         if t.radial==nil then
411                                 local pts=linear_orientation(t,te)
412                                 pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
413                         else
414                                 pat = cairo_pattern_create_radial (t.radial[1],t.radial[2],t.radial[3],t.radial[4],t.radial[5],t.radial[6])
415                         end
416                     for i=1, #t.colour do
417                         cairo_pattern_add_color_stop_rgba (pat, t.colour[i][1], rgb_to_r_g_b2(t.colour[i]))
418                     end
419                     cairo_set_source (cr, pat)
420                         cairo_pattern_destroy(pat)
421                 end
422     end
423     
424     --set default values if needed
425     if t.text==nil then t.text="Conky is good for you !" end
426     if t.x==nil then t.x = conky_window.width/2 end
427     if t.y==nil then t.y = conky_window.height/2 end
428     if t.colour==nil then t.colour={{1,0xFFFFFF,1}} end
429     if t.font_name==nil then t.font_name="Free Sans" end
430     if t.font_size==nil then t.font_size=14 end
431     if t.angle==nil then t.angle=0 end
432     if t.italic==nil then t.italic=false end
433     if t.oblique==nil then t.oblique=false end
434     if t.bold==nil then t.bold=false end
435     if t.radial ~= nil then
436         if #t.radial~=6 then 
437                 print ("error in radial table")
438                 t.radial=nil 
439         end
440     end
441     if t.orientation==nil then t.orientation="ww" end
442     if t.h_align==nil then t.h_align="l" end
443     if t.v_align==nil then t.v_align="b" end    
444     if t.reflection_alpha == nil then t.reflection_alpha=0 end
445     if t.reflection_length == nil then t.reflection_length=1 end
446     if t.reflection_scale == nil then t.reflection_scale=1 end
447     if t.rotx==nil then t.rotx=0 end
448     if t.roty==nil then t.roty=0 end    
449     cairo_translate(cr,t.x,t.y)
450     cairo_rotate(cr,t.angle*math.pi/180)
451     cairo_save(cr)       
452     local slant = CAIRO_FONT_SLANT_NORMAL
453     local weight = CAIRO_FONT_WEIGHT_NORMAL
454     if t.italic then slant = CAIRO_FONT_SLANT_ITALIC end
455     if t.oblique then slant = CAIRO_FONT_SLANT_OBLIQUE end
456     if t.bold then weight = CAIRO_FONT_WEIGHT_BOLD end
457     cairo_select_font_face(cr, t.font_name, slant,weight)
458     for i=1, #t.colour do    
459         if #t.colour[i]~=3 then 
460                 print ("error in color table")
461                 t.colour[i]={1,0xFFFFFF,1} 
462         end
463     end
464         local matrix0 = cairo_matrix_t:create()
465         tolua.takeownership(matrix0) 
466         local rotx,roty=t.rotx/t.font_size,t.roty/t.font_size
467         cairo_matrix_init (matrix0, 1,roty,rotx,1,0,0)
468         cairo_transform(cr,matrix0)
469         cairo_set_font_size(cr,t.font_size)
470         local te=cairo_text_extents_t:create()
471         tolua.takeownership(te) 
472     cairo_text_extents (cr,t.text,te)
473         set_pattern(te)
474     local mx,my=0,0
475     if t.h_align=="c" then
476             mx=-te.width/2
477     elseif t.h_align=="r" then
478             mx=-te.width
479         end
480     if t.v_align=="m" then
481             my=-te.height/2-te.y_bearing
482     elseif t.v_align=="t" then
483             my=-te.y_bearing
484         end
485         cairo_move_to(cr,mx,my)
486     cairo_show_text(cr,t.text)
487                 
488    if t.reflection_alpha ~= 0 then 
489                 local matrix1 = cairo_matrix_t:create()
490                 tolua.takeownership(matrix1) 
491                 cairo_set_font_size(cr,t.font_size)
492                 cairo_matrix_init (matrix1,1,0,0,-1*t.reflection_scale,0,(te.height+te.y_bearing+my)*(1+t.reflection_scale))
493                 cairo_set_font_size(cr,t.font_size)
494                 te=nil
495                 local te=cairo_text_extents_t:create()
496                 tolua.takeownership(te) 
497                 cairo_text_extents (cr,t.text,te)               
498                 cairo_transform(cr,matrix1)
499                 set_pattern(te)
500                 cairo_move_to(cr,mx,my)
501                 cairo_show_text(cr,t.text)
502                 local pat2 = cairo_pattern_create_linear (0,
503                                                                                 (te.y_bearing+te.height+my),
504                                                                                 0,
505                                                                                 te.y_bearing+my)
506                 cairo_pattern_add_color_stop_rgba (pat2, 0,1,0,0,1-t.reflection_alpha)
507                 cairo_pattern_add_color_stop_rgba (pat2, t.reflection_length,0,0,0,1)   
508                 cairo_set_line_width(cr,0)
509                 local dy=te.x_bearing
510                 if dy<0 then dy=dy*(-1) end
511                 cairo_rectangle(cr,mx+te.x_bearing,te.y_bearing+te.height+my,te.width+dy,-te.height*1.05)
512                 cairo_clip_preserve(cr)
513                 cairo_set_operator(cr,CAIRO_OPERATOR_CLEAR)
514                 --cairo_stroke(cr)
515                 cairo_mask(cr,pat2)
516                 cairo_pattern_destroy(pat2)
517                 cairo_set_operator(cr,CAIRO_OPERATOR_OVER)
518                 te=nil
519     end
520 end
521
522
523  function linear_orientation(t,te)
524         local w,h=te.width,te.height
525         local xb,yb=te.x_bearing,te.y_bearing
526         
527     if t.h_align=="c" then
528             xb=xb-w/2
529     elseif t.h_align=="r" then
530             xb=xb-w
531         end     
532     if t.v_align=="m" then
533             yb=-h/2
534     elseif t.v_align=="t" then
535             yb=0
536         end     
537         local p=0
538         if t.orientation=="nn" then
539                 p={xb+w/2,yb,xb+w/2,yb+h}
540         elseif t.orientation=="ne" then
541                 p={xb+w,yb,xb,yb+h}
542         elseif t.orientation=="ww" then
543                 p={xb,h/2,xb+w,h/2}
544         elseif vorientation=="se" then
545                 p={xb+w,yb+h,xb,yb}
546         elseif t.orientation=="ss" then
547                 p={xb+w/2,yb+h,xb+w/2,yb}
548         elseif vorientation=="ee" then
549                 p={xb+w,h/2,xb,h/2}             
550         elseif t.orientation=="sw" then
551                 p={xb,yb+h,xb+w,yb}
552         elseif t.orientation=="nw" then
553                 p={xb,yb,xb+w,yb+h}
554         end
555         return p
556 end
557