8 Replies Latest reply on Jan 19, 2012 8:23 AM by cotcodac

    Fit text frame to content - proportionally

    cotcodac Level 1

      Hi

      I created a few textframes with a certain ratio aspect (heigh/width) that I would like to maintain after I apply

       

      myTextFrame.fit(FitOptions.FRAME_TO_CONTENT);

       

      Any ideas on how to do it? I found something on the forum but it maintend constant either the height or the width. Thank you.

        • 1. Re: Fit text frame to content - proportionally
          absqua Level 4

          Something along the lines of:

           

          function resize(frame, by) {
                    frame.resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.MULTIPLYING_CURRENT_DIMENSIONS_BY, [by, by]);
          }
          function fitTextFrameProportionally(frame, factor) {
                    if (frame.overflows) {
                         while (frame.overflows) {
                              resize(frame, 1 + factor);
                         }
                    }
                    else {
                         while (!frame.overflows) {
                              resize(frame, 1 - factor);
                         }
                         resize(frame, 1 / (1 - factor));
                    }
          }
          
          fitTextFrameProportionally(myTextFrame, 0.01);
          

           

          Jeff

          • 2. Re: Fit text frame to content - proportionally
            Peter Kahrel Adobe Community Professional & MVP

            Bravo, Jeff! You must be one of the few people around who understand the resize() and other transform methods. I always struggle with them and can use them only by using concrete examples. This one of yours goes into my library.

             

            Peter

            • 3. Re: Fit text frame to content - proportionally
              absqua Level 4

              If by "understand" you mean "one time stumbled across something that works and now copy and paste that line when needed," then I understand.

               

              Actually, resize() isn't that complicated; it's transform() I stay well away from. I'm still waiting for Marc Autret's eight-part blog post about it...

              • 4. Re: Fit text frame to content - proportionally
                [Jongware] Most Valuable Participant

                Nice piece of code, except it doesn't work the same way Fit Frame To Content does. This code will make text re-flow on multiple lines, and the built-in function does not (it merely changes the height of the text frame without changing the width).

                Perhaps it is what the OP wants, perhaps it's not.

                 

                To get the minimum necessary height for a text frame with paragraphs of multiple lines, all you need is Fit Frame To Content. Getting the minimum possible width, though, needs some consideration -- if, that is, you don't want this text to reflow. Since text can be left aligned, off the top of my head you can only do this by (a) storing the rightmost text position per line, then (b) decrease frame width until any of the right positions change. Only then you can be assured the frame is as narrow as possible.

                 

                Ah -- and you need the above, minimum height & width, and the original width/height ratio, so you can compare this new size to the original and adjust either width or height to a larger value. (I'm typing this, and thinking, "Changing the width to a larger value may change the text flow as well, wouldn't it?")

                 

                Anyway, it's all just random muttering if the OP only has to adjust single-line text frames.

                • 5. Re: Fit text frame to content - proportionally
                  [Jongware] Most Valuable Participant

                  This is what I mean (Resizing was done with my version of the same function but it ended up so similar to Jeff's that I bow to his nicer looking code.) The proportions are the same, the text runs different.

                   

                  Screen Shot 2012-01-18 at 10.05.16 PM.png

                  • 6. Re: Fit text frame to content - proportionally
                    [Jongware] Most Valuable Participant

                    Hah! Got it to work but it failed every time! Then I noticed I used the center anchor to resize ... and of course the horizontal offsets of the right hand side are relative to the page, not the text frame. A quick change to Top Left and it works for multi-line text as well (uh, technically I think it'd be neater if it worked from the center, though -- this only requires Yet Even More Code).

                     

                    box = app.selection[0];
                    lineEnds = box.lines.everyItem().insertionPoints[-1].horizontalOffset;
                    limit = 0.1;
                    
                    do
                    {
                      while (!box.overflows && linesSame(box, lineEnds))
                        box.resize (CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.MULTIPLYING_CURRENT_DIMENSIONS_BY, [1-limit, 1-limit]);
                      box.resize (CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.MULTIPLYING_CURRENT_DIMENSIONS_BY, [1/(1-limit), 1/(1-limit)]);
                      limit /= 2;
                    } while (limit > 0.001);
                    
                    function linesSame (frame, originalEnds)
                    {
                      newEnds = frame.lines.everyItem().insertionPoints[-1].horizontalOffset;
                      if (newEnds.length != originalEnds.length)
                        return false;
                      for (i=0; i<originalEnds.length; i++)
                        if (newEnds[i] != originalEnds[i])
                          return false;
                      return true;
                    }
                    

                     

                    Same text frame as in the previous post, only now resizing honours line breaks. Look, the width/height ratio is still the same!

                    Screen Shot 2012-01-18 at 10.18.04 PM.png

                    1 person found this helpful
                    • 7. Re: Fit text frame to content - proportionally
                      absqua Level 4

                      Hmm. I hadn't even considered that the OP might want to keep the line breaks.

                       

                      If the frame starts out overflowed—if you're sizing up, that is—you couldn't very well keep them...

                      • 8. Re: Fit text frame to content - proportionally
                        cotcodac Level 1

                        Thank you all for trying my problem... Jongware, I am trying to use the function fitframetocontent, wich doesnt work in the way you assumed... I am sorry for not being more specific. I needed a script that would break a textframe into a number of other textframe, each textframe would contain only one paragraph from the original tf, new tf would have a object style aplied. When creating the new tf I would make them bigger than needed and than resize the tf so the text would just fill the tf, without overflow or too much white space. I wanted the same action as you would get by double clicking the right bottom control point of the textframe.script results.jpg

                        I atached a image - in the left is the result I got from absqua (wich is the one I want), in the right is the result I got running your script.

                        Thank you absqua... Your script is easier to understand than the one marc autret provided here , where the problem was that op wanted only the height modified I think. There is a problem of speed, Marc's script runs much faster, while yours is showing me the steps it takes... Other than that... it works perfectly.

                        • 9. Re: Fit text frame to content - proportionally
                          absqua Level 4

                          Marc's is taking a more sophisticated "binary search"-style approach of bouncing around and zeroing in on the right value, which results in many fewer steps. If I have time I'll try to incorporate that approach into mine. (Actually, modifying his function to work on both dimensions at the same time would probably be easier than building on mine...)

                           

                          In the meantime, you could wrap the call in an UndoModes.FAST_ENTIRE_SCRIPT doScript() call, and turn off screen redrawing. Those two steps would likely speed it up quite a bit.

                           

                          function resize(frame, by) {
                                    frame.resize(CoordinateSpaces.INNER_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.MULTIPLYING_CURRENT_DIMENSIONS_BY, [by, by]);
                          }
                          function fitTextFrameProportionally(frame, factor) {
                                    if (frame.overflows) {
                                         while (frame.overflows) {
                                              resize(frame, 1 + factor);
                                         }
                                    }
                                    else {
                                         while (!frame.overflows) {
                                              resize(frame, 1 - factor);
                                         }
                                         resize(frame, 1 / (1 - factor));
                                    }
                          }
                          
                          app.scriptPreferences.enableRedraw = false;
                          app.doScript("fitTextFrameProportionally(myTextFrame, 0.01)", ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Fit Text Frame to Content Proportionally");
                          app.scriptPreferences.enableRedraw = true;
                          

                           

                          Edited to show the extra code.

                          • 10. Re: Fit text frame to content - proportionally
                            cotcodac Level 1

                            Thank you. I dont feel a difference in speed, just that I dont see the intermediary steps... Anyway, it is ok at least I understand what it does and at least I'm sitting back and watch the computer do the work I was supposed to do - it really doesnt matter how fast since its faster than me doing it manually...

                            On the other hand I managed to modify Marc's version to do what I want - it modifies now both dimensions. Of course, I'm not sure what I did, I just change wherever I saw [x, 0] to [x,x]... (It was a function that resized the textframe only on x axis, now it works on both).

                            Thank you again.