GetTextWidth bug when string contains "&"

Discussions on GFL SDK, the graphic library for reading and writing graphic files

Moderators: XnTriq, helmut, xnview

mindplay
Posts: 16
Joined: Wed Sep 21, 2005 9:30 am
Location: Denmark

GetTextWidth bug when string contains "&"

Post by mindplay »

For strings containing an ampersand (the "&" character), the GetTextWidth function returns a size that is too small - as you can see in the following simple ASP example, this is true.

Code: Select all

<%

Const AX_GIF = 2
Const AX_To8Colors = 8
Const AX_NoDither = 0
Const AX_ToColors = 0

BG = RGB(255,255,255)
FG = RGB(255,0,0)
Text = "High & Low"

Set Pic = Server.CreateObject("GflAx.GflAx")
Pic.EnableLZW = True

Pic.FontName = "Verdana"
Pic.FontSize = 36

W = Pic.GetTextWidth(Text)
H = Pic.GetTextHeight(Text)

Pic.NewBitmap W, H, BG
Pic.TextOut Text, 0, 0, FG

Pic.SaveFormat = AX_GIF
Pic.ChangeColorDepth AX_To8Colors, AX_NoDither, AX_ToColors
Pic.SaveBitmap Server.MapPath("test.gif")

%>
<HTML><HEAD></HEAD><BODY BGCOLOR=#000000><IMG SRC="test.gif"></BODY></HTML>
It happens because the "&" character in the windows API subroutine interprets "&" as meaning "underline the next character", which is used to display keyboard shortcuts in menus and dialogs.

To work around this problem, simply replace "&" with "&&" in your string when getting the text width, e.g.:

W = Pic.GetTextWidth(Replace(Text,"&","&&"))

The "&&" combination is interpreted by the windows subroutine as "an actual & character", and will give the correct resulting width.
User avatar
xnview
Author of XnView
Posts: 44926
Joined: Mon Oct 13, 2003 7:31 am
Location: France

Re: GetTextWidth bug when string contains "&"

Post by xnview »

mindplay wrote:For strings containing an ampersand (the "&" character), the GetTextWidth function returns a size that is too small - as you can see in the following simple ASP example, this is true.

It happens because the "&" character in the windows API subroutine interprets "&" as meaning "underline the next character", which is used to display keyboard shortcuts in menus and dialogs.

To work around this problem, simply replace "&" with "&&" in your string when getting the text width, e.g.:

W = Pic.GetTextWidth(Replace(Text,"&","&&"))

The "&&" combination is interpreted by the windows subroutine as "an actual & character", and will give the correct resulting width.
Ok, i'll fix it
Pierre.