Yes, it's one of the long lasting issues the Javascript API, basically Photoshop fails to apply transformations to the values. When you resize things you can have a transform either at the doc level or at the layer level. Also I think there was separately some issue in different behaviour of different versions with unit values. I.e. if you assign a number to .size some versions take it as pixel value and some as a point value. More problematically this behavior varies by CC version and win/mac. Here's the code I have on the topic, you have to basically retest every version (reminds I've not tested the latest).
_getTextScale: function (direction) {
var ref = new ActionReference()
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") )
var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey'))
//Photoshop._debugActionKeys(desc)
if (desc.hasKey(stringIDToTypeID('transform'))) {
var transform = desc.getObjectValue(stringIDToTypeID('transform'))
//Photoshop._debugActionKeys(transform)
var mFactor = transform.getUnitDoubleValue (stringIDToTypeID(direction))
return mFactor
}
return 1
},
getTextScaleVertical: function () {
return Photoshop._getTextScale('yy')
},
getTextScaleHorizontal: function () {
return Photoshop._getTextScale('xx')
},
getTextExtents: function (layer) {
if (layer && layer.kind == LayerKind.TEXT) {
app.activeDocument.activeLayer = layer
/*
var ext_2015v2 = Photoshop._getTextExtents2015v2(layer)
var ext_2015v1 = Photoshop._getTextExtents2015v1(layer)
var ext_2014v2 = Photoshop._getTextExtents2014v2(layer)
var ext_2014v1 = Photoshop._getTextExtents2014v1(layer)
*/
// best results @ 2015.03.17
if ((Photoshop.isWindows() && (Photoshop.isCC2014() || Photoshop.isCC2015())) ||
(Photoshop.isMac() && Photoshop.isCC2012())) {
return Photoshop._getTextExtents2014v2(layer) // better choice
} else {
return Photoshop._getTextExtents2015v1(layer) // backup choice
}
}
},
_getTextExtents2015v2: function (layer) {
var text_item = layer.textItem
var ref = new ActionReference()
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") )
var action = executeActionGet(ref)
//Photoshop._debugActionKeys(action)
var textKey = action.getObjectValue(stringIDToTypeID('textKey'))
var bounds = textKey.getObjectValue(stringIDToTypeID('bounds'))
var width = bounds.getUnitDoubleValue (stringIDToTypeID('right'))
var height = bounds.getUnitDoubleValue (stringIDToTypeID('bottom'))
var x_scale = 1
var y_scale = 1
return {
x: Math.round(text_item.position[0]),
y: Math.round(text_item.position[1]),
width: Math.round(width),
height: Math.round(height) }
},
// few pixels off due to rounding error (layer bounds integer, text item bounds float), but works in CS6 also
_getTextExtents2015v1: function(layer) {
var text_item = layer.textItem
var ref = new ActionReference()
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") )
var action = executeActionGet(ref)
var textKey = action.getObjectValue(stringIDToTypeID('textKey'))
//Photoshop._debugActionKeys(action)
// calculate the ratio of bounding box sizes reported for the layer and text item and is at scale
var text_bounding_box = textKey.getObjectValue(stringIDToTypeID('boundingBox'))
var text_bounding_width = Math.round(text_bounding_box.getUnitDoubleValue (stringIDToTypeID('right'))-text_bounding_box.getUnitDoubleValue (stringIDToTypeID('left')))
var text_bounding_height = Math.round(text_bounding_box.getUnitDoubleValue (stringIDToTypeID('bottom'))-text_bounding_box.getUnitDoubleValue (stringIDToTypeID('top')))
var x_scale = Photoshop.getLayerWidth(layer) / text_bounding_width
var y_scale = Photoshop.getLayerHeight(layer) / text_bounding_height
var width = text_item.width.as("px")
var height = text_item.height.as("px")
return {
x: Math.round(text_item.position[0]),
y: Math.round(text_item.position[1]),
width: Math.round(width*x_scale),
height: Math.round(height*y_scale)
}
},
// gives pixel perfect results when working
_getTextExtents2014v2: function (layer) {
var text_item = layer.textItem
var ref = new ActionReference()
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") )
var action = executeActionGet(ref)
//Photoshop._debugActionKeys(action)
var textKey = action.getObjectValue(stringIDToTypeID('textKey'))
var bounds = textKey.getObjectValue(stringIDToTypeID('bounds'))
var width = bounds.getUnitDoubleValue (stringIDToTypeID('right'))
var height = bounds.getUnitDoubleValue (stringIDToTypeID('bottom'))
var x_scale = 1
var y_scale = 1
if (textKey.hasKey(stringIDToTypeID('transform'))) {
var transform = textKey.getObjectValue(stringIDToTypeID('transform'))
x_scale = transform.getUnitDoubleValue (stringIDToTypeID('xx'))
y_scale = transform.getUnitDoubleValue (stringIDToTypeID('yy'))
}
return {
x: Math.round(text_item.position[0]),
y: Math.round(text_item.position[1]),
width: Math.round(width*x_scale),
height: Math.round(height*y_scale) }
},
_getTextExtents2014v1: function(layer) {
var text_item = layer.textItem
var ref = new ActionReference()
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") )
var action = executeActionGet(ref)
var textKey = action.getObjectValue(stringIDToTypeID('textKey'))
//Photoshop._debugActionKeys(textKey)
var bounds = textKey.getObjectValue(stringIDToTypeID('bounds'))
var width = bounds.getUnitDoubleValue (stringIDToTypeID('right'))
var height = bounds.getUnitDoubleValue (stringIDToTypeID('bottom'))
var x_scale = 1
var y_scale = 1
if (textKey.hasKey(stringIDToTypeID('transform'))) {
var transform = textKey.getObjectValue(stringIDToTypeID('transform'))
x_scale = transform.getUnitDoubleValue (stringIDToTypeID('xx'))
y_scale = transform.getUnitDoubleValue (stringIDToTypeID('yy'))
}
x_scale *= width / text_item.width.as("px")
y_scale *= height / text_item.height.as("px")
return {
x: Math.round(text_item.position[0]),
y: Math.round(text_item.position[1]),
width: Math.round(width*x_scale),
height: Math.round(height*y_scale)
}
},
getTextSize: function (layer) {
if (layer && layer.kind == LayerKind.TEXT) {
app.activeDocument.activeLayer = layer
var text_item = layer.textItem
var pixels = text_item.size.as("px")
var scale = Photoshop.getTextScaleVertical()
return Math.round(pixels * scale) // .size is a unit value and we want to round it first to a regular number
}
},
setTextSize: function (layer, size) {
if (layer && layer.kind == LayerKind.TEXT && size) {
app.activeDocument.activeLayer = layer
var text_item = layer.textItem
var scale = Photoshop.getTextScaleVertical()
var unit_val = new UnitValue(size/scale+"px")
text_item.size = unit_val
}
},
... View more