summaryrefslogtreecommitdiffstats
path: root/src/bltGrText.C
blob: 820df9388e259942503123e46afd66b6638c2e71 (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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * Smithsonian Astrophysical Observatory, Cambridge, MA, USA
 * This code has been modified under the terms listed below and is made
 * available under the same terms.
 */

#include <tk.h>

/*
 * 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/Xutil.h>

#include <tkPort.h>
#include <tkInt.h>
#include <tk3d.h>

#include "bltInt.h"
#include "bltMath.h"
#include "bltHash.h"
#include "bltImage.h"
#include "bltBitmap.h"
#include "bltFont.h"
#include "bltText.h"
#include "bltBgStyle.h"

static Blt_HashTable bitmapGCTable;
static int initialized;

void dump_Ts(TextStyle* ts)
{
  printf("  state: %d\n",ts->state);
  printf("  angle: %f\n",ts->angle);
  printf("  anchor: %d\n",ts->anchor);
  printf("  justify: %d\n",ts->justify);
  printf("  leader: %d\n",ts->leader);
  printf("  underline: %d\n",ts->underline);
  printf("  maxLength: %d\n",ts->maxLength);
  printf("  flags: %d\n",ts->flags);
}

GC
Blt_GetBitmapGC(Tk_Window tkwin)
{
    int isNew;
    GC gc;
    Display *display;
    Blt_HashEntry *hPtr;

    if (!initialized) {
	Blt_InitHashTable(&bitmapGCTable, BLT_ONE_WORD_KEYS);
	initialized = TRUE;
    }
    display = Tk_Display(tkwin);
    hPtr = Blt_CreateHashEntry(&bitmapGCTable, (char *)display, &isNew);
    if (isNew) {
	Pixmap bitmap;
	XGCValues gcValues;
	unsigned long gcMask;
	Window root;

	root = Tk_RootWindow(tkwin);
	bitmap = Tk_GetPixmap(display, root, 1, 1, 1);
	gcValues.foreground = gcValues.background = 0;
	gcMask = (GCForeground | GCBackground);
	gc = Blt_GetPrivateGCFromDrawable(display, bitmap, gcMask, &gcValues);
	Tk_FreePixmap(display, bitmap);
	Blt_SetHashValue(hPtr, gc);
    } else {
	gc = (GC)Blt_GetHashValue(hPtr);
    }
    return gc;
}

/*
 *---------------------------------------------------------------------------
 *
 * Blt_GetTextExtents --
 *
 *	Get the extents of a possibly multiple-lined text string.
 *
 * Results:
 *	Returns via *widthPtr* and *heightPtr* the dimensions of the text
 *	string.
 *
 *---------------------------------------------------------------------------
 */
void
Blt_GetTextExtents(
    Blt_Font font, 
    int leader,
    const char *text,		/* Text string to be measured. */
    int textLen,		/* Length of the text. If -1, indicates that
				 * text is an ASCIZ string that the length
				 * should be computed with strlen. */
    unsigned int *widthPtr, 
    unsigned int *heightPtr)
{
    unsigned int lineHeight;

    if (text == NULL) {
	return;			/* NULL string? */
    }
    {
	Blt_FontMetrics fm;

	Blt_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 = Blt_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 = Blt_TextWidth(font, line, lineLen);
	    if (lineWidth > maxWidth) {
		maxWidth = lineWidth;
	    }
	}
	*widthPtr = maxWidth;
	*heightPtr = maxHeight;
    }
}

/*
 *---------------------------------------------------------------------------
 *
 * Blt_Ts_GetExtents --
 *
 *	Get the extents of a possibly multiple-lined text string.
 *
 * Results:
 *	Returns via *widthPtr* and *heightPtr* the dimensions of
 *	the text string.
 *
 *---------------------------------------------------------------------------
 */
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 + PADDING(tsPtr->xPad);
	*heightPtr = h + PADDING(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,	/* Unrotated region */
    float angle,		/* Rotation of box */
    double *rotWidthPtr, 
    double *rotHeightPtr,	/* (out) Bounding box region */
    Point2d *bbox)		/* (out) Points of the rotated box */
{
    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_TranslateAnchor --
 *
 * 	Translate the coordinates of a given bounding box based upon the
 * 	anchor specified.  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.
 *
 *---------------------------------------------------------------------------
 */
void
Blt_TranslateAnchor(
    int x, int y,		/* Window coordinates of anchor */
    int w, int h,		/* Extents of the bounding box */
    Tk_Anchor anchor,		/* Direction of the anchor */
    int *xPtr, int *yPtr)
{
    switch (anchor) {
    case TK_ANCHOR_NW:		/* 7 Upper left corner */
	break;
    case TK_ANCHOR_W:		/* 6 Left center */
	y -= (h / 2);
	break;
    case TK_ANCHOR_SW:		/* 5 Lower left corner */
	y -= h;
	break;
    case TK_ANCHOR_N:		/* 0 Top center */
	x -= (w / 2);
	break;
    case TK_ANCHOR_CENTER:	/* 8 Center */
	x -= (w / 2);
	y -= (h / 2);
	break;
    case TK_ANCHOR_S:		/* 4 Bottom center */
	x -= (w / 2);
	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 / 2);
	break;
    case TK_ANCHOR_SE:		/* 3 Lower right corner */
	x -= w;
	y -= h;
	break;
    }
    *xPtr = x;
    *yPtr = y;
}

/*
 *---------------------------------------------------------------------------
 *
 * 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,		/* Coordinates of anchor. */
    double w, double h,		/* Extents of the bounding box */
    Tk_Anchor anchor)		/* Direction of the 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;
}

/*
 *---------------------------------------------------------------------------
 *
 * Blt_Ts_DrawText --
 *
 *	Draw a text string, possibly rotated, using the the given window
 *	coordinates as an anchor for the text bounding box.  If the text is
 *	not rotated, simply use the X text drawing routines. Otherwise,
 *	generate a bitmap of the rotated text.
 *
 * Results:
 *	Returns the x-coordinate to the right of the text.
 *
 * Side Effects:
 *	Text string is drawn using the given font and GC at the the given
 *	window coordinates.
 *
 *      The Stipple, FillStyle, and TSOrigin fields of the GC are modified for
 *      rotated text.  This assumes the GC is private, *not* shared (via
 *      Tk_GetGC)
 *
 *---------------------------------------------------------------------------
 */
void
Blt_Ts_DrawText(
    Tk_Window tkwin,
    Drawable drawable,
    const char *text,
    int textLen,
    TextStyle *stylePtr,		/* Text attribute information */
    int x, int y)			/* Window coordinates to draw text */
{
    printf("Blt_Ts_DrawText: %s\n",text);
    dump_Ts(stylePtr);

    if ((text == NULL) || (*text == '\0')) {
	return;			/* Empty string, do nothing */
    }

    Display* display = Tk_Display(tkwin);

    Tk_Font font = Tk_GetFont(stylePtr->font->interp,tkwin,"Helvetica 10 normal roman");
    GC gc = XCreateGC(display, Tk_WindowId(tkwin), 0, NULL);
    XSetFont(display, gc, Tk_FontId(font));
    //    Tk_Font font = stylePtr->font;
    //    GC gc = stylePtr->gc;

    int width,height, xx, yy;
    Tk_TextLayout layout = Tk_ComputeTextLayout(font, text, textLen, -1, 
						stylePtr->justify, 0,
						&width, &height);
    Blt_TranslateAnchor(x, y, width, height, stylePtr->anchor, &xx, &yy);
    //    printf("x=%d y=%d, xx=%d yy=%d\n",x,y,xx,yy);
    TkDrawAngledTextLayout(display, drawable, gc, layout, xx, yy, 
			   stylePtr->angle, 0, textLen);

    if (gc)
      XFreeGC(display, gc);

    if (font)
      Tk_FreeFont(font);
}

void
Blt_DrawText2(
    Tk_Window tkwin,
    Drawable drawable,
    const char *text,
    TextStyle *stylePtr,		/* Text attribute information */
    int x, int y,		/* Window coordinates to draw text */
    Dim2D *areaPtr)
{
    printf("Blt_DrawText2: %s\n",text);
    dump_Ts(stylePtr);

    if ((text == NULL) || (*text == '\0')) {
	return;			/* Empty string, do nothing */
    }

    Display* display = Tk_Display(tkwin);

    Tk_Font font = Tk_GetFont(stylePtr->font->interp,tkwin,"Helvetica 10 normal roman");
    GC gc = XCreateGC(display, Tk_WindowId(tkwin), 0, NULL);
    XSetFont(display, gc, Tk_FontId(font));
    //    Tk_Font font = stylePtr->font;
    //    GC gc = stylePtr->gc;

    int width,height, xx, yy;
    Tk_TextLayout layout = Tk_ComputeTextLayout(font, text, -1, -1, 
						stylePtr->justify, 0,
						&width, &height);
    Blt_TranslateAnchor(x, y, width, height, stylePtr->anchor, &xx, &yy);
    TkDrawAngledTextLayout(display, drawable, gc, layout, xx, yy, 
			   stylePtr->angle, 0, -1);

    if (gc)
      XFreeGC(display, gc);

    if (font)
      Tk_FreeFont(font);

    float angle = fmod(stylePtr->angle, 360.0);
    if (angle < 0.0) {
	angle += 360.0;
    }
    //    width = layoutPtr->width;
    //    height = layoutPtr->height;
    if (angle != 0.0) {
	double rotWidth, rotHeight;

	Blt_GetBoundingBox(width, height, angle, &rotWidth, &rotHeight, 
	   (Point2d *)NULL);
	width = ROUND(rotWidth);
	height = ROUND(rotHeight);
    }
    areaPtr->width = width;
    areaPtr->height = height;
    //    free(layoutPtr);
}

void
Blt_DrawText(
    Tk_Window tkwin,
    Drawable drawable,
    const char *text,
    TextStyle *stylePtr,		/* Text attribute information */
    int x, int y)		/* Window coordinates to draw text */
{
    printf("Blt_DrawText: %s\n",text);
    dump_Ts(stylePtr);

    if ((text == NULL) || (*text == '\0')) {
	return;			/* Empty string, do nothing */
    }

    Display* display = Tk_Display(tkwin);

    Tk_Font font = Tk_GetFont(stylePtr->font->interp,tkwin,"Helvetica 10 normal roman");
    GC gc = XCreateGC(display, Tk_WindowId(tkwin), 0, NULL);
    XSetFont(display, gc, Tk_FontId(font));
    //    Tk_Font font = stylePtr->font;
    //    GC gc = stylePtr->gc;

    int width,height, xx, yy;
    Tk_TextLayout layout = Tk_ComputeTextLayout(font, text, -1, -1, 
						stylePtr->justify, 0,
						&width, &height);
    Blt_TranslateAnchor(x, y, width, height, stylePtr->anchor, &xx, &yy);
    TkDrawAngledTextLayout(display, drawable, gc, layout, xx, yy, 
			   stylePtr->angle, 0, -1);

    if (gc)
      XFreeGC(display, gc);

    if (font)
      Tk_FreeFont(font);
}

void
Blt_Ts_ResetStyle(Tk_Window tkwin, TextStyle *stylePtr)
{
    GC newGC;
    XGCValues gcValues;
    unsigned long gcMask;

    gcMask = GCFont;
    gcValues.font = Blt_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);
    }
}