Home AMX User Forum AMX Technical Discussion

String pixel width

Anyone tried figuring out the width of a string as presented on a UI and then elegantly truncating if it exceeds the bounds of the text field? And no, word wrap is not an option.

There's some AWT classes that might work in Duet. What about in Netlinx?

Comments

  • Joe HebertJoe Hebert Posts: 2,159
    Don’t know if it’s an option for you or not but I’ve used a fixed-width font in the past to determine the length of the text on a button to do things like creating a stock ticker and a scrolling marquee.
  • PhreaKPhreaK Posts: 966
    If a fixed width doesn't suit the design and you are just using standard ASCII set yourself up a 255 element array. Place the rendered width of each character in there then iterate across your string until you reach the character that exceeds the element width - 1. It would be a pain to set up initially but once you've done a font once you could use it across any other systems.
  • ericmedleyericmedley Posts: 4,177
    PhreaK wrote: »
    If a fixed width doesn't suit the design and you are just using standard ASCII set yourself up a 255 element array. Place the rendered width of each character in there then iterate across your string until you reach the character that exceeds the element width - 1. It would be a pain to set up initially but once you've done a font once you could use it across any other systems.

    This is very slick. But with my luck here's how it would play out.

    ME: "see how that works? Now the text fits."

    CLIENT (yawning): "that's cool... But I don't like the font. Can I see it with several font examples?"
  • Something along those lines. I had in mind maybe factoring in a font size multipier as well.
    PhreaK wrote: »
    If a fixed width doesn't suit the design and you are just using standard ASCII set yourself up a 255 element array. Place the rendered width of each character in there then iterate across your string until you reach the character that exceeds the element width - 1. It would be a pain to set up initially but once you've done a font once you could use it across any other systems.
  • PhreaKPhreaK Posts: 966
    I not a typography expert by any means, however, I think different type faces can render characters differently depending on the size, i.e. it's not a linear mapping. Depending on how accurate you want to get with this it may be sufficient for your purposes though.

    I actually need to implement something similar on a current project. Looking at workload this will probably take place next week. I'll open source it up on the AMX Australia GitHub and most likely roll it into the modero library. Something like:
    truncateToWidth(char text[], integer width, integer characterWidths[255])
    
    That way you can just have a an array of character widths for the font in question. If you're using multiple sizes just keep an array handy for each and pass in the appropriate one as required. You could build these each manually or construct them on system boot based on a size multiplier from an initial array.

    If anyone is keen on building something together / a better a approach let me know as we can probably save each other some duplicate effort.
  • ericmedleyericmedley Posts: 4,177
    PhreaK wrote: »
    I not a typography expert by any means, however, I think different type faces can render characters differently depending on the size, i.e. it's not a linear mapping. Depending on how accurate you want to get with this it may be sufficient for your purposes though.

    I actually need to implement something similar on a current project. Looking at workload this will probably take place next week. I'll open source it up on the AMX Australia GitHub and most likely roll it into the modero library. Something like:
    truncateToWidth(char text[], integer width, integer characterWidths[255])
    
    That way you can just have a an array of character widths for the font in question. If you're using multiple sizes just keep an array handy for each and pass in the appropriate one as required. You could build these each manually or construct them on system boot based on a size multiplier from an initial array.

    If anyone is keen on building something together / a better a approach let me know as we can probably save each other some duplicate effort.

    Not to mention there's the whole thing with Kerning and so forth which deal with the spacing between letters, their position above or below the baseline, rotation on both axis, bla bla. Each font is literally a moving target to nail down. (...as I deftly mix my metaphors)
Sign In or Register to comment.