summaryrefslogtreecommitdiffstats
path: root/Mac/Wastemods/WETabHooks.c
blob: 9595daf797925c6b48f92d14f20dfdbbd53dc4df (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 *	WETabHooks.c
 *
 *	WASTE TABS PACKAGE
 *	Hooks for adding tab support to WASTE
 *
 *	Written by:
 *		Mark Alldritt (original code)
 *		Dan Crevier (line breaks)
 *		John Daub (maintenance)
 *		Jonathan Kew (variable-width tabs)
 *		Marco Piovanelli (?)
 *		Bert Seltzer (horizontal scrolling)
 *
 */

#include "WETabs.h"
#include "WETabHooks.h"

#define FIXROUND(f)			((SInt16) (((f) + 0x00008000) >> 16))
#define BSL(A, B)			(((SInt32) (A)) << (B))

static const Point kOneToOneScaling = { 1, 1 } ;

pascal void _WETabDrawText
	(
		const char * pText,
		SInt32 textLength,
		Fixed slop,
		JustStyleCode styleRunPosition,
		WEReference we
	)
{
#pragma unused ( slop, styleRunPosition )

	LongRect destRect;
	SInt32 beginChar = 0;
	SInt32 ii;
	SInt16 tabWidth;
	SInt16 destLeft;
	Point penPos;
	SInt16 tabSize = WEGetTabSize(we);

	WEGetDestRect(&destRect, we);
	destLeft = (SInt16) destRect.left;

	for ( ii = 0; ii < textLength; ii++ )
	{
		if (pText[ii] == '\t')
		{
			DrawText(pText, beginChar, ii - beginChar);

			/* advance the pen to the next tab stop */
			GetPen(&penPos);
			tabWidth = tabSize - (penPos.h - destLeft) % tabSize;
			MoveTo(penPos.h + tabWidth, penPos.v);
			beginChar = ii + 1;
		}
	}	/* for */

	DrawText(pText, beginChar, textLength - beginChar);
}

pascal SInt32 _WETabPixelToChar
	(
		const char * pText,
		SInt32 textLength,
		Fixed slop,
		Fixed *width,
		WEEdge *edge,
		JustStyleCode styleRunPosition,
		Fixed hPos,
		WEReference we
	)
{
	SInt32 beginChar = 0;
	SInt32 offset = 0;
	SInt32 ii;
	Fixed lastWidth;
	Fixed tabWidth;
	SInt16 tabSize = WEGetTabSize(we);

	/* loop through every character in the segment looking for tabs */
	for ( ii = 0; ii < textLength; ii++ )
	{
		/* exit now if width has gone negative */
		/* (i.e., if we have found which glyph was hit) */
		if (*width <= 0)
		{
			break;
		}

		/* tab found? */
		if (pText[ii] == '\t')
		{
			/* calculate the width of the sub-segment preceding the tab */
			lastWidth = *width;
			offset += PixelToChar((char *)pText + beginChar, ii - beginChar, slop,
					lastWidth, (Boolean *) edge, width, styleRunPosition,
					kOneToOneScaling, kOneToOneScaling);
			beginChar = ii + 1;

			/* hit point past sub-segment? */
			if (*width >= 0)
			{
				/* increment hPos by width of sub-segment preceding the tab */
				hPos += (lastWidth - *width);

				/* calculate the width of the tab "glyph" (as a Fixed value) */
				tabWidth = BSL(tabSize - FIXROUND(hPos) % tabSize, 16);

				/* increment hPos by width of tab character */
				hPos += tabWidth;

				/* hit point within tab glyph? */
				if (*width < tabWidth)
				{
					/* yes: determine which half of tab glyph was hit */
					if (*width > (tabWidth >> 1))
					{
						*edge = kTrailingEdge;	/* second (trailing) edge of tab */
						offset++;
					}
					else
					{
						*edge = kLeadingEdge;	/* first (leading) edge of tab */
					}

					/* returning -1 (as Fixed) in width means we're finished */
					*width = 0xFFFF0000;
				}
				else
				{
					/* hit point is past tab: keep looping */
					offset++;
					*width -= tabWidth;
				}
			} /* if (*width >= 0) */
		} /* if tab found */
	} /* for */

	/* no more tabs in this segment: process the last sub-segment */
	if (*width >= 0)
	{
		lastWidth = *width;
		offset += PixelToChar((char *)pText + beginChar, textLength - beginChar, slop,
					lastWidth, (Boolean *) edge, width, styleRunPosition,
					kOneToOneScaling, kOneToOneScaling);
	}

	/* round width to nearest integer value */
	/* this is supposed to fix an incompatibility with the WorldScript Power Adapter */
	*width = (*width + 0x00008000) & 0xFFFF0000;

	return offset;
}

pascal SInt16 _WETabCharToPixel
	(
		const char * pText,
		SInt32 textLength,
		Fixed slop,
		SInt32 offset,
		SInt16 direction,
		JustStyleCode styleRunPosition,
		SInt16 hPos,
		WEReference we
	)
{
	LongRect destRect;
	SInt32 beginChar = 0;
	SInt32 ii;
	SInt16 width;
	SInt16 destLeft;
	SInt16 totalWidth = 0;
	SInt16 tabSize = WEGetTabSize(we);

	WEGetDestRect(&destRect, we);
	destLeft = (SInt16) destRect.left;

	/* measure text up to offset, if offset is within this segment,
		otherwise to textLength */
	if (offset > textLength)
	{
		offset = textLength;
	}

	for ( ii = 0; ii < offset; ii++ )
	{
		if (pText[ii] == '\t')
		{
			/* calculate the pixel width of the subsegment preceding the tab */
			width = TextWidth(pText, beginChar, ii - beginChar);
			totalWidth += width;
			hPos += width;

			/* calculate tab width */
			width = tabSize - (hPos - destLeft) % tabSize;
			totalWidth += width;
			hPos += width;

			/* go to next subsegment */
			beginChar = ii + 1;
		}
	} /* for */

	/* calculate width of remaining characters */
	width = CharToPixel((char *)pText + beginChar, textLength - beginChar, slop,
						offset - beginChar, direction, styleRunPosition,
						kOneToOneScaling, kOneToOneScaling);
	totalWidth += width;

	return totalWidth;
}

pascal StyledLineBreakCode _WETabLineBreak
	(
		const char * pText,
		SInt32 textLength,
		SInt32 textStart,
		SInt32 textEnd,
		Fixed *textWidth,
		SInt32 *textOffset,
		WEReference we
	)
{
	LongRect destRect;
	SInt32 beginChar = textStart;
	SInt32 ii;
	Fixed tabWidth;
	SInt16 destWidth;
	StyledLineBreakCode breakCode = smBreakOverflow;
	SInt16 tabSize = WEGetTabSize(we);

	WEGetDestRect(&destRect, we);
	destWidth = (SInt16) (destRect.right - destRect.left);

	for ( ii = textStart; ii < textEnd; ii++ )
	{
		if (pText[ii] == 0x0D)
		{
			/* found a <return>, so stop looking ahead for tabs */
			ii++;
			break;
		}
		if (pText[ii] == '\t')
		{
			/* do previous "segment" */
			breakCode = StyledLineBreak((char *)pText, textLength, beginChar, ii, 0, textWidth, textOffset);
			if ((breakCode != smBreakOverflow) || (ii >= textLength))
			{
				break;
			}
			beginChar = ii + 1;

			/* calculate tab width (as a Fixed value) */
			tabWidth = BSL(tabSize - (destWidth - FIXROUND(*textWidth)) % tabSize, 16);

			/* if tabWidth > pixelWidth we break in tab */
			/* don't move tab to next line */
			if (tabWidth > *textWidth)
			{
				breakCode = smBreakWord;
				*textOffset = ii + 1;
				break;
			}
			else
			{
				*textWidth -= tabWidth;
			}
		}
	} /* for */

	/* do last sub-segment */
	if ((ii - beginChar >= 0) && (breakCode == smBreakOverflow))
	{
		breakCode = StyledLineBreak((char *)pText, textLength, beginChar, ii, 0, textWidth, textOffset);
	}

	return breakCode;
}