12 Replies Latest reply on Jul 21, 2015 4:15 PM by Trevorׅ

    Struggling to understand CoordinateSpaces using resolve method

    McShaman Level 1

      As the title of my post says... I'm struggling to understand CoordinateSpaces. I read the description provided in the 'Adobe InDesign CS6 JavaScript Scripting Guide' and I created a simple document to confirm my understanding. But the results I received were not as expected and I don't think I understanding some core concepts.

       

      I will write some blocks of code and then try to explain what I think they work.

       

      $.writeln( document.selection[0].resolve( [[0,0], BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS], CoordinateSpaces.PAGE_COORDINATES )[0] );
      

       

      This functions as I expected. The following is a breakdown of the arguments and my understanding of them:

      1. [0,0], BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS
        1. [0,0] – anchor position relative to the object bounds – Top Left
        2. BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS –  I am assuming that this makes the anchor position relative to the PageItem's path where as OUTER_STROKE_BOUNDS makes the anchor position include stroke weight extending past the PageItem's path
      2. CoordinateSpaces.PASTEBOARD_COORDINATES – Is the location of the anchor point within the pasteboard coordinate space


      Now this is all understandable but then I saw the following code on indiscripts:

       

      $.writeln( document.selection[0].resolve( [[0,0], BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS, CoordinateSpaces.INNER_COORDINATES], CoordinateSpaces.INNER_COORDINATES)[0];
      


      1. [0,0], BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS, CoordinateSpaces.INNER_COORDINATES]
        1. [0,0] – as above
        2. BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS – as above
        3. CoordinateSpaces.INNER_COORDINATES – This bit I don't understand. I read the Adobe InDesign CS6 JavaScript Scripting Guide and it seems to suggest that this is the location of the anchor point within the coordinate space. But that doesn't make sense to me because thats what the last argument is meant to do isn't it?
      2. CoordinateSpaces.INNER_COORDINATES – Confused now because of point above


      So as you can see this example the introduction of a coordinate space in the first argument confuses me. If somebody could help me understand its purpose I would really appreciate it.

       

      Also I have the following questions about coordinate spaces:

      1. PARENT_COORDINATES – Wouldn't this just be spread? All PageItems parents are a spread right?
      2. INNER_COORDINATES – What is this!? Documentation says it's the coordinate space the object was created in. How can I tell what coordinate space an object is created in? Why is this usful? If I draw a rectangle on a page is its created the pasteboard, page, or spread coordinate space?
        • 1. Re: Struggling to understand CoordinateSpaces using resolve method
          Marc Autret Level 4

          Hi McShaman,

           

          1. On the introduction of a coordinate space in the location argument of the resolve() method, give a read at Indiscripts :: InDesign Scripting Forum Roundup #7

          In short this parameter allows you to specify which bounding box is considered, noting that a single spline item has many possible b-boxes depending on the space you look from. More details on the full syntax of the location argument will come soon in the next chapters of my “Coordinate Spaces & Transformations” PDF.

           

          2. The parent coordinate space of a PageItem might be another PageItem's (inner) space, in case for example of a grouped object. Coordinate spaces basically reflect object hierarchy.

          Many details on this topic have been already exposed in CS&T: http://www.indiscripts.com/post/2014/03/coordinate-spaces-and-transformations-1

           

          Hope that helps,

          @+

          Marc

          • 2. Re: Struggling to understand CoordinateSpaces using resolve method
            McShaman Level 1

            Hi Marc Autret,

            Thanks for those resources. I wish Adobe put in half as much effort with their documentation as you guys at Indesign scripts do!

            I am starting to grasp these concepts better, I just have a couple of questions:

             

            1. Consider that I have a new document and I draw a rectangle somewhere on the page. With that rectangle selected I run the following code:

             

            $.writeln( document.selection[0].resolve( [ AnchorPoint.TOP_LEFT_ANCHOR, BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS, CoordinateSpaces.INNER_COORDINATES ], CoordinateSpaces.INNER_COORDINATES ) );
            

             

            Now the second argument (inSpace) is set to CoordinateSpaces.INNER_COORDINATES. I would have thought position 0, 0 of the rectangles inner coordinate space would be the middle of the shape. But instead it seems to be the middle of the spread coordinate space. Is this right? Is there a reason why?

             

            2. In the Scripting Roundup link you sent me it makes the following statement:

             

            The PageItem.resolve() method has three parameters: location, inSpace, and consideringRulerUnits (optional). The most important one, location, has almost ten different forms (all poorly documented).

             

            Is there anywhere these other forms a documented?

            • 3. Re: Struggling to understand CoordinateSpaces using resolve method
              Marc Autret Level 4

              Hi McShaman,

               

              Now the second argument (inSpace) is set to CoordinateSpaces.INNER_COORDINATES. I would have thought position 0, 0 of the rectangles inner coordinate space would be the middle of the shape. But instead it seems to be the middle of the spread coordinate space. Is this right? Is there a reason why?

               

              Yes and yes. According to your parameters, the resolve() method is asked to express the location of the inner bounding box top left anchor point relative to the inner coordinate space. A little known fact is that the origin of the inner coordinate space of any new page item coincides with the origin of the pasteboard space—which also is the origin of the first spread. Details here: http://indiscripts.com/blog/public/data/coordinate-spaces-and-transformations-1/Coordinate SpacesTransfos01-02.pdf#page=1…

              (It took me a long time to understand this phenomenon, for intuitively we all think that the inner space origin of an object should coincide with some inherent center point. The fact is, inner coordinate space does not fit inner bounding box. Moreover, bounding boxes are not coordinate spaces, although one can express locations in any bounding box system.)

               

              Also, take note that the coordinates returned by your resolve() code will not change if you move the object! Indeed, the translation affects the inner space origin itself, in a way that the transformation matrix of the object relative to its inner space always remains the IDENTITY matrix. Seen from the pasteboard space perspective, the original location of the inner space origin is [0,0] and any change in this value reveals the distance to which the object has been moved since its creation (which is an interesting feature.)

               

              For example, suppose you draw a rectangle at some location anywhere in your document. Now, move the object randomly, and move it again as much as you like… You can even close and reopen the document, create other objects, and so on. The very original location of the rectangle can always be restored using the [x,y] coordinates of its inner space origin relative to the pasteboard and applying the inverse translation, as shown below:

               

              var myPageItem = app.selection[0];
              
              // Inner space origin of the sel (in pasteboard space.)
              // ---
              var xy = myPageItem.resolve(
                  [[0,0],CoordinateSpaces.INNER_COORDINATES],
                  CoordinateSpaces.PASTEBOARD_COORDINATES
                  )[0];
              
              // Restore the original location by reverting the translation.
              // ---
              myPageItem.transform(
                  CoordinateSpaces.PASTEBOARD_COORDINATES,
                  [0,0],
                  [1,0,0,1].concat([-xy[0],-xy[1]])
                  );
              

               

               

              About the full documentation of the location parameter, it will be detailed in the next chapters of my PDF.

               

              @+

              Marc

              • 4. Re: Struggling to understand CoordinateSpaces using resolve method
                Trevorׅ Adobe Community Professional

                Hi Marc

                 

                I was debating starting a new thread but decided to hitch a ride on this existing one as the title is very descriptive.

                Ignoring problems like Indiscripts :: Dealing with Rotated Spread Views in a Script or spreads that had been transformed in other ways and also ignoring that the spread could be made of pages of varying heights.

                I thought of getting the spreads dimensions by resolving it as follows:

                var tl, br, h, w, s;
                s = app.activeDocument.spreads[0];
                tl = s.resolve(AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.SPREAD_COORDINATES, false)[0];
                br = s.resolve(AnchorPoint.BOTTOM_RIGHT_ANCHOR, CoordinateSpaces.SPREAD_COORDINATES, false)[0];
                w = br[1] - tl[1];
                h = br[0] - tl[0];
                alert ("Spread Dimensions\nHeight: " + h + "\nWidth: " + w + "\nRatio: " + (h/w));
                

                 

                On my A4 spread the output is:

                Spread Dimensions

                Height: 1785.826771653

                Width: 985.889763778

                Ratio: 1.8113858539413

                 

                Why is this not what one would expect, shouldn't the ratio be ≈ 1.41

                 

                Trevor

                • 5. Re: Struggling to understand CoordinateSpaces using resolve method
                  Laubender Adobe Community Professional & MVP

                  Hi Trevor,

                  your code is calculating the "standard" sized pasteboard of spread 1, not the A4 page on spread 1:

                   

                  RectangleSizedOfPasteboardSelected.png

                   

                  Uwe

                  • 6. Re: Struggling to understand CoordinateSpaces using resolve method
                    Trevorׅ Adobe Community Professional

                    Thanks Uwe

                     

                    Yes I guess my misconception was that a spread is a collection of pages but I guess the spread is really the pasteboard and the pasteboard contains those pages.

                    You can have items off the page but never off the spread.

                     

                    Trevor

                    • 7. Re: Struggling to understand CoordinateSpaces using resolve method
                      Trevorׅ Adobe Community Professional
                      var tl, br, h, w, s;
                      s = app.activeDocument.spreads[0];
                      tl = s.pages[0].resolve(AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.SPREAD_COORDINATES)[0];
                      br = s.pages[-1].resolve(AnchorPoint.BOTTOM_RIGHT_ANCHOR, CoordinateSpaces.SPREAD_COORDINATES)[0];
                      h = br[1] - tl[1];
                      w =br[0] - tl[0];
                      $.writeln ("Spread Dimensions\nHeight: " + h + "\nWidth: " + w + "\nRatio: " + (h/w));
                      

                      Gives

                      Spread Dimensions

                      Height: 841.889763778

                      Width: 595.275590551

                      Ratio: 1.41428571428358

                       

                      But if the spread is multi-paged it will not return the sum (I don't know why) so I guess it's easier to just add up the page bounds.

                      • 8. Re: Struggling to understand CoordinateSpaces using resolve method
                        Marc Autret Level 4

                        Hi Trevor,

                         

                        Uwe's answer is correct. The spread, seen as a bounding box, is the entire area that wraps pages including pasteboard margins.

                         

                        (Note that a PageItem inner box may still overflow beyond the spread area, although both always have at least one intersection point.)

                         

                        @+

                        Marc

                        • 9. Re: Struggling to understand CoordinateSpaces using resolve method
                          Marc Autret Level 4

                          Trevorׅ wrote:

                          But if the spread is multi-paged it will not return the sum (...)

                           

                          Your script works for me

                          • 10. Re: Struggling to understand CoordinateSpaces using resolve method
                            Laubender Adobe Community Professional & MVP

                            Hi Trevor,

                            your script in #7 is working for me as well.

                            I added one line at the end to create a rectangle the size according to your results:

                             

                            var tl, br, h, w, s; 
                            s = app.activeDocument.spreads[0]; 
                            tl = s.pages[0].resolve(AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.SPREAD_COORDINATES)[0]; 
                            br = s.pages[-1].resolve(AnchorPoint.BOTTOM_RIGHT_ANCHOR, CoordinateSpaces.SPREAD_COORDINATES)[0]; 
                            h = br[1] - tl[1]; 
                            w =br[0] - tl[0]; 
                            $.writeln ("Spread Dimensions\nHeight: " + h + "\nWidth: " + w + "\nRatio: " + (h/w));
                            //Added:
                            app.documents[0].rectangles.add({geometricBounds : [0,0,h+"pt",w+"pt"]});
                            

                             

                            However, using the Page Tool to move one page a bit in a spread will give some unpleasant results:

                             

                            OnePageMovedByThePageTool.png

                             

                            So I think, it's better to exploit the page bounds to get an idea what's going on in the spread.

                             

                            Uwe

                            • 11. Re: Struggling to understand CoordinateSpaces using resolve method
                              McShaman Level 1

                              Hi Marc

                               

                              I look forward to reading your PDF when it comes out. Just a quick question in relation to the location parameter. Consider the following:


                              pageItem.resolve( AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.INNER_COORDINATES );
                              

                               

                              When passing only the anchor point argument to the location parameter are defaults for the bounds type and coordinate system assumed? And if so... What are the defaults?

                               

                              In other words is the following code the same as above?

                               

                              pageItem.resolve( [ AnchorPoint.TOP_LEFT_ANCHOR, BoundingBoxLimits.OUTER_STROKE_BOUNDS, CoordinateSpaces.INNER_COORDINATES ], CoordinateSpaces.INNER_COORDINATES );
                              
                              • 12. Re: Struggling to understand CoordinateSpaces using resolve method
                                Trevorׅ Adobe Community Professional

                                Hi Marc and Uwe

                                 

                                Thanks for the feedback.

                                 

                                My snippet is very unreliable as it depends on which page is [0] and which is [-1].

                                So it was a right to left binding issue.  That in addition the page tool issue renders it pretty useless.

                                 

                                So back to the standard bounds method

                                var doc, s, p, l, w, h;
                                doc = app.activeDocument;
                                s = doc.spreads[0];
                                p = s.pages;
                                l = p.length;
                                w = 0;
                                b = p[0].bounds;
                                h = b[2] - b[0];
                                while (l--) {
                                    b = p[l].bounds;
                                    w += b[3] - b[1];
                                }
                                $.writeln ("w: " + w + "\nh: "+ h);
                                
                                doc.rectangles.add({geometricBounds : [0,0,h,w], fillColor: app.activeDocument.colors.itemByName("Yellow")}); 
                                

                                 

                                Trevor