-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathFnt.ahk
4380 lines (3880 loc) · 131 KB
/
Fnt.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
/*
v2.0.x release notes
Possible script breaker
-----------------------
* Fnt_HardWordBreak moved out of the main library into a separate add-on
function. Function has been enhanced to support additional
parameter values. Also, the return value has changed. See the function
documentation for more information.
*/
/*
Title: Fnt Library v2.0.1
Group: Introduction
Fonts are logical objects that instruct the computer how to draw text on a
device (display, printers, plotters, etc.). This library provides a means
of managing some of the aspects of fonts used in AutoHotkey.
Group: AutoHotkey Compatibility
This library was designed to run on all versions of AutoHotkey v1.1+: ANSI,
Unicode, and Unicode 64-bit.
Group: Issues and Considerations
The *<DPIScale at https://autohotkey.com/docs/commands/Gui.htm#DPIScale>*
feature introduced in AutoHotkey v1.1.11 can produce unexpected results when
the Fnt library is used to determine the size and/or position of anything
GUI. The DPIScale feature is enabled by default so if necessary, it must be
manually disabled for each GUI by adding a "gui -DPIScale" command.
Important: Conflicts with the DPIScale feature do not occur when using the
default DPI setting, i.e. 96 DPI. Errors only occur if using the large
(120 DPI), larger (144 DPI), or a custom DPI setting.
Group: Links
Font and Text Reference
- <https://msdn.microsoft.com/en-us/library/windows/desktop/dd144824%28v=vs.85%29.aspx>
Group: Credit
* Some of the code in this library and in the example scripts was extracted
from the AutoHotkey source. Thanks to authors of *AutoHotkey*.
* The <Fnt_ChooseFont> function was originally adapted from the Dlg library
which was published by *majkinetor*.
* The <Fnt_GetListOfFonts> function was inspired by an example published by
*Sean*.
Group: Functions
*/
;------------------------------
;
; Function: Fnt_AddFontFile
;
; Description:
;
; Add one or more fonts from a font file (Ex: "MySpecialFont.ttf") to the
; system font table.
;
; Type:
;
; Experimental. Subject to change.
;
; Parameters:
;
; p_File - The full path and name of the font file.
;
; p_Private - If set to TRUE, only the process that called this function can
; use the added font(s).
;
; p_Hidden - If set to TRUE, the added font(s) cannot be enumerated, i.e. not
; included when any program requests a list of fonts from the OS.
;
; Returns:
;
; The number of the fonts added if successful, otherwise FALSE.
;
; Remarks:
;
; All fonts added using this function are temporary. If the p_Private
; parameter is set to TRUE, the added font(s) are automatically removed when
; the process that added the font(s) ends. If p_Private is FALSE, the font(s)
; are only available for the current session. When the system restarts, the
; font(s) will not be present. If desired, use <Fnt_RemoveFontFile> to remove
; the font(s) added by this function.
;
; A complete list of the font file types that can be loaded as well as
; additional considerations can be found <here at http://tinyurl.com/j3nrbw2>.
;
;-------------------------------------------------------------------------------
Fnt_AddFontFile(p_File,p_Private,p_Hidden=False)
{
Static Dummy0661
;-- Font Resource flags
,FR_PRIVATE :=0x10
,FR_NOT_ENUM:=0x20
;-- Messages and flags
,WM_FONTCHANGE :=0x1D
,HWND_BROADCAST:=0xFFFF
;-- Build flags
l_Flags:=0
if p_Private
l_Flags|=FR_PRIVATE
if p_Hidden
l_Flags|=FR_NOT_ENUM
;-- Add font
RC:=DllCall("AddFontResourceEx","Str",p_File,"UInt",l_Flags,"UInt",0)
;-- If one or more fonts were added, notify all top-level windows that the
; pool of font resources has changed.
if RC
SendMessage WM_FONTCHANGE,0,0,,ahk_id %HWND_BROADCAST%,,,,1000
;-- Wait up to (but no longer than) 1000 ms for all windows to
; respond to the message.
Return RC
}
;------------------------------
;
; Function: Fnt_ChooseFont
;
; Description:
;
; Creates a Font dialog box that enables the user to choose attributes for a
; logical font.
;
; Parameters:
;
; hOwner - A handle to the window that owns the dialog box. This parameter
; can be any valid window handle or it can be set to 0 or null if the
; dialog box has no owner.
;
; r_Name - Typeface name. [Input/Output] On input, this variable can contain
; contain the default typeface name. On output, this variable will
; contain the selected typeface name.
;
; r_Options - Font options. [Input/Output] See the *Options* section for the
; details.
;
; p_Effects - If set to TRUE (the default), the dialog box will display the
; controls that allow the user to specify strikeout, underline, and
; text color options.
;
; p_Flags - [Advanced Feature] Additional ChooseFont flags. [Optional] The
; default is 0 (no additional flags). See the *Flags* section for more
; information.
;
; Options:
;
; On input, the r_Options parameter contains the default font options. On
; output, r_Options will contain the selected font options. The following
; space-delimited options (in alphabetical order) are available:
;
; bold - On input, this option will preselect the "Bold" font style. On
; output, this option will be returned if a bold font was selected.
;
; c{color} - Text color. {color} is one of 16 color names (see the AutoHotkey
; documentation for a list of supported color names) or a 6-digit hex RGB
; color value. Example values: Blue or FF00FA. On input, this option
; will attempt to pre-select the text color. On output, this option is
; returned with the selected text color. Notes and exceptions: 1) The
; default text color is pre-selected if a color option is not specified or
; if the "Default" color is specified. 2) Color names (Ex: "Blue") are
; only accepted on input. A 6-digit hex RGB color value is set on output
; (Ex: 0000FF). Exception: If the default text color is selected, the
; color name "Default" is set. 3) If p_Effects is FALSE, this option is
; ignored on input and is not returned.
;
; italic - On input, this option will preselect the "italic" font style. On
; output, this option will be returned if an italic font was selected.
; Exception: If p_Effects is FALSE, this option is ignored on input and is
; not returned.
;
; s{SizeInPoints} - Font size (in points). For example: s12. On input,
; this option will load the font size and if on the dialog's "Size" list,
; will preselect the font size. On output, the font size that was
; entered/selected is returned.
;
; SizeMax{MaxPointSize} - [Input only] The maximum point size the user can
; enter/select. For example: SizeMax72. If this option is specified
; without also specifying the SizeMin option, the SizeMin value is
; automatically set to 1.
;
; SizeMin{MinPointSize} - [Input only] The minimum point size the user can
; enter/select. For example: SizeMin10. If this option is specified
; without also specifying the SizeMax option, the SizeMax value is
; automatically set to 0xBFFF (49151).
;
; strike - On input, this option will check the "Strikeout" option. On
; output, this option will be returned if the "Strikeout" option was
; checked. Exception: If p_Effects is FALSE, this option is ignored on
; input and is not returned.
;
; underline - On input, this option will check the "Underline" option. On
; output, this option will be returned if the "Underline" option was
; checked. Exception: If p_Effects is FALSE, this option is ignored on
; input and is not returned.
;
; w{FontWeight} - Font weight (thickness or boldness), which is an integer
; between 1 and 1000. For example, 400 is Normal and 700 is Bold. On
; input, this option will preselect the font style that most closely
; matches the weight specified. If not specified, the default weight for
; the font is selected. On output, this option is only returned if the
; font weight is not Normal (400) and not Bold (700).
;
; To specify more than one option, include a space between each. For
; example: s12 cFF0000 bold. On output, the selected options are defined
; in the same format.
;
; Returns:
;
; TRUE if a font was selected, otherwise FALSE is returned if the dialog was
; canceled or if an error occurred.
;
; Calls To Other Functions:
;
; * <Fnt_ColorName2RGB>
; * <Fnt_GetWindowTextColor>
;
; Flags:
;
; Flexibility in the operation of the Font dialog box is available via a large
; number of ChooseFont flags. For this function, the flags are determined by
; constants, options in the r_Options parameter, and the value of the
; p_Effects parameter. Although the flags set by these conditions will handle
; the needs of the majority of developers, there are a few ChooseFont flags
; that could provide additional value. The p_Flags parameter is used to _add_
; additional ChooseFont flags to control the operation of the Font dialog box.
; See the function's static variables for a list of possible flag values.
;
; This is an advanced feature. Including invalid or conflicting flags may
; produce unexpected results. Be sure to test throroughly. With that said,
; many of the flags can be used to limit or exclude fonts. This is a simple
; but powerful feature to only show the fonts that are needed for a
; particular task.
;
; Remarks:
;
; The ChooseFont dialog box supports the selection of text color. Although
; text color is an attribute of many common controls, please note that it is
; not a font attribute.
;
; Although the font weight can be any number between 1 and 1000, most fonts
; only support 400 (Normal/Regular) and 700 (Bold). A very small number of
; fonts support additional font weights. At this writing, the ChooseFont
; dialog does not display the font weight as a number. Instead, the font
; weight is displayed as font styles like Regular, ExtraLight, Black, etc. See
; the <CreateFont at http://tinyurl.com/n2qe72w> documentation for a list of
; common font weight names and their associated font weight values.
;
; The SizeMin and SizeMax options (r_Options parameter) not only affect the
; list of fonts sizes that are shown in the Font Size selection list box in
; the Font dialog box, they affect the font size that can be manually entered
; in the Font Size combo box. If a font size that is outside the boundaries
; set by the SizeMin and SizeMax options, a MsgBox dialog is shown and the
; user is not allowed to continue until a valid font size is entered/selected.
; Warning: If the value of the SizeMin option is greater than the SizeMax
; option, the "ChooseFont" API function will generate a CFERR_MAXLESSTHANMIN
; error and will return without showing the Font dialog box.
;
;-------------------------------------------------------------------------------
Fnt_ChooseFont(hOwner=0,ByRef r_Name="",ByRef r_Options="",p_Effects=True,p_Flags=0)
{
Static Dummy3155
;-- ChooseFont flags
,CF_SCREENFONTS:=0x1
;-- List only the screen fonts supported by the system. This
; flag is automatically set.
,CF_PRINTERFONTS:=0x2
;-- List only printer fonts. Not supported by this libary. Do
; not use.
,CF_SHOWHELP:=0x4
;-- Causes the dialog box to display the Help button. Not
; supported by this library. Do not use.
,CF_ENABLEHOOK:=0x8
;-- Enables the hook procedure specified in the lpfnHook member
; of this structure. Not supported by this library. Do not
; use.
,CF_ENABLETEMPLATE:=0x10
;-- Indicates that the hInstance and lpTemplateName members
; specify a dialog box template to use in place of the default
; template. Not supported by this library. Do not use.
,CF_ENABLETEMPLATEHANDLE:=0x20
;-- Indicates that the hInstance member identifies a data block
; that contains a preloaded dialog box template. The system
; ignores the lpTemplateName member if this flag is specified.
; Not supported by this library. Do not use.
,CF_INITTOLOGFONTSTRUCT:=0x40
;-- Use the structure pointed to by the lpLogFont member to
; initialize the dialog box controls. This flag is
; automatically set.
,CF_USESTYLE:=0x80
;-- Not supported by this library. Do not use.
,CF_EFFECTS:=0x100
;-- Causes the dialog box to display the controls that allow
; the user to specify strikeout, underline, and text color
; options. This flag is automatically set if the p_Effects
; parameter is set to TRUE.
,CF_APPLY:=0x200
;-- Causes the dialog box to display the Apply button. Not
; supported by this library. Do not use.
,CF_SCRIPTSONLY:=0x400
;-- Prevent the dialog box from displaying or selecting OEM or
; Symbol fonts.
,CF_NOOEMFONTS:=0x800
;-- Prevent the dialog box from displaying or selecting OEM
; fonts. Note: The CF_NOVECTORFONTS constant (not used here)
; is set to the same value as this constant.
,CF_NOSIMULATIONS:=0x1000
;-- Prevent the dialog box from displaying or selecting font
; simulations.
,CF_LIMITSIZE:=0x2000
;-- Select only font sizes within the range specified by the
; nSizeMin and nSizeMax members. This flag is automatically
; added if the SizeMin and/or the SizeMax options (p_Options
; parameter) are used.
,CF_FIXEDPITCHONLY:=0x4000
;-- Show and allow selection of only fixed-pitch fonts.
,CF_WYSIWYG:=0x8000
;-- Obsolete. ChooseFont ignores this flag.
,CF_FORCEFONTEXIST:=0x10000
;-- Display an error message if the user attempts to select a
; font or style that is not listed in the dialog box.
,CF_SCALABLEONLY:=0x20000
;-- Show and allow selection of only scalable fonts.
,CF_TTONLY:=0x40000
;-- Show and allow the selection of only TrueType fonts.
,CF_NOFACESEL:=0x80000
;-- Prevent the dialog box from displaying an initial selection
; for the font name combo box.
,CF_NOSTYLESEL:=0x100000
;-- Prevent the dialog box from displaying an initial selection
; for the Font Style combo box.
,CF_NOSIZESEL:=0x200000
;-- Prevent the dialog box from displaying an initial selection
; for the Font Size combo box.
,CF_SELECTSCRIPT:=0x400000
;-- When specified on input, only fonts with the character set
; identified in the lfCharSet member of the LOGFONT structure
; are displayed. The user will not be allowed to change the
; character set specified in the Scripts combo box. Not
; supported by this library. Do not use.
,CF_NOSCRIPTSEL:=0x800000
;-- Disables the Script combo box.
,CF_NOVERTFONTS:=0x1000000
;-- Display only horizontally oriented fonts.
,CF_INACTIVEFONTS:=0x2000000
;-- ChooseFont should additionally display fonts that are set to
; Hide in Fonts Control Panel. Windows 7+.
;-- Device constants
,LOGPIXELSY:=90
;-- Misc. font constants
,CFERR_MAXLESSTHANMIN:=0x2002
,FW_NORMAL :=400
,FW_BOLD :=700
,LF_FACESIZE :=32 ;-- In TCHARS
;--------------
;-- Initialize
;--------------
;-- Collect the number of pixels per logical inch along the screen height
hDC:=DllCall("CreateDC","Str","DISPLAY","Ptr",0,"Ptr",0,"Ptr",0)
l_LogPixelsY:=DllCall("GetDeviceCaps","Ptr",hDC,"Int",LOGPIXELSY)
DllCall("DeleteDC","Ptr",hDC)
;-- Default window text color
l_WindowTextColor:=Fnt_GetWindowTextColor()
;--------------
;-- Parameters
;--------------
r_Name:=Trim(r_Name," `f`n`r`t`v")
;-- Remove all leading/trailing white space
;-- p_Flags
if p_Flags is not Integer
p_Flags:=0x0
p_Flags|=CF_SCREENFONTS|CF_INITTOLOGFONTSTRUCT
if p_Effects
p_Flags|=CF_EFFECTS
;-----------
;-- Options
;-----------
;-- Initialize
o_Color :=l_WindowTextColor
o_Height :=13
o_Italic :=False
o_Size :="" ;-- Undefined
o_SizeMin :="" ;-- Undefined
o_SizeMax :="" ;-- Undefined
o_Strikeout:=False
o_Underline:=False
o_Weight :="" ;-- Undefined
;-- Extract options (if any) from r_Options
Loop Parse,r_Options,%A_Space%
{
if (InStr(A_LoopField,"bold")=1)
o_Weight:=FW_BOLD
else if (InStr(A_LoopField,"italic")=1)
o_Italic:=True
else if (InStr(A_LoopField,"sizemin")=1)
{
o_SizeMin:=SubStr(A_LoopField,8)
if o_SizeMin is not Integer
o_SizeMin:=1
}
else if (InStr(A_LoopField,"sizemax")=1)
{
o_SizeMax:=SubStr(A_LoopField,8)
if o_SizeMax is not Integer
o_SizeMax:=0xBFFF
}
else if (InStr(A_LoopField,"strike")=1)
o_Strikeout:=True
else if (InStr(A_LoopField,"underline")=1)
o_Underline:=True
else if (InStr(A_LoopField,"c")=1 and StrLen(A_Loopfield)>1)
{
;-- Initial value
l_Color:=o_Color:=SubStr(A_LoopField,2)
;-- If not set already, prepend hex prefix
if not InStr(SubStr(o_Color,1,2),"0x")
o_Color:="0x" . o_Color
;-- If not a valid hex value, convert color name to hex value
; Note: All color names have 2 or more non-hex digit values
if o_Color is not xDigit
o_Color:=Fnt_ColorName2RGB(l_Color)
}
else if (InStr(A_LoopField,"s")=1)
o_Size:=SubStr(A_LoopField,2)
else if (InStr(A_LoopField,"w")=1)
o_Weight:=SubStr(A_LoopField,2)
}
;-- If needed, reset Effects options to defaults
if not p_Flags & CF_EFFECTS
{
o_Color :=l_WindowTextColor
o_Strikeout:=False
o_Underline:=False
}
;--------------------------
;-- Convert or fix invalid
;-- or unspecified options
;--------------------------
if o_Color is Space ;-- No color options
o_Color:=l_WindowTextColor
;-- Convert color to BRG
o_Color:=((o_Color&0xFF)<<16)+(o_Color&0xFF00)+((o_Color>>16)&0xFF)
if o_SizeMin is Integer
if o_SizeMax is Space
o_SizeMax:=0xBFFF
if o_SizeMax is Integer
if o_SizeMin is Space
o_SizeMin:=1
if o_Weight is not Integer
o_Weight:=FW_NORMAL
;-- If needed, convert point size to height, in logical units
if o_Size is Integer
o_Height:=Round(o_Size*l_LogPixelsY/72)*-1
;-- Update flags
if o_SizeMin or o_SizeMax
p_Flags|=CF_LIMITSIZE
;-----------------------
;-- Populate structures
;-----------------------
;-- Create, initialize, and populate LOGFONT structure
VarSetCapacity(LOGFONT,A_IsUnicode ? 92:60,0)
NumPut(o_Height, LOGFONT,0,"Int") ;-- lfHeight
NumPut(o_Weight, LOGFONT,16,"Int") ;-- lfWeight
NumPut(o_Italic, LOGFONT,20,"UChar") ;-- lfItalic
NumPut(o_Underline,LOGFONT,21,"UChar") ;-- lfUnderline
NumPut(o_Strikeout,LOGFONT,22,"UChar") ;-- lfStrikeOut
if StrLen(r_Name)
StrPut(SubStr(r_Name,1,31),&LOGFONT+28,LF_FACESIZE)
;-- lfFaceName
;-- Create, initialize, and populate CHOOSEFONT structure
CFSize:=VarSetCapacity(CHOOSEFONT,(A_PtrSize=8) ? 104:60,0)
NumPut(CFSize,CHOOSEFONT,0,"UInt")
;-- lStructSize
NumPut(hOwner,CHOOSEFONT,(A_PtrSize=8) ? 8:4,"Ptr")
;-- hwndOwner
NumPut(&LOGFONT,CHOOSEFONT,(A_PtrSize=8) ? 24:12,"Ptr")
;-- lpLogFont
NumPut(p_Flags,CHOOSEFONT,(A_PtrSize=8) ? 36:20,"UInt")
;-- Flags
NumPut(o_Color,CHOOSEFONT,(A_PtrSize=8) ? 40:24,"UInt")
;-- rgbColors
if o_SizeMin
NumPut(o_SizeMin,CHOOSEFONT,(A_PtrSize=8) ? 92:52,"Int")
;-- nSizeMin
if o_SizeMax
NumPut(o_SizeMax,CHOOSEFONT,(A_PtrSize=8) ? 96:56,"Int")
;-- nSizeMax
;---------------
;-- Choose font
;---------------
if not DllCall("comdlg32\ChooseFont" . (A_IsUnicode ? "W":"A"),"Ptr",&CHOOSEFONT)
{
if CDERR:=DllCall("comdlg32\CommDlgExtendedError")
{
if (CDERR=CFERR_MAXLESSTHANMIN)
outputdebug,
(ltrim join`s
Function: %A_ThisFunc% Error -
The size specified in the SizeMax option is less than the
size specified in the SizeMin option.
)
else
outputdebug,
(ltrim join`s
Function: %A_ThisFunc% Error -
Unknown error returned from the "ChooseFont" API. Error
code: %CDERR%.
)
}
Return False
}
;------------------
;-- Rebuild output
;------------------
;-- Typeface name
r_Name:=StrGet(&LOGFONT+28,LF_FACESIZE)
;-- r_Options
r_Options:="s" . Floor(NumGet(CHOOSEFONT,(A_PtrSize=8) ? 32:16,"Int")/10)
;-- iPointSize
if p_Flags & CF_EFFECTS
{
l_Color:=NumGet(CHOOSEFONT,(A_PtrSize=8) ? 40:24,"UInt")
;-- rgbColors
;-- Convert to RGB
l_Color:=((l_Color&0xFF)<<16)+(l_Color&0xFF00)+((l_Color>>16)&0xFF)
;-- Append to r_Options in 6-digit hex format
if (l_Color=l_WindowTextColor) ;-- i.e. the default
r_Options.=A_Space . "cDefault"
else
r_Options.=A_Space . "c" . Format("{:06X}",l_Color)
}
l_Weight:=NumGet(LOGFONT,16,"Int")
if (l_Weight<>FW_NORMAL)
if (l_Weight=FW_BOLD)
r_Options.=A_Space . "bold"
else
r_Options.=A_Space . "w" . l_Weight
if NumGet(LOGFONT,20,"UChar")
r_Options.=A_Space . "italic"
if NumGet(LOGFONT,21,"UChar")
r_Options.=A_Space . "underline"
if NumGet(LOGFONT,22,"UChar")
r_Options.=A_Space . "strike"
Return True
}
;------------------------------
;
; Function: Fnt_ColorName2RGB
;
; Description:
;
; Convert a color name to it's 6-digit hexadecimal RGB value.
;
; Type:
;
; Internal function. Subject to change. Do not use.
;
; Parameters:
;
; p_ColorName - A color name (Ex: "Fuchsia"). See the function's static
; variables for a list of supported names.
;
; Returns:
;
; A 6-digit hexadecimal RGB value. Ex: 0xFF00FF. If an invalid color name is
; specified or if the "Default" color name is specified, the value from
; <Fnt_GetWindowTextColor> is returned.
;
; Calls To Other Functions:
;
; * <Fnt_GetWindowTextColor>
;
;-------------------------------------------------------------------------------
Fnt_ColorName2RGB(p_ColorName)
{
Static Dummy3054
;-- Supported color names
,Color_Aqua :=0x00FFFF
,Color_Black :=0x000000
,Color_Blue :=0x0000FF
,Color_Fuchsia:=0xFF00FF
,Color_Gray :=0x808080
,Color_Green :=0x008000
,Color_Lime :=0x00FF00
,Color_Maroon :=0x800000
,Color_Navy :=0x000080
,Color_Olive :=0x808000
,Color_Purple :=0x800080
,Color_Red :=0xFF0000
,Color_Silver :=0xC0C0C0
,Color_Teal :=0x008080
,Color_White :=0xFFFFFF
,Color_Yellow :=0xFFFF00
;-- Set to the default (covers the "Default" color name)
l_Color:=Fnt_GetWindowTextColor()
;-- Convert if supported color name (not case sensitive)
if Color_%p_ColorName% is not Space
l_Color:=Color_%p_ColorName%
Return l_Color
}
;------------------------------
;
; Function: Fnt_CompactPath
;
; Description:
;
; Shortens a file path to fit within a given pixel width by replacing path
; components with ellipses.
;
; Parameters:
;
; hFont - Handle to a logical font. Set to 0 to use the default GUI font.
;
; p_Path - A file path to shorten. Ex: "C:\MyFiles\A long file name.txt"
;
; p_MaxW - The maximum width for the return path, in pixels.
;
; p_Strict - If set to TRUE, the function will return null if the minimum
; path value is longer (measured in pixels) than p_MaxW. The default is
; FALSE. See the *Remarks* section for more information.
;
; Returns:
;
; The compacted path.
;
; Remarks:
;
; By default, the PathCompactPath function will not compact the path beyond
; a minimum value which is usually a base file name preceded by ellipses. If
; the value of p_MaxW is too small (relative to the specified font), the
; width of the minimum path value (measured in pixels) may be larger than
; p_MaxW. If the p_Strict parameter is set to TRUE, the return value will be
; set to null if the compacted path is wider than p_MaxW. If p_Strict is set
; to FALSE (the default), the function will return whatever value is returned
; from the PathCompactPath function.
;
;-------------------------------------------------------------------------------
Fnt_CompactPath(hFont,p_Path,p_MaxW,p_Strict=False)
{
Static Dummy6513
,DEFAULT_GUI_FONT:=17
,HWND_DESKTOP :=0
,MAX_PATH :=260
;-- If needed, get the handle to the default GUI font
if not hFont
hFont:=DllCall("GetStockObject","Int",DEFAULT_GUI_FONT)
;-- Select the font into the device context for the desktop
hDC :=DllCall("GetDC","Ptr",HWND_DESKTOP)
old_hFont:=DllCall("SelectObject","Ptr",hDC,"Ptr",hFont)
;-- Compact path
VarSetCapacity(l_Path,MAX_PATH*(A_IsUnicode ? 2:1),0)
l_Path:=p_Path
RC:=DllCall("shlwapi\PathCompactPath" . (A_IsUnicode ? "W":"A")
,"Ptr",hDC ;-- hDC,
,"Str",l_Path ;-- lpszPath
,"UInt",p_MaxW) ;-- dx
;-- Release the objects needed by the PathCompactPath function
DllCall("SelectObject","Ptr",hDC,"Ptr",old_hFont)
;-- Necessary to avoid memory leak
DllCall("ReleaseDC","Ptr",HWND_DESKTOP,"Ptr",hDC)
;-- Strict?
if p_Strict
if (Fnt_GetStringWidth(hFont,l_Path)>p_MaxW)
l_Path:=""
;-- Return to sender
Return l_Path
}
;------------------------------
;
; Function: Fnt_CreateFont
;
; Description:
;
; Creates a logical font.
;
; Parameters:
;
; p_Name - Typeface name of the font. [Optional] If null (the default), the
; default GUI font name is used.
;
; p_Options - Font options. [Optional] See the *Options* section for more
; information.
;
; Options:
;
; The following options can be used in the p_Options parameter.
;
; bold - Set the font weight to bold (700).
;
; italic - Create an italic font.
;
; q{quality} - Text rendering quality. For example: q3. See the function's
; static variables for a list of possible quality values. AutoHotkey
; v1.0.90+.
;
; s{SizeInPoints} - Font size (in points). For example: s12
;
; strike - Create a strikeout font.
;
; underline - Create an underlined font.
;
; w{FontWeight} - Font weight (thickness or boldness), which is an integer
; between 1 and 1000 (400 is normal and 700 is bold). For example: w600
;
; To specify more than one option, include a space between each. For
; example: s12 bold
;
; Returns:
;
; A handle to a logical font.
;
; Calls To Other Functions:
;
; * <Fnt_GetFontName>
; * <Fnt_GetFontSize>
;
; Remarks:
;
; When no longer needed, call <Fnt_DeleteFont> to delete the font.
;
;-------------------------------------------------------------------------------
Fnt_CreateFont(p_Name="",p_Options="")
{
Static Dummy3436
;-- Device constants
,LOGPIXELSY:=90
;-- Font quality
,DEFAULT_QUALITY :=0
,DRAFT_QUALITY :=1
,PROOF_QUALITY :=2 ;-- AutoHotkey default
,NONANTIALIASED_QUALITY:=3
,ANTIALIASED_QUALITY :=4
,CLEARTYPE_QUALITY :=5
;-- Misc. font constants
,CLIP_DEFAULT_PRECIS:=0
,DEFAULT_CHARSET :=1
,FF_DONTCARE :=0
,FW_NORMAL :=400
,FW_BOLD :=700
,OUT_TT_PRECIS :=4
;-- Parameters
p_Name:=Trim(p_Name," `f`n`r`t`v")
;-- Remove all leading/trailing white space
;-- Initialize options
o_Italic :=False
o_Quality :=PROOF_QUALITY
o_Size :="" ;-- Undefined
o_Strikeout:=False
o_Underline:=False
o_Weight :="" ;-- Undefined
;-- Extract options (if any) from p_Options
Loop Parse,p_Options,%A_Space%
{
if (InStr(A_LoopField,"bold")=1)
o_Weight:=FW_BOLD
else if (InStr(A_LoopField,"italic")=1)
o_Italic:=True
else if (InStr(A_LoopField,"strike")=1)
o_Strikeout:=True
else if (InStr(A_LoopField,"underline")=1)
o_Underline:=True
else if (InStr(A_LoopField,"q")=1)
o_Quality:=SubStr(A_LoopField,2)
else if (InStr(A_LoopField,"s")=1)
o_Size:=SubStr(A_LoopField,2)
else if (InStr(A_LoopField,"w")=1)
o_Weight:=SubStr(A_LoopField,2)
}
;-- Fix invalid or unspecified parameters/options
if p_Name is Space
p_Name:=Fnt_GetFontName() ;-- Typeface name of default GUI font
if o_Quality is not Integer
o_Quality:=PROOF_QUALITY
if o_Size is not Integer
o_Size:=Fnt_GetFontSize() ;-- Font size of default GUI font
if o_Weight is not Integer
o_Weight:=FW_NORMAL
;-- Convert point size to height, in logical units
hDC:=DllCall("CreateDC","Str","DISPLAY","Ptr",0,"Ptr",0,"Ptr",0)
o_Height:=Round(o_Size*DllCall("GetDeviceCaps","Ptr",hDC,"Int",LOGPIXELSY)/72)*-1
DllCall("DeleteDC","Ptr",hDC)
;-- Create font
hFont:=DllCall("CreateFont"
,"Int",o_Height ;-- nHeight
,"Int",0 ;-- nWidth
,"Int",0 ;-- nEscapement (0=normal horizontal)
,"Int",0 ;-- nOrientation
,"Int",o_Weight ;-- fnWeight
,"UInt",o_Italic ;-- fdwItalic
,"UInt",o_Underline ;-- fdwUnderline
,"UInt",o_Strikeout ;-- fdwStrikeOut
,"UInt",DEFAULT_CHARSET ;-- fdwCharSet
,"UInt",OUT_TT_PRECIS ;-- fdwOutputPrecision
,"UInt",CLIP_DEFAULT_PRECIS ;-- fdwClipPrecision
,"UInt",o_Quality ;-- fdwQuality
,"UInt",FF_DONTCARE ;-- fdwPitchAndFamily
,"Str",p_Name) ;-- lpszFace
Return hFont
}
;------------------------------
;
; Function: Fnt_CreateCaptionFont
;
; Description:
;
; Creates a logical font with the same attributes as the caption font.
;
; Returns:
;
; A handle to a logical font.
;
; Calls To Other Functions:
;
; * <Fnt_GetNonClientMetrics>
;
; Remarks:
;
; When no longer needed, call <Fnt_DeleteFont> to delete the font.
;
;-------------------------------------------------------------------------------
Fnt_CreateCaptionFont()
{
Return DllCall("CreateFontIndirect","Ptr",Fnt_GetNonClientMetrics()+24)
}
;------------------------------
;
; Function: Fnt_CreateMenuFont
;
; Description:
;
; Creates a logical font with the same attributes as the font used in menu
; bars.
;
; Returns:
;
; A handle to a logical font.
;
; Calls To Other Functions:
;
; * <Fnt_GetNonClientMetrics>
;
; Remarks:
;
; When no longer needed, call <Fnt_DeleteFont> to delete the font.
;
;-------------------------------------------------------------------------------
Fnt_CreateMenuFont()
{
Return DllCall("CreateFontIndirect","Ptr",Fnt_GetNonClientMetrics()+(A_IsUnicode ? 224:160))
}
;------------------------------
;
; Function: Fnt_CreateMessageFont
;
; Description:
;
; Creates a logical font with the same attributes as the font used in message
; boxes.
;
; Returns:
;
; A handle to a logical font.
;
; Calls To Other Functions:
;
; * <Fnt_GetNonClientMetrics>
;
; Remarks:
;
; When no longer needed, call <Fnt_DeleteFont> to delete the font.
;
;-------------------------------------------------------------------------------
Fnt_CreateMessageFont()
{
Return DllCall("CreateFontIndirect","Ptr",Fnt_GetNonClientMetrics()+(A_IsUnicode ? 408:280))
}
;------------------------------
;
; Function: Fnt_CreateSmCaptionFont
;
; Description:
;
; Creates a logical font with the same attributes as the small caption font.
;
; Returns:
;