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

  • SEARCH
  • Home
  • 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 6692681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:53:44+00:00 2026-05-26T05:53:44+00:00

I am looking for stable algorithm to draw possibly long text horizontally and vertically

  • 0

I am looking for stable algorithm to draw possibly long text horizontally and vertically centered on a 2D surface. This is not language specific, so if you can provide examples in any language it would be very helpful.

The following information I have about the text I want to draw and the surface:

Text infomation:

  • size of the text in px (line height), is of course way smaller than the height of the surface
  • the type face (could be any though)
  • simple character set from ASCII32 to ASCII126
  • text could be relatively long, words are separated by spaces
  • text should break “automatically” on spaces once it will not fit in the width of the surface anymore
  • text is in English.

Surface information:

  • width and height
  • x and y coordinates in the global coordinate system (assuming the origin is left top corner of the screen)

I did implement my own approach, but it was very unstable: text spread over the surface, alignment not correct, sometimes not centered at all. Also googled for some good approaches though couldn’t find something useful. I hope the stackoverflow community can help me. Thank you very much.

  • 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-26T05:53:45+00:00Added an answer on May 26, 2026 at 5:53 am

    The approach is simple in concept, a bit involved in execution. It involves only a few steps:

    Split the text into lines that will fit horizontally
    Compute the vertical position of the first line
    for each line
        Compute its width and the X position
        Display it at the Y position
        Add the line height to the current Y position
    

    The hard part is the first step. The rest is easy.

    If you have a fixed-width font, then splitting the text into lines isn’t too hard. You simply compute how many characters will fit in the given width, index into the string to that position, and then back up to the previous word break. Grab the substring from the start of the previous line (or start of the string for the first line) to that position, and that’s your line. Repeat until you get to the end of the string.

    With a variable-width font, things are a bit harder because you can’t just index into the string by n * character_width characters. Instead, you have to guess, test, refine the guess, etc. Every graphics subsystem I’ve worked with had some kind of MeasureString method that would tell me how many pixels it would take to render a string, given a particular font. Given that, what I’ve done in the past is:

    Divide the surface width by the font's average character width
    Index that far into the string, and find the next (or previous) word break.
    Measure the string
    If the result is wider than the width, go back one word and measure again.
    If the result is narrower than the width, go forward one word and measure again.
    Repeat the measure/adjust until you find the string that will fit.
    

    Once you’ve found the substring that will fit, move your start index forward to the start of the next line, and do it again until you reach the end of the string.

    Vertically centering the group of lines:

    starting_position.Y = (Surface_height - (num_lines * line_height)) / 2
    

    Horizontally centering a line is easily accomplished:

    starting_position.X = (Surface_width - Measured_string_width) / 2
    

    You have to compute the starting_position.X for each line. The Y coordinate is increased by line_height for each successive line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a STABLE FTP library for Compact Framework. I would prefer
I am looking for a .net templating engine - something simple, lightweight, stable with
I'm looking for a collection of rich, well-written, stable, customizable and cool jsp 'controls'.
I am looking for a good and stable threadpool library for C++ that's fairly
Looking for the guice support in restlet I have come across this article -
I'm looking for a really simple / limited scripting language for the JVM or
I'm looking at doing some initial tests and experiments with a toy language and
I have been looking for a stable RSS/Atom library for .NET, preferably open source
I just downloaded the stable release of Reactive Extensions for .NET and am looking
Looking for suggestions on how to best track this project structure in some sort

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.