File: tabstr.c

package info (click to toggle)
v1 1.20-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 6,240 kB
  • ctags: 9,439
  • sloc: cpp: 48,033; ansic: 8,939; makefile: 1,369; sh: 30
file content (189 lines) | stat: -rw-r--r-- 4,604 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
/*
 *	Functions for drawing String's with tab characters in them
 */

#include <stdlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include "tabstr.h"

/*
 *	Like DrawImageString, except it takes an additional  "tabs"
 *	argument, used to specify what horizontal pixel position to
 *	move to when tab characters are present in the string.  If
 *	the "tabs" argument is NULL, works exactly like its
 *	counterpart.
 */
void
XfwfDrawImageString(display, drawable, gc, font, x, y, string, length, tabs, underline)
     Display *display;
     Drawable drawable;
     GC gc;
     XFontStruct *font;
     int x;
     int y;
     String string;
     int length;
     int *tabs;
     char *underline;
{
    register char	*p, *ep;
    register int	tx, tab;

    tab = tx = 0;
    for (p = string; length; ) {
	ep = strnchr(p, '\t', length);
	if (underline && p <= underline && underline < ((ep && tabs) ? ep : p+length)) {
	    int _x = 0, _wd = 0;
	    if (p != underline) {
		int direction, ascent, descent; XCharStruct overall;
		XTextExtents(font, p, underline - p, &direction, &ascent, &descent, &overall);
		_x = overall.width;
	    }
	    _wd = XTextWidth(font, underline, 1);
	    XDrawLine(display, drawable, gc,
		      x+tx + _x      , y + font->max_bounds.descent - 1,
		      x+tx + _x + _wd, y + font->max_bounds.descent - 1);
	}
	if (ep && tabs) {
	    XDrawImageString(display, drawable, gc, x+tx, y, p, ep - p);
	    tx = tabs[tab++];
	    length -= ep - p + 1;
	    p = ep + 1;
	} else {
	    XDrawImageString(display, drawable, gc, x+tx, y, p, length);
	    break;
	}
    }
}

/*
 *	Like DrawString, except it takes an additional  "tabs"
 *	argument, used to specify what horizontal pixel position to
 *	move to when tab characters are present in the string.  If
 *	the "tabs" argument is NULL, works exactly like its
 *	counterpart.
 */
void XfwfDrawString(display, drawable, gc, font, x, y, string, length, tabs, underline)
     Display *display;
     Drawable drawable;
     GC gc;
     XFontStruct *font;
     int x;
     int y;
     String string;
     int length;
     int *tabs;
     char *underline;
{
    register char *p, *ep;
    register int  tx, tab;
    
    tab = tx = 0;
    for (p = string; length; ) {
	ep = strnchr(p, '\t', length);
	if (underline && p <= underline && underline < ((ep && tabs) ? ep : p+length)) {
	    int _x = 0, _wd = 0;
	    if (p != underline) {
		int direction, ascent, descent; XCharStruct overall;
		XTextExtents(font, p, underline - p, &direction, &ascent, &descent, &overall);
		_x = overall.width;
	    }
	    _wd = XTextWidth(font, underline, 1);
	    XDrawLine(display, drawable, gc,
		      x+tx + _x      , y + font->max_bounds.descent - 1,
		      x+tx + _x + _wd, y + font->max_bounds.descent - 1);
	}
	if (ep && tabs) {
	    XDrawString(display, drawable, gc, x+tx, y, p, ep - p);
	    tx = tabs[tab++];
	    length -= ep - p + 1;
	    p = ep + 1;
	} else {
	    XDrawString(display, drawable, gc, x+tx, y, p, length);
	    break;
	}
    }
}

/*
 *	Converts a string list of tabs to an array of tabs
 */
int *XfwfTablist2Tabs(tablist)
    char *tablist;
{
	register int	*tabs = NULL;
	register int	ntabs = 0;

	if (!tablist)
		return NULL;
	for (;;)
	{
		/* Skip leading blanks */
		while (*tablist && *tablist == ' ') ++tablist;
		if (!*tablist) break;

		/* Allocate space for the new tab */
		if (ntabs)
			tabs = (int *) XtRealloc( (char *) tabs,
						(ntabs+1) * sizeof(int));
		else
			tabs = (int *) XtMalloc( (ntabs + 1) * sizeof(int));
		/* Add it to the list */
		tabs[ntabs++] = atoi(tablist);
		/* Skip to the next blank */
		while (*tablist && *tablist != ' ') ++tablist;
	}
	return (tabs);
}

/*
 *	Like TextWidth, except it takes an additional  "tabs"
 *	argument, used to specify what horizontal pixel position to
 *	move to when tab characters are present in the string.  If
 *	the "tabs" argument is NULL, works exactly like its
 *	counterpart.
 */
int
XfwfTextWidth(font, str, length, tabs)
     XFontStruct *font;
     String str;
     int length;
     int *tabs;
{
	register char	*p, *ep;
	register int	tx, tab, rc;

	tab = tx = 0;
 	if (length == 0) return 0;
	for (p = str; length; )
	{
		ep = strnchr(p, '\t', length);
		if (ep && tabs)
		{
			tx = tabs[tab++];
			length -= ep - p + 1;
			p = ep + 1;
		}
		else
		{
			rc = XTextWidth(font, p, length);
			if (rc < 0) return rc; else return rc + tx;
		}
	}
	return -1;
}

/*
 *	Like strchr, except has a length limit.
 */
char *
strnchr(s, c, n)
     char *s;
     int c;
     int n;
{
	while (n--)
		if (*s == c) return s; else ++s;
	return NULL;
}