Recent

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

TonnyB

  • New member
  • *
  • 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: 561
    • My "almost daily" development blog
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 »
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories