Results 1 to 3 of 3

Thread: Reverse of TextMetrics

  1. #1
    Ext JS Premium Member prakashpaudel's Avatar
    Join Date
    May 2009
    Location
    Kathmandu, Nepal
    Posts
    78

    Default Reverse of TextMetrics

    With reference to some element, TextMetrics is able to calculate the width of some text. Is it possible to do the reverse? ie. with width provided, how many characters on a text can accomodate on that given width?

  2. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,791

    Default

    I think you could do it using TextMetrics. Given the text you could keep removing characters off the end until it fits into the given width. Not the most efficient algorithm but it should be pretty easy to implement.

  3. #3
    Ext JS Premium Member stevil's Avatar
    Join Date
    Nov 2007
    Location
    Denver, CO
    Posts
    1,045

    Default

    Quote Originally Posted by skirtle View Post
    I think you could do it using TextMetrics. Given the text you could keep removing characters off the end until it fits into the given width. Not the most efficient algorithm but it should be pretty easy to implement.
    To amplify @skirtle's point, I think you HAVE to do it with TextMetrics. Trying to figure out how many characters will deterministically fit in a specified width (your initial request) will only work with a fixed-pitch font (Courier, monospace, etc.) If it's not a fixed-pitch font, then giving something a width to calculate text widths is useless, because it can't know the distribution of character widths you intend to render.

    Therefore, the best you can do is loop to get the string width and truncate it until it fits.

    stevil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •