File: face_annotate.c

package info (click to toggle)
xfaces 3.3-29
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 948 kB
  • ctags: 1,414
  • sloc: ansic: 7,487; makefile: 948; sh: 5
file content (590 lines) | stat: -rw-r--r-- 13,064 bytes parent folder | download | duplicates (7)
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
/*                             -*- Mode: C++-C -*- 
 * 
 *		 Copyright 1994 Christopher B. Liebman
 *
 *     Permission to use, copy, modify, distribute, and sell this software
 *     and its documentation for any purpose is hereby granted without fee,
 *     provided that the above copyright notice appear in all copies and that
 *     both that copyright notice and this permission notice appear in
 *     supporting documentation, and that the name Christopher B. Liebman not
 *     be used in advertising or publicity pertaining to distribution of this
 *     software without specific, written prior permission.
 *
 *    THIS SOFTWARE IS PROVIDED `AS-IS'.  CHRISTOPHER B. LIEBMAN, DISCLAIMS
 *    ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT
 *    LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *    PARTICULAR PURPOSE, OR NONINFRINGEMENT.  IN NO EVENT SHALL CHRISTOPHER
 *    B. LIEBMAN, BE LIABLE FOR ANY DAMAGES WHATSOEVER, INCLUDING SPECIAL,
 *    INCIDENTAL OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS OF USE, DATA, OR
 *    PROFITS, EVEN IF ADVISED OF THE POSSIBILITY THEREOF, AND REGARDLESS OF
 *    WHETHER IN AN ACTION IN CONTRACT, TORT OR NEGLIGENCE, ARISING OUT OF
 *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author          : Chris Liebman
 * Created On      : Tue Jan 11 14:11:30 1994
 * Last Modified By: Chris Liebman
 * Last Modified On: Mon Mar  7 18:02:58 1994
 * Update Count    : 285
 * Status          : Released
 * 
 * HISTORY
 * 6-Mar-1994		Chris Liebman	
 *    Last Modified: Sun Mar  6 12:28:00 1994 #274 (Chris Liebman)
 *    Fixed a problem with FaceAnnotateWork being called with a struct
 *    rather than a pointer to a struct!
 *
 * 13-Feb-1994		Chris Liebman	
 *    Last Modified: Sun Feb 13 00:38:41 1994 #271 (Chris Liebman)
 *    Make images that are smaller than the tile size bigger and
 *    Allow choice as to weather the extra height is on the top or the
 *    bottom.  The image is always centered left to right.
 * 
 * PURPOSE
 * 	Handle image annotation.
*/

#ifndef lint
static char *RCSid = "$Id: face_annotate.c,v 1.4 1994/03/08 02:16:44 liebman Exp $";
#endif

#include "faces.h"
#include "face_image.h"

typedef struct annotation Annotation;

struct annotation
{
    Position		x;
    Position		y;
    Dimension		max_width;
    Pixel		fg;
    Pixel		bg;
    XFontStruct*	font;
    Boolean		shape_text;
    Boolean		opaque_text;
};


#define	offset(field)	XtOffset(Annotation*, field)

static XtResource AnnotationResourcesList[] =
{
    {
	"x", "Position", XtRPosition, sizeof(Position),
	offset(x), XtRString, "0"
    },
    {
	"y", "Position", XtRPosition, sizeof(Position),
	offset(y), XtRString, "0"
    },
    {
	"maxWidth", "Dimension", XtRDimension, sizeof(Dimension),
	offset(max_width), XtRString, "48"
    },
    {
	"foreground", "Foreground", XtRPixel, sizeof(Pixel),
	offset(fg), XtRString, XtDefaultForeground
    },
    {
	"background", "Background", XtRPixel, sizeof(Pixel),
	offset(bg), XtRString, XtDefaultBackground
    },
    {
	"font", "Font", XtRFontStruct, sizeof(XFontStruct *),
	offset(font), XtRString, XtDefaultFont
    },
    {
	"shapeText", "ShapeText", XtRBoolean, sizeof(Boolean),
	offset(shape_text), XtRString, "False"
    },
    {
	"opaqueText", "OpaqueText", XtRBoolean, sizeof(Boolean),
	offset(opaque_text), XtRString, "True"
    },
};

#undef offset

static int		annotationCount = 0;
static Annotation*	annotations = NULL;

static int		unknownAnnotationCount = 0;
static Annotation*	unknownAnnotations = NULL;

static void
FaceAnnotateWork(face, a, astr, imageWidth, imageHeight, imageGC, shapeGC)
Face*		face;
Annotation*	a;
char*		astr;
unsigned int	imageWidth;
unsigned int	imageHeight;
GC		imageGC;
GC		shapeGC;
{
    int			length;
    int			dir, ascent, descent;
    int			x, y;
    int			width, height;
    int			maxWidth;
    XCharStruct		size;
    Display*		display = XtDisplay(TheTopLevel);
    
    if (astr == NULL || *astr == '\0')
    {
	return;
    }
    
    /*
     *  Compute max allowable width for text.
    */
    
    x = a->x;
    y = a->y;
    
    if (x < 0)
    {
	maxWidth = imageWidth + x;
    }
    else
    {
	maxWidth = imageWidth - x;
    }
    
    if (maxWidth > a->max_width)
    {
	maxWidth = a->max_width;
    }

    /*
     *  Compute the size of the string.
    */
    
    size.width = imageWidth + 1;
    for(length = strlen(astr); length > 0; --length)
    {
	XTextExtents(a->font, astr, length, &dir, &ascent, &descent, &size);
	
	if (size.width <= maxWidth)
	{
	    break;
	}
    }
    
    if (length == 0)
    {
	length = 1;
    }
    
    width  = size.width;
    height = ascent + descent;
    
    /*
     * Upper left corner of text.
    */
    
    if (x < 0)
    {
	x = imageWidth - width + x;
    }
    
    if (y < 0)
    {
	y = imageHeight - height + y;
    }
    
    /*
     * Set shape mask area for label if shaped.
    */
    
    if (face->shape != None)
    {
	if (a->shape_text)
	{
	    XDrawString(display, face->shape, shapeGC,
			x, y+ascent, astr, length);
	}
	else
	{
	    XFillRectangle(display, face->shape, shapeGC,
			   x, y, width, height);
	}
    }
    
    if (a->opaque_text)
    {
	XDrawImageString(display, face->pixmap, imageGC,
			 x, y+ascent, astr, length);
    }
    else
    {
	XDrawString(display, face->pixmap, imageGC,
			 x, y+ascent, astr, length);
    }
}

void
FaceAnnotateFree(face)
Face *face;
{
    int i;
    
    if (face->annotations == NULL)
    {
	face->pixmap = None;
	face->shape  = None;
	return;
    }
    
    for(i = 0; face->annotations[i]; ++i)
    {
	XtFree(face->annotations[i]);
    }
    
    XtFree((char*)face->annotations);
    
    if (face->pixmap != None)
    {
	XFreePixmap(XtDisplay(TheTopLevel), face->pixmap);
    }
    
    if (face->shape != None)
    {
	XFreePixmap(XtDisplay(TheTopLevel), face->shape);
    }
    
    face->annotations = NULL;
    face->pixmap      = None;
    face->shape       = None;
}

/*
 *   Return 1 if image changed!
*/

int
FaceAnnotate(face)
Face*	face;
{
    Pixmap		pixmap;
    Pixmap		shape;
    unsigned int	imageWidth;
    unsigned int	imageHeight;
    int			imageDepth;
    Window		imageRoot;
    unsigned int	shapeWidth;
    unsigned int	shapeHeight;
    int			shapeDepth;
    Window		shapeRoot;
    int			junkx, junky;
    unsigned int	junkbw;
    unsigned int	mask;
    XGCValues		values;
    GC			imageGC;
    GC			shapeGC;
    Display*		display = XtDisplay(TheTopLevel);
    int			i;
    int			x = 0;
    int			y = 0;
    char**		adata;
    Dimension		tileHeight;
    Dimension		tileWidth;
    int			extraHeight = 0;
    int			extraWidth = 0;
    int			count;
    Annotation*		annos;

    if (face->items->item->unknown)
    {
	count = unknownAnnotationCount;
	annos = unknownAnnotations;
    }
    else
    {
	count = annotationCount;
	annos = annotations;
    }
    
    /*
     *  We use the newest mail item to do the annotations.
    */
    
    adata = face->items->item->annotations;
    
    /*
     *  If there is no "new" annotation data now then just clear it all off.
    */
    
    if (adata == NULL)
    {
 	pixmap = face->pixmap;
 	FaceAnnotateFree(face);
 	face->pixmap = FaceImagePixmap(face->image);
 	face->shape  = FaceImageShape(face->image);
  	
 	return (pixmap != face->pixmap);
    }
    
    /*
     *  If there is old annotation data then see if it the same as the
     * new data.  If it is then we need to do nothing!
    */
    
    if (face->annotations != NULL)
    {
	
	for(i = 0;  i < count; ++i)
	{
	    if ((face->annotations[i] == NULL) ||
		(adata[i]             == NULL) ||
		(strcmp(face->annotations[i], adata[i]) != 0))
	    {
		break;
	    }
	}
	
	/*
	 * Nothing changed!
	*/
	
	if ((i >= count) ||
	    ((face->annotations[i] == NULL) && (adata[i] == NULL)))
	{
	    return 0;
	}
    }
    
    /*
     *  Ok, things have changed, free the old annotations.
    */
    
    FaceAnnotateFree(face);
    
    /*
     *    Now make the annotations.
    */
    
    if ((pixmap = FaceImagePixmap(face->image)) == None)
    {
	return 0;
    }
    
    shape  = FaceImageShape(face->image);
    
    /*
     *  We may need to make the pixmaps bigger to accomidate annotation
     * either above or below.  Here we compute the position of the old
     * pixmap data and the extra height of the new.
    */
    
    XtVaGetValues(TheFrame,
		  XtNtileWidth,  &tileWidth,
		  XtNtileHeight, &tileHeight, NULL);
    
    /*
     *   Copy the pixmap and any shape.
    */
    
    XGetGeometry(display, pixmap, &imageRoot, &junkx, &junky,
		 &imageWidth, &imageHeight, &junkbw, &imageDepth);
    
    /*
     * If the image is smaller than the frame need to enlarge it.
    */
    
    if (tileHeight > imageHeight)
    {
	extraHeight = tileHeight - imageHeight;
	if (TheFacesResources.annotation_above)
	{
	    y = extraHeight;
	}
    }
    
    if (tileWidth > imageWidth)
    {
	extraWidth = tileWidth - imageWidth;
	x = extraWidth / 2;
    }
    
    face->pixmap = XCreatePixmap(display, imageRoot,
				 imageWidth  + extraWidth,
				 imageHeight + extraHeight,
				 imageDepth);
    
    /*
     * Create a GC to use with the image.
    */
    
    mask = GCForeground | GCFunction | GCGraphicsExposures | GCFillStyle;
    
    values.foreground         = TheFacesResources.background;
    values.function           = GXcopy;
    values.fill_style         = FillSolid;
    values.graphics_exposures = False;
    
    imageGC = XCreateGC(display, face->pixmap, mask, &values);
    
    /*
     * Copy the image.
    */
    
    XFillRectangle(display, face->pixmap, imageGC,
		   0, 0,
		   imageWidth  + extraWidth,
		   imageHeight + extraHeight);
    
    XCopyArea(display, pixmap, face->pixmap, imageGC,
	      0, 0, imageWidth, imageHeight, x, y);
    
    if (shape == None)
    {
	shapeWidth  = imageWidth;
	shapeHeight = imageHeight;
	shapeRoot   = imageRoot;
	shapeDepth  = 1;
    }
    else
    {
	XGetGeometry(display, shape, &shapeRoot, &junkx, &junky,
		     &shapeWidth, &shapeHeight, &junkbw, &shapeDepth);
    }
    
    face->shape = XCreatePixmap(display, shapeRoot,
				shapeWidth  + extraWidth,
				shapeHeight + extraHeight,
				shapeDepth);
    
    /*
     *   Create a gc for use with the shape.
    */
    
    mask = (GCForeground | GCBackground | GCFunction |
	    GCFillStyle  | GCGraphicsExposures);
    
    values.foreground         = 0;
    values.background         = 0;
    values.function           = GXcopy;
    values.fill_style         = FillSolid;
    values.graphics_exposures = False;
    
    shapeGC = XCreateGC(display, face->shape, mask, &values);
    
    /*
     *  Copy shape data.
    */

    XFillRectangle(display, face->shape, shapeGC,
		   0, 0,
		   shapeWidth  + extraWidth,
		   shapeHeight + extraHeight);
    
    XSetForeground(display, shapeGC, 1);
    
    if (shape == None)
    {
	if (!TheFacesResources.shape_extra)
	{
	    shapeWidth  += extraWidth;
	    shapeHeight += extraHeight;
	    x = 0;
	    y = 0;
	}
	
	XFillRectangle(display, face->shape, shapeGC,
		       x, y, shapeWidth, shapeHeight);
    }
    else
    {
	XCopyArea(display, shape, face->shape, shapeGC,
		  0, 0, shapeWidth, shapeHeight, x, y);
    }
    
    /*
     *   Now for each valid annotation.
    */
    
    face->annotations = (char**)XtCalloc(count+1, sizeof(char*));
    
    for(i = 0; i < count; ++i)
    {
	/*
	 *  Stop if we run out of data.
	*/
	
	if (adata[i] == NULL)
	{
	    face->annotations[i] = NULL;
	    break;
	}
	
	/*
	 *  Copy the string.
	*/
	
	face->annotations[i] = XtNewString(adata[i]);
	
	/*
	 * Annotate image!
	*/
	
	XSetFont(display, imageGC, annos[i].font->fid);
	
	if (face->shape != None)
	{
	    XSetFont(display, shapeGC, annos[i].font->fid);
	}
	
	XSetForeground(display, imageGC, annos[i].fg);
	XSetBackground(display, imageGC, annos[i].bg);
	
	FaceAnnotateWork(face, &annos[i], face->annotations[i],
			 imageWidth  + extraWidth,
			 imageHeight + extraHeight,
			 imageGC, shapeGC);
    }
    
    XFreeGC(display, imageGC);
    
    if (face->shape)
    {
	XFreeGC(display, shapeGC);
    }
    
    return 1;
}

void
FaceAnnotateInit()
{
    int			i;
    char		name[30];
    
    annotationCount = TheFacesResources.annotation_count;
    annotations = (Annotation*) XtCalloc(annotationCount, sizeof(Annotation));
    
    for(i = 0; i < annotationCount; ++i)
    {
	sprintf(name, "annotation%d", i+1);
	
	XtGetSubresources(TheTopLevel, &annotations[i], name, "Annotation",
			  AnnotationResourcesList,
			  XtNumber(AnnotationResourcesList),
			  NULL, 0);
    };
    
    unknownAnnotationCount = TheFacesResources.unknown_annotation_count;
    unknownAnnotations = (Annotation*) XtCalloc(unknownAnnotationCount,
						sizeof(Annotation));
    
    for(i = 0; i < unknownAnnotationCount; ++i)
    {
	sprintf(name, "unknownAnnotation%d", i+1);
	
	XtGetSubresources(TheTopLevel, &unknownAnnotations[i],
			  name, "Annotation",
			  AnnotationResourcesList,
			  XtNumber(AnnotationResourcesList),
			  NULL, 0);
    };
}