Copy link to clipboard
Copied
Hi,
I am trying to read the values off some text boxes in photoshop for an exporter script that I am working on. I ran into an issue that I can't figure out, hopefully it's just a small thing.
When reading the values from paragraph text, there are some instances in which the values will come out wrong. For example:
I have a layer with paragraph text in it that says its paragraph size (text box) is 400 px by 400 px and the font size is 60 pt.
If I run a script similar to this:
// get the text layer and save ir to a variable named layer //
if (layer.textItem.kind == TextType.PARAGRAPHTEXT)
{
log("Width: " + layer.textItem.width.as('px'));
log("Height: " + layer.textItem.height.as('px'));
log("Font size: " + layer.textItem.size.as('pt'));
}
My output reads:
Width: 200
Height: 200
Font size: 30
Which is obviously wrong because all the values are half of what they should be. I have other paragraph text layers in the same file that print out the correct values!
After trying a bunch of different things I figured a way to reproduce the error, but not a way to correct it, here it is:
- Create a new paragraph text layer by clicking and dragging using the text tool, make the text box roughly 200 px by 200 px
- Type some text in the box
- Select the text and make it 30 pt in size
- Finish editing the text
- Make sure the text layer is selected
- Press command+t (ctrl+t in windows)
- In the free transform parameters, manually type 200% for both with and height
- Accept the transformation changes
- Use the text tool and select the text from the layer
At this point you will notice that the text box reports its size as 400 px by 400 px and the font size says 60 pt, so far everything makes sense, the issue comes when you look at the info tab, the scale is reported as 200% for both width and height and if I use the previous script the sizes printed are the original sizes of the text box: 200 px by 200 px and 30 pt for the font size.
At first I though it would be as easy as using the verticalScale and horizontalScale properties of TextItem like so:
log("Vertical scale: " + layer.textItem.verticalScale);
log("Horizontal scale: " + layer.textItem.horizontalScale);
But even though we know the scale is 200% the output reads:
Vertical scale: 100
Horizontal scale: 100
Unfortunatelly I do not have control over how the content is created so I can't avoid the use of the transform tool on pararaph text, or all text for that matter (this issue also affects the font size in single line text layers). So I need to find a reliable way to either read the correct values from the text or to read the scale which is affecting the text within my layer.
Any help would be appreciated. Thank you in advance.
Dario Segura
The font size shown by the DOM is the original size of the font used, if the text has been transform it needs to multiplied by the transform factor.
As an example create a text layer and transform the text then run the following code on the text layer...
...var domTextSize = activeDocument.activeLayer.textItem.size;
var amTextSize = getTextSize();
alert("DOM text size = " + domTextSize +"\rAction Manager text size = "+amTextSize);function getTextSize(){
var ref = new ActionReference();
ref.putEnumerated(
Copy link to clipboard
Copied
The font size shown by the DOM is the original size of the font used, if the text has been transform it needs to multiplied by the transform factor.
As an example create a text layer and transform the text then run the following code on the text layer...
var domTextSize = activeDocument.activeLayer.textItem.size;
var amTextSize = getTextSize();
alert("DOM text size = " + domTextSize +"\rAction Manager text size = "+amTextSize);function getTextSize(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey'));
var textSize = desc.getList(stringIDToTypeID('textStyleRange')).getObjectValue(0).getObjectValue(stringIDToTypeID('textStyle')).getDouble (stringIDToTypeID('size'));
if (desc.hasKey(stringIDToTypeID('transform'))) {
var mFactor = desc.getObjectValue(stringIDToTypeID('transform')).getUnitDoubleValue (stringIDToTypeID("yy") );
textSize = (textSize* mFactor).toFixed(2);
}
return textSize;
}
Copy link to clipboard
Copied
Thank you so much, this solves my issues!
Copy link to clipboard
Copied
Hi Paul. How do I use this on a text layer? Do I have to make & import a script? I've been looking for a fix to this type resize bug and this is the only response that gives me any hope.
Copy link to clipboard
Copied
If you want to set the text size you would do something like this...
var wantedTextSize = 50;
app.activeDocument.activeLayer.textItem.size= (Number(wantedTextSize)/Number(getTextdivideFactor()));
function getTextdivideFactor(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey'));
var textSize = desc.getList(stringIDToTypeID('textStyleRange')).getObjectValue(0).getObjectValue(stringIDToTypeID('textStyle')).getDouble (stringIDToTypeID('size'));
if (desc.hasKey(stringIDToTypeID('transform'))) {
var mFactor = desc.getObjectValue(stringIDToTypeID('transform')).getUnitDoubleValue (stringIDToTypeID("yy") );
return mFactor;
}
return 1;
};
N.B. This will only be valid with CS6 as it stands, if Adobe fixs the bug it will give the wrong results!
Copy link to clipboard
Copied
I can confirm that both issue and solution (Thanks Paul!) seem to be relevant also in Photoshop CC and CS6 Extended. Not understanding the Action-side of things, a few clarifications/questions
Copy link to clipboard
Copied
Hmm, as usual, delving deeper just reveals more issues..
At least with this PSD the solution above does not give correct answer. The textbox width is 722px and calculating like above will give you width of 548px. The transform descriptor gives horizontal&vertical scale of 1.6, which corresponds to scaling the image from 480px to 768px. But in addition to that there is some additional transform of ~1.3 done to the layer that is not counted. I've checked the other keys in the transform descriptor and they are all 0's. Any ideas?
Link to example: Dropbox - text_box_extents.psd
Copy link to clipboard
Copied
After a few more experiments, you can get correct values from the 'bounds'-descriptor.
function getTextExtents(text_item) {
app.activeDocument.activeLayer = text_item.parent
var ref = new ActionReference()
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") )
var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('textKey'))
var bounds = desc.getObjectValue(stringIDToTypeID('bounds'))
var width = bounds.getUnitDoubleValue (stringIDToTypeID('right'))
var height = bounds.getUnitDoubleValue (stringIDToTypeID('bottom'))
var x_scale = 1
var y_scale = 1
if (desc.hasKey(stringIDToTypeID('transform'))) {
var transform = desc.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) }
}
I tested that this returns correct values both when image and text box get scaled. There is some strange Photoshop behaviour to note though:
Copy link to clipboard
Copied
The 'Number' being what exactly or where would you take it from ?
EDIT: Nvm, figured it out. But it doesn't work. Read a lot of people having this issue. They should really make the font layer smart. If you resize it vertically, by means of transform, it should change the vertical scale as well, same goes for horizontal, or font size if you use constrained resizing (with shift pressed).
I'm so disappointed. Year after year, Adobe's products get progressively worse and dumber.