ExtendScript textItem.width bug

ExtendScript has some issues reporting the correct width and height of textItems when the document dpi is something other than 72. Actually it's not just width and height font-size is affected too.

example script output

Here a small example script that highlights the issue. A possible solution seems to be to divide the document dpi by 72 and multiply the affected values with this factor.

#target photoshop

// example script to highlight text size issues with a document dpi other then 72

var defaultRulerUnits = app.preferences.rulerUnits;  
app.preferences.rulerUnits = Units.PIXELS;  
app.preferences.typeUnits = TypeUnits.PIXELS;

var cGrey = new SolidColor();  
cGrey.rgb.red = 60;  
cGrey.rgb.green = 60;  
cGrey.rgb.blue = 60;

var cWhite = new SolidColor();  
cWhite.rgb.red = 255;  
cWhite.rgb.green = 255;  
cWhite.rgb.blue = 255;

var cYellowGrey = new SolidColor();  
cYellowGrey.rgb.red = 80;  
cYellowGrey.rgb.green = 80;  
cYellowGrey.rgb.blue = 50;


var docRef = app.documents.add(800,600,72.0,"TestCase",NewDocumentMode.RGB, DocumentFill.TRANSPARENT);

generateTestDoc()

changeDocumentDPI(96);  
var layerA = docRef.layers.getByName("250px");  
var layerB = docRef.layers.getByName("450px");

var factor = 96.0/72.0;

var msg = "96dpi:\ntextItem.width:\nParagraph 1: "+layerA.textItem.width+"\nParagraph 2: "+layerB.textItem.width;  
  msg += "\n\n96dpi with factor 96.0/72.0:\ntextItem.width:\nParagraph 1: "+layerA.textItem.width*factor+"\nParagraph 2: "+layerB.textItem.width*factor;
alert(msg);

changeDocumentDPI(72);

msg = "72dpi:\ntextItem.width:\nParagraph 1: "+layerA.textItem.width+"\nParagraph 2: "+layerB.textItem.width;  
alert(msg);


function changeDocumentDPI(dpi) {  
  var dialogMode = DialogModes.NO;
  var desc1 = new ActionDescriptor();
  desc1.putUnitDouble(app.charIDToTypeID('Rslt'), app.charIDToTypeID('#Rsl'), dpi);
  executeAction(app.stringIDToTypeID('imageSize'), desc1, dialogMode);
};

function createText(x, y, width, height, fontSize, text) {  
  var doc = app.activeDocument;
  var textLayer = doc.artLayers.add();
  textLayer.kind = LayerKind.TEXT;
  var txtRef = textLayer.textItem;
  txtRef.kind = TextType.PARAGRAPHTEXT;
  txtRef.antiAliasMethod = AntiAlias.SMOOTH;
  txtRef.size = fontSize;
  txtRef.width = width;
  txtRef.height = height;
  txtRef.color = cWhite;
  txtRef.contents = text;
  txtRef.position = new Array(x,y);
  return textLayer;
}

function drawBox(x,y,width,height,color){  
  var docRef = app.activeDocument;
  docRef.selection.select([[x,y],[x+width,y],[x+width,y+height],[x,y+height]],SelectionType.REPLACE,0,false);
  docRef.selection.fill(color);
  docRef.selection.deselect();
}

function generateTestDoc(){  
    var lyrBackground = docRef.artLayers.add();
    docRef.selection.select([[0,0],[0,600],[800,600],[800,0]]);
    docRef.selection.fill(cGrey);
    docRef.selection.deselect();

    var lyrText1 = createText(50,50,250,80,24,"Paragraph Text 1\rwidth: 250px");
  lyrText1.name = "250px";
    var lyrText2 = createText(50,150,450,100,32,"Paragraph Text 2\rwidth: 450px");
  lyrText2.name = "450px";

  docRef.activeLayer = lyrBackground;
  drawBox(50,50,250,80,cYellowGrey);
  drawBox(50,150,450,100,cYellowGrey);
}
comments powered by Disqus