Thread: Is there a way to determine string width (not string length)?

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    72

    Is there a way to determine string width (not string length)?

    Hi,

    I'm trying to create a scrolling feature for a webpage index. I'm trying to find a way to determine the width of a string.

    For example, I have two strings with 20 characters:
    `WWWWWWWWWWWWWWWWWWWW`
    `iiiiiiiiiiiiiiiiiiii`

    As you can see the width of the strings are different. Is there a way I can determine the width of a string. I don't care what unit they are measured in, I only want to be able to use a value, and from there I can determine what is to be done.

    Doing lots of searching isn't helping as the terms seem to be pretty generic. I did come across something talking about `GetTextMetrics`, but that seems to be for cpp. Is there something equivalent in c?

    Any insight is appreciated.

    Failing this, it looks like I will have to figure out a lookup table based on character ratios. It looks like the two strings above, uppercase `W` is ~4x bigger than lowercase `i`.

    Thanks.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,037
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    18
    Hm, can you mention why you'd want to do this? The text "width" you're talking about depends completely on the font the text is written in; for example, on a terminal, the font will most likely be monospaced, so all printable ASCII characters will have the same width.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,127
    GetTextMetrics has nothing to do with C++. It's a Windows API function that would appear to do what you want. Is your program a Windows GUI program?
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    72
    Ty for the links, the letter spacing doesn't help much but the `em` page helps. I will admit, when I first saw a reference to `em`, I wrote it off as `css` crap, as that is the only context, up until now, I have ever seen `em` mentioned in regards. But this is what I needed.

    Quote Originally Posted by erikkonstas View Post
    Hm, can you mention why you'd want to do this? The text "width" you're talking about depends completely on the font the text is written in; for example, on a terminal, the font will most likely be monospaced, so all printable ASCII characters will have the same width.
    I understand your curiosity, this is a private site that sets the font (it won't ever change). It isn't monospace, as per my original example.

    Quote Originally Posted by john.c View Post
    GetTextMetrics has nothing to do with C++. It's a Windows API function that would appear to do what you want. Is your program a Windows GUI program?
    I don't program in Windows, nor do I encourage it. I saw this in a cpp file that was apparently using a Windows API.

    Looks like the solution is to actually measure the characters used. Determine the average size of characters (which covers ~85% of them), Then adjust the timing based on how many smalls (i j . etc), and how many bigs (W M Q etc). Smalls reduce timing, bigs increase timing. It's not what I wanted, but the end results are the same.

    Ty for the replies.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    502
    Quote Originally Posted by erikkonstas View Post
    Hm, can you mention why you'd want to do this? The text "width" you're talking about depends completely on the font the text is written in; for example, on a terminal, the font will most likely be monospaced, so all printable ASCII characters will have the same width.
    Yes. You need to know what font your output device is using.
    Standard C has no functions for querying a font, but your platform will. Since getting the width of a string is such a common requirement, there will almost certainly be a function which does this directly. The hard part is often specifying the font.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read string length when string contains the null character
    By DecoratorFawn82 in forum C Programming
    Replies: 4
    Last Post: 01-06-2018, 09:07 AM
  2. how to get string width in pixels??
    By sar90 in forum C Programming
    Replies: 2
    Last Post: 07-02-2011, 07:10 AM
  3. Replies: 20
    Last Post: 03-08-2011, 05:16 PM
  4. Width of string
    By Mithoric in forum Windows Programming
    Replies: 2
    Last Post: 05-01-2004, 11:50 PM

Tags for this Thread