File: estimate.trm

package info (click to toggle)
gnuplot5 5.0.0~rc%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,548 kB
  • ctags: 8,104
  • sloc: ansic: 77,108; cpp: 6,848; makefile: 1,932; sh: 1,343; lisp: 657; perl: 302; awk: 235; pascal: 194; tcl: 88; python: 46
file content (219 lines) | stat: -rw-r--r-- 6,087 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Hello, Emacs, this is -*-C-*-
 * $Id: estimate.trm,v 1.11 2012/05/06 00:31:36 sfeam Exp $
 *
 */

/* GNUPLOT - estimate.trm */

/*
 * This file is included by ../src/term.c via term.h.
 *
 * This terminal driver supports:
 *   On return from ENHest_put_text()
 *	(*term)->xmax = estimated string width
 *	(*term)->ymax = estimated string height (in tenths of a character height)
 *
 * AUTHORS
 *
 *   Ethan A Merritt - Dec 2004
 *
 */
#include "driver.h"

#ifdef TERM_PROTO
TERM_PUBLIC void ENHest_put_text __PROTO((unsigned int x, unsigned int y, const char str[]));
TERM_PUBLIC void ENHest_OPEN __PROTO((char * fontname, double fontsize,
	                            double base, TBOOLEAN widthflag, TBOOLEAN showflag,
				    int overprint));
TERM_PUBLIC void ENHest_FLUSH __PROTO((void));
#endif /* TERM_PROTO */

#ifdef TERM_BODY

static double ENHest_x, ENHest_y;
static double ENHest_xsave, ENHest_ysave;
static double ENHest_fragment_width;
static double ENHest_fontsize;
static double ENHest_min_height, ENHest_max_height;
static double ENHest_total_width;

static TBOOLEAN ENHest_opened_string;
static TBOOLEAN ENHest_show = TRUE;
static int ENHest_overprint = 0;
static TBOOLEAN ENHest_widthflag = TRUE;
#define ENHest_font ""
static double ENHest_base;

/* Internal routines for UTF-8 support */
static size_t strwidth_utf8 __PROTO((const char *s));

TERM_PUBLIC void
ENHest_OPEN(
    char *fontname,
    double fontsize, double base,
    TBOOLEAN widthflag, TBOOLEAN showflag,
    int overprint)
{
    /* There are two special cases:
     * overprint = 3 means save current position
     * overprint = 4 means restore saved position
     */
    if (overprint == 3) {
	ENHest_xsave = ENHest_x;
	ENHest_ysave = ENHest_y;
	return;
    } else if (overprint == 4) {
	ENHest_x = ENHest_xsave;
	ENHest_y = ENHest_ysave;
	return;
    }

    if (!ENHest_opened_string) {
	ENHest_opened_string = TRUE;
	/* Start new text fragment */
	    ENHest_fragment_width = 0;
	/* font size will be used to estimate width of each character */
	    ENHest_fontsize = fontsize > 2.0 ? 1.0 : fontsize;
	/* Scale fractional font height */
	    ENHest_base = base * 1.0;
	    if (ENHest_max_height < ENHest_base+1.0*fontsize)
		ENHest_max_height = ENHest_base+1.0*fontsize;
	    if (ENHest_min_height > ENHest_base)
		ENHest_min_height = ENHest_base;
	    FPRINTF((stderr,"ENHest_OPEN: base %g fontsize %g  min %g max %g\n",
	    	    base,fontsize,ENHest_min_height,ENHest_max_height));
	/* Keep track of whether we are supposed to show this string */
	    ENHest_show = showflag;
	/* 0/1/2  no overprint / 1st pass / 2nd pass */
	    ENHest_overprint = overprint;
	/* widthflag FALSE means do not update text position after printing */
	    ENHest_widthflag = widthflag;
    }
}

TERM_PUBLIC void
ENHest_FLUSH()
{
    double len = ENHest_fragment_width;

    if (ENHest_opened_string) {
	ENHest_fragment_width = 0;

	if (!ENHest_widthflag)
	    /* don't update position */
	    ;
	else if (ENHest_overprint == 1)
	    /* First pass of overprint, leave position in center of fragment */
	    ENHest_x += len / 2;
	else
	    /* Normal case is to update position to end of fragment */
	    ENHest_x += len;

	ENHest_total_width = GPMAX(ENHest_total_width, ENHest_x);
	ENHest_opened_string = FALSE;
    }
}

TERM_PUBLIC void
ENHest_put_text(unsigned int x, unsigned int y, const char *str)
{
    /* Set up global variables needed by enhanced_recursion() */
    ENHest_fontsize  = 1.0;
    ENHest_opened_string = FALSE;
    ENHest_max_height = 1.0;
    ENHest_min_height = 0.0;
    ENHest_total_width = 0.0;
    strncpy(enhanced_escape_format,".",sizeof(enhanced_escape_format));

    /* If no enhanced text processing is needed, strlen() is sufficient */
    if (ignore_enhanced_text || !strpbrk(str, "{}^_@&~\n")) {
	if (encoding == S_ENC_UTF8)
	    term->xmax = strwidth_utf8(str);
	else
	    term->xmax = strlen(str);
	term->ymax = 10;
	return;
    }

    ENHest_x = x;
    ENHest_y = y;

    while (*(str = enhanced_recursion((char *)str, TRUE,
    			ENHest_font, ENHest_fontsize,
			0.0, TRUE, TRUE, 0))) {
	(term->enhanced_flush)();

	enh_err_check(str);
	if (!*++str)
	    break; /* end of string */
    }

    if (ENHest_x > 0.0 && ENHest_x < 1.0)
	ENHest_x = 1;
    term->xmax = ENHest_total_width;
    term->ymax = 10. * (ENHest_max_height - ENHest_min_height) + 0.5;
}

TERM_PUBLIC void
ENHest_writec(int c)
{
    if (c == '\n') {
	ENHest_FLUSH();
	ENHest_opened_string = TRUE;
	ENHest_min_height -= 1.0 * ENHest_fontsize;
	ENHest_base -= 1.0 * ENHest_fontsize;
	ENHest_x = 0;
    }

    if (encoding == S_ENC_UTF8) {
	/* Skip all but the first byte of UTF-8 multi-byte characters. */
	if ((c & 0xc0) != 0x80) {
	    ENHest_fragment_width += ENHest_fontsize;
	    /* [most] characters above 0x3000 are square CJK glyphs, */
	    /* which are wider than western characters.              */
	    if ((unsigned int)c >= 0xec)
		ENHest_fragment_width += ENHest_fontsize;
	}
    } else
	ENHest_fragment_width += ENHest_fontsize;
}

/*
 * This routine accounts for multi-byte characters in UTF-8.
 * NB:  It does not return the _number_ of characters in the string, but
 * rather their approximate _width_ in units of typical character width.
 * As with the ENHest_writec() routine, it approximates the width of characters
 * above unicode 0x3000 as being twice that of western alphabetic characters.
 */
size_t strwidth_utf8(const char *s) {
    int i = 0, j = 0;
    while (s[i]) {
       if ((s[i] & 0xc0) != 0x80) {
           j++;
	   if ((unsigned char)(s[i]) >= 0xe3)
		j++;
       }
       i++;
    }
    return j;
}


static struct termentry ENHest = {
    "estimate", "estimate width of enhanced text string",
    1, 1, 1, 1, 1, 1,
    NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL,
    NULL, ENHest_put_text, NULL,
    NULL, NULL, NULL, NULL,
    0, 0,			/* pointsize, flags */
    NULL, NULL, NULL, NULL
#ifdef USE_MOUSE
    , NULL, NULL, NULL, NULL, NULL
#endif
    , NULL, NULL, NULL, NULL
    , NULL
    , ENHest_OPEN, ENHest_FLUSH, ENHest_writec
};

#endif /* TERM_BODY */