# g.StringWidth return wrong results with non ASCII values included

**URL:** <https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124>\
**Category:** General\
**Created:** [November 2, 2014, 9:46am UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124 "2014-11-02T09:46:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 2, 2014, 9:46am UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124/1 "2014-11-02T09:46:27Z")

</div>

I do not have the sources with me right now, but I saw a wrong result in an application.

I used g.StringWidth, etc. to get a ListBox column width / Set LB.ColumnWidths

An entry had non ASCII characters in it and that column width is incorrect.  
The entry is: \<Nom de code Cimeterre\>  
(without \< and \>)

Do we know this (as a bug) ?  
(27 entries returned by Feedback; search strins is “StringWidth”.)

Notes (after checking docs): I certainly use Integer instead of Double.

MacBook Pro 2011-11  
OS X 10.9.5  
Xojo 2014r2.1

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [November 2, 2014, 9:50am UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124/2 "2014-11-02T09:50:19Z")

</div>

> [@140735:@Emile Schwarz](#):
>
> \<Nom de code Cimeterre\>

I see only ASCII characters there. It would be more useful to post the entry returned that has higher Unicode points.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [November 2, 2014, 10:22am UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124/3 "2014-11-02T10:22:49Z")

</div>

I just built a small test project that measures strings entered in a TextField and reports the StringWidth.

“ete” and “t” give exactly the same stringwidth.

Please to be more specific.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 2, 2014, 10:36am UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124/4 "2014-11-02T10:36:15Z")

</div>

I used curly quotes (start-end). These are not in the ASCII values (0-127). To be sure of what I say, copy / paste the text in the question (far above) into Apples TextEdit and resize the text to say 36 and you will see what I wanted to tell. My glasses needs to be improved, so I just do that in case Ive made a mistake.

On a second click (several minutes after, searches on [www.cbs.com](http://www.cbs.com), save data, etc.), and a manual resize, another click in the resize button shows a different behavior.

So, now, I do not know.

I know: I close the window but keep the application running,  
I re-open the window,  
I open the original text file,  
I clicked in the ToolBars Resize Columns button

and I get the same behavior. Worst, this also does not fit in the new column size:  
Les vieux he?ros ne meurent jamais

The displays stop at “jama” leaving “is” lost in the space/time warp !

ete and été: try in a ListBox and with a far larger string like La chanson de lété dernier and you may see a discrepancy.

All of this is really queer.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 2, 2014, 10:48am UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124/5 "2014-11-02T10:48:20Z")

</div>

I do not closed the window, but I changed the first column contents from an acronym to its full name, then I re-open the newly modified file and the ListBox width resize do correctly its job.

BTW: the other word to say Strange (last word of my previous entry have been changed. Why ?

That word is Q-U-E-E-R (Queen with an ending r instead of an ending n).

I found that word in some comic strips / newspaper of the 1930s and checked its meaning with Systran (I think).

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [November 2, 2014, 11:01am UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124/6 "2014-11-02T11:01:16Z")

</div>

[quote=140754:@Emile Schwarz]I do not closed the window, but I changed the first column contents from an acronym to its full name, then I re-open the newly modified file and the ListBox width resize do correctly its job.

BTW: the other word to say Strange (last word of my previous entry have been changed. Why ?

That word is Q-U-E-E-R (Queen with an ending r instead of an ending n).

I found that word in some comic strips / newspaper of the 1930s and checked its meaning with Systran (I think).[/quote]

Que-er is derogatory for gay. Then it is beeped like other profanities.

Back to your current issue of stringwidth, I cannot verify what you report.  
With Arial at point 36,  
ete or été = 50.04492  
La chanson de lete dernier or La chanson de lété dernier = 470.3027

My test program is awfully simple :

- One TextField set to Arial, 36 points
- OneTextArea for the results
- In the TextField :

`Function KeyDown(Key As String) As Boolean
  if key = chr(13) then
    dim p as new picture(me.width,me.height,32)
    p.graphics.textfont = me.textfont
    p.graphics.textsize = me.Textsize
    TextArea1.text = TextArea1.Text+str(p.graphics.stringwidth(me.text))+EndOfLine
    return true
  end if
End Function
`

Are you sure you are setting the same textfont and textsize for the listbox as for stringwidth ? Are you using System ? If so, you may want to switch to Arial (cross platform).

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 2, 2014, 11:15am UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124/7 "2014-11-02T11:15:07Z")

</div>

> same textfont and textsize for the listbox as for stringwidth  
> I probably used ListBox Text Size and ListBox TextFont.

Used font: probably system.

> you may want to switch to Arial  
> Good idea.

Dont shake your head. I will watch that carefully this afternoon with the project and after a shutdown / clean boot. I have some troubles with the rtf load (tabs are different from Apples TextEdit). I used declares to display those RTF files.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 3, 2014, 8:18am UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124/8 "2014-11-03T08:18:33Z")

</div>

Here are the answers to the previous questions in the following code:

[code] // a. Get the number of Rows & Columns  
ColCnt = LB.ColumnCount - 1  
RowCnt = LB.ListCount - 1

```
// Get the selected Font Name & Size
FontName = LB.TextFont
FontSize = LB.TextSize

// Get a new Picture
PictTmp = New Picture(600,40)

// Set the default Text Font & Size
PictTmp.Graphics.TextFont = FontName
PictTmp.Graphics.TextSize = FontSize

// b. Loop thru all Columns
For ColIdx = 0 To ColCnt
  For RowIdx = 0 To RowCnt
    // Get one Cell contents
    CellStr = LB.Cell(RowIdx,ColIdx)
    
    // Get the width of the Cell String
    CellWidth = PictTmp.Graphics.StringWidth(CellStr) + 10
    
    // Get the highest width
    ColWidth = Max(ColWidth,CellWidth)
    
    // Clears some variables
    CellStr = ""
  Next // Row Count[/code]

```

Note: `PictTmp = New Picture(600,40)
`Yes, I know I can use `PictTmp = New Picture(0,0)`

Note also that I add 10 pixels to the cell width !

PS: I do not have faith in Phantom or Ghost (Who Walks or fly), but sometimes I deep ly wonder

PPS: Michel, don’t you recall the piece of code that was shared lately in this forum ?  
(I wrote mine sometimes earlier, but I could use that one)

At last: I never clears any cache or whatever file between compilations. This may explain that ?

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:28pm UTC](https://forum.xojo.com/t/g-stringwidth-return-wrong-results-with-non-ascii-values-included/20124/9 "2020-10-29T17:28:40Z")

</div>


