5 Replies Latest reply on Jan 16, 2014 7:38 PM by Franck Payen

    mm -> px horizontalScale rounding

    Franck Payen MVP & Adobe Community Professional

      Hi all.

       

      Quite new to Ai's scripting stuff.

       

      I'm having a complicate time with Ai's save to jpg options vs artboard sizes and units conversions.

       

      Say, i have a doc (from my client), which they inherited and enhanced for 10 years may be.

       

      It's "supposed" to be in millimeters, but artboardRect gives me those values :

      -0.27880000000005

      436.191388980878

      646.457

      -0.20900000000074

       

      which sound like weird points or pixels.

       

      I need to export a jpg (or gif, or png), that will be X pixels wide. For the purpose of the test i set it at 1200.

       

      var maxLargeurPx = 1200;

       

      First thing, getting the artboard's width :

       

      largeurArtboard = Math.abs(thisArtboardRect[2]-thisArtboardRect[0]);

      // 646.7358

       

      Then i try to have a multiply factor to use

       

      coefExport = /*Math.round*/ (100 * maxLargeurPx / largeurArtboard);

      // 185.54717397738

       

      and then set my ExportFile jpg to this value (i can't give it a number)

       

           // exportOptions.horizontalScale = coefExport

       

      That's when it's getting fun : my images are 1204 px wide with or without clipping.

       

      Of course, i "can't" resize the doc for each export, especially expecting to have dozens of those, with different values, dimensions, etc.

       

      Any idea is welcome

        • 1. Re: mm -> px horizontalScale rounding
          Gustavo Del Vechio Level 3

          Hi Franck

           

          Do not know if it helps, but, always you need value conversion, you can work with an object called UnitValue.

           

          So, an example:

           

           

          var test = new UnitValue ("120", "mm"); //here you create you object and set the value and measurement.
          var testPT = test.as ("pt");  //I'´m using a function, avaliable for this kind of object, to convert the value as point, so you can use in the functions that accept value in points.
          alert(testPT);
          
          

           

           

          Let me know if it helps

          Best Regards

          Gustavo

          • 2. Re: mm -> px horizontalScale rounding
            CarlosCanto Adobe Community Professional & MVP

            It's "supposed" to be in millimeters, but artboardRect gives me those values :

            -0.27880000000005

            436.191388980878

            646.457

            -0.20900000000074

            these values are [left, top, right, bottom] always in points, regardless of the Units used in the user interface, you have to make your unit conversions yourself.

            • 3. Re: mm -> px horizontalScale rounding
              [Jongware] Most Valuable Participant

              > ... you have to make your unit conversions yourself ...

               

              .. which is (knowing the reported values are in *points*) as simple as

               

              mmvalue = ptvalue*25.4/72;

               

              A formula so short it's *barely* worth the effort of writing a function for.

              • 4. Re: mm -> px horizontalScale rounding
                Franck Payen MVP & Adobe Community Professional

                Oh, thank you both.

                 

                My Original Post was quite long, so i realize i shouldn't even have mentionned millimeters and got you confused in my need.

                 

                Problem here is, Ai using points, when i try to get a percentage value for the exportOptions i might be applying rounding at the wrong place.

                 

                So i'll go with trial and error to see what's inside the Ai export engine.

                 

                var AR = artboardRect[left, top, right, bottom]

                i was ok with AR[2]-AR[0] being my Artboard width.

                 

                Now Ai must use "other" rounded values of my work to determine the size through horizontal scale, because my calculation uses 1200 px to get the 185.xxxxx %, from AR[0] and AR[2] which have not rounded units either.

                • 5. Re: mm -> px horizontalScale rounding
                  Franck Payen MVP & Adobe Community Professional

                  Ok, forget it all, i've got it.

                   

                  I found the proper formula, and it was all about rounding :

                   

                  Now i use :

                  largeurArtboard = Math.abs( Math.floor(AR[2])- Math.floor(AR[0]));

                  coefExport[artboardIndex] = 100 * maxLargeurPx / largeurArtboard;

                  and i don't forget to set the artBoardClipping value to true.

                   

                  I get nice 1200px exports, which means, that's how Ai's export work