It very much depends on where, when and how (and why?) you are trying to determine this information.

For example. If you are trying to do this within a windows application, that will be writing to the local screen using a pre-selected font, there are a whole raft of entrypoints for dealing with fonts, querying the metrics, and determining the bounds. Which calls you would need to use is very much dependant upon upon what you are trying to achieve.

Other graphical APIs, Tk, Gtk, X etc. will all (probably) have equivalent sets of APIs for doing this.

However, if you are working on a CGI/HTML application and are hoping to work out how many pixels of screen real estate any given string will occupy, forget it it and give up now:)

Whatever you do at the server end to try and determine this is doomed to fail. Even if you specify the exact font and size, each browser may choose to render that font and size with more or less inter-character and inter-word spacing, larger or smaller margins and use (or not) kerning, all of which will completely derail your server-side calculations.

If the fontsize you specify is not available, then the browser may choose to synthesize it from a size that is available, but it is unlikely (if not impossible) that it will reach a similar set of metrics to those you have used in your calculations.

If the font you specify is not available, it will substitute a "similar font", and again you are screwed.

Finally, most if not all browsers allow the user to override the fonts specified by the author and substitute larger, smaller or completely different fonts. I use this facility all the time, opting for small fonts that pack as much text onto my screen as possible. Partially sighted people will tend to opt for larger fonts.

If this is your target, then not only is it impossible to do, you should probably be asking yourself why you want to do it.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!


In reply to Re: Font size by pixels. by BrowserUk
in thread Font size by pixels. by the_0ne

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.