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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
/*
* Smithsonian Astrophysical Observatory, Cambridge, MA, USA
* This code has been modified under the terms listed below and is made
* available under the same terms.
*/
/*
* bltText.c --
*
* This module implements multi-line, rotate-able text for the BLT toolkit.
*
* Copyright 1993-2004 George A Howlett.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <tk.h>
#include <tkPort.h>
#include <tkInt.h>
#include "bltInt.h"
#include "bltMath.h"
#include "bltText.h"
#define ROTATE_0 0
#define ROTATE_90 1
#define ROTATE_180 2
#define ROTATE_270 3
void Blt_GetTextExtents(Tk_Font font, int leader, const char *text, int textLen,
unsigned int *widthPtr, unsigned int *heightPtr)
{
unsigned int lineHeight;
if (text == NULL)
return;
{
Tk_FontMetrics fm;
Tk_GetFontMetrics(font, &fm);
lineHeight = fm.linespace;
}
if (textLen < 0) {
textLen = strlen(text);
}
{
unsigned int lineLen; /* # of characters on each line */
const char *p, *pend;
const char *line;
unsigned int maxWidth, maxHeight;
maxWidth = maxHeight = 0;
lineLen = 0;
for (p = line = text, pend = text + textLen; p < pend; p++) {
if (*p == '\n') {
if (lineLen > 0) {
unsigned int lineWidth;
lineWidth = Tk_TextWidth(font, line, lineLen);
if (lineWidth > maxWidth) {
maxWidth = lineWidth;
}
}
maxHeight += lineHeight;
line = p + 1; /* Point to the start of the next line. */
lineLen = 0; /* Reset counter to indicate the start of a
* new line. */
continue;
}
lineLen++;
}
if ((lineLen > 0) && (*(p - 1) != '\n')) {
unsigned int lineWidth;
maxHeight += lineHeight;
lineWidth = Tk_TextWidth(font, line, lineLen);
if (lineWidth > maxWidth) {
maxWidth = lineWidth;
}
}
*widthPtr = maxWidth;
*heightPtr = maxHeight;
}
}
void Blt_Ts_GetExtents(TextStyle *tsPtr, const char *text,
unsigned int *widthPtr, unsigned int *heightPtr)
{
if (text == NULL)
*widthPtr = *heightPtr = 0;
else {
unsigned int w, h;
Blt_GetTextExtents(tsPtr->font, tsPtr->leader, text, -1, &w, &h);
*widthPtr = w + 2*tsPtr->xPad;
*heightPtr = h + 2*tsPtr->yPad;
}
}
/*
*---------------------------------------------------------------------------
*
* Blt_GetBoundingBox
*
* Computes the dimensions of the bounding box surrounding a rectangle
* rotated about its center. If pointArr isn't NULL, the coordinates of
* the rotated rectangle are also returned.
*
* The dimensions are determined by rotating the rectangle, and doubling
* the maximum x-coordinate and y-coordinate.
*
* w = 2 * maxX, h = 2 * maxY
*
* Since the rectangle is centered at 0,0, the coordinates of the
* bounding box are (-w/2,-h/2 w/2,-h/2, w/2,h/2 -w/2,h/2).
*
* 0 ------- 1
* | |
* | x |
* | |
* 3 ------- 2
*
* Results:
* The width and height of the bounding box containing the rotated
* rectangle are returned.
*
*---------------------------------------------------------------------------
*/
void Blt_GetBoundingBox(int width, int height, float angle,
double *rotWidthPtr, double *rotHeightPtr,
Point2d *bbox)
{
int i;
double sinTheta, cosTheta;
double radians;
double xMax, yMax;
double x, y;
Point2d corner[4];
angle = fmod(angle, 360.0);
if (fmod(angle, (double)90.0) == 0.0) {
int ll, ur, ul, lr;
double rotWidth, rotHeight;
int quadrant;
/* Handle right-angle rotations specially. */
quadrant = (int)(angle / 90.0);
switch (quadrant) {
case ROTATE_270: /* 270 degrees */
ul = 3, ur = 0, lr = 1, ll = 2;
rotWidth = (double)height;
rotHeight = (double)width;
break;
case ROTATE_90: /* 90 degrees */
ul = 1, ur = 2, lr = 3, ll = 0;
rotWidth = (double)height;
rotHeight = (double)width;
break;
case ROTATE_180: /* 180 degrees */
ul = 2, ur = 3, lr = 0, ll = 1;
rotWidth = (double)width;
rotHeight = (double)height;
break;
default:
case ROTATE_0: /* 0 degrees */
ul = 0, ur = 1, lr = 2, ll = 3;
rotWidth = (double)width;
rotHeight = (double)height;
break;
}
if (bbox != NULL) {
x = rotWidth * 0.5;
y = rotHeight * 0.5;
bbox[ll].x = bbox[ul].x = -x;
bbox[ur].y = bbox[ul].y = -y;
bbox[lr].x = bbox[ur].x = x;
bbox[ll].y = bbox[lr].y = y;
}
*rotWidthPtr = rotWidth;
*rotHeightPtr = rotHeight;
return;
}
/* Set the four corners of the rectangle whose center is the origin. */
corner[1].x = corner[2].x = (double)width * 0.5;
corner[0].x = corner[3].x = -corner[1].x;
corner[2].y = corner[3].y = (double)height * 0.5;
corner[0].y = corner[1].y = -corner[2].y;
radians = (-angle / 180.0) * M_PI;
sinTheta = sin(radians), cosTheta = cos(radians);
xMax = yMax = 0.0;
/* Rotate the four corners and find the maximum X and Y coordinates */
for (i = 0; i < 4; i++) {
x = (corner[i].x * cosTheta) - (corner[i].y * sinTheta);
y = (corner[i].x * sinTheta) + (corner[i].y * cosTheta);
if (x > xMax) {
xMax = x;
}
if (y > yMax) {
yMax = y;
}
if (bbox != NULL) {
bbox[i].x = x;
bbox[i].y = y;
}
}
/*
* By symmetry, the width and height of the bounding box are twice the
* maximum x and y coordinates.
*/
*rotWidthPtr = xMax + xMax;
*rotHeightPtr = yMax + yMax;
}
/*
*---------------------------------------------------------------------------
*
* Blt_AnchorPoint --
*
* Translates a position, using both the dimensions of the bounding box,
* and the anchor direction, returning the coordinates of the upper-left
* corner of the box. The anchor indicates where the given x-y position
* is in relation to the bounding box.
*
* 7 nw --- 0 n --- 1 ne
* | |
* 6 w 8 center 2 e
* | |
* 5 sw --- 4 s --- 3 se
*
* The coordinates returned are translated to the origin of the bounding
* box (suitable for giving to XCopyArea, XCopyPlane, etc.)
*
* Results:
* The translated coordinates of the bounding box are returned.
*
*---------------------------------------------------------------------------
*/
Point2d
Blt_AnchorPoint(double x, double y, double w, double h, Tk_Anchor anchor)
{
Point2d t;
switch (anchor) {
case TK_ANCHOR_NW: /* 7 Upper left corner */
break;
case TK_ANCHOR_W: /* 6 Left center */
y -= (h * 0.5);
break;
case TK_ANCHOR_SW: /* 5 Lower left corner */
y -= h;
break;
case TK_ANCHOR_N: /* 0 Top center */
x -= (w * 0.5);
break;
case TK_ANCHOR_CENTER: /* 8 Center */
x -= (w * 0.5);
y -= (h * 0.5);
break;
case TK_ANCHOR_S: /* 4 Bottom center */
x -= (w * 0.5);
y -= h;
break;
case TK_ANCHOR_NE: /* 1 Upper right corner */
x -= w;
break;
case TK_ANCHOR_E: /* 2 Right center */
x -= w;
y -= (h * 0.5);
break;
case TK_ANCHOR_SE: /* 3 Lower right corner */
x -= w;
y -= h;
break;
}
t.x = x;
t.y = y;
return t;
}
static Point2d Rotate_Text(int x, int y, int w1, int h1, TextStyle* stylePtr)
{
// Matrix t0 = Translate(-x,-y);
// Matrix t1 = Translate(-w1/2,-h1/2);
// Matrix rr = Rotate(angle);
// Matrix t2 = Translate(w2/2,h2/2);
// Matrix t3 = Translate(x,y);
double angle, ccos, ssin;
double w2, h2;
double x1, y1, x2, y2;
double rx, ry;
angle = stylePtr->angle;
ccos = cos(M_PI*angle/180.);
ssin = sin(M_PI*angle/180.);
Blt_GetBoundingBox(w1, h1, angle, &w2, &h2, (Point2d *)NULL);
x1 = x+w1/2.;
y1 = y+h1/2.;
x2 = w2/2.+x;
y2 = h2/2.+y;
rx = x*ccos + y*ssin + (-x1*ccos -y1*ssin +x2);
ry = -x*ssin + y*ccos + ( x1*ssin -y1*ccos +y2);
return Blt_AnchorPoint(rx, ry, w2, h2, stylePtr->anchor);
}
void Blt_Ts_DrawText(Tk_Window tkwin, Drawable drawable, const char *text,
int textLen, TextStyle *stylePtr,int x, int y)
{
Tk_TextLayout layout;
int w1, h1;
Point2d rr;
if ((text == NULL) || (*text == '\0'))
return;
if ((stylePtr->gc == NULL) || (stylePtr->flags & UPDATE_GC))
Blt_Ts_ResetStyle(tkwin, stylePtr);
layout = Tk_ComputeTextLayout(stylePtr->font, text, textLen,-1,
stylePtr->justify, 0, &w1, &h1);
rr = Rotate_Text(x, y, w1, h1, stylePtr);
TkDrawAngledTextLayout(Tk_Display(tkwin), drawable, stylePtr->gc, layout,
rr.x, rr.y, stylePtr->angle, 0, textLen);
}
void Blt_DrawText2(Tk_Window tkwin, Drawable drawable, const char *text,
TextStyle *stylePtr, int x, int y, Dim2D *areaPtr)
{
Tk_TextLayout layout;
int w1, h1;
Point2d rr;
if ((text == NULL) || (*text == '\0'))
return;
if ((stylePtr->gc == NULL) || (stylePtr->flags & UPDATE_GC))
Blt_Ts_ResetStyle(tkwin, stylePtr);
layout = Tk_ComputeTextLayout(stylePtr->font, text, -1, -1,
stylePtr->justify, 0, &w1, &h1);
rr = Rotate_Text(x, y, w1, h1, stylePtr);
TkDrawAngledTextLayout(Tk_Display(tkwin), drawable, stylePtr->gc, layout,
rr.x, rr.y, stylePtr->angle, 0, -1);
float angle = fmod(stylePtr->angle, 360.0);
if (angle < 0.0)
angle += 360.0;
if (angle != 0.0) {
double rotWidth, rotHeight;
Blt_GetBoundingBox(w1, h1, angle, &rotWidth, &rotHeight,
(Point2d *)NULL);
w1 = ROUND(rotWidth);
h1 = ROUND(rotHeight);
}
areaPtr->width = w1;
areaPtr->height = h1;
}
void Blt_DrawText(Tk_Window tkwin, Drawable drawable, const char *text,
TextStyle *stylePtr, int x, int y)
{
Tk_TextLayout layout;
int w1, h1;
Point2d rr;
if ((text == NULL) || (*text == '\0'))
return;
if ((stylePtr->gc == NULL) || (stylePtr->flags & UPDATE_GC))
Blt_Ts_ResetStyle(tkwin, stylePtr);
layout = Tk_ComputeTextLayout(stylePtr->font, text, -1, -1,
stylePtr->justify, 0, &w1, &h1);
rr = Rotate_Text(x, y, w1, h1, stylePtr);
TkDrawAngledTextLayout(Tk_Display(tkwin), drawable, stylePtr->gc, layout,
rr.x, rr.y, stylePtr->angle, 0, -1);
}
void Blt_Ts_ResetStyle(Tk_Window tkwin, TextStyle *stylePtr)
{
GC newGC;
XGCValues gcValues;
unsigned long gcMask;
gcMask = GCFont;
gcValues.font = Tk_FontId(stylePtr->font);
if (stylePtr->color != NULL) {
gcMask |= GCForeground;
gcValues.foreground = stylePtr->color->pixel;
}
newGC = Tk_GetGC(tkwin, gcMask, &gcValues);
if (stylePtr->gc != NULL) {
Tk_FreeGC(Tk_Display(tkwin), stylePtr->gc);
}
stylePtr->gc = newGC;
stylePtr->flags &= ~UPDATE_GC;
}
void Blt_Ts_FreeStyle(Display *display, TextStyle *stylePtr)
{
if (stylePtr->gc != NULL)
Tk_FreeGC(display, stylePtr->gc);
}
|