Description
Hi there,
I found a few problems with b.bounds()
and have put together some improvements that could be made to the function.
Here we go:
1) Incorrect bounds on rotated frames
At the moment the results of b.bounds()
on rotated text frames are incorrect, as values like endBaseline are not taken into consideration. Fortunately properties like left
or right
are all relative positions (as opposed to x
and y
), so I would suggest to calculate the bounds in a way that rotates along the rotated word. That way we can always use a line like
var rect = b.rect(bounds.left, bounds.top, bounds.width, bounds.height);
to draw a rect around a text (if the matrix is still rotated), no matter if that text is rotated or upside down. I have already implemented this in a branch I am working on, and can report that it works. Keep in mind that this does not change the behavior for non-rotated frames at all, it only makes it work for rotated frames, too!
2) Text collections throw an error
Text collections like [Object Words]
until yesterday's commit threw the error
"b.bounds(), invalide type of parameter! Can't get bounds for this object."
as they were not recognized as a text object by basil. Now they are and b.bounds()
throws an error about the fact that baseline is not a property. Therefore we need to address those collections and pick their baseline
, endBaseline
from the objects contained in the collections.
3) Text object throws yet another error
Text objects (as in [Object Text]
) that are created by using the itemByRange()
method throw a different error. They do have a baseline
property, but for some reason this property is an array whose only value contains the actual baseline
value. Why on earth that is the case, I have no idea. But we need to address this type of object seperately yet again.
4) Calculate actual bounds of multiline text
At the moment something like
var tf = b.text(b.LOREM, 0, 0, 200, 200);
var bounds = b.bounds(tf.paragraphs.firstItem());
b.rect(bounds.left, bounds.top, bounds.width, bounds.height);
draws really incorrect bounds (see image). For multiline text the actual last line is not taken into consideration. Also the right edge of the paragraph is calculated by the last line when it should be calculated by the longest line. I suggest to treat every type of text that the actual bounds are calculated (in this for example by figuring out the longest line of the paragraph).
5) Smarter handling of disconnected
text parts
This one is tricky. At the moment on a line break basil throws the warning
warning("b.bounds(), not possible to get correct bounds, possible line break within textObj");
That's alright, but I would suggest to also introduce an boolean property like lineBreak
to the returned object, so people can start to handle those line breaks in any way they desire, as I find it impossible to predict what a user wants to do with a "line broken" word. Additionally I think we should check if there is a frame break, a page break and a spread break in the text, as these might all require different plans of action. So, b.bounds()
would return these flags as well.
I just had a student who was trying to connect words with lines between rotated frames cross page (sometimes) and it turned out to be really tricky. Those flags would have made his project a whole lot easier.
6) x-Height
Lastly for now, I want to address this comment in the source code
// TODO: not sure if this 100% correct, check
// http://en.wikipedia.org/wiki/File:Typography_Line_Terms.svg
var xHeight = y1 + descent;
Unfortunately this is not correct, as this effectively only subtracts the descent from the ascent. While this might result in something not far off from the actual x-height, the descent has nothing to do with the x-height and therefore could produce really wrong results. As the results are not reliable, I suggest, we take x-height out, as the user might be better off guessing the x-height or measuring it with creating outlines of an x.
I started implementing some of this stuff, but I would like to hear your thoughts, before I get to far into this.