stable now, graphs might need some tweaks
[monky] / data / 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                 {
103                                 text=conky_parse("$freq MHz"),
104                                 font_name="Nokia Pure",
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=845,
119                     y=69,
120                     v_align="m",
121                     h_align="r",
122                     font_name="Nokia Pure",
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_rate}mA"),
131                     x=750,
132                     y=69,
133                     v_align="m",
134                     h_align="l",
135                     font_name="Nokia Pure",
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("${battery_short}   ${battery_volts}mV"),
144                     x=845,
145                     y=45,
146                     v_align="m",
147                     h_align="r",
148                     font_name="Nokia Pure",
149                     font_size=14,
150                         colour={{0,col0,1},{0.5,colbg,1}},
151                                 orientation="nn",
152                 reflection_alpha=0,
153                 reflection_length=0,                    
154         }, 
155         {
156                     text=conky_parse("${cell_radio_dbm}".."dBm"),
157                         x=510,
158                     y=50,
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.0,
164                 reflection_length=0.0,                  
165         },
166         {
167                     text=conky_parse('${cpu}').."%",
168                         x=223,
169                     y=67,
170                     v_align="t",
171                     h_align="l",
172                     font_name="Nokia Pure",
173                     font_size=18,
174                         colour={{0,col0,1},{0.5,colbg,1}},
175                                 orientation="ww",
176                 reflection_alpha=0,
177                 reflection_length=0,                    
178         },  
179         {
180                     text=conky_parse('${memperc}').."% RAM",
181                     x=223,
182                     y=101,
183                     v_align="t",
184                     font_name="Nokia Pure",
185                     font_size=18,
186                         colour={{0,col0,1},{0.5,colbg,1}},
187                                 orientation="nn",
188                                 reflection_alpha=0,
189                                 reflection_length=0,
190         },
191         {
192                     text=conky_parse('${fs_used /} / ${fs_size /} (${fs_free /})'),
193                     x=5,
194                     y=125,
195                     v_align="t",
196                     font_name="Nokia Pure",
197                     font_size=16,
198                         colour={{conky_parse('${fs_used_perc /}')/100,0x000000,1},{1,0xFFFFFF,1}},
199                                 orientation="ww",
200                                 reflection_alpha=0,
201                                 reflection_length=0,
202         },
203         {
204                     text="/",
205                     x=220,
206                     y=124,
207                     v_align="t",
208                     font_name="Nokia Pure",
209                     font_size=18,
210                         colour={{0,0x336633,1},{1,col0,1}},
211                         bold=true,
212                                 orientation="ww",
213                                 reflection_alpha=0,
214                                 reflection_length=0,
215         },
216         {
217                     text=conky_parse('${fs_used /home} / ${fs_size /home} (${fs_free /home})'),
218                     x=5,
219                     y=145,
220                     v_align="t",
221                     font_name="Nokia Pure",
222                     font_size=16,
223                         colour={{conky_parse('${fs_used_perc /home}')/100,0x000000,1},{1,0xFFFFFF,1}},
224                                 orientation="ww",
225                                 reflection_alpha=0,
226                                 reflection_length=0,
227         },
228         {
229                     text="/home",
230                     x=220,
231                     y=144,
232                     v_align="t",
233                     font_name="Nokia Pure",
234                     font_size=18,
235                         colour={{0,0x336633,1},{1,col0,1}},
236                         bold=true,
237                                 orientation="ww",
238                                 reflection_alpha=0,
239                                 reflection_length=0,
240         },
241         {
242                     text=conky_parse('${fs_used /home/user/MyDocs} / ${fs_size /home/user/MyDocs} (${fs_free /home/user/MyDocs})'),
243                     x=5,
244                     y=165,
245                     v_align="t",
246                     font_name="Nokia Pure",
247                     font_size=16,
248                         colour={{conky_parse('${fs_used_perc /home/user/MyDocs}')/100-.1,0x000000,1},{1,0xFFFFFF,1}},
249                                 orientation="ww",
250                                 reflection_alpha=0,
251                                 reflection_length=0,
252                                 DrawMe=conky_parse("${if_mounted /home/user/MyDocs}1$endif")
253         },
254         {
255                     text="MyDocs",
256                     x=220,
257                     y=164,
258                     v_align="t",
259                     font_name="Nokia Pure",
260                     font_size=18,
261                         colour={{0,0x336633,1},{1,col0,1}},
262                         bold=true,
263                                 orientation="ww",
264                                 reflection_alpha=0,
265                                 reflection_length=0,
266                                 DrawMe=conky_parse("${if_mounted /home/user/MyDocs}1$endif")
267         },        
268         {
269                     text="DiskIO",
270                     x=520,
271                     y=101,
272                     v_align="t",
273                     font_name="Nokia Pure",
274                     font_size=18,
275                         colour={{0,0xcc6600,1},{1,col0,1}},
276                         bold=true,
277                                 orientation="ww",
278                                 reflection_alpha=0,
279                                 reflection_length=0,
280         },
281         {
282                     text="Charge Rate",
283                     x=720,
284                     y=101,
285                     v_align="t",
286                     font_name="Nokia Pure",
287                     font_size=18,
288                         colour={{0,0xFF0000,1},{1,col0,1}},
289                         bold=true,
290                                 orientation="ww",
291                                 reflection_alpha=0,
292                                 reflection_length=0,
293         },
294         
295  --[[   {
296                     text=conky_parse('${fs_used /media/mmc1} / ${fs_size /media/mmc1} (${fs_free /media/mmc1})'),
297                     x=5,
298                     y=175,
299                     v_align="t",
300                     font_name="Nokia Pure",
301                     font_size=16,
302                         colour={{conky_parse('${fs_used_perc /media/mmc1}')/100-.1,0x000000,1},{1,0xFFFFFF,1}},
303                                 orientation="ww",
304                                 reflection_alpha=0,
305                                 reflection_length=0,
306                                 DrawMe=conky_parse("${if_mounted /media/mmc1}1$endif")
307         },
308         {
309                     text="SDCard",
310                     x=220,
311                     y=174,
312                     v_align="t",
313                     font_name="Nokia Pure",
314                     font_size=18,
315                         colour={{0,0x336633,1},{1,col0,1}},
316                         bold=true,
317                                 orientation="ww",
318                                 reflection_alpha=0,
319                                 reflection_length=0,
320                                 DrawMe=conky_parse("${if_mounted /media/mmc1}1$endif"),
321         },
322                 ]]
323         {
324                     text=conky_parse('${wireless_essid wlan0}'),
325                         x=427,
326                     y=202,
327                     h_align="c",
328                     v_align="m",
329                     font_name="Nokia Pure",
330                     font_size=18,
331                         colour={{0,col0,1},{0.5,colbg,1}},
332                                 orientation="nn",
333                 reflection_alpha=0,
334                 reflection_length=0,
335                 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
336         },
337         {
338                     text="GPRS",
339                         x=427,
340                     y=201,
341                     h_align="c",
342                     v_align="m",
343                     font_name="Nokia Pure",
344                     font_size=18,
345                         colour={{0,col0,1},{0.5,colbg,1}},
346                                 orientation="nn",
347                 reflection_alpha=0,
348                 reflection_length=0,
349                 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
350         },
351         {
352                     text=conky_parse('${wireless_link_qual_perc wlan0}')..'%',
353                         x=427,
354                     y=226,
355                     h_align="c",
356                     v_align="m",
357                     font_name="Nokia Pure",
358                     font_size=14,
359                         colour={{0,col0,1},{0.5,colbg,1}},
360                                 orientation="nn",
361                 reflection_alpha=0,
362                 reflection_length=0,
363                 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
364         },
365         {
366                     text='Up '..conky_parse('${upspeedf wlan0}') + conky_parse('${upspeedf gprs0}'),
367                         x=225,
368                     y=223,
369                     h_align="l",
370                     v_align="m",
371                     font_name="LEDFont",
372                     font_size=22,
373                     bold=true,
374                         colour={{0,0xcc0066,1}},
375                                 orientation="ww",
376                 reflection_alpha=0,
377                 reflection_length=0,
378                 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
379         },
380         {
381                     text=conky_parse('${downspeedf wlan0}') + conky_parse('${downspeedf gprs0}')..' Down',
382                         x=650,
383                     y=223,
384                     h_align="r",
385                     v_align="m",
386                     font_name="LEDFont",
387                     font_size=22,
388                     bold=true,
389                         colour={{0,0xcc0066,1}},
390                                 orientation="ww",
391                 reflection_alpha=0,
392                 reflection_length=0,
393                 DrawMe=conky_parse("${if_up wlan0}1${else}0$endif"),
394         },
395    --[[     {
396                     text='Up '..conky_parse('${upspeed gprs0}'),
397                         x=225,
398                     y=223,
399                     h_align="l",
400                     v_align="m",
401                     font_name="LEDFont",
402                     font_size=22,
403                     bold=true,
404                         colour={{0,0xcc0066,1}},
405                                 orientation="ww",
406                 reflection_alpha=0,
407                 reflection_length=0,
408                 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
409         },
410         {
411                     text=conky_parse('${downspeed gprs0}')..' Down',
412                         x=650,
413                     y=223,
414                     h_align="r",
415                     v_align="m",
416                     font_name="LEDFont",
417                     font_size=22,
418                     bold=true,
419                         colour={{0,0xcc0066,1}},
420                                 orientation="ww",
421                 reflection_alpha=0,
422                 reflection_length=0,
423                 DrawMe=conky_parse("${if_up gprs0}1${else}0$endif"),
424         },]]  
425     }
426     if conky_window == nil then return end
427     if tonumber(conky_parse("$updates"))<3 then return end
428         local cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, conky_window.width, conky_window.height)
429     for i,v in pairs(text_settings) do  
430         cr = cairo_create (cs)
431                         display_text(v)
432             cairo_destroy(cr)
433             cr = nil
434     end
435         cairo_surface_destroy(cs)
436 end
437
438 function rgb_to_r_g_b2(tcolour)
439     local colour,alpha=tcolour[2],tcolour[3]
440     return ((colour / 0x10000) % 0x100) / 255., ((colour / 0x100) % 0x100) / 255., (colour % 0x100) / 255., alpha
441 end
442
443 function display_text(t)
444         if t.DrawMe~=nil and t.DrawMe ~= "1" then return end
445         local function set_pattern(te)
446                 --this function set the pattern
447                 if #t.colour==1 then 
448                     cairo_set_source_rgba(cr,rgb_to_r_g_b2(t.colour[1]))
449                 else
450                         local pat
451                         if t.radial==nil then
452                                 local pts=linear_orientation(t,te)
453                                 pat = cairo_pattern_create_linear (pts[1],pts[2],pts[3],pts[4])
454                         else
455                                 pat = cairo_pattern_create_radial (t.radial[1],t.radial[2],t.radial[3],t.radial[4],t.radial[5],t.radial[6])
456                         end
457                     for i=1, #t.colour do
458                         cairo_pattern_add_color_stop_rgba (pat, t.colour[i][1], rgb_to_r_g_b2(t.colour[i]))
459                     end
460                     cairo_set_source (cr, pat)
461                         cairo_pattern_destroy(pat)
462                 end
463     end
464     
465     --set default values if needed
466     if t.text==nil then t.text="Conky is good for you !" end
467     if t.x==nil then t.x = conky_window.width/2 end
468     if t.y==nil then t.y = conky_window.height/2 end
469     if t.colour==nil then t.colour={{1,0xFFFFFF,1}} end
470     if t.font_name==nil then t.font_name="Free Sans" end
471     if t.font_size==nil then t.font_size=14 end
472     if t.angle==nil then t.angle=0 end
473     if t.italic==nil then t.italic=false end
474     if t.oblique==nil then t.oblique=false end
475     if t.bold==nil then t.bold=false end
476     if t.radial ~= nil then
477         if #t.radial~=6 then 
478                 print ("error in radial table")
479                 t.radial=nil 
480         end
481     end
482     if t.orientation==nil then t.orientation="ww" end
483     if t.h_align==nil then t.h_align="l" end
484     if t.v_align==nil then t.v_align="b" end    
485     if t.reflection_alpha == nil then t.reflection_alpha=0 end
486     if t.reflection_length == nil then t.reflection_length=1 end
487     if t.reflection_scale == nil then t.reflection_scale=1 end
488     if t.rotx==nil then t.rotx=0 end
489     if t.roty==nil then t.roty=0 end    
490     cairo_translate(cr,t.x,t.y)
491     cairo_rotate(cr,t.angle*math.pi/180)
492     cairo_save(cr)       
493     local slant = CAIRO_FONT_SLANT_NORMAL
494     local weight = CAIRO_FONT_WEIGHT_NORMAL
495     if t.italic then slant = CAIRO_FONT_SLANT_ITALIC end
496     if t.oblique then slant = CAIRO_FONT_SLANT_OBLIQUE end
497     if t.bold then weight = CAIRO_FONT_WEIGHT_BOLD end
498     cairo_select_font_face(cr, t.font_name, slant,weight)
499     for i=1, #t.colour do    
500         if #t.colour[i]~=3 then 
501                 print ("error in color table")
502                 t.colour[i]={1,0xFFFFFF,1} 
503         end
504     end
505         local matrix0 = cairo_matrix_t:create()
506         tolua.takeownership(matrix0) 
507         local rotx,roty=t.rotx/t.font_size,t.roty/t.font_size
508         cairo_matrix_init (matrix0, 1,roty,rotx,1,0,0)
509         cairo_transform(cr,matrix0)
510         cairo_set_font_size(cr,t.font_size)
511         local te=cairo_text_extents_t:create()
512         tolua.takeownership(te) 
513     cairo_text_extents (cr,t.text,te)
514         set_pattern(te)
515     local mx,my=0,0
516     if t.h_align=="c" then
517             mx=-te.width/2
518     elseif t.h_align=="r" then
519             mx=-te.width
520         end
521     if t.v_align=="m" then
522             my=-te.height/2-te.y_bearing
523     elseif t.v_align=="t" then
524             my=-te.y_bearing
525         end
526         cairo_move_to(cr,mx,my)
527     cairo_show_text(cr,t.text)
528                 
529    if t.reflection_alpha ~= 0 then 
530                 local matrix1 = cairo_matrix_t:create()
531                 tolua.takeownership(matrix1) 
532                 cairo_set_font_size(cr,t.font_size)
533                 cairo_matrix_init (matrix1,1,0,0,-1*t.reflection_scale,0,(te.height+te.y_bearing+my)*(1+t.reflection_scale))
534                 cairo_set_font_size(cr,t.font_size)
535                 te=nil
536                 local te=cairo_text_extents_t:create()
537                 tolua.takeownership(te) 
538                 cairo_text_extents (cr,t.text,te)               
539                 cairo_transform(cr,matrix1)
540                 set_pattern(te)
541                 cairo_move_to(cr,mx,my)
542                 cairo_show_text(cr,t.text)
543                 local pat2 = cairo_pattern_create_linear (0,
544                                                                                 (te.y_bearing+te.height+my),
545                                                                                 0,
546                                                                                 te.y_bearing+my)
547                 cairo_pattern_add_color_stop_rgba (pat2, 0,1,0,0,1-t.reflection_alpha)
548                 cairo_pattern_add_color_stop_rgba (pat2, t.reflection_length,0,0,0,1)   
549                 cairo_set_line_width(cr,0)
550                 local dy=te.x_bearing
551                 if dy<0 then dy=dy*(-1) end
552                 cairo_rectangle(cr,mx+te.x_bearing,te.y_bearing+te.height+my,te.width+dy,-te.height*1.05)
553                 cairo_clip_preserve(cr)
554                 cairo_set_operator(cr,CAIRO_OPERATOR_CLEAR)
555                 --cairo_stroke(cr)
556                 cairo_mask(cr,pat2)
557                 cairo_pattern_destroy(pat2)
558                 cairo_set_operator(cr,CAIRO_OPERATOR_OVER)
559                 te=nil
560     end
561 end
562
563
564  function linear_orientation(t,te)
565         local w,h=te.width,te.height
566         local xb,yb=te.x_bearing,te.y_bearing
567         
568     if t.h_align=="c" then
569             xb=xb-w/2
570     elseif t.h_align=="r" then
571             xb=xb-w
572         end     
573     if t.v_align=="m" then
574             yb=-h/2
575     elseif t.v_align=="t" then
576             yb=0
577         end     
578         local p=0
579         if t.orientation=="nn" then
580                 p={xb+w/2,yb,xb+w/2,yb+h}
581         elseif t.orientation=="ne" then
582                 p={xb+w,yb,xb,yb+h}
583         elseif t.orientation=="ww" then
584                 p={xb,h/2,xb+w,h/2}
585         elseif vorientation=="se" then
586                 p={xb+w,yb+h,xb,yb}
587         elseif t.orientation=="ss" then
588                 p={xb+w/2,yb+h,xb+w/2,yb}
589         elseif vorientation=="ee" then
590                 p={xb+w,h/2,xb,h/2}             
591         elseif t.orientation=="sw" then
592                 p={xb,yb+h,xb+w,yb}
593         elseif t.orientation=="nw" then
594                 p={xb,yb,xb+w,yb+h}
595         end
596         return p
597 end
598