8 Replies Latest reply on Nov 17, 2011 10:35 AM by xbytor2

    photoshop text overflow

    chalkstar

      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

        • 1. Re: photoshop text overflow
          c.pfaffenbichler Level 9

          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/

          • 2. Re: photoshop text overflow
            c.pfaffenbichler Level 9

            One hardly practicable workaround might be to create a workPath from the text (Layer > Type > Create Work Path), then assess the number of subPathItems and if they are fewer than the text-string’s length minus the spaces the text probably overflows.

            • 3. Re: photoshop text overflow
              Michael L Hale Level 5

              I also don't see any direct way to determine overflow text from looking at the layer descriptor. However instead of converting the text to a path and trying to work out the size I would get the current layer bounds, increase the size of the text bounding box and check the layer bounds again. If the bounds change the text was overflowed.

               

              var currentBounds = app.activeDocument.activeLayer.bounds;
              var currentBottom = currentBounds[3];
              var currentTextBoxHeight = app.activeDocument.activeLayer.textItem.height;
              app.activeDocument.activeLayer.textItem.height = app.activeDocument.activeLayer.textItem.height + new UnitValue(1200,'px');
              var newBounds = app.activeDocument.activeLayer.bounds;
              if( newBounds[3] != currentBottom ) alert('The layer has overflowed text');
              app.activeDocument.activeLayer.textItem.height = currentTextBoxHeight;
              

               

              Then you can deal with the overflow by changing the text properties, the text box, or converting to point type depending on which method fits your document best.

              • 4. Re: photoshop text overflow
                c.pfaffenbichler Level 9

                Now that’s a good idea!

                • 5. Re: photoshop text overflow
                  xbytor2 Level 4

                  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;
                  };
                  
                  • 6. Re: photoshop text overflow
                    c.pfaffenbichler Level 9

                    Thanks for sharing that, xbytor!

                    • 7. Re: photoshop text overflow
                      chalkstar Level 1

                      thanks xbytor2, will give this a whirl. I dont know how but i did get the horizontalscale to reduce, But since sleping its gone. I cant recall if it was horizontalscale = -1 or horizontalscale.value = etc. but thanks for your input. i hope to return with good news.

                       

                      Date: Tue, 15 Nov 2011 12:05:41 -0700

                      From: forums@adobe.com

                      To: chalkyw42@live.co.uk

                      Subject: photoshop text overflow

                       

                       

                       

                       

                       

                       

                            

                                 

                                 

                                 

                          Re: photoshop text overflow

                       

                       

                          created by xbytor2 in Photoshop Scripting - View the full discussion

                       

                       

                       

                      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;

                      };

                       

                       

                       

                           Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/4027519#4027519

                           To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/4027519#4027519. In the Actions box on the right, click the Stop Email Notifications link.

                            

                           Start a new discussion in Photoshop Scripting by email or at Adobe Forums

                        For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

                      • 8. Re: photoshop text overflow
                        xbytor2 Level 4

                        thanks xbytor2, will give this a whirl. I dont know how but i did get the horizontalscale to reduce, But since sleping its gone. I cant recall if it was horizontalscale = -1 or horizontalscale.value = etc. but thanks for your input. i hope to return with good news.

                         

                        Somebody donated the scaling tweak to CSX awhile back. It's a nice clean little fix The truncation + '...' code is slightly less elegant. Also, since it is making a lot of changes to the text layer to reset the text and recompute the width, it's punishingly slow. Without the bit of code in there to estimate the length of the truncated string, the whole thing would have been unusable.