--- /dev/null
+/* Hello, Emacs, this is -*-C-*-
+ * $Id: estimate.trm,v 1.4.2.1 2007/11/26 04:06: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
+ * enhanced_min_height = lowest baseline used
+ * enhanced_max_height = highest (baseline + fontsize) used
+ *
+ * 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 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;
+
+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;
+ /* Scale fractional font height */
+ ENHest_base = base * 1.0;
+ /* 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;
+ /* font size will be used to estimate width of each character */
+ ENHest_fontsize = fontsize > 2.0 ? 1.0 : fontsize;
+ }
+}
+
+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_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;
+ enhanced_max_height = 1;
+ enhanced_min_height = 0;
+
+ /* If no enhanced text processing is needed, strlen() is sufficient */
+ if (ignore_enhanced_text || !strpbrk(str, "{}^_@&~")) {
+ term->xmax = strlen(str);
+ 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_x;
+ term->ymax = enhanced_max_height - enhanced_min_height;
+}
+
+TERM_PUBLIC void
+ENHest_writec(int c)
+{
+ ENHest_fragment_width += ENHest_fontsize;
+}
+
+
+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
+#ifdef WITH_IMAGE
+ , NULL
+#endif
+ , ENHest_OPEN, ENHest_FLUSH, ENHest_writec
+};
+
+#endif /* TERM_BODY */