Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found