Skip to main content
Participant
October 30, 2009
Question

How to get the coordinate of FlowLeafElement

  • October 30, 2009
  • 1 reply
  • 1393 views

Hi all.

Now, I'm developping ruby-function, which is not supported currently by TLF, but is supported by InDesign for Japanese.

(Note: I mean "ruby" as east-asian notation. Not programming language. About ruby, see wikipedia.)

To realize it, it's necessary to get the coordinate of FlowLeafElement like SpanElement or LinkElement.

I could have get a FlowLeafElement by method TextFlow.getElementById(id:str), but I don't know how to specify it's coordinate.

Does anyone have good idea?

I think FlowLeafElement.getComputedFontMetrics().emBox will help, but haven't tried.

If you have other approach to realize ruby-function as bi-linear notation, please share it.

Thanks in advance.

This topic has been closed for replies.

1 reply

Adobe Employee
November 2, 2009

A FlowLeafElement doesn't have a single coordinate but let's suppose you mean the x,y of the first character.  It's not as simple as it should be.  This isn't tested but it's close.

// must be composed

textFlow.flowComposer.updateAllContainer();

// any positiion in the TextFlow - this is the start of leafElement

var position:int = leafElement.getAbsoluteStart()

// get the TextLine

var textFlowLine:TextFlowLine = textFlow.flowComposer.findLineAtPosition(position);

// adjust position to be position the character in the line

position = position-textFlowLine.getAbsoluteStart()

var textLine:TextLine = textFlowLine.getTextLine(true);

// adjust to the proper atom - multiple positions can be in a single atom

position = textLine.getAtomIndexAtCharIndex(position);

// get the bounding box - use the x and y

var bbox:Rectangle = textLine.getAtomBounds(position);

The font metrics you mention below are just that font metrics - they are the same for every character with the same formats.

Hope that helps,

Richard

FumikitoAuthor
Participant
November 5, 2009

Thanks Richard!

Due to your advice, I made some sample app.

(I'm affraid that it isn't displayed correctly in case of japanese font not installed. But you can

view the source by right-click.)

As you mentioned, font metricks dosen't related.

//Get SpanElement from TextFlowElement and detect TextLine containing it.
var
span:SpanElement = SpanElement(flow.getElementByID("cite" + (j + 1).toString()));
var line:TextFlowLine = flow.flowComposer.findLineAtPosition(span.getAbsoluteStart());
                   
//Calculate the coordinate of ruby from TextLine's coordinate and font size.
var
rubyX:Number = line.getBounds().x;
var rubyY:Number = line.getBounds().y +  (textLayoutFormat.fontSize * (span.getAbsoluteStart() - line.absoluteStart));
                   
//Make TLFTextField object for ruby and place it at proper coordinate(This code need refactoring)
var
tlf:TLFTextField = new TLFTextField();
tlf.multiline = true;
tlf.setTextFormat(new TextFormat(fn,7)); //'fn' is fontName.
//To make TLFTextField vertical, insert linebreak between each letters. It's temporary.
var
arr:Array = new Array();
for(var k:int = 0,m:int = rubyContainer[j].length; k<m;k++){
   arr.push(rubyContainer[j].charAt(k));
}
var str:String = arr.join("\n");
tlf.text = str;
tlf.x = rubyX + ruby.width + textLayoutFormat.fontSize;
tlf.y = rubyY + textLayoutFormat.paddingTop;
ruby.addChild(tlf);

Anyway, fundamental logic is "Get coordinate from textline conatining FlowLeafElement and then calculate proper coordinate from TextLine's coordinate and font-size".

Thank you so much, Richard.

//------------------

If anyone has other idea, please share it.