1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5 
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer.
8 
9  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12 
13  * The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef FONTFORGE_GDRAWP_H
29 #define FONTFORGE_GDRAWP_H
30 
31 #include "basics.h"
32 
33 #include "gdraw.h"
34 
35 typedef struct gcol {
36     int16 red, green, blue;
37     uint32 pixel;
38 } GCol;
39 
40 struct revcol /* : GCol */ {
41     int16 red, green, blue;
42     uint32 index;
43     uint8 dist;
44     struct revcol *next;
45 };
46 
47 struct revitem {
48     struct revcol *cols[2];	/* cols[0] => colours in this subcube, cols[1] => those near */
49     int16 cnt;
50     struct revcmap *sub;
51 };
52 
53 struct revcmap {
54     int16 range;		/* red_max-red_min+1, total number of colors */
55 				/*  in the cube along any linear dimension */
56     int16 side_cnt;		/* Number of sub-cubes along each linear side side of the cube */
57 				/* ie. we decimate by a factor of side_cnt, there */
58 			        /*  will be side_cnt levels of red, and side_cnt^3*/
59 			        /*  subcubes */
60     int16 side_shift;		/* if side_cnt==2^n then this is n */
61     int16 div_mul, div_shift, div_add;
62 				/* tricks for dividing by range/side_cnt */
63 			        /* We can do (small) integer division by */
64 			        /*  multiplying by an integer reciprical and */
65 			        /*  left shifting */
66     unsigned int is_grey: 1;
67     Color mask;			/* masks off the high order bits that this revclut handles, leaving us with those bits of interest to the subclut */
68     struct revitem *cube;
69     GCol *greys;		/* 256 entries, if set */
70 };
71 
72 typedef struct {		/* normal 16 bit characters are two bytes */
73     unsigned char byte1;
74     unsigned char byte2;
75 } GChar2b;
76 
77 struct gchr_transform {
78     uint32 oldstate;
79     uint32 newstate;
80     unichar_t resch;
81 };
82 
83 struct gchr_lookup {
84     int cnt;
85     struct gchr_transform *transtab;
86 };
87 
88 struct gchr_accents {
89     unichar_t accent;
90     uint32 mask;
91 };
92 
93 enum text_funcs { tf_width, tf_drawit, tf_rect, tf_stopat, tf_stopbefore, tf_stopafter };
94 struct tf_arg { GTextBounds size; int width, maxwidth; unichar_t *last; char *utf8_last; int first; int dont_replace; };
95 
96 struct gwindow {
97     GGC *ggc;
98     GDisplay *display;
99     int (*eh)(GWindow,GEvent *);
100     GRect pos;
101     GWindow parent;
102     void *user_data;
103     struct gwidgetdata *widget_data;
104     void *native_window;
105     unsigned int is_visible: 1;
106     unsigned int is_pixmap: 1;
107     unsigned int is_toplevel: 1;
108     unsigned int visible_request: 1;
109     unsigned int is_dying: 1;
110     unsigned int is_popup: 1;
111     unsigned int disable_expose_requests: 1;
112     unsigned int usecairo: 1;		/* use a cairo context -- if meaningful */
113     /**
114      * This is an optional window type string that is used by the hotkey subsystem.
115      * It is a pointer to a constant string which is never to be freeed or NULL.
116      * This will be, for example, "CharView" for the glyph editing window.
117      */
118     char* window_type_name;
119 };
120 
121 struct ginput_context {
122     GWindow w;
123     enum gic_style style;
124     void *ic;
125     struct ginput_context *next;
126 };
127 
128 struct gtimer {
129     long time_sec;				/* longs not int32s to match timeval */
130     long time_usec;
131     int32 repeat_time;				/* 0 => one shot */
132     GWindow owner;
133     void *userdata;
134     struct gtimer *next;
135     unsigned int active: 1;
136 };
137 
138 struct gdisplay {
139     struct displayfuncs *funcs;
140     struct font_state *fontstate;
141     int16 res;
142     GWindow groot;
143     Color def_background, def_foreground;
144     uint16 mykey_state;
145     uint16 mykey_keysym;
146     uint16 mykey_mask;
147     unsigned int mykeybuild: 1;
148     unsigned int default_visual: 1;
149     unsigned int do_dithering: 1;
150     unsigned int focusfollowsmouse: 1;
151     unsigned int top_offsets_set: 1;
152     unsigned int wm_breaks_raiseabove: 1;
153     unsigned int wm_raiseabove_tested: 1;
154     unsigned int endian_mismatch: 1;
155     unsigned int macosx_cmd: 1;		/* if set then map state=0x20 to control */
156     unsigned int twobmouse_win: 1;	/* if set then map state=0x40 to mouse button 2 */
157     unsigned int devicesinit: 1;	/* the devices structure has been initialized. Else call XListInputDevices */
158     unsigned int expecting_core_event: 1;/* when we move an input extension device we generally get two events, one for the device, one later for the core device. eat the core event */
159     unsigned int has_xkb: 1;		/* we were able to initialize the XKB extension */
160     unsigned int supports_alpha_images: 1;
161     unsigned int supports_alpha_windows: 1;
162     int err_flag;
163     char * err_report;
164     /* display specific data */
165 };
166 #define PointToPixel(points,res)		(((points)*(res)+36)/72)
167 #define PointTenthsToPixel(pointtenths,res)	((((pointtenths)*(res)+36)/72)/10)
168 #define PixelToPoint(pixels,res)		(((pixels)*72+(res)/2)/(res))
169 #define PixelToPointTenths(pixels,res)		(((pixels)*720+(res)/2)/(res))
170 
171 struct font_data;
172 
173 struct displayfuncs {
174     void (*init)(GDisplay *);
175 
176     void (*setDefaultIcon)(GWindow);
177 
178     GWindow (*createTopWindow)(GDisplay *, GRect *pos, int (*eh)(GWindow,GEvent *), void *user_data, GWindowAttrs *);
179     GWindow (*createSubWindow)(GWindow, GRect *pos, int (*eh)(GWindow,GEvent *), void *user_data, GWindowAttrs *);
180     GWindow (*createPixmap)(GDisplay *, GWindow similar, uint16 width, uint16 height);
181     GWindow (*createBitmap)(GDisplay *, uint16 width, uint16 height, uint8 *data);
182     GCursor (*createCursor)(GWindow src, GWindow mask, Color fg, Color bg, int16 x, int16 y);
183     void (*destroyWindow)(GWindow);
184     void (*destroyCursor)(GDisplay *,GCursor);
185     int (*nativeWindowExists)(GDisplay *,void *native_window);
186     void (*setZoom)(GWindow,GRect *size,enum gzoom_flags flags);
187     void (*setWindowBackground)(GWindow,Color);
188     int (*setDither)(GDisplay *,int);
189 
190     void (*setVisible)(GWindow,int);
191     void (*move)(GWindow,int32,int32);
192     void (*trueMove)(GWindow,int32,int32);
193     void (*resize)(GWindow,int32,int32);
194     void (*moveResize)(GWindow,int32,int32,int32,int32);
195     void (*raise)(GWindow);
196     void (*raiseAbove)(GWindow,GWindow);
197     int  (*isAbove)(GWindow,GWindow);
198     void (*lower)(GWindow);
199     void (*setWindowTitles)(GWindow, const unichar_t *title, const unichar_t *icontitle);
200     void (*setWindowTitles8)(GWindow, const char *title, const char *icontitle);
201     unichar_t *(*getWindowTitle)(GWindow);
202     char *(*getWindowTitle8)(GWindow);
203     void (*setTransientFor)(GWindow, GWindow);
204     void (*getPointerPos)(GWindow,GEvent *);
205     GWindow (*getPointerWindow)(GWindow);
206     void (*setCursor)(GWindow, GCursor);
207     GCursor (*getCursor)(GWindow);
208     GWindow (*getRedirectWindow)(GDisplay *gd);
209     void (*translateCoordinates)(GWindow from, GWindow to, GPoint *pt);
210 
211     void (*beep)(GDisplay *);
212 
213     void (*pushClip)(GWindow, GRect *rct, GRect *old);
214     void (*popClip)(GWindow, GRect *old);
215 
216     void (*setDifferenceMode)(GWindow);
217 
218     void (*clear)(GWindow,GRect *);
219     void (*drawLine)(GWindow, int32 x,int32 y, int32 xend,int32 yend, Color col);
220     void (*drawArrow)(GWindow, int32 x,int32 y, int32 xend,int32 yend, int16 arrows, Color col); /* arrows&1 => arrow at start, &2 => at end */
221     void (*drawRect)(GWindow, GRect *rect, Color col);
222     void (*fillRect)(GWindow, GRect *rect, Color col);
223     void (*fillRoundRect)(GWindow, GRect *rect, int radius, Color col);
224     void (*drawElipse)(GWindow, GRect *rect, Color col);
225     void (*fillElipse)(GWindow, GRect *rect, Color col);
226     void (*drawArc)(GWindow, GRect *rect, int32 sangle, int32 eangle, Color col);
227     void (*drawPoly)(GWindow, GPoint *pts, int16 cnt, Color col);
228     void (*fillPoly)(GWindow, GPoint *pts, int16 cnt, Color col);
229     void (*scroll)(GWindow, GRect *rect, int32 hor, int32 vert);
230 
231     void (*drawImage)(GWindow, GImage *, GRect *src, int32 x, int32 y);
232     void (*drawGlyph)(GWindow, GImage *, GRect *src, int32 x, int32 y);
233     void (*drawImageMag)(GWindow, GImage *, GRect *src, int32 x, int32 y, int32 width, int32 height);
234     void (*drawPixmap)(GWindow, GWindow, GRect *src, int32 x, int32 y);
235 
236     GIC *(*createInputContext)(GWindow, enum gic_style);
237     void (*setGIC)(GWindow, GIC *, int x, int y);
238     int (*keyState)(GWindow w, int keysym);
239 
240     void (*grabSelection)(GWindow w,enum selnames sel);
241     void (*addSelectionType)(GWindow w,enum selnames sel,char *type,
242 	void *data,int32 cnt,int32 unitsize,void *(*gendata)(void *,int32 *len),
243 	void (*freedata)(void *));
244     void *(*requestSelection)(GWindow w,enum selnames sn, char *typename, int32 *len);
245     int (*selectionHasType)(GWindow w,enum selnames sn, char *typename);
246     void (*bindSelection)(GDisplay *disp,enum selnames sn, char *atomname);
247     int (*selectionHasOwner)(GDisplay *disp,enum selnames sn);
248 
249     void (*pointerUngrab)(GDisplay *);
250     void (*pointerGrab)(GWindow);
251     void (*requestExpose)(GWindow,GRect *,int);
252     void (*forceUpdate)(GWindow);
253     void (*sync)(GDisplay *);
254     void (*skipMouseMoveEvents)(GWindow, GEvent *);
255     void (*processPendingEvents)(GDisplay *);
256     void (*processWindowEvents)(GWindow);
257     void (*processOneEvent)(GDisplay *);
258     void (*eventLoop)(GDisplay *);
259     void (*postEvent)(GEvent *e);
260     void (*postDragEvent)(GWindow w,GEvent *mouse,enum event_type et);
261     int  (*requestDeviceEvents)(GWindow w,int devcnt,struct gdeveventmask *de);
262 
263     GTimer *(*requestTimer)(GWindow w,int32 time_from_now,int32 frequency, void *userdata);
264     void (*cancelTimer)(GTimer *timer);
265 
266     void (*getFontMetrics)(GWindow,GFont *,int *,int *,int *);
267 
268     enum gcairo_flags (*hasCairo)(GWindow w);
269 
270     void (*startNewPath)(GWindow w);
271     void (*closePath)(GWindow w);
272     void (*moveto)(GWindow w,double x, double y);
273     void (*lineto)(GWindow w,double x, double y);
274     void (*curveto)(GWindow w, double cx1,double cy1, double cx2,double cy2, double x, double y);
275     void (*stroke)(GWindow w, Color col);
276     void (*fill)(GWindow w, Color col);
277     void (*fillAndStroke)(GWindow w, Color fillcol,Color strokecol);
278 
279     void (*layoutInit)(GWindow w, char *text, int cnt, GFont *fi);
280     void (*layoutDraw)(GWindow w, int32 x, int32 y, Color fg);
281     void (*layoutIndexToPos)(GWindow w, int index, GRect *pos);
282     int  (*layoutXYToIndex)(GWindow w, int x, int y);
283     void (*layoutExtents)(GWindow w, GRect *size);
284     void (*layoutSetWidth)(GWindow w, int width);
285     int  (*layoutLineCount)(GWindow w);
286     int  (*layoutLineStart)(GWindow w,int line);
287     void (*startNewSubPath)(GWindow w);
288     int  (*fillRuleSetWinding)(GWindow w);
289 
290     int (*doText8)(GWindow w, int32 x, int32 y, const char *text, int32 cnt, Color col, enum text_funcs drawit, struct tf_arg *arg);
291 
292     void (*PushClipOnly)(GWindow w);
293     void (*ClipPreserve)(GWindow w);
294 
295 };
296 
297 extern int16 div_tables[257][2]; // in div_tables.c
298 
299 extern void GDrawIErrorRun(const char *fmt,...);
300 extern void GDrawIError(const char *fmt,...);
301 
302 extern void _GXDraw_DestroyDisplay(GDisplay * gdisp);
303 extern GDisplay *_GXDraw_CreateDisplay(char *displayname,char *programname);
304 extern void _GGDKDraw_DestroyDisplay(GDisplay *disp);
305 extern GDisplay *_GGDKDraw_CreateDisplay(char *displayname, char *programname);
306 extern void _GDraw_InitError(GDisplay *);
307 extern void _GDraw_ComposeChars(GDisplay *gdisp,GEvent *gevent);
308 
309 extern void _GDraw_getimageclut(struct _GImage *base, struct gcol *clut);
310 extern const GCol *_GImage_GetIndexedPixel(Color col,RevCMap *rev);
311 extern const GCol *_GImage_GetIndexedPixelPrecise(Color col,RevCMap *rev);
312 
313 extern void (*_GDraw_BuildCharHook)(GDisplay *);
314 extern void (*_GDraw_InsCharHook)(GDisplay *,unichar_t);
315 
316 #endif /* FONTFORGE_GDRAWP_H */
317