Wednesday 7 February 2007
Units Patch Landed
Eli Friedman has landed his "units patch" (bug 177805).
This was a monumental effort spanning two years from posting the first patch to finally landing. The original design was proposed (by me, as it happens) back in 2002. Congratulations to Eli for pushing this through to resolution.
This patch is a major cleanup of the way we work with length units in Gecko. The new design is very clean and gives us some important new capabilities. The design revolves around CSS pixels. Think of a CSS pixel as 1/96 of an inch, rounded to the nearest device pixel, assuming the user is looking at a typical desktop screen. It's an abstract measurement that we use to make sure Web sites using measurements in "px" look reasonable. In particular, if the user has a (say) 200dpi screen, we don't want to set one CSS pixel to be one device pixel (as we currently do), because Web pages will simply look too small.
The new design focuses on three clearly defined quantities:
- The number of "application units" per CSS pixel. We make the "application units" we use for internal layout smaller than CSS pixels because we want to support subpixel positioning of elements. This value is device-independent and currently set to 60.
- The number of device pixels per CSS pixel. This is device dependent and depends on the device DPI. For devices up to 144 DPI, the value is 1. For 144 up to 240 DPI, the value is 2, and so on, increasing by 1 every 96 DPI. This means that effectively on a 144-240 DPI device, everything is specified by CSS pixels is scaled up by a factor of 2.
- The number of device pixels per inch. This is just the device DPI. This value is used to convert CSS length-based measurements (e.g., CSS "in" units) to device pixels and then to application units.
The net results are that if you have a high DPI screen, trunk builds will be scaling everything up by a factor of two or more, including Firefox UI. This is basically a good thing except that for now, there are some bugs that make it hard to use. If you can't bear it, you can use about:config to set the preference "layout.css.dpi" to something like 100 to turn the scaling off. (For a good time, you can use this preference to test the scaling feature if you have only a regular screen.)
Also, whatever your DPI, we now support much better matching of CSS physical length values to your actual device. If you draw a 1in x 1in box on the screen, it should really be a square inch if your system DPI is set correctly or you set the preference in Firefox. Up until now it has been a rather poor approximation.
Another benefit of this patch, by the way, is that scaling in printing and print preview has been reimplemented so that you can use any scale factor you like. Up until now only we've only been able to scale at certain mysterious predefined ratios.
This is not the end of the story about scaling, however. User-controlled zooming into Web pages is a separate feature that we will implement for Firefox 3.
For people working with Gecko layout code, this means that the old pixels-to-twips (and back again) conversions have been replaced with new conversion functions in nsPresContext, that convert between application units, CSS pixels and device pixels. Whenever you work with pixels you need to know which sort of pixels you're dealing with. In lower level code without access to an nsPresContext, you can get similar information from an nsIDeviceContext. Note that because the application units to CSS pixels ratio is device independent, you can access it via static methods of nsPresContext and nsIDeviceContext.
Comments
And when are we getting the ability to set the angle of a DIV? I want my text to be like on Tony Hawks, all at slightly different angles between -5' and 5'
Cheers,
monk.e.boy
You can rotate DIVs today (in trunk builds) using SVG and foreignobject.
Will Firefox 3 include higher-resolution icon toolbar button icons?
What syntax does Webkit use? In bug 177805 I suggested including the device-to-CSS-pixel ratio in the useragent string so that servers can send higher-res images. Another alternative would be a new HTTP header for image loads.
- the new background-size css property
- the ::marker pseudo element
- media queries for requesting a high res stylesheet
More info:
http://webkit.org/blog/?p=55
BTW David Baron was working on media queries in bug 156716. Would be nice to have that in Gecko 1.9 (parity with Opera and Webkit)
On a high-dpi desktop display, it makes sense to draw things bigger (pixel-wise) so that they don't look abnormally small.
But on a mobile device, part of the advantage of a high-dpi display is that you can squeeze more into a smaller space. For example, my Nokia N800 has 800x480 screen with a ~4" diagonal, which works out to 225dpi.
I suppose it would be easy to just lie to Firefox about the real dpi of the display, although I wonder if that's always what a user would want.
Thanks to Araeon for putting a minefield on his machine.
I have had terrible trouble with DPI in firefox after switching from Windows to Linux. IE seems to respect the DPI setting and Firefox not. Most sites in Firefox are displayed too small on my laptop, using only half the screen.
So I've downloaded both the 2.0.0.4 latest and the 3.0 alpha nightly after reading about this update, and I was disappointed. the 2.0.0.4 doesn't seem to do anything different. The 3 alpha displays at one resolution for DPI numbers up to 143 and then flips to another resolution for DPI numbers 144 and above. The first is too small and the second is too big.
Am I missing something?
Btw, my screen is a 15.4" UXGA(1920x1200)