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

Post your working scripts, libraries and tools
rommmcek
Posts: 622
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 99 times
Last edited by rommmcek on 05 Jan 2018, 13:47, edited 3 times in total.
User avatar
jballi
Posts: 553
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: 622
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: 553
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: 553
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: 488
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)
My home page.
rommmcek
Posts: 622
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 335 times
Fnt.ahk
(140.99 KiB) Downloaded 18 times
Example - ToolTipFnt funtion.ahk
(1.18 KiB) Downloaded 17 times
burque505
Posts: 981
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

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 47 guests