Recent

Author Topic: Get correct text width  (Read 3482 times)

TonnyB

  • Newbie
  • Posts: 6
Get correct text width
« on: September 27, 2017, 02:39:13 am »
Hi there. I try to get text width.

Code: Pascal  [Select][+][-]
  1.   Memo1.Lines.Add(SynEdit1.Lines[0]+' is '+IntToStr(SynEdit1.Canvas.TextWidth(SynEdit1.Lines[0])));
  2.   Memo1.Lines.Add(SynEdit1.Lines[1]+' is '+IntToStr(SynEdit1.Canvas.TextWidth(SynEdit1.Lines[1])));

But i have different results: 35 and 45. First line is contained tabulator.

How to get real text width ?

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: Get correct text width
« Reply #1 on: September 27, 2017, 05:21:22 am »
Use monotype font. E.g. Courier New in Windows or Liberation Mono in Linux.

On the second thought:
Quote
First line is contained tabulator.
You mean that the first line contains a tab symbol, and the second - a few spaces, right? Hardly you can count on "tab width" being equal in different situations (e.g. you can choose your custom tab width in OpenOffice Writer which can be a non-integer ratio to a space width).
You may do a "tab to spaces" conversion (as IDE does). Or avoid using #9 symbol at all unless you're writing to a file in case you need setting exact screen width of strings. Or, of course, you can try figuring out how tab will render in each situation and for each different font, but I don't think it's worth it.
« Last Edit: September 27, 2017, 06:34:09 am by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

 

TinyPortal © 2005-2018