Watch, Follow, &
Connect with Us

Welcome, Guest
Guest Settings
Help

Thread: Text fit into cell algorithm



Permlink Replies: 1 - Last Post: Sep 7, 2017 12:49 PM Last Post By: Markus Humm Threads: [ Previous | Next ]
Markus Humm

Posts: 4,813
Registered: 11/9/03
Text fit into cell algorithm
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 7, 2017 9:17 AM
Hello,

I started to develop some Delphi algorithm to fit text in a TTMSFMXGrid
cell.Now I found out that it still misses something.

Conditions are: the cell has a fixed width, but you may change the
height of the cell and you may reduce font size up to a certain minimum.
You may wrap the text at the positions containing a space char or a -
and text may contain a unicode char $200B (zero width space char) which
is usually not displayed but where a word can be broken up without
having a - in it.

My algorithm works good in a lot of conditions already, but now I
encountered thexts which are too long so they need to be broken up but
they contain a few rather short words (sometimes even only 1-char
physical units). This leads to the unfortunate situation that the number
of lines is quite high, but the width of the cell is not being properly
used. This looks ugly.

Does anybody know some "universal" text wrapping and font rescaling
algorithm?

Greetings

Markus
Markus Humm

Posts: 4,813
Registered: 11/9/03
Re: Text fit into cell algorithm
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 7, 2017 12:49 PM   in response to: Markus Humm in response to: Markus Humm
Hello,

I now built an enhanced version of my algorithm which does what I want.
It's simple: after splitting the texts into lines you determine length
of each line in pixels.

Then you loop through the texts and check, whether two adjacent texts
(means two adjacent entries in the array with the splitted texts) can be
combined and will still fit into the column width. If necessary factor a
space char between those in.

If they can be combined do so and delete the 2nd of those texts from the
array. The new array delete function from XE7+ is a nice helper here.

Works like a charm.

Greetings

Markus
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02