KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jpdf > PDFPage


1 /*
2  * $Id: PDFPage.java,v 1.3 2002/03/04 20:33:18 ezb Exp $
3  *
4  * $Date: 2002/03/04 20:33:18 $
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package gnu.jpdf;
21
22 import java.awt.*;
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * <p>This class defines a single page within a document. It is linked to a
28  * single PDFGraphics object</p>
29  *
30  * @author Peter T Mount
31  * @author Eric Z. Beard, ericzbeard@hotmail.com
32  * @author $Author: ezb $
33  * @version $Revision: 1.3 $, $Date: 2002/03/04 20:33:18 $
34  *
35  *
36  */

37 public class PDFPage extends PDFObject implements Serializable
38 {
39   /*
40    * NOTE: The original class is the work of Peter T. Mount, who released it
41    * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
42    * follows:
43    * The package name was changed to gnu.pdf.
44    * The formatting was changed a little bit.
45    * It is still licensed under the LGPL.
46    */

47
48     /**
49      * Specifies that the page is in PORTRAIT orientation.
50      */

51     public static final int PORTRAIT = 0;
52
53     /**
54      * Specifies that the page is in LANDSCAPE orientation.
55      */

56     public static final int LANDSCAPE = 90;
57
58     /**
59      * Specifies that the page is in INVERTEDPORTRAIT orientation.
60      */

61     public static final int INVERTEDPORTRAIT = 180;
62
63     /**
64      * Specifies that the page is in SEASCAPE orientation.
65      */

66     public static final int SEASCAPE = 270;
67
68     /**
69      * Rectangle defining a page in letter format.
70      */

71     public static final Rectangle MEDIA_letter = new Rectangle(0,0,612,792);
72
73     /**
74      * Rectangle defining a page in note format.
75      */

76     public static final Rectangle MEDIA_note = new Rectangle(0,0,540,720);
77
78     /**
79      * Rectangle defining a page in legal format.
80      */

81     public static final Rectangle MEDIA_legal = new Rectangle(0,0,612,1008);
82
83     /**
84      * Rectangle defining a page in a0 format.
85      */

86     public static final Rectangle MEDIA_a0 = new Rectangle(0,0,2380,3368);
87
88     /**
89      * Rectangle defining a page in a1 format.
90      */

91     public static final Rectangle MEDIA_a1 = new Rectangle(0,0,1684,2380);
92
93     /**
94      * Rectangle defining a page in a2 format.
95      */

96     public static final Rectangle MEDIA_a2 = new Rectangle(0,0,1190,1684);
97
98     /**
99      * Rectangle defining a page in a3 format.
100      */

101     public static final Rectangle MEDIA_a3 = new Rectangle(0,0,842,1190);
102
103     /**
104      * Rectangle defining a page in a4 format.
105      */

106     public static final Rectangle MEDIA_a4 = new Rectangle(0,0,595,842);
107
108     /**
109      * Rectangle defining a page in a5 format.
110      */

111     public static final Rectangle MEDIA_a5 = new Rectangle(0,0,421,595);
112
113     /**
114      * Rectangle defining a page in a6 format.
115      */

116     public static final Rectangle MEDIA_a6 = new Rectangle(0,0,297,421);
117
118     /**
119      * Rectangle defining a page in a7 format.
120      */

121     public static final Rectangle MEDIA_a7 = new Rectangle(0,0,210,297);
122
123     /**
124      * Rectangle defining a page in a8 format.
125      */

126     public static final Rectangle MEDIA_a8 = new Rectangle(0,0,148,210);
127
128     /**
129      * Rectangle defining a page in a9 format.
130      */

131     public static final Rectangle MEDIA_a9 = new Rectangle(0,0,105,148);
132
133     /**
134      * Rectangle defining a page in a10 format.
135      */

136     public static final Rectangle MEDIA_a10 = new Rectangle(0,0,74,105);
137
138     /**
139      * Rectangle defining a page in b0 format.
140      */

141     public static final Rectangle MEDIA_b0 = new Rectangle(0,0,2836,4008);
142
143     /**
144      * Rectangle defining a page in b1 format.
145      */

146     public static final Rectangle MEDIA_b1 = new Rectangle(0,0,2004,2836);
147
148     /**
149      * Rectangle defining a page in b2 format.
150      */

151     public static final Rectangle MEDIA_b2 = new Rectangle(0,0,1418,2004);
152
153     /**
154      * Rectangle defining a page in b3 format.
155      */

156     public static final Rectangle MEDIA_b3 = new Rectangle(0,0,1002,1418);
157
158     /**
159      * Rectangle defining a page in b4 format.
160      */

161     public static final Rectangle MEDIA_b4 = new Rectangle(0,0,709,1002);
162
163     /**
164      * Rectangle defining a page in b5 format.
165      */

166     public static final Rectangle MEDIA_b5 = new Rectangle(0,0,501,709);
167
168     /**
169      * Rectangle defining a page in archE format.
170      */

171     public static final Rectangle MEDIA_archE = new Rectangle(0,0,2592,3456);
172
173     /**
174      * Rectangle defining a page in archD format.
175      */

176     public static final Rectangle MEDIA_archD = new Rectangle(0,0,1728,2592);
177
178     /**
179      * Rectangle defining a page in archC format.
180      */

181     public static final Rectangle MEDIA_archC = new Rectangle(0,0,1296,1728);
182
183     /**
184      * Rectangle defining a page in archB format.
185      */

186     public static final Rectangle MEDIA_archB = new Rectangle(0,0,864,1296);
187
188     /**
189      * Rectangle defining a page in archA format.
190      */

191     public static final Rectangle MEDIA_archA = new Rectangle(0,0,648,864);
192
193     /**
194      * Rectangle defining a page in flsa format.
195      */

196     public static final Rectangle MEDIA_flsa = new Rectangle(0,0,612,936);
197
198     /**
199      * Rectangle defining a page in flse format.
200      */

201     public static final Rectangle MEDIA_flse = new Rectangle(0,0,612,936);
202
203     /**
204      * Rectangle defining a page in halfletter format.
205      */

206     public static final Rectangle MEDIA_halfletter = new Rectangle(0,0,396,612);
207
208     /**
209      * Rectangle defining a page in 11x17 format.
210      */

211     public static final Rectangle MEDIA_11x17 = new Rectangle(0,0,792,1224);
212
213     /**
214      * Rectangle defining a page in ledger format.
215      */

216     public static final Rectangle MEDIA_ledger = new Rectangle(0,0,1224,792);
217
218     /**
219      * This is this pages media box, ie the size of the page
220      */

221     protected Rectangle mediabox;
222
223     /**
224      * This is the pages object id that this page belongs to.
225      * It is set by the pages object when it is added to it.
226      */

227     protected PDFObject pdfPageList;
228
229     /**
230      * This holds the contents of the page.
231      */

232     protected Vector contents;
233
234     /**
235      * Specifies the number of degrees the page should be rotated clockwise
236      * when it is displayed. This value must be zero (the default), or a
237      * multiple of 90.
238      * @see #PORTRAIT
239      * @see #LANDSCAPE
240      * @see #INVERTEDPORTRAIT
241      * @see #SEASCAPE
242      */

243     protected int rotate;
244
245     /**
246      * Object ID that contains a thumbnail sketch of the page.
247      * -1 indicates no thumbnail.
248      */

249     protected PDFObject thumbnail;
250
251     /**
252      * This holds any Annotations contained within this page.
253      */

254     protected Vector annotations;
255
256     /**
257      * This holds any resources for this page
258      */

259     protected Vector resources;
260
261     // JM
262
protected Vector imageResources;
263
264     /**
265      * The fonts associated with this page
266      */

267     protected Vector fonts;
268
269     /**
270      * The xobjects or other images in the pdf
271      */

272 // protected Vector xobjects;
273

274     /**
275      * These handle the procset for this page.
276      * Refer to page 140 of the PDF Reference manual
277      * NB: Text is handled when the fonts Vector is null, and a font is created
278      * refer to getFont() to see where it's defined
279      */

280     protected boolean hasImageB,hasImageC,hasImageI;
281     protected procset procset;
282
283     /**
284      * This constructs a Page object, which will hold any contents for this
285      * page.
286      *
287      * <p>Once created, it is added to the document via the PDF.add() method.
288      * (For Advanced use, via the PDFPages.add() method).
289      *
290      * <p>This defaults to a4 media.
291      */

292     public PDFPage()
293     {
294         super("/Page");
295         mediabox = MEDIA_letter;
296         contents = new Vector();
297         rotate = 0;
298         thumbnail = null;
299         annotations = new Vector();
300         resources = new Vector();
301     // JM
302
imageResources = new Vector();
303         fonts = new Vector();
304         procset = null;
305     }
306
307     /**
308      * Constructs a page, using the supplied media size.
309      *
310      * @param mediabox Rectangle describing the page size
311      */

312     public PDFPage(Rectangle mediabox)
313     {
314         this();
315         setMedia(mediabox);
316     }
317
318     /**
319      * Constructs a page using A4 media, but using the supplied orientation.
320      * @param rotate Rotation: 0, 90, 180 or 270
321      * @see #PORTRAIT
322      * @see #LANDSCAPE
323      * @see #INVERTEDPORTRAIT
324      * @see #SEASCAPE
325      */

326     public PDFPage(int rotate)
327     {
328         this();
329         setOrientation(rotate);
330     }
331
332     /**
333      * Constructs a page using the supplied media size and orientation.
334      * @param mediabox Rectangle describing the page size
335      * @param rotate Rotation: 0, 90, 180 or 270
336      * @see #PORTRAIT
337      * @see #LANDSCAPE
338      * @see #INVERTEDPORTRAIT
339      * @see #SEASCAPE
340      */

341     public PDFPage(Rectangle mediabox,int rotate)
342     {
343         this();
344         setMedia(mediabox);
345         setOrientation(rotate);
346     }
347
348     public void addToProcset(String JavaDoc proc) {
349       if (procset == null) {
350     addProcset();
351       }
352       procset.add(proc);
353     }
354
355     /**
356      * This returns a PDFGraphics object, which can then be used to render
357      * on to this page. If a previous PDFGraphics object was used, this object
358      * is appended to the page, and will be drawn over the top of any previous
359      * objects.
360      */

361     public PDFGraphics getGraphics() {
362         try {
363             PDFGraphics g = new PDFGraphics();
364             g.init(this);
365             return g;
366         } catch(Exception JavaDoc ex) {
367             ex.printStackTrace();
368         }
369
370         return null;
371     }
372
373     /**
374      * Returns a PDFFont, creating it if not yet used.
375      * @para type Font type, usually /Type1
376      * @param font Font name
377      * @param style java.awt.Font style, ie Font.NORMAL
378      */

379     public PDFFont getFont(String JavaDoc type,String JavaDoc font,int style) {
380         // Search the fonts on this page, and return one that matches this
381
// font.
382
// This keeps the number of font definitions down to one per font/style
383
for(Enumeration en = fonts.elements(); en.hasMoreElements(); ) {
384             PDFFont ft = (PDFFont) en.nextElement();
385             if(ft.equals(type,font,style))
386                 return ft;
387         }
388
389         // Ok, the font isn't in the page, so create one.
390

391         // We need a procset if we are using fonts, so create it (if not
392
// already created, and add to our resources
393
if(fonts.size()==0) {
394             addProcset();
395             procset.add("/Text");
396         }
397
398         // finally create and return the font
399
PDFFont f = pdfDocument.getFont(type,font,style);
400         fonts.addElement(f);
401         return f;
402     }
403
404     /**
405      * Sets the media size for this page.
406      *
407      * <p>Normally, this should be done when the page is created, to avoid
408      * problems.
409      *
410      * @param mediabox Rectangle describing the page size
411      */

412     public void setMedia(Rectangle mediabox)
413     {
414         this.mediabox = mediabox;
415     }
416
417     /**
418      * Returns the page's media.
419      * @return Rectangle describing the page size in device units (72dpi)
420      */

421     public Rectangle getMedia() {
422         return mediabox;
423     }
424
425   public Dimension getDimension() {
426     Rectangle r = getMedia();
427
428     // if were landscape or seascape, then we swap the dimensions which
429
// should fool existing code.
430
int rot = getOrientation();
431     if(rot==90 || rot==270) {
432       return new Dimension(r.height-r.y,r.width-r.x);
433     }
434     return new Dimension(r.width-r.x,r.height-r.y);
435   }
436
437
438     /**
439      * Sets the page's orientation.
440      *
441      * <p>Normally, this should be done when the page is created, to avoid
442      * problems.
443      *
444      * @param rotate Rotation: 0, 90, 180 or 270
445      */

446     public void setOrientation(int rotate)
447     {
448         this.rotate = rotate - (rotate%90); // must be modulus of 90
449
}
450
451     /**
452      * Returns the pages orientation
453      * @see #PORTRAIT
454      * @see #LANDSCAPE
455      * @see #INVERTEDPORTRAIT
456      * @see #SEASCAPE
457      * @return current rotation of the page
458      */

459     public int getOrientation() {
460         return rotate;
461     }
462
463     /**
464      * This adds an object that describes some content to this page.
465      *
466      * <p><b>Note:</b> Objects that describe contents must be added using this
467      * method _AFTER_ the PDF.add() method has been called.
468      *
469      * @param ob PDFObject describing some contents
470      */

471     public void add(PDFObject ob) {
472         contents.addElement(ob);
473     }
474
475     /**
476      * This adds an Annotation to the page.
477      *
478      * <p>As with other objects, the annotation must be added to the pdf
479      * document using PDF.add() before adding to the page.
480      *
481      * @param ob Annotation to add.
482      */

483     public void addAnnotation(PDFObject ob) {
484         annotations.addElement(ob);
485     }
486
487     /**
488      * This method adds a text note to the document.
489      * @param note Text of the note
490      * @param x Coordinate of note
491      * @param y Coordinate of note
492      * @param w Width of the note
493      * @param h Height of the note
494      * @return Returns the annotation, so other settings can be changed.
495      */

496     public PDFAnnot addNote(String JavaDoc note,int x,int y,int w,int h) {
497         int xy1[] = cxy(x,y+h);
498         int xy2[] = cxy(x+w,y);
499         PDFAnnot ob = new PDFAnnot(xy1[0],xy1[1],
500                                    xy2[0],xy2[1],
501                                    note);
502         pdfDocument.add(ob);
503         annotations.addElement(ob);
504         return ob;
505     }
506
507     /**
508      * Adds a hyperlink to the document.
509      * @param x Coordinate of active area
510      * @param y Coordinate of active area
511      * @param w Width of the active area
512      * @param h Height of the active area
513      * @param dest Page that will be displayed when the link is activated. When
514      * displayed, the zoom factor will be changed to fit the display.
515      * @return Returns the annotation, so other settings can be changed.
516      */

517     public PDFAnnot addLink(int x,int y,int w,int h,PDFObject dest) {
518         int xy1[] = cxy(x,y+h);
519         int xy2[] = cxy(x+w,y);
520         PDFAnnot ob = new PDFAnnot(xy1[0],xy1[1],
521                                    xy2[0],xy2[1],
522                                    dest
523                                    );
524         pdfDocument.add(ob);
525         annotations.addElement(ob);
526         return ob;
527     }
528
529     /**
530      * Adds a hyperlink to the document.
531      * @param x Coordinate of active area
532      * @param y Coordinate of active area
533      * @param w Width of the active area
534      * @param h Height of the active area
535      * @param dest Page that will be displayed when the link is activated
536      * @param view Rectangle defining what part of the page should be displayed
537      * (defined in Java coordinates). If this is null, then the page is fitted to
538      * the display.
539      * @return Returns the annotation, so other settings can be changed.
540      */

541     public PDFAnnot addLink(int x,int y,int w,int h,
542                             PDFObject dest,
543                             int vx,int vy,int vw,int vh) {
544         int xy1[] = cxy(x,y+h);
545         int xy2[] = cxy(x+w,y);
546         int xy3[] = cxy(vx,vy+vh);
547         int xy4[] = cxy(vx+vw,vy);
548         PDFAnnot ob = new PDFAnnot(xy1[0],xy1[1],
549                                    xy2[0],xy2[1],
550                                    dest,
551                                    xy3[0],xy3[1],
552                                    xy4[0],xy4[1]
553                                    );
554         pdfDocument.add(ob);
555         annotations.addElement(ob);
556         return ob;
557     }
558
559     /** Contains the text strings for the xobjects. */
560     private Vector xobjects = new Vector();
561     /**
562      * This adds an XObject resource to the page.
563      * The string should be of the format /Name ObjectNumber RevisionNumber R as in /Image1 13 0 R .
564      */

565     public void addXObject(String JavaDoc inxobject){
566         xobjects.addElement(inxobject);
567     }
568
569     /**
570      * This adds a resource to the page.
571      * @param resource String defining the resource
572      */

573     public void addResource(String JavaDoc resource) {
574         resources.addElement(resource);
575     }
576
577     // JM
578
public void addImageResource(String JavaDoc resource) {
579         imageResources.addElement(resource);
580     }
581
582     /**
583      * This adds an object that describes a thumbnail for this page.
584      * <p><b>Note:</b> The object must already exist in the PDF, as only the
585      * object ID is stored.
586      * @param thumbnail PDFObject containing the thumbnail
587      */

588     public void setThumbnail(PDFObject thumbnail)
589     {
590         this.thumbnail = thumbnail;
591     }
592
593     /**
594      * This method attaches an outline to the current page being generated. When
595      * selected, the outline displays the top of the page.
596      * @param title Outline title to attach
597      * @return PDFOutline object created, for addSubOutline if required.
598      */

599     public PDFOutline addOutline(String JavaDoc title) {
600         PDFOutline outline = new PDFOutline(title,this);
601         pdfDocument.add(outline);
602         pdfDocument.getOutline().add(outline);
603         return outline;
604     }
605
606     /**
607      * This method attaches an outline to the current page being generated.
608      * When selected, the outline displays the top of the page.
609      *
610      * <p>Note: If the outline is not in the top level (ie below another
611      * outline) then it must <b>not</b> be passed to this method.
612      *
613      * @param title Outline title to attach
614      * @param l Left coordinate of region
615      * @param b Bottom coordinate of region
616      * @param r Right coordinate of region
617      * @param t Top coordinate of region
618      * @return PDFOutline object created, for addSubOutline if required.
619      */

620     public PDFOutline addOutline(String JavaDoc title,int x,int y,int w,int h) {
621         int xy1[] = cxy(x,y+h);
622         int xy2[] = cxy(x+w,y);
623         PDFOutline outline = new PDFOutline(title,this,
624                                             xy1[0],xy1[1],
625                                             xy2[0],xy2[1]);
626         pdfDocument.add(outline);
627         pdfDocument.getOutline().add(outline);
628         return outline;
629     }
630
631     /**
632      * @param os OutputStream to send the object to
633      * @exception IOException on error
634      */

635     public void write(OutputStream os) throws IOException
636     {
637         // Write the object header
638
writeStart(os);
639
640         // now the objects body
641

642         // the /Parent pages object
643
os.write("/Parent ".getBytes());
644         os.write(pdfPageList.toString().getBytes());
645         os.write("\n".getBytes());
646
647         // the /MediaBox for the page size
648
os.write("/MediaBox [".getBytes());
649         os.write(Integer.toString(mediabox.x).getBytes());
650         os.write(" ".getBytes());
651         os.write(Integer.toString(mediabox.y).getBytes());
652         os.write(" ".getBytes());
653         os.write(Integer.toString(mediabox.x+mediabox.width).getBytes());
654         os.write(" ".getBytes());
655         os.write(Integer.toString(mediabox.y+mediabox.height).getBytes());
656         os.write("]\n".getBytes());
657
658         // Rotation (if not zero)
659
if(rotate!=0) {
660             os.write("/Rotate ".getBytes());
661             os.write(Integer.toString(rotate).getBytes());
662             os.write("\n".getBytes());
663         }
664
665         // Now the resources
666
os.write("/Resources << ".getBytes());
667         // fonts
668
if(fonts.size()>0) {
669           //os.write("/Font << ".getBytes());
670
os.write("\n/Font << ".getBytes());
671             for(Enumeration en=fonts.elements();en.hasMoreElements();) {
672                 PDFFont font = (PDFFont)en.nextElement();
673                 os.write(font.getName().getBytes());
674                 os.write(" ".getBytes());
675                 os.write(font.toString().getBytes());
676                 os.write(" ".getBytes());
677             }
678             os.write(">> ".getBytes());
679         }
680         // Now the XObjects
681
if (xobjects.size() > 0){
682             os.write("\n/XObject << ".getBytes());
683             for(Enumeration en=xobjects.elements();en.hasMoreElements();) {
684                 os.write(en.nextElement().toString().getBytes());
685                 os.write(" ".getBytes());
686             }
687             os.write(">> ".getBytes());
688         }
689         // Any other resources
690
for(Enumeration en=resources.elements();en.hasMoreElements();) {
691       String JavaDoc str = en.nextElement().toString();
692             os.write(str.getBytes());
693             os.write(" ".getBytes());
694         }
695     // JM
696
if(imageResources.size() > 0) {
697       os.write("/XObject << ".getBytes());
698       for(Enumeration en=imageResources.elements();en.hasMoreElements();) {
699         String JavaDoc str = en.nextElement().toString();
700             os.write(str.getBytes());
701             os.write(" ".getBytes());
702       }
703       os.write(" >> ".getBytes());
704     }
705         os.write(">>\n".getBytes());
706
707         // The thumbnail
708
if(thumbnail!=null) {
709             os.write("/Thumb ".getBytes());
710             os.write(thumbnail.toString().getBytes());
711             os.write("\n".getBytes());
712         }
713
714         // the /Contents pages object
715
if(contents.size()>0) {
716             if(contents.size()==1) {
717                 PDFObject ob = (PDFObject)contents.elementAt(0);
718                 os.write("/Contents ".getBytes());
719                 os.write(ob.toString().getBytes());
720                 os.write("\n".getBytes());
721             } else {
722                 os.write("/Contents [".getBytes());
723                 os.write(PDFObject.toArray(contents).getBytes());
724                 os.write("\n".getBytes());
725             }
726         }
727
728         // The /Annots object
729
if(annotations.size()>0) {
730             os.write("/Annots ".getBytes());
731             os.write(PDFObject.toArray(annotations).getBytes());
732             os.write("\n".getBytes());
733         }
734
735         // finish off with its footer
736
writeEnd(os);
737     }
738
739     /**
740      * This creates a procset and sets up the page to reference it
741      */

742     private void addProcset() {
743         if(procset==null) {
744             pdfDocument.add(procset = new procset());
745             resources.addElement("/ProcSet "+procset);
746         }
747     }
748
749     /**
750      * This defines a procset
751      */

752     public class procset extends PDFObject {
753         private Vector set;
754
755         public procset() {
756             super(null);
757             set = new Vector();
758
759             // Our default procset (use addElement not add, as we dont want a
760
// leading space)
761
set.addElement("/PDF");
762         }
763
764         /**
765          * @param proc Entry to add to the procset
766          */

767         public void add(String JavaDoc proc) {
768             set.addElement(" "+proc);
769         }
770
771         /**
772          * @param os OutputStream to send the object to
773          * @exception IOException on error
774          */

775         public void write(OutputStream os) throws IOException {
776             // Write the object header
777
//writeStart(os);
778

779             os.write(Integer.toString(objser).getBytes());
780             os.write(" 0 obj\n".getBytes());
781
782             // now the objects body
783
os.write("[".getBytes());
784             for(Enumeration en = set.elements(); en.hasMoreElements(); )
785                 os.write(en.nextElement().toString().getBytes());
786             os.write("]\n".getBytes());
787
788             // finish off with its footer
789
//writeEnd(os);
790
os.write("endobj\n".getBytes());
791         }
792     }
793
794     /**
795      * This utility method converts the y coordinate from Java to User space
796      * within the page.
797      * @param y Coordinate in Java space
798      * @return y Coordinate in User space
799      */

800     public int cy(int x,int y) {
801         return cxy(x,y)[1];
802     }
803
804     /**
805      * This utility method converts the y coordinate from Java to User space
806      * within the page.
807      * @param y Coordinate in Java space
808      * @return y Coordinate in User space
809      */

810     public int cx(int x,int y) {
811         return cxy(x,y)[0];
812     }
813
814     /**
815      * This utility method converts the Java coordinates to User space
816      * within the page.
817      * @param x Coordinate in Java space
818      * @param y Coordinate in Java space
819      * @return array containing the x & y Coordinate in User space
820      */

821     public int[] cxy(int x,int y) {
822         int nx = x, ny = y;
823         int mw = mediabox.width, mh = mediabox.height;
824
825         switch(rotate)
826             {
827             case 0:
828                 // Portrait
829
//nx = x;
830
ny = mh - y;
831                 break;
832
833             case 90:
834                 // Landscape
835
nx = y;
836                 ny = x;
837                 break;
838
839             case 180:
840                 // Inverse Portrait
841
nx = mw - x;
842                 //ny = y;
843
break;
844
845             case 270:
846                 // Seascape
847
nx = mw - y;
848                 ny = mh - x;
849                 break;
850             }
851
852         int r[] = new int[2];
853         r[0] = nx;
854         r[1] = ny;
855         return r;
856     }
857
858 }
859
Popular Tags