how to get exact x & y coordinates for the text inside the textframe
Hii Team,
We are using adobe extendScript to get the json data by parsing indesign file. We are trying to implement some logic based on exact text coordinates, we tried several options using font size , line height , offsets , but we are unable to get exact starting point of the text for each line in a textframe .
[P.S. - we are getting text frames coordinates, we need the coords for actual text inside].
One more consideration is the text alignment , both vertically and horizontally.
Currently this is what we have implemented yet, but it is not working as expected, (mostly for Y -coordinate)
function getXCoordinateForText(paragraph, bounds) {
var frameLeft = bounds[1]; // X position of the left edge of the frame
var frameRight = bounds[3]; // X position of the right edge of the frame
var frameCenter = (frameLeft + frameRight) / 2; // Center of the frame
// Get text properties
var startX = paragraph.horizontalOffset;
var endX = paragraph.horizontalOffset;
try {
endX = paragraph.endHorizontalOffset;
} catch (error) {
logInfo("Error while accessesing endHorizontalOffset (this issue is due to text being not properly formatted: \n "+error ,4121)
return 0;
}
var textWidth = endX - startX;
// Get paragraph alignment
var alignment = paragraph.justification;
var textX; // Final x-coordinate based on alignment
// Adjust x-coordinate based on alignment
if (alignment == Justification.LEFT_ALIGN) {
textX = startX; // Text starts at startX
} else if (alignment == Justification.RIGHT_ALIGN) {
textX = frameRight - textWidth; // Align text to the right edge
} else if (alignment == Justification.CENTER_ALIGN) {
textX = frameCenter - (textWidth / 2); // Center text in the frame
} else if (alignment == Justification.FULL_JUSTIFY || alignment == Justification.LEFT_JUSTIFIED) {
textX = startX; // Justified text starts at startX, spacing varies
}
return textX - frameLeft;
}
function getYcoordinateForText(textFrame, bounds) {
// y coordinate of text lines
// var y_offset = lineSpacing * font_size_in_points
// vAlign = alignmentAnchor.toString() //top/middle/bottom
// // numLines = <get number of lines after splitting the text with \n>
// var visibleLineCount = Math.floor(containerHeight / y_offset)
// var vShift = 0;
// switch (vAlign) {
// case "middle":
// case 'CENTER_ALIGN':
// vShift = (containerHeight - visibleLineCount * y_offset) / 2;
// break;
// case "bottom":
// vShift = containerHeight - visibleLineCount * y_offset
// break;
// default:
// vShift = 0;
// break;
// }
// var y_coordinate_of_a_line = vShift + ((line_index + 1) * (y_offset));
// return y_coordinate_of_a_line;
//Changed the logic of calculating y coordinate for the text frames
var paragraph = textFrame.paragraphs[0]; // Get the first paragraph
// Get text frame boundaries
var bounds = textFrame.geometricBounds;
var frameTop = bounds[0]; // Y position of the top edge of the frame
var frameBottom = bounds[2]; // Y position of the bottom edge of the frame
var frameCenter = (frameTop + frameBottom) / 2; // Center of the frame
// Get baseline position of the first line of text
var baselineY = paragraph.lines[0].baseline;
// Get paragraph alignment
var alignment = paragraph.justification;
var textY =baselineY; // Final y-coordinate based on alignment , default first lime's baseline position
// Adjust y-coordinate based on alignment
if (alignment == VerticalJustification.TOP_ALIGN) {
textY = baselineY; // Text starts at the first line's baseline
} else if (alignment == VerticalJustification.BOTTOM_ALIGN) {
textHeight = paragraph.lines[paragraph.lines.length-1].baseline - paragraph.lines[0].baseline; // Total text height
textY = frameBottom - textHeight; // Align text to the bottom edge
} else if (alignment == VerticalJustification.CENTER_ALIGN) {
textHeight = paragraph.lines[paragraph.lines.length-1].baseline - paragraph.lines[0].baseline;
textY = frameCenter - (textHeight / 2); // Center text vertically
} else if (alignment == VerticalJustification.JUSTIFY_ALIGN) {
textY = baselineY; // Justified text starts at baseline, but line spacing varies
}
return textY - frameTop; // actaul distance of the text from the text- frame's top
}We are attaching the the screenshot here , what we want to achieve. can anybody please help us on this one.
