12 Replies Latest reply on Jun 2, 2009 2:13 PM by dkhicks7

    fit content proportionally?

    dkhicks7

      I'm using javascript on Indesign CS4. I have a function thats placing different amounts of text into a template that I have previously. Since all the text is differently sized, sometimes it doesn't fit the template. I tried using fit.(fitOptions.proportionally) when my text frame was selected, but nothing happened. There wasn't a bug either, nothing happened at all. What should I do? Thanks!

        • 1. Re: fit content proportionally?
          Peter Kahrel Adobe Community Professional & MVP

          Try .fit (FitOptions.frameToContent).

           

          Peter

          • 2. Re: fit content proportionally?
            Dave Saunders Level 4

            As far as fitting options are concerned, you can only do in a script what is available in the UI. And, as Peter has pointed out,that is just the fit frame to contents (and even then it only works in certain circumstances).

             

            If you want to increase the size of the text in your frame so it fills the frame, you'll need to do this the hard way. A sneaky way to do it would be to adjust the horizontal and vertical scale of all the text, but even there, you'd need to work a text style range at a time.

             

            And getting the amount right would involve iterating.

             

            Dave

            1 person found this helpful
            • 3. Re: fit content proportionally?
              Steven.. Level 3

              You can use my free javascript FitStoryToFrame at Indesign Exchange, http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1657519

              works with cs3 and cs4 (maybe more but that all that was tested) it iterates text ranges and therefor does not with nested styles, grep styles and line styles. But otherwise it's an amazing tool and has many choices for how to expand or shrink story

              • 4. Re: fit content proportionally?
                Harbs. Level 6

                I wrote this script way back when.. Maybe it'll be of use...

                 

                /*************************************************************************************
                 FitTextToFrame.jsx
                   InDesign CS2 Script: version 1.0 Feb 4, 07 by Harbs
                   isText method and errorExit function by Dave Saunders
                
                   One line frames will expand down to fit the whole line in the width of the frame
                   Multi-line frames will fit the widest line
                   Text of varying sizes is scaled proportionally
                **************************************************************************************/
                Object.prototype.isText = function() {
                  switch(this.constructor.name){
                    case "InsertionPoint":
                    case "Character":
                    case "Word":
                    case "TextStyleRange":
                    case "Line":
                    case "Paragraph":
                    case "TextColumn":
                    case "Text":
                    return true;
                    default :
                    return false;
                    }
                  }
                if (!app.selection[0]){errorExit("No Text Selected")}
                if (app.selection[0].isText()){
                  var myTF = app.selection[0].parentTextFrames[0];
                  }else if (app.selection[0].constructor.name == "TextFrame"){
                  var myTF = app.selection[0];
                  }else{errorExit("No Text Selected")}
                var mySI = myTF.characters[0].index;
                var myIndx = myTF.characters[-1].index;
                var myLineLength = myTF.lines.length;
                var myText = myTF.parentStory.characters.itemByRange(mySI,myIndx);
                var myJust = myText.justification;
                myText.justification = Justification.leftAlign;
                aoIPDif = new Array;
                for(i = 0; i < myTF.lines.length; i++){
                  var oIPs = myText.lines[i].insertionPoints.everyItem().horizontalOffset;
                  oIPs.sort(sortNumber);
                  aoIP1 = oIPs[0];
                  aoIP2 = oIPs[oIPs.length-1];
                  aoIPDif.push(aoIP2-aoIP1)
                  }
                myText.justification = Justification.fullyJustified;
                aeIPs = myText.lines[0].insertionPoints.everyItem().horizontalOffset;
                aeIPs.sort(sortNumber);
                aeIP1 = aeIPs[0];
                aeIP2 = aeIPs[aeIPs.length-1];
                aeIPDif = aeIP2-aeIP1;
                var aoDif = aoIPDif[0]
                myLine = myTF.lines[0];
                for (var i = 1; i < aoIPDif.length; i++){
                  if (aoIPDif[i]>aoDif){var aoDif=aoIPDif[i]}
                  myLine = myTF.lines[i];
                  }
                myLineLength = myLine.characters.length;
                var myFactor = aeIPDif / aoDif;
                for(var i=0;i<myText.characters.length;i++){
                  var myChar = myText.characters[i];
                  myChar.pointSize = myChar.pointSize * myFactor;
                  }
                myText.justification = myJust[0];
                var myDoc = app.activeDocument;
                var myPage = myTF.parent;
                var userVert = app.documents[0].viewPreferences.verticalMeasurementUnits; 
                app.documents[0].viewPreferences.verticalMeasurementUnits = MeasurementUnits.points; 
                var myBottomMarginLocation = myDoc.documentPreferences.pageHeight - myPage.marginPreferences.bottom;
                var myBounds = myTF.geometricBounds;
                myBounds[2] = myBottomMarginLocation;
                myTF.geometricBounds = myBounds;
                try {var myForceErr = myLine.characters.length}
                catch(e){
                  app.documents[0].viewPreferences.verticalMeasurementUnits = userVert;
                  errorExit("Some text got overset")
                  }
                while (myLine.characters.length<myLineLength){
                  for(var i=0;i<myText.characters.length;i++){
                    var myChar = myText.characters[i];
                    myChar.pointSize = myChar.pointSize /1.01;
                    }
                  }
                myBounds[2] = myTF.characters[-1].baseline + myTF.characters[-1].descent + myTF.textFramePreferences.insetSpacing[2] + myTF.strokeWeight;
                myTF.geometricBounds = myBounds;
                app.documents[0].viewPreferences.verticalMeasurementUnits = userVert;
                if (myTF.characters[-1].index < myIndx) {errorExit("Some Text Got Overset")}
                
                function errorExit(message) {
                  if (arguments.length > 0) {
                    if (app.version != 3) { beep() } // CS2 includes beep() function.
                    alert(message);
                    }
                  exit(); // CS exits with a beep; CS2 exits silently.
                  }//end function errorExit
                
                function sortNumber(a, b){
                  return a - b
                  }
                
                • 5. Re: fit content proportionally?
                  dkhicks7 Level 1

                  That's almost perfect. Any idea of how to change it for when my text doesn't fit in the frame at all, or is larger than the text frame?

                  • 6. Re: fit content proportionally?
                    Harbs. Level 6

                    I'll check if I have a version which first shrinks the text. I don't remember anymore...

                    • 7. Re: fit content proportionally?
                      Harbs. Level 6

                      Nope. I can't find one that does that.

                       

                      All you really need to do is loop through all the textStyleRanges in the story and shrink them all by a large set amount before you expand the text...

                       

                      Harbs

                      1 person found this helpful
                      • 8. Re: fit content proportionally?
                        dkhicks7 Level 1

                        Thanks a lot! I think I can manage that!

                        • 9. Re: fit content proportionally?
                          Harbs. Level 6

                          I wrote this script quite some time ago. I'd probably do many things differently if I was writing it today.

                           

                          One thing which will make it much more efficient would be to change the loops from looping through characters to looping through textStyleRanges...

                           

                          Harbs

                          • 10. Re: fit content proportionally?
                            TᴀW Adobe Community Professional & MVP

                            I just wrote a script that does the following:

                             

                            If a text frame is overset, it will reduce the contents of the frame until all the text fits.

                             

                            If a text frame is half empty, it will enlarge the contents of the frame until everything fits.

                             

                            Is that what you would need?

                            • 11. Re: fit content proportionally?
                              dkhicks7 Level 1

                              yes maybe. id love to try it!

                              • 12. Re: fit content proportionally?
                                TᴀW Adobe Community Professional & MVP

                                (I sent you a private message.)