PSDriver.h

Go to the documentation of this file.
00001 //# PSDriver.h: Defines PSDriver, a low-level PostScript interface for the DL.
00002 //# Copyright (C) 1999,2000,2001,2002,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id$
00027 
00028 #ifndef TRIALDISPLAY_PSDRIVER_H
00029 #define TRIALDISPLAY_PSDRIVER_H
00030 
00031 #include <casa/aips.h>
00032 #include <display/Display/DisplayEnums.h>
00033 #include <casa/BasicSL/String.h>
00034 
00035 //# Forward declarations
00036 #include <casa/iosfwd.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 // <summary>
00041 // Low level interface between PSPixelCanvas{ColorTable} and PostScript.
00042 // </summary>
00043 
00044 // <use visibility=local>
00045 
00046 //<a name="Synopsis"></a>
00047 // <synopsis>
00048 // PSDriver takes graphics requests from
00049 // <linkto class="PSPixelCanvas"> PSPixelCanvas </linkto> and
00050 // <linkto class="PSPixelCanvasColorTable"> PSPixelCanvasColorTable </linkto>
00051 // and generates PostScript code. It might be thought of as playing an
00052 // analogous role to what Xlib does for the X11PixelCanvas{ColorTable}.
00053 // It could be used separately, but mostly programers will just
00054 // create a PSDriver object to pass to PSPixelCanvas{ColorTable}.
00055 //
00056 // While there are many constructors, PSDrivers will probably most frequently
00057 // be created using something like:
00058 //
00059 // <srcblock>
00060 //      PSdriver *ps = new PSDriver("filename", PSDriver::LETTER,
00061 //                                  PSDriver::PORTRAIT);
00062 // </srcblock>
00063 //
00064 // If it were desired to use RGB colormode rather than INDEXED,
00065 // the following could be added.
00066 //
00067 //<srcblock>
00068 //      ps->setColorSpace(PSDriver::RGB);
00069 //</srcblock>
00070 //
00071 // PSDriver is logically split into two sections. The 'upper' level
00072 // contains public functions, such as drawLine(). These do various
00073 // amounts of bookkeeping, such as bounding box calculations, then call
00074 // the 'lower' level routines, typically with names starting with emit.
00075 // (eg. emitLine() in this example).
00076 //
00077 // There are many public routines that will rarely be called from outside
00078 // of PSDriver. Typical of these are the various xxxToAscii85 routines
00079 // that are used internally to generate image data.
00080 
00081 // </synopsis>
00082 //
00083 // <thrown>
00084 // PSDriver does not currently explicitly throw any exceptions.
00085 // </thrown>
00086 //
00087 //<a name="ToDo"></a>
00088 // <todo>
00089 // <ol>
00090 // <li> EPS support is preliminary.
00091 // <li> Vertical text justification doesn't look correct, presumably because
00092 //      the character's bounding box includes interline space.
00093 // <li> No display list support.
00094 // <li> Clipping support is disabled since it is currently possible to
00095 //      only reduce the size of the clipping path, not replace it.
00096 // <li> Points are drawn as filled circles of fixed size.
00097 // </ol>
00098 // </todo>
00099 //
00100 // Notes:
00101 // <ol>
00102 // <li> It is possible to switch between Index, RGB and HSV modes at will.
00103 // <li> The length of the color table is 4096 entries.
00104 // <li> PostScript doesn't deal with pixels.
00105 // <li> The transform matrix affects virtually everything.
00106 // <li> Font names must be known by the PostScript interpreter.
00107 // </ol>
00108 
00109         class PSDriver {
00110         public:
00111                 // The various color spaces PSDriver knows about.
00112                 enum ColorSpace { UNKNOWNSPACE=-1, INDEXED, GRAY, RGB, HSV, HSB=HSV};
00113                 // Different line types that can be drawn.
00114                 enum LineStyle { UNKNOWNSTYPE=-1, SOLID, DASHED, DASHDASH};
00115                 // Constants describing the the length of the indexed color table and the number of bits per color component.
00116                 enum {NUMCOLORS = 4096, BITSPERCOMPONENT=12};
00117                 enum Dimension { POINTS, INCHES, MM};
00118                 enum Layout {   PORTRAIT=1, LANDSCAPE=2, EPS=0x4, EPS_PORTRAIT=0x5,
00119                                 EPS_LANDSCAPE=0x6
00120                             };
00121 
00122 //<srcblock>
00123                 //              Options for drawImage.
00124                 // SMOOTH        Pixels are interpolated. Not supported by all
00125                 //              interpreters. Old versions of ghostscript may core
00126                 //              dump if this is selected.
00127 //</srcblock>
00128                 enum ImageOptions { NOIMGOPTS=0, SMOOTH=0x1};
00129 
00130                 enum MediaSize {
00131                     // USERPAGE is used internally. It is not a valid media type.
00132                     USERPAGE=-1,
00133                     // The following lists the known media types (paper sizes).
00134                     LETTER=0, NA_LETTER=LETTER, LEGAL, NA_LEGAL=LEGAL,
00135                     NA_10X13_ENVELOPE, NA_9X12_ENVELOPE,
00136                     NA_NUMBER_10_ENVELOPE, NA_7X9_ENVELOPE, NA_9X11_ENVELOPE,
00137                     NA_10X14_ENVELOPE, NA_6X9_ENVELOPE, NA_10X15_ENVELOPE,
00138                     A, B, C, D, E,
00139                     ISO_A0, ISO_A1, ISO_A2, ISO_A3, ISO_A4, A4=ISO_A4, ISO_A5, ISO_A6,
00140                     ISO_A7, ISO_A8, ISO_A9, ISO_A10, ISO_B0, ISO_B1, ISO_B2, ISO_B3,
00141                     ISO_B4, ISO_B5, ISO_B6, ISO_B7, ISO_B8, ISO_B9, ISO_B10, ISO_C0,
00142                     ISO_C1, ISO_C2, ISO_C3, ISO_C4, ISO_C5, ISO_C6, ISO_C7, ISO_C8,
00143                     ISO_DESIGNATED,
00144                     JIS_B0, JIS_B1, JIS_B2, JIS_B3, JIS_B4, JIS_B5, JIS_B6, JIS_B7,
00145                     JIS_B8, JIS_B9, JIS_B10
00146                 };
00147 //
00148                 enum TextAlign {
00149                     AlignCenter = Display::AlignCenter,
00150                     AlignLeft = Display::AlignLeft,
00151                     AlignTop = Display::AlignTop,
00152                     AlignRight = Display::AlignRight,
00153                     AlignBottom = Display::AlignBottom,
00154                     AlignTopLeft = Display::AlignTopLeft,
00155                     AlignTopRight = Display::AlignTopRight,
00156                     AlignBottomLeft = Display::AlignBottomLeft,
00157                     AlignBottomRight = Display::AlignBottomRight
00158                 };
00159 
00160 //      PSInfo is used to supply comment fields for the PostScript header
00161 //      and other information that needs to be available when the PSDriver
00162 //      object is created. Its use is optional.
00163                 class PSInfo {
00164                 public:
00165                         PSInfo();
00166                         ~PSInfo();
00167                         void Creator(const char *);
00168                         void Creator(const String &);
00169                         const String &Creator()const {
00170                                 return creator_;
00171                         }
00172                         void For(const char *);
00173                         void For(const String &);
00174                         const String &For()const {
00175                                 return for_;
00176                         }
00177                         void Title(const char *);
00178                         void Title(const String &);
00179                         const String &Title()const {
00180                                 return title_;
00181                         }
00182                         void Comment(const String &);
00183                         void Comment(const char *);
00184                         const String &Comment()const {
00185                                 return comment_;
00186                         }
00187 
00188                         void setMargins(const float lm, const float rm,
00189                                         const float tm, const float bm,
00190                                         const Dimension dim= PSDriver::INCHES);
00191                         // If setMargins has been called, returns True and the
00192                         // margin values. Otherwise, returns False and does not
00193                         // change the arguments.
00194                         Bool getMargins(float &lm, float &rm,
00195                                         float &tm, float &bm);
00196                 private:
00197                         String  for_;
00198                         String  title_;
00199                         String  creator_;
00200                         String  comment_;
00201                         float   lm_, rm_, tm_, bm_;     // Margins.
00202                         Bool    haveMargins_;           // setMargins has been called.
00203                 };
00204                 // Generic PostScript
00205 
00206         public:
00207 // Arguments to the various constructors:
00208 // <ul>
00209 // <li> out             is an ostream that the PostScript code is written to.
00210 // <li> fname           it the name of the output file to be written to.
00211 // <li> MediaSize       is the paper size. Each paper size has default margins.
00212 // <li> Dimension       is POINTS, INCHES or MM. Describes values of x0,y0,x1,y1.
00213 // <li> x0,y0,x1,y1     are used instead of MediaSize to explicitly give
00214 //                      the lower left and upper right corners of the
00215 //                      drawing surface.
00216 // <li> Layout          is PORTRAIT or LANDSCAPE.
00217 // <li> PSInfo          supplies optional header comments or margins.
00218 //                      Optional argument.
00219 // </ul>
00220 //
00221                 // INDEXED, PORTRAIT to a default file.
00222                 PSDriver();
00223                 PSDriver(ostream &out);
00224                 PSDriver(const char *filename);
00225                 PSDriver(ostream &out, const MediaSize, const Layout=PORTRAIT,
00226                          PSInfo *info=NULL);
00227                 PSDriver(const String &fname, const MediaSize, const Layout=PORTRAIT,
00228                          PSInfo *info=NULL);
00229                 PSDriver(const char *fname, const MediaSize, const Layout=PORTRAIT,
00230                          PSInfo *info=NULL);
00231 
00232                 PSDriver(ostream &out, const Dimension dim,
00233                          const float x0, const float y0,
00234                          const float x1, const float y1,
00235                          const Layout=PORTRAIT, PSInfo *info=NULL);
00236                 PSDriver(const String &outname, const Dimension dim,
00237                          const float x0, const float y0,
00238                          const float x1, const float y1,
00239                          const Layout=PORTRAIT, PSInfo *info=NULL);
00240                 PSDriver(const String &outname, const MediaSize,
00241                          const Dimension dim,
00242                          const float x0, const float y0,
00243                          const float x1, const float y1,
00244                          const Layout=PORTRAIT, PSInfo *info=NULL);
00245                 PSDriver(const char *outname, const Dimension dim,
00246                          const float x0, const float y0,
00247                          const float x1, const float y1,
00248                          const Layout=PORTRAIT, PSInfo *info=NULL);
00249                 ~PSDriver();
00250 
00251 //#     ////////////////////////////////////////////////////////////////
00252 //
00253                 // Set/get desired color space. (Default is INDEXED).
00254 //<group>
00255                 void setColorSpace(const ColorSpace);
00256                 inline ColorSpace colorSpace()const {
00257                         return colorSpace_;
00258                 }
00259 //</group>
00260                 // Set current color used for text and vectors. Only the first
00261                 // argument is used for indexed & gray.
00262                 // For Indexed mode, color should be in the range (0..ncolors-1)
00263                 // where ncolors is the number of colors in the table. color
00264                 // is truncated to an integer.
00265                 // For the others, color should be in the range (0..1).
00266                 void setColor(const float rh, const float gs, const float bv);
00267                 // Set color value for indexed and gray scale modes.
00268                 // If called when not in indexed or gray modes, the same value
00269                 // is used for all three components.
00270                 void setColor(const float color);
00271                 // Color to be used for dashes.
00272                 void setBackgroundColor(const float rh, const float gs,
00273                                         const float bv, const ColorSpace=RGB);
00274                 // Set color value for indexed and gray scale modes.
00275                 void setBackgroundColor(const float color, const ColorSpace=INDEXED);
00276 
00277                 // Change color space then set color.
00278                 void setIndexColor(const float indx);
00279                 void setRGBColor(const float r, const float g, const float b);
00280                 void setHSVColor(const float h, const float s, const float v) {
00281                         setHSBColor(h, s, v);
00282                 }
00283                 void setHSBColor(const float h, const float s, const float b);
00284 
00285                 // Load color tables with contents of a, b & c. Typically, a holds
00286                 // red, b holds green & c holds blue. Values should be in the
00287                 // range (0..1).
00288                 // May be called anytime, but values are only used in INDEXED mode.
00289                 // Start is starting index in the color table. Len is the
00290                 // number of entries to set.
00291                 void storeColors(const int start, const int len,
00292                                  const float *r, const float *g, const float *b);
00293                 // Scatter load the colortable. Much more effecient if indexes are
00294                 // consecutive.
00295                 void storeColorValues(const int len, const int *indexes,
00296                                       const float *a, const float *b, const float *c);
00297                 // Store 1 color.
00298                 void storeColor(const int index,
00299                                 const float r, const float g, const float b);
00300                 // Load linear ramps (0..1.0) into the first ncolors entries.
00301                 void setLinearRamps(const int ncolors);
00302 //#     ////////////////////////////////////////////////////////////////
00303 //#     //              Coordinate commands.
00304                 // Current transform matrix is changed by the given values, not
00305                 // replaced.
00306 // <group>
00307                 // Change origin to x/y.
00308                 void translate(const float x, const float y);
00309                 // Change scale by x/y.
00310                 void scale(const float x, const float y);
00311                 // Rotate by # degrees.
00312                 void rotate(const float degrees);
00313 // </group>
00314 
00315                 // Set the length of dashes.
00316                 void setDashLength(const float length);
00317         public:
00318 //#     ////////////////////////////////////////////////////////////////
00319 //<srcblock>
00320                 //                      Draw an Image
00321                 // x0,y0        Image is drawn with lower left corner at x0, y0.
00322                 //              (In current user coordinates).
00323                 // width,height Size of source image in elements.
00324                 // xsize,ysize  Size of output image. (Eg. If current user
00325                 //              coordinates were in inches, a size of 2 would create
00326                 //              a two inch output square regardless of the size of the
00327                 //              input image).
00328                 // data         Data for an image that is width pixels wide by
00329                 //              height pixels high. The actual length of the array
00330                 //              depends on the ColorSpace argument:
00331                 //              INDEXED:        The array contains width*height
00332                 //                              indexes in the range 0.. 2^bpc -1.
00333                 //                              (0..255 or 0..4095).
00334                 //              RGB or HSV:     The array contains width*height
00335                 //                              RGB (HSV) triples in the range
00336                 //                              0..255 for bpc=8 or 0..1023 otherwise.
00337                 //                              The length of the array is
00338                 //                              width*height*3.
00339                 // bpc          Used to determing how data is encoded.
00340                 //              If (0 < bpc <= 8) 8 bit encoding is used.
00341                 //              If bpc > 8, 12 bit encoding is used.
00342                 //              If bpc <= 0, the array is scanned to determine the
00343                 //              encoding method.
00344                 // ColorSpace   Whether image is INDEXED, RGB or HSV.
00345                 // imageopts    Bitwise OR of IMAGEOPTS.
00346                 //               Currently only whether to smooth image.
00347 //</srcblock>
00348 
00349                 void drawImage( const float x0, const float y0,
00350                                 const int width, const int height,
00351                                 const float xsize, const float ysize,
00352                                 const uShort *data,
00353                                 const int bpc=0, const ColorSpace=INDEXED,
00354                                 const int imageopts=0);
00355 
00356                 // Save/restore graphics state.
00357 //<group>
00358                 void gsave();
00359                 void grestore();
00360 //</group>
00361 //
00362 //<srcblock>
00363                 //                       Graphics
00364 //</srcblock>
00365 //
00366 
00367                 void setLineStyle(const LineStyle);
00368                 // w = 1.0 means 0.005".
00369                 void lineWidth(const float w);
00370 
00371                 void moveTo(const float x, const float y);
00372                 void lineTo(const float x, const float y, const int stroke=1);
00373 
00374                 void drawPolygon(const int len, const float *x, const float *y,
00375                                  const int fill=0);
00376                 void drawPolyline(const int len, const float *x, const float *y,
00377                                   const int close=0, const int fill=0);
00378                 void drawLine(  const float x0, const float y0,
00379                                 const float x1, const float y1);
00380                 void drawLines(const int len,   const float *x1, const float *y1,
00381                                const float *x2, const float *y2);
00382                 void drawRectangle(const float x0, const float y0,
00383                                    const float x1, const float y1, const int fill=0);
00384                 void drawFilledRectangle(const float x0, const float y0,
00385                                          const float x1, const float y1);
00386                 void drawPoint( const float x, const float y, const float radius=1.0);
00387                 void drawPoints(const int len, const float *x, const float *y,
00388                                 const float radius=1.0);
00389                 void drawColoredPoints( const int len, const float *x, const float *y,
00390                                         const float *colors, const float radius=1.0);
00391 
00392 
00393 //#     ////////////////////////////////////////////////////////////////
00394 //
00395 //<srcblock>
00396                 //                              Text
00397 //</srcblock>
00398 //#
00399 
00400 //   Display a line of text. Tabs, newlines, etc. are not handled.
00401                 void drawText(const float x, const float y, const char *str,
00402                               const TextAlign algn=AlignBottomLeft,
00403                               const float angle = 0);
00404 //<note role=caution> PSDriver just passes the name to the output file.
00405 //      It can't know whether the font really exists on the output device.
00406 //</note>
00407                 // Set font using current font scaling.
00408                 void setFont(const char *fn);
00409                 // Use font with supplied scaling.
00410                 void findFont(const char *fn, const float scale);
00411 
00412                 // Set/Get font scaling used with setFont.
00413                 void setDefaultFontScale(const float scl);
00414                 float getDefaultFontScale()const {
00415                         return defaultFontSize_;
00416                 }
00417 //
00418 //#     ////////////////////////////////////////////////////////////////
00419 //<srcblock>
00420                 //                              Misc.
00421 //</srcblock>
00422 //
00423                 // Return page size (drawable area - margins).
00424                 // If userCoords is True, values are in current user coordinates.
00425                 // Otherwise, they are in transformed coordinates ( points).
00426                 void pageSize(float &width, float &height,
00427                               const Bool userCoords=True);
00428 
00429                 // Return bounding box.
00430                 // Return is True if a bounding box was declared initially. Otherwise,
00431                 // False and the current value of the bounding box will be returned.
00432                 // If userCoords is True, values are in current user coordinates.
00433                 // Otherwise, transformed coords.
00434                 Bool getBoundingBox( float &x0, float &y0, float &x1, float &y1,
00435                                      const Bool userCoords=True)const;
00436 
00437                 // Returns True if bounding box size was given to the constructor.
00438                 inline Bool haveBoundingBox()const {
00439                         return haveBoundingBox_;
00440                 }
00441 
00442                 // Put a comment in the output file.
00443                 // The comment may include newlines.
00444 //<group>
00445                 void comment(const char *);
00446                 void comment(const String &);
00447 //</group>
00448                 // Push/pop transform matrix.
00449 //<group>
00450                 void pushMatrix();
00451                 void popMatrix();
00452 //</group>
00453                 void newPage();
00454                 // Flush the output.
00455                 void flush();
00456                 // Done. Called by the destructor.
00457                 void finish();
00458                 // Set clipping rectangle.
00459                 // Subsequent calls can only make the clipping rectangle smaller.
00460                 void clipRect(  const float x0, const float y0,
00461                                 const float width, const float height);
00462         public:
00463 //
00464 //<srcblock>
00465 //              The following, while public, will almost will almost
00466 // never be called from outside of PSDriver.
00467 //</srcblock>
00468 
00469                 // Routines to encode non negative binary numbers in ASCII85 format.
00470                 // There will be upto 5/4 as many output characters as input bytes.
00471                 // Returns the number of bytes written to out.
00472 
00473                 static int bytesToAscii85(const char *in, const int inlength,
00474                                           char *out);
00475                 static int uShorts8ToAscii85(const uShort *in,
00476                                              const int inlength, char *out);
00477 
00478 
00479                 // Converts in to ASCII85 bytes using only lower 12 bits of each
00480                 // integer. Out must be at least 1.75*inlength rounded up to
00481                 // a multiple of 5 bytes long.
00482                 // ( <1.5bytes/entry>*<inlength entries>*<1.25 expansion>).
00483                 // If inlength is odd, a 0 valued entry will be implicitly added.
00484                 static int uShorts12ToAscii85(const uShort *in,
00485                                               const int width,
00486                                               const int height, char *out);
00487 
00488                 // Encode data as ASCII85 chars. Function return is an array of
00489                 // chars. (use delete [] to free). outlen is set to the number of
00490                 // bytes in the array.
00491                 // On input, if bpc is 8, data is treated as containing 1 byte per
00492                 // element. If bpc is 12, data is assumed to contain 12 bits/element.
00493                 // If bpc is 0, the array is scanned.
00494                 // On return, bpc is set the the value actually used.
00495                 static char *encodeUShorts(const int width, const int height,
00496                                            int &bpc, const uShort *data,
00497                                            int &outlen);
00498 
00499 //#     ////////////////////////////////////////////////////////////////
00500                 // Miscellaneous functions to convert between typical scalings.
00501                 static inline float pointsToInches(const float in) {
00502                         return in/72.0;
00503                 }
00504                 static inline float pointsToMM(const float in) {
00505                         return in*(25.4/72.0);
00506                 }
00507                 static inline float inchesToPoints(const float in) {
00508                         return in*72.0;
00509                 }
00510                 static inline float mmToPoints(const float in) {
00511                         return in*(72./25.4);
00512                 }
00513 
00514                 static float toPoints(const float in, const Dimension indem);
00515                 static float fromPoints(const float in, const Dimension indem);
00516 //#     ////////////////////////////////////////////////////////////////
00517 
00518                 // Do forward or reverse transformation on a point.
00519                 // If absolute is False, the translations aren't done.
00520                 void toPoints(const float xin, const float yin,
00521                               float &xout, float &yout,
00522                               const Bool absolute=True)const {
00523                         state_.toPoints(xin, yin, xout, yout, absolute);
00524                 }
00525 
00526                 void fromPoints(const float xin, const float yin,
00527                                 float &xout, float &yout,
00528                                 const Bool absolute=True)const {
00529                         state_.fromPoints(xin, yin, xout, yout, absolute);
00530                 }
00531 
00532 
00533                 // Return a string representation of a dimension.
00534                 static const char *dimensionToString(const PSDriver::Dimension dim);
00535                 const char *dimensionToString()const {
00536                         return dimensionToString(dimension_);
00537                 }
00538                 Dimension dimension()const {
00539                         return dimension_;
00540                 }
00541 
00542                 // Is the output EPS? Portrait?
00543 //<group>
00544                 Bool isEPS()const {
00545                         return eps_;
00546                 }
00547                 Bool isPortrait()const {
00548                         return portrait_;
00549                 }
00550 //</group>
00551 
00552                 // The PageInfo struct is used internally to supply descriptions
00553                 // of the various page types.
00554                 typedef struct PageInfo {
00555                         PSDriver::MediaSize     media;          // A4, LETTER, etc.
00556                         float                   width;
00557                         float                   height;
00558                         float                   lrmargin;
00559                         float                   tbmargin;
00560                         PSDriver::Dimension     dimension;      // MM or INCHES.
00561                         const char                      *name; //"North American letter", etc.
00562                         const char                      *alias1;        // "NA_LETTER", etc.
00563                         const char                      *alias2;        // "LETTER", etc.
00564                 } pageinfo_;
00565                 // Given a paper type, return a pointer to its description.
00566                 static const PSDriver::PageInfo *pageInfo(const PSDriver::MediaSize);
00567                 // Return page description for index indx.
00568                 static const PSDriver::PageInfo *getPageInfo(const int indx);
00569                 // Lookup for a page description using the name field.
00570                 static const PSDriver::PageInfo *lookupPageInfo(const char *name);
00571                 static int numPageTypes();                      // # of page types.
00572         private:
00573 //<srcblock>
00574                 // Similar to the public drawImage but uses a transform matrix instead of
00575                 // position and scaling.
00576                 // imagematrix          6 element array that defines transformation
00577                 //                      from user space to image space:
00578                 //                      m = [ a b c d tx ty ]
00579                 //                       x' = ax + cy + tx
00580                 //                       y' = bx + dy + ty
00581                 //      The transformation is such that [ width 0 0 height 0 0] will
00582                 //      map the image into a unit square which would then be scaled
00583                 //      by the current scale() values.
00584                 //      To invert the image, use: [ width 0 0 -height 0 height].
00585                 //      If imagematrix is NULL, the image will be mapped into the
00586                 //      unit square.
00587                 // bpc is as above.
00588                 // If smooth is 0, no smoothing is done. Otherwise, pixels are
00589                 // interpolated.
00590                 // Caution: This routine bypasses boundary box checks! If
00591                 // BB checks are not important, then this routine could be public.
00592 //</srcblock>
00593 #if 0
00594                 void drawImage(const float imagematrix[6],
00595                                const int width, const int height,
00596                                const uShort *data,
00597                                const int bpc=-1, const ColorSpace cs=INDEXED,
00598                                const int smooth=0);
00599 #endif
00600 //#     ////////////////////////////////////////////////////////////////
00601                 void newPath();         // These are used internally.
00602                 void stroke();
00603                 void closePath();
00604 //#     ////////////////////////////////////////////////////////////////
00605                 void init(ostream &output, const ColorSpace cs,
00606                           const PageInfo *, const Layout, PSInfo *);
00607                 void initname(const String &name, const ColorSpace cs,
00608                               const PageInfo *, const Layout, PSInfo *);
00609                 // Compare x/y to current bounding box. x and y are in current
00610                 // user coordinates unless userCoords is False in which case they
00611                 // have already been transformed.
00612                 void bbCheck(const float x, const float y, const Bool userCoords=True);
00613                 void bbCheck(); // Do bbCheck on current position.
00614 
00615                 void setCurrXY( const float x, const float y,
00616                                 const Bool userCoords = True) {
00617                         state_.setXY(x, y, userCoords);
00618                 }
00619                 void getCurrXY( float &x, float &y, const Bool userCoords=True) {
00620                         state_.getXY(x, y, userCoords);
00621                 }
00622         private:
00623                 void startDocument();
00624                 // Code writers.
00625                 void emitHeader();
00626                 void emitProlog();
00627                 void emitTrailer();
00628                 void emitShowPage(const int force=0);
00629                 void emitPageNum();
00630                 void emitSetup();
00631 
00632                 void emitColorSpace(const ColorSpace);
00633                 void emitLineStyle(const LineStyle);
00634                 void emitDashLength(const float length);
00635                 void emitLineWidth(const float w);
00636                 void emitMoveTo(const float x, const float y);
00637                 void emitLineTo(const float x, const float y, const int stroke=1);
00638                 void emitPolyline(const int len, const float *x, const float *y,
00639                                   const int close, const int fill);
00640                 void emitPolygon(const int len, const float *x, const float *y,
00641                                  const int fill);
00642                 void emitLine(const float x0, const float y0,
00643                               const float x1, const float y1);
00644                 void emitLines(const int len, const float *x1, const float *y1,
00645                                const float *x2, const float *y2);
00646                 void emitRectangle(const float x, const float y,
00647                                    const float width, const float height,
00648                                    const int fill);
00649                 void emitPoint(const float x, const float y, const float radius=1.0);
00650                 void emitPoints(const int len, const float *x, const float *y,
00651                                 const float radius=1.0);
00652                 // Rotate by degrees.
00653                 void emitRotate(const float degrees);
00654                 // Scale. (new scale = <old scale>*<new scale>.
00655                 void emitScale(const float x, const float y);
00656                 // Change origin.
00657                 void emitTranslate(const float x, const float y);
00658 
00659                 void emitFindFont(const char *fn, const float scale);   // Load font.
00660                 // Draw text starting at point x, y.
00661                 // Newlines, tabs, etc. are not handled.
00662                 void emitText(const float x, const float y, const char *str,
00663                               const TextAlign=AlignBottomLeft,
00664                               const float angle = 0);
00665                 // Just throw a string on the stack and leave it there.
00666                 void emitString(const char *str);
00667 
00668                 // Draw a width x height PS image.
00669                 // matrix               Matrix to convert between user & image space.
00670                 // width, height        size of input image in pixels.
00671                 // bpc                  bits per color component. ( 8 or 12)
00672                 // smooth               0 - no smoothing, 1 - smooth pixels.
00673                 //                      ghostscript may core dump if not 0.
00674                 //  ascii85             data encoded as ASCII85 chars.
00675                 //  len                 length of data array.
00676                 void emitImage( const float matrix[6],
00677                                 const int width, const int height, const int bpc,
00678                                 const int smooth, const char *ascii85, const int len,
00679                                 const ColorSpace=INDEXED);
00680                 // Load color tables with contents of a, b & c. Typically, a holds
00681                 // red, b holds green & c holds blue. Values should be in the
00682                 // range (0..1).
00683                 void emitStoreColors(const int start, const int len,
00684                                      const float *a, const float *b, const float *c);
00685                 // Load color tables given an array of colors and their corresponding
00686                 // indexes. Much more effecient if indexes are consecutive.
00687                 void emitStoreColorValues(const int len, const int *indexes,
00688                                           const float *a, const float *b, const float *c);
00689                 // Internal helper function.
00690                 void emitStoreColors(const int color, const int start, const int len,
00691                                      const float *ary);
00692 
00693                 // Load one value into color table.
00694                 void emitStoreColor(const int index,
00695                                     const float r, const float g, const float b);
00696                 // Set curren`t color.
00697                 void emitSetColor(const ColorSpace cs, const int index);
00698                 void emitSetColor(const ColorSpace cs,
00699                                   const float a, const float b, const float c);
00700                 // Load color first ncolors entries in the tables with linear
00701                 // ramps scaled 0..1.
00702                 void emitSetLinearRamps(const int ncolors);
00703                 void emitGSave();
00704                 void emitGRestore();
00705                 void emitPushMatrix();
00706                 void emitPopMatrix();
00707                 void emitClipRect(const float x0, const float y0,
00708                                   const float width, const float height);
00709                 void emitBackgroundColor(const float a, const float b, const float c,
00710                                          const ColorSpace cs);
00711                 // Write value of bounding box to output.
00712                 void emitBoundingBox();
00713         private:
00714                 // Holds our copy of the current transformation matrix. Used for
00715                 // bounding box computations.
00716                 class PSState {
00717                 public:
00718                         PSState();
00719                         ~PSState();
00720                         void scale( const float x, const float y);
00721                         void translate( const float x, const float y);
00722                         void rotate(const float degrees);
00723                         // [ x' y' 1 ] = [ x y 1] * CTM
00724                         inline void toPoints(const float xin, const float yin,
00725                                              float &xout, float &yout,
00726                                              const Bool absolute=True)const {
00727                                 xout = (float)(a_*xin + c_*yin);
00728                                 yout = (float)(b_*xin + d_*yin);
00729                                 if(absolute) {
00730                                         xout += (float)tx_;
00731                                         yout += (float)ty_;
00732                                 }
00733                         }
00734 
00735                         // [ x y 1 ] = [ x' y' 1] * inverse(CTM);
00736                         inline void fromPoints(const float xin, const float yin,
00737                                                float &xout, float &yout,
00738                                                const Bool absolute=True)const {
00739                                 xout = (float)(ai_*xin + ci_*yin);
00740                                 yout = (float)(bi_*xin + di_*yin);
00741                                 if(absolute) {
00742                                         xout += (float)txi_;
00743                                         yout += (float)tyi_;
00744                                 }
00745                         }
00746 
00747                         void setIdentity();
00748                         void operator=(const PSState &);
00749                         // Set/Get the current position.
00750                         void setXY(const float x, const float y,
00751                                    const Bool userCoords=True);
00752                         void getXY(float &x, float &y, const Bool userCoords=True);
00753                 private:
00754                         void invert();
00755 
00756                         double  a_, b_, c_, d_, tx_, ty_;       // Forward
00757                         double  ai_, bi_, ci_, di_, txi_, tyi_; // Inverse
00758                         float   currX_, currY_;                 // Current x/y in pnts
00759 
00760                 };
00761                 void pushState();
00762                 void popState();
00763                 // Length of internal state(transform) stack.
00764                 enum {STATESTACKLENGTH=16};
00765                 int             statestackindex_;
00766                 PSState         state_;
00767                 PSState         statestack_[STATESTACKLENGTH];
00768                 ostream         *out;
00769                 std::fstream    *mystream_;
00770                 ColorSpace      colorSpace_;
00771                 Dimension       dimension_;
00772                 Bool            portrait_;
00773                 Bool            eps_;
00774                 LineStyle       lineStyle_;
00775                 float           defaultFontSize_;
00776                 float           xscale_, yscale_; // Paper size scale.
00777                 // Bounding box in points.
00778                 float           bbx0_, bby0_;
00779                 float           bbx1_, bby1_;
00780                 Bool            boxCheck0_;             // Has anything been checked?
00781                 Bool            haveBoundingBox_;
00782                 Bool            checkBoundingBox_;
00783                 // user coords.
00784                 // Page boundaries in points;
00785                 float           xll_, yll_;
00786                 float           xur_, yur_;
00787 
00788                 // Clipping rectangle in points.
00789                 float           clipXll_, clipXur_;
00790                 float           clipYll_, clipYur_;
00791 
00792                 unsigned int    pageNum_;               // Current page number.
00793                 int             finished_;              // Set when finished called.
00794                 PSInfo          *info_;
00795         };
00796 
00797 
00798 } //# NAMESPACE CASA - END
00799 
00800 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1