-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwxLoupe.lua
More file actions
701 lines (548 loc) · 18 KB
/
wxLoupe.lua
File metadata and controls
701 lines (548 loc) · 18 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
-- ----------------------------------------------------------------------------
--
-- wxLoupe - window for displaying a single char
--
-- ----------------------------------------------------------------------------
local wx = require "wx" -- uses wxWidgets for Lua 5.2
local palette = require "wxPalette" -- common colours definition in wxWidgets
-- local trace = require "trace" -- shortcut for tracing
local _floor = math.floor
local _format = string.format
local _find = string.find
local _concat = table.concat
-- ----------------------------------------------------------------------------
-- reference to the application
--
local m_App = nil
-- ----------------------------------------------------------------------------
-- window's private members
--
local m_Frame =
{
hWindow = nil, -- main frame
hBackDC = nil, -- background device context
hMemoryDC = nil, -- device context for the window
bTreat = true, -- append fix '\n' to character
fnDraw = nil, -- font big size
fnText = nil, -- font for details
sData = "", -- current character
sUnicode = "", -- Unicode number value
sDescription= " ", -- Unicode's description
sFontName = "", -- selected font name
iFontSize = -50, -- selected font size
rcClientW = 0,
rcClientH = 0,
}
-- ----------------------------------------------------------------------------
-- font names that do not need treatment
--
local m_tNames =
{
sNamesFile = nil, -- file name for Unicode's names
fhNames = nil, -- file handle " " " " "
tFileCache = { }, -- file's indexing
}
-- ----------------------------------------------------------------------------
-- font names that do not need treatment
--
local m_tTreat =
{
"Unifont",
"Gulim",
"Batang",
"Gungsuh",
}
-- ----------------------------------------------------------------------------
-- preallocated GDI objects
--
local m_penNULL = wx.wxPen(palette.Black, 0, wx.wxTRANSPARENT)
local m_brNULL = wx.wxBrush(palette.White, wx.wxTRANSPARENT)
local m_penRect = wx.wxPen(palette.White, 3, wx.wxDOT)
local m_brBack = wx.wxBrush(palette.Black, wx.wxSOLID)
local m_clrFore = palette.White
local m_clrExtra = palette.OrangeRed
-- ----------------------------------------------------------------------------
-- return the wxWindow handle
--
local function GetHandle()
return m_Frame.hWindow
end
-- ----------------------------------------------------------------------------
-- get a string and convert it to an Unicode reference number
-- expect an array made of 8 characters maximum
-- where each character is a nibble
--
local function _text2uni(inText)
if 1 == inText:len() then return string.byte(inText) end
local tBytes = { } -- split values
local iRefUTF8 = -1
for i=1, inText:len() do
tBytes[i] = inText:byte(i)
end
-- if the code entered is invalid these arithmetics will overflow
--
if 4 == #tBytes then
iRefUTF8 = ((tBytes[1] - 0xf0) * 0x40000) + ((tBytes[2] - 0x80) * 0x1000) + ((tBytes[3] - 0x80) * 0x40) + (tBytes[4] - 0x80)
elseif 3 == #tBytes then
if 0xe0 == tBytes[1] then
iRefUTF8 = ((tBytes[1] - 0xe0) * 0x1000) + ((tBytes[2] - 0x81) * 0x40) + (tBytes[3] - 0x80)
else
iRefUTF8 = ((tBytes[1] - 0xe0) * 0x1000) + ((tBytes[2] - 0x80) * 0x40) + (tBytes[3] - 0x80)
end
else -- if 2 == #tBytes then
iRefUTF8 = ((tBytes[1] - 0xc0) * 0x40) + (tBytes[2] - 0x80)
end
-- Unicode prints its documents with uppercase letters
-- Unicode uses a 5 nibbles format (not 6) for long codes
--
return iRefUTF8
end
-- ----------------------------------------------------------------------------
--
local function DrawChar(inDrawDC)
-- trace.line("DrawChar")
if not m_Frame.fnDraw then return end
local chCurrent = m_Frame.sData
if 0 == #chCurrent then return end
inDrawDC:SetFont(m_Frame.fnDraw)
inDrawDC:SetTextForeground(m_clrFore)
local iWidth, iHeight, iExtent, iLeading = inDrawDC:GetTextExtent(chCurrent)
-- center the character to the middle of the client area width
-- and leave some room on the top
--
local iLeft = _floor((m_Frame.rcClientW - iWidth) / 2)
local iTop = _floor((m_Frame.rcClientH - iHeight) / 8)
if 0 > iLeft then iLeft = 0 end
if 0 > iTop then iTop = 0 end
-- with wxWidgets many characters won't display at all
-- if the text is not completed with a '\n' or '\r',
-- there are exceptions that must be treated
--
if not m_Frame.bTreat then
inDrawDC:DrawText(chCurrent, iLeft, iTop)
else
inDrawDC:DrawText(chCurrent .. "\n", iLeft, iTop)
end
-- draw a thin bounding box for the char
--
inDrawDC:SetPen(m_penRect)
inDrawDC:SetBrush(m_brNULL)
inDrawDC:DrawRoundedRectangle(iLeft, iTop, iWidth, iHeight, 10)
if 0 < iLeading then
local iOffsetY = iTop + iLeading
inDrawDC:DrawLine(iLeft, iOffsetY, iLeft + iWidth, iOffsetY)
end
if 0 < iExtent then
local iOffsetY = iTop + iHeight - iExtent
inDrawDC:DrawLine(iLeft, iOffsetY, iLeft + iWidth, iOffsetY)
end
-- switch font
--
inDrawDC:SetFont(m_Frame.fnText)
inDrawDC:SetTextForeground(m_clrExtra)
-- draw the details
--
local iLength = chCurrent:len()
local sHexFmt = "0x%02x"
local sText
if 1 == iLength then
sText = _format(sHexFmt, chCurrent:byte(i))
else
local tToDraw = { }
for i=1, iLength do
tToDraw[#tToDraw + 1] = _format(sHexFmt, chCurrent:byte(i))
end
sText = _concat(tToDraw, " ")
end
-- do draw the char as UTF_8
--
local iExtX, iExtY = inDrawDC:GetTextExtent(sText)
local iPosX = iLeft + iWidth / 2 - iExtX / 2 -- center on X
local iPosY = iTop + iHeight + 25 -- align to bounding box bottom
inDrawDC:DrawText(sText, iPosX, iPosY)
-- corresponding Unicode value (16 bits)
--
sText = m_Frame.sUnicode
iExtX = inDrawDC:GetTextExtent(sText)
iPosX = iLeft + iWidth / 2 - iExtX / 2
iPosY = iPosY + iExtY
inDrawDC:DrawText(sText, iPosX, iPosY)
-- if got a Unicode's description then write it
-- (break at 'WITH' word to span on 2 lines, note
-- that Unicode's descriptions are uppercase)
--
if 0 < m_Frame.sDescription:len() then
local sLine1, sLine2
local iStart, iEnd = m_Frame.sDescription:find("WITH")
if iStart then
sLine1 = m_Frame.sDescription:sub(1, iStart - 1)
sLine2 = m_Frame.sDescription:sub(iEnd + 1)
else
sLine1 = m_Frame.sDescription
sLine2 = ""
end
if 0 < sLine1:len() then
iExtX = inDrawDC:GetTextExtent(sLine1)
iPosX = iLeft + iWidth / 2 - iExtX / 2
iPosY = iPosY + iExtY
inDrawDC:DrawText(sLine1, iPosX, iPosY)
end
if 0 < sLine2:len() then
iExtX = inDrawDC:GetTextExtent(sLine2)
iPosX = iLeft + iWidth / 2 - iExtX / 2
iPosY = iPosY + iExtY
inDrawDC:DrawText(sLine2, iPosX, iPosY)
end
end
end
-- ----------------------------------------------------------------------------
--
local function NewMemDC()
-- trace.line("NewMemDC")
-- create a bitmap wide as the client area
--
local memDC = m_Frame.hMemoryDC
if not memDC then
local bitmap = wx.wxBitmap(m_Frame.rcClientW, m_Frame.rcClientH)
memDC = wx.wxMemoryDC()
memDC:SelectObject(bitmap)
end
-- draw the background
--
if not m_Frame.hBackDC then return end
memDC:Blit(0, 0, m_Frame.rcClientW, m_Frame.rcClientH, m_Frame.hBackDC, 0, 0, wx.wxBLIT_SRCCOPY)
-- draw the current char
--
DrawChar(memDC)
return memDC
end
-- ----------------------------------------------------------------------------
--
local function NewBackground()
-- trace.line("NewBackground")
-- create a bitmap wide as the client area
--
local memDC = wx.wxMemoryDC()
local bitmap = wx.wxBitmap(m_Frame.rcClientW, m_Frame.rcClientH)
memDC:SelectObject(bitmap)
-- draw the background
--
memDC:SetPen(m_penNULL)
memDC:SetBrush(m_brBack)
memDC:DrawRectangle(0, 0, m_Frame.rcClientW, m_Frame.rcClientH)
-- select font and do the draw in the middle
--
if m_Frame.fnText then
memDC:SetFont(m_Frame.fnText)
memDC:SetTextForeground(m_clrFore) -- colour follows big font
-- show the current font selected
--
local sDraw = _format("(%d %s)", m_Frame.iFontSize, m_Frame.sFontName)
local iExtX, iExtY = memDC:GetTextExtent(sDraw)
local iLeft = _floor(m_Frame.rcClientW / 2)
local iPosX = iLeft - iExtX / 2 -- center on X
local iTopY = m_Frame.rcClientH - iExtY - 10 -- align to bottom of window
memDC:DrawText(sDraw, iPosX, iTopY)
end
return memDC
end
-- ----------------------------------------------------------------------------
--
local function RefreshBackground()
-- trace.line("RefreshBackground")
if m_Frame.hBackDC then
m_Frame.hBackDC:delete()
m_Frame.hBackDC = nil
end
m_Frame.hBackDC = NewBackground()
end
-- ----------------------------------------------------------------------------
--
local function Refresh()
-- trace.line("Refresh")
m_Frame.hMemoryDC = NewMemDC()
if m_Frame.hWindow then
m_Frame.hWindow:Refresh(false)
end
end
-- ----------------------------------------------------------------------------
-- regenerate the offscreen buffer
--
local function RefreshAll()
-- trace.line("RefreshAll")
RefreshBackground()
Refresh()
end
-- ----------------------------------------------------------------------------
-- we just splat the off screen dc over the current dc
--
local function OnPaint()
-- trace.line("OnPaint")
if not m_Frame.hMemoryDC then return end
local dc = wx.wxPaintDC(m_Frame.hWindow)
dc:Blit(0, 0, m_Frame.rcClientW, m_Frame.rcClientH, m_Frame.hMemoryDC, 0, 0, wx.wxBLIT_SRCCOPY)
dc:delete()
end
-- ----------------------------------------------------------------------------
--
local function OnSize(event)
-- trace.line("OnSize")
local size = event:GetSize()
m_Frame.rcClientW = size:GetWidth() - 4
m_Frame.rcClientH = size:GetHeight() - 36
if m_Frame.hMemoryDC then
m_Frame.hMemoryDC:delete()
m_Frame.hMemoryDC = nil
end
RefreshAll()
end
-- ----------------------------------------------------------------------------
-- handle the show window event, starts the timer
--
local function OnShow(event)
-- trace.line("OnShow")
if not m_Frame.hWindow then return end
if event:GetShow() then RefreshAll() end
end
-- ----------------------------------------------------------------------------
--
local function IsWindowVisible()
-- trace.line("IsWindowVisible")
local wFrame = m_Frame.hWindow
if not wFrame then return false end
return wFrame:IsShown()
end
-- ----------------------------------------------------------------------------
-- set the font properties
--
local function SetupFont(inFontSize, inFontName)
-- trace.line("SetupFont")
-- allocate the requested font name with specified font size
--
local fnDraw = wx.wxFont(-1 * inFontSize, wx.wxFONTFAMILY_DEFAULT, wx.wxFONTFLAG_ANTIALIASED,
wx.wxFONTWEIGHT_NORMAL, false, inFontName, wx.wxFONTENCODING_SYSTEM)
-- this font instead has hardcoded properties
--
local fnText = wx.wxFont(-14, wx.wxFONTFAMILY_MODERN, wx.wxFONTFLAG_ANTIALIASED,
wx.wxFONTWEIGHT_NORMAL, false, "DejaVu Sans Mono")
-- check if characters must be treated
--
m_Frame.bTreat = true
for _, fontName in ipairs(m_tTreat) do
if fontName == inFontName then m_Frame.bTreat = false break end
end
-- store for later
--
m_Frame.fnDraw = fnDraw
m_Frame.fnText = fnText
m_Frame.sFontName = inFontName -- selected font name
m_Frame.iFontSize = inFontSize -- selected font size
if IsWindowVisible() then RefreshAll() end
end
-- ----------------------------------------------------------------------------
-- set the colour properties
--
local function SetupColour(inBack, inFront, inExtra)
-- trace.line("SetupColour")
m_brBack = wx.wxBrush(inBack, wx.wxSOLID)
m_clrFore = inFront
m_clrExtra = inExtra
-- this is the colour used for the bounding rectangle
--
local colour = wx.wxColour(0xff - inBack:Red(),
0xff - inBack:Green(),
0xff - inBack:Blue())
m_penRect = wx.wxPen(colour, 3, wx.wxDOT)
if IsWindowVisible() then RefreshAll() end
end
-- ----------------------------------------------------------------------------
-- binary search for the index in this special table for the file in memory
-- table's rows are:
-- {Unicode value, seek position}
--
local function SeekPosLookup(inUnicode)
local tCached = m_tNames.tFileCache
local iStart = 1
local iEnd = #tCached
local iIndex
-- check for the very first row that happens to start with a zero
--
if (0 == inUnicode) and (0 < #tCached) then return tCached[1][2] end
-- do the scan
--
while iStart <= iEnd do
iIndex = _floor(iStart + (iEnd - iStart) / 2)
if tCached[iIndex][1] == inUnicode then return tCached[iIndex][2] end
if tCached[iIndex][1] < inUnicode then iStart = iIndex + 1 else iEnd = iIndex - 1 end
end
return -1
end
-- ----------------------------------------------------------------------------
-- check for a description in the name's file
--
local function GetDescription(inUnicode)
-- sanity check
--
if 0 > inUnicode then return "" end
if not m_tNames.fhNames then return "" end
-- get the seek position
--
local iSeekPos = SeekPosLookup(inUnicode)
local sText = ""
if -1 < iSeekPos then
m_tNames.fhNames:seek("set", iSeekPos) -- seek pos
sText = m_tNames.fhNames:read("*l") -- read line
sText = sText:sub(sText:find("\t") + 1) -- extract description
end
return sText
end
-- ----------------------------------------------------------------------------
-- update the current character
--
local function SetData(inBytes)
-- trace.line("SetData")
if inBytes ~= m_Frame.sData then
m_Frame.sData = inBytes or ""
m_Frame.sUnicode = ""
m_Frame.sDescription = ""
if 0 < m_Frame.sData:len() then
local iUnicode = _text2uni(inBytes)
m_Frame.sUnicode = _format("U+%04X", iUnicode)
m_Frame.sDescription = GetDescription(iUnicode)
end
Refresh()
end
end
-- ----------------------------------------------------------------------------
-- associate the Unicode's names file (if any)
-- bare file format has
-- Unicode value tab description
-- file indexing is made of rows like
-- {Unicode value, seek offset}
--
local function SetNamesFile(inFilename)
-- trace.line("SetNamesFile")
m_tNames.sNamesFile = inFilename
m_tNames.fhNames = nil -- reset
m_tNames.tFileCache = { }
-- sanity check
--
if not inFilename then return end
local fhSrc = io.open(inFilename, "r")
if not fhSrc then return end
-- read all lines (with newline)
--
local tLines = { }
local tCurr = {0, 0}
local iSeek = 0
local iStart, iEnd
local iUnicode
wx.wxBeginBusyCursor()
local sLine = fhSrc:read("*L") -- note that we must read all the line
while sLine do
iStart = _find(sLine, "\t")
if iStart and 1 < iStart then
iUnicode = tonumber(sLine:sub(1, iStart - 1), 16)
if iUnicode then
tCurr[1] = iUnicode -- Unicode value
tCurr[2] = iSeek -- seek position
tLines[#tLines + 1] = tCurr -- add row to lookup
tCurr = {0, 0} -- make new row
end
end
-- update bytes counter and read next line
--
iSeek = iSeek + sLine:len()
sLine = fhSrc:read("*L")
end
-- store for reading
--
m_tNames.fhNames = fhSrc -- valid file's handle
m_tNames.tFileCache = tLines -- lookup table
wx.wxEndBusyCursor()
end
-- ----------------------------------------------------------------------------
-- show/hide the window
--
local function DisplayWindow(inShowStatus)
-- trace.line("DisplayWindow")
local wFrame = m_Frame.hWindow
if not wFrame then return end
-- display/hide
--
wFrame:Show(inShowStatus)
end
-- ----------------------------------------------------------------------------
--
local function OnClose()
-- trace.line("OnClose")
if m_tNames.fhNames then -- if left open release file
m_tNames.fhNames:close()
m_tNames.tFileCache = { } -- .. and release any memory
end
local wFrame = m_Frame.hWindow
if not wFrame then return end
-- finally destroy the window
--
wFrame:Destroy(wFrame)
m_Frame.hWindow = nil
end
-- ----------------------------------------------------------------------------
--
local function CloseWindow()
-- trace.line("CloseWindow")
OnClose()
end
-- ----------------------------------------------------------------------------
-- create the main window
--
local function CreateWindow(inApp, inParent, inConfig)
-- trace.line("CreateWindow")
inParent = inParent or wx.NULL
-- flags in use for the frame
--
local dwFrameFlags = bit32.bor(wx.wxFRAME_TOOL_WINDOW, wx.wxCAPTION)
dwFrameFlags = bit32.bor(dwFrameFlags, wx.wxRESIZE_BORDER)
dwFrameFlags = bit32.bor(dwFrameFlags, wx.wxFRAME_FLOAT_ON_PARENT)
-- create a window
--
local ptLeft = inConfig[1]
local ptTop = inConfig[2]
local siWidth = inConfig[3]
local siHeight = inConfig[4]
local frame = wx.wxFrame(inParent, wx.wxID_ANY, "Loupe",
wx.wxPoint(ptLeft, ptTop),
wx.wxSize(siWidth, siHeight),
dwFrameFlags)
-- standard event handlers
--
frame:Connect(wx.wxEVT_SHOW, OnShow)
frame:Connect(wx.wxEVT_PAINT, OnPaint)
frame:Connect(wx.wxEVT_SIZE, OnSize)
frame:Connect(wx.wxEVT_CLOSE_WINDOW, OnClose)
-- this is necessary to avoid flickering
-- (comment the line if running with the debugger
-- and the Lua's version is below 5.2)
--
frame:SetBackgroundStyle(wx.wxBG_STYLE_CUSTOM)
-- store for later
--
m_Frame.hWindow = frame
m_App = inApp
end
-- ----------------------------------------------------------------------------
--
return
{
GetHandle = GetHandle,
Create = CreateWindow,
Display = DisplayWindow,
Close = CloseWindow,
IsVisible = IsWindowVisible,
SetupFont = SetupFont,
SetupColour = SetupColour,
SetData = SetData,
SetNames = SetNamesFile,
}
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------