-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcairo-tools.lua
416 lines (364 loc) · 9.7 KB
/
cairo-tools.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
require 'cairo'
require 'lib'
--[[
Convert hexadezimal colors to rgba values
e.g. cairo_set_source_rgba(cr, rgbToRgba(0xff7f00, 0.8))
]]
function rgbToRgba(color, alpha)
return ((color / 0x10000) % 0x100) / 255.0,
((color / 0x100) % 0x100) / 255.0,
(color % 0x100) / 255.0,
alpha
end
--[[
Write text on cr by definition (def)
def = {
color = 0xffffff,
alpha = 1,
pos = {x = 23, y = 42},
font = {'Open Sans', 12, 0|1|2|3},
align = {'LEFT|right|center', 'BASELINE|bottom|middle|top'},
}
font[3] are bit flags where first bit is bold and second bit is italic. So:
0 is normal,
1 is bold,
2 is italic,
3 is bold+italic
returns the boundingBox = {
left = %d,
top = %d,
width = %d,
height = %d,
right = %d,
bottom = %d,
}
]]
function write(cr, text, def)
local font = def.font[1] or 'Open Sans'
local fontSize = def.font[2] or 12
local fontSlant = CAIRO_FONT_SLANT_NORMAL
local fontWeight = CAIRO_FONT_WEIGHT_NORMAL
if def.font[3] and def.font[3] % 2 == 1 then
fontWeight = CAIRO_FONT_WEIGHT_BOLD
end
if def.font[3] and def.font[3] / 2 >= 1 then
fontWeight = CAIRO_FONT_SLANT_ITALIC
end
cairo_select_font_face(cr, font, fontSlant, fontWeight)
cairo_set_font_size(cr, fontSize)
cairo_set_source_rgba(cr, rgbToRgba(def.color or 0xffffff, def.alpha or 1))
local x = def.pos.x
local y = def.pos.y
local te = cairo_text_extents_t:create()
local fe = cairo_font_extents_t:create()
cairo_text_extents(cr, text, te)
cairo_font_extents(cr, fe)
if def.align ~= nil then
if def.align[1] == 'right' then
x = x - te.width - te.x_bearing
elseif def.align[1] == 'center' then
x = x - te.width/2 - te.x_bearing
end
if def.align[2] == 'bottom' then
y = y - fe.descent
elseif def.align[2] == 'middle' then
y = y + te.height/2
elseif def.align[2] == 'top' then
y = y + fe.ascent
end
end
cairo_move_to(cr, x, y)
cairo_show_text(cr, text)
cairo_new_path(cr)
local boundingBox = {
x = x,
y = y,
left = x + te.x_bearing,
top = y + te.y_bearing,
width = te.width,
height = te.height,
right = x + te.x_bearing + te.width,
bottom = y + te.y_bearing + te.height
}
return boundingBox
end
--[[
Draw a rectangle on cr by definition (def)
def = {
pos = {x = 10, y = 10},
size = {width = 200, height = 100},
fill = {color = 0x000000, alpha = 0.2},
stroke = {width = 2, color = 0x000000, alpha = 0.5},
cornerRadius = 0,
}
]]
function roundRectangle(cr, def)
local pos, size = def.pos, def.size;
local double radius = def.cornerRadius or 0;
local double degrees = math.pi / 180.0;
cairo_new_sub_path(cr)
cairo_arc(
cr,
pos.x + size.width - radius,
pos.y + radius,
radius,
270 * degrees,
360 * degrees
)
cairo_arc(
cr,
pos.x + size.width - radius,
pos.y + size.height - radius,
radius,
0 * degrees,
90 * degrees
)
cairo_arc(
cr,
pos.x + radius,
pos.y + size.height - radius,
radius,
90 * degrees,
180 * degrees
)
cairo_arc(
cr,
pos.x + radius,
pos.y + radius,
radius,
180 * degrees,
270 * degrees
)
cairo_close_path(cr)
if def.fill then
cairo_set_source_rgba(
cr,
rgbToRgba(def.fill.color, def.fill.alpha or 1)
)
cairo_fill_preserve(cr)
end
if def.stroke ~= nil then
cairo_set_source_rgba(
cr,
rgbToRgba(def.stroke.color, def.stroke.alpha or 1)
)
cairo_set_line_width(cr, def.stroke.width)
cairo_stroke_preserve(cr)
end
cairo_new_path(cr)
end
--[[
Draw a path on cr by definition (def)
def = {
pos = {x = 10, y = 10},
points = { {x = 5, y = 15}, {x = 15, y = 15}, ... }
fill = {color = 0x000000, alpha = 0.2},
stroke = {width = 2, color = 0x000000, alpha = 0.5}
}
]]
function path(cr, def)
cairo_new_sub_path(cr)
cairo_move_to(cr, def.pos.x, def.pos.y)
for i,point in ipairs(def.points) do
cairo_line_to(cr, point.x, point.y)
end
cairo_close_path(cr)
if def.fill then
cairo_set_source_rgba(
cr,
rgbToRgba(def.fill.color, def.fill.alpha or 1)
)
cairo_fill_preserve(cr)
end
if def.stroke then
cairo_set_source_rgba(
cr,
rgbToRgba(def.stroke.color, def.stroke.alpha or 1)
)
cairo_set_line_width(cr, def.stroke.width)
cairo_stroke_preserve(cr)
end
cairo_new_path(cr)
end
--[[
Draw a ring on cr by definition (def)
def = {
pos = {x = 100, y = 100},
radius = 85,
thickness = 10,
from = 0, -- top
to = 270, -- left
color = 0xffffff,
alpha = 0.8,
}
]]
function ring(cr, def)
local pos, radius, thickness, from, to, color, alpha =
def.pos, def.radius, def.thickness, def.from, def.to, def.color, def.alpha
local double degrees = math.pi / 180.0;
from = from * degrees - math.pi/2
to = to * degrees - math.pi/2
-- prevent errors with nan
if tostring(to) == '-nan' then return end
if tostring(to) == 'nan' then return end
cairo_set_line_width(cr, thickness)
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND)
cairo_set_source_rgba(cr, rgbToRgba(color, alpha))
cairo_arc(cr, pos.x, pos.y, radius, from, to)
cairo_stroke(cr)
cairo_new_path(cr)
end
function gauge(cr, value, def)
if def.background then
local bgDef = table.clone(def)
bgDef.color = def.background.color
bgDef.alpha = def.background.alpha
ring(cr, bgDef)
end
if def.level2 then
local level2Def = table.clone(def)
level2Def.color = def.level2.color
level2Def.alpha = def.level2.alpha
local t = def.level2.value / (def.max or 100)
if t > 1 then t = 1 end
level2Def.to = def.from + t * (def.to - def.from)
ring(cr, level2Def)
end
local t = value / (def.max or 100)
if t > 1 then t = 1 end
if t == 0.0 then return end
def.to = def.from + t * (def.to - def.from)
if def.warn and (def.warn.from and def.warn.from <= value or def.warn.to and def.warn.to >= value) then
def.color = def.warn.color
end
if def.crit and (def.crit.from and def.crit.from <= value or def.crit.to and def.crit.to >= value) then
def.color = def.crit.color
end
ring(cr, def)
end
local graphHistory = {}
--[[
Draw a graph
]]
function graph(cr, key, currentValue, def)
if def.max == 0 then return end -- cant't draw a graph from 0 to 0
-- fill graph history
if graphHistory[key] == nil then graphHistory[key] = {} end
table.insert(graphHistory[key], 1, currentValue)
-- get defintions
local thickness = def.thickness or 1
local direction = def.direction or 'right'
local amplitude = def.amplitude or 'up'
local height = def.height or 50
local width = def.width or 100
local max = def.max or 100
local count = def.count or width
local color = def.color or 0xffffff
local alpha = def.alpha or 1
-- configure direction
local incX, incY, fullX, fullY = 0, 0, 0, 0
if direction == 'right' then
incX = thickness
fullY = 0 - height
elseif direction == 'left' then
incX = 0 - thickness
fullY = 0 - height
elseif direction == 'bottom' then
incY = thickness
fullX = width
count = def.count or height
elseif direction == 'top' then
incY = 0 - thickness
fullX = width
count = def.count or height
else
print('direction has to be one of left, right, bottom, top')
return
end
-- configure amplitude
if amplitude == 'down' then
fullY = height
elseif amplitude == 'left' then
fullX = width
elseif amplitude == 'center' then
if direction == 'right' or direction == 'left' then
fullY = height/2
elseif direction == 'top' or direction == 'right' then
fullX = width/2
end
end
-- remove old history
if #graphHistory[key] > count then table.remove(graphHistory[key]) end
-- get the current max
if max == 'auto' then
max = 0
for i,value in pairs(graphHistory[key]) do
if max < value then
max = value
end
end
if max == 0 then return end
end
-- draw
for i,value in pairs(graphHistory[key]) do
local v = value / max
if v > 1 then v = 1 end
local s = {
x = round(def.pos.x + incX * (i-1)),
y = round(def.pos.y + incY * (i-1))
}
local e = {
x = round(s.x + (v * fullX), 1),
y = round(s.y + (v * fullY), 1)
}
if amplitude == 'center' then
s = {
x = round(s.x - (v * fullX), 1),
y = round(s.y - (v * fullY), 1),
}
end
if e.y ~= s.y or e.x ~= s.x then
line(cr, {
thickness = thickness, color = color, alpha = alpha,
from = {x = s.x + thickness/2, y = s.y + thickness/2},
to = {x = e.x + thickness/2, y = e.y + thickness/2},
})
end
end
end
function bar(cr, value, def)
def.caps = def.caps or CAIRO_LINE_CAP_ROUND
if def.background then
local bgDef = table.clone(def)
bgDef.color = def.background.color
bgDef.alpha = def.background.alpha
line(cr, bgDef)
end
local t = value / (def.max or 100)
if t > 1 then t = 1 end
if t == 0.0 then return end
def.to = {
x = def.from.x + t * (def.to.x - def.from.x),
y = def.from.y + t * (def.to.y - def.from.y),
}
if def.warn and (def.warn.from and def.warn.from <= value or def.warn.to and def.warn.to >= value) then
def.color = def.warn.color
end
if def.crit and (def.crit.from and def.crit.from <= value or def.crit.to and def.crit.to >= value) then
def.color = def.crit.color
end
line(cr, def)
end
function line(cr, def)
local thickness = def.thickness or 1
local color = def.color or 0xffffff
local alpha = def.alpha or 1
local caps = def.caps or CAIRO_LINE_CAP_BUTT
cairo_set_line_width(cr, thickness)
cairo_set_line_cap(cr, caps)
cairo_set_source_rgba(cr, rgbToRgba(color, alpha))
cairo_move_to(cr, def.from.x, def.from.y)
cairo_line_to(cr, def.to.x, def.to.y)
cairo_stroke(cr)
cairo_new_path(cr) -- just to be sure
end