
Originally Posted by
Wedgy
I wonder if in this case it can be achieved in Flash or Python or something
Java should certainly be able to, and I'd guess that Flash could.
It seems fantastic (in the negative sense) that no one has implemented this in Javascript or at least faked it.
However, you seem to think that it's relatively trivial. As far as I can see, it's not feasible with client-side scripting.
Several things need to be calculated. Depending on whether the column widths or number of columns is predefined, the order of these calculations will need to changed. I'll take the former as an example.
It's obviously necessary to know how many columns we can fit inside an element (set column numbers would mean calculating the column width), so the first necessary bit of data is how wide is the element? Browsers that properly support the W3C DOM 2 Style module can return that information using the getComputedStyle method. Other browsers can use proprietary properties like clientWidth. If neither is available, the text will just have to be displayed normally (similarly with later calculations).
Next, the number of columns, using the formula:
Code:
total width ≥ column width × columns + column spacing × (columns − 1)
columns = floor((total width − column width) ÷ (column width + column spacing) + 1)
In order to get complete lines in each column, knowing the line height is important. Again, the getComputedStyle method can get this directly. Other browsers will need to jump through hoops, but it's also possible for some of them as well.
It's at this stage where things become rather more difficult. To split the text between the columns as evenly as possible, the number of lines in the text needs to be known (at the calculated column width). The only way I can think of doing that is to compute the height of block level element - similar to the width, previously - fixed to the column width. Dividing this height by the computed line height will yield the number of lines. Next, these lines need to be distributed between the columns, so the total number of lines is divided by the number of columns, rounding the result up to the nearest whole number. The problem is then working out where, in the text, the column breaks will appear.
When working with a GUI toolkit, or directly with the operating system, a complete set of font metric and line wrapping functions should be provided. None of that exists for client-side scripts. The nearest thing would be to use our fixed-width, block-level element again. Words would be added one-by-one until the computed height is greater than the line height (signalling that the word just added created a second line), or until you've reached the required number of lines. However, this method would be incredibly inefficient. Granted, the operating system is likely to compute line lengths for variable-width type faces on a character basis, summing the extent of each character, but at least that will be implemented in highly optimised, native code. I wouldn't predict anything near that performance for a script.
A further complication is changing the font size after rendering. An applet would provide its own controls for such an operation, so triggering a change could simultaneously repaint the columns. However, a script won't receive a similar notification. Another is that resizing the viewport would also require recalculating, and repainting, the columns.
Mike
Bookmarks