Текст по центру скриптом
Как известно, Индизайн не может делать вертикальное выравнивание текста по центру в непрямоугольных фреймах, таких, например, как на рисунке ниже.
Dave Saunders написал скрипт, который решает эту проблему смещением первой базовой линии фрейма на нужную величину.
//DESCRIPTION: Pseudo Vertical Align Center function
if (app.documents.length == 0 || app.selection.length == 0) { exit() }
processSelection(app.selection);
function processSelection(sel) {
if (sel.length > 1) {
processMultiSelection(sel);
} else {
vjcenterTextFrame(locateTF(sel[0]));
}
function processMultiSelection(sel) {
for (var j = sel.length – 1; j >= 0; j–) {
if (sel[j].constructor.name == «TextFrame») {
vjcenterTextFrame(sel[j]);
}
}
}
function vjcenterTextFrame(tf) {
if (tf.textFramePreferences.textColumnCount > 1) {
// alert(«Multi-column text frames not supported»);
return
}
//save users measurement preferences
var userVert = app.documents[0].viewPreferences.verticalMeasurementUnits;
app.documents[0].viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
var prevError = Number.POSITIVE_INFINITY;
var frameCenter = getFrameCenter(tf);
while (Math.abs(prevError) > 0.25) {
var textCenter = getTextCenter(tf);
var error = frameCenter – textCenter;
if (oscillating(error, prevError)) { return }
try {
tf.textFramePreferences.minimumFirstBaselineOffset += error;
} catch (e) { return }
prevError = error;
}
app.documents[0].viewPreferences.verticalMeasurementUnits = userVert;
}
function oscillating(err, olderr) {
if (Math.abs(olderr) > Math.abs(err)) { return false }
if (err * olderr >= 0) { return false }
return true
}
function getTextCenter(tf) {
var topchar = tf.texts[0].characters[0];
var top = topchar.baseline – topchar.baselineShift – topchar.ascent;
var botline = tf.texts[0].lines[-1];
var bottom = botline.baseline + (botline.descent)*.66; // .66 derived empirically
return (top + bottom)/2;
}
function getFrameCenter(tf) {
var aBounds = tf.geometricBounds;
return (aBounds[0] + aBounds[2])/2
}
function locateTF(obj) {
if (obj.constructor.name == «TextFrame») { return obj }
try {
return obj.parentTextFrames[0];
} catch (e) {
alert(«Can’t locate text frame from selection.»);
}
}
}
Автор назвал этот скрипт PsevdoVJ-centered и предупреждает, что он не будет работать, если вы попытаетесь применить его для многоколоночного фрейма.
Поскольку в Индизайне также существует проблема выравнивания содержимого текстового фрейма по нижнему краю (опять же для непрямоугольных фреймов), то Dave Saunders следующим скриптом решает и эту задачу:
//DESCRIPTION: Pseudo Bottom align text frame(s)
if (app.documents.length == 0 || app.selection.length == 0) { exit() }
processSelection(app.selection);
function processSelection(sel) {
if (sel.length > 1) {
processMultiSelection(sel);
} else {
vjbottomTextFrame(locateTF(sel[0]));
}
function processMultiSelection(sel) {
for (var j = sel.length – 1; j >= 0; j–) {
if (sel[j].constructor.name == «TextFrame») {
vjbottomTextFrame(sel[j]);
}
}
}
function vjbottomTextFrame(tf) {
if (tf.textFramePreferences.textColumnCount > 1) {
// alert(«Multi-column text frames not supported»);
return
}
//save users measurement preferences
var userVert = app.documents[0].viewPreferences.verticalMeasurementUnits;
app.documents[0].viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
var frameBottom = getFrameBottom(tf);
tf.textFramePreferences.minimumFirstBaselineOffset = 0;
var lastChar = tf.texts[0].characters[-1].index;
var textBottom = getTextBottom(tf);
var error = frameBottom – textBottom;
tf.textFramePreferences.minimumFirstBaselineOffset += error;
while (lastChar – tf.texts[0].characters[-1].index > tf.texts[0].lines[-1].length) {
tf.textFramePreferences.minimumFirstBaselineOffset -= getLeading(tf.texts[0].lines[-1]);
}
while (lastChar > tf.texts[0].characters[-1].index) {
tf.textFramePreferences.minimumFirstBaselineOffset -= getLeading(tf.texts[0].lines[-1])/4;
}
while (tf.texts[0].characters[-1].index >= lastChar) {
tf.textFramePreferences.minimumFirstBaselineOffset += getLeading(tf.texts[0].lines[-1])/16;
}
while (lastChar > tf.texts[0].characters[-1].index) {
tf.textFramePreferences.minimumFirstBaselineOffset -= getLeading(tf.texts[0].lines[-1])/64;
}
while (tf.texts[0].characters[-1].index >= lastChar) {
tf.textFramePreferences.minimumFirstBaselineOffset += getLeading(tf.texts[0].lines[-1])/256;
}
while (lastChar > tf.texts[0].characters[-1].index) {
tf.textFramePreferences.minimumFirstBaselineOffset -= getLeading(tf.texts[0].lines[-1])/1024;
}
app.documents[0].viewPreferences.verticalMeasurementUnits = userVert;
}
function getLeading(text) {
if (text.leading == Leading.auto) {
return text.pointSize * text.autoLeading/100;
} else {
return text.leading
}
}
function oscillating(err, olderr) {
if (Math.abs(olderr) > Math.abs(err)) { return false }
if (err * olderr >= 0) { return false }
return true
}
function getTextBottom(tf) {
var botline = tf.texts[0].lines[-1];
var bottom = botline.baseline;
return bottom;
}
function getFrameBottom(tf) {
var aBounds = tf.geometricBounds;
return aBounds[2]
}
function locateTF(obj) {
if (obj.constructor.name == «TextFrame») { return obj }
try {
return obj.parentTextFrames[0];
} catch (e) {
alert(«Can’t locate text frame from selection.»);
}
}
}
Скрипт получил название PsevdoVJ-bottom.
Как обычно, скопируйте скрипты с этой страницы в простой текстовый редактор и сохраните в папку пользовательских шрифтов Индизайна с расширением jsx. Перед сохранением внимательно просмотрите скрипт, и если увидите кавычки-лапки, то поменяйте их на обычные двойные прямые кавычки.










Leave your response!
You must be logged in to post a comment.