[Library] Fnt v3.0 - Do Stuff With Fonts

Post your working scripts, libraries and tools
rommmcek
Posts: 1053
Joined: 15 Aug 2014, 15:18

Re: [Library] Fnt v2.0 - Do Stuff With Fonts

28 Jan 2017, 06:15

I made a small contribution regarding ToolTip. I've packed all the stuff in the function ToolTipFnt() and appended it to Fnt.ahk for easier use. I noticed an additional positive effect on my system (Windows 10 at 120 DPI):
Using this function, ToolTip when touching the bottom of the screen, won't flip any more to the top, instead it behaves exactly like native Windows ToolTip.

Code: Select all

ToolTipFnt(fs:="s14", ff:="") {
    ;-- Initialize 
    hTT :=0
    ;-- Get the current process ID
    Process Exist
    ScriptPID :=ErrorLevel
    ;-- Find the tooltip
    WinGet IDList,List,ahk_pid %ScriptPID%
    Loop %IDList%
        {
        WinGetClass Class,% "ahk_ID " . IDList%A_Index%
        if (Class="tooltips_class32")
            {
            hTT:=IDList%A_Index%
            Break
            }
        }
    ;-- Create font
    ;~ hFont :=Fnt_CreateFont("Arial Unicode MS","s14") ; - orignal syntax
    hFont :=Fnt_CreateFont(ff, fs)
    ;-- Set the font on the tooltip control and redraw
    Fnt_SetFont(hTT,hFont)
        ;-- RControlSend, AListCtl321, {Tab}, ahk_class CabinetWClassedraw set to FALSE (the default)
    Fnt_UpdateTooltip(hTT)
        ;-- This forces the tooltip to redraw without deactivating it first
}
P.s.: As a measure against broken ToolTip display, which occurs occasionally, it seems to work Sleep, -1 before Return hFont in Fnt_CreateFont() function of the Fnt.ahk library.

Two updated (7) examples, one original from jballi using ToolTipFnt():
Attachments
Examples.zip
(2.19 KiB) Downloaded 159 times
Last edited by rommmcek on 05 Jan 2018, 13:47, edited 3 times in total.
User avatar
jballi
Posts: 620
Joined: 29 Sep 2013, 17:34

Re: [Library] Fnt v2.0 - Do Stuff With Fonts

28 Jan 2017, 23:50

rommmcek,

Thank you for your contribution. I tested it and both of the examples work great! I have a few notes. I hope you don't mind.

1) The function creates a logical font but there is no mechanism for deleting the font. Fixing this is only needed for completeness. The amount of memory saved is trivial.

This is an easy fix. Simply have the function return the handle to the font that was created (Ex: Return hFont) and then delete the font after the tooltip is destroyed. For example:

Code: Select all

Tooltip Brown Chicken`, Brown Cow
hTTFont:=ToolTipFnt()
MsgBox The tooltip will continue to show while this window is open.
Tooltip                   ;-- Destroy the tooltip
Fnt_DeleteFont(hTTFont)   ;-- Delete the tooltip font
2) Calling the function multiple times for the same AutoHotkey tooltip is not a good idea because a new font is created for every call and the previous font (if any) is not destroyed. This will create the equivalent of a memory leak that will use a tiny bit of additional memory every time the function is called. Probably not enough to worry about for the average script but the extra memory usage can add up for a long-running script.

One possible fix is to delete the previous font before creating a new one (see #1) but the better and more efficient solution is to only call the function once for every tooltip. Let me explain...

When a tooltip is created for the first time, AutoHotkey will create a new tooltip control. That control is destroyed when the Tooltip command is called without any parameters. For example:

Code: Select all

Tooltip Brown Chicken   ;-- New tooltip control is created
Sleep 2000
Tooltip                 ;-- Tooltip control is destroyed
However, if a tooltip exists and the Tooltip command is called again, a new tooltip control is not created. Instead, the tooltip control is updated with the new text from the Tooltip command. Since a new tooltip control is not created, the font changes made by the function remain in effect. For example:

Code: Select all

Tooltip Brown Chicken   ;-- New tooltip control is created
hTTFont:=ToolTipFnt()   ;-- New tooltip font is set
Sleep 2000
Tooltip Brown Cow       ;-- The same tooltip control is used
;-- No need to set/update the font here
Sleep 2000
Tooltip                 ;-- Tooltip control is destroyed
Fnt_DeleteFont(hTTFont)
3) In your example, you imply that the function somehow fixes a tooltip positioning bug when the pointer and the tooltip is moved to the bottom right section of the screen. The positioning code for the AutoHotkey tooltip has been in AutoHotkey since as long as I can remember and changing the font or font size doesn't (or shouldn't) change how it works. OTOH, I'm not experiencing the "skip to the top" problem you mentioned and I don't have Windows 10 to try to duplicate the problem.

Once again, thank you for your contribution.
rommmcek
Posts: 1053
Joined: 15 Aug 2014, 15:18

Re: [Library] Fnt v2.0 - Do Stuff With Fonts

03 Feb 2017, 19:19

This is what we get when a layman firefights his problems!
Despite frequent admonishments in the library, I just didn't pay attention!

Manny thanks for your survey & fix!

P.s.: I just updated examples.
P.p.s.: The anti skip example doesn't prove anything (I tested it extensively), but skipping to the top remains an issue on Win 10, even at 96 DPI and "my" function for sure mitigate this problem, but I couldn't isolate the trigger.
User avatar
jballi
Posts: 620
Joined: 29 Sep 2013, 17:34

Re: [Library] Fnt v2.0 - Do Stuff With Fonts

29 Sep 2017, 19:44

Tab Stop Size

Preface
This discussion is about the tab stop size in some of the Microsoft common controls and dialogs, not the tab stop size in applications like Microsoft Word.

Introduction
The amount of space that a tab character is expanded to is known as the "tab stop size". For some reason, Microsoft created two different methods for measuring the size of a tab stop. Both methods are based on the width of an "average character" but the methods for calculating the width of an "average character" are different.

Method 1
In the first method, the width of an "average character" is defined by the tmAveCharWidth member of the font's metrics. Typically this is the width of the letter x but it can be anything. This value can be collected by calling Fnt_GetFontAvgCharWidth. The tab stop size is the width of 8 "average characters". Ex: Fnt_GetFontAvgCharWidth(hFont)*8.

The Text and Tooltip controls use this measurement method. There may be others.

Credit: Thanks to jeeswg for figuring out that the tab stop size for these controls are based on the font's average character width as defined by the font's text metrics.

Method 2
In the second method, the width of an "average character" is the average width of a character in the following string: "AaBbCc...XxYyZz". The width for this "average character" measurement can be collected by calling Fnt_GetDialogBaseUnits.

The name for this measurement is not consistent in the Microsoft documentation. In many cases, this measurement is written as "average character" or the "width of an average character". When this measurement relates to a Microsoft custom dialog, is sometimes labeled as Dialog Base Units. Although Dialog Base Units is an unfortunate name for this measurement, it is more useful than "average character" which has two different meanings. In addition, the tab stop size for some of these controls are set in Dialog Template Units which are based on Dialog Base Units. For these controls, the default tab stop size is the font's Dialog Base Units times 8. Ex: Fnt_GetDialogBaseUnits(hFont)*8.

The Edit, ListBox, and RichEdit controls use this measurement method as well as the MsgBox dialog. There may be others.
User avatar
jballi
Posts: 620
Joined: 29 Sep 2013, 17:34

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

02 Jan 2018, 00:41

v3.0
Major new release. See the Release Notes section of the first post for the details.

If using a earlier version of the libary, please read the Release Notes section of the first post and the library documentation, especially the Issues and Considerations section, before using the new version.
robodesign
Posts: 672
Joined: 30 Sep 2017, 03:59
Facebook: marius.sucan
GitHub: mariussucan
Location: Romania
Contact:

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

16 Jan 2018, 16:48

It's a lovely project and very nicely coded and organized. Thank you very much.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
rommmcek
Posts: 1053
Joined: 15 Aug 2014, 15:18

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

27 Jun 2019, 09:13

Just made custom color options for ToolTipFnt().
Fresh code, very little tested though!
Attachments
ToolTipFnt.png
ToolTipFnt.png (35.51 KiB) Viewed 3862 times
Fnt.ahk
(140.99 KiB) Downloaded 75 times
Example - ToolTipFnt funtion.ahk
(1.18 KiB) Downloaded 80 times
burque505
Posts: 1358
Joined: 22 Jan 2017, 19:37

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

02 Jul 2019, 16:10

@jballi, thanks for this library, I downloaded it months ago but haven't opened it till now.
@rommmcek, very, very nice. That's going to be useful.
Regards,
burque505
m3user
Posts: 164
Joined: 17 Jan 2014, 18:11

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

31 Oct 2019, 18:43

Wonderful library, thanks!
I just wonder if there is a way to hide only the color selection in Choose Font standard dialog? I know I can hide all effects but then underline and strikeout options are hidden too.
User avatar
jballi
Posts: 620
Joined: 29 Sep 2013, 17:34

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

01 Nov 2019, 05:19

Thank you for your interest.
m3user wrote:
31 Oct 2019, 18:43
I just wonder if there is a way to hide only the color selection in Choose Font standard dialog? I know I can hide all effects but then underline and strikeout options are hidden too.
No, not with the standard ChooseFont dialog. If the CF_EFFECTS flag is included, all of the elements in the Effects group (Strikeout, Underline, and Color) are shown. There is no way to hide or delete the Color element without writing some code that hides or deletes the Color controls after the dialog is showing. Not recommended.

The Fnt_ChooseFontDlg add-on function was written to specifically deal with this type of problem. It allows the developer the option to choose which elements of a custom "Choose Font" dialog are shown. You can easily create a dialog that only excludes the Color control. In addition, the custom "Choose Font" dialog pops up significantly faster than the standard ChooseFont dialog. See the example scripts for an example.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

01 Nov 2019, 08:53

Hello jballi. Do you have any idea why the font dialog can be slow to open, e.g. on Notepad (Windows 7), at around 10 seconds. It's often slow the first time, but then fine after that. Btw Notepad only lists 158 fonts for me. Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
m3user
Posts: 164
Joined: 17 Jan 2014, 18:11

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

01 Nov 2019, 18:07

Thanks jballi, very useful function. I see the "Character set" can be selected in the Example - Get list of fonts. Is this the same as "Script" selection in the standard dialog? Do you know what is the practical purpose of this two selections?
User avatar
jballi
Posts: 620
Joined: 29 Sep 2013, 17:34

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

02 Nov 2019, 06:37

jeeswg wrote:
01 Nov 2019, 08:53
Do you have any idea why the font dialog can be slow to open, e.g. on Notepad (Windows 7), at around 10 seconds. It's often slow the first time, but then fine after that.
Notepad uses the standard ChooseFont dialog and yes, it does take an unusually long time to start it for the first time. I don't know why.
jeeswg wrote:
01 Nov 2019, 08:53
Btw Notepad only lists 158 fonts for me.
Some programs, especially native Windows programs, try hard to combine like font families into a single group. For example, the Font window in the Control Panel and the ChooseFont dialog combine like font families into a single group. This is done specifically for these programs and is not available elsewhere. These custom groupings work OK for some typefaces like Arial but doesn’t work as well for typefaces like Segoe which are represented by a large number of font families. Some combinations of the font typeface and font attributes cannot be selected when the fonts are grouped this way.

The Fnt library does not group the font families together so the user will see few more fonts.
User avatar
jballi
Posts: 620
Joined: 29 Sep 2013, 17:34

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

02 Nov 2019, 07:31

m3user wrote:
01 Nov 2019, 18:07
I see the "Character set" can be selected in the Example - Get list of fonts. Is this the same as "Script" selection in the standard dialog? Do you know what is the practical purpose of this two selections?
Yes and no.

Disclaimer: I am not an expert on this topic. Some of the following is based on speculation. If I get anything wrong, please feel free to correct me.

Fonts for computers have evolved dramatically since they were originally introduced. A "character set" is just the list of characters that are used for a particular purpose. The original purpose was to identify different languages but with the introduction of Unicode and fonts that now support many languages, the purpose and need to identify a particular character set has changed.

The ChooseFont dialog uses the Script combo box to show a list of characters sets supported by the font typeface that is currently selected. Selecting a particular character set (Ex: Greek) doesn't really do anything that I'm aware of. The user must still enter the correct characters needed for the particular language.

More:
https://docs.microsoft.com/en-us/windows/win32/gdi/character-sets-used-by-fonts

The Fnt library uses "Character Set" as a means to filter the list of fonts that support that character set. It can be helpful if there are only a limited number of fonts that support a particular language.

Language is the primary use but "Symbol" is also a character set. It can be used to only show fonts that only contain symbols.
m3user
Posts: 164
Joined: 17 Jan 2014, 18:11

Re: [Library] Fnt v3.0 - Do Stuff With Fonts

03 Nov 2019, 17:09

Thanks jballi, very much appreciated.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: TheArkive and 25 guests