3 Replies Latest reply on Oct 28, 2013 1:16 AM by CarlosCanto

    How to resize a textFrame, while also resizing its parent (a groupitem)?

    clrx01

      I'm trying to find the smallest textframe within a group and then matching that textframe font size to the minimum size allowed for printing. Everything else works, except I can't figure out how to resize the textframe while also resizing its parent, the group.

       

       

       

       

      #target illustrator

      docRef = app.activeDocument;

      gpRef = docRef.groupItems[0];

      var gpHeight = gpRef.height;

       

       

      //this is the minimum print font size allowed

      var minFntSize = 18;

      var garbLay;

      var txtFrmCount = gpRef.textFrames.length;

       

      var container = { };

       

      var i;

       

      //put each textframe and height prop into json

      for(i = 0; i < txtFrmCount; i++) {

          container['prop'+i] = { 'gpTfHeight':gpRef.textFrames[i].height / 72, 'gpTarget':gpRef.textFrames[i] };

      }

       

      //find the min value of height prop in textframe; i have the max in there too just incase i need it in the future

      var min = Infinity;

      var max = -Infinity;

      var x;

      for (x in container) {

         if( container[x]['gpTfHeight'] < min) {

              min = container[x]['gpTfHeight'];

              minTg = container[x]['gpTarget'];

          }

          if( container[x]['gpTfHeight'] > max) {

              max = container[x]['gpTfHeight'];

          }

      }

       

      //duplicate textframe, convert to ouline and get actual height

       

      var txtFrmDup = minTg.duplicate();

      txtFrmDup.position = [(minTg.position[0] - 100), (minTg.position[1] - 100)];

      txtFrmDup.name = 'garbage';

      var tOutline = txtFrmDup.createOutline();

      tOutline.name = 'garbage';

      var tLogHeight = Math.round(tOutline.height);

      garbLay = gpRef.groupItems.getByName('garbage');

       

      //this is the part where I might need to convert to a percentage? tgSize is the target size and I figured I could get the different of the 2 and then use that amount to resize down/up...

       

      var tgSize = minFntSize - tLogHeight;

      var perc = Math.round((tgSize/gpRef.height)*100);

       

      $.writeln(minFntSize);

      $.writeln(tLogHeight);

      $.writeln(tgSize);

      $.writeln(perc);

       

      //resize gp so that min textframe font size meets minFntSize

      if(tLogHeight < minFntSize) {

          gpRef.resize(perc, perc);

          $.writeln('smaller');

          garbLay.remove();

      } else if(tLogHeight > minFntSize) {

          gpRef.resize(perc, perc);

          $.writeln('bigger');

          garbLay.remove();

      } else if(tLogHeight == minFntSize) {

          $.writeln('equal');

      }

       

      I'm not even sure if I'm going about this the right way. Hoping a fresh set of eyes can take a look and see what I'm doing wrong or not doing? Thanks!

        • 1. Re: How to resize a textFrame, while also resizing its parent (a groupitem)?
          CarlosCanto Adobe Community Professional & MVP

          check this, I changed it a little

           

          // scale grouped text Frames to a minimum font size
          // http://forums.adobe.com/thread/1323065?tstart=0
          // carlos canto
          
          
          #target illustrator
          
          
          var docRef = app.activeDocument;
          var gpRef = docRef.groupItems[0];
          
          
          //this is the minimum print font size allowed
          var minFntSize = 18;
          
          var txtFrmCount = gpRef.textFrames.length;
          
          
          // make the first frame the one with the minimum font size
          var idx = 0;
          var minFrameFontSize = gpRef.textFrames[idx].textRange.characterAttributes.size;
          
          
          // see if the other frames have smaller font size than the first
          for(i = 1; i < txtFrmCount; i++) {
              var itext = gpRef.textFrames[i];
              var fontSize = itext.textRange.characterAttributes.size;
              if (fontSize<minFrameFontSize) {
                  minFrameFontSize = fontSize;
                  idx = i;
              }
          }
          
          
          // now see if the smallest font size frame is smaller than the minimum font size (18)
          if (minFrameFontSize<minFntSize) {
              var frameToScale = gpRef.textFrames[idx]; // get a ref to the frame
              var frameToScaleHeight = frameToScale.height; // get its height
              frameToScale.textRange.characterAttributes.size = minFntSize; // temporarily change font size to the minimum 18
              var frameScaledHeight = frameToScale.height; // check the new height
          
              var scale = frameScaledHeight/frameToScaleHeight*100; // get the scale
          
              frameToScale.textRange.characterAttributes.size = minFrameFontSize; // change the font size back
          
              frameToScale.parent.resize (scale, scale); // scale the whole group
          }
          
          • 2. Re: How to resize a textFrame, while also resizing its parent (a groupitem)?
            clrx01 Level 1

            Awesome job!!! Thank you!! Nothing happened at first run so I switched minFrameFontSize<minFntSize to minFrameFontSize>minFntSize and it worked! Also added a 2nd a if statement with the original minFrameFontSize<minFntSize

             

            I will need to size it up if its too small. For the sake of learning and other than the scale issue, was the way I had it to find the min/max wrong or its just a different way? I only converted to outline cause I didn't know about the characterAttributes.size property. I was looking for fontSize in the manual lol.

             

            Thank you again!!

            • 3. Re: How to resize a textFrame, while also resizing its parent (a groupitem)?
              CarlosCanto Adobe Community Professional & MVP

              you're welcome.

              clrx01 wrote:

               

              For the sake of learning and other than the scale issue, was the way I had it to find the min/max wrong or its just a different way?

              I guess it is just different, as long as you get the results you're after, I guess anything goes...efficiency is the next step.