GetTextWidth not working for unicode characters

GetTextWidth not working for unicode characters

Forum member · Mar 28, 2011
This is to let you know of a shortcoming and a workaround: The following reports the wrong width when you use a unicode character such as the TM character: Dim myText As String = "What is the ™ Length?" Dim titleWidth1 As Single = Font.Courier.GetTextWidth(myText, 15) This is a workaround to get an approximate correct answer: Dim titleWidth1 As Single = Font.Courier.GetTextWidth(Replace(myText, "™", "W"), 15) Thanks for a very good product. Nick

Re: GetTextWidth not working for unicode characters

Support · Mar 29, 2011
Hello Nick, We did some testing on our end and we are not able to recreate the behavior which you have mentioned about returning wrong width for the "TM" text. Can you please let us know what values you are getting for the titleWidth1 using your code and what is the expected values? Please send over more details regarding your problem to our support team at:[^a|mailto:support@cete.com|opennewiwndow|support@cete.com^] so that they can look into it further. Thanks, ceTe Software Support Team.

Re: GetTextWidth not working for unicode characters

Support · Jun 21, 2011
Hello, We did some further testing on this font issue. When you are using the trademark symbol you will need to use decimal (8482) or hex (2122) codes with correct escape sequences. The characters &# preceding the number is causing it to not recognize as the code for special character hence it is counting each of the seven characters. If you use Convert.ToChar(8482) or the string "™" should work. Please see the code below, since we do not have the font file that you are using we used the Arial Unicode font for testing. Dim document As Document = New Document() Dim page As Page = New Page(PageSize.A4, PageOrientation.Landscape) document.Pages.Add(page) Dim symblolFont As ceTe.DynamicPDF.Text.OpenTypeFont = New ceTe.DynamicPDF.Text.OpenTypeFont("ARIALUNI.TTF") Dim myText As String = "What is the " & System.Text.RegularExpressions.Regex.Unescape("\u2122") & " Length?" Dim titleWidth1 As Decimal = symblolFont.GetTextWidth(myText, 15) Dim l1 As Label = New Label(myText, 100, 100, titleWidth1, 20, symblolFont) page.Elements.Add(l1) Dim myText1 As String = myText.Replace("™", "a") Dim width1 As Decimal = symblolFont.GetTextWidth(myText1, 15) Dim l2 As Label = New Label(myText1, 100, 200, width1, 20, symblolFont) page.Elements.Add(l2) Dim myText2 As String = "What is the ™ Length?" Dim titleWidth2 As Single = Font.Courier.GetTextWidth(myText, 15) Dim l3 As Label = New Label(myText2, 100, 300, titleWidth2, 20, symblolFont) page.Elements.Add(l3) Dim myText3 As String = "I-TRaC™ REPORT" Dim titleWidth3 As Single = Font.Courier.GetTextWidth(myText, 15) Dim l4 As Label = New Label(myText3, 100, 350, titleWidth3, 20, symblolFont) page.Elements.Add(l4) 'Dim myText4 As String = myText3.Replace("™", "™") OR Dim myText4 As String = myText3.Replace("™", Convert.ToChar(8482)) Dim titleWidth4 As Single = Font.Courier.GetTextWidth(myText, 15) Dim l5 As Label = New Label(myText4, 100, 400, titleWidth4, 20, symblolFont) page.Elements.Add(l5) document.Draw("C:\MyDocument.pdf") Thanks, ceTe Software Support Team.