summaryrefslogtreecommitdiffstats
path: root/format.c
blob: edcbf3aeadc4f2c679cd3109114fb939a3f2dd97 (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (c) 1997
 * Digital Equipment Corporation.  All rights reserved.
 * 
 * By downloading, installing, using, modifying or distributing this
 * software, you agree to the following:
 * 
 * 1. CONDITIONS. Subject to the following conditions, you may download,
 * install, use, modify and distribute this software in source and binary
 * forms:
 * 
 * a) Any source code, binary code and associated documentation
 * (including the online manual) used, modified or distributed must
 * reproduce and retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 * 
 * b) No right is granted to use any trade name, trademark or logo of
 * Digital Equipment Corporation.  Neither the "Digital Equipment
 * Corporation" name nor any trademark or logo of Digital Equipment
 * Corporation may be used to endorse or promote products derived from
 * this software without the prior written permission of Digital
 * Equipment Corporation.
 * 
 * 2.  DISCLAIMER.  THIS SOFTWARE IS PROVIDED BY DIGITAL "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <X11/Xlib.h>
#include <string.h>
#include "format.h"

static void formatMeasureText();

void formatText(dpy, textw, pgc, tfont, nformats, formatv, pwidth, pheight)
Display *dpy;
Window  textw;
GC      *pgc;
XFS     *tfont;
int     nformats;
Format  *formatv;
int     *pwidth, *pheight;
{
  int   fontheight, fontwidth;
  int   x, y;
  int   len, textwidth, xoffset;
  int   format;
  int   top, left, lmargin, rmargin;
  int   width, height;
  char  *buffer;
  Bool  bMLineCenter;

  top = left = lmargin = rmargin = x = y = width = height = 0;
  fontheight = tfont->ascent + tfont->descent;
  fontwidth  = tfont->max_bounds.width;
  bMLineCenter = False;

  for (format = 0; format < nformats; ++format) {
    switch (formatv[format]) {
    case FormatSetTop:
      y = top = formatv[++format];
      break;
    case FormatSetLeft:
      x = left = formatv[++format];
      break;
    case FormatHome:
      x = left + lmargin;
      y = top;
      break;
    case FormatCR:
      x = left + lmargin;
      break;
    case FormatNL:
      x = left + lmargin;
      y += fontheight;
      break;
    case FormatSetY:
      y = top + formatv[++format];
      break;
    case FormatSetTextY:
      y = top + fontheight * formatv[++format];
      break;
    case FormatAddY:
      y += formatv[++format];
      break;
    case FormatAddTextY:
      y += fontheight * formatv[++format];
      break;
    case FormatAddHalfTextY:
      y += (fontheight >> 1) * formatv[++format];
      break;
    case FormatSetX:
      x = left + formatv[++format];
      break;
    case FormatSetTextX:
      x = left + fontwidth * formatv[++format];
      break;
    case FormatAddX:
      x += formatv[++format];
      break;
    case FormatAddTextX:
      x += fontwidth * formatv[++format];
      break;
    case FormatAddHalfTextX:
      x += (fontwidth >> 1) * formatv[++format];
      break;
    case FormatString:
      len = strlen((char *)formatv[++format]);
      if (textw)
	XDrawString(dpy, textw, *pgc, x, y, (char *)formatv[format], len);
      x += XTextWidth(tfont, (char *)formatv[format], len);
      break;
    case FormatStringCenter:
      x = left;
      len = strlen((char *)formatv[++format]);
      textwidth = XTextWidth(tfont, (char *)formatv[format], len);
      xoffset = MAX(lmargin, ((width >> 1) - (textwidth >> 1)));
      if (textw)
	XDrawString(dpy, textw, *pgc, x + xoffset, y, 
		    (char *)formatv[format], len);
      x += textwidth;
      break;
    case FormatMultiLine:
      buffer = (char *)formatv[++format];
      len = 0;
      x = left;
      while (*buffer) {
	/* if first character in the line is a tab, then center */
	if (buffer[len] == '\t') {
	  buffer++; /* don't print the tab */
	  bMLineCenter = True;
	}
	while ((buffer[len] != '\n') && buffer[len])
	  len++;
	textwidth = XTextWidth(tfont, buffer, len);
	if (textw) {
	  if (bMLineCenter) {
	    xoffset = MAX(lmargin, ((width >> 1) - (textwidth >> 1)));
	    bMLineCenter = False; /* ready for next line */
	  } else { /* normal print */
	    xoffset = lmargin;
	  }
	  XDrawString(dpy, textw, *pgc, left + xoffset, y, 
		      (char *)buffer, len);
	}
	if (buffer[len]) {
	  width = MAX(width, left + lmargin + textwidth + rmargin);
	  buffer += len + 1;
	  len = 0;
	  y += fontheight;
	}
	else {
	  x = left + lmargin + textwidth;
	  break;
	}
      }
      break;
    case FormatMeasureText:
      formatMeasureText(tfont, (nformats - format - 1),
			&formatv[format + 1], &fontwidth, &fontheight);
      break;
    case FormatSetWidth:
      width = formatv[++format];
      break;
    case FormatSetLMargin:
      if ((lmargin = formatv[++format]) == -1)
	lmargin = MAX(0, x - left);
      break;
    case FormatSetRMargin:
      if ((rmargin = formatv[++format]) == -1)
	rmargin = MAX(0, x - left);
      break;
    default: /* might want an error message here */
      break;
    } /* END switch */

    width  = MAX(width,  x - left + rmargin);
    height = MAX(height, y - top);

  } /* END for */

  if (pwidth)  *pwidth  = width;
  if (pheight) *pheight = height;

} /* END formatText */

static void formatMeasureText(tfont, nformats, formatv, pwidth, pheight)
XFS     *tfont;
int     nformats;
Format  *formatv;
int     *pwidth, *pheight;
{
  int           fontheight, fontwidth;
  int           width, height;
  int           format;
  unsigned      minchar, maxchar;
  unsigned char *buffer, thechar;
  XCharStruct   *xcs;
  
  fontheight = fontwidth = 0;
  minchar = tfont->min_char_or_byte2;
  maxchar = tfont->max_char_or_byte2;

  for (format = 0; format < nformats; ++format) {
    switch (formatv[format]) {
    case FormatHome: case FormatCR: case FormatNL: case FormatMeasureText:
      /* nothing to do */
      break;
    case FormatString: case FormatStringCenter: case FormatMultiLine:
      for (buffer = (unsigned char *)formatv[++format];
	   (thechar = *buffer);
	   buffer++) {
	if ((thechar != '\n') && (thechar != '\t') &&
	    (minchar <= thechar) && (thechar <= maxchar)) {
	  xcs = &(tfont->per_char[thechar]);
	  if (fontwidth < (width = xcs->width))
	    fontwidth = width;
	  if (fontheight < (height = (xcs->ascent + xcs->descent)))
	    fontheight = height;
	} /* END if thechar */
      } /* END for buffer */
      break;
    default: /* most format instructions need to increment the counter */
      format++;
      break;
    } /* END switch */
  } /* END for */

  if (pwidth)  *pwidth  = fontwidth;
  if (pheight) *pheight = fontheight;

} /* END formatMeasureText */