diff options
author | William Wilgus <me.theuser@yahoo.com> | 2019-08-04 09:48:09 -0500 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2019-08-04 16:57:02 +0200 |
commit | f85df30e3e7da55c6880a321b3d8dc737f7af5b2 (patch) | |
tree | c778d743feef43162f53df9c1b642912822f664e /apps/plugins/lua/include_lua/draw_text.lua | |
parent | 4209c097705ce474f955bf1d73ca6ccf1ac6786a (diff) | |
download | rockbox-f85df30e3e7da55c6880a321b3d8dc737f7af5b2.tar.gz rockbox-f85df30e3e7da55c6880a321b3d8dc737f7af5b2.zip |
lua add rlimg.lua example script split large includes to separate files
Change-Id: I67cac5bc4ce5525ab30abf9443f6cc1a33190512
Diffstat (limited to 'apps/plugins/lua/include_lua/draw_text.lua')
-rw-r--r-- | apps/plugins/lua/include_lua/draw_text.lua | 121 |
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 | |||
25 | if not rb.lcd_framebuffer then rb.splash(rb.HZ, "No Support!") return nil end | ||
26 | |||
27 | do | ||
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 | ||
121 | end | ||