Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 190825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:17:21+00:00 2026-05-11T16:17:21+00:00

This is a long shot, but does anyone know of an algorithm for estimating

  • 0

This is a long shot, but does anyone know of an algorithm for estimating and categorising text width (for a variable width font) based on its contents?

For example, I’d like to know that iiiiiiii is not as wide as abcdefgh, which in turn is not as wide as WWWWWWWW, even though all three strings are eight characters in length.

This is actually an attempt to build some smarts into a string truncation method, which at the moment is correctly truncating a visually wide string, but is also unnecessarily truncating a visually narrow string, because both strings contain the same number of characters. It’s probably sufficient for the algorithm to categorise the input string as narrow, normal or wide and then truncate as appropriate.

This question isn’t really language-specific, but if there is an algorithm then I’ll implement it in Java. This is for a web application. I’m aware that there are answers on SO that deal with this problem using JavaScript to obtain the width of a containing div element, but I wondered if a server-side solution is possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:17:21+00:00Added an answer on May 11, 2026 at 4:17 pm

    Most GUI frameworks provide some way to calculate text metrics for fonts on given output devices.

    Using java.awt.FontMetrics, for example, I believe you can do this:

    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics; 
    
    public int measureText(Graphics g, String text) {
       g.setFont(new Font("TimesRoman", Font.PLAIN, 12));
       FontMetrics metrics = g.getFontMetrics();
    
       return metrics.stringWidth(text);
    }
    

    Not tested, but you get the idea.


    Under .Net you can use the Graphics.MeasureString method. In C#:

    private void MeasureStringMin(PaintEventArgs e)
    {
    
        // Set up string.
        string measureString = "Measure String";
        Font stringFont = new Font("Arial", 16);
    
        // Measure string.
        SizeF stringSize = new SizeF();
        stringSize = e.Graphics.MeasureString(measureString, stringFont);
    
        // Draw rectangle representing size of string.
        e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
    
        // Draw string to screen.
        e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 110k
  • Answers 110k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You may want to read up on the basics of… May 11, 2026 at 9:33 pm
  • Editorial Team
    Editorial Team added an answer Many other vector formats (like PostScript and PDF) will let… May 11, 2026 at 9:33 pm
  • Editorial Team
    Editorial Team added an answer As it turns out it has nothing to do with… May 11, 2026 at 9:33 pm

Related Questions

I have a set of Visual Studio Team System unit (integration really) tests that
I know this one is going to be a long-shot, but I thought I'd
I'm not entirely sure if this is possible, but does anyone know how, or
This is a pretty vague question and getting it answered seems like a long
I'm working on a project where we're using a Java applet for part of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.