Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Library] Fnt v0.5 (Preview) - Do Stuff With Fonts


  • Please log in to reply
23 replies to this topic
jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
--------------------
Notice: Updates on this topic have been moved here.
--------------------

Introduction
AutoHotkey handles most of our font needs via the gui Font and GUIControl Font commands. The Fnt library was created to expand on what can be done with fonts.

Fnt Library
Key features:
  • Create fonts. Create and delete logical fonts as needed.
  • Calculate the size of GUI objects before creating them. Or after changing the font of an object.
  • Get the default GUI margins. The original reason I started this project. Useful for spacing multiple objects when the margins are unknown.
The Code
The pertinent files are as follows:Issues/Considerations
A few considerations:
  • Preview release. This is still a preview release of this library. Everything is subject to change.
  • Compatibility. The library was designed to run on all versions of AutoHotkey and all versions of Windows >= Window XP. The reality is that I only have one machine to test this software on. If you experience any problems with your version of AutoHotkey and/or your version of Windows, please let me know. I will try to correct the problem.
  • Examples. Most of the examples are ready to go but a few lack maturity and/or enough documentation. These weaknesses will be improved in future releases. If you have issues with any of the examples, please let me know. I will try to correct the problem.

References
Scripts that use the Fnt library:Final Thoughts
I hope that someone finds this library useful.

---------------------------------------------------------------------------
Release Notes

v0.1 (Preview)
First release.

v0.2 (Preview)
New Functions:
- Fnt_EnumFontFamExProc (internal function) - The default EnumFontFamiliesEx callback function for the Fnt library. Currently used by Fnt_GetListOfFonts.
- Fnt_GetListOfFonts - Generate a list of font names.
- Fnt_IsFixedPitchFont - Returns TRUE if the font is a fixed pitch font.
- Fnt_IsTrueTypeFont - Returns TRUE if the font is a TrueType font.
- Fnt_IsVectorFont - Returns TRUE if the font is a vector font.

Updates/Enhancements:
- Fnt_ChooseFont. Changes: 1) SizeMin and SizeMax options added to the p_Options parameter. These options allow the developer to limit the range of font sizes that can be entered/selected. 2) Added p_Flags parameter. [Optional] Additional ChooseFont flags can be added via this parameter. Note: The p_Flags parameter is an advanced feature.
- Fnt_GetFontOptions. Changes: 1) Added a "w{font weight}" return option to indicate the weight of the font. This option is only returned if the font weight is not normal (400) and not bold (700). 2) The "bold" return option is now only returned if the font weight is exactly 700.
- Added and updated examples.

v0.3 (Preview)
New Functions:
- Fnt_GetStringWidth - Shell call to Fnt_GetStringSize to get the width of the specified string.

Updates/Enhancements:
- Minor changes to a few functions.
- Added example to set the font of a tooltip. Thanks to uname for the idea.

v0.4 (Preview)
New Functions:
- Fnt_GetMaxStringSize - Calculates the size of a multiline string. Thanks to Verdlin for the idea.

Fixes/Updates/Enhancements:
- Fixed Fnt_ChooseFont to work on x64. Thanks to maestrith for identifying many of the correct offsets for x64 and to okram for identifying the outstanding offsets and for testing all of the changes.
- Minor changes to a few functions to correctly support x64.
- Added example to set the width of a tooltip.
- Added example to calculate the the complete size (w+h) of various controls.

v0.5 (Preview)
Fixes/Updates/Enhancements:
- Fnt_ChooseFont updated to no longer use the AutoHotkey "SetFormat, Integer" command which could potentially slow down some scripts.
- Fnt_GetFont and Fnt_SetFont updated to work on both visible and hidden windows.
- Minor documentation updates.


tidbit
  • Administrators
  • 2709 posts
  • Hates playing Janitor
  • Last active: Jan 15 2016 11:37 PM
  • Joined: 09 Mar 2008

can this be used to bypass AHK's "Too many fonts" error? 

http://www.autohotke...fonts/?p=562771


rawr. be very afraid
*poke*
. Populate the AutoHotkey city. Pointless but somewhat fun. .


jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

can this be used to bypass AHK's "Too many fonts" error? 
http://www.autohotke...fonts/?p=562771

Short answer:
Yes.

Long(er) answer:
Yes. Use Fnt_CreateFont to create the desired new font and then use Fnt_SetFont to attach the new font to the desired GUI object. Set the p_Redraw parameter to TRUE to see the changes immediately. To do it again (and again), do the same thing. Don't forget to use Fnt_DeleteFont when you no longer need a font.

I hope this helps.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
v0.2 (Preview)
A few new functions and a few updates. See the bottom of the first post for a list of changes.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
v0.3 (Preview)
One new function, a few updates, and one new example. See the bottom of the first post for a list of changes.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

Update: As of v0.4, this example is now included in the Fnt library.
 

 

Set Tooltip Width

Background
By default, the tooltips created by AutoHotkey can show multiple lines. If the the text is longer than the screen width, the tooltip control will break the tooltip text into multiple lines. This occurs automatically because when the tooltip control is created, AutoHotkey sets the maximum tip width to the width of the primary monitor. In addition, the developer can force multiple lines by including a newline character (i.e. "`n") within the tooltip text.

The Problem
Tooltips with a large amount of a text can be difficult to read because the text is displayed across the entire screen. The developer can improve readability by adding newline characters within the text but the workaround is hit-and-miss and can be problematic if the text is dynamic.

The Solution
In this example, the Fnt library is used to calculate a new "maximum tip width" for a tooltip based upon a "maximum number of characters" per line. This technique will show the same number of characters per line regardless of the current font or font size.

Observations and Notes
This example uses the Fnt_GetFontAvgCharWidth function to calculate the "maximum tip width" for the tooltip. As you might imagine, the calculation is dead-on when using a monospaced font. For proportional fonts, this technique is fairly accurate but the content can cause deviations. Example: Using text with all uppercase characters. If the text has unique characteristics, use Fnt_GetStringWidth for improved accuracy.

The Code
Requires the Fnt library.


Final Thoughts
I hope that someone finds this useful.



Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Just wanted to comment here, this is a great library! I have been searching many places in the forums for font functions, mainly ones for measuring text. The best I could find was a library by Majkinetor, and it wasn't as easy to use as this lib is. Great work here. This is definitely in my standard lib, now.


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
Thanks for giving it a try. I'm glad you found it useful.

Joe Glines
  • Members
  • 118 posts
  • Last active: Jan 24 2016 03:08 PM
  • Joined: 23 Dec 2009

I played with it a little.   I rarely build GUIS or display much with AutoHotKey so I'd never given much thought to the "problem" but this seeems very cool!  Opens up a lot of doors of having a polished looking tool.  :)


Automating the mundane 1 script at a time...
https://www.linkedin.com/in/joeglines
The-Automator

Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Does the library not take into account word wrapping, or am I simply doing something wrong?

 

Spoiler

Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

Does the library not take into account word wrapping, or am I simply doing something wrong?


Excellent question. The short answer is no, the library does not take into account word wrapping.

The Fnt_GetStringSize function (via the GetTextExtentPoint32 API function) is able to correctly calculate the width of a specified string but it does not take into consideration characters that may affect the height of a string such CR (Carriage Return) and/or CR+LF (Carriage Return and Line Feed) characters. This is documented in the Fnt_GetStringSize function. Moreover, the function can't possibly know the final width and height of a string once the font is attached to a control and the string is sent to the control.

In your example, a long string is sent to an Edit control with word wrapping enabled. The perceived height of the string has doubled because the string appears on 2 lines. That exact same string sent to an Edit control without word wrapping enabled would only appear on 1 line.

Calculating exactly how much space a string will use in a specific control can be complex. For the Edit control, I took a shot at it with the "Example - Calculate Control Height.ahk" example script that is published with the library. The script uses library functions to calculate most of the stuff but it also includes some undocumented values that I extracted from the AutoHotkey source. Combined, these values create a fairly accurate picture of how much space is needed for an Edit control with multiple lines.

I hope this helps.



Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

This did help, indeed! I did not use your precise example because, for my uses, I needed the number of rows to be dynamic. I wrote a string wrapping function which, from preliminary testing, seems to be working well. I thought you may be interested to see it, so I decided to post it below:

 

Spoiler

 

I was pleasantly surprised to see that your calculation takes into account font weight(? Like bold fonts). Great work!

 

Fyi, I understand string wrapping functions need to be as fast as possible. I would appreciate anyone's help in speeding this up. Specifically I think perhaps Rseding91 or MasterFocus would be interested given their work in the st_LineWrap function.


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.


jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005

I only did a few preliminary tests but it looks like it works, at least on my machine.  Good job. happy.png



jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
v0.4 (Preview)
One new function, a number of updates to support x64, and new and updated examples. See the bottom of the first post for a list of changes.

A special thanks to okram for performing all of the x64 testing for this version.

Verdlin
  • Members
  • 256 posts
  • Last active: Apr 29 2016 06:46 PM
  • Joined: 21 Dec 2012

Yes, yes, yes! Finally, a working Font dialog! Thank you.


Scripts are written and tested using AHK_H 64w (unless otherwise specified).

CFlyout. EasyIni. Dynamic Label Execution (No Reload). Word Lookup.