The R Project SVN R

Rev

Rev 70779 | Rev 84150 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42938 urbaneks 1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
71512 ripley 3
 *  Copyright (C) 2007-2016  The R Core Team
42938 urbaneks 4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, a copy is available at
68949 ripley 17
 *  https://www.R-project.org/Licenses/
42938 urbaneks 18
 *
44552 urbaneks 19
 *---------------------------------------------------------------------
44799 ripley 20
 *  This header file constitutes the (unofficial) API to the Quartz
21
 *  device. Being unofficial, the API may change at any point without
44552 urbaneks 22
 *  warning.
42938 urbaneks 23
 *
70779 ripley 24
 *  Quartz is a general device-independent way of drawing in macOS,
42938 urbaneks 25
 *  therefore the Quartz device modularizes the actual drawing target
26
 *  implementation into separate modules (e.g. Carbon and Cocoa for
44799 ripley 27
 *  on-screen display and PDF, Bitmap for off-screen drawing). The API
42938 urbaneks 28
 *  below is used by the modules to talk to the Quartz device without
29
 *  having to know anything about R graphics device API.
30
 *
31
 *  Key functions are listed here:
32
 *  QuartzDevice_Create - creates a Quartz device
44552 urbaneks 33
 *  QuartzDevice_ResetContext - should be called after the target
34
 *    context has been created to initialize it.
42938 urbaneks 35
 *  QuartzDevice_Kill - closes the Quartz device (e.g. on window close)
44552 urbaneks 36
 *  QuartzDevice_SetScaledSize - resize device (does not include
37
 *    re-painting, it should be followed by a call to
38
 *    QuartzDevice_ReplayDisplayList)
42938 urbaneks 39
 *  QuartzDevice_ReplayDisplayList - replays all plot commands
40
 *
43160 urbaneks 41
 *  Key concepts
42
 *  - all Quartz modules are expected to provide a device context
44552 urbaneks 43
 *    (CGContextRef) for drawing. A device can temporarily return NULL
44
 *    (e.g. if the context is not available immediately) and replay
45
 *    the display list later to catch up.
44799 ripley 46
 *
44552 urbaneks 47
 *  - interactive devices can use QuartzDevice_SetScaledSize to resize
48
 *    the device (no context is necessary), then prepare the context
49
 *    (call QuartzDevice_ResetContext if a new context was created)
50
 *    and finally re-draw using QuartzDevice_ReplayDisplayList.
44799 ripley 51
 *
44552 urbaneks 52
 *  - snapshots can be created either off the current display list
53
 *    (last=0) or off the last known one (last=1). NewPage callback
54
 *    can only use last=1 as there is no display list during that
55
 *    call. Restored snapshots become the current display list and
56
 *    thus can be extended by further painting (yet the original saved
57
 *    copy is not influenced). Also note that all snapshots are SEXPs
58
 *    (the declaration doesn't use SEXP as to not depend on
45255 urbaneks 59
 *    Rinternals.h) therefore must be protected or preserved immediately
60
 *    (i.e. the Quartz device does NOT protect them - except in the
61
 *    call to RestoreSnapshot).
44799 ripley 62
 *
44552 urbaneks 63
 *  - dirty flag: the dirty flag is not used internally by the Quartz
64
 *    device, but can be useful for the modules to determine whether
65
 *    the current graphics is a restored copy or in-progress
66
 *    drawing. The Quartz device manages the flag as follows: a)
67
 *    display list replay does NOT change the flag, b) snapshot
68
 *    restoration resets the flag, c) all other paint operations
69
 *    (i.e. outside of restore/replay) set the flag. Most common use
70
 *    is to determine whether restored snapshots have been
71
 *    subsequently modified.
44799 ripley 72
 *
44552 urbaneks 73
 *  - history: currently the history management is not used by any
74
 *    modules and as such is untested and strictly experimental. It
75
 *    may be removed in the future as it is not clear whether it makes
76
 *    sense to be part of the device. See Cocoa module for a
77
 *    module-internal implementation of the display history.
45759 urbaneks 78
 *
79
 *  Quartz device creation path:
80
 *    quartz() function -> SEXP Quartz(args) ->
81
 *    setup QuartzParameters_t, call backend constructor
82
 *    [e.g. QuartzCocoa_DeviceCreate(dd, fn, QuartzParameters_t *pars)] ->
83
 *    create backend definition (QuartzBackend_t backend) -> 
84
 *    fn->Create(dd, &backend), return the result
42938 urbaneks 85
 */
86
 
71512 ripley 87
/* Unix-only header */
88
 
42938 urbaneks 89
#ifndef R_EXT_QUARTZDEVICE_H_
90
#define R_EXT_QUARTZDEVICE_H_
91
 
61471 ripley 92
/* FIXME: this is installed, but can it really work without config.h? */
51833 ripley 93
 
45255 urbaneks 94
#ifdef HAVE_CONFIG_H
95
#include <config.h>
96
#endif
97
 
42938 urbaneks 98
#ifdef __cplusplus
99
extern "C" {
100
#endif   
45255 urbaneks 101
 
102
#if HAVE_AQUA
103
#include <ApplicationServices/ApplicationServices.h>
104
#else
105
    typedef void* CGContextRef;
106
#endif
42938 urbaneks 107
 
43160 urbaneks 108
/* flags passed to the newPage callback */
109
#define QNPF_REDRAW 0x0001 /* is set when NewPage really means re-draw of an existing page */
110
 
111
/* flags passed to QuartzDevice_Create (as fs parameter) */
45147 urbaneks 112
#define QDFLAG_DISPLAY_LIST 0x0001
113
#define QDFLAG_INTERACTIVE  0x0002 
45759 urbaneks 114
#define QDFLAG_RASTERIZED   0x0004 /* rasterized media - may imply disabling AA paritally for rects etc. */
115
 
116
/* parameter flags (they should not conflict with QDFLAGS to allow chaining) */
117
#define QPFLAG_ANTIALIAS 0x0100
118
 
42938 urbaneks 119
typedef void* QuartzDesc_t;
120
 
44314 urbaneks 121
typedef struct QuartzBackend_s {
122
    int    size;                          /* structure size */
123
    double width, height;
124
    double scalex, scaley, pointsize;
125
    int    bg, canvas;
126
    int    flags;
127
    void*  userInfo;
44799 ripley 128
    CGContextRef (*getCGContext)(QuartzDesc_t dev, void*userInfo); /* Get the context for this device */
129
    int          (*locatePoint)(QuartzDesc_t dev, void*userInfo, double*x, double*y);
130
    void         (*close)(QuartzDesc_t dev, void*userInfo);
131
    void         (*newPage)(QuartzDesc_t dev, void*userInfo, int flags);
132
    void         (*state)(QuartzDesc_t dev, void*userInfo, int state);
45255 urbaneks 133
    void*        (*par)(QuartzDesc_t dev, void*userInfo, int set, const char *key, void *value);
44799 ripley 134
    void         (*sync)(QuartzDesc_t dev, void*userInfo);
50568 murrell 135
    void*        (*cap)(QuartzDesc_t dev, void*userInfo);
44314 urbaneks 136
} QuartzBackend_t;
42938 urbaneks 137
 
44314 urbaneks 138
/* parameters that are passed to functions that create backends */
139
typedef struct QuartzParameters_s {
140
    int        size;                   /* structure size */
141
    const char *type, *file, *title;
142
    double     x, y, width, height, pointsize;
143
    const char *family;
144
    int        flags;
145
    int        connection;
146
    int        bg, canvas;
147
    double     *dpi;
148
    /* the following parameters can be used to pass custom parameters when desired */
149
    double     pard1, pard2;
150
    int        pari1, pari2;
151
    const char *pars1, *pars2;
152
    void       *parv;
153
} QuartzParameters_t;
154
 
42938 urbaneks 155
/* all device implementations have to call this general Quartz device constructor at some point */
44314 urbaneks 156
QuartzDesc_t QuartzDevice_Create(void *dd, QuartzBackend_t* def);
157
 
158
typedef struct QuartzFunctons_s {
159
    void*  (*Create)(void *, QuartzBackend_t *);  /* create a new device */
160
    int    (*DevNumber)(QuartzDesc_t desc);       /* returns device number */
161
    void   (*Kill)(QuartzDesc_t desc);            /* call to close the device */
162
    void   (*ResetContext)(QuartzDesc_t desc);    /* notifies Q back-end that the implementation has created a new context */
163
    double (*GetWidth)(QuartzDesc_t desc);        /* get device width (in inches) */
164
    double (*GetHeight)(QuartzDesc_t desc);       /* get device height (in inches) */
165
    void   (*SetSize)(QuartzDesc_t desc, double width, double height); /* set device size (in inches) */
166
 
167
    double (*GetScaledWidth)(QuartzDesc_t desc);  /* get device width (in pixels) */
168
    double (*GetScaledHeight)(QuartzDesc_t desc); /* get device height (in pixels) */
169
    void   (*SetScaledSize)(QuartzDesc_t desc, double width, double height); /* set device size (in pixels) */
42938 urbaneks 170
 
44314 urbaneks 171
    double (*GetXScale)(QuartzDesc_t desc);     /* get x scale factor (px/pt ratio) */
172
    double (*GetYScale)(QuartzDesc_t desc);     /* get y scale factor (px/pt ratio) */
173
    void   (*SetScale)(QuartzDesc_t desc,double scalex, double scaley); /* sets both scale factors (px/pt ratio) */
42938 urbaneks 174
 
44314 urbaneks 175
    void   (*SetTextScale)(QuartzDesc_t desc,double scale); /* sets text scale factor */
176
    double (*GetTextScale)(QuartzDesc_t desc);  /* sets text scale factor */
42938 urbaneks 177
 
44314 urbaneks 178
    void   (*SetPointSize)(QuartzDesc_t desc,double ps); /* sets point size */
179
    double (*GetPointSize)(QuartzDesc_t desc);  /* gets point size */
42938 urbaneks 180
 
44314 urbaneks 181
    int    (*GetDirty)(QuartzDesc_t desc);        /* sets dirty flag */
182
    void   (*SetDirty)(QuartzDesc_t desc,int dirty); /* gets dirty flag */
42938 urbaneks 183
 
44314 urbaneks 184
    void   (*ReplayDisplayList)(QuartzDesc_t desc); /* replay display list
185
     Note: it inhibits sync calls during repaint,
186
     the caller is responsible for calling sync if needed.
187
     Dirty flag is kept unmodified */
44799 ripley 188
    void*  (*GetSnapshot)(QuartzDesc_t desc, int last);    
189
    /* create a (replayable) snapshot of the device contents. 
190
       when 'last' is set then the last stored display list is used, 
191
       otherwise a new snapshot is created */
192
    void   (*RestoreSnapshot)(QuartzDesc_t desc,void* snapshot);
193
    /* restore a snapshot. also clears the dirty flag */
42938 urbaneks 194
 
44314 urbaneks 195
    int    (*GetAntialias)(QuartzDesc_t desc);    /* get anti-alias flag */
44799 ripley 196
    void   (*SetAntialias)(QuartzDesc_t desc, int aa); /* set anti-alias flag */
42938 urbaneks 197
 
44314 urbaneks 198
    int    (*GetBackground)(QuartzDesc_t desc);   /* get background color */
45255 urbaneks 199
    void   (*Activate)(QuartzDesc_t desc);        /* activate/select the device */
200
    /* get/set Quartz-specific parameters. desc can be NULL for global parameters */
201
    void*  (*SetParameter)(QuartzDesc_t desc, const char *key, void *value);
202
    void*  (*GetParameter)(QuartzDesc_t desc, const char *key);
44314 urbaneks 203
} QuartzFunctions_t;
42938 urbaneks 204
 
45255 urbaneks 205
#define QuartzParam_EmbeddingFlags "embeddeding flags" /* value: int[1] */
206
#define QP_Flags_CFLoop 0x0001  /* drives application event loop */
207
#define QP_Flags_Cocoa  0x0002  /* Cocoa is fully initialized */
208
#define QP_Flags_Front  0x0004  /* is front application */
209
 
61471 ripley 210
/* FIXME: no longer used, remove in due course */
44552 urbaneks 211
/* from unix/aqua.c - loads grDevices if necessary and returns NULL on failure */
212
QuartzFunctions_t *getQuartzFunctions();
42938 urbaneks 213
 
44552 urbaneks 214
/* type of a Quartz contructor */
45255 urbaneks 215
typedef QuartzDesc_t (*quartz_create_fn_t)(void *dd, QuartzFunctions_t *fn, QuartzParameters_t *par);
44552 urbaneks 216
 
217
/* grDevices currently supply following constructors:
44799 ripley 218
   QuartzCocoa_DeviceCreate, QuartzCarbon_DeviceCreate,
219
   QuartzBitmap_DeviceCreate, QuartzPDF_DeviceCreate */
44552 urbaneks 220
 
44314 urbaneks 221
/* embedded Quartz support hook (defined in unix/aqua.c):
222
     dd = should be passed-through to QuartzDevice_Create
44552 urbaneks 223
     fn = Quartz API functions
224
     par = parameters (see above) */
45255 urbaneks 225
#ifndef IN_AQUA_C
226
    extern
227
#endif
228
    QuartzDesc_t (*ptr_QuartzBackend)(void *dd, QuartzFunctions_t *fn, QuartzParameters_t *par);
44552 urbaneks 229
 
42938 urbaneks 230
/* C version of the Quartz call (experimental)
231
   returns 0 on success, error code on failure */
45255 urbaneks 232
QuartzDesc_t Quartz_C(QuartzParameters_t *par, quartz_create_fn_t q_create, int *errorCode);
44552 urbaneks 233
 
42938 urbaneks 234
#ifdef __cplusplus
235
}
236
#endif   
44552 urbaneks 237
 
42938 urbaneks 238
#endif