-
Notifications
You must be signed in to change notification settings - Fork 11
/
TextRender.ahk
2986 lines (2536 loc) · 147 KB
/
TextRender.ahk
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Script: TextRender.ahk
; License: MIT License
; Author: Edison Hua (iseahound)
; Github: https://github.com/iseahound/TextRender
; Date: 2024-11-22
; Version: 1.9.3
#Requires AutoHotkey v2.0-beta.13+
; TextRender() - Display custom text on screen.
class TextRender {
static call(text:="", background_style:="", text_style:="") {
if (text == "" && background_style == "" && text_style == "")
return super()
return super().Render(text, background_style, text_style)
}
__New(title := "", style := 0x80000000, styleEx := 0x80088, parent := 0
, OffsetLeft := 0, OffsetTop := 0, ScaleWidth := False, ScaleHeight := False) {
TextRender.gdiplusStartup()
; Create the window and save a reference to this instance.
hwnd := this.CreateWindow(title, style, styleEx, parent)
DllCall("SetWindowLong" (A_PtrSize=8 ? "Ptr":""), "ptr", hwnd, "int", 0, "ptr", ObjPtr(this))
; Check if a custom parent window was set and save it.
parent := DllCall("GetAncestor", "ptr", hwnd, "uint", 1, "ptr")
(parent == DllCall("GetDesktopWindow", "ptr")) && parent := 0
; Show the window without activating it. (WS_VISIBLE wouldn't work here.)
DllCall("ShowWindow", "ptr", hwnd, "int", 4) ; SW_SHOWNOACTIVATE
; Save parameters.
this.hwnd := hwnd
this.parent := parent
this.OffsetLeft := OffsetLeft
this.OffsetTop := OffsetTop
this.ScaleWidth := ScaleWidth
this.ScaleHeight := ScaleHeight
; These are always preserved between all calls.
this.style1 := ""
this.style2 := ""
this.layers := []
this.status := 0xFFFF0001 ; Resets when the lower 16 bits overflow.
; Initalize default events.
this.events := Map()
this.OnEvent("LeftMouseDown", this.EventMoveWindow)
this.OnEvent("MiddleMouseDown", this.EventShowCoordinates)
this.OnEvent("RightMouseDown", this.EventCopyData)
; The current memory state is uninitialized, and will be allocated by UpdateMemory().
this.memorystate := 0
return this
}
__Delete() {
; __Delete → DestroyWindow → WM_DESTROY → FreeMemory → Remove Persistence → ExitApp
this.DestroyWindow() ; Calls FreeMemory()
TextRender.gdiplusShutdown()
}
; Window Styles
Default() {
; Left Click to drag. Right click to close.
return this
.OnEvent("MiddleMouseDown", "")
.OnEvent("RightMouseDown", "")
.OnEvent("RightMouseUp", this.DestroyWindow)
}
None() {
; Removes all events.
return this
.OnEvent("LeftMouseDown", "")
.OnEvent("MiddleMouseDown", "")
.OnEvent("RightMouseDown", "")
}
Show() {
DllCall("ShowWindow", "ptr", this.hwnd, "int", 4) ; SW_SHOWNOACTIVATE
return this
}
Hide() {
DllCall("ShowWindow", "ptr", this.hwnd, "int", 0) ; SW_HIDE
return this
}
ToggleVisible() {
DllCall("IsWindowVisible", "ptr", this.hwnd) ? this.Hide() : this.Show()
return this
}
TopMost() {
WinSetAlwaysOnTop 1, "ahk_id" this.hwnd
return this
}
AlwaysOnTop() {
WinSetAlwaysOnTop -1, "ahk_id" this.hwnd
return this
}
ClickThrough() {
return this.ToggleExStyle(0x20)
}
NoActivate() {
return this.ToggleExStyle(0x8000000)
}
ToggleStyle(long) {
return this.ToggleWindowLong(-16, long)
}
ToggleExStyle(long) {
return this.ToggleWindowLong(-20, long)
}
ToggleWindowLong(index, long) {
value := DllCall("GetWindowLong", "ptr", this.hwnd, "int", index, "int")
(value & long) == long
? DllCall("SetWindowLong", "ptr", this.hwnd, "int", index, "int", value ^ long)
: DllCall("SetWindowLong", "ptr", this.hwnd, "int", index, "int", value | long)
return this
}
; Simple Questions and Tests
InBounds() { ; Requires memorystate 2 or greater
; Check if canvas coordinates are inside bitmap coordinates.
return this.x >= this.BitmapLeft
and this.y >= this.BitmapTop
and this.x2 <= this.BitmapRight
and this.y2 <= this.BitmapBottom
}
Bounds() { ; Requires memorystate 2 or greater
return [this.x, this.y, this.x2, this.y2]
}
Rect() { ; Requires memorystate 2 or greater
return [this.x, this.y, this.w, this.h]
}
; Renders and Effects
RenderAgain() {
if (this.memorystate < 3)
return this
DllCall("QueryPerformanceFrequency", "int64*", &frequency:=0)
DllCall("QueryPerformanceCounter", "int64*", &end:=0)
time_elapsed := (end - this.t0) / frequency * 1000
remaining_time := this.t - time_elapsed
if (this.t == 0 || remaining_time > 0) {
this.UpdateMemory()
this.Redraw()
this.UpdateLayeredWindow()
; Create a timer that eventually clears the canvas.
if (remaining_time > 0) {
; Create a reference to the object held by a timer.
blank := ObjBindMethod(this, "blank", this.status) ; Calls Blank()
SetTimer blank, -remaining_time ; Calls __Delete.
}
}
this.memorystate := 3
return this
}
Render(terms*) {
this.Draw(terms*)
; Reminder: Only the visible screen area will be rendered. Clipping will occur.
this.UpdateLayeredWindow()
; Start Timestamp
DllCall("QueryPerformanceCounter", "int64*", &start:=0)
this.t0 := start
; Create a timer that eventually clears the canvas.
if (this.t > 0) {
; Create a reference to the object held by a timer.
blank := ObjBindMethod(this, "blank", this.status) ; Calls Blank()
SetTimer blank, -this.t ; Calls __Delete.
}
; Ensure that Flush() will be called at the start of a new drawing.
; This approach keeps this.layers and the underlying graphics intact,
; so that calls to Save() and Screenshot() will not encounter a blank canvas.
this.memorystate := 3
return this
}
RenderOnScreen(terms*) {
this.Draw(terms*)
; Allow Render() to commit when previous Draw() has happened.
if (this.layers.length > 0) {
; Use the default rendering when the canvas coordinates fall within the bitmap area.
if this.InBounds()
return this.Render(terms*)
; Render objects that reside off screen.
; Create a new bitmap using the width and height of the canvas object.
hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
bi := Buffer(40, 0) ; sizeof(bi) = 40
NumPut( "uint", 40, bi, 0) ; Size
NumPut( "int", this.w, bi, 4) ; Width
NumPut( "int", -this.h, bi, 8) ; Height - Negative so (0, 0) is top-left.
NumPut("ushort", 1, bi, 12) ; Planes
NumPut("ushort", 32, bi, 14) ; BitCount / BitsPerPixel
hbm := DllCall("CreateDIBSection", "ptr", hdc, "ptr", bi, "uint", 0, "ptr*", &pBits:=0, "ptr", 0, "uint", 0, "ptr")
obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
DllCall("gdiplus\GdipCreateFromHDC", "ptr", hdc, "ptr*", &Graphics:=0)
DllCall("gdiplus\GdipTranslateWorldTransform", "ptr", Graphics, "float", -this.x, "float", -this.y, "int", 0)
; Redraw on the canvas.
for i, layer in this.layers
this.DrawOnGraphics(Graphics, layer[1], layer[2], layer[3], this.BitmapWidth, this.BitmapHeight)
; Show the objects on screen.
; This suffers from a windows limitation in that windows will appear in places that do not match the intended coordinates.
; Therefore this is not the default rendering approach as style commands are not respected.
DllCall("UpdateLayeredWindow"
, "ptr", this.hwnd ; hWnd
, "ptr", 0 ; hdcDst
,"uint64*", this.x | this.y << 32 ; *pptDst
,"uint64*", this.w | this.h << 32 ; *psize
, "ptr", hdc ; hdcSrc
,"uint64*", 0 ; *pptSrc
, "uint", 0 ; crKey
, "uint*", 0xFF << 16 | 0x01 << 24 ; *pblend
, "uint", 2) ; dwFlags
; Adjust location
DllCall("SetWindowPos", "ptr", this.hwnd, "ptr", 0, "int", this.x, "int", this.y, "int", 0, "int", 0
, "uint", 0x400 | 0x10 | 0x4 | 0x1) ; SWP_NOSENDCHANGING | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE
; Cleanup
DllCall("gdiplus\GdipDeleteGraphics", "ptr", Graphics)
DllCall("SelectObject", "ptr", hdc, "ptr", obm)
DllCall("DeleteObject", "ptr", hbm)
DllCall("DeleteDC", "ptr", hdc)
; Set Coordinates
WinGetPos &x, &y, &w, &h, "ahk_id " this.hwnd
this.WindowLeft := x
this.WindowTop := y
this.WindowWidth := w
this.WindowHeight := h
this.WindowRight := this.WindowLeft + this.WindowWidth
this.WindowBottom := this.WindowTop + this.WindowHeight
}
; Start Timestamp
DllCall("QueryPerformanceCounter", "int64*", &start:=0)
this.t0 := start
; Create a timer that eventually clears the canvas.
if (this.t > 0) {
; Create a reference to the object held by a timer.
blank := ObjBindMethod(this, "blank", this.status) ; Calls Blank()
SetTimer blank, -this.t ; Calls __Delete.
}
this.memorystate := 3
return this
}
; Fade is actually broken with how it interfaces with Render().
Fade(fade_in := 250, fade_out := 250, status := "") {
if (fade_in > 0) {
; Render: Off-Screen areas are not rendered. Clip objects that reside off screen.
duration := 0
current := -1
;count := 0
DllCall("QueryPerformanceFrequency", "int64*", &frequency:=0)
DllCall("QueryPerformanceCounter", "int64*", &start:=0)
while (duration < fade_in) {
alpha := Ceil(duration/fade_in * 255)
if (alpha != current) {
;if (count != alpha)
; FileAppend % count ", " alpha "`n", log.txt
;count++
this.UpdateLayeredWindow(alpha)
current := alpha
}
DllCall("QueryPerformanceCounter", "int64*", &now:=0)
duration := (now - start)/frequency * 1000
}
if (alpha != 255)
this.UpdateLayeredWindow()
; Start Timestamp
DllCall("QueryPerformanceCounter", "int64*", &start:=0)
this.t0 := start
; Create a timer that eventually clears the canvas.
if (this.t > 0) {
; Create a reference to the object held by a timer.
fade := ObjBindMethod(this, "fade", 0, fade_out, this.status) ; Calls Fade() with no fade_in.
SetTimer fade, -this.t ; Calls __Delete.
}
this.memorystate := 3
return this
}
; Check to see if the state of the canvas has changed before clearing and updating.
if (fade_out > 0 && this.status = status) {
duration := 0
current := -1
;count := 0
DllCall("QueryPerformanceFrequency", "int64*", &frequency:=0)
DllCall("QueryPerformanceCounter", "int64*", &start:=0)
while (duration < fade_out) {
alpha := 255 - Ceil(duration/fade_out * 255)
if (alpha != current) {
;if (count != alpha)
; FileAppend % count ", " alpha "`n", log.txt
;count++
this.UpdateLayeredWindow(alpha)
current := alpha
}
DllCall("QueryPerformanceCounter", "int64*", &now:=0)
duration := (now - start)/frequency * 1000
}
this.UpdateLayeredWindow(0)
return this
}
}
; Basic Methods
Draw(data := "", style1 := "", style2 := "") {
; Check if the screen or window or window size has changed.
this.UpdateMemory() ; Calls LoadMemory() if needed.
; Redraw the canvas if any layers are present.
if (this.memorystate == 1)
this.Redraw()
; Clear the canvas as it has been rendered to screen.
if (this.memorystate == 3)
this.Flush()
; Use previous styles if blank.
(style1 = "" && style2 = "") && (style1 := this.style1, style2 := this.style2)
; Save data and styles into layers.
this.data := data
this.style1 := style1, this.style2 := style2
this.layers.push([data, style1, style2])
this.Paint(data, style1, style2)
; Create a unique signature for each call to Draw().
this.CanvasChanged()
this.memorystate := 2
return this
}
Paint(data := "", style1 := "", style2 := "") {
; Drawing
try dpi := DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr")
obj := this.DrawOnGraphics(this.Graphics, data, style1, style2
, this.ScaleWidth ? this.BitmapWidth : A_ScreenWidth
, this.ScaleHeight ? this.BitmapHeight : A_ScreenHeight)
try DllCall("SetThreadDpiAwarenessContext", "ptr", dpi, "ptr")
; Set canvas coordinates.
this.t := this.HasProp("t") ? max(this.t, obj.t) : obj.t
this.x := this.HasProp("x") ? min(this.x, obj.x) : obj.x
this.y := this.HasProp("y") ? min(this.y, obj.y) : obj.y
this.x2 := this.HasProp("x2") ? max(this.x2, obj.x2) : obj.x2
this.y2 := this.HasProp("y2") ? max(this.y2, obj.y2) : obj.y2
this.w := this.x2 - this.x
this.h := this.y2 - this.y
this.chars := obj.chars
this.words := obj.words
this.lines := obj.lines
}
Flush() {
if (this.memorystate < 2)
return this
DllCall("gdiplus\GdipSetClipRect", "ptr", this.Graphics, "float", this.x, "float", this.y, "float", this.w, "float", this.h, "int", 0)
DllCall("gdiplus\GdipGraphicsClear", "ptr", this.Graphics, "uint", 0x00FFFFFF) ; All colors are the same speed.
DllCall("gdiplus\GdipResetClip", "ptr", this.Graphics)
this.CanvasChanged()
try this.DeleteProp("t")
try this.DeleteProp("x")
try this.DeleteProp("y")
try this.DeleteProp("x2")
try this.DeleteProp("y2")
try this.DeleteProp("w")
try this.DeleteProp("h")
try this.DeleteProp("chars")
try this.DeleteProp("words")
try this.DeleteProp("lines")
; Redraws are no longer possible!
this.layers := []
this.memorystate := 1
return this
}
Redraw() {
if (this.memorystate == 0)
this.UpdateMemory()
if (this.memorystate > 1)
return this
for i, layer in this.layers
this.Paint(layer*)
this.memorystate := 2
return this
}
Clear() {
this.Flush()
this.UpdateLayeredWindow(0)
return this
}
UpdateLayeredWindow(alpha := 255) {
if (this.memorystate == 1) {
; Make the window completely invisible but preserves what was already on screen.
DllCall("UpdateLayeredWindow"
, "ptr", this.hwnd ; hWnd
, "ptr", 0 ; hdcDst
, "ptr", 0 ; *pptDst
, "ptr", 0 ; *psize
, "ptr", 0 ; hdcSrc
, "ptr", 0 ; *pptSrc
, "uint", 0 ; crKey
, "uint*", 0 << 16 | 0x01 << 24 ; *pblend
, "uint", 2 ; dwFlags
, "int") ; Success = 1
}
if (this.memorystate >= 2) {
; Define the smaller of canvas and bitmap coordinates.
x := this.WindowLeft := max(this.BitmapLeft, this.x)
y := this.WindowTop := max(this.BitmapTop, this.y)
x2 := this.WindowRight := min(this.BitmapRight, this.x2)
y2 := this.WindowBottom := min(this.BitmapBottom, this.y2)
w := this.WindowWidth := this.WindowRight - this.WindowLeft
h := this.WindowHeight := this.WindowBottom - this.WindowTop
; Changing x, y, w, h to be stationary does not provide a speed boost.
; Nor does making the window opaque.
pptDst := x - this.OffsetLeft << 32 >>> 32 | y - this.OffsetTop << 32
pptSrc := x - this.BitmapLeft << 32 >>> 32 | y - this.BitmapTop << 32
DllCall("UpdateLayeredWindow"
, "ptr", this.hwnd ; hWnd
, "ptr", 0 ; hdcDst
,"uint64*", pptDst ; *pptDst
,"uint64*", w | h << 32 ; *psize
, "ptr", this.hdc ; hdcSrc
,"uint64*", pptSrc ; *pptSrc
, "uint", 0 ; crKey
, "uint*", alpha << 16 | 0x01 << 24 ; *pblend
, "uint", 2 ; dwFlags
, "int") ; Success = 1
; Fixes a long standing bug where Windows forgets which windows are on top.
; Seems to happen mostly when connecting a laptop to an external monitor.
WinSetAlwaysOnTop True, this.hwnd
}
return this
}
; Timers and Queues
Sleep(sleep_time := 0, wait_time := 0) {
this.Wait(wait_time)
if (sleep_time > 0) {
this.UpdateLayeredWindow(0)
this.memorystate := 3
Sleep sleep_time
}
return this
}
Wait(wait_time := 0) {
; Allow the user to override the original duration with a positive number.
(wait_time <= 0) && wait_time := this.t
if (wait_time > 0) {
; Prevents the timer from blanking the canvas.
this.UpdateStatus()
; Always use QPC over GetTickCount for finer time intervals.
DllCall("QueryPerformanceFrequency", "int64*", &frequency:=0)
loop {
DllCall("QueryPerformanceCounter", "int64*", &end:=0)
elapsed_time := (end - this.t0) / frequency * 1000
remaining_time := wait_time - elapsed_time
if (remaining_time > 30)
Sleep 10
if (remaining_time <= 0)
break
}
}
return this ; Allow the next render call to update the current window.
}
CanvasChanged() {
this.UpdateStatus()
try if callback := this.events["CanvasChange"]
return (callback.MaxParams = 0) ? callback() : callback(this) ; Callbacks have a reference to "this".
}
UpdateStatus() {
this.status += 1
if 0xFFFF & this.status == 0xFFFF {
h := Random(0x1000, 0xFFFF)
l := 0
this.status := h << 32 | l
}
}
Blank(status) {
; Check to see if the state of the canvas has changed before clearing and updating.
if (this.status = status) {
this.UpdateLayeredWindow(0)
}
}
; Drawing
get(name, p*) {
switch(Type(this)) {
case "Array", "Map":
try ___ := Integer(name)
catch
___ := name
finally name := ___
return this.Has(name) ? this[name] : ""
default:
return ObjHasOwnProp(this, name) ? this.name : ""
}
}
DrawOnGraphics(Graphics, text := "", style1 := "", style2 := "", CanvasWidth := "", CanvasHeight := "", CanvasLeft := "", CanvasTop := "") {
; RegEx help? https://regex101.com/r/rNsP6n/1
static q1 := "(?i)^.*?\b(?<!:|:\s)\b"
static q2 := "(?!(?>\([^()]*\)|[^()]*)*\))(:\s*)?\(?(?<value>(?<=\()([\\\/\s:#%_a-z\-\.\d]+|\([\\\/\s:#%_a-z\-\.\d]*\))*(?=\))|[#%_a-z\-\.\d]+).*$"
; Extract styles to variables.
if IsObject(style1) {
style1.base.__get := this.get ; Returns the empty string for unknown properties.
_t := (style1.time != "") ? style1.time : style1.t
_s := (style1.screen != "") ? style1.screen : style1.s
_a := (style1.anchor != "") ? style1.anchor : style1.a
_x := (style1.left != "") ? style1.left : style1.x
_y := (style1.top != "") ? style1.top : style1.y
_w := (style1.width != "") ? style1.width : style1.w
_h := (style1.height != "") ? style1.height : style1.h
_r := (style1.radius != "") ? style1.radius : style1.r
_c := (style1.color != "") ? style1.color : style1.c
_m := (style1.margin != "") ? style1.margin : style1.m
_q := (style1.quality != "") ? style1.quality : (style1.q) ? style1.q : style1.SmoothingMode
} else {
RegExReplace(style1, "\s+", A_Space) ; Limit whitespace for fixed width look-behinds.
_t := ((___ := RegExReplace(style1, q1 "(t(ime)?)" q2, "${value}")) != style1) ? ___ : ""
_s := ((___ := RegExReplace(style1, q1 "(s(creen)?)" q2, "${value}")) != style1) ? ___ : ""
_a := ((___ := RegExReplace(style1, q1 "(a(nchor)?)" q2, "${value}")) != style1) ? ___ : ""
_x := ((___ := RegExReplace(style1, q1 "(x|left)" q2, "${value}")) != style1) ? ___ : ""
_y := ((___ := RegExReplace(style1, q1 "(y|top)" q2, "${value}")) != style1) ? ___ : ""
_w := ((___ := RegExReplace(style1, q1 "(w(idth)?)" q2, "${value}")) != style1) ? ___ : ""
_h := ((___ := RegExReplace(style1, q1 "(h(eight)?)" q2, "${value}")) != style1) ? ___ : ""
_r := ((___ := RegExReplace(style1, q1 "(r(adius)?)" q2, "${value}")) != style1) ? ___ : ""
_c := ((___ := RegExReplace(style1, q1 "(c(olor)?)" q2, "${value}")) != style1) ? ___ : ""
_m := ((___ := RegExReplace(style1, q1 "(m(argin)?)" q2, "${value}")) != style1) ? ___ : ""
_q := ((___ := RegExReplace(style1, q1 "(q(uality)?)" q2, "${value}")) != style1) ? ___ : ""
}
if IsObject(style2) {
style2.base.__get := this.get ; Returns the empty string for unknown properties.
t := (style2.time != "") ? style2.time : style2.t
a := (style2.anchor != "") ? style2.anchor : style2.a
x := (style2.left != "") ? style2.left : style2.x
y := (style2.top != "") ? style2.top : style2.y
w := (style2.width != "") ? style2.width : style2.w
h := (style2.height != "") ? style2.height : style2.h
m := (style2.margin != "") ? style2.margin : style2.m
f := (style2.font != "") ? style2.font : style2.f
s := (style2.size != "") ? style2.size : style2.s
c := (style2.color != "") ? style2.color : style2.c
b := (style2.bold != "") ? style2.bold : style2.b
i := (style2.italic != "") ? style2.italic : style2.i
u := (style2.underline != "") ? style2.underline : style2.u
j := (style2.justify != "") ? style2.justify : style2.j
v := (style2.vertical != "") ? style2.vertical : style2.v
n := (style2.noWrap != "") ? style2.noWrap : style2.n
z := (style2.condensed != "") ? style2.condensed : style2.z
d := (style2.dropShadow != "") ? style2.dropShadow : style2.d
o := (style2.outline != "") ? style2.outline : style2.o
q := (style2.quality != "") ? style2.quality : (style2.q) ? style2.q : style2.TextRenderingHint
} else {
RegExReplace(style2, "\s+", A_Space) ; Limit whitespace for fixed width look-behinds.
t := ((___ := RegExReplace(style2, q1 "(t(ime)?)" q2, "${value}")) != style2) ? ___ : ""
a := ((___ := RegExReplace(style2, q1 "(a(nchor)?)" q2, "${value}")) != style2) ? ___ : ""
x := ((___ := RegExReplace(style2, q1 "(x|left)" q2, "${value}")) != style2) ? ___ : ""
y := ((___ := RegExReplace(style2, q1 "(y|top)" q2, "${value}")) != style2) ? ___ : ""
w := ((___ := RegExReplace(style2, q1 "(w(idth)?)" q2, "${value}")) != style2) ? ___ : ""
h := ((___ := RegExReplace(style2, q1 "(h(eight)?)" q2, "${value}")) != style2) ? ___ : ""
m := ((___ := RegExReplace(style2, q1 "(m(argin)?)" q2, "${value}")) != style2) ? ___ : ""
f := ((___ := RegExReplace(style2, q1 "(f(ont)?)" q2, "${value}")) != style2) ? ___ : ""
s := ((___ := RegExReplace(style2, q1 "(s(ize)?)" q2, "${value}")) != style2) ? ___ : ""
c := ((___ := RegExReplace(style2, q1 "(c(olor)?)" q2, "${value}")) != style2) ? ___ : ""
b := ((___ := RegExReplace(style2, q1 "(b(old)?)" q2, "${value}")) != style2) ? ___ : ""
i := ((___ := RegExReplace(style2, q1 "(i(talic)?)" q2, "${value}")) != style2) ? ___ : ""
u := ((___ := RegExReplace(style2, q1 "(u(nderline)?)" q2, "${value}")) != style2) ? ___ : ""
j := ((___ := RegExReplace(style2, q1 "(j(ustify)?)" q2, "${value}")) != style2) ? ___ : ""
v := ((___ := RegExReplace(style2, q1 "(v(ertical)?)" q2, "${value}")) != style2) ? ___ : ""
n := ((___ := RegExReplace(style2, q1 "(n(oWrap)?)" q2, "${value}")) != style2) ? ___ : ""
z := ((___ := RegExReplace(style2, q1 "(z|condensed)" q2, "${value}")) != style2) ? ___ : ""
d := ((___ := RegExReplace(style2, q1 "(d(ropShadow)?)" q2, "${value}")) != style2) ? ___ : ""
o := ((___ := RegExReplace(style2, q1 "(o(utline)?)" q2, "${value}")) != style2) ? ___ : ""
q := ((___ := RegExReplace(style2, q1 "(q(uality)?)" q2, "${value}")) != style2) ? ___ : ""
}
; Set canvas boundaries. Although inifinite, this rectangle gives it an internal sense of scale.
try dpi := DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr")
; Use the coordinates of the screen index.
if (_s ~= "^\d+$" && _s > 0 && _s <= MonitorGetCount()) {
MonitorGet(_s, &CanvasLeft, &CanvasTop, &CanvasRight, &CanvasBottom)
CanvasWidth := CanvasRight - CanvasLeft
CanvasHeight := CanvasBottom - CanvasTop
}
; Use the coordinates of all screens.
if (_s ~= "^\d+$" && _s == 0) {
CanvasLeft := DllCall("GetSystemMetrics", "int", 76, "int")
CanvasTop := DllCall("GetSystemMetrics", "int", 77, "int")
CanvasWidth := DllCall("GetSystemMetrics", "int", 78, "int")
CanvasHeight := DllCall("GetSystemMetrics", "int", 79, "int")
}
try DllCall("SetThreadDpiAwarenessContext", "ptr", dpi, "ptr")
; Check if an hMonitor is passed.
if (_s ~= "^\d+$" && _s > MonitorGetCount())
hMon := _s
; Use the screen where the cursor is located.
if (_s = "cursor") {
DllCall("GetCursorPos", "uint64*", &point:=0)
hMon := DllCall("MonitorFromPoint", "uint64", point, "uint", 0x2, "ptr")
}
; Or use the screen where the current active window is located.
if (_s = "window")
hMon := DllCall("MonitorFromWindow", "ptr", WinExist("A"), "uint", 0, "ptr")
; Convert the hMonitor to canvas coordinates.
if IsSet(hMon) {
MIEX := Buffer(40 + 64)
NumPut("uint", MIEX.size, MIEX)
if !DllCall("GetMonitorInfo", "ptr", hMon, "ptr", MIEX)
throw Error("The following value " _s " is not a correct screen parameter. ('s')")
CanvasLeft := NumGet(MIEX, 4, "int")
CanvasTop := NumGet(MIEX, 8, "int")
CanvasRight := NumGet(MIEX, 12, "int")
CanvasBottom := NumGet(MIEX, 16, "int")
CanvasWidth := CanvasRight - CanvasLeft
CanvasHeight := CanvasBottom - CanvasTop
}
; Set default width and height from undocumented graphics pointer offset.
(CanvasLeft == "") && CanvasLeft := NumGet(Graphics + 12 + A_PtrSize, "int")
(CanvasTop == "") && CanvasTop := NumGet(Graphics + 16 + A_PtrSize, "int")
(CanvasWidth == "") && CanvasWidth := NumGet(Graphics + 20 + A_PtrSize, "int")
(CanvasHeight == "") && CanvasHeight := NumGet(Graphics + 24 + A_PtrSize, "int")
; Parse background color.
_c := this.color(_c, 0xDD212121) ; Default color for background is transparent gray.
; Parse text color.
AlphaCopy := False
if (c ~= "i)(delete|eraser?|overwrite|AlphaCopy)")
AlphaCopy := True, c := 0 ; Eraser brush for text.
if (c ~= "^-") ; Allow negative color values to overwrite alpha.
AlphaCopy := True, c := LTrim(c, "-")
; Default color is white text on a dark background or black text on a light background.
c := this.color(c, this.grayscale(_c) < 128 ? 0xFFFFFFFF : 0xFF000000)
; Default SmoothingMode is 5 for outlines and rounded corners. To disable use 0. See Draw 1, 2, 3.
_q := (_q ~= "^\d+$" && _q >= 0 && _q <= 5) ? _q : 5 ; SmoothingModeAntiAlias8x8
; Default TextRenderingHint is Cleartype on a opaque background and Anti-Alias on a transparent background.
if (q ~= "^\d+$") and (q < 0 || q > 5)
q := (_c & 0xFF000000 = 0xFF000000) && (!AlphaCopy) ? 5 : 4 ; TextRenderingHintClearTypeGridFit = 5, TextRenderingHintAntialias = 4
else
q := 4
; Save original Graphics settings.
DllCall("gdiplus\GdipSaveGraphics", "ptr", Graphics, "ptr*", &pState:=0)
; Use pixels as the defualt unit when rendering.
DllCall("gdiplus\GdipSetPageUnit", "ptr", Graphics, "int", 2) ; A unit is 1 pixel.
; Set Graphics settings.
DllCall("gdiplus\GdipSetPixelOffsetMode", "ptr", Graphics, "int", 4) ; PixelOffsetModeHalf
;DllCall("gdiplus\GdipSetCompositingMode", "ptr", Graphics, "int", 1) ; CompositingModeSourceCopy
DllCall("gdiplus\GdipSetCompositingQuality", "ptr", Graphics, "int", 4) ; CompositingQualityGammaCorrected
DllCall("gdiplus\GdipSetSmoothingMode", "ptr", Graphics, "int", _q)
DllCall("gdiplus\GdipSetInterpolationMode", "ptr", Graphics, "int", 7) ; HighQualityBicubic
DllCall("gdiplus\GdipSetTextRenderingHint", "ptr", Graphics, "int", q)
; These are the type checkers.
static valid := "^\s*(-?((\d+(\.\d*)?)|(\.\d+)))\s*(?i:%|pt|px|vh|vmin|vw)?\s*$"
static valid_positive := "^\s*((\d+(\.\d*)?)|(\.\d+))\s*(?i:%|pt|px|vh|vmin|vw)?\s*$"
; Define viewport width and height. This is the visible canvas area.
vw := 0.01 * CanvasWidth ; 1% of viewport width.
vh := 0.01 * CanvasHeight ; 1% of viewport height.
vmin := min(vw, vh) ; 1vw or 1vh, whichever is smaller.
vr := CanvasWidth / CanvasHeight ; Aspect ratio of the viewport.
; Get background width and height.
_w := (_w ~= valid_positive) ? RegExReplace(_w, "\s") : ""
_w := (_w ~= "i)(pt|px)$") ? SubStr(_w, 1, -2) : _w
_w := (_w ~= "i)(%|vw)$") ? RegExReplace(_w, "i)(%|vw)$") * vw : _w
_w := (_w ~= "i)vh$") ? RegExReplace(_w, "i)vh$") * vh : _w
_w := (_w ~= "i)vmin$") ? RegExReplace(_w, "i)vmin$") * vmin : _w
_h := (_h ~= valid_positive) ? RegExReplace(_h, "\s") : ""
_h := (_h ~= "i)(pt|px)$") ? SubStr(_h, 1, -2) : _h
_h := (_h ~= "i)vw$") ? RegExReplace(_h, "i)vw$") * vw : _h
_h := (_h ~= "i)(%|vh)$") ? RegExReplace(_h, "i)(%|vh)$") * vh : _h
_h := (_h ~= "i)vmin$") ? RegExReplace(_h, "i)vmin$") * vmin : _h
; Get Font size.
s := (s ~= valid_positive) ? RegExReplace(s, "\s") : "2.23vh" ; Default font size is 2.23vh.
s := (s ~= "i)(pt|px)$") ? SubStr(s, 1, -2) : s ; Strip spaces, px, and pt.
s := (s ~= "i)vh$") ? RegExReplace(s, "i)vh$") * vh : s ; Relative to viewport height.
s := (s ~= "i)vw$") ? RegExReplace(s, "i)vw$") * vw : s ; Relative to viewport width.
s := (s ~= "i)(%|vmin)$") ? RegExReplace(s, "i)(%|vmin)$") * vmin : s ; Relative to viewport minimum.
; Get Bold, Italic, Underline, NoWrap, and Justification of text.
style := (b) ? 1 : 0 ; bold
style += (i) ? 2 : 0 ; italic
style += (u) ? 4 : 0 ; underline
; style += (strikeout) ? 8 : 0 ; strikeout, not implemented.
n := (n) ? 0x4000 | 0x1000 : 0x4000 ; Defaults to text wrapping.
; Define text justification. Default text justification to center.
j := (j ~= "i)(near|left)") ? 0
: (j ~= "i)cent(er|re)") ? 1
: (j ~= "i)(far|right)") ? 2
: (j ~= "^[1-3]$") ? j-1
: 1
; Define vertical alignment. Default vertical alignment to top.
v := (v ~= "i)(near|top)") ? 0
: (v ~= "i)cent(er|re)") ? 1
: (v ~= "i)(far|bottom)") ? 2
: (v ~= "^[1-3]$") ? v-1
: 0
; Later when text x and w are finalized and it is found that x + width exceeds the screen,
; then the _redrawBecauseOfCondensedFont flag is set to true.
static _redrawBecauseOfCondensedFont := False
if (_redrawBecauseOfCondensedFont == True)
f:=z, z:=0, _redrawBecauseOfCondensedFont := False
; Specifies whether to load an external font file, or to use an font already installed on the system.
if (f ~= "(ttf|otf)$") {
; Temporarily load a font from file. This does not install the font.
DllCall("gdiplus\GdipNewPrivateFontCollection", "ptr*", &hCollection:=0)
DllCall("gdiplus\GdipPrivateAddFontFile", "ptr", hCollection, "wstr", f)
; A collection of fonts can hold more than just 1 font. Since only 1 font will be needed, a single pointer suffices.
DllCall("gdiplus\GdipGetFontCollectionFamilyList", "ptr", hCollection, "int", 1, "ptr*", &pFontFamily:=0, "int*", &found:=0)
; Normally, pFontFamily is an array of pointers. For a single pointer, no special requirements are needed.
VarSetStrCapacity(&FontName, 256)
DllCall("gdiplus\GdipGetFamilyName", "ptr", pFontFamily, "str", FontName, "ushort", 1033) ; en-US
; Create a font family. For ANSI compatibility, use str as the output type and StrGet to pass wide chars.
DllCall("gdiplus\GdipCreateFontFamilyFromName", "wstr", StrGet(&FontName, "UTF-16"), "ptr", hCollection, "ptr*", &hFamily:=0)
; Delete the private font collection. It is strange a pointer reference is used.
DllCall("gdiplus\GdipDeletePrivateFontCollection", "ptr*", hCollection)
} else {
; Create Font. Defaults to Segoe UI or Tahoma on older systems.
if DllCall("gdiplus\GdipCreateFontFamilyFromName", "wstr", f, "uint", 0, "ptr*", &hFamily:=0)
if DllCall("gdiplus\GdipCreateFontFamilyFromName", "wstr", "Segoe UI", "uint", 0, "ptr*", &hFamily:=0)
DllCall("gdiplus\GdipCreateFontFamilyFromName", "wstr", "Tahoma", "uint", 0, "ptr*", &hFamily:=0)
}
DllCall("gdiplus\GdipCreateFont", "ptr", hFamily, "float", s, "int", style, "int", 0, "ptr*", &hFont:=0)
DllCall("gdiplus\GdipCreateStringFormat", "int", n, "int", 0, "ptr*", &hFormat:=0)
DllCall("gdiplus\GdipSetStringFormatAlign", "ptr", hFormat, "int", j) ; Left = 0, Center = 1, Right = 2
DllCall("gdiplus\GdipSetStringFormatLineAlign", "ptr", hFormat, "int", v) ; Top = 0, Center = 1, Bottom = 2
; Use the declared width and height of the text box if given.
RectF := Buffer(16, 0) ; sizeof(RectF) = 16
(_w != "") && NumPut("float", _w, RectF, 8) ; Width
(_h != "") && NumPut("float", _h, RectF, 12) ; Height
; Otherwise simulate the drawing...
DllCall("gdiplus\GdipMeasureString"
, "ptr", Graphics
, "wstr", text
, "int", -1 ; string length is null terminated.
, "ptr", hFont
, "ptr", RectF ; (in) layout RectF that bounds the string.
, "ptr", hFormat
, "ptr", RectF ; (out) simulated RectF that bounds the string.
, "uint*", &chars:=0
, "uint*", &lines:=0)
; Extract the simulated width and height of the text string's bounding box...
width := NumGet(RectF, 8, "float")
height := NumGet(RectF, 12, "float")
minimum := min(width, height)
aspect := (height != 0) ? width / height : 0
; And use those values for the background width and height.
(_w == "") && _w := width
(_h == "") && _h := height
; Get margin. Default margin is 1vmin.
m := this.margin_and_padding( m, vw, vh)
_m := this.margin_and_padding(_m, vw, vh, (text != "" && m.void && _w > 0 && _h > 0) ? "1vmin" : "")
; Modify _w, _h with margin and padding, increasing the size of the background.
_w += m.2 + m.4
_h += m.1 + m.3
; Get background anchor. This is where the origin of the background is located.
_a := (_a ~= "^[1-9]$") ? _a-1
: (_a ~= "i)top" && _a ~= "i)left") ? 0
: (_a ~= "i)top" && _a ~= "i)cent(er|re)") ? 1
: (_a ~= "i)top" && _a ~= "i)right") ? 2
: (_a ~= "i)cent(er|re)" && _a ~= "i)left") ? 3
: (_a ~= "i)cent(er|re)" && _a ~= "i)right") ? 5
: (_a ~= "i)bottom" && _a ~= "i)left") ? 6
: (_a ~= "i)bottom" && _a ~= "i)cent(er|re)") ? 7
: (_a ~= "i)bottom" && _a ~= "i)right") ? 8
: (_a ~= "i)top") ? 1
: (_a ~= "i)left") ? 3
: (_a ~= "i)right") ? 5
: (_a ~= "i)bottom") ? 7
: (_a ~= "i)cent(er|re)") ? 4
; The anchor can be implied from _x and _y (left, center, right, top, bottom).
: ((_x ~= "i)left") ? 0 : (_x ~= "i)cent(er|re)") ? 1 : (_x ~= "i)right") ? 2 : 0)
+ ((_y ~= "i)top") ? 0 : (_y ~= "i)cent(er|re)") ? 3 : (_y ~= "i)bottom") ? 6 : 0)
; Default anchor is top-left (0).
; Convert English words to numbers. Don't mess with these values any further.
_x := (_x ~= "i)left") ? 0 : (_x ~= "i)cent(er|re)") ? 0.5*CanvasWidth : (_x ~= "i)right") ? CanvasWidth : _x
_y := (_y ~= "i)top") ? 0 : (_y ~= "i)cent(er|re)") ? 0.5*CanvasHeight : (_y ~= "i)bottom") ? CanvasHeight : _y
; Get _x and _y.
_x := (_x ~= valid) ? RegExReplace(_x, "\s") : ""
_x := (_x ~= "i)(pt|px)$") ? SubStr(_x, 1, -2) : _x
_x := (_x ~= "i)(%|vw)$") ? RegExReplace(_x, "i)(%|vw)$") * vw : _x
_x := (_x ~= "i)vh$") ? RegExReplace(_x, "i)vh$") * vh : _x
_x := (_x ~= "i)vmin$") ? RegExReplace(_x, "i)vmin$") * vmin : _x
_y := (_y ~= valid) ? RegExReplace(_y, "\s") : ""
_y := (_y ~= "i)(pt|px)$") ? SubStr(_y, 1, -2) : _y
_y := (_y ~= "i)vw$") ? RegExReplace(_y, "i)vw$") * vw : _y
_y := (_y ~= "i)(%|vh)$") ? RegExReplace(_y, "i)(%|vh)$") * vh : _y
_y := (_y ~= "i)vmin$") ? RegExReplace(_y, "i)vmin$") * vmin : _y
; Default x and y to center of the canvas. Default anchor to horizontal center and vertical center.
if (_x == "")
_x := 0.5*CanvasWidth, _a := 1+(_a//3*3)
if (_y == "")
_y := 0.5*CanvasHeight, _a := 3+mod(_a,3)
; Now let's modify the _x and _y values with the _anchor, so that the image has a new point of origin.
; We need our calculated _width and _height for this!
_x -= (mod(_a,3) == 0) ? 0 : (mod(_a,3) == 1) ? _w/2 : (mod(_a,3) == 2) ? _w : 0
_y -= ((_a//3) == 0) ? 0 : ((_a//3) == 1) ? _h/2 : ((_a//3) == 2) ? _h : 0
; Offset with canvas boundaries.
_x += CanvasLeft
_y += CanvasTop
; Prevent half-pixel rendering and keep image sharp.
_w := Round(_x + _w) - Round(_x) ; Use real x2 coordinate to determine width.
_h := Round(_y + _h) - Round(_y) ; Use real y2 coordinate to determine height.
_x := Round(_x) ; NOTE: simple Floor(w) or Round(w) will NOT work.
_y := Round(_y) ; The float values need to be added up and then rounded!
; Get the text width and text height.
w := ( w ~= valid_positive) ? RegExReplace( w, "\s") : width ; Default is simulated text width.
w := ( w ~= "i)(pt|px)$") ? SubStr( w, 1, -2) : w
w := ( w ~= "i)vw$") ? RegExReplace( w, "i)vw$") * vw : w
w := ( w ~= "i)vh$") ? RegExReplace( w, "i)vh$") * vh : w
w := ( w ~= "i)vmin$") ? RegExReplace( w, "i)vmin$") * vmin : w
w := ( w ~= "%$") ? RegExReplace( w, "%$") * 0.01 * _w : w
h := ( h ~= valid_positive) ? RegExReplace( h, "\s") : height ; Default is simulated text height.
h := ( h ~= "i)(pt|px)$") ? SubStr( h, 1, -2) : h
h := ( h ~= "i)vw$") ? RegExReplace( h, "i)vw$") * vw : h
h := ( h ~= "i)vh$") ? RegExReplace( h, "i)vh$") * vh : h
h := ( h ~= "i)vmin$") ? RegExReplace( h, "i)vmin$") * vmin : h
h := ( h ~= "%$") ? RegExReplace( h, "%$") * 0.01 * _h : h
; Manually justify because text width and height may be set above.
; If text justification is set but x is not, align the justified text relative to the center
; or right of the backgound, after taking into account the text width.
if (x == "")
x := (j = 1) ? _x + (_w/2) - (w/2) : (j = 2) ? _x + _w - w : x
if (y == "")
y := (v = 1) ? _y + (_h/2) - (h/2) : (v = 2) ? _y + _h - h : y
; Get text anchor. This is where the origin of the text is located.
a := (a ~= "i)top" && a ~= "i)left") ? 0
: (a ~= "i)top" && a ~= "i)cent(er|re)") ? 1
: (a ~= "i)top" && a ~= "i)right") ? 2
: (a ~= "i)cent(er|re)" && a ~= "i)left") ? 3
: (a ~= "i)cent(er|re)" && a ~= "i)right") ? 5
: (a ~= "i)bottom" && a ~= "i)left") ? 6
: (a ~= "i)bottom" && a ~= "i)cent(er|re)") ? 7
: (a ~= "i)bottom" && a ~= "i)right") ? 8
: (a ~= "i)top") ? 1
: (a ~= "i)left") ? 3
: (a ~= "i)right") ? 5
: (a ~= "i)bottom") ? 7
: (a ~= "i)cent(er|re)") ? 4
: (a ~= "^[1-9]$") ? a-1
: 0 ; Default anchor is top-left.
; Text x and text y can be specified as locations (left, center, right, top, bottom).
; These location words in text x and text y take precedence over the values in the text anchor.
a := ( x ~= "i)left") ? 0+( a//3*3) : ( x ~= "i)cent(er|re)") ? 1+( a//3*3) : ( x ~= "i)right") ? 2+( a//3*3) : a
a := ( y ~= "i)top") ? 0+mod( a,3) : ( y ~= "i)cent(er|re)") ? 3+mod( a,3) : ( y ~= "i)bottom") ? 6+mod( a,3) : a
; Convert English words to numbers. Don't mess with these values any further.
; Also, these values are relative to the background.
x := ( x ~= "i)left") ? _x : (x ~= "i)cent(er|re)") ? _x + 0.5*_w : (x ~= "i)right") ? _x + _w : x
y := ( y ~= "i)top") ? _y : (y ~= "i)cent(er|re)") ? _y + 0.5*_h : (y ~= "i)bottom") ? _y + _h : y
; Default text x is background x.
x := ( x ~= valid) ? RegExReplace( x, "\s") : _x
x := ( x ~= "i)(pt|px)$") ? SubStr( x, 1, -2) : x
x := ( x ~= "i)vw$") ? RegExReplace( x, "i)vw$") * vw : x
x := ( x ~= "i)vh$") ? RegExReplace( x, "i)vh$") * vh : x
x := ( x ~= "i)vmin$") ? RegExReplace( x, "i)vmin$") * vmin : x
x := ( x ~= "%$") ? RegExReplace( x, "%$") * 0.01 * _w : x
; Default text y is background y.
y := ( y ~= valid) ? RegExReplace( y, "\s") : _y
y := ( y ~= "i)(pt|px)$") ? SubStr( y, 1, -2) : y
y := ( y ~= "i)vw$") ? RegExReplace( y, "i)vw$") * vw : y
y := ( y ~= "i)vh$") ? RegExReplace( y, "i)vh$") * vh : y
y := ( y ~= "i)vmin$") ? RegExReplace( y, "i)vmin$") * vmin : y
y := ( y ~= "%$") ? RegExReplace( y, "%$") * 0.01 * _h : y
; If margin/padding are defined in the text parameter, shift the position of the text.
x += (j == 0) ? m.4 : (j == 1) ? (m.4/2)-(m.2/2) : -m.2
y += (v == 0) ? m.1 : (v == 1) ? (m.1/2)-(m.3/2) : -m.3
; Modify text x and text y values with the anchor, so that the text has a new point of origin.
; The text anchor is relative to the text width and height before margin/padding.
; This is NOT relative to the background width and height.
x -= (mod(a,3) == 0) ? 0 : (mod(a,3) == 1) ? w/2 : (mod(a,3) == 2) ? w : 0
y -= ((a//3) == 0) ? 0 : ((a//3) == 1) ? h/2 : ((a//3) == 2) ? h : 0
; Modify _x, _y, _w, _h with margin and padding, increasing the size of the background.
_w += _m.4 + _m.2
_h += _m.1 + _m.3
_x -= _m.4
_y -= _m.1
; Re-run: Condense Text using a Condensed Font if simulated text width exceeds screen width.
if (z) {