InDesign: フレームグリッドの文字数と行数をget/set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given no arguments, returns array [charsPerLine, linesPerColumn] of the | |
// text frame. To set the text frame grid dimensions to a different size, | |
// supply an argument in the same array format. Value of 0 will make no changes. | |
TextFrame.prototype.gridDimensions = | |
function gridDimensions(charLineArray /* optional */) { | |
var f = this; | |
var originalUnit = app.scriptPreferences.measurementUnit; | |
var g; | |
function frameIsVertical() { | |
return f.parentStory.storyPreferences.storyOrientation == | |
StoryHorizontalOrVertical.VERTICAL; | |
} | |
//// Main routine for GET | |
function getGridDimensions() { | |
var charsPerLine, linesPerColumn; | |
var fDimensions = getContextualDimensions(); | |
// Returns object containing per-column dimensions of the text frame, | |
// accounting for text orientation. | |
// .charSide = the side with characters going across/down | |
// .lineSide = the side with lines going across/down | |
function getContextualDimensions() { | |
var columnDimensions; | |
var charSideLength, lineSideLength; | |
// Returns object containing per-column dimensions (.height/.width), | |
// accounting for inset spacing and borders | |
function getColumnDimensions() { | |
var fBounds = f.geometricBounds; | |
var frameHeight = fBounds[2] - fBounds[0]; | |
var frameWidth = fBounds[3] - fBounds[1]; | |
var inset = f.textFramePreferences.insetSpacing; | |
var paddedHeight = inset[0] + inset[2]; | |
var paddedWidth = inset[1] + inset[3]; | |
return { | |
height: frameHeight - paddedHeight, | |
width: frameWidth - paddedWidth, | |
}; | |
} | |
function getCharSideLength(myLength) { | |
var columnCount = f.textFramePreferences.textColumnCount; | |
var columnGutter = f.textFramePreferences.textColumnGutter; | |
var totalGutter = (columnCount - 1) * columnGutter; | |
return (myLength - totalGutter) / columnCount; | |
} | |
columnDimensions = getColumnDimensions(); | |
if (frameIsVertical()) { | |
charSideLength = getCharSideLength(columnDimensions.height); | |
lineSideLength = columnDimensions.width; | |
} else { | |
charSideLength = getCharSideLength(columnDimensions.width); | |
lineSideLength = columnDimensions.height; | |
} | |
return { | |
charSide: charSideLength, | |
lineSide: lineSideLength, | |
}; | |
} | |
function calculateCharsOrLines(myLength, myAki) { | |
var charSize = g.pointSize; | |
var myCount; | |
myAki = myAki; | |
myCount = (myLength - charSize) / (charSize + myAki) + 1; | |
return myCount.toFixed(0); | |
} | |
charsPerLine = | |
calculateCharsOrLines(fDimensions.charSide, g.characterAki); | |
linesPerColumn = | |
calculateCharsOrLines(fDimensions.lineSide, g.lineAki); | |
return [charsPerLine, linesPerColumn]; | |
} | |
//// Main routine for SET | |
function setGridDimensions() { | |
var currentGridDimensions; | |
var charOffset, lineOffset; | |
var ptOffsetArray; | |
// Check that argument is an array of two integers | |
function checkArgument() { | |
function areIntegers() { | |
for (var i = 0; i < 2; i++) { | |
if (charLineArray[i] !== Math.round(charLineArray[i])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
if (!(charLineArray instanceof Array) || | |
charLineArray.length != 2 || | |
areIntegers() == false) { | |
throw new Error(arguments.callee.name + "() expects an array of " + | |
"integers in the form [charsPerLine, linesPerColumn].") | |
} | |
} | |
function getGridDimensionOffset(arrayElementIndex) { | |
var i = arrayElementIndex; | |
if (charLineArray[i] == 0) { | |
return 0; | |
} else { | |
return charLineArray[i] - currentGridDimensions[i]; | |
} | |
} | |
function gridOffsetsToPts(charOffset, lineOffset) { | |
var columnCount = f.textFramePreferences.textColumnCount; | |
var charOffsetInPts = | |
(g.pointSize + g.characterAki) * charOffset * columnCount; | |
var lineOffsetInPts = | |
(g.pointSize + g.lineAki) * lineOffset; | |
return [charOffsetInPts, lineOffsetInPts]; | |
} | |
function applyPtOffsets(ptOffsetArray) { | |
if (frameIsVertical()) { | |
f.resize(BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS, | |
AnchorPoint.TOP_RIGHT_ANCHOR, | |
ResizeMethods.ADDING_CURRENT_DIMENSIONS_TO, | |
ptOffsetArray.reverse()); | |
} else { | |
f.resize(BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS, | |
AnchorPoint.TOP_LEFT_ANCHOR, | |
ResizeMethods.ADDING_CURRENT_DIMENSIONS_TO, | |
ptOffsetArray); | |
} | |
} | |
checkArgument(); | |
// Get offsets of the requested gridDimens to the current gridDimens | |
currentGridDimensions = f.gridDimensions(); | |
charOffset = getGridDimensionOffset(0); | |
lineOffset = getGridDimensionOffset(1); | |
// Convert grid offsets to point values and apply them to the text frame | |
ptOffsetArray = gridOffsetsToPts(charOffset, lineOffset); | |
applyPtOffsets(ptOffsetArray); | |
} | |
// Check that this is a text frame grid | |
if (f.parentStory.storyPreferences.frameType != FrameTypes.FRAME_GRID_TYPE) { | |
alert("Error:\r" + arguments.callee.name + | |
"() can only be called on text frame grids."); | |
exit(); | |
} | |
try { | |
// Temporarily change to points | |
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS; | |
g = f.parentStory.gridData; | |
if (arguments.length == 0) { | |
return getGridDimensions(); | |
} else { | |
return setGridDimensions(); | |
} | |
} catch (e) { | |
alert(e + "\nLine: " + e.line); | |
} finally { | |
app.scriptPreferences.measurementUnit = originalUnit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment