changes related to temperature and layout
[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="Nokia Pure",
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="Nokia Pure",
94                                 h_align="r",
95                                 x=850,
96                                 y=15,
97                                 reflection_alpha=0,
98                                 reflection_length=0,
99                                 colour={{0,col0,0.75},{1,colbg,0.75}},
100                 },
101                 {
102                                 text="Conky v"..conky_parse("$conky_version").." - Lance Colton",
103                                 font_size=16,
104                                 bold=true,
105                                 font_name="Nokia Pure",
106                                 h_align="r",
107                                 x=850,
108                                 y=28,
109                                 reflection_alpha=0,
110                                 reflection_length=0,
111                                 colour={{0,col0,0.75},{1,colbg,0.75}},
112                 }, 
113                 {
114                                 text=conky_parse("$freq MHz"),
115                                 font_name="Nokia Pure",
116                                 font_size=16,
117                                 h_align="l",
118                                 v_align="t",
119                                 bold=true,
120                                 x=20,
121                                 y=60,
122                                 reflection_alpha=0,
123                                 reflection_length=0,
124                                 colour={{0,col0,1},{0.5,colbg,1}},
125                                 orientation="nn",
126                 },
127                 {
128                     text=conky_parse("${battery_temp}").."\194\176",--yes i had to do the degree symbol like that...
129                     x=845,
130                     y=69,
131                     v_align="m",
132                     h_align="r",
133                     font_name="Nokia Pure",
134                     font_size=14,
135                         colour={{0,col0,1},{0.5,colbg,1}},
136                                 orientation="nn",
137                 reflection_alpha=0,
138                 reflection_length=0,                    
139         },
140                 {
141                     text=conky_parse("${battery_rate}mA"),
142                     x=750,
143                     y=69,
144                     v_align="m",
145                     h_align="l",
146                     font_name="Nokia Pure",
147                     font_size=14,
148                         colour={{0,col0,1},{0.5,colbg,1}},
149                                 orientation="nn",
150                 reflection_alpha=0,
151                 reflection_length=0,                    
152         },
153         {
154                     text=conky_parse("${battery_short}   ${battery_volts}mV"),
155                     x=845,
156                     y=45,
157                     v_align="m",
158                     h_align="r",
159                     font_name="Nokia Pure",
160                     font_size=14,
161                         colour={{0,col0,1},{0.5,colbg,1}},
162                                 orientation="nn",
163                 reflection_alpha=0,
164                 reflection_length=0,                    
165         }, 
166         {
167                     text=conky_parse("${cell_radio_dbm}".."dBm"),
168                         x=510,
169                     y=50,
170                     font_name="Nokia Pure",
171                     font_size=14,
172                         colour={{0,col0,1},{0.5,colbg,1}},
173                                 orientation="nn",
174                 reflection_alpha=0.0,
175                 reflection_length=0.0,                  
176         },
177         {
178                     text=conky_parse('${cpu}').."%",
179                         x=223,
180                     y=67,
181                     v_align="t",
182                     h_align="l",
183                     font_name="Nokia Pure",
184                     font_size=18,
185                         colour={{0,col0,1},{0.5,colbg,1}},
186                                 orientation="ww",
187                 reflection_alpha=0,
188                 reflection_length=0,                    
189         },  
190         {
191                     text=conky_parse('${memperc}').."% RAM",
192                     x=223,
193                     y=101,
194                     v_align="t",
195                     font_name="Nokia Pure",
196                     font_size=18,
197                         colour={{0,col0,1},{0.5,colbg,1}},
198                                 orientation="nn",
199                                 reflection_alpha=0,
200                                 reflection_length=0,
201         },
202         {
203                     text=conky_parse('${fs_used /} / ${fs_size /} (${fs_free /})'),
204                     x=5,
205                     y=125,
206                     v_align="t",
207                     font_name="Nokia Pure",
208                     font_size=16,
209                         colour={{conky_parse('${fs_used_perc /}')/100,0x000000,1},{1,0xFFFFFF,1}},
210                                 orientation="ww",
211                                 reflection_alpha=0,
212                                 reflection_length=0,
213         },
214         {
215                     text="/",
216                     x=220,
217                     y=124,
218                     v_align="t",
219                     font_name="Nokia Pure",
220                     font_size=18,
221                         colour={{0,0x336633,1},{1,col0,1}},
222                         bold=true,
223                                 orientation="ww",
224                                 reflection_alpha=0,
225                                 reflection_length=0,
226         },
227         {
228                     text=conky_parse('${fs_used /home} / ${fs_size /home} (${fs_free /home})'),
229                     x=5,
230                     y=145,
231                     v_align="t",
232                     font_name="Nokia Pure",
233                     font_size=16,
234                         colour={{conky_parse('${fs_used_perc /home}')/100,0x000000,1},{1,0xFFFFFF,1}},
235                                 orientation="ww",
236                                 reflection_alpha=0,
237                                 reflection_length=0,
238         },
239         {
240                     text="/home",
241                     x=220,
242                     y=144,
243                     v_align="t",
244                     font_name="Nokia Pure",
245                     font_size=18,
246                         colour={{0,0x336633,1},{1,col0,1}},
247                         bold=true,
248                                 orientation="ww",
249                                 reflection_alpha=0,
250                                 reflection_length=0,
251         },
252         {
253                     text=conky_parse('${fs_used /home/user/MyDocs} / ${fs_size /home/user/MyDocs} (${fs_free /home/user/MyDocs})'),
254                     x=5,
255                     y=165,
256                     v_align="t",
257                     font_name="Nokia Pure",
258                     font_size=16,
259                         colour={{conky_parse('${fs_used_perc /home/user/MyDocs}')/100-.1,0x000000,1},{1,0xFFFFFF,1}},
260                                 orientation="ww",
261                                 reflection_alpha=0,
262                                 reflection_length=0,
263                                 DrawMe=conky_parse("${if_mounted /home/user/MyDocs}1$endif")
264         },
265         {
266                     text="MyDocs",
267                     x=220,
268                     y=164,
269                     v_align="t",
270                     font_name="Nokia Pure",
271                     font_size=18,
272                         colour={{0,0x336633,1},{1,col0,1}},
273                         bold=true,
274                                 orientation="ww",
275                                 reflection_alpha=0,
276                                 reflection_length=0,
277                                 DrawMe=conky_parse("${if_mounted /home/user/MyDocs}1$endif")
278         },        
279         {
280                     text="DiskIO",
281                     x=520,
282                     y=101,
283                     v_align="t",
284                     font_name="Nokia Pure",
285                     font_size=18,
286                         colour={{0,0xcc6600,1},{1,col0,1}},
287                         bold=true,
288                                 orientation="ww",
289                                 reflection_alpha=0,
290                                 reflection_length=0,
291         },
292         {
293                     text="Charge Rate",
294                     x=720,
295                     y=101,
296                     v_align="t",
297                     font_name="Nokia Pure",
298                     font_size=18,
299                         colour={{0,0xFF0000,1},{1,col0,1}},
300                         bold=true,
301                                 orientation="ww",
302                                 reflection_alpha=0,
303                                 reflection_length=0,
304         },
305         
306  --[[   {
307                     text=conky_parse('${fs_used /media/mmc1} / ${fs_size /media/mmc1} (${fs_free /media/mmc1})'),
308                     x=5,
309                     y=175,
310                     v_align="t",
311                     font_name="Nokia Pure",
312                     font_size=16,
313                         colour={{conky_parse('${fs_used_perc /media/mmc1}')/100-.1,0x000000,1},{1,0xFFFFFF,1}},
314                                 orientation="ww",
315                                 reflection_alpha=0,
316                                 reflection_length=0,
317                                 DrawMe=conky_parse("${if_mounted /media/mmc1}1$endif")
318         },
319         {
320                     text="SDCard",
321                     x=220,
322                     y=174,
323                     v_align="t",
324                     font_name="Nokia Pure",
325                     font_size=18,
326                         colour={{0,0x336633,1},{1,col0,1}},
327                         bold=true,
328                                 orientation="ww",
329                                 reflection_alpha=0,
330                                 reflection_length=0,
331                                 DrawMe=conky_parse("${if_mounted /media/mmc1}1$endif"),
332         },
333                 ]]
334         {
335                     text=conky_parse('${wireless_essid wlan0}'),
336                         x=427,
337                     y=202,
338                     h_align="c",
339                     v_align="m",
340                     font_name="Nokia Pure",
341                     font_size=18,
342                         colour={{0,col0,1},{0.5,colbg,1}},
343                                 orientation="nn",
344                 reflection_alpha=0,
345                 reflection_length=0,
346                 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
347         },
348         {
349                     text="GPRS",
350                         x=427,
351                     y=201,
352                     h_align="c",
353                     v_align="m",
354                     font_name="Nokia Pure",
355                     font_size=18,
356                         colour={{0,col0,1},{0.5,colbg,1}},
357                                 orientation="nn",
358                 reflection_alpha=0,
359                 reflection_length=0,
360                 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
361         },
362         {
363                     text=conky_parse('${wireless_link_qual_perc wlan0}')..'%',
364                         x=427,
365                     y=226,
366                     h_align="c",
367                     v_align="m",
368                     font_name="Nokia Pure",
369                     font_size=14,
370                         colour={{0,col0,1},{0.5,colbg,1}},
371                                 orientation="nn",
372                 reflection_alpha=0,
373                 reflection_length=0,
374                 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
375         },
376         {
377                     text='Up '..conky_parse('${upspeedf wlan0}') + conky_parse('${upspeedf gprs0}'),
378                         x=225,
379                     y=223,
380                     h_align="l",
381                     v_align="m",
382                     font_name="LEDFont",
383                     font_size=22,
384                     bold=true,
385                         colour={{0,0xcc0066,1}},
386                                 orientation="ww",
387                 reflection_alpha=0,
388                 reflection_length=0,
389                 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
390         },
391         {
392                     text=conky_parse('${downspeedf wlan0}') + conky_parse('${downspeedf gprs0}')..' Down',
393                         x=650,
394                     y=223,
395                     h_align="r",
396                     v_align="m",
397                     font_name="LEDFont",
398                     font_size=22,
399                     bold=true,
400                         colour={{0,0xcc0066,1}},
401                                 orientation="ww",
402                 reflection_alpha=0,
403                 reflection_length=0,
404                 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
405         },
406    --[[     {
407                     text='Up '..conky_parse('${upspeed gprs0}'),
408                         x=225,
409                     y=223,
410                     h_align="l",
411                     v_align="m",
412                     font_name="LEDFont",
413                     font_size=22,
414                     bold=true,
415                         colour={{0,0xcc0066,1}},
416                                 orientation="ww",
417                 reflection_alpha=0,
418                 reflection_length=0,
419                 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
420         },
421         {
422                     text=conky_parse('${downspeed gprs0}')..' Down',
423                         x=650,
424                     y=223,
425                     h_align="r",
426                     v_align="m",
427                     font_name="LEDFont",
428                     font_size=22,
429                     bold=true,
430                         colour={{0,0xcc0066,1}},
431                                 orientation="ww",
432                 reflection_alpha=0,
433                 reflection_length=0,
434                 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
435         },]]  
436     }
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)
442                         display_text(v)
443             cairo_destroy(cr)
444             cr = nil
445     end
446         cairo_surface_destroy(cs)
447 end
448
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
452 end
453
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
458                 if #t.colour==1 then 
459                     cairo_set_source_rgba(cr,rgb_to_r_g_b2(t.colour[1]))
460                 else
461                         local pat
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])
465                         else
466                                 pat = cairo_pattern_create_radial (t.radial[1],t.radial[2],t.radial[3],t.radial[4],t.radial[5],t.radial[6])
467                         end
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]))
470                     end
471                     cairo_set_source (cr, pat)
472                         cairo_pattern_destroy(pat)
473                 end
474     end
475     
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
488         if #t.radial~=6 then 
489                 print ("error in radial table")
490                 t.radial=nil 
491         end
492     end
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)
503     cairo_save(cr)       
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} 
514         end
515     end
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)
525         set_pattern(te)
526     local mx,my=0,0
527     if t.h_align=="c" then
528             mx=-te.width/2
529     elseif t.h_align=="r" then
530             mx=-te.width
531         end
532     if t.v_align=="m" then
533             my=-te.height/2-te.y_bearing
534     elseif t.v_align=="t" then
535             my=-te.y_bearing
536         end
537         cairo_move_to(cr,mx,my)
538     cairo_show_text(cr,t.text)
539                 
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)
546                 te=nil
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)
551                 set_pattern(te)
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),
556                                                                                 0,
557                                                                                 te.y_bearing+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)
566                 --cairo_stroke(cr)
567                 cairo_mask(cr,pat2)
568                 cairo_pattern_destroy(pat2)
569                 cairo_set_operator(cr,CAIRO_OPERATOR_OVER)
570                 te=nil
571     end
572 end
573
574
575  function linear_orientation(t,te)
576         local w,h=te.width,te.height
577         local xb,yb=te.x_bearing,te.y_bearing
578         
579     if t.h_align=="c" then
580             xb=xb-w/2
581     elseif t.h_align=="r" then
582             xb=xb-w
583         end     
584     if t.v_align=="m" then
585             yb=-h/2
586     elseif t.v_align=="t" then
587             yb=0
588         end     
589         local p=0
590         if t.orientation=="nn" then
591                 p={xb+w/2,yb,xb+w/2,yb+h}
592         elseif t.orientation=="ne" then
593                 p={xb+w,yb,xb,yb+h}
594         elseif t.orientation=="ww" then
595                 p={xb,h/2,xb+w,h/2}
596         elseif vorientation=="se" then
597                 p={xb+w,yb+h,xb,yb}
598         elseif t.orientation=="ss" then
599                 p={xb+w/2,yb+h,xb+w/2,yb}
600         elseif vorientation=="ee" then
601                 p={xb+w,h/2,xb,h/2}             
602         elseif t.orientation=="sw" then
603                 p={xb,yb+h,xb+w,yb}
604         elseif t.orientation=="nw" then
605                 p={xb,yb,xb+w,yb+h}
606         end
607         return p
608 end
609