Skip to main content
November 12, 2011
Question

photoshop text overflow

  • November 12, 2011
  • 1 reply
  • 7079 views

Hello,

Can someon help me with an issue i have. I have a text box in photoshop which i am putting varible data in, I cant stop the ext from overflowing. Can anyone help me with a script that if the text overflows it scales down. either by font or by scaling horizontaly or vertcally.

Thanks

This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
November 15, 2011

I’m not optimistic for this task.

After all Photoshop is not really a page layout program and has somewhat limited type-spedific features.

And I could not identify a marker for overflow in the ActionDescriptor-code (though admittedly I only browsed it). 

You might be better off using BridgeTalk to do the typework in Indesign or Illustrator.

Maybe/probably someone else has more insight than me, though; you could also post on http://www.ps-scripts.com/

Inspiring
November 15, 2011

I've solved a related problem in doing contact sheet thumbnail captions. In my case, I know the bounds of the caption and need to fit the name of the image in that bounds. If I can't scale it to fit, I truncate it and append '...' until it fits. The performance is not what I would like, but it's not unreasonable.

ContactSheetII.prototype.insertCaption = function(doc, csOpts, bnds, file,  image) {

  var self = this;

  if (file == undefined) {

    file = File(image.name);

  }

  var caption = decodeURI(file.name);

  if (csOpts.captionFilename) {

    if (csOpts.noExtensions) {

      caption = file.strf("%f");

    }

  } else {

    caption = '';

  }

//   LogFile.write("Inserting caption " + caption + " at " + bnds);

  app.activeDocument = doc;

  var layer = doc.artLayers.add();

  layer.kind = LayerKind.TEXT;

  layer.name = decodeURI(file.name);

  var titem = layer.textItem;

  if (csOpts.autoscaleCaptions) {

    titem.minimumGlyphScaling = 50;

    titem.desiredGlyphScaling = 100;

  }

  titem.contents = caption;

  if (!csOpts._fontColor) {

    csOpts._fontColor = psx.colorFromString(csOpts.fontColor);

  }

  titem.color = csOpts._fontColor;

  titem.size = csOpts.fontSizePX;

  titem.font = csOpts.font;

  titem.kind = TextType.PARAGRAPHTEXT;

  var captionWidth = bnds[2]-bnds[0];

  // Check to see if the caption is too wide

  var twidth = Math.round(titem.width);

  if (csOpts.autoscaleCaptions) {

    // try to scale the glyph width to make it fit

    if (twidth > captionWidth) {

      if ((captionWidth / twidth) > 0.5) {

        titem.desiredGlyphScaling = (100 * captionWidth) / twidth;

      } else {

        titem.desiredGlyphScaling = 50;

      }

    }

    // switching the kind forces PS to recompute the text size

    titem.kind = TextType.POINTTEXT;

    titem.kind = TextType.PARAGRAPHTEXT;

    twidth = Math.round(titem.width);

  }

  if (csOpts.dotTruncate) {

    // truncate the text until it fits

    if (twidth > captionWidth) {

      if (caption.length <= 4) {

        // handle the deviant case here

        titem.contents = caption[0] + '...';

      } else {


        // estimate the string length...

        var len = Math.round((captionWidth/twidth) * caption.length);

        var str = caption.substring(0, len-4) + '...';

        // take off the file extension first

        while (twidth > captionWidth+4) {

          titem.contents = str;

          if (str.length <= 4) {

            break;

          }

          titem.kind = TextType.POINTTEXT;

          titem.kind = TextType.PARAGRAPHTEXT;

          twidth = Math.round(titem.width);

         

          str = str.substring(0, str.length-4) + '...';

        }

      }

    }

  }

  titem.justification = Justification.CENTERJUSTIFIED;

  titem.width = bnds[2]-bnds[0];

  titem.position = [bnds[0], bnds[1]];

  var style = csOpts.captionStyle;

  if (style && style != CSII.NO_CAPTION_STYLE) {

    layer.applyStyle(style);

  }

  return layer;

};

c.pfaffenbichler
Community Expert
Community Expert
November 16, 2011

Thanks for sharing that, xbytor!