Highlighted

Why does my text come out 4.17 times bigger than I ask for in my script?

Community Beginner ,
Oct 30, 2020

Copy link to clipboard

Copied

I have the following function in a script:

function CreateTextLayer(document, message, distanceFromLeft, distanceFromTop)
{
// make the layer;
var myLayerRef = myDocument.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = message;

var myTextRef = myLayerRef.textItem;
myTextRef.size = 14;
myTextRef.font = "Goudy Stout";
myTextRef.justification = Justification.LEFT;

//Set text colour in RGB values
var newColor = new SolidColor();
newColor.rgb.red = 0;
newColor.rgb.green = 0;
newColor.rgb.blue = 0;
myTextRef.color = newColor;

// Set the position of the text - percentages from left first, then from top.
myTextRef.position = new Array(distanceFromLeft, distanceFromTop);
myLayerRef.blendMode = BlendMode.NORMAL;
myLayerRef.opacity = 100;
myTextRef.contents = message;
}

 

When I select the resulting layer from my image, the size box on top tells me the font size is 58.33 px, or 4.17 times bigger than I asked for.  In this case, I didn't specify the units.  But in another script, the function is a bit different:

function CreateTextLayer(document, layerName, content, justification,
color, size, distanceFromLeft, distanceFromTop)
{
var textColor = color
if (typeof(color) == "undefined")
{
alert ("Creating black color");
textColor = new SolidColor();
textColor.rgb.red = 0;
textColor.rgb.green = 0;
textColor.rgb.blue = 0;
}

// make the layer;
var myLayerRef = document.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = layerName;

var myTextRef = myLayerRef.textItem;
// myTextRef.size = size;
myTextRef.size = new UnitValue(size, "px");
myTextRef.font = "Myriad Pro";
myTextRef.justification = justification;

//Set text colour
myTextRef.color = textColor;

// Set the position of the text - percentages from left first, then from top.
myTextRef.position = new Array(distanceFromLeft, distanceFromTop);
myLayerRef.blendMode = BlendMode.NORMAL;
myLayerRef.opacity = 100;
myTextRef.contents = content;
}

 

Here, I'm explicitly askng for the font size in pixels.  When I run this script and ask for text with a font size of 70 pixels, I get 291.67 pixels.  Again, 4.17 times the size I asked for.  

 

In case it matters, here's the code from the second example that calls this function:

doc = app.activeDocument;
doc.changeMode(ChangeMode.RGB);
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
doc.resizeImage (undefined, undefined, 300, ResampleMethod.NONE)

var gray = new SolidColor();
gray.rgb.hexValue = "505050";
var white = new SolidColor();
white.rgb.hexValue = "FFFFFF";

// Inner border eight pixels gray
app.backgroundColor = gray;
app.activeDocument.resizeCanvas(UnitValue(doc.width + 8,"px"),UnitValue(doc.height + 8,"px"));
// Inner frame six pixels white
app.backgroundColor = white;
app.activeDocument.resizeCanvas(UnitValue(doc.width + 6,"px"),UnitValue(doc.height + 6,"px"));
// outer frame 170 pixels gray
app.backgroundColor = gray;
app.activeDocument.resizeCanvas(UnitValue(doc.width + 120,"px"),UnitValue(doc.height + 120,"px"));
// Extend outer frame down by 170 pixels
app.activeDocument.resizeCanvas(UnitValue(doc.width,"px"),
UnitValue(doc.height + 120,"px"),
AnchorPosition.TOPCENTER);

// Add title
CreateTextLayer(doc, "Title", "This Is The Title of This Photo", Justification.LEFT,
undefined, 70, 0, doc.height);



 

 

TOPICS
Actions and scripting, Problem or error

Views

71

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Why does my text come out 4.17 times bigger than I ask for in my script?

Community Beginner ,
Oct 30, 2020

Copy link to clipboard

Copied

I have the following function in a script:

function CreateTextLayer(document, message, distanceFromLeft, distanceFromTop)
{
// make the layer;
var myLayerRef = myDocument.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = message;

var myTextRef = myLayerRef.textItem;
myTextRef.size = 14;
myTextRef.font = "Goudy Stout";
myTextRef.justification = Justification.LEFT;

//Set text colour in RGB values
var newColor = new SolidColor();
newColor.rgb.red = 0;
newColor.rgb.green = 0;
newColor.rgb.blue = 0;
myTextRef.color = newColor;

// Set the position of the text - percentages from left first, then from top.
myTextRef.position = new Array(distanceFromLeft, distanceFromTop);
myLayerRef.blendMode = BlendMode.NORMAL;
myLayerRef.opacity = 100;
myTextRef.contents = message;
}

 

When I select the resulting layer from my image, the size box on top tells me the font size is 58.33 px, or 4.17 times bigger than I asked for.  In this case, I didn't specify the units.  But in another script, the function is a bit different:

function CreateTextLayer(document, layerName, content, justification,
color, size, distanceFromLeft, distanceFromTop)
{
var textColor = color
if (typeof(color) == "undefined")
{
alert ("Creating black color");
textColor = new SolidColor();
textColor.rgb.red = 0;
textColor.rgb.green = 0;
textColor.rgb.blue = 0;
}

// make the layer;
var myLayerRef = document.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = layerName;

var myTextRef = myLayerRef.textItem;
// myTextRef.size = size;
myTextRef.size = new UnitValue(size, "px");
myTextRef.font = "Myriad Pro";
myTextRef.justification = justification;

//Set text colour
myTextRef.color = textColor;

// Set the position of the text - percentages from left first, then from top.
myTextRef.position = new Array(distanceFromLeft, distanceFromTop);
myLayerRef.blendMode = BlendMode.NORMAL;
myLayerRef.opacity = 100;
myTextRef.contents = content;
}

 

Here, I'm explicitly askng for the font size in pixels.  When I run this script and ask for text with a font size of 70 pixels, I get 291.67 pixels.  Again, 4.17 times the size I asked for.  

 

In case it matters, here's the code from the second example that calls this function:

doc = app.activeDocument;
doc.changeMode(ChangeMode.RGB);
var originalUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
doc.resizeImage (undefined, undefined, 300, ResampleMethod.NONE)

var gray = new SolidColor();
gray.rgb.hexValue = "505050";
var white = new SolidColor();
white.rgb.hexValue = "FFFFFF";

// Inner border eight pixels gray
app.backgroundColor = gray;
app.activeDocument.resizeCanvas(UnitValue(doc.width + 8,"px"),UnitValue(doc.height + 8,"px"));
// Inner frame six pixels white
app.backgroundColor = white;
app.activeDocument.resizeCanvas(UnitValue(doc.width + 6,"px"),UnitValue(doc.height + 6,"px"));
// outer frame 170 pixels gray
app.backgroundColor = gray;
app.activeDocument.resizeCanvas(UnitValue(doc.width + 120,"px"),UnitValue(doc.height + 120,"px"));
// Extend outer frame down by 170 pixels
app.activeDocument.resizeCanvas(UnitValue(doc.width,"px"),
UnitValue(doc.height + 120,"px"),
AnchorPosition.TOPCENTER);

// Add title
CreateTextLayer(doc, "Title", "This Is The Title of This Photo", Justification.LEFT,
undefined, 70, 0, doc.height);



 

 

TOPICS
Actions and scripting, Problem or error

Views

72

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 30, 2020 0
Adobe Community Professional ,
Oct 31, 2020

Copy link to clipboard

Copied

Most likely because of a bug (very old bug). In most cases, you need to use points or convert pixels or millimeters into them.

Notice and check what the expression 300/72 is equal to.

I suspect that your file has a resolution of 300 dpi.
 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 31, 2020 1
Community Beginner ,
Nov 01, 2020

Copy link to clipboard

Copied

Thanks very much.  I will modify my script accordingly.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 01, 2020 0
rob day LATEST
Adobe Community Professional ,
Nov 01, 2020

Copy link to clipboard

Copied

Sorry I should have checked—the .size property is always points even when the TypeUnits are set to pixels. So @r-bin  is right  you need (res/72)*size:

 

 

var res = 300
doc.resizeImage (undefined, undefined, res, ResampleMethod.NONE)

//and then
myTextRef.size = (72/res) * size;

 

You will need to change the TypeUnits to Pixels if you want the size in the UI to read as 70px and not 16.8 pt

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 01, 2020 0
Adobe Community Professional ,
Nov 01, 2020

Copy link to clipboard

Copied

If you want to set your type as Pixels, I think you will need to change the TypeUnit Preference:

 

 


app.preferences.typeUnits = TypeUnits.PIXELS;

var myTextRef = myLayerRef.textItem;
myTextRef.size = 14;

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 01, 2020 0