
Reece Dunn wrote:
Andy Little wrote:
Despite my previous remarks regarding automatic conversions, after reviewing what I have done on this previously I now conjecture that the two types of space (device space and logical space) have very different properties and their types cannot be directly compared against each other eg using operator < without some subtleties ( I now remember this is the reason that I havent completed a 'pixel' type). There needs to be a device available, to provide the 'context' of pixels_per_inch() or whatever, however, by changing the device mode( screen resolution) or the device itself( display <--> printer)) ,the comparison of two constants, one of each type, may give different results at different times. I am not sure that is a wise move! (I have always ended up using physical units for very much the above reasons and contrive to keep device units as a low level implementation detail.. It is a major reason that I ended up looking for and eventually writing a physical quantities library, to solve the problem once and for all! )
Hmm. This is a complex issue! It makes sense to have some sort of metric_type that supports pixels, percentages, picas/points, millimeters and inches. As you hint at, these need a device to resolve or convert.
In general, you will only want to manipulate a value using one specific metric type (e.g. specifying a font in points). You only really need to perform conversions when evaluating a value (e.g. when drawing a line from two coordinates). However, there may be a need for conversions in other areas, e.g. when getting the size that a string will take when rendered to a device using a specific font, you may want this to be in inches.
Taking a look at the CSS3 module on values and units (http://www.w3.org/TR/2001/WD-css3-values-20010713/), we have the following unit types: px (pixels) -- pixel units relative to the resolution of the viewing device. CSS3 gives a recommendation for rescaling to various devices (e.g. printer). in (inches) mm (millimeters) cm (centimeters) pt (points) pc (picas) % (percentage) sf (scalefactor) -- a multiplier for the size value em -- 'font-size' of the relevant font ex -- x-height of the relevant font Here, the CSS3 units em and ex are not really relevant because we have no font property to use to calculate the relative em/ex values. Looking at the values, it is possible to split them into 3 groups: * pixels -- device-specific unit size * inches -- logical unit size * scalefactor -- relative unit size All values are reducible to one of the three groups: pixels: 1px = 1px inches: 1in = 25.4mm = 2.54cm = 72pt = 6pc using the relationships: 1in = 2.54cm 1cm = 10mm 1pt = 1/72in 1pc = 12pt scalefactor: 1sf = 100% Now the problem is conversion between pixels, inches and scalefactor. scalefactor <--> pixels: required = max px sf --> px: px = max * sf px --> sf: sf = px / max scalefactor <--> inches required = max in sf --> in: in = max * sf in --> sf: sf = in / max pixels <--> inches required = pixels-per-inch (ppi) px --> in: in = px / ppi in --> px: px = in * ppi The above max px and max in can be reduced to max in since it is possible to convert max in --> max px, Thus we have: metric convert_to ( metric val, metric::type to, float max, float ppi ); Thoughts? Comments? Regards, Reece