summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/include_lua/draw_text.lua
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/include_lua/draw_text.lua')
-rw-r--r--apps/plugins/lua/include_lua/draw_text.lua121
1 files changed, 121 insertions, 0 deletions
diff --git a/apps/plugins/lua/include_lua/draw_text.lua b/apps/plugins/lua/include_lua/draw_text.lua
new file mode 100644
index 0000000000..3722931c2c
--- /dev/null
+++ b/apps/plugins/lua/include_lua/draw_text.lua
@@ -0,0 +1,121 @@
1--[[ Lua Draw Text function
2/***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
10 *
11 * Copyright (C) 2017 William Wilgus
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22]]
23-- draw text onto image if width/height are supplied text is centered
24
25if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end
26
27do
28 -- Internal Constants
29 local rocklib_image = getmetatable(rb.lcd_framebuffer())
30 local _LCD = rb.lcd_framebuffer()
31 local LCD_W, LCD_H = rb.LCD_WIDTH, rb.LCD_HEIGHT
32 local BSAND = 8 -- blits color to dst if src <> 0
33 local _NIL = nil -- nil placeholder
34
35 local _clear = rocklib_image.clear
36 local _copy = rocklib_image.copy
37 local _newimg = rb.new_image
38
39
40 return function(img, x, y, width, height, font, color, text)
41 font = font or rb.FONT_UI
42
43 local opts = {x = 0, y = 0, width = LCD_W - 1, height = LCD_H - 1,
44 font = font, drawmode = 3, fg_pattern = 0x1, bg_pattern = 0}
45
46 if rb.LCD_DEPTH == 2 then -- invert 2-bit screens
47 --vp.drawmode = bit.bxor(vp.drawmode, 4)
48 opts.fg_pattern = 3 - opts.fg_pattern
49 opts.bg_pattern = 3 - opts.bg_pattern
50 end
51 rb.set_viewport(opts)
52
53 local res, w, h = rb.font_getstringsize(text, font)
54
55 if not width then
56 width = 0
57 else
58 width = (width - w) / 2
59 end
60
61 if not height then
62 height = 0
63 else
64 height = (height - h) / 2
65 end
66
67 -- make a copy of the current screen for later
68 --local screen_img = _newimg(LCD_W, LCD_H)
69 local screen_img = _newimg(LCD_W, h * 2)
70 _copy(screen_img, _LCD)
71
72 -- check if the screen buffer is supplied image if so set img to the copy
73 if img == _LCD then
74 img = screen_img
75 end
76
77 -- we will be printing the text to the screen then blitting into img
78 --rb.lcd_clear_display()
79 _clear(_LCD, opts.bg_pattern or 0, 1, 1, LCD_W, h * 2)
80
81 if w > LCD_W then -- text is too long for the screen do it in chunks
82 local l = 1
83 local resp, wp, hp
84 local lenr = text:len()
85
86 while lenr > 1 do
87 l = lenr
88 resp, wp, hp = rb.font_getstringsize(text:sub(1, l), font)
89
90 while wp >= LCD_W and l > 1 do
91 l = l - 1
92 resp, wp, hp = rb.font_getstringsize(text:sub( 1, l), font)
93 end
94
95 rb.lcd_putsxy(0, 0, text:sub(1, l))
96 text = text:sub(l)
97
98 if x + width > img:width() or y + height > img:height() then
99 break
100 end
101
102 -- using the mask we made blit color into img
103 _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
104 x = x + wp
105 --rb.lcd_clear_display()
106 _clear(_LCD, opts.bg_pattern or 0, 1, 1, LCD_W, h * 2)
107
108 lenr = text:len()
109 end
110 else --w <= LCD_W
111 rb.lcd_putsxy(0, 0, text)
112
113 -- using the mask we made blit color into img
114 _copy(img, _LCD, x + width, y + height, _NIL, _NIL, _NIL, _NIL, false, BSAND, color)
115 end
116
117 _copy(_LCD, screen_img) -- restore screen
118 rb.set_viewport() -- set viewport default
119 return res, w, h
120 end
121end