Initial release of Maemo 5 port of gnuplot
[gnuplot] / term / estimate.trm
1 /* Hello, Emacs, this is -*-C-*-
2  * $Id: estimate.trm,v 1.4.2.1 2007/11/26 04:06:36 sfeam Exp $
3  *
4  */
5
6 /* GNUPLOT - estimate.trm */
7
8 /*
9  * This file is included by ../src/term.c via term.h.
10  *
11  * This terminal driver supports:
12  *   On return from ENHest_put_text()
13  *      (*term)->xmax = estimated string width
14  *      (*term)->ymax = estimated string height
15  *      enhanced_min_height = lowest baseline used
16  *      enhanced_max_height = highest (baseline + fontsize) used
17  *
18  * AUTHORS
19  *
20  *   Ethan A Merritt - Dec 2004
21  *
22  */
23 #include "driver.h"
24
25 #ifdef TERM_PROTO
26 TERM_PUBLIC void ENHest_put_text __PROTO((unsigned int x, unsigned int y, const char str[]));
27 TERM_PUBLIC void ENHest_OPEN __PROTO((char * fontname, double fontsize,
28                                     double base, TBOOLEAN widthflag, TBOOLEAN showflag,
29                                     int overprint));
30 TERM_PUBLIC void ENHest_FLUSH __PROTO((void));
31 #endif /* TERM_PROTO */
32
33 #ifdef TERM_BODY
34
35 static double ENHest_x, ENHest_y;
36 static double ENHest_xsave, ENHest_ysave;
37 static double ENHest_fragment_width;
38 static double ENHest_fontsize;
39
40 static TBOOLEAN ENHest_opened_string;
41 static TBOOLEAN ENHest_show = TRUE;
42 static int ENHest_overprint = 0;
43 static TBOOLEAN ENHest_widthflag = TRUE;
44 #define ENHest_font ""
45 static double ENHest_base;
46
47 TERM_PUBLIC void
48 ENHest_OPEN(
49     char *fontname,
50     double fontsize, double base,
51     TBOOLEAN widthflag, TBOOLEAN showflag,
52     int overprint)
53 {
54     /* There are two special cases:
55      * overprint = 3 means save current position
56      * overprint = 4 means restore saved position
57      */
58     if (overprint == 3) {
59         ENHest_xsave = ENHest_x;
60         ENHest_ysave = ENHest_y;
61         return;
62     } else if (overprint == 4) {
63         ENHest_x = ENHest_xsave;
64         ENHest_y = ENHest_ysave;
65         return;
66     }
67
68     if (!ENHest_opened_string) {
69         ENHest_opened_string = TRUE;
70         /* Start new text fragment */
71             ENHest_fragment_width = 0;
72         /* Scale fractional font height */
73             ENHest_base = base * 1.0;
74         /* Keep track of whether we are supposed to show this string */
75             ENHest_show = showflag;
76         /* 0/1/2  no overprint / 1st pass / 2nd pass */
77             ENHest_overprint = overprint;
78         /* widthflag FALSE means do not update text position after printing */
79             ENHest_widthflag = widthflag;
80         /* font size will be used to estimate width of each character */
81             ENHest_fontsize = fontsize > 2.0 ? 1.0 : fontsize;
82     }
83 }
84
85 TERM_PUBLIC void
86 ENHest_FLUSH()
87 {
88     double len = ENHest_fragment_width;
89
90     if (ENHest_opened_string) {
91         ENHest_fragment_width = 0;
92
93         if (!ENHest_widthflag)
94             /* don't update position */
95             ;
96         else if (ENHest_overprint == 1)
97             /* First pass of overprint, leave position in center of fragment */
98             ENHest_x += len / 2;
99         else
100             /* Normal case is to update position to end of fragment */
101             ENHest_x += len;
102
103         ENHest_opened_string = FALSE;
104     }
105 }
106
107 TERM_PUBLIC void
108 ENHest_put_text(unsigned int x, unsigned int y, const char *str)
109 {
110     /* Set up global variables needed by enhanced_recursion() */
111     ENHest_fontsize  = 1.0;
112     ENHest_opened_string = FALSE;
113     enhanced_max_height = 1;
114     enhanced_min_height = 0;
115
116     /* If no enhanced text processing is needed, strlen() is sufficient */
117     if (ignore_enhanced_text || !strpbrk(str, "{}^_@&~")) {
118         term->xmax = strlen(str);
119         return;
120     }
121
122     ENHest_x = x;
123     ENHest_y = y;
124
125     while (*(str = enhanced_recursion((char *)str, TRUE,
126                         ENHest_font, ENHest_fontsize,
127                         0.0, TRUE, TRUE, 0))) {
128         (term->enhanced_flush)();
129
130         enh_err_check(str);
131         if (!*++str)
132             break; /* end of string */
133     }
134
135     if (ENHest_x > 0.0 && ENHest_x < 1.0)
136         ENHest_x = 1;
137
138     term->xmax = ENHest_x;
139     term->ymax = enhanced_max_height - enhanced_min_height;
140 }
141
142 TERM_PUBLIC void
143 ENHest_writec(int c)
144 {
145     ENHest_fragment_width += ENHest_fontsize;
146 }
147
148
149 static struct termentry ENHest = {
150     "estimate", "estimate width of enhanced text string",
151     1, 1, 1, 1, 1, 1,
152     NULL, NULL, NULL,
153     NULL, NULL, NULL, NULL, NULL,
154     NULL, ENHest_put_text, NULL,
155     NULL, NULL, NULL, NULL,
156     0, 0,                       /* pointsize, flags */
157     NULL, NULL, NULL, NULL
158 #ifdef USE_MOUSE
159     , NULL, NULL, NULL, NULL, NULL
160 #endif
161     , NULL, NULL, NULL, NULL
162 #ifdef WITH_IMAGE
163     , NULL
164 #endif
165     , ENHest_OPEN, ENHest_FLUSH, ENHest_writec
166 };
167
168 #endif /* TERM_BODY */