RAPI
R_ext/GraphicsEngine.h
Go to the documentation of this file.
00001 /*
00002  *  R : A Computer Language for Statistical Data Analysis
00003  *  Copyright (C) 2001-11 The R Core Team.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU Lesser General Public License as published by
00007  *  the Free Software Foundation; either version 2.1 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public License
00016  *  along with this program; if not, a copy is available at
00017  *  https://www.R-project.org/Licenses/
00018  */
00019 
00020 /* Used by graphics.c, grid and by third-party graphics devices */
00021 
00022 #ifndef R_GRAPHICSENGINE_H_
00023 #define R_GRAPHICSENGINE_H_
00024 
00025 #ifdef __cplusplus
00026 extern "C" {
00027 #endif
00028 
00029 /*
00030  * The current graphics engine (including graphics device) API version
00031  * MUST be integer
00032  *
00033  * This number should be bumped whenever there are changes to
00034  * GraphicsEngine.h or GraphicsDevice.h so that add-on packages
00035  * that compile against these headers (graphics systems such as
00036  * graphics and grid;  graphics devices such as gtkDevice, RSvgDevice)
00037  * can detect any version mismatch.
00038  *
00039  * Version 1:  Introduction of the version number.
00040  * Version 2:  GEDevDesc *dd dropped from GEcontourLines().
00041  * Version 3:  R_GE_str2col() added to API. (r41887)
00042  * Version 4:  UTF-8 text hooks, useRotatedTextInContour,
00043  *             add newFrameConfirm() to NewDevDesc.
00044  *             New API: GEaddDevice[2] GEgetDevice, GEkillDevice,
00045  *             ndevNumber. (R 2.7.0)
00046  * Version 5:  Clean up 1.4.0/2.0.0 changes!
00047  *             Remove newDevStruct from GEDevDesc and NewDevDesc.
00048  *             Remove asp, dot(), hold(), open() from NewDevDesc.
00049  *             Move displayList, DLlastElt, savedSnapshot from
00050  *             NewDevDesc to GEDevDesc.
00051  *             Add 'ask' to GEDevDesc. (R 2.8.0)
00052  * Version 6:  Add dev_Raster() and dev_Cap()  (R 2.11.0)
00053  * Version 7:  Change graphics event handling, adding eventEnv and eventHelper()
00054  *         to DevDesc.  (R 2.12.0)
00055  * Version 8:  Add dev_Path() (R 2.12.0)
00056  * Version 9:  Add dev_HoldFlush(), haveTrans*, haveRaster,
00057  *             haveCapture, haveLocator.  (R 2.14.0)
00058  * Version 10: For R 3.0.0.  Typedef and use 'rcolor',
00059  *             Remove name2col (R_GE_str2col does the job).
00060  * Version 11: For R 3.3.0.
00061  *             Official support for saving/restoring display lists
00062  *             across R sessions (via recordPlot() and replayPlot())
00063  *             - added grid DL to snapshots (used to be NULL)
00064  *             - added this version number to snapshots (as attribute)
00065  *             - added R version number to snapshots (as attribute)
00066  *             - added pkgName to graphics system state info (as attribute)
00067  */
00068 
00069 #define R_GE_version 11
00070 
00071 int R_GE_getVersion(void);
00072 
00073 void R_GE_checkVersionOrDie(int version);
00074 
00075 /* The graphics engine will only accept locations and dimensions
00076  * in native device coordinates, but it provides the following functions
00077  * for converting between a couple of simple alternative coordinate
00078  * systems and device coordinates:
00079  *    DEVICE = native units of the device
00080  *    NDC = Normalised device coordinates
00081  *    INCHES = inches (!)
00082  *    CM = centimetres (!!)
00083  */
00084 
00085 typedef enum {
00086  GE_DEVICE  = 0,    /* native device coordinates (rasters) */
00087  GE_NDC = 1,    /* normalised device coordinates x=(0,1), y=(0,1) */
00088  GE_INCHES = 2,
00089  GE_CM     = 3
00090 } GEUnit;
00091 
00092 #define MAX_GRAPHICS_SYSTEMS 24
00093 
00094 typedef enum {
00095     /* In response to this event, the registered graphics system
00096      * should allocate and initialise the systemSpecific structure
00097      *
00098      * Should return R_NilValue on failure so that engine
00099      * can tidy up memory allocation
00100      */
00101     GE_InitState = 0,
00102     /* This event gives the registered system a chance to undo
00103      * anything done in the initialisation.
00104      */
00105     GE_FinaliseState = 1,
00106     /* This is sent by the graphics engine prior to initialising
00107      * the display list.  It give the graphics system the chance
00108      * to squirrel away information it will need for redrawing the
00109      * the display list
00110      */
00111     GE_SaveState = 2,
00112     /* This is sent by the graphics engine prior to replaying the
00113      * display list.  It gives the graphics system the chance to
00114      * restore any information it saved on the GE_SaveState event
00115      */
00116     GE_RestoreState = 6,
00117     /* Copy system state information to the current device.
00118      * This is used when copying graphics from one device to another
00119      * so all the graphics system needs to do is to copy across
00120      * the bits required for the display list to draw faithfully
00121      * on the new device.
00122      */
00123     GE_CopyState = 3,
00124     /* Create a snapshot of the system state that is sufficient
00125      * for the current "image" to be reproduced
00126      */
00127     GE_SaveSnapshotState = 4,
00128     /* Restore the system state that is saved by GE_SaveSnapshotState
00129      */
00130     GE_RestoreSnapshotState = 5,
00131     /* When replaying the display list, the graphics engine
00132      * checks, after each replayed action, that the action
00133      * produced valid output.  This is the graphics system's
00134      * chance to say that the output is crap (in which case the
00135      * graphics engine will abort the display list replay).
00136      */
00137     GE_CheckPlot = 7,
00138     /* The device wants to scale the current pointsize
00139      * (for scaling an image)
00140      * This is not a nice general solution, but a quick fix for
00141      * the Windows device.
00142      */
00143     GE_ScalePS = 8
00144 } GEevent;
00145 
00146 /*
00147  *  Some line end/join constants
00148  */
00149 typedef enum {
00150   GE_ROUND_CAP  = 1,
00151   GE_BUTT_CAP   = 2,
00152   GE_SQUARE_CAP = 3
00153 } R_GE_lineend;
00154 
00155 typedef enum {
00156   GE_ROUND_JOIN = 1,
00157   GE_MITRE_JOIN = 2,
00158   GE_BEVEL_JOIN = 3
00159 } R_GE_linejoin;
00160 
00161 /*
00162  * A structure containing graphical parameters
00163  *
00164  * This is how graphical parameters are passed from graphics systems
00165  * to the graphics engine AND from the graphics engine to graphics
00166  * devices.
00167  *
00168  * Devices are not *required* to honour graphical parameters
00169  * (e.g., alpha transparency is going to be tough for some)
00170  */
00171 typedef struct {
00172     /*
00173      * Colours
00174      *
00175      * NOTE:  Alpha transparency included in col & fill
00176      */
00177     int col;             /* pen colour (lines, text, borders, ...) */
00178     int fill;            /* fill colour (for polygons, circles, rects, ...) */
00179     double gamma;        /* Gamma correction */
00180     /*
00181      * Line characteristics
00182      */
00183     double lwd;          /* Line width (roughly number of pixels) */
00184     int lty;             /* Line type (solid, dashed, dotted, ...) */
00185     R_GE_lineend lend;   /* Line end */
00186     R_GE_linejoin ljoin; /* line join */
00187     double lmitre;       /* line mitre */
00188     /*
00189      * Text characteristics
00190      */
00191     double cex;          /* Character expansion (font size = fontsize*cex) */
00192     double ps;           /* Font size in points */
00193     double lineheight;   /* Line height (multiply by font size) */
00194     int fontface;        /* Font face (plain, italic, bold, ...) */
00195     char fontfamily[201]; /* Font family */
00196 } R_GE_gcontext;
00197 
00198 typedef R_GE_gcontext* pGEcontext;
00199 
00200 
00201 #include <R_ext/GraphicsDevice.h> /* needed for DevDesc */
00202 
00203 typedef struct _GEDevDesc GEDevDesc;
00204 
00205 typedef SEXP (* GEcallback)(GEevent, GEDevDesc *, SEXP);
00206 
00207 typedef struct {
00208     /* An array of information about each graphics system that
00209      * has registered with the graphics engine.
00210      * This is used to store graphics state for each graphics
00211      * system on each device.
00212      */
00213     void *systemSpecific;
00214     /*
00215      * An array of function pointers, one per graphics system that
00216      * has registered with the graphics engine.
00217      *
00218      * system_Callback is called when the graphics engine wants
00219      * to give a graphics system the chance to play with its
00220      * device-specific information (stored in systemSpecific)
00221      * There are two parameters:  an "event" to tell the graphics
00222      * system why the graphics engine has called this function,
00223      * and the systemSpecific pointer.  The graphics engine
00224      * has to pass the systemSpecific pointer because only
00225      * the graphics engine will know what array index to use.
00226      */
00227     GEcallback callback;
00228 } GESystemDesc;
00229 
00230 struct _GEDevDesc {
00231     /*
00232      * Stuff that the devices can see (and modify).
00233      * All detailed in GraphicsDevice.h
00234      */
00235     pDevDesc dev;
00236     /*
00237      * Stuff about the device that only the graphics engine sees
00238      * (the devices don't see it).
00239      */
00240     Rboolean displayListOn;  /* toggle for display list status */
00241     SEXP displayList;        /* display list */
00242     SEXP DLlastElt;          /* A pointer to the end of the display list
00243                 to avoid tranversing pairlists */
00244     SEXP savedSnapshot;      /* The last element of the display list
00245                   * just prior to when the display list
00246                   * was last initialised
00247                   */
00248     Rboolean dirty;          /* Has the device received any output? */
00249     Rboolean recordGraphics; /* Should a graphics call be stored
00250                   * on the display list?
00251                   * Set to FALSE by do_recordGraphics,
00252                   * do_dotcallgr, and do_Externalgr
00253                   * so that nested calls are not
00254                   * recorded on the display list
00255                   */
00256     /*
00257      * Stuff about the device that only graphics systems see.
00258      * The graphics engine has no idea what is in here.
00259      * Used by graphics systems to store system state per device.
00260      */
00261     GESystemDesc *gesd[MAX_GRAPHICS_SYSTEMS];
00262 
00263     /* per-device setting for 'ask' (use NewFrameConfirm) */
00264     Rboolean ask;
00265 };
00266 
00267 typedef GEDevDesc* pGEDevDesc;
00268 
00269 /* functions from devices.c for use by graphics devices */
00270 
00271 #define desc2GEDesc     Rf_desc2GEDesc
00272 /* map DevDesc to enclosing GEDevDesc */
00273 pGEDevDesc desc2GEDesc(pDevDesc dd);
00274 int GEdeviceNumber(pGEDevDesc dd);
00275 pGEDevDesc GEgetDevice(int i);
00276 void GEaddDevice(pGEDevDesc gdd);
00277 void GEaddDevice2(pGEDevDesc gdd, const char * name);
00278 void GEaddDevice2f(pGEDevDesc gdd, const char * name, const char * file);
00279 void GEkillDevice(pGEDevDesc gdd);
00280 pGEDevDesc GEcreateDevDesc(pDevDesc dev);
00281 
00282 void GEdestroyDevDesc(pGEDevDesc dd);
00283 void *GEsystemState(pGEDevDesc dd, int index);
00284 void GEregisterWithDevice(pGEDevDesc dd);
00285 void GEregisterSystem(GEcallback callback, int *systemRegisterIndex);
00286 void GEunregisterSystem(int registerIndex);
00287 SEXP GEhandleEvent(GEevent event, pDevDesc dev, SEXP data);
00288 
00289 #define fromDeviceX     GEfromDeviceX
00290 #define toDeviceX       GEtoDeviceX
00291 #define fromDeviceY     GEfromDeviceY
00292 #define toDeviceY       GEtoDeviceY
00293 #define fromDeviceWidth     GEfromDeviceWidth
00294 #define toDeviceWidth       GEtoDeviceWidth
00295 #define fromDeviceHeight    GEfromDeviceHeight
00296 #define toDeviceHeight      GEtoDeviceHeight
00297 
00298 double fromDeviceX(double value, GEUnit to, pGEDevDesc dd);
00299 double toDeviceX(double value, GEUnit from, pGEDevDesc dd);
00300 double fromDeviceY(double value, GEUnit to, pGEDevDesc dd);
00301 double toDeviceY(double value, GEUnit from, pGEDevDesc dd);
00302 double fromDeviceWidth(double value, GEUnit to, pGEDevDesc dd);
00303 double toDeviceWidth(double value, GEUnit from, pGEDevDesc dd);
00304 double fromDeviceHeight(double value, GEUnit to, pGEDevDesc dd);
00305 double toDeviceHeight(double value, GEUnit from, pGEDevDesc dd);
00306 
00307 /*-------------------------------------------------------------------
00308  *
00309  *  COLOUR CODE is concerned with the internals of R colour representation
00310  *
00311  *  From colors.c, used in par.c, grid/src/gpar.c
00312  */
00313 
00314 typedef unsigned int rcolor;
00315 
00316 #define RGBpar          Rf_RGBpar
00317 #define RGBpar3         Rf_RGBpar3
00318 #define col2name                Rf_col2name
00319 
00320 /* Convert an element of a R colour specification (which might be a
00321    number or a string) into an internal colour specification. */
00322 rcolor RGBpar(SEXP x, int i);
00323 rcolor RGBpar3(SEXP x, int i, rcolor bg);
00324 
00325 /* Convert an internal colour specification to/from a colour name */
00326 const char *col2name(rcolor col); /* used in par.c, grid */
00327 
00328 /* Convert either a name or a #RRGGBB[AA] string to internal.
00329    Because people were using it, it also converts "1", "2" ...
00330    to a colour in the palette, and "0" to transparent white.
00331 */
00332 rcolor R_GE_str2col(const char *s);
00333 
00334 
00335 
00336 /*
00337  *  Some Notes on Line Textures
00338  *
00339  *  Line textures are stored as an array of 4-bit integers within
00340  *  a single 32-bit word.  These integers contain the lengths of
00341  *  lines to be drawn with the pen alternately down and then up.
00342  *  The device should try to arrange that these values are measured
00343  *  in points if possible, although pixels is ok on most displays.
00344  *
00345  *  If newlty contains a line texture description it is decoded
00346  *  as follows:
00347  *
00348  *      ndash = 0;
00349  *      for(i=0 ; i<8 && newlty & 15 ; i++) {
00350  *          dashlist[ndash++] = newlty & 15;
00351  *          newlty = newlty>>4;
00352  *      }
00353  *      dashlist[0] = length of pen-down segment
00354  *      dashlist[1] = length of pen-up segment
00355  *      etc
00356  *
00357  *  An integer containing a zero terminates the pattern.  Hence
00358  *  ndash in this code fragment gives the length of the texture
00359  *  description.  If a description contains an odd number of
00360  *  elements it is replicated to create a pattern with an
00361  *  even number of elements.  (If this is a pain, do something
00362  *  different its not crucial).
00363  *
00364  */
00365 
00366 /*--- The basic numbered & names line types; Here device-independent:
00367   e.g. "dashed" == "44",  "dotdash" == "1343"
00368 */
00369 
00370 /* NB: was also in Rgraphics.h in R < 2.7.0 */
00371 #define LTY_BLANK   -1
00372 #define LTY_SOLID   0
00373 #define LTY_DASHED  4 + (4<<4)
00374 #define LTY_DOTTED  1 + (3<<4)
00375 #define LTY_DOTDASH 1 + (3<<4) + (4<<8) + (3<<12)
00376 #define LTY_LONGDASH    7 + (3<<4)
00377 #define LTY_TWODASH 2 + (2<<4) + (6<<8) + (2<<12)
00378 
00379 R_GE_lineend GE_LENDpar(SEXP value, int ind);
00380 SEXP GE_LENDget(R_GE_lineend lend);
00381 R_GE_linejoin GE_LJOINpar(SEXP value, int ind);
00382 SEXP GE_LJOINget(R_GE_linejoin ljoin);
00383 
00384 void GESetClip(double x1, double y1, double x2, double y2, pGEDevDesc dd);
00385 void GENewPage(const pGEcontext gc, pGEDevDesc dd);
00386 void GELine(double x1, double y1, double x2, double y2,
00387         const pGEcontext gc, pGEDevDesc dd);
00388 void GEPolyline(int n, double *x, double *y,
00389         const pGEcontext gc, pGEDevDesc dd);
00390 void GEPolygon(int n, double *x, double *y,
00391            const pGEcontext gc, pGEDevDesc dd);
00392 SEXP GEXspline(int n, double *x, double *y, double *s, Rboolean open,
00393            Rboolean repEnds, Rboolean draw,
00394            const pGEcontext gc, pGEDevDesc dd);
00395 void GECircle(double x, double y, double radius,
00396           const pGEcontext gc, pGEDevDesc dd);
00397 void GERect(double x0, double y0, double x1, double y1,
00398         const pGEcontext gc, pGEDevDesc dd);
00399 void GEPath(double *x, double *y,
00400             int npoly, int *nper,
00401             Rboolean winding,
00402             const pGEcontext gc, pGEDevDesc dd);
00403 void GERaster(unsigned int *raster, int w, int h,
00404               double x, double y, double width, double height,
00405               double angle, Rboolean interpolate,
00406               const pGEcontext gc, pGEDevDesc dd);
00407 SEXP GECap(pGEDevDesc dd);
00408 void GEText(double x, double y, const char * const str, cetype_t enc,
00409         double xc, double yc, double rot,
00410         const pGEcontext gc, pGEDevDesc dd);
00411 void GEMode(int mode, pGEDevDesc dd);
00412 void GESymbol(double x, double y, int pch, double size,
00413           const pGEcontext gc, pGEDevDesc dd);
00414 void GEPretty(double *lo, double *up, int *ndiv);
00415 void GEMetricInfo(int c, const pGEcontext gc,
00416           double *ascent, double *descent, double *width,
00417           pGEDevDesc dd);
00418 double GEStrWidth(const char *str, cetype_t enc,
00419           const pGEcontext gc, pGEDevDesc dd);
00420 double GEStrHeight(const char *str, cetype_t enc,
00421           const pGEcontext gc, pGEDevDesc dd);
00422 void GEStrMetric(const char *str, cetype_t enc, const pGEcontext gc,
00423                  double *ascent, double *descent, double *width,
00424                  pGEDevDesc dd);
00425 int GEstring_to_pch(SEXP pch);
00426 
00427 /*-------------------------------------------------------------------
00428  *
00429  *  LINE TEXTURE CODE is concerned with the internals of R
00430  *  line texture representation.
00431  */
00432 unsigned int GE_LTYpar(SEXP value, int ind);
00433 SEXP GE_LTYget(unsigned int lty);
00434 
00435 /*
00436  * Raster operations
00437  */
00438 void R_GE_rasterScale(unsigned int *sraster, int sw, int sh,
00439                       unsigned int *draster, int dw, int dh);
00440 void R_GE_rasterInterpolate(unsigned int *sraster, int sw, int sh,
00441                             unsigned int *draster, int dw, int dh);
00442 void R_GE_rasterRotatedSize(int w, int h, double angle,
00443                             int *wnew, int *hnew);
00444 void R_GE_rasterRotatedOffset(int w, int h, double angle, int botleft,
00445                               double *xoff, double *yoff);
00446 void R_GE_rasterResizeForRotation(unsigned int *sraster,
00447                                   int w, int h,
00448                                   unsigned int *newRaster,
00449                                   int wnew, int hnew,
00450                                   const pGEcontext gc);
00451 void R_GE_rasterRotate(unsigned int *sraster, int w, int h, double angle,
00452                        unsigned int *draster, const pGEcontext gc,
00453                        Rboolean perPixelAlpha);
00454 
00455 
00456 /*
00457  * From plotmath.c
00458  */
00459 double GEExpressionWidth(SEXP expr,
00460              const pGEcontext gc, pGEDevDesc dd);
00461 double GEExpressionHeight(SEXP expr,
00462               const pGEcontext gc, pGEDevDesc dd);
00463 void GEExpressionMetric(SEXP expr, const pGEcontext gc,
00464                         double *ascent, double *descent, double *width,
00465                         pGEDevDesc dd);
00466 void GEMathText(double x, double y, SEXP expr,
00467         double xc, double yc, double rot,
00468         const pGEcontext gc, pGEDevDesc dd);
00469 /*
00470  * (End from plotmath.c)
00471  */
00472 
00473 /*
00474  * From plot3d.c : used in package clines
00475  */
00476 SEXP GEcontourLines(double *x, int nx, double *y, int ny,
00477             double *z, double *levels, int nl);
00478 /*
00479  * (End from plot3d.c)
00480  */
00481 
00482 /*
00483  * From vfonts.c
00484  */
00485 double R_GE_VStrWidth(const char *s, cetype_t enc, const pGEcontext gc, pGEDevDesc dd);
00486 
00487 double R_GE_VStrHeight(const char *s, cetype_t enc, const pGEcontext gc, pGEDevDesc dd);
00488 void R_GE_VText(double x, double y, const char * const s, cetype_t enc,
00489         double x_justify, double y_justify, double rotation,
00490         const pGEcontext gc, pGEDevDesc dd);
00491 /*
00492  * (End from vfonts.c)
00493  */
00494 
00495 /* Also in Graphics.h */
00496 #define DEG2RAD 0.01745329251994329576
00497 
00498 pGEDevDesc GEcurrentDevice(void);
00499 Rboolean GEdeviceDirty(pGEDevDesc dd);
00500 void GEdirtyDevice(pGEDevDesc dd);
00501 Rboolean GEcheckState(pGEDevDesc dd);
00502 Rboolean GErecording(SEXP call, pGEDevDesc dd);
00503 void GErecordGraphicOperation(SEXP op, SEXP args, pGEDevDesc dd);
00504 void GEinitDisplayList(pGEDevDesc dd);
00505 void GEplayDisplayList(pGEDevDesc dd);
00506 void GEcopyDisplayList(int fromDevice);
00507 SEXP GEcreateSnapshot(pGEDevDesc dd);
00508 void GEplaySnapshot(SEXP snapshot, pGEDevDesc dd);
00509 void GEonExit(void);
00510 void GEnullDevice(void);
00511 
00512 
00513 /* From ../../main/plot.c, used by ../../library/grid/src/grid.c : */
00514 #define CreateAtVector      Rf_CreateAtVector
00515 SEXP CreateAtVector(double* axp, double* usr, int nint, Rboolean logflag);
00516 /* From ../../main/graphics.c, used by ../../library/grDevices/src/axis_scales.c : */
00517 #define GAxisPars       Rf_GAxisPars
00518 void GAxisPars(double *min, double *max, int *n, Rboolean log, int axis);
00519 
00520 #ifdef __cplusplus
00521 }
00522 #endif
00523 
00524 #endif /* R_GRAPHICSENGINE_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines