-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.lua
308 lines (231 loc) · 7.73 KB
/
main.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
-- main.lua
--[[
Demonstration of textrender.lua, a module for rendering styled text.
textrender parameters:
text = text to render
font = font name, e.g. "AvenirNext-DemiBoldItalic"
size = font size in pixels
lineHeight = line height in pixels
color = text color in an RGBa color table, e.g. {250, 0, 0, 255}
width = Width of the text column,
textAlignment = text alignment: "Left", "Right", "Center"
opacity = text opacity (between 0 and 1, or as a percent, e.g. "50%" or 0.5
minCharCount = Minimum number of characters per line. Estimate low, e.g. 5
targetDeviceScreenSize = String of target screen size, in the form, "width,height", e.g. e.g. "1024,768". May be different from current screen size.
letterspacing = (unused)
maxHeight = Maximum height of the text column. Extra text will be hidden.
minWordLen = Minimum length of a word shown at the end of a line. In good typesetting, we don't end our lines with single letter words like "a", so normally this value is 2.
textstyles = A table of text styles, loaded using funx.loadTextStyles()
defaultStyle = The name of the default text style for the text block
cacheDir = the name of the cache folder to use inside system.CachesDirectory, e.g. "text_render_cache"
--]]
-- Patches to allow Graphics 1.0 calls while using Graphics 2.0
require( 'scripts.dmc.dmc_kompatible' )
--require( 'scripts.dmc.dmc_kolor' )
--require ( 'scripts.patches.refPointConversions' )
-- Default anchor settings
--display.setDefault( "anchorX", 0 )
--display.setDefault( "anchorY", 0 )
-- My useful function collection
local funx = require("funx")
local textwrap = require("textrender")
-- Make a local copy of the application settings global
local screenW, screenH = display.contentWidth, display.contentHeight
local viewableScreenW, viewableScreenH = display.viewableContentWidth, display.viewableContentHeight
local screenOffsetW, screenOffsetH = display.contentWidth - display.viewableContentWidth, display.contentHeight - display.viewableContentHeight
local midscreenX = screenW*(0.5)
local midscreenY = screenH*(0.5)
local w = screenW/2 - 50
--w = 270
local textStyles = funx.loadTextStyles("textstyles.txt", system.ResourceDirectory)
local mytext = funx.readFile("sampletext.txt")
-- To cache, set the cache directory
local cacheDir = "textrender_cache"
funx.mkdir (cacheDir, "",false, system.CachesDirectory)
------------------------------------------------------------
------------------------------------------------------------
local reps = 1
------------------------------------------------------------
------------------------------------------------------------
-- To prevent caching, set the cache dir to empty
--cacheDir = ""
local params = {
text = mytext,
font = "AvenirNext-Regular",
size = "12",
lineHeight = "16",
color = {0, 0, 0, 255},
width = w,
alignment = "Left",
opacity = "100%",
minCharCount = 5, -- Minimum number of characters per line. Start low.
targetDeviceScreenSize = screenW..","..screenH, -- Target screen size, may be different from current screen size
letterspacing = 0,
maxHeight = screenH - 50,
minWordLen = 2,
textstyles = textStyles,
defaultStyle = "Normal",
cacheDir = "",
cacheToDB = true,
isHTML = true,
useHTMLSpacing = true,
}
------------
-- Speed tests, cache vs. no cache
local function drawAndDelete(params, reps)
local startTime = system.getTimer()
reps = reps or 10
for i = 1, reps do
--print ("Render text #" .. i)
local t = textwrap.autoWrappedText(params)
t:setReferencePoint(display.TopLeftReferencePoint)
t.x = 50
t.y = 50
t:removeSelf()
t = nil
end
local tdiff = system.getTimer() - startTime
return tdiff
end
local tmsg = ""
-- Page background
local bkgd = display.newRect(0,0,screenW, screenH)
bkgd:setFillColor(255,255,255,255)
bkgd:setReferencePoint(display.TopLeftReferencePoint)
bkgd.x = 0
bkgd.y = 0
bkgd.strokeWidth = 20
bkgd:setStrokeColor(0,200,200)
local t, t2, textframe, textframe2
local gobutton
local function go()
gobutton.isVisible = false
------------
-- We clear the cache before begining, so first render creates a cache, second uses it.
textwrap.clearAllCaches(cacheDir)
if t then
t:removeSelf()
t = nil
end
if t2 then
t2:removeSelf()
t2 = nil
end
if textframe then
textframe:removeSelf()
textframe = nil
end
if textframe2 then
textframe2:removeSelf()
textframe2 = nil
end
if (reps > 10) then
params.cacheDir = ""
params.cacheToDB = false
params.text = mytext
-- RENDER WITHOUT CACHING
local tdiffNoCache = drawAndDelete(params,reps)
params.cacheDir = ""
params.cacheToDB = true
params.text = mytext
-- RENDER USING THE CACHE
local tdiffCached = drawAndDelete(params, reps)
------------
tmsg = "RENDERING TIME\n"
tmsg = tmsg .. "<br>"
tmsg = tmsg .. "<br>"
tmsg = tmsg .. "<br>"
tmsg = tmsg .. "<p>"
tmsg = tmsg .. "NO CACHE: ("..reps.. " times) Time elapsed = " .. math.floor(tdiffNoCache) .. " microseconds (".. tdiffNoCache/1000 .." seconds) (average = " .. (math.floor(tdiffNoCache)/reps) .. " microseconds)"
tmsg = tmsg .. "</p>"
tmsg = tmsg .. "<p>"
tmsg = tmsg .. "\nCACHED: ("..reps.. " times) Time elapsed = " .. math.floor(tdiffCached) .. " microseconds (".. tdiffCached/1000 .." seconds) (average = " .. (math.floor(tdiffCached)/reps) .. " microseconds)"
tmsg = tmsg .. "</p>"
--params.text = tmsg
--local tout = textwrap.autoWrappedText(tmsg)
--tout:setReferencePoint(display.TopLeftReferencePoint)
--tout.x = 200
--tout.y = 100
end
-- PRINT THE RESULTS
params.text = mytext .. tmsg
params.cacheDir = nil
params.cacheToDB = true
t = textwrap.autoWrappedText(params)
t.x = 20
t.y = 100 + t.yAdjustment
params.testing = false
params.cacheDir = ""
params.cacheToDB = true
t2 = textwrap.autoWrappedText(params)
t2.x = w + 50
t2.y = 100 + t.yAdjustment
local yAdjustment = t.yAdjustment
-- Labels
local label1 = display.newText("This Text is from XML", 0, 0, nil, 18)
label1:setReferencePoint(display.BottomLeftReferencePoint)
label1.x = 20
label1.y = 90
label1:setFillColor(100,100,0) -- transparent
local label2 = display.newText("This Text is from the Cache", 0, 0, nil, 18)
label2:setReferencePoint(display.BottomLeftReferencePoint)
label2.x = t2.x
label2.y = 90
label2:setFillColor(100,100,0) -- transparent
-- Frame the text
textframe = display.newRect(0,0, w+2, t.height + 2)
textframe:setFillColor(100,100,0,20) -- transparent
textframe:setStrokeColor(0,0,0,255)
textframe.strokeWidth = 1
textframe:setReferencePoint(display.TopLeftReferencePoint)
textframe.x = 20
textframe.y = 100
textframe:toBack()
-- Frame the text
textframe2 = display.newRect(0,0, w+2, t.height + 2)
textframe2:setFillColor(100,100,0,20) -- transparent
textframe2:setStrokeColor(0,0,0,255)
textframe2.strokeWidth = 1
textframe2:setReferencePoint(display.TopLeftReferencePoint)
textframe2.x = t2.x
textframe2.y = 100
textframe2:toBack()
bkgd:toBack()
gobutton.isVisible = true
end
if textwrap.db and textwrap.db:isopen() then
textwrap.db:close()
print ("MAIN TESTING: Close DB. The textwrap database was left open?")
end
local widget = require ( "widget" )
local wf = false
local function toggleWireframe()
wf = not wf
display.setDrawMode( "wireframe", wf )
if (not wf) then
display.setDrawMode( "forceRender" )
end
print ("WF = ",wf)
end
local wfb = widget.newButton{
label = "WIREFRAME",
labelColor = { default={ 200, 1, 1 }, over={ 250, 0, 0, 0.5 } },
fontSize = 20,
x =screenW - 100,
y=50,
onRelease = toggleWireframe,
}
wfb:toFront()
gobutton = widget.newButton{
label = "GO",
labelColor = { default={ 200, 1, 1 }, over={ 250, 0, 0, 0.5 } },
fontSize = 20,
x =screenW - 200,
y=50,
onRelease = go,
}
gobutton:toFront()
-- Run if we're not testing reps
if (reps == 1) then
go()
end