HTMPrintMeasure or basics of HTML/CSS printing

March 31, 2007

Filed under: HTML and CSS,HTMLayout — Andrew @ 4:20 pm

PrintEx API of htmlayout has HTMPrintMeasure function with the following signature:

HPRESULT HTMPrintMeasure(HTMPRINT hPrint, HDC hdc,
            int scaledWidth,
            int viewportWidth,
            int viewportHeight,
            int* pOutNumberOfPages);

I would like to explain here meanings of its parameters as I am getting questions about printing and scaling pretty frequently.

First fact that we need to know: pixels in HTML and CSS are logical units and have nothing common with device pixels our HTML/CSS is rendered on.

By accident 1px in HTML normally mapped to one physical pixel on screen (but not always, sic!) and one "HTML pixel" on printer can be mapped to some rectangular area that covers multiple printer "pixels". Strictly speaking printers have no pixels.

Showing that more device pixels (dots)
are needed to cover a 1px by 1px area on a high-resolution device than
on a low-res one   [D]
(illustration taken from W3C CSS specification)

According to the specification, HTML pixels are logical units that are just fractions of real length units:

   1px (html,css) = 1in / 96

Thus pixel in HTML is very close by its nature to "point" – 1pt is 1/72nd of an inch.

And this is it about theory, back to our business – HTMPrintMeasure function and its parameters:

  • int scaledWidth

    – is a number of "HTML pixels" between left and right border of the page.

  • int viewportWidth

    – is a number of "printer physical pixels" or dots between left and right border of the page (a.k.a. physical page width).

  • int viewportHeight

    – is a number of "printer physical pixels" or dots between top and bottom border of the page (a.k.a. physical page height).

At this point of our "printing saga" we can ask the question "what are the correct values for these parameters?"

Let’s assume that we have given:

  1. printer that has 600 dpi (dots per inch) resolution.

  2. paper of the format "legal" – 8.5 by 11 inches.

Calculation then is pretty straightforward:

viewportWidth = 600 dpi * 8.5in = 5100 dots (or printer pixels if you wish)
viewportHeight = 600 dpi * 11in = 6600 dots.
scaledWidth = 8.5in * 96 (html px per inch) = 816

Therefore we have 816 "HTML pixels" on printing page of the legal/portrait format in horizontal direction.
This means that if you have <table width=800> or <img width=800> then they shall fit on such page in full (if printing margins were set to zero).

In reality things are a bit more complex – you need to take in account printing margins in calculations to get precise results. But this I hope will not be so difficult for people who knows, let’s say, C# programming language .

Try/catch/finally extravaganza #3

March 13, 2007

Filed under: Philosophy,Script — Andrew @ 12:55 pm

return in finally sections is a bad idea:

  function foo()
       try { return 1/0; }  catch(e) { return 2; } finally { return 3; }
    catch(e) { return 4; } finally { return 5; }

Probably return allowance in finally sections is a design flaw of JavaScript language itself?

Pessimistic challenge #2

March 10, 2007

Filed under: Philosophy,Script — Andrew @ 12:09 pm

And what answer will give us following function?

  function test()
    var flavour = "optimistic";
      return flavour = "pessimistic", "I am " + flavour + " optimist";
      return "I am " + flavour + " pessimist";

Test: who are you?

March 9, 2007

Filed under: Philosophy,Script — Andrew @ 2:49 pm

What will following JavaScript function return?

function test()
      return "I am optimist";
      return "I am pessimist";

Try to answer as this is a good chance to measure your attitude 🙂

W3C decided to reincarnate HTML development

March 8, 2007

Filed under: Philosophy — Andrew @ 10:41 am

W3C decided to reincarnate HTML development.

Phylosophical difference between HTML and XHTML can be demonstraed by the fact that tag <html> is optional in HTML and is not in XHTML. By definition of XML (XHTML):

Definition: A data object is an XML document if it is well-formed, as defined in this specification. In addition, the XML document is valid if it meets certain further constraints.

What means that if UA (user agent, browser) can deal with loading XHTML document only when it receives closing </html> and everything inside is valid. To be short: any partial markup is not XHTML by definition and can be interpretted as such.

New forum

March 5, 2007

Filed under: Philosophy — Andrew @ 10:06 pm

We have moved forum to the new location.

For many reasons we like bbpress engine more than PHPBB.

It is not possible to convert user accounts so please register yourself again.

Next Page »