Truncation using Pixels

Is there any method available to truncate string based on Pixels in
Ruby?

Thanks,
Dhanuja

On 12/07/2011 12:40 PM, Dhanuja B. wrote:

Is there any method available to truncate string based on Pixels in
Ruby?

I’m not aware of any built-in method to do this, and I suspect it will
depend greatly on how exactly you’re trying to render the string to the
display device. For instance font selection and point size are probably
the biggest factors that determine the rendered representation of a
string, and those are generally variable. The tools available to you to
figure out the string size in pixels almost certainly varies depending
on the library or framework you’re using to handle the actual work.

So, first of all, how do you plan to render your strings, disregarding
the overall length in pixels? Are you going to have them inserted into
a web page that is viewed by a browser, or are you using something like
GTK, QT, etc. as a conventional graphical user interface, or are you
doing something else?

Can you provide an example of truncating strings the way you need in
another language?

-Jeremy

Thanks Jeremy for the reply.

We need the truncation method to display the text in the browser.
It should support the widely used browsers.

Example1: This is a long piece of text which will need to be truncated
Example1: THIS IS A LONG PIECE OF TEXT WHICH WILL NEED TO BE TRUNCATED

In Display, the column width will be less and the text needs to be
truncated.

Expected Result1: This is a long…
Expected Result2: THIS IS A LON…

Result when character truncation is used: THIS IS A LON
G…

To avoid this, we need to use truncation based on pixels.

Thanks in Advance,
Dhanuja

Jeremy B. wrote in post #1035630:

On 12/07/2011 12:40 PM, Dhanuja B. wrote:

Is there any method available to truncate string based on Pixels in
Ruby?

I’m not aware of any built-in method to do this, and I suspect it will
depend greatly on how exactly you’re trying to render the string to the
display device. For instance font selection and point size are probably
the biggest factors that determine the rendered representation of a
string, and those are generally variable. The tools available to you to
figure out the string size in pixels almost certainly varies depending
on the library or framework you’re using to handle the actual work.

So, first of all, how do you plan to render your strings, disregarding
the overall length in pixels? Are you going to have them inserted into
a web page that is viewed by a browser, or are you using something like
GTK, QT, etc. as a conventional graphical user interface, or are you
doing something else?

Can you provide an example of truncating strings the way you need in
another language?

-Jeremy

If you’re rendering in a web browser, unless you’re using some sort of
Ruby plugin within the browser, you very likely won’t ever be able to
know text width in pixels unless you were to implement a server-side
test renderer that used exactly the same algorithms and font data that
the browser did.

Your question looks a lot like this one in a sense:

You could possibly do this within the browser using JavaScript and one
of the proposed text measuring methods the above link includes by
test-rendering a string (keeping it hidden so it’s not actually seen
by the browser user), then adjusting your truncation (binary search
for optimum truncation width, maybe?).

Aaron out.

On Thu, Dec 8, 2011 at 11:32 AM, Dhanuja B. [email protected]
wrote:

We need the truncation method to display the text in the browser.

That’s what CSS is for. Set a width on the containing element and use
the overflow: hidden to truncate.

There’s a CSS3 attribute ‘text-overflow’ which provides an ellipsis on
the end of line where truncation occurs; if you need to support older
browsers you can use the :after pseudo-element.

HTH,

“Dhanuja B.” [email protected] wrote:

We need the truncation method to display the text in the browser.
It should support the widely used browsers.

This is simply impossible to do on the server side, in part because you
don’t know exactly what font substitutions might be made. The only way
to
compute the pixel size of a string is to know the exact pixel width of
all
of the font glyphs in the string, plus any kerning rules that might be
used. You do not have that information, and you cannot get that
information.

You’ll have to use CSS.