/[CvsGraph]/cvsgraph/cvsgraph.c
ViewVC logotype

Annotate of /cvsgraph/cvsgraph.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.56 - (show annotations)
Tue Jun 21 00:02:48 2005 UTC (12 years, 4 months ago) by bertho
Branch: MAIN
Changes since 1.55: +70 -60 lines
File MIME type: text/plain
Change color management to reduce used colors. This will help future
color extensions with color changes based on context/content. The
default setup uses 10 colors now instead of 14.
1 /*
2 * CvsGraph graphical representation generator of brances and revisions
3 * of a file in cvs/rcs.
4 *
5 * Copyright (C) 2001,2002,2003,2004 B. Stultiens
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #ifdef HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
34 #endif
35 #include <fcntl.h>
36 #include <regex.h>
37 #include <errno.h>
38 #include <ctype.h>
39 #include <time.h>
40 #include <limits.h>
41 #include <regex.h>
42 #include <math.h>
43
44 #ifdef HAVE_GETOPT_H
45 # include <getopt.h>
46 #endif
47
48 #include <gd.h>
49 #include <gdfontt.h>
50
51 #include "cvsgraph.h"
52 #include "utils.h"
53 #include "readconf.h"
54 #include "rcs.h"
55
56 #if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG) && !defined(HAVE_IMAGE_JPEG)
57 # error No image output format available. Check libgd
58 #endif
59
60
61 /*#define DEBUG 1*/
62 /*#define NOGDFILL 1*/
63 /*#define DEBUG_IMAGEMAP 1*/
64
65 #define LOOPSAFEGUARD 10000 /* Max itterations in possible infinite loops */
66
67 #ifndef MAX
68 # define MAX(a,b) ((a) > (b) ? (a) : (b))
69 #endif
70
71 #ifndef MIN
72 # define MIN(a,b) ((a) < (b) ? (a) : (b))
73 #endif
74
75 #define ALIGN_HL 0x00
76 #define ALIGN_HC 0x01
77 #define ALIGN_HR 0x02
78 #define ALIGN_HX 0x0f
79 #define ALIGN_VT 0x00
80 #define ALIGN_VC 0x10
81 #define ALIGN_VB 0x20
82 #define ALIGN_VX 0xf0
83
84 #ifndef M_PI /* math.h should have defined this */
85 # define M_PI 3.14159265358979323846
86 #endif
87 #define ROUND(f) ((f >= 0.0)?((int)(f + 0.5)):((int)(f - 0.5)))
88
89 #define ARROW_LENGTH 12 /* Default arrow dimensions */
90 #define ARROW_WIDTH 3
91
92 /*
93 **************************************************************************
94 * Globals
95 **************************************************************************
96 */
97
98 config_t conf;
99 int debuglevel;
100
101 static color_t white_color = {255, 255, 255, 0};
102 static color_t black_color = {0, 0, 0, 0};
103
104 static branch_t *subtree_branch = NULL; /* Set to the (first) subtree branch that we want to show */
105 static revision_t *subtree_rev = NULL; /* Set to the subtree revision which branches we want to show */
106
107 static msg_stack_t *msg_stack = NULL; /* Messages that would otherwise be sent to stderr goto the image */
108 static int nmsg_stack = 0;
109
110 /*
111 **************************************************************************
112 * Forwards
113 **************************************************************************
114 */
115 static void zap_string(void);
116 static char *dup_string(void);
117 static void add_string_str(const char *s);
118 static void add_string_ch(int ch);
119 static void add_string_date(const char *d);
120 static void add_string_str_html(const char *s, int maxlen);
121 static void add_string_str_len(const char *s, int maxlen);
122
123 static void calc_subtree_size(branch_t *b, int *x, int *y, int *w, int *h);
124
125 /*
126 **************************************************************************
127 * Debug routines
128 **************************************************************************
129 */
130 static void dump_rev(char *p, rev_t *r)
131 {
132 printf("%s", p);
133 if(r)
134 printf("'%s', '%s', %d\n", r->rev, r->branch, r->isbranch);
135 else
136 printf("<null>\n");
137 }
138
139 static void dump_id(char *p, char *d)
140 {
141 printf("%s", p);
142 if(d)
143 printf("'%s'\n", d);
144 else
145 printf("<null>\n");
146 }
147
148 static void dump_idrev(char *p, idrev_t *t)
149 {
150 printf("%s", p);
151 if(t)
152 {
153 printf("'%s' -> ", t->id);
154 dump_rev("", t->rev);
155 }
156 else
157 printf("<null>\n");
158 }
159
160 static void dump_tag(char *p, tag_t *t)
161 {
162 printf("%s", p);
163 if(t)
164 {
165 printf("'%s' -> ", t->tag);
166 dump_rev("", t->rev);
167 }
168 else
169 printf("<null>\n");
170 }
171
172 static void dump_delta(char *p, delta_t *d)
173 {
174 int i;
175 printf("%sdelta.rev : ", p);
176 dump_rev("", d->rev);
177 printf("%sdelta.date : %s\n", p, d->date);
178 printf("%sdelta.author: %s\n", p, d->author);
179 printf("%sdelta.state : %s\n", p, d->state);
180 for(i = 0; d->branches && i < d->branches->nrevs; i++)
181 {
182 printf("%sdelta.branch: ", p);
183 dump_rev("", d->branches->revs[i]);
184 }
185 printf("%sdelta.next : ", p);
186 dump_rev("", d->next);
187 printf("\n");
188 }
189
190 static void dump_dtext(char *p, dtext_t *d)
191 {
192 printf("%sdtext.rev : ", p);
193 dump_rev("", d->rev);
194 printf("%sdtext.log : %d bytes\n", p, d->log ? strlen(d->log) : -1);
195 printf("%sdtext.text : %d bytes\n", p, d->text ? strlen(d->text) : -1);
196 printf("\n");
197 }
198
199 static void dump_rcsfile(rcsfile_t *rcs)
200 {
201 int i;
202 printf("root : '%s'\n", rcs->root);
203 printf("module : '%s'\n", rcs->module);
204 printf("file : '%s'\n", rcs->file);
205 dump_rev("head : ", rcs->head);
206 dump_rev("branch : ", rcs->branch);
207 printf("access :\n");
208 for(i = 0; rcs->access && i < rcs->access->nids; i++)
209 dump_id("\t", rcs->access->ids[i]);
210 printf("tags :\n");
211 for(i = 0; rcs->tags && i < rcs->tags->ntags; i++)
212 dump_tag("\t", rcs->tags->tags[i]);
213 printf("locks :%s\n", rcs->strict ? " (strict)" : "");
214 for(i = 0; rcs->locks && i < rcs->locks->nidrevs; i++)
215 dump_idrev("\t", rcs->locks->idrevs[i]);
216 printf("comment: '%s'\n", rcs->comment);
217 printf("expand : '%s'\n", rcs->expand ? rcs->expand : "(default -kv)");
218 printf("deltas :\n");
219 for(i = 0; rcs->deltas && i < rcs->deltas->ndeltas; i++)
220 dump_delta("\t", rcs->deltas->deltas[i]);
221 printf("desc : '%s'\n", rcs->desc);
222 printf("dtexts :\n");
223 for(i = 0; rcs->dtexts && i < rcs->dtexts->ndtexts; i++)
224 dump_dtext("\t", rcs->dtexts->dtexts[i]);
225
226 fflush(stdout);
227 }
228
229 /*
230 **************************************************************************
231 * Error/Warning Message helpers
232 **************************************************************************
233 */
234 #define MSGBUFSIZE 256
235 void stack_msg(int severity, const char *fmt, ...)
236 {
237 va_list va;
238 int i;
239 char *buf = xmalloc(MSGBUFSIZE);
240 switch(severity)
241 {
242 case MSG_WARN: sprintf(buf, "Warning: "); break;
243 case MSG_ERR: sprintf(buf, "Error: "); break;
244 default: sprintf(buf, "Unqualified error: "); break;
245 }
246 i = strlen(buf);
247 assert(i < MSGBUFSIZE);
248 va_start(va, fmt);
249 vsnprintf(buf+i, MSGBUFSIZE-i, fmt, va);
250 va_end(va);
251 if(!msg_stack)
252 msg_stack = xmalloc(sizeof(*msg_stack));
253 else
254 {
255 msg_stack = xrealloc(msg_stack, (nmsg_stack+1)*sizeof(*msg_stack));
256 }
257 msg_stack[nmsg_stack].msg = buf;
258 msg_stack[nmsg_stack].severity = severity;
259 nmsg_stack++;
260 }
261
262 /*
263 **************************************************************************
264 * Read the rcs file
265 **************************************************************************
266 */
267 static rcsfile_t *get_rcsfile(const char *cvsroot, const char *module, const char *file)
268 {
269 char *cmd = NULL;
270 int rv;
271
272 if(file)
273 {
274 cmd = xmalloc(strlen(cvsroot) + strlen(module) + strlen(file) + 1);
275 sprintf(cmd, "%s%s%s", cvsroot, module, file);
276 if(!(rcsin = fopen(cmd, "rb")))
277 {
278 perror(cmd);
279 return NULL;
280 }
281 input_file = cmd;
282 }
283 else
284 {
285 rcsin = stdin;
286 input_file = "<stdin>";
287 }
288 line_number = 1;
289 rv = rcsparse();
290 if(file)
291 {
292 fclose(rcsin);
293 xfree(cmd);
294 }
295 if(rv)
296 return NULL;
297 input_file = NULL;
298 if(file)
299 {
300 rcsfile->root = xstrdup(cvsroot);
301 rcsfile->module = xstrdup(module);
302 rcsfile->file = xstrdup(file);
303 }
304 else
305 {
306 rcsfile->root = xstrdup("");
307 rcsfile->module = xstrdup("");
308 rcsfile->file = xstrdup("<stdin>");
309 }
310 return rcsfile;
311 }
312
313 /*
314 **************************************************************************
315 * Sort and find helpers
316 **************************************************************************
317 */
318 static int count_dots(const char *s)
319 {
320 int i;
321 for(i = 0; *s; s++)
322 {
323 if(*s == '.')
324 i++;
325 }
326 return i;
327 }
328
329 static int compare_rev(int bcmp, const rev_t *r1, const rev_t *r2)
330 {
331 int d1, d2;
332 char *c1, *c2;
333 char *v1, *v2;
334 char *s1, *s2;
335 int retval = 0;
336 assert(r1 != NULL);
337 assert(r2 != NULL);
338 if(bcmp)
339 {
340 assert(r1->branch != NULL);
341 assert(r2->branch != NULL);
342 c1 = r1->branch;
343 c2 = r2->branch;
344 }
345 else
346 {
347 assert(r1->rev != NULL);
348 assert(r2->rev != NULL);
349 c1 = r1->rev;
350 c2 = r2->rev;
351 }
352
353 d1 = count_dots(c1);
354 d2 = count_dots(c2);
355 if(d1 != d2)
356 {
357 return d1 - d2;
358 }
359
360 s1 = v1 = xstrdup(c1);
361 s2 = v2 = xstrdup(c2);
362 while(1)
363 {
364 char *vc1 = strchr(s1, '.');
365 char *vc2 = strchr(s2, '.');
366 if(vc1 && vc2)
367 *vc1 = *vc2 = '\0';
368 if(*s1 && *s2)
369 {
370 d1 = atoi(s1);
371 d2 = atoi(s2);
372 if(d1 != d2)
373 {
374 retval = d1 - d2;
375 break;
376 }
377 }
378 if(!vc1 || !vc2)
379 break;
380 s1 = vc1 + 1;
381 s2 = vc2 + 1;
382 }
383 xfree(v1);
384 xfree(v2);
385 return retval;
386 }
387
388 /*
389 **************************************************************************
390 * Reorganise the rcsfile for the branches
391 *
392 * Basically, we have a list of deltas (i.e. administrative info on
393 * revisions) and a list of delta text (the actual logs and diffs).
394 * The deltas are linked through the 'next' and the 'branches' fields
395 * which describe the tree-structure of revisions.
396 * The reorganisation means that we put each delta and corresponding
397 * delta text in a revision structure and assign it to a specific
398 * branch. This is required because we want to be able to calculate
399 * the bounding boxes of each branch. The revisions expand vertically
400 * and the branches expand horizontally.
401 * The reorganisation is performed in these steps:
402 * 1 - sort deltas and delta text on revision number for quick lookup
403 * 2 - start at the denoted head revision:
404 * * create a branch structure and add this revision
405 * * for each 'branches' in the delta do:
406 * - walk all 'branches' of the delta and recursively goto 2
407 * with the denoted branch delta as new head
408 * - backlink the newly create sub-branch to the head revision
409 * so that we can draw them recursively
410 * * set head to the 'next' field and goto 2 until no next is
411 * available
412 * 3 - update the administration
413 **************************************************************************
414 */
415 static int sort_delta(const void *d1, const void *d2)
416 {
417 return compare_rev(0, (*(delta_t **)d1)->rev, (*(delta_t **)d2)->rev);
418 }
419
420 static int search_delta(const void *r, const void *d)
421 {
422 return compare_rev(0, (rev_t *)r, (*(delta_t **)d)->rev);
423 }
424
425 static delta_t *find_delta(delta_t **dl, int n, rev_t *r)
426 {
427 delta_t **d;
428 if(!n)
429 return NULL;
430 d = bsearch(r, dl, n, sizeof(*dl), search_delta);
431 if(!d)
432 return NULL;
433 return *d;
434 }
435
436 static int sort_dtext(const void *d1, const void *d2)
437 {
438 return compare_rev(0, (*(dtext_t **)d1)->rev, (*(dtext_t **)d2)->rev);
439 }
440
441 static int search_dtext(const void *r, const void *d)
442 {
443 return compare_rev(0, (rev_t *)r, (*(dtext_t **)d)->rev);
444 }
445
446 static dtext_t *find_dtext(dtext_t **dl, int n, rev_t *r)
447 {
448 dtext_t **d;
449 if(!n)
450 return NULL;
451 d = bsearch(r, dl, n, sizeof(*dl), search_dtext);
452 if(!d)
453 return NULL;
454 return *d;
455 }
456
457 static rev_t *dup_rev(const rev_t *r)
458 {
459 rev_t *t = xmalloc(sizeof(*t));
460 t->rev = xstrdup(r->rev);
461 t->branch = xstrdup(r->branch);
462 t->isbranch = r->isbranch;
463 return t;
464 }
465
466 static branch_t *new_branch(delta_t *d, dtext_t *t)
467 {
468 branch_t *b = xmalloc(sizeof(*b));
469 revision_t *r = xmalloc(sizeof(*r));
470 r->delta = d;
471 r->dtext = t;
472 r->rev = d->rev;
473 r->branch = b;
474 b->branch = dup_rev(d->rev);
475 b->branch->isbranch = 1;
476 b->nrevs = 1;
477 b->revs = xmalloc(sizeof(b->revs[0]));
478 b->revs[0] = r;
479 return b;
480 }
481
482 static revision_t *add_to_branch(branch_t *b, delta_t *d, dtext_t *t)
483 {
484 revision_t *r = xmalloc(sizeof(*r));
485 r->delta = d;
486 r->dtext = t;
487 r->rev = d->rev;
488 r->branch = b;
489 b->revs = xrealloc(b->revs, (b->nrevs+1) * sizeof(b->revs[0]));
490 b->revs[b->nrevs] = r;
491 b->nrevs++;
492 return r;
493 }
494
495 static int sort_branch_height(const void *b1, const void *b2)
496 {
497 return (*(branch_t **)b1)->nrevs - (*(branch_t **)b2)->nrevs;
498 }
499
500 static void build_branch(branch_t ***bl, int *nbl, delta_t **sdl, int nsdl, dtext_t **sdt, int nsdt, delta_t *head)
501 {
502 branch_t *b;
503 dtext_t *text;
504 revision_t *currev;
505
506 assert(head != NULL);
507
508 if(head->flag)
509 {
510 stack_msg(MSG_ERR, "Circular reference on '%s' in branchpoint\n", head->rev->rev);
511 return;
512 }
513 head->flag++;
514 text = find_dtext(sdt, nsdt, head->rev);
515
516 /* Create a new branch for this head */
517 b = new_branch(head, text);
518 *bl = xrealloc(*bl, (*nbl+1)*sizeof((*bl)[0]));
519 (*bl)[*nbl] = b;
520 (*nbl)++;
521 currev = b->revs[0];
522 while(1)
523 {
524 /* Process all sub-branches */
525 if(head->branches)
526 {
527 int i;
528 for(i = 0; i < head->branches->nrevs; i++)
529 {
530 delta_t *d = find_delta(sdl, nsdl, head->branches->revs[i]);
531 int btag = *nbl;
532 if(!d)
533 continue;
534 build_branch(bl, nbl, sdl, nsdl, sdt, nsdt, d);
535
536 /* Set the new branch's origin */
537 (*bl)[btag]->branchpoint = currev;
538
539 /* Add branch to this revision */
540 currev->branches = xrealloc(currev->branches, (currev->nbranches+1)*sizeof(currev->branches[0]));
541 currev->branches[currev->nbranches] = (*bl)[btag];
542 currev->nbranches++;
543 }
544 if(conf.branch_resort)
545 qsort(currev->branches, currev->nbranches, sizeof(currev->branches[0]), sort_branch_height);
546 }
547
548 /* Walk through the next list */
549 if(!head->next)
550 return;
551
552 head = find_delta(sdl, nsdl, head->next);
553 if(!head)
554 {
555 stack_msg(MSG_ERR, "Next revision (%s) not found in deltalist\n", head->next->rev);
556 return;
557 }
558 if(head->flag)
559 {
560 stack_msg(MSG_ERR, "Circular reference on '%s'\n", head->rev->rev);
561 return;
562 }
563 head->flag++;
564 text = find_dtext(sdt, nsdt, head->rev);
565 currev = add_to_branch(b, head, text);
566 }
567 }
568
569 int reorganise_branches(rcsfile_t *rcs)
570 {
571 delta_t **sdelta;
572 int nsdelta;
573 dtext_t **sdtext;
574 int nsdtext;
575 delta_t *head;
576 branch_t **bl;
577 int nbl;
578 int i;
579
580 assert(rcs->deltas != NULL);
581 assert(rcs->head != NULL);
582
583 /* Make a new list for quick lookup */
584 nsdelta = rcs->deltas->ndeltas;
585 sdelta = xmalloc(nsdelta * sizeof(sdelta[0]));
586 memcpy(sdelta, rcs->deltas->deltas, nsdelta * sizeof(sdelta[0]));
587 qsort(sdelta, nsdelta, sizeof(sdelta[0]), sort_delta);
588
589 /* Do the same for the delta text */
590 if(rcs->dtexts)
591 {
592 nsdtext = rcs->dtexts->ndtexts;
593 sdtext = xmalloc(nsdtext * sizeof(sdtext[0]));
594 memcpy(sdtext, rcs->dtexts->dtexts, nsdtext * sizeof(sdtext[0]));
595 qsort(sdtext, nsdtext, sizeof(sdtext[0]), sort_dtext);
596 }
597 else
598 {
599 nsdtext = 0;
600 sdtext = NULL;
601 }
602
603 /* Start from the head of the trunk */
604 head = find_delta(sdelta, nsdelta, rcs->head);
605 if(!head)
606 {
607 stack_msg(MSG_ERR, "Head revision (%s) not found in deltalist\n", rcs->head->rev);
608 return 0;
609 }
610 bl = NULL;
611 nbl = 0;
612 build_branch(&bl, &nbl, sdelta, nsdelta, sdtext, nsdtext, head);
613
614 /* Reverse the head branch */
615 for(i = 0; i < bl[0]->nrevs/2; i++)
616 {
617 revision_t *r;
618 r = bl[0]->revs[i];
619 bl[0]->revs[i] = bl[0]->revs[bl[0]->nrevs-i-1];
620 bl[0]->revs[bl[0]->nrevs-i-1] = r;
621 }
622
623 /* Update the branch-number of the head because it was reversed */
624 xfree(bl[0]->branch->branch);
625 bl[0]->branch->branch = xstrdup(bl[0]->revs[0]->rev->branch);
626
627 /* Keep the admin */
628 rcs->branches = bl;
629 rcs->nbranches = nbl;
630 rcs->sdelta = sdelta;
631 rcs->nsdelta = nsdelta;
632 rcs->sdtext = sdtext;
633 rcs->nsdtext = nsdtext;
634 rcs->active = bl[0];
635 return 1;
636 }
637
638 /*
639 **************************************************************************
640 * Assign the symbolic tags to the revisions and branches
641 *
642 * The tags point to revision numbers somewhere in the tree structure
643 * of branches and revisions. First we make a sorted list of all
644 * revisions and then we assign each tag to the proper revision.
645 **************************************************************************
646 */
647 static int sort_revision(const void *r1, const void *r2)
648 {
649 return compare_rev(0, (*(revision_t **)r1)->delta->rev, (*(revision_t **)r2)->delta->rev);
650 }
651
652 static int search_revision(const void *t, const void *r)
653 {
654 return compare_rev(0, (rev_t *)t, (*(revision_t **)r)->delta->rev);
655 }
656
657 static int sort_branch(const void *b1, const void *b2)
658 {
659 return compare_rev(1, (*(branch_t **)b1)->branch, (*(branch_t **)b2)->branch);
660 }
661
662 static int search_branch(const void *t, const void *b)
663 {
664 return compare_rev(1, (rev_t *)t, (*(branch_t **)b)->branch);
665 }
666
667 static char *previous_rev(const char *c)
668 {
669 int dots = count_dots(c);
670 char *cptr;
671 char *r;
672 if(!dots)
673 {
674 stack_msg(MSG_ERR, "FIXME: previous_rev(\"%s\"): Cannot determine parent branch revision\n", c);
675 return xstrdup("1.0"); /* FIXME: don't know what the parent is */
676 }
677 if(dots & 1)
678 {
679 /* Is is a revision we want the parent of */
680 r = xstrdup(c);
681 cptr = strrchr(r, '.');
682 assert(cptr != NULL);
683 if(dots == 1)
684 {
685 stack_msg(MSG_ERR, "FIXME: previous_rev(\"%s\"): Going beyond top-level?\n", c);
686 /* FIXME: What is the parent of 1.1? */
687 cptr[1] = '\0';
688 strcat(r, "0");
689 return r;
690 }
691 /* Here we have a "x.x[.x.x]+" case */
692 *cptr = '\0';
693 cptr = strrchr(r, '.');
694 assert(cptr != NULL);
695 *cptr = '\0';
696 return r;
697 }
698 /* It is a branch we want the parent of */
699 r = xstrdup(c);
700 cptr = strrchr(r, '.');
701 assert(cptr != NULL);
702 *cptr = '\0';
703 return r;
704 }
705
706 static char *build_regex(size_t n, regmatch_t *m, const char *ms)
707 {
708 char *cptr;
709 int i;
710
711 if(!conf.merge_to || !conf.merge_to[0])
712 return NULL;
713
714 zap_string();
715 for(cptr = conf.merge_to; *cptr; cptr++)
716 {
717 if(*cptr == '%')
718 {
719 if(cptr[1] >= '1' && cptr[1] <= '9')
720 {
721 int idx = cptr[1] - '0';
722 regmatch_t *p = &m[idx];
723 if(idx < n && !(p->rm_so == -1 || p->rm_so >= p->rm_eo))
724 {
725 for(i = p->rm_so; i < p->rm_eo; i++)
726 {
727 if(strchr("^$.*+\\[{()", ms[i]))
728 add_string_ch('\\');
729 add_string_ch(ms[i]);
730 }
731 }
732 cptr++;
733 }
734 else
735 add_string_ch('%');
736 }
737 else
738 add_string_ch(*cptr);
739 }
740 return dup_string();
741 }
742
743 static void find_merges_cvsnt(rcsfile_t *rcs)
744 {
745 int i;
746
747 if(!conf.merge_cvsnt)
748 return;
749
750 for(i = 0; i < rcs->nsrev; i++)
751 {
752 revision_t **r;
753
754 if(!rcs->srev[i]->delta->mergepoint)
755 continue;
756
757 r = bsearch(rcs->srev[i]->delta->mergepoint->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
758 if(!r)
759 continue;
760 rcs->merges = xrealloc(rcs->merges, sizeof(rcs->merges[0]) * (rcs->nmerges+1));
761 rcs->merges[rcs->nmerges].type = TR_REVISION;
762 rcs->merges[rcs->nmerges].from.rev = *r;
763 rcs->merges[rcs->nmerges].to.rev = rcs->srev[i];
764 rcs->nmerges++;
765 (*r)->stripped = -1;
766 rcs->srev[i]->stripped = -1;
767 }
768 }
769
770 static void find_merges(rcsfile_t *rcs)
771 {
772 int i;
773 int err;
774 int rcflags = REG_EXTENDED | (conf.merge_nocase ? REG_ICASE : 0);
775 regex_t *refrom = NULL;
776 regex_t *reto = NULL;
777 regmatch_t *matchfrom = NULL;
778
779 if(!conf.merge_from || !conf.merge_from[0] || !conf.merge_to || !conf.merge_to[0])
780 return;
781
782 refrom = xmalloc(sizeof(*refrom));
783 reto = xmalloc(sizeof(*reto));
784
785 /* Compile the 'from' regex match for merge identification */
786 err = regcomp(refrom, conf.merge_from, rcflags);
787 if(err)
788 {
789 char *msg;
790 i = regerror(err, refrom, NULL, 0);
791 msg = xmalloc(i+1);
792 regerror(err, refrom, msg, i+1);
793 stack_msg(MSG_WARN, "%s", msg);
794 xfree(msg);
795 xfree(refrom);
796 xfree(reto);
797 return;
798 }
799 else
800 matchfrom = xmalloc((refrom->re_nsub+1) * sizeof(*matchfrom));
801
802 for(i = 0; i < rcs->tags->ntags; i++)
803 {
804 tag_t *t = rcs->tags->tags[i];
805
806 /* Must be revision tags and not detached */
807 if(t->rev->isbranch || !t->logrev)
808 continue;
809
810 /* Try to find merge tag matches */
811 if(!regexec(refrom, t->tag, refrom->re_nsub+1, matchfrom, 0))
812 {
813 int n;
814 char *to;
815
816 to = build_regex(refrom->re_nsub+1, matchfrom, t->tag);
817 if(to)
818 {
819 err = regcomp(reto, to, rcflags);
820 if(err)
821 {
822 char *msg;
823 i = regerror(err, reto, NULL, 0);
824 msg = xmalloc(i+1);
825 regerror(err, reto, msg, i+1);
826 stack_msg(MSG_WARN, "%s", msg);
827 xfree(msg);
828 }
829 else if(!err)
830 {
831 for(n = 0; n < rcs->tags->ntags; n++)
832 {
833 tag_t *nt = rcs->tags->tags[n];
834 /* From and To never should match the same tag or belong to a branch */
835 if(n == i || nt->rev->isbranch || !nt->logrev)
836 continue;
837
838 if(!regexec(reto, nt->tag, 0, NULL, 0))
839 {
840 /* Tag matches */
841 rcs->merges = xrealloc(rcs->merges,
842 sizeof(rcs->merges[0]) * (rcs->nmerges+1));
843 rcs->merges[rcs->nmerges].type = TR_TAG;
844 rcs->merges[rcs->nmerges].to.tag = nt;
845 rcs->merges[rcs->nmerges].from.tag = t;
846 rcs->nmerges++;
847 if(!conf.tag_ignore_merge)
848 {
849 nt->ignore = 0;
850 t->ignore = 0;
851 }
852 /* We cannot (should not) match multiple times */
853 if(!conf.merge_findall)
854 break;
855 }
856 }
857 regfree(reto);
858 }
859 xfree(to);
860 }
861 }
862 }
863 if(matchfrom) xfree(matchfrom);
864 if(refrom) { regfree(refrom); xfree(refrom); }
865 if(reto) xfree(reto);
866 }
867
868 static void assign_tags(rcsfile_t *rcs)
869 {
870 int i;
871 int nr;
872 regex_t *regextag = NULL;
873
874 if(conf.tag_ignore && conf.tag_ignore[0])
875 {
876 int err;
877 regextag = xmalloc(sizeof(*regextag));
878 err = regcomp(regextag, conf.tag_ignore, REG_EXTENDED | REG_NOSUB | (conf.tag_nocase ? REG_ICASE : 0));
879 if(err)
880 {
881 char *msg;
882 i = regerror(err, regextag, NULL, 0);
883 msg = xmalloc(i+1);
884 regerror(err, regextag, msg, i+1);
885 stack_msg(MSG_WARN, "%s", msg);
886 xfree(msg);
887 xfree(regextag);
888 regextag = NULL;
889 }
890 }
891
892 for(i = nr = 0; i < rcs->nbranches; i++)
893 nr += rcs->branches[i]->nrevs;
894
895 rcs->srev = xmalloc(nr * sizeof(rcs->srev[0]));
896 rcs->nsrev = nr;
897 for(i = nr = 0; i < rcs->nbranches; i++)
898 {
899 memcpy(&rcs->srev[nr], rcs->branches[i]->revs, rcs->branches[i]->nrevs * sizeof(rcs->branches[i]->revs[0]));
900 nr += rcs->branches[i]->nrevs;
901 }
902
903 qsort(rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), sort_revision);
904 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
905
906 if(!rcs->branch)
907 {
908 /* The main trunk is the active trunk */
909 rcs->tags->tags = xrealloc(rcs->tags->tags, (rcs->tags->ntags+1)*sizeof(rcs->tags->tags[0]));
910 rcs->tags->tags[rcs->tags->ntags] = xmalloc(sizeof(tag_t));
911 rcs->tags->tags[rcs->tags->ntags]->tag = xstrdup("MAIN");
912 rcs->tags->tags[rcs->tags->ntags]->rev = xmalloc(sizeof(rev_t));
913 rcs->tags->tags[rcs->tags->ntags]->rev->rev = xstrdup(rcs->active->branch->rev);
914 rcs->tags->tags[rcs->tags->ntags]->rev->branch = xstrdup(rcs->active->branch->branch);
915 rcs->tags->tags[rcs->tags->ntags]->rev->isbranch = 1;
916 rcs->tags->ntags++;
917 }
918
919 /* We should have at least two tags (HEAD and MAIN) */
920 assert(rcs->tags != NULL);
921
922 for(i = 0; i < rcs->tags->ntags; i++)
923 {
924 tag_t *t = rcs->tags->tags[i];
925 if(t->rev->isbranch)
926 {
927 branch_t **b;
928 add_btag:
929 b = bsearch(t->rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
930 if(!b)
931 {
932 rev_t rev;
933 revision_t **r;
934 /* This happens for the magic branch numbers if there are
935 * no commits within the new branch yet. So, we add the
936 * branch and try to continue.
937 */
938 rev.rev = previous_rev(t->rev->branch);
939 rev.branch = NULL;
940 rev.isbranch = 0;
941 r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
942 xfree(rev.rev);
943 if(!r)
944 {
945 stack_msg(MSG_WARN, "No branch found for tag '%s:%s'", t->tag, t->rev->branch);
946 }
947 else
948 {
949 rcs->branches = xrealloc(rcs->branches, (rcs->nbranches+1)*sizeof(rcs->branches[0]));
950 rcs->branches[rcs->nbranches] = xmalloc(sizeof(branch_t));
951 rcs->branches[rcs->nbranches]->branch = dup_rev(t->rev);
952 rcs->branches[rcs->nbranches]->branchpoint = *r;
953 (*r)->branches = xrealloc((*r)->branches, ((*r)->nbranches+1)*sizeof((*r)->branches[0]));
954 (*r)->branches[(*r)->nbranches] = rcs->branches[rcs->nbranches];
955 (*r)->nbranches++;
956 rcs->nbranches++;
957 /* Resort the branches */
958 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
959 goto add_btag;
960 }
961 }
962 else
963 {
964 branch_t *bb = *b;
965 bb->tags = xrealloc(bb->tags, (bb->ntags+1)*sizeof(bb->tags[0]));
966 bb->tags[bb->ntags] = t;
967 bb->ntags++;
968 }
969 }
970 else
971 {
972 revision_t **r = bsearch(t->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
973 if(!r)
974 {
975 stack_msg(MSG_WARN, "No revision found for tag '%s:%s'\n", t->tag, t->rev->rev);
976 }
977 else
978 {
979 revision_t *rr = *r;
980 t->logrev = rr;
981 if(!conf.rev_maxtags || rr->ntags <= conf.rev_maxtags)
982 {
983 rr->tags = xrealloc(rr->tags, (rr->ntags+1)*sizeof(rr->tags[0]));
984 if(conf.rev_maxtags && rr->ntags == conf.rev_maxtags)
985 {
986 rr->tags[rr->ntags] = xmalloc(sizeof(tag_t));
987 rr->tags[rr->ntags]->tag = xstrdup("...");
988 rr->tags[rr->ntags]->rev = t->rev;
989 }
990 else
991 rr->tags[rr->ntags] = t;
992 rr->ntags++;
993 }
994 }
995
996 if(conf.tag_negate)
997 t->ignore++;
998 /* Mark the tag ignored if it matches the configuration */
999 if(regextag && !regexec(regextag, t->tag, 0, NULL, 0))
1000 {
1001 if(conf.tag_negate)
1002 t->ignore--;
1003 else
1004 t->ignore++;
1005 }
1006 }
1007 }
1008
1009 /* We need to reset the first in the list of branches to the
1010 * active branch to ensure the drawing of all
1011 */
1012 if(rcs->active != rcs->branches[0])
1013 {
1014 branch_t **b = bsearch(rcs->active->branch, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
1015 branch_t *t;
1016 assert(b != NULL);
1017 t = *b;
1018 *b = rcs->branches[0];
1019 rcs->branches[0] = t;
1020 }
1021
1022 if(regextag)
1023 {
1024 regfree(regextag);
1025 xfree(regextag);
1026 }
1027 }
1028
1029 /*
1030 **************************************************************************
1031 * String expansion
1032 **************************************************************************
1033 */
1034 static char *_string;
1035 static int _nstring;
1036 static int _nastring;
1037
1038 static void zap_string(void)
1039 {
1040 _nstring = 0;
1041 if(_string)
1042 _string[0] = '\0';
1043 }
1044
1045 static char *dup_string(void)
1046 {
1047 if(_string)
1048 return xstrdup(_string);
1049 else
1050 return "";
1051 }
1052
1053 static void add_string_str(const char *s)
1054 {
1055 int l = strlen(s) + 1;
1056 if(_nstring + l > _nastring)
1057 {
1058 _nastring += MAX(128, l);
1059 _string = xrealloc(_string, _nastring * sizeof(_string[0]));
1060 }
1061 memcpy(_string+_nstring, s, l);
1062 _nstring += l-1;
1063 }
1064
1065 static void add_string_ch(int ch)
1066 {
1067 char buf[2];
1068 buf[0] = ch;
1069 buf[1] = '\0';
1070 add_string_str(buf);
1071 }
1072
1073 static void add_string_date(const char *d)
1074 {
1075 struct tm tm, *tmp;
1076 int n;
1077 time_t t;
1078 char *buf;
1079 int nbuf;
1080 char *env;
1081
1082 memset(&tm, 0, sizeof(tm));
1083 n = sscanf(d, "%d.%d.%d.%d.%d.%d",
1084 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
1085 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
1086 tm.tm_mon--;
1087 if(tm.tm_year > 1900)
1088 tm.tm_year -= 1900;
1089
1090 env = getenv("TZ");
1091 putenv("TZ=UTC0");
1092 t = mktime(&tm);
1093 if(env)
1094 {
1095 char *c = xmalloc(strlen(env) + 3 + 1); /* Extra space for TZ and = */
1096 sprintf(c, "TZ=%s", env);
1097 putenv(c);
1098 xfree(c);
1099 }
1100 else
1101 putenv("TZ");
1102
1103 if(n != 6 || t == (time_t)(-1))
1104 {
1105 add_string_str("<invalid date>");
1106 return;
1107 }
1108
1109 tmp = localtime(&t);
1110 nbuf = (strlen(conf.date_format)+1) * 16; /* Should be enough to hold all types of expansions */
1111 buf = xmalloc(nbuf);
1112 strftime(buf, nbuf, conf.date_format, tmp);
1113 add_string_str(buf);
1114 xfree(buf);
1115 }
1116
1117 static void add_string_str_html(const char *s, int maxlen)
1118 {
1119 int l = 0;
1120 char *str = xmalloc(6 * strlen(s) + 1); /* Should hold all char entity-expand */
1121 char *cptr = str;
1122 for(; *s; s++)
1123 {
1124 if(maxlen && l > abs(maxlen))
1125 {
1126 cptr += sprintf(cptr, "...");
1127 break;
1128 }
1129 if(*s < 0x20)
1130 {
1131 if(*s == '\n')
1132 {
1133 if(maxlen < 0)
1134 *cptr++ = ' ';
1135 else
1136 cptr += sprintf(cptr, "<br%s>", conf.html_level == HTMLLEVEL_X ? " /" : "");
1137 }
1138 }
1139 else if(*s >= 0x7f || *s == '"')
1140 cptr += sprintf(cptr, "&#%d;", (int)(unsigned char)*s);
1141 else if(*s == '<')
1142 cptr += sprintf(cptr, "&lt;");
1143 else if(*s == '>')
1144 cptr += sprintf(cptr, "&gt;");
1145 else if(*s == '&')
1146 cptr += sprintf(cptr, "&amp;");
1147 else if(*s == '"')
1148 cptr += sprintf(cptr, "&quot;");
1149 else
1150 *cptr++ = *s;
1151 l++;
1152 }
1153 *cptr = '\0';
1154 add_string_str(str);
1155 xfree(str);
1156 }
1157
1158 static void add_string_str_len(const char *s, int maxlen)
1159 {
1160 int l = strlen(s);
1161 char *str = xmalloc(l + 1 + 3);
1162 strcpy(str, s);
1163 if(maxlen < l)
1164 sprintf(&str[maxlen], "...");
1165 add_string_str(str);
1166 xfree(str);
1167 }
1168
1169 static char *expand_string(const char *s, rcsfile_t *rcs, revision_t *r, rev_t *rev, rev_t *prev, tag_t *tag)
1170 {
1171 char nb[32];
1172 char nr[32];
1173 char *base;
1174 char *exp;
1175 int l;
1176 char ch;
1177
1178 if(!s)
1179 return xstrdup("");
1180
1181 zap_string();
1182
1183 sprintf(nb, "%d", rcs->nbranches + rcs->nfolds);
1184 sprintf(nr, "%d", rcs->nsrev);
1185 for(; *s; s++)
1186 {
1187 if(*s == '%')
1188 {
1189 switch(*++s)
1190 {
1191 case 'c':
1192 case 'C':
1193 add_string_str(conf.cvsroot);
1194 if(*s == 'C' && conf.cvsroot[0] && conf.cvsroot[strlen(conf.cvsroot)-1] == '/')
1195 {
1196 /* Strip the trailing '/' */
1197 _nstring--;
1198 _string[_nstring] = '\0';
1199 }
1200 break;
1201 case 'f':
1202 case 'F':
1203 base = strrchr(rcs->file, '/');
1204 if(!base)
1205 add_string_str(rcs->file);
1206 else
1207 add_string_str(base+1);
1208 if(*s == 'F' && _string[_nstring-1] == 'v' && _string[_nstring-2] == ',')
1209 {
1210 _nstring -= 2;
1211 _string[_nstring] = '\0';
1212 }
1213 break;
1214 case 'p':
1215 base = strrchr(rcs->file, '/');
1216 if(base)
1217 {
1218 char *c = xstrdup(rcs->file);
1219 base = strrchr(c, '/');
1220 assert(base != NULL);
1221 base[1] = '\0';
1222 add_string_str(c);
1223 xfree(c);
1224 }
1225 /*
1226 * We should not add anything here because we can encounter
1227 * a completely empty path, in which case we do not want
1228 * to add any slash. This prevents an inadvertent root redirect.
1229 *
1230 * else
1231 * add_string_str("/");
1232 */
1233 break;
1234 case 'm':
1235 case 'M':
1236 add_string_str(conf.cvsmodule);
1237 if(*s == 'M' && conf.cvsmodule[0] && conf.cvsmodule[strlen(conf.cvsmodule)-1] == '/')
1238 {
1239 /* Strip the trailing '/' */
1240 _nstring--;
1241 _string[_nstring] = '\0';
1242 }
1243 break;
1244 case 'r': add_string_str(nr); break;
1245 case 'b': add_string_str(nb); break;
1246 case '%': add_string_ch('%'); break;
1247 case '0': if(conf.expand[0]) add_string_str(conf.expand[0]); break;
1248 case '1': if(conf.expand[1]) add_string_str(conf.expand[1]); break;
1249 case '2': if(conf.expand[2]) add_string_str(conf.expand[2]); break;
1250 case '3': if(conf.expand[3]) add_string_str(conf.expand[3]); break;
1251 case '4': if(conf.expand[4]) add_string_str(conf.expand[4]); break;
1252 case '5': if(conf.expand[5]) add_string_str(conf.expand[5]); break;
1253 case '6': if(conf.expand[6]) add_string_str(conf.expand[6]); break;
1254 case '7': if(conf.expand[7]) add_string_str(conf.expand[7]); break;
1255 case '8': if(conf.expand[8]) add_string_str(conf.expand[8]); break;
1256 case '9': if(conf.expand[9]) add_string_str(conf.expand[9]); break;
1257 case 'R': if(rev && rev->rev) add_string_str(rev->rev); break;
1258 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
1259 case 'B': if(rev && rev->branch) add_string_str(rev->branch); break;
1260 case 't': if(tag && tag->tag) add_string_str(tag->tag); break;
1261 case 'd': if(r && r->delta && r->delta->date) add_string_date(r->delta->date); break;
1262 case 's': if(r && r->delta && r->delta->state) add_string_str(r->delta->state); break;
1263 case 'a': if(r && r->delta && r->delta->author) add_string_str(r->delta->author); break;
1264 case 'L':
1265 case 'l':
1266 ch = *s;
1267 l = 0;
1268 if(s[1] == '[')
1269 {
1270 char *cptr = strchr(s, ']');
1271 char *eptr;
1272 if(cptr)
1273 {
1274 l = strtol(&s[2], &eptr, 10);
1275 if(eptr != cptr)
1276 l = 0;
1277 else
1278 s = cptr;
1279 }
1280 }
1281 if(!conf.parse_logs)
1282 add_string_str("N/A");
1283 else if(r && r->dtext && r->dtext->log)
1284 {
1285 if(ch == 'l')
1286 add_string_str_html(r->dtext->log, l);
1287 else
1288 add_string_str_len(r->dtext->log, abs(l));
1289 }
1290 break;
1291 case '(':
1292 base = dup_string();
1293 exp = expand_string(s+1, rcs, r, rev, prev, tag);
1294 zap_string();
1295 add_string_str(base);
1296 add_string_str_html(exp, 0);
1297 xfree(base);
1298 xfree(exp);
1299 /* Find the %) in this recursion level */
1300 for(; *s; s++)
1301 {
1302 if(*s == '%' && s[1] == ')')
1303 {
1304 s++;
1305 break;
1306 }
1307 }
1308 if(!*s)
1309 {
1310 s--; /* To end outer loop */
1311 stack_msg(MSG_WARN, "string expand: Missing %%) in expansion");
1312 }
1313 break;
1314 case ')':
1315 return dup_string();
1316 default:
1317 add_string_ch('%');
1318 add_string_ch(*s);
1319 break;
1320 }
1321 }
1322 else
1323 add_string_ch(*s);
1324 }
1325 return dup_string();
1326 }
1327
1328 /*
1329 **************************************************************************
1330 * Drawing routines
1331 **************************************************************************
1332 */
1333 static color_t *clr_id = NULL;
1334 static int nclr_id = 0;
1335
1336 static color_t *clr(gdImagePtr im, const char *s, revision_t *r, branch_t *b)
1337 {
1338 int i;
1339 color_t *c = get_colorref(s);
1340 if(!c)
1341 c = &black_color;
1342 for(i = 0; i < nclr_id; i++)
1343 {
1344 if(c->r == clr_id[i].r && c->g == clr_id[i].g && c->b == clr_id[i].b)
1345 return &clr_id[i];
1346 }
1347 /* FIXME: Do color evaluation */
1348 clr_id = xrealloc(clr_id, (nclr_id+1) * sizeof(*clr_id));
1349 clr_id[nclr_id] = *c;
1350 clr_id[nclr_id].id = gdImageColorAllocate(im, c->r, c->g, c->b);
1351 return &clr_id[nclr_id++];
1352 }
1353
1354 static int get_swidth(const char *s, font_t *f)
1355 {
1356 int n;
1357 int m;
1358 if(!s || !*s)
1359 return 0;
1360
1361 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1362 if(conf.use_ttf && f->ttfont)
1363 {
1364 int bb[8];
1365 char *e;
1366 #ifdef HAVE_GDIMAGESTRINGFT
1367 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1368 #else
1369 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1370 #endif
1371 if(!e)
1372 return bb[2] - bb[6];
1373 }
1374 #endif
1375 for(n = m = 0; *s; n++, s++)
1376 {
1377 if(*s == '\n')
1378 {
1379 if(n > m)
1380 m = n;
1381 n = 0;
1382 }
1383 }
1384 if(n > m)
1385 m = n;
1386 return f->gdfont ? m * f->gdfont->w : m;
1387 }
1388
1389 static int get_sheight(const char *s, font_t *f)
1390 {
1391 int nl;
1392 if(!s || !*s)
1393 return 0;
1394
1395 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1396 if(conf.use_ttf && f->ttfont)
1397 {
1398 int bb[8];
1399 char *e;
1400 #ifdef HAVE_GDIMAGESTRINGFT
1401 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1402 #else
1403 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1404 #endif
1405 if(!e)
1406 return bb[3] - bb[7] + 4;
1407 }
1408 #endif
1409 for(nl = 1; *s; s++)
1410 {
1411 if(*s == '\n' && s[1])
1412 nl++;
1413 }
1414 return nl * f->gdfont->h;
1415 }
1416
1417 static void draw_rbox(gdImagePtr im, int x1, int y1, int x2, int y2, int r, color_t *color, color_t *bgcolor)
1418 {
1419 int r2 = 2*r;
1420 if(!r)
1421 gdImageFilledRectangle(im, x1, y1, x2, y2, bgcolor->id);
1422 #ifdef HAVE_GDIMAGEFILLEDARC
1423 else
1424 {
1425 gdImageFilledArc(im, x1+r, y1+r, r2, r2, 180, 270, bgcolor->id, gdArc);
1426 gdImageFilledArc(im, x2-r, y1+r, r2, r2, 270, 360, bgcolor->id, gdArc);
1427 gdImageFilledArc(im, x1+r, y2-r, r2, r2, 90, 180, bgcolor->id, gdArc);
1428 gdImageFilledArc(im, x2-r, y2-r, r2, r2, 0, 90, bgcolor->id, gdArc);
1429 gdImageFilledRectangle(im, x1+r, y1, x2-r, y1+r, bgcolor->id);
1430 gdImageFilledRectangle(im, x1, y1+r, x2, y2-r, bgcolor->id);
1431 gdImageFilledRectangle(im, x1+r, y2-r, x2-r, y2, bgcolor->id);
1432 }
1433 #endif
1434 gdImageLine(im, x1+r, y1, x2-r, y1, color->id);
1435 gdImageLine(im, x1+r, y2, x2-r, y2, color->id);
1436 gdImageLine(im, x1, y1+r, x1, y2-r, color->id);
1437 gdImageLine(im, x2, y1+r, x2, y2-r, color->id);
1438 if(conf.box_shadow)
1439 {
1440 gdImageLine(im, x1+r+1, y2+1, x2-r, y2+1, black_color.id);
1441 gdImageLine(im, x2+1, y1+r+1, x2+1, y2-r, black_color.id);
1442 }
1443 if(r)
1444 {
1445 /* FIXME: Pixelization is not perfect */
1446 gdImageArc(im, x1+r, y1+r, r2, r2, 180, 270, color->id);
1447 gdImageArc(im, x2-r, y1+r, r2, r2, 270, 360, color->id);
1448 gdImageArc(im, x1+r, y2-r, r2, r2, 90, 180, color->id);
1449 if(conf.box_shadow)
1450 {
1451 gdImageArc(im, x2-r+1, y2-r+1, r2, r2, 0, 90, black_color.id);
1452 gdImageArc(im, x2-r+1, y2-r, r2, r2, 0, 90, black_color.id);
1453 gdImageArc(im, x2-r, y2-r+1, r2, r2, 0, 90, black_color.id);
1454 }
1455 gdImageArc(im, x2-r, y2-r, r2, r2, 0, 90, color->id);
1456 #if !defined(NOGDFILL) && !defined(HAVE_GDIMAGEFILLEDARC)
1457 /* BUG: We clip manually because libgd segfaults on out of bound values */
1458 if((x1+x2)/2 >= 0 && (x1+x2)/2 < gdImageSX(im) && (y1+y2)/2 >= 0 && (y1+y2)/2 < gdImageSY(im))
1459 gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
1460 #endif
1461 }
1462 }
1463
1464 static void draw_string(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1465 {
1466 int h = get_sheight(s, f);
1467 int xx, yy;
1468 switch(align & ALIGN_HX)
1469 {
1470 default:
1471 case ALIGN_HL: xx = 0; break;
1472 case ALIGN_HC: xx = -get_swidth(s, f)/2; break;
1473 case ALIGN_HR: xx = -get_swidth(s, f); break;
1474 }
1475 switch(align & ALIGN_VX)
1476 {
1477 default:
1478 case ALIGN_VT: yy = 0; break;
1479 case ALIGN_VC: yy = h/2; break;
1480 case ALIGN_VB: yy = h; break;
1481 }
1482 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1483 if(conf.use_ttf && f->ttfont)
1484 {
1485 int bb[8];
1486 char *e;
1487 int cid = conf.anti_alias ? c->id : -c->id;
1488 #ifdef HAVE_GDIMAGESTRINGFT
1489 e = gdImageStringFT(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1490 #else
1491 e = gdImageStringTTF(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1492 #endif
1493 if(!e)
1494 return;
1495 }
1496 #endif
1497 yy = -yy;
1498 gdImageString(im, f->gdfont, x+xx+1, y+yy, s, c->id);
1499 }
1500
1501 static void draw_stringnl(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1502 {
1503 char *t;
1504 char *d;
1505 d = s = xstrdup(s);
1506 do
1507 {
1508 t = strchr(s, '\n');
1509 if(t)
1510 *t = '\0';
1511 draw_string(im, s, f, x, y, align, c);
1512 y += get_sheight(s, f);
1513 s = t+1;
1514 } while(t);
1515 xfree(d);
1516 }
1517
1518 static void draw_rev(gdImagePtr im, revision_t *r)
1519 {
1520 int lx;
1521 int rx;
1522 int x2;
1523 int i;
1524 int ty;
1525
1526 if(conf.left_right)
1527 {
1528 lx = r->cx;
1529 rx = r->cx + r->w;
1530 ty = r->y - r->h/2;
1531 x2 = r->cx + r->w/2;
1532 }
1533 else
1534 {
1535 lx = r->cx - r->w/2;
1536 rx = lx + r->w;
1537 ty = r->y;
1538 x2 = r->cx;
1539 }
1540 draw_rbox(im, lx, ty, rx, ty+r->h, 0, clr(im, "rev_color", r, NULL), clr(im, "rev_bgcolor", r, NULL));
1541 ty += conf.rev_tspace;
1542 if(!conf.rev_hidenumber)
1543 {
1544 draw_string(im, r->rev->rev, &conf.rev_font, x2, ty, ALIGN_HC, clr(im, "rev_color", r, NULL));
1545 ty += get_sheight(r->rev->rev, &conf.rev_font);
1546 }
1547 draw_stringnl(im, r->revtext, &conf.rev_text_font, x2, ty, ALIGN_HC, clr(im, "rev_text_color", r, NULL));
1548 ty += get_sheight(r->revtext, &conf.rev_text_font);
1549 for(i = 0; i < r->ntags; i++)
1550 {
1551 draw_string(im, r->tags[i]->tag, &conf.tag_font, x2, ty, ALIGN_HC, clr(im, "tag_color", r, NULL));
1552 ty += get_sheight(r->tags[i]->tag, &conf.tag_font) + conf.rev_separator;
1553 }
1554 }
1555
1556 static void draw_branch_box(gdImagePtr im, branch_t *b, int xp, int yp)
1557 {
1558 int lx;
1559 int rx;
1560 int i;
1561 int yy;
1562 int x2;
1563
1564 if(conf.left_right)
1565 {
1566 lx = b->cx;
1567 rx = lx + b->w;
1568 x2 = b->cx + b->w/2;
1569 }
1570 else
1571 {
1572 lx = b->cx - b->w/2;
1573 rx = lx + b->w;
1574 x2 = b->cx;
1575 }
1576 draw_rbox(im, lx+xp, yp, rx+xp, yp+b->h, 5, clr(im, "branch_color", NULL, b), clr(im, "branch_bgcolor", NULL, b));
1577 yy = conf.branch_tspace;
1578 if(!b->nfolds)
1579 {
1580 if(!conf.rev_hidenumber)
1581 {
1582 draw_string(im, b->branch->branch, &conf.branch_font, x2+xp, yp+yy, ALIGN_HC, clr(im, "branch_color", NULL, b));
1583 yy += get_sheight(b->branch->branch, &conf.branch_font);
1584 }
1585 for(i = 0; i < b->ntags; i++)
1586 {
1587 draw_string(im, b->tags[i]->tag, &conf.branch_tag_font, x2+xp, yp+yy, ALIGN_HC, clr(im, "branch_tag_color", NULL, b));
1588 yy += get_sheight(b->tags[i]->tag, &conf.branch_tag_font);
1589 }
1590 }
1591 else
1592 {
1593 int y1, y2;
1594 int tx = lx + b->fw + conf.branch_lspace;
1595 int nx = tx - get_swidth(" ", &conf.branch_font);
1596 draw_string(im, b->branch->branch, &conf.branch_font, nx+xp, yp+yy, ALIGN_HR, clr(im, "branch_color", NULL, b));
1597 y1 = get_sheight(b->branch->branch, &conf.branch_font);
1598 draw_string(im, b->tags[0]->tag, &conf.branch_tag_font, tx+xp, yp+yy, ALIGN_HL, clr(im, "branch_tag_color", NULL, b));
1599 y2 = get_sheight(b->tags[0]->tag, &conf.branch_font);
1600 yy += MAX(y1, y2);
1601 for(i = 0; i < b->nfolds; i++)
1602 {
1603 draw_string(im, b->folds[i]->branch->branch, &conf.branch_font, nx+xp, yp+yy, ALIGN_HR, clr(im, "branch_color", NULL, b));
1604 y1 = get_sheight(b->folds[i]->branch->branch, &conf.branch_font);
1605 draw_string(im, b->folds[i]->tags[0]->tag, &conf.branch_tag_font, tx+xp, yp+yy, ALIGN_HL, clr(im, "branch_tag_color", NULL, b));
1606 y2 = get_sheight(b->folds[i]->tags[0]->tag, &conf.branch_tag_font);
1607 yy += MAX(y1, y2);
1608 }
1609 }
1610 }
1611
1612 static void draw_branch(gdImagePtr im, branch_t *b)
1613 {
1614 int yy, xx;
1615 int i;
1616 int line[4];
1617 int l;
1618 int sign;
1619
1620 line[1] = line[2] = gdTransparent;
1621
1622 /* Trivial clip the branch */
1623 if(conf.left_right)
1624 {
1625 if(b->cx > gdImageSX(im) || b->cx+b->tw < 0 || b->y-b->th/2 > gdImageSY(im) || b->y+b->th/2 < 0)
1626 return;
1627 }
1628 else
1629 {
1630 if(b->cx-b->tw/2 > gdImageSX(im) || b->cx+b->tw/2 < 0 || b->y > gdImageSY(im) || b->y+b->th < 0)
1631 return;
1632 }
1633
1634 draw_branch_box(im, b, 0, conf.left_right ? b->y - b->h/2 : b->y);
1635
1636 if(conf.left_right)
1637 {
1638 if(conf.upside_down)
1639 {
1640 xx = b->cx;
1641 for(i = 0; i < b->nrevs; i++)
1642 {
1643 revision_t *r = b->revs[i];
1644 line[0] = line[3] = clr(im, "rev_color", r, b)->id;
1645 gdImageSetStyle(im, line, r->stripped > 0 ? 4 : 1);
1646 gdImageLine(im, xx, r->y, r->cx+r->w, r->y, gdStyled);
1647 for(sign = l = 1; l < conf.thick_lines; l++)
1648 {
1649 int pp = (l+1)/2*sign;
1650 gdImageLine(im, xx, r->y+pp, r->cx+r->w, r->y+pp, gdStyled);
1651 sign *= -1;
1652 }
1653 draw_rev(im, r);
1654 xx = r->cx;
1655 }
1656 if(conf.branch_dupbox && b->nrevs)
1657 {
1658 i = b->cx - b->tw + b->w;
1659 gdImageLine(im, xx, b->y, i+b->w, b->y, clr(im, "rev_color", NULL, b)->id);
1660 for(sign = l = 1; l < conf.thick_lines; l++)
1661 {
1662 int pp = (l+1)/2*sign;
1663 gdImageLine(im, xx, b->y+pp, i+b->w, b->y+pp, clr(im, "rev_color", NULL, b)->id);
1664 sign *= -1;
1665 }
1666 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1667 }
1668 }
1669 else
1670 {
1671 xx = b->cx + b->w;
1672 for(i = 0; i < b->nrevs; i++)
1673 {
1674 revision_t *r = b->revs[i];
1675 line[0] = line[3] = clr(im, "rev_color", r, b)->id;
1676 gdImageSetStyle(im, line, r->stripped > 0 ? 4 : 1);
1677 gdImageLine(im, xx, r->y, r->cx, r->y, gdStyled);
1678 for(sign = l = 1; l < conf.thick_lines; l++)
1679 {
1680 int pp = (l+1)/2*sign;
1681 gdImageLine(im, xx, r->y+pp, r->cx, r->y+pp, gdStyled);
1682 sign *= -1;
1683 }
1684 draw_rev(im, r);
1685 xx = r->cx + r->w;
1686 }
1687 if(conf.branch_dupbox && b->nrevs)
1688 {
1689 i = b->cx + b->tw - b->w;
1690 gdImageLine(im, xx, b->y, i, b->y, clr(im, "rev_color", NULL, b)->id);
1691 for(sign = l = 1; l < conf.thick_lines; l++)
1692 {
1693 int pp = (l+1)/2*sign;
1694 gdImageLine(im, xx, b->y+pp, i, b->y+pp, clr(im, "rev_color", NULL, b)->id);
1695 sign *= -1;
1696 }
1697 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1698 }
1699 }
1700 }
1701 else
1702 {
1703 if(conf.upside_down)
1704 {
1705 yy = b->y;
1706 for(i = 0; i < b->nrevs; i++)
1707 {
1708 revision_t *r = b->revs[i];
1709 line[0] = line[3] = clr(im, "rev_color", r, b)->id;
1710 gdImageSetStyle(im, line, r->stripped > 0 ? 4 : 1);
1711 gdImageLine(im, r->cx, yy, r->cx, r->y+r->h, gdStyled);
1712 for(sign = l = 1; l < conf.thick_lines; l++)
1713 {
1714 int pp = (l+1)/2*sign;
1715 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y+r->h, gdStyled);
1716 sign *= -1;
1717 }
1718 draw_rev(im, r);
1719 yy = r->y;
1720 }
1721 if(conf.branch_dupbox && b->nrevs)
1722 {
1723 i = b->y - b->th + b->h;
1724 gdImageLine(im, b->cx, yy, b->cx, i, clr(im, "rev_color", NULL, b)->id);
1725 for(sign = l = 1; l < conf.thick_lines; l++)
1726 {
1727 int pp = (l+1)/2*sign;
1728 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, clr(im, "rev_color", NULL, b)->id);
1729 sign *= -1;
1730 }
1731 draw_branch_box(im, b, 0, i);
1732 }
1733 }
1734 else
1735 {
1736 yy = b->y + b->h;
1737 for(i = 0; i < b->nrevs; i++)
1738 {
1739 revision_t *r = b->revs[i];
1740 line[0] = line[3] = clr(im, "rev_color", r, b)->id;
1741 gdImageSetStyle(im, line, r->stripped > 0 ? 4 : 1);
1742 gdImageLine(im, r->cx, yy, r->cx, r->y, gdStyled);
1743 for(sign = l = 1; l < conf.thick_lines; l++)
1744 {
1745 int pp = (l+1)/2*sign;
1746 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y, gdStyled);
1747 sign *= -1;
1748 }
1749 draw_rev(im, r);
1750 yy = r->y + r->h;
1751 }
1752 if(conf.branch_dupbox && b->nrevs)
1753 {
1754 i = b->y + b->th - b->h;
1755 gdImageLine(im, b->cx, yy, b->cx, i, clr(im, "rev_color", NULL, b)->id);
1756 for(sign = l = 1; l < conf.thick_lines; l++)
1757 {
1758 int pp = (l+1)/2*sign;
1759 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, clr(im, "rev_color", NULL, b)->id);
1760 sign *= -1;
1761 }
1762 draw_branch_box(im, b, 0, i);
1763 }
1764 }
1765 }
1766 }
1767
1768 static void draw_connector(gdImagePtr im, branch_t *b)
1769 {
1770 int l;
1771 int sign;
1772 revision_t *r = b->branchpoint;
1773 int x1 = r->cx + r->w/2 + 2;
1774 int y1 = r->y + r->h/2;
1775 int x2 = b->cx;
1776 int y2 = b->y;
1777
1778 if(conf.left_right)
1779 {
1780 x2 = r->cx + r->w/2;
1781 y2 = r->y + r->h/2 + 3;
1782 x1 = b->cx;
1783 y1 = b->y;
1784 if(conf.upside_down)
1785 x1 += b->w;
1786 }
1787 else
1788 {
1789 x1 = r->cx + r->w/2 + 2;
1790 y1 = r->y + r->h/2;
1791 x2 = b->cx;
1792 y2 = b->y;
1793 if(conf.upside_down)
1794 y2 += b->h;
1795 }
1796 gdImageLine(im, x1, y1, x2, y1, clr(im, "branch_color", NULL, b)->id);
1797 gdImageLine(im, x2, y1, x2, y2, clr(im, "branch_color", NULL, b)->id);
1798 for(sign = l = 1; l < conf.thick_lines; l++)
1799 {
1800 int pp = (l+1)/2*sign;
1801 gdImageLine(im, x1, y1+pp, x2, y1+pp, clr(im, "branch_color", NULL, b)->id);
1802 gdImageLine(im, x2+pp, y1, x2+pp, y2, clr(im, "branch_color", NULL, b)->id);
1803 sign *= -1;
1804 }
1805 }
1806
1807 static void draw_merges(gdImagePtr im, rcsfile_t *rcs, int dot)
1808 {
1809 int i;
1810 for(i = 0; i < rcs->nmerges; i++)
1811 {
1812 revision_t *fr;
1813 revision_t *tr;
1814 int colorid;
1815 int x1, x2, y1, y2;
1816 switch(rcs->merges[i].type)
1817 {
1818 case TR_TAG:
1819 fr = rcs->merges[i].from.tag->logrev;
1820 tr = rcs->merges[i].to.tag->logrev;
1821 colorid = clr(im, "merge_color.id", NULL, NULL)->id;
1822 break;
1823 case TR_REVISION:
1824 fr = rcs->merges[i].from.rev;
1825 tr = rcs->merges[i].to.rev;
1826 colorid = clr(im, "merge_cvsnt_color", NULL, NULL)->id;
1827 break;
1828 default:
1829 continue;
1830 }
1831 if(!fr || !tr || fr == tr)
1832 continue; /* This can happen with detached tags and self-references */
1833 if(conf.left_right)
1834 {
1835 if(fr->branch == tr->branch)
1836 {
1837 y1 = fr->y - fr->h/2;
1838 y2 = tr->y - tr->h/2;
1839 }
1840 else
1841 {
1842 if(fr->y < tr->y)
1843 {
1844 y1 = fr->y + fr->h/2;
1845 y2 = tr->y - tr->h/2;
1846 }
1847 else
1848 {
1849 y1 = fr->y - fr->h/2;
1850 y2 = tr->y + tr->h/2;
1851 }
1852 }
1853 x1 = fr->cx + fr->w/2;
1854 x2 = tr->cx + tr->w/2;
1855 }
1856 else
1857 {
1858 if(fr->branch == tr->branch)
1859 {
1860 x1 = fr->cx - fr->w/2;
1861 x2 = tr->cx - tr->w/2;
1862 }
1863 else
1864 {
1865 if(fr->cx < tr->cx)
1866 {
1867 x1 = fr->cx + fr->w/2;
1868 x2 = tr->cx - tr->w/2;
1869 }
1870 else
1871 {
1872 x1 = fr->cx - fr->w/2;
1873 x2 = tr->cx + tr->w/2;
1874 }
1875 }
1876 if(rcs->merges[i].type == TR_TAG)
1877 {
1878 y1 = fr->y + rcs->merges[i].from.tag->yofs;
1879 y2 = tr->y + rcs->merges[i].to.tag->yofs;
1880 }
1881 else
1882 {
1883 y1 = fr->y + fr->h/2;
1884 y2 = tr->y + tr->h/2;
1885 }
1886 }
1887 if(dot && !conf.merge_arrows)
1888 {
1889 int o = conf.left_right ? 1 : 0;
1890 gdImageArc(im, x2, y2+o, 8, 8, 0, 360, colorid);
1891 /* BUG: We clip manually because libgd segfaults on out of bound values */
1892 if(x2+1 >= 0 && x2+1 < gdImageSX(im) && y2+o+1 >= 0 && y2+o+1 < gdImageSY(im))
1893 gdImageFillToBorder(im, x2+1, y2+o+1, colorid, colorid);
1894 }
1895 else if(dot && conf.merge_arrows)
1896 {
1897 /*
1898 * Arrow patch from Haroon Rafique <haroon.rafique@utoronto.ca>
1899 * Slightly adapted to be more configurable.
1900 */
1901 int sx, sy; /* start point coordinates */
1902 int ex, ey; /* end point coordinates */
1903 double theta;
1904 double u1, v1, u2, v2;
1905 gdPoint p[3];
1906
1907 sx = x1; sy = y1;
1908 ex = x2; ey = y2;
1909 if(conf.left_right)
1910 {
1911 if(fr->branch == tr->branch)
1912 {
1913 int yy = (y1 < y2 ? y1 : y2) - 5;
1914 /* line from (x1,yy) to (x2,yy) */
1915 sy = ey = yy;
1916 }
1917 else
1918 {
1919 if(y1 > y2)
1920 {
1921 /* line from (x1,y1-3) to (x2,y2+3+1) */
1922 sy = y1-3;
1923 ey = y2+3+1;
1924 }
1925 else
1926 {
1927 /* line from (x1,y1+3+1) to (x2,y2-3) */
1928 sy = y1+3+1;
1929 ey = y2-3;
1930 }
1931 }
1932 }
1933 else
1934 {
1935 if(fr->branch == tr->branch)
1936 {
1937 int xx = (x1 < x2 ? x1 : x2) - 5;
1938 /* line from (xx,y1) to (xx,y2) */
1939 sx = ex = xx;
1940 }
1941 else
1942 {
1943 if(x1 > x2)
1944 {
1945 /* line from (x1-3,y1) to (x2+3,y2) */
1946 sx = x1-3;
1947 ex = x2+3;
1948 }
1949 else
1950 {
1951 /* line from (x1+3,y1) to (x2-3,y2) */
1952 sx = x1+3;
1953 ex = x2-3;
1954 }
1955 }
1956 }
1957 /*
1958 * inspiration for arrow code comes from arrows.c in the
1959 * graphviz package. Thank you, AT&T
1960 */
1961 /* theta in radians */
1962 theta = atan2((double)(sy-ey), (double)(sx-ex));
1963 u1 = (double)conf.arrow_length * cos(theta);
1964 v1 = (double)conf.arrow_length * sin(theta);
1965 u2 = (double)conf.arrow_width * cos(theta + M_PI/2.0);
1966 v2 = (double)conf.arrow_width * sin(theta + M_PI/2.0);
1967 /* points of polygon (triangle) */
1968 p[0].x = ROUND(ex + u1 - u2);
1969 p[0].y = ROUND(ey + v1 - v2);
1970 p[1].x = ex;
1971 p[1].y = ey;
1972 p[2].x = ROUND(ex + u1 + u2);
1973 p[2].y = ROUND(ey + v1 + v2);
1974 /* draw the polygon (triangle) */
1975 gdImageFilledPolygon(im, p, 3, colorid);
1976 }
1977 else
1978 {
1979 if(conf.left_right)
1980 {
1981 if(fr->branch == tr->branch)
1982 {
1983 int yy = (y1 < y2 ? y1 : y2) - 5;
1984 gdImageLine(im, x1, y1, x1, yy, colorid);
1985 gdImageLine(im, x2, y2, x2, yy, colorid);
1986 gdImageLine(im, x1, yy, x2, yy, colorid);
1987 }
1988 else
1989 {
1990 if(y1 > y2)
1991 {
1992 gdImageLine(im, x1, y1, x1, y1-3, colorid);
1993 gdImageLine(im, x2, y2+1, x2, y2+3+1, colorid);
1994 gdImageLine(im, x1, y1-3, x2, y2+3+1, colorid);
1995 }
1996 else
1997 {
1998 gdImageLine(im, x1, y1+1, x1, y1+3+1, colorid);
1999 gdImageLine(im, x2, y2, x2, y2-3, colorid);
2000 gdImageLine(im, x1, y1+3+1, x2, y2-3, colorid);
2001 }
2002 }
2003 }
2004 else
2005 {
2006 if(fr->branch == tr->branch)
2007 {
2008 int xx = (x1 < x2 ? x1 : x2) - 5;
2009 gdImageLine(im, xx, y1, x1, y1, colorid);
2010 gdImageLine(im, xx, y2, x2, y2, colorid);
2011 gdImageLine(im, xx, y1, xx, y2, colorid);
2012 }
2013 else
2014 {
2015 if(x1 > x2)
2016 {
2017 gdImageLine(im, x1, y1, x1-3, y1, colorid);
2018 gdImageLine(im, x2, y2, x2+3, y2, colorid);
2019 gdImageLine(im, x1-3, y1, x2+3, y2, colorid);
2020 }
2021 else
2022 {
2023 gdImageLine(im, x1, y1, x1+3, y1, colorid);
2024 gdImageLine(im, x2, y2, x2-3, y2, colorid);
2025 gdImageLine(im, x1+3, y1, x2-3, y2, colorid);
2026 }
2027 }
2028 }
2029 }
2030 }
2031 }
2032
2033 static void draw_messages(gdImagePtr im, int offset)
2034 {
2035 int i;
2036
2037 for(i = 0; i < nmsg_stack; i++)
2038 {
2039 draw_stringnl(im, msg_stack[i].msg, &conf.msg_font, conf.margin_left, offset, ALIGN_HL|ALIGN_VT, clr(im, "msg_color", NULL, NULL));
2040 offset += msg_stack[i].h;
2041 }
2042 }
2043
2044 static void alloc_color(gdImagePtr im, color_t *c)
2045 {
2046 c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
2047 }
2048
2049 static gdImagePtr make_image(rcsfile_t *rcs)
2050 {
2051 gdImagePtr im;
2052 int i;
2053 char *cptr;
2054 int w, h;
2055 int subx = 0, suby = 0;
2056 int subw, subh;
2057 int msgh = 0;
2058
2059 if(subtree_branch)
2060 {
2061 subw = 0;
2062 subh = 0;
2063 if(subtree_rev)
2064 {
2065 for(i = 0; i < subtree_rev->nbranches; i++)
2066 calc_subtree_size(subtree_rev->branches[i], &subx, &suby, &subw, &subh);
2067 }
2068 else
2069 calc_subtree_size(subtree_branch, &subx, &suby, &subw, &subh);
2070 }
2071 else
2072 {
2073 subw = rcs->tw;
2074 subh = rcs->th;
2075 }
2076
2077 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
2078 w = subw + conf.margin_left + conf.margin_right;
2079 h = subh + conf.margin_top + conf.margin_bottom;
2080 i = get_swidth(cptr, &conf.title_font);
2081 if(i > w)
2082 w = i;
2083
2084 if(!quiet && nmsg_stack)
2085 {
2086 int msgw = 0;
2087 for(i = 0; i < nmsg_stack; i++)
2088 {
2089 int ww = msg_stack[i].w = get_swidth(msg_stack[i].msg, &conf.msg_font);
2090 int hh = msg_stack[i].h = get_sheight(msg_stack[i].msg, &conf.msg_font);
2091 msgh += hh;
2092 h += hh;
2093 if(ww > msgw)
2094 msgw = ww;
2095 }
2096 if(msgw > w)
2097 w = msgw;
2098 }
2099
2100 im = gdImageCreate(w, h);
2101 alloc_color(im, &conf.color_bg); /* The background is always a unique color */
2102 clr(im, "__black_color__", NULL, NULL);
2103
2104 if(conf.transparent_bg)
2105 gdImageColorTransparent(im, conf.color_bg.id);
2106
2107 if(!conf.merge_front)
2108 draw_merges(im, rcs, 0);
2109
2110 for(i = 0; i < rcs->nbranches; i++)
2111 {
2112 if(!rcs->branches[i]->folded && !(subtree_branch && !rcs->branches[i]->subtree_draw))
2113 draw_branch(im, rcs->branches[i]);
2114 }
2115
2116 draw_merges(im, rcs, 1); /* The dots of the merge dest */
2117
2118 for(i = 0; i < rcs->nbranches; i++)
2119 {
2120 if(rcs->branches[i]->branchpoint)
2121 draw_connector(im, rcs->branches[i]);
2122 }
2123
2124 /* Clear the margins if we have a partial tree */
2125 if(subtree_branch)
2126 {
2127 gdImageFilledRectangle(im, 0, 0, w-1, conf.margin_top-1, conf.color_bg.id);
2128 gdImageFilledRectangle(im, 0, 0, conf.margin_left-1, h-1, conf.color_bg.id);
2129 gdImageFilledRectangle(im, 0, h-conf.margin_bottom, w-1, h-1, conf.color_bg.id);
2130 gdImageFilledRectangle(im, w-conf.margin_right, 0, w-1, h-1, conf.color_bg.id);
2131 }
2132
2133 draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, clr(im, "title_color", NULL, NULL));
2134 xfree(cptr);
2135
2136 if(conf.merge_front)
2137 draw_merges(im, rcs, 0);
2138
2139 if(!quiet)
2140 draw_messages(im, h - conf.margin_bottom/2 - msgh);
2141
2142 return im;
2143 }
2144
2145 /*
2146 **************************************************************************
2147 * Layout routines
2148 *
2149 * Branch BBox:
2150 * left = center_x - total_width / 2 (cx-tw)/2
2151 * right = center_x + total_width / 2 (cx+tw)/2
2152 * top = y_pos (y)
2153 * bottom = y_pos + total_height (y+th)
2154 *
2155 * Margins of branches:
2156 *
2157 * . .
2158 * . .
2159 * +--------------+
2160 * ^
2161 * | branch_margin .
2162 * v .
2163 * ----------------+ .
2164 * | ^ |
2165 * | | branch_connect |
2166 * | v |
2167 *..-+ +t-----+------+ +------+------+
2168 * | l | | |
2169 * | <--> | branch bbox | <--> | branch bbox |
2170 * | | | r | | |
2171 *..-+ | +------------b+ | +-------------+
2172 * | ^ branch_margin
2173 * | | branch_margin
2174 * | v
2175 * | +-------------+
2176 * | . .
2177 * | . .
2178 * |
2179 * branch_margin
2180 *
2181 * FIXME: There are probable som +/-1 errors in the code...
2182 * (notably shadows are not calculated in the margins)
2183 **************************************************************************
2184 */
2185 static void move_branch(branch_t *b, int x, int y)
2186 {
2187 int i;
2188 b->cx += x;
2189 b->y += y;
2190 for(i = 0; i < b->nrevs; i++)
2191 {
2192 b->revs[i]->cx += x;
2193 b->revs[i]->y += y;
2194 }
2195 }
2196
2197 static void initial_reposition_branch(revision_t *r, int *x, int *w)
2198 {
2199 int i, j;
2200 for(j = 0; j < r->nbranches; j++)
2201 {
2202 branch_t *b = r->branches[j];
2203 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
2204 *w = b->tw/2;
2205 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
2206 *x = b->cx;
2207 /* Recurse to move branches of branched revisions */
2208 for(i = b->nrevs-1; i >= 0; i--)
2209 {
2210 initial_reposition_branch(b->revs[i], x, w);
2211 }
2212 }
2213 }
2214
2215 static void initial_reposition_branch_lr(revision_t *r, int *y, int *h)
2216 {
2217 int i, j;
2218 for(j = 0; j < r->nbranches; j++)
2219 {
2220 branch_t *b = r->branches[j];
2221 *y += *h + conf.rev_minline + b->th/2 - b->y;
2222 *h = b->th/2;
2223 move_branch(b, r->cx + r->w/2 + conf.branch_connect, *y);
2224 *y = b->y;
2225 /* Recurse to move branches of branched revisions */
2226 for(i = b->nrevs-1; i >= 0; i--)
2227 {
2228 initial_reposition_branch_lr(b->revs[i], y, h);
2229 }
2230 }
2231 }
2232
2233 static void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
2234 {
2235 int x1 = *x;
2236 int x2 = x1 + *w;
2237 int y1 = *y;
2238 int y2 = y1 + *h;
2239 int xx1;
2240 int xx2;
2241 int yy1;
2242 int yy2;
2243
2244 if(conf.left_right)
2245 {
2246 xx1 = b->cx;
2247 yy1 = b->y - b->th/2;
2248 }
2249 else
2250 {
2251 xx1 = b->cx - b->tw/2;
2252 yy1 = b->y;
2253 }
2254 xx2 = xx1 + b->tw;
2255 yy2 = yy1 + b->th;
2256
2257 x1 = MIN(x1, xx1);
2258 x2 = MAX(x2, xx2);
2259 y1 = MIN(y1, yy1);
2260 y2 = MAX(y2, yy2);
2261 *x = x1;
2262 *y = y1;
2263 *w = x2 - x1;
2264 *h = y2 - y1;
2265 }
2266
2267 static void calc_subtree_size(branch_t *b, int *x, int *y, int *w, int *h)
2268 {
2269 int i, j;
2270
2271 rect_union(x, y, w, h, b);
2272
2273 for(i = 0; i < b->nrevs; i++)
2274 {
2275 for(j = 0; j < b->revs[i]->nbranches; j++)
2276 calc_subtree_size(b->revs[i]->branches[j], x, y, w, h);
2277 }
2278 }
2279
2280 static int branch_intersects(int top, int bottom, int left, branch_t *b)
2281 {
2282 int br = b->cx + b->tw/2;
2283 int bt = b->y - conf.branch_connect - conf.branch_margin/2;
2284 int bb = b->y + b->th + conf.branch_margin/2;
2285 return !(bt > bottom || bb < top || br >= left);
2286 }
2287
2288 static int branch_intersects_lr(int left, int right, int top, branch_t *b)
2289 {
2290 int bt = b->y + b->th/2;
2291 int bl = b->cx - conf.branch_connect - conf.branch_margin/2;
2292 int br = b->cx + b->tw + conf.branch_margin/2;
2293 return !(bl > right || br < left || bt >= top);
2294 }
2295
2296 static int kern_branch(rcsfile_t *rcs, branch_t *b)
2297 {
2298 int left = b->cx - b->tw/2;
2299 int top = b->y - conf.branch_connect - conf.branch_margin/2;
2300 int bottom = b->y + b->th + conf.branch_margin/2;
2301 int i;
2302 int xpos = 0;
2303
2304 for(i = 0; i < rcs->nbranches; i++)
2305 {
2306 branch_t *bp = rcs->branches[i];
2307 if(bp == b)
2308 continue;
2309 if(branch_intersects(top, bottom, left, bp))
2310 {
2311 int m = bp->cx + bp->tw/2 + conf.branch_margin;
2312 if(m > xpos)
2313 xpos = m;
2314 }
2315 }
2316 if(xpos && (b->cx - b->tw/2) - xpos > 0)
2317 {
2318 move_branch(b, xpos - (b->cx - b->tw/2), 0);
2319 return 1;
2320 }
2321 return 0;
2322 }
2323
2324 static int kern_branch_lr(rcsfile_t *rcs, branch_t *b)
2325 {
2326 int top = b->y - b->th/2;
2327 int left = b->cx - conf.branch_connect - conf.branch_margin/2;
2328 int right = b->cx + b->tw + conf.branch_margin/2;
2329 int i;
2330 int ypos = 0;
2331
2332 for(i = 0; i < rcs->nbranches; i++)
2333 {
2334 branch_t *bp = rcs->branches[i];
2335 if(bp == b)
2336 continue;
2337 if(branch_intersects_lr(left, right, top, bp))
2338 {
2339 int m = bp->y + bp->th/2 + conf.branch_margin;
2340 if(m > ypos)
2341 ypos = m;
2342 }
2343 }
2344 if(ypos && (b->y - b->th/2) - ypos > 0)
2345 {
2346 move_branch(b, 0, ypos - (b->y - b->th/2));
2347 return 1;
2348 }
2349 return 0;
2350 }
2351
2352 static int kern_tree(rcsfile_t *rcs)
2353 {
2354 int i;
2355 int moved;
2356 int safeguard;
2357 int totalmoved = 0;
2358 for(moved = 1, safeguard = LOOPSAFEGUARD; moved && safeguard; safeguard--)
2359 {
2360 moved = 0;
2361 for(i = 1; i < rcs->nbranches; i++)
2362 {
2363 if(conf.left_right)
2364 moved += kern_branch_lr(rcs, rcs->branches[i]);
2365 else
2366 moved += kern_branch(rcs, rcs->branches[i]);
2367 }
2368 totalmoved += moved;
2369 #ifdef DEBUG
2370 fprintf(stderr, "kern_tree: moved=%d\n", moved);
2371 #endif
2372 }
2373 if(!safeguard)
2374 stack_msg(MSG_WARN, "kern_tree: safeguard terminated possible infinite loop; please report.");
2375 return totalmoved;
2376 }
2377
2378 static int index_of_revision(revision_t *r)
2379 {
2380 branch_t *b = r->branch;
2381 int i;
2382 for(i = 0; i < b->nrevs; i++)
2383 {
2384 if(r == b->revs[i])
2385 return i;
2386 }
2387 stack_msg(MSG_ERR, "index_of_revision: Cannot find revision in branch\n");
2388 return 0;
2389 }
2390
2391 static void branch_bbox(branch_t *br, int *l, int *r, int *t, int *b)
2392 {
2393 if(l) *l = br->cx - br->tw/2;
2394 if(r) *r = br->cx + br->tw/2;
2395 if(t) *t = br->y;
2396 if(b) *b = br->y + br->th + ((conf.branch_dupbox && br->nrevs) ? conf.rev_minline + br->h : 0);
2397 }
2398
2399 static void branch_ext_bbox(branch_t *br, int *l, int *r, int *t, int *b)
2400 {
2401 int extra = conf.branch_margin & 1; /* Correct +/-1 error on div 2 */
2402 branch_bbox(br, l, r, t, b);
2403 if(l) *l -= conf.branch_margin/2;
2404 if(r) *r += conf.branch_margin/2 + extra;
2405 if(t) *t -= conf.branch_connect + conf.branch_margin/2;
2406 if(b) *b += conf.branch_margin/2 + extra;
2407 }
2408
2409 static int branch_distance(branch_t *br1, branch_t *br2)
2410 {
2411 int l1, r1, t1, b1;
2412 int l2, r2, t2, b2;
2413 assert(br1 != NULL);
2414 assert(br2 != NULL);
2415 branch_bbox(br1, &l1, &r1, NULL, NULL);
2416 branch_bbox(br2, &l2, &r2, NULL, NULL);
2417 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
2418 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
2419 /* Return:
2420 * - 0 if branches have no horizontal overlap
2421 * - positive if b1 is left of b2
2422 * - negative if b2 is left of b1
2423 */
2424 if((t1 > t2 && t1 < b2) || (b1 > t2 && b1 < b2))
2425 return l1 < l2 ? l2 - r1 : -(l1 - r2);
2426 else
2427 return 0;
2428 }
2429
2430 static int space_needed(branch_t *br1, branch_t *br2)
2431 {
2432 int t1, b1;
2433 int t2, b2;
2434 assert(br1 != NULL);
2435 assert(br2 != NULL);
2436 assert(br1->cx < br2->cx); /* br1 must be left of br2 */
2437 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
2438 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
2439 /* Return:
2440 * - positive if top br1 is located lower than br2
2441 * - negatve is top br2 is located lower than br1
2442 */
2443 if(t1 > t2)
2444 return -(t1 - b2);
2445 else
2446 return t2 - b1;
2447 }
2448
2449 static void move_yr_branch(branch_t *b, int dy)
2450 {
2451 int i, j;
2452 #ifdef DEBUG
2453 /* fprintf(stderr, "move_yr_branch: b=%s, dy=%d\n", b->branch->branch, dy);*/
2454 #endif
2455 b->y += dy;
2456 for(i = 0; i < b->nrevs; i++)
2457 {
2458 b->revs[i]->y += dy;
2459 for(j = 0; j < b->revs[i]->nbranches; j++)
2460 {
2461 #ifdef DEBUG
2462 /* fprintf(stderr, ".");*/
2463 #endif
2464 move_yr_branch(b->revs[i]->branches[j], dy);
2465 }
2466 }
2467 }
2468
2469 static void move_trunk(revision_t *r, int dy)
2470 {
2471 int i, j;
2472 branch_t *b = r->branch;
2473 b->th += dy;
2474 for(i = index_of_revision(r); i < b->nrevs; i++)
2475 {
2476 #ifdef DEBUG
2477 fprintf(stderr, "move_trunk: start %s, moving %s by %d (b's %d)\n", r->rev->rev, b->revs[i]->rev->rev, dy, b->revs[i]->nbranches);
2478 #endif
2479 b->revs[i]->y += dy;
2480 for(j = 0; j < b->revs[i]->nbranches; j++)
2481 {
2482 move_yr_branch(b->revs[i]->branches[j], dy);
2483 }
2484 }
2485 }
2486
2487 static int space_below(rcsfile_t *rcs, revision_t *r)
2488 {
2489 int i, j;
2490 int bl, br, bb;
2491 int space = INT_MAX;
2492 branch_t *b = r->branch;
2493 branch_t *minb = NULL;
2494
2495 branch_ext_bbox(b, &bl, &br, NULL, &bb);
2496 for(i = 0; i < rcs->nbranches; i++)
2497 {
2498 int tbl, tbr, tbt;
2499 branch_t *tb = rcs->branches[i];
2500 branch_ext_bbox(tb, &tbl, &tbr, &tbt, NULL);
2501 if(tb == b)
2502 continue;
2503 if(tbt > bb) /* Must be below our branch */
2504 {
2505 if(tb->branchpoint) /* Take account for the horiz connector */
2506 tbl = tb->branchpoint->cx + tb->branchpoint->branch->tw/2;
2507 if((bl >= tbl && bl <= tbr) || (br <= tbr && br >= tbl))
2508 {
2509 int s = tbt - bb - conf.branch_connect;
2510 if(s < space)
2511 {
2512 space = s;
2513 minb = tb;
2514 }
2515 }
2516 }
2517 }
2518 if(b->branchpoint)
2519 {
2520 for(i = index_of_revision(r); i < b->nrevs; i++)
2521 {
2522 for(j = 0; j < b->revs[i]->nbranches; j++)
2523 {
2524 int s = space_below(rcs, b->revs[i]->branches[j]->revs[0]);
2525 if(s < space)
2526 space = s;
2527 }
2528 }
2529 }
2530 #ifdef DEBUG
2531 fprintf(stderr, "space_below: from %s have %d to %s\n", b->branch->branch, space, minb ? minb->branch->branch : "<recursed>");
2532 #endif
2533 return space;
2534 }
2535
2536 static int space_available(rcsfile_t *rcs, branch_t *colbr, branch_t *tagbr, int *nl, revision_t **bpcommon)
2537 {
2538 int i;
2539 int space = 0;
2540 int nlinks = 0;
2541 revision_t *r;
2542 branch_t *b;
2543 branch_t *ancestor;
2544 revision_t *branchpoint;
2545
2546 if(!tagbr->branchpoint || !colbr->branchpoint)
2547 {
2548 stack_msg(MSG_WARN, "space_available: Trying to stretch the top?");
2549 return 0;
2550 }
2551
2552 r = colbr->branchpoint;
2553 b = r->branch;
2554 branchpoint = tagbr->branchpoint;
2555 ancestor = branchpoint->branch;
2556 assert(b != NULL);
2557 assert(ancestor != NULL);
2558
2559 while(1)
2560 {
2561 int s;
2562 int rtag = b == ancestor ? index_of_revision(branchpoint)+1 : 0;
2563 for(i = index_of_revision(r); i >= rtag; i--)
2564 {
2565 if(i > 0)
2566 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
2567 else
2568 s = b->revs[i]->y - (b->y + b->h);
2569 if(s < conf.rev_maxline)
2570 {
2571 space += conf.rev_maxline - s;
2572 nlinks++;
2573 }
2574 }
2575 s = space_below(rcs, r);
2576 if(s < space)
2577 space = s;
2578 #ifdef DEBUG
2579 if(space < 0)
2580 return -1;
2581 #endif
2582 if(b == ancestor)
2583 break;
2584 r = b->branchpoint;
2585 if(!r)
2586 {
2587 /* Not a common ancestor */
2588 r = colbr->branchpoint;
2589 b = r->branch;
2590 branchpoint = ancestor->branchpoint;
2591 if(!branchpoint)
2592 {
2593 stack_msg(MSG_WARN, "space_available: No common ancestor?");
2594 return 0;
2595 }
2596 ancestor = branchpoint->branch;
2597 assert(ancestor != NULL);
2598 nlinks = 0;
2599 space = 0;
2600 continue; /* Restart with a new ancestor */
2601 }
2602 b = r->branch;
2603 }
2604 if(nl)
2605 *nl = nlinks; /* Return the number of links that can stretch */
2606 if(bpcommon)
2607 *bpcommon = branchpoint; /* Return the ancestral branchpoint on the common branch */
2608 return space;
2609 }
2610
2611 static int stretch_branches(rcsfile_t *rcs, branch_t *br1, branch_t *br2, int totalstretch)
2612 {
2613 revision_t *r;
2614 revision_t *bpcommon = NULL;
2615 branch_t *ancestor = NULL;
2616 branch_t *b;
2617 int i;
2618 int space;
2619 int nlinks;
2620 int dy;
2621 int rest;
2622
2623 space = space_available(rcs, br1, br2, &nlinks, &bpcommon);
2624 if(bpcommon)
2625 ancestor = bpcommon->branch;
2626
2627 #ifdef DEBUG
2628 if(space == -1)
2629 return 0;
2630 fprintf(stderr, "stretch_branches: space available %d over %d links common %s\n", space, nlinks, ancestor->branch->branch);
2631 #endif
2632 if(space < totalstretch)
2633 return 0;
2634
2635 dy = totalstretch / nlinks;
2636 rest = totalstretch - dy * nlinks;
2637
2638 r = br1->branchpoint;
2639 b = r->branch;
2640 while(1)
2641 {
2642 int rtag = b == ancestor ? index_of_revision(bpcommon)+1 : 0;
2643 for(i = index_of_revision(r); i >= rtag; i--)
2644 {
2645 int s, q;
2646 if(i > 0)
2647 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
2648 else
2649 s = b->revs[i]->y - (b->y + b->h);
2650 q = conf.rev_maxline - s;
2651 if(q > 0)
2652 {
2653 int d = rest ? rest/nlinks+1 : 0;
2654 if(q >= dy+d)
2655 {
2656 move_trunk(b->revs[i], dy+d);
2657 }
2658 else
2659 {
2660 move_trunk(b->revs[i], q);
2661 rest += dy+d - q;
2662 }
2663 rest -= d;
2664 nlinks--;
2665 }
2666 }
2667 if(b == ancestor)
2668 break;
2669 r = b->branchpoint;
2670 assert(r != NULL); /* else 'space_available' wouldn't have returned positively */
2671 b = r->branch;
2672 }
2673 return 1;
2674 }
2675
2676 static branch_t *find_collision_branch(rcsfile_t *rcs, branch_t *b)
2677 {
2678 int i;
2679 int dist = INT_MAX;
2680 branch_t *col = NULL;
2681
2682 for(i = 0; i < rcs->nbranches; i++)
2683 {
2684 int t = branch_distance(rcs->branches[i], b);
2685 if(t > 0 && t < dist)
2686 {
2687 dist = t;
2688 col = rcs->branches[i];
2689 }
2690 }
2691 return col;
2692 }
2693
2694 static void auto_stretch(rcsfile_t *rcs)
2695 {
2696 int i;
2697 int safeguard;
2698
2699 for(i = 0, safeguard = LOOPSAFEGUARD; i < rcs->nbranches && safeguard; i++)
2700 {
2701 int bl, pr;
2702 branch_t *b = rcs->branches[i];
2703 if(!b->branchpoint)
2704 continue;
2705 branch_bbox(b, &bl, NULL, NULL, NULL);
2706 branch_bbox(b->branchpoint->branch, NULL, &pr, NULL, NULL);
2707 if(bl - conf.branch_margin - pr > 0)
2708 {
2709 branch_t *col;
2710 int spaceneeded;
2711 /* There is a potential to move branch b further left.
2712 * All branches obstructing this one from moving further
2713 * left must be originating from revisions below
2714 * b->branchpoint until a common ancester.
2715 * So, we search all branches for a branch that lies left
2716 * of b and is closest to b. This is then the collission
2717 * branch that needs to be moved.
2718 */
2719 col = find_collision_branch(rcs, b);
2720 if(!col)
2721 continue;
2722 spaceneeded = space_needed(col, b);
2723 if(spaceneeded < 0)
2724 continue;
2725 #ifdef DEBUG
2726 fprintf(stderr, "auto_stretch: %s collides %s need %d\n", b->branch->branch, col->branch->branch, spaceneeded);
2727 #endif
2728 /* Trace the collision branch back to find the common ancester
2729 * of both col and b. All revisions encountered while traversing
2730 * backwards must be stretched, including all revisions on the
2731 * common ancester from where the branches sprout.
2732 */
2733 if(stretch_branches(rcs, col, b, spaceneeded))
2734 {
2735 if(kern_tree(rcs))
2736 {
2737 /* Restart the process because movement can
2738 * cause more movement.
2739 */
2740 i = 0 - 1; /* -1 for the i++ of the loop */
2741 safeguard--; /* Prevent infinite loop, just in case */
2742 }
2743 /*return;*/
2744 }
2745 }
2746 }
2747 if(!safeguard)
2748 stack_msg(MSG_ERR, "auto_stretch: safeguard terminated possible infinite loop; please report.");
2749 }
2750
2751 static void fold_branch(rcsfile_t *rcs, revision_t *r)
2752 {
2753 int i, j;
2754 branch_t *btag = NULL;
2755
2756 for(i = 0; i < r->nbranches; i++)
2757 {
2758 branch_t *b = r->branches[i];
2759 if(!b->nrevs && b->ntags < 2)
2760 {
2761 /* No commits in this branch and no duplicate tags */
2762 if(!btag)
2763 btag = b;
2764 else
2765 {
2766 /* We have consecutive empty branches, fold */
2767 b->folded = 1;
2768 b->folded_to = btag;
2769 for(j = 0; j < rcs->nbranches; j++)
2770 {
2771 if(b == rcs->branches[j])
2772 {
2773 /* Zap the branch from the admin */
2774 memmove(&rcs->branches[j],
2775 &rcs->branches[j+1],
2776 (rcs->nbranches - j - 1)*sizeof(rcs->branches[0]));
2777 rcs->nbranches--;
2778 rcs->nfolds++;
2779 break;
2780 }
2781
2782 }
2783 memmove(&r->branches[i], &r->branches[i+1], (r->nbranches - i - 1)*sizeof(r->branches[0]));
2784 r->nbranches--;
2785 i--; /* We have one less now */
2786
2787 /* Add to the fold-list */
2788 btag->folds = xrealloc(btag->folds, (btag->nfolds+1) * sizeof(btag->folds[0]));
2789 btag->folds[btag->nfolds] = b;
2790 btag->nfolds++;
2791 }
2792 }
2793 else
2794 {
2795 if(!conf.branch_foldall)
2796 btag = NULL; /* Start a new box */
2797 /* Recursively fold sub-branches */
2798 for(j = 0; j < b->nrevs; j++)
2799 fold_branch(rcs, b->revs[j]);
2800 }
2801 }
2802 }
2803
2804 static void mark_subtree(branch_t *b)
2805 {
2806 int i, j;
2807 b->subtree_draw = 1;
2808 for(i = 0; i < b->nrevs; i++)
2809 {
2810 for(j = 0; j < b->revs[i]->nbranches; j++)
2811 mark_subtree(b->revs[i]->branches[j]);
2812 }
2813 }
2814
2815 static void make_layout(rcsfile_t *rcs)
2816 {
2817 int i, j;
2818 int x, y;
2819 int w, h;
2820 int w2;
2821
2822 /* Remove all unwanted revisions */
2823 if(conf.strip_untagged)
2824 {
2825 int fr = conf.strip_first_rev ? 0 : 1;
2826 for(i = 0; i < rcs->nbranches; i++)
2827 {
2828 branch_t *bp = rcs->branches[i];
2829 for(j = fr; j < bp->nrevs-1; j++)
2830 {
2831 if(!bp->revs[j]->ntags && bp->revs[j]->stripped >= 0 && !bp->revs[j]->nbranches)
2832 {
2833 bp->revs[j]->stripped = 1;
2834 memmove(&bp->revs[j], &bp->revs[j+1], (bp->nrevs-j-1) * sizeof(bp->revs[0]));
2835 bp->nrevs--;
2836 j--;
2837 }
2838 }
2839 }
2840 }
2841
2842 /* Find the sub-tree(s) we want to see */
2843 if(conf.branch_subtree && conf.branch_subtree[0])
2844 {
2845 branch_t **b;
2846 revision_t **r;
2847 rev_t rev;
2848 int k;
2849 char *tag = conf.branch_subtree;
2850
2851 /* First translate any symbolic tag to a real branch/revision number */
2852 if(rcs->tags)
2853 {
2854 for(k = 0; k < rcs->tags->ntags; k++)
2855 {
2856 if(!strcmp(conf.branch_subtree, rcs->tags->tags[k]->tag))
2857 {
2858 if(rcs->tags->tags[k]->rev->isbranch)
2859 tag = rcs->tags->tags[k]->rev->branch;
2860 else
2861 tag = rcs->tags->tags[k]->rev->rev;
2862 break;
2863 }
2864 }
2865 }
2866
2867 /* Find the corresponding branch */
2868 rev.branch = tag;
2869 rev.rev = NULL;
2870 rev.isbranch = 1;
2871 b = bsearch(&rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
2872 if(b)
2873 {
2874 if((*b)->branchpoint)
2875 {
2876 subtree_branch = *b;
2877 for(k = 0; k < (*b)->branchpoint->nbranches; k++)
2878 mark_subtree((*b)->branchpoint->branches[k]);
2879 }
2880 /*
2881 * else -> we want everything.
2882 * This happens for the top level branch because it has no
2883 * branchpoint. We do not set the subtree_branch, which then
2884 * results in drawing the whole tree as if we did not select a
2885 * particular branch.
2886 */
2887 }
2888 else
2889 {
2890 /* Maybe it is a revision we want all subtrees from */
2891 rev.rev = tag;
2892 rev.branch = NULL;
2893 rev.isbranch = 0;
2894 r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
2895 if(r)
2896 {
2897 if((*r)->nbranches)
2898 {
2899 subtree_branch = (*r)->branches[0];
2900 subtree_rev = *r;
2901 for(k = 0; k < (*r)->nbranches; k++)
2902 mark_subtree((*r)->branches[k]);
2903 }
2904 /*
2905 * else -> we select everything.
2906 * This happens for the any revision that has no branches.
2907 * We do not set the subtree_branch, which then results in
2908 * drawing the whole tree as if we did not select a
2909 * particular revision's branches.
2910 */
2911 }
2912 }
2913 }
2914
2915 /* Fold all empty branches in one box on the same branchpoint */
2916 if(conf.branch_fold)
2917 {
2918 for(i = 0; i < rcs->branches[0]->nrevs; i++)
2919 {
2920 if(rcs->branches[0]->revs[i]->nbranches > 0)
2921 fold_branch(rcs, rcs->branches[0]->revs[i]);
2922 }
2923 }
2924
2925 /* Remove all unwanted tags */
2926 for(i = 0; i < rcs->nbranches; i++)
2927 {
2928 branch_t *bp = rcs->branches[i];
2929 for(j = 0; j < bp->nrevs; j++)
2930 {
2931 revision_t *r = bp->revs[j];
2932 int k;
2933 for(k = 0; k < r->ntags; k++)
2934 {
2935 if(r->tags[k]->ignore > 0)
2936 {
2937 memmove(&r->tags[k], &r->tags[k+1], (r->ntags-k-1) * sizeof(r->tags[0]));
2938 r->ntags--;
2939 k--;
2940 }
2941 }
2942 }
2943 }
2944
2945 /* Calculate the box-sizes of the revisions */
2946 for(i = 0; i < rcs->nsrev; i++)
2947 {
2948 revision_t *rp;
2949 int w;
2950 int h;
2951 rp = rcs->srev[i];
2952 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
2953 w = get_swidth(rp->revtext, &conf.rev_text_font);
2954 j = get_swidth(rp->rev->rev, &conf.rev_font);
2955 if(j > w)
2956 w = j;
2957 h = get_sheight(rp->revtext, &conf.rev_text_font);
2958 if(!conf.rev_hidenumber)
2959 h += get_sheight(rp->rev->rev, &conf.rev_font);
2960 for(j = 0; j < rp->ntags; j++)
2961 {
2962 int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
2963 int th;
2964 if(ww > w) w = ww;
2965 th = get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
2966 rp->tags[j]->yofs = h + th/2 + conf.rev_tspace;
2967 h += th;
2968 }
2969 rp->w = w + conf.rev_lspace + conf.rev_rspace;
2970 rp->h = h + conf.rev_tspace + conf.rev_bspace;
2971 }
2972
2973 /* Calculate the box-sizes of the branches */
2974 for(i = 0; i < rcs->nbranches; i++)
2975 {
2976 branch_t *bp = rcs->branches[i];
2977 int w;
2978 int h;
2979 if(!bp->nfolds)
2980 {
2981 w = get_swidth(bp->branch->branch, &conf.branch_font);
2982 if(conf.rev_hidenumber)
2983 h = 0;
2984 else
2985 h = get_sheight(bp->branch->branch, &conf.branch_font);
2986 for(j = 0; j < bp->ntags; j++)
2987 {
2988 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_tag_font);
2989 if(ww > w) w = ww;
2990 h += get_sheight(bp->tags[j]->tag, &conf.branch_tag_font);
2991 }
2992 }
2993 else
2994 {
2995 int h1, h2;
2996 int w1, w2;
2997 int fw;
2998 w1 = get_swidth(bp->branch->branch, &conf.branch_font);
2999 w1 += get_swidth(" ", &conf.branch_font);
3000 w2 = get_swidth(bp->tags[0]->tag, &conf.branch_tag_font);
3001 fw = w1;
3002 w = w1 + w2;
3003 h1 = get_sheight(bp->branch->branch, &conf.branch_font);
3004 h2 = get_sheight(bp->tags[0]->tag, &conf.branch_tag_font);
3005 h = MAX(h1, h2);
3006 for(j = 0; j < bp->nfolds; j++)
3007 {
3008 w1 = get_swidth(bp->folds[j]->branch->branch, &conf.branch_font);
3009 w1 += get_swidth(" ", &conf.branch_font);
3010 w2 = get_swidth(bp->folds[j]->tags[0]->tag, &conf.branch_tag_font);
3011 if(w1 > fw)
3012 fw = w1;
3013 if(w1 + w2 > w)
3014 w = w1 + w2;
3015 h1 = get_sheight(bp->folds[j]->branch->branch, &conf.branch_font);
3016 h2 = get_sheight(bp->folds[j]->tags[0]->tag, &conf.branch_tag_font);
3017 h += MAX(h1, h2);
3018 }
3019 bp->fw = fw;
3020 }
3021 w += conf.branch_lspace + conf.branch_rspace;
3022 h += conf.branch_tspace + conf.branch_bspace;
3023 bp->w = w;
3024 bp->h = h;
3025 if(conf.left_right)
3026 {
3027 for(j = 0; j < bp->nrevs; j++)
3028 {
3029 if(bp->revs[j]->h > h)
3030 h = bp->revs[j]->h;
3031 w += bp->revs[j]->w + conf.rev_minline;
3032 }
3033 if(conf.branch_dupbox && bp->nrevs)
3034 w += bp->w + conf.rev_minline;
3035 }
3036 else
3037 {
3038 for(j = 0; j < bp->nrevs; j++)
3039 {
3040 if(bp->revs[j]->w > w)
3041 w = bp->revs[j]->w;
3042 h += bp->revs[j]->h + conf.rev_minline;
3043 }
3044 if(conf.branch_dupbox && bp->nrevs)
3045 h += bp->h + conf.rev_minline;
3046 }
3047 bp->th = h;
3048 bp->tw = w;
3049 }
3050
3051 /* Calculate the relative positions of revs in a branch */
3052 if(conf.left_right)
3053 {
3054 for(i = 0; i < rcs->nbranches; i++)
3055 {
3056 branch_t *b = rcs->branches[i];
3057 y = b->th/2;
3058 x = b->w;
3059 b->y = y;
3060 b->cx = 0;
3061 for(j = 0; j < b->nrevs; j++)
3062 {
3063 x += conf.rev_minline;
3064 b->revs[j]->y = y;
3065 b->revs[j]->cx = x;
3066 x += b->revs[j]->w;
3067 }
3068 }
3069 }
3070 else
3071 {
3072 for(i = 0; i < rcs->nbranches; i++)
3073 {
3074 branch_t *b = rcs->branches[i];
3075 x = b->tw/2;
3076 y = b->h;
3077 b->cx = x;
3078 b->y = 0;
3079 for(j = 0; j < b->nrevs; j++)
3080 {
3081 y += conf.rev_minline;
3082 b->revs[j]->cx = x;
3083 b->revs[j]->y = y;
3084 y += b->revs[j]->h;
3085 }
3086 }
3087 }
3088
3089 /* Initially reposition the branches from bottom to top progressively right */
3090 if(conf.left_right)
3091 {
3092 x = rcs->branches[0]->y;
3093 w2 = rcs->branches[0]->th / 2;
3094 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
3095 {
3096 initial_reposition_branch_lr(rcs->branches[0]->revs[i], &x, &w2);
3097 }
3098 }
3099 else
3100 {
3101 x = rcs->branches[0]->cx;
3102 w2 = rcs->branches[0]->tw / 2;
3103 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
3104 {
3105 initial_reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
3106 }
3107 }
3108
3109 /* Initially move branches left if there is room */
3110 kern_tree(rcs);
3111
3112 /* Try to kern the branches more by expanding the inter-revision spacing */
3113 if(conf.auto_stretch && !conf.left_right)
3114 auto_stretch(rcs);
3115
3116 /* Calculate overall image size */
3117 if(conf.left_right)
3118 {
3119 x = rcs->branches[0]->cx;
3120 y = rcs->branches[0]->y - rcs->branches[0]->th/2;
3121 }
3122 else
3123 {
3124 x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
3125 y = rcs->branches[0]->y;
3126 }
3127 w = rcs->branches[0]->tw;
3128 h = rcs->branches[0]->th;
3129 for(i = 1; i < rcs->nbranches; i++)
3130 rect_union(&x, &y, &w, &h, rcs->branches[i]);
3131 rcs->tw = w;
3132 rcs->th = h;
3133
3134 /* Flip the entire tree */
3135 if(conf.upside_down)
3136 {
3137 if(conf.left_right)
3138 {
3139 x += rcs->tw;
3140 for(i = 0; i < rcs->nbranches; i++)
3141 {
3142 branch_t *b = rcs->branches[i];
3143 for(j = 0; j < b->nrevs; j++)
3144 {
3145 revision_t *r = b->revs[j];
3146 r->cx = x - r->cx - r->w;
3147 }
3148 b->cx = x - b->cx - b->w;
3149 }
3150 }
3151 else
3152 {
3153 y += rcs->th;
3154 for(i = 0; i < rcs->nbranches; i++)
3155 {
3156 branch_t *b = rcs->branches[i];
3157 for(j = 0; j < b->nrevs; j++)
3158 {
3159 revision_t *r = b->revs[j];
3160 r->y = y - r->y - r->h;
3161 }
3162 b->y = y - b->y - b->h;
3163 }
3164 }
3165 }
3166
3167 /* Relocate the lot if we only draw a sub-tree */
3168 if(subtree_branch)
3169 {
3170 int xx, yy;
3171
3172 if(subtree_branch->folded) /* Fix the reference if the branch got folded */
3173 subtree_branch = subtree_branch->folded_to;
3174
3175 xx = conf.left_right ? subtree_branch->cx : subtree_branch->cx - subtree_branch->tw/2;
3176 yy = conf.left_right ? subtree_branch->y - subtree_branch->th/2 : subtree_branch->y;
3177 if(subtree_branch != rcs->branches[0])
3178 {
3179 if(conf.left_right)
3180 xx -= conf.branch_connect;
3181 else
3182 yy -= conf.branch_connect;
3183 }
3184 for(i = 0; i < rcs->nbranches; i++)
3185 move_branch(rcs->branches[i], -xx, -yy);
3186 }
3187
3188 /* Move everything w.r.t. the top-left margin */
3189 for(i = 0; i < rcs->nbranches; i++)
3190 move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
3191 }
3192
3193 /*
3194 **************************************************************************
3195 * Imagemap functions
3196 **************************************************************************
3197 */
3198 static void map_merge_box(rcsfile_t *rcs, FILE *fp, revision_t *fr, revision_t *tr, gdImagePtr im, int x1, int y1, int x2, int y2)
3199 {
3200 char *href = expand_string(conf.map_merge_href, rcs, tr, tr->rev, fr->rev, NULL);
3201 char *alt = expand_string(conf.map_merge_alt, rcs, tr, tr->rev, fr->rev, NULL);
3202 const char *htp = conf.html_level == HTMLLEVEL_X ? " /" : "";
3203
3204 if(x1 > 0 && x2 > 0 && y1 > 0 && y2 > 0)
3205 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3206 href, x1, y1, x2, y2, alt, htp);
3207 xfree(alt);
3208 xfree(href);
3209
3210 if(im)
3211 {
3212 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL)->id);
3213 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color.id", NULL, NULL)->id);
3214 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL)->id);
3215 }
3216 }
3217
3218 static void map_merges(rcsfile_t *rcs, FILE *fp, gdImagePtr im)
3219 {
3220 int i;
3221 int tagh2 = get_sheight("Hg", &conf.tag_font) / 2;
3222 int bm = conf.branch_margin / 2;
3223
3224 for(i = 0; i < rcs->nmerges; i++)
3225 {
3226 revision_t *fr;
3227 revision_t *tr;
3228 int x1, x2, y1, y2;
3229 switch(rcs->merges[i].type)
3230 {
3231 case TR_TAG:
3232 fr = rcs->merges[i].from.tag->logrev;
3233 tr = rcs->merges[i].to.tag->logrev;
3234 break;
3235 case TR_REVISION:
3236 fr = rcs->merges[i].from.rev;
3237 tr = rcs->merges[i].to.rev;
3238 break;
3239 default:
3240 continue;
3241 }
3242 if(!fr || !tr || fr == tr)
3243 continue; /* This can happen with detached tags and self-references */
3244 if(conf.left_right)
3245 {
3246 if(fr->branch == tr->branch)
3247 {
3248 y1 = fr->y - fr->h/2;
3249 y2 = tr->y - tr->h/2;
3250 }
3251 else
3252 {
3253 if(fr->y < tr->y)
3254 {
3255 y1 = fr->y + fr->h/2;
3256 y2 = tr->y - tr->h/2;
3257 }
3258 else
3259 {
3260 y1 = fr->y - fr->h/2;
3261 y2 = tr->y + tr->h/2;
3262 }
3263 }
3264 x1 = fr->cx + fr->w/2;
3265 x2 = tr->cx + tr->w/2;
3266 }
3267 else
3268 {
3269 if(fr->branch == tr->branch)
3270 {
3271 x1 = fr->cx - fr->w/2;
3272 x2 = tr->cx - tr->w/2;
3273 }
3274 else
3275 {
3276 if(fr->cx < tr->cx)
3277 {
3278 x1 = fr->cx + fr->w/2;
3279 x2 = tr->cx - tr->w/2;
3280 }
3281 else
3282 {
3283 x1 = fr->cx - fr->w/2;
3284 x2 = tr->cx + tr->w/2;
3285 }
3286 }
3287 if(rcs->merges[i].type == TR_TAG)
3288 {
3289 y1 = fr->y + rcs->merges[i].from.tag->yofs;
3290 y2 = tr->y + rcs->merges[i].to.tag->yofs;
3291 }
3292 else
3293 {
3294 y1 = fr->y + fr->h/2;
3295 y2 = tr->y + tr->h/2;
3296 }
3297 }
3298
3299 if(conf.left_right)
3300 {
3301 if(fr->branch == tr->branch)
3302 {
3303 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-bm, x1+bm, y1);
3304 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-bm, x2+bm, y2);
3305 }
3306 else
3307 {
3308 if(y1 > y2)
3309 {
3310 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-bm, x1+bm, y1);
3311 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2, x2+bm, y2+bm);
3312 }
3313 else
3314 {
3315 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1, x1+bm, y1+bm);
3316 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-bm, x2+bm, y2);
3317 }
3318 }
3319 }
3320 else
3321 {
3322 if(fr->branch == tr->branch)
3323 {
3324 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-tagh2, x1, y1+tagh2);
3325 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-tagh2, x2, y2+tagh2);
3326 }
3327 else
3328 {
3329 if(x1 > x2)
3330 {
3331 map_merge_box(rcs, fp, fr, tr, im, x1-bm, y1-tagh2, x1, y1+tagh2);
3332 map_merge_box(rcs, fp, fr, tr, im, x2, y2-tagh2, x2+bm, y2+tagh2);
3333 }
3334 else
3335 {
3336 map_merge_box(rcs, fp, fr, tr, im, x1, y1-tagh2, x1+bm, y1+tagh2);
3337 map_merge_box(rcs, fp, fr, tr, im, x2-bm, y2-tagh2, x2, y2+tagh2);
3338 }
3339 }
3340 }
3341 }
3342 }
3343
3344 static void make_imagemap(rcsfile_t *rcs, FILE *fp, gdImagePtr im)
3345 {
3346 int i, j;
3347 const char *htp = conf.html_level == HTMLLEVEL_X ? " /" : "";
3348
3349 switch(conf.html_level)
3350 {
3351 case HTMLLEVEL_4:
3352 fprintf(fp, "<map name=\"%s\" id=\"%s\">\n", conf.map_name, conf.map_name);
3353 break;
3354 case HTMLLEVEL_X:
3355 fprintf(fp, "<map id=\"%s\">\n", conf.map_name);
3356 break;
3357 default:
3358 fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
3359 }
3360
3361 for(i = 0; i < rcs->nbranches; i++)
3362 {
3363 branch_t *b = rcs->branches[i];
3364 tag_t *tag = b->ntags ? b->tags[0] : NULL;
3365 char *bhref;
3366 char *balt;
3367 int x1;
3368 int x2;
3369 int y1;
3370 int y2;
3371
3372 if(subtree_branch && !b->subtree_draw)
3373 continue;
3374
3375 bhref = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
3376 balt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
3377
3378 if(!b->nfolds)
3379 {
3380 if(conf.left_right)
3381 {
3382 x1 = b->cx;
3383 y1 = b->y - b->h/2;
3384 x2 = b->cx + b->w;
3385 y2 = b->y + b->h/2;
3386 }
3387 else
3388 {
3389 x1 = b->cx - b->w/2;
3390 y1 = b->y;
3391 x2 = b->cx + b->w/2;
3392 y2 = b->y + b->h;
3393 }
3394 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3395 bhref, x1, y1, x2, y2, balt, htp);
3396 if(im)
3397 {
3398 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL)->id);
3399 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color", NULL, NULL)->id);
3400 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL)->id);
3401 }
3402 }
3403 else
3404 {
3405 int yy1, yy2, yy;
3406 if(conf.left_right)
3407 {
3408 x1 = b->cx + conf.branch_lspace;
3409 y1 = b->y - b->h/2 + conf.branch_tspace;
3410 }
3411 else
3412 {
3413 x1 = b->cx - b->w/2 + conf.branch_lspace;
3414 y1 = b->y + conf.branch_tspace;
3415 }
3416 x2 = x1 + b->w - conf.branch_rspace;
3417
3418 yy1 = get_sheight(b->branch->branch, &conf.branch_font);
3419 yy2 = get_sheight(b->tags[0]->tag, &conf.branch_tag_font);
3420 yy = MAX(yy1, yy2);
3421 y2 = y1 + yy;
3422 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3423 bhref, x1, y1, x2, y2, balt, htp);
3424
3425 y1 += yy;
3426 y2 += yy;
3427 for(j = 0; j < b->nfolds; j++)
3428 {
3429 branch_t *fb = b->folds[j];
3430 tag_t *t = fb->tags[0];
3431 xfree(bhref);
3432 xfree(balt);
3433 bhref = expand_string(conf.map_branch_href, rcs, NULL, fb->branch, NULL, t);
3434 balt = expand_string(conf.map_branch_alt, rcs, NULL, fb->branch, NULL, t);
3435 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3436 bhref, x1, y1, x2, y2, balt, htp);
3437 yy1 = get_sheight(fb->branch->branch, &conf.branch_font);
3438 yy2 = get_sheight(fb->tags[0]->tag, &conf.branch_tag_font);
3439 yy = MAX(yy1, yy2);
3440 y1 += yy;
3441 y2 += yy;
3442 }
3443 }
3444
3445 for(j = 0; j < b->nrevs; j++)
3446 {
3447 revision_t *r = b->revs[j];
3448 revision_t* r1;
3449 int xoff = 1;
3450 int yoff = 1;
3451 char *href;
3452 char *alt;
3453
3454 tag = r->ntags ? r->tags[0] : NULL;
3455 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
3456 alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
3457 if(conf.left_right)
3458 {
3459 x1 = r->cx;
3460 y1 = r->y - r->h/2;
3461 x2 = r->cx + r->w;
3462 y2 = r->y + r->h/2;
3463 }
3464 else
3465 {
3466 x1 = r->cx - r->w/2;
3467 y1 = r->y;
3468 x2 = r->cx + r->w/2;
3469 y2 = r->y + r->h;
3470 }
3471 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3472 href, x1, y1, x2, y2, alt, htp);
3473 if(im)
3474 {
3475 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL)->id);
3476 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color", NULL, NULL)->id);
3477 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL)->id);
3478 }
3479 xfree(href);
3480 xfree(alt);
3481 if(j > 0 || b->branchpoint)
3482 {
3483 if(j > 0)
3484 {
3485 r1 = b->revs[j-1];
3486 if(conf.left_right)
3487 {
3488 yoff = MIN(r->h, r1->h)/4;
3489 x1 = conf.upside_down ? r1->cx : r1->cx + r1->w;
3490 }
3491 else
3492 {
3493 xoff = MIN(r->w, r1->w)/4;
3494 y1 = conf.upside_down ? r1->y : r1->y + r1->h;
3495 }
3496 }
3497 else
3498 {
3499 r1 = b->branchpoint;
3500 if(conf.left_right)
3501 {
3502 yoff = MIN(r->h, b->h)/4;
3503 x1 = conf.upside_down ? b->cx : b->cx + b->w;
3504 }
3505 else
3506 {
3507 xoff = MIN(r->w, b->w)/4;
3508 y1 = conf.upside_down ? b->y : b->y + b->h;
3509 }
3510 }
3511 if(conf.left_right)
3512 {
3513 y1 = r->y - yoff;
3514 y2 = r->y + yoff;
3515 x2 = conf.upside_down ? r->cx + r->w : r->cx;
3516 yoff = 0;
3517 }
3518 else
3519 {
3520 x1 = r->cx - xoff;
3521 x2 = r->cx + xoff;
3522 y2 = conf.upside_down ? r->y + r->h : r->y;
3523 xoff = 0;
3524 }
3525 if(x1 > x2)
3526 {
3527 int tt = x1;
3528 x1 = x2;
3529 x2 = tt;
3530 }
3531 if(y1 > y2)
3532 {
3533 int tt = y1;
3534 y1 = y2;
3535 y2 = tt;
3536 }
3537 href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
3538 alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
3539 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3540 href,
3541 x1+xoff, y1+yoff, x2-xoff, y2-yoff,
3542 alt, htp);
3543 if(im)
3544 {
3545 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL)->id);
3546 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color", NULL, NULL)->id);
3547 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL)->id);
3548 }
3549 xfree(href);
3550 xfree(alt);
3551 }
3552 }
3553 if(conf.branch_dupbox && b->nrevs)
3554 {
3555 if(conf.left_right)
3556 {
3557 x1 = conf.upside_down ? b->cx + b->w - b->tw : b->cx - b->w + b->tw;
3558 y1 = b->y - b->h/2;
3559 x2 = x1 + b->w;
3560 y2 = b->y + b->h/2;
3561 }
3562 else
3563 {
3564 x1 = b->cx - b->w/2;
3565 y1 = conf.upside_down ? b->y + b->h - b->th : b->y - b->h + b->th;
3566 x2 = b->cx + b->w/2;
3567 y2 = y1 + b->h;
3568 }
3569 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s%s>\n",
3570 bhref, x1, y1, x2, y2, balt, htp);
3571 if(im)
3572 {
3573 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, clr(im, "title_color", NULL, NULL)->id);
3574 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, clr(im, "tag_color", NULL, NULL)->id);
3575 gdImageLine(im, x1, y1, x2, y2, clr(im, "title_color", NULL, NULL)->id);
3576 }
3577 }
3578 xfree(bhref);
3579 xfree(balt);
3580 }
3581
3582 map_merges(rcs, fp, im);
3583
3584 fprintf(fp, "</map>\n");
3585 }
3586
3587 /*
3588 **************************************************************************
3589 * Program entry
3590 **************************************************************************
3591 */
3592 static const char usage_str[] =
3593 "Usage: cvsgraph [options] <file>\n"
3594 " -b Add a branch box at both sides of the trunk (config value is negated)\n"
3595 " -c <file> Read alternative config from <file>\n"
3596 " -d <level> Enable debug mode at <level>\n"
3597 " -h This message\n"
3598 " -i Generate an imagemap instead of image\n"
3599 " -I <file> Also write the imagemap to <file>\n"
3600 " -k Auto stretch the tree (config value is negated)\n"
3601 " -M <name> Use <name> as imagemap name\n"
3602 " -m <mod> Use <mod> as cvs module\n"
3603 " -o <file> Output to <file>\n"
3604 " -O <opt=val> Set option opt to value val\n"
3605 " -q Be quiet (i.e. no warnings)\n"
3606 " -r <path> Use <path> as cvsroot path\n"
3607 " -s Strip untagged revisions (config value is negated)\n"
3608 " -S Also strip the first revision (config value is negated)\n"
3609 " -u Upside down image (mirror vertically; config value is negated)\n"
3610 " -V Print version and exit\n"
3611 " -x [34x] Specify level of HTML 3.2 (default), 4.0 or XHTML\n"
3612 " -[0-9] <txt> Use <txt> for expansion\n"
3613 ;
3614
3615 #define VERSION_STR "1.5.2"
3616 #define NOTICE_STR "Copyright (c) 2001,2002,2003,2004 B.Stultiens"
3617
3618 static void append_slash(char **path)
3619 {
3620 int l;
3621 assert(path != NULL);
3622 assert(*path != NULL);
3623 l = strlen(*path);
3624 if(!l || (*path)[l-1] == '/')
3625 return;
3626 *path = xrealloc(*path, l+2);
3627 strcat(*path, "/");
3628 }
3629
3630 int main(int argc, char *argv[])
3631 {
3632 extern int rcs_flex_debug;
3633 extern int rcsdebug;
3634 int optc;
3635 char *confpath = NULL;
3636 char *outfile = NULL;
3637 char *cvsroot = NULL;
3638 char *cvsmodule = NULL;
3639 int imagemap = 0;
3640 int upsidedown = 0;
3641 int bdupbox = 0;
3642 int stripuntag = 0;
3643 int stripfirst = 0;
3644 int autostretch = 0;
3645 int htmllevel = 0;
3646 char *imgmapname = NULL;
3647 char *imgmapfile = NULL;
3648 int lose = 0;
3649 FILE *fp;
3650 char *rcsfilename;
3651 rcsfile_t *rcs;
3652 gdImagePtr im;
3653
3654 while((optc = getopt(argc, argv, "0:1:2:3:4:5:6:7:8:9:bc:d:hI:ikM:m:O:o:qr:SsuVx:")) != EOF)
3655 {
3656 switch(optc)
3657 {
3658 case 'b':
3659 bdupbox = 1;
3660 break;
3661 case 'c':
3662 confpath = xstrdup(optarg);
3663 break;
3664 case 'd':
3665 debuglevel = strtol(optarg, NULL, 0);
3666 break;
3667 case 'I':
3668 imgmapfile = xstrdup(optarg);
3669 break;
3670 case 'i':
3671 imagemap = 1;
3672 break;
3673 case 'k':
3674 autostretch = 1;
3675 break;
3676 case 'M':
3677 imgmapname = xstrdup(optarg);
3678 break;
3679 case 'm':
3680 cvsmodule = xstrdup(optarg);
3681 break;
3682 case 'O':
3683 stack_option(optarg);
3684 break;
3685 case 'o':
3686 outfile = xstrdup(optarg);
3687 break;
3688 case 'q':
3689 quiet = 1;
3690 break;
3691 case 'r':
3692 cvsroot = xstrdup(optarg);
3693 break;
3694 case 'S':
3695 stripfirst = 1;
3696 break;
3697 case 's':
3698 stripuntag = 1;
3699 break;
3700 case 'u':
3701 upsidedown = 1;
3702 break;
3703 case 'V':
3704 fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
3705 return 0;
3706 case 'x':
3707 switch(optarg[0])
3708 {
3709 case '3':
3710 htmllevel = HTMLLEVEL_3;
3711 break;
3712 case '4':
3713 htmllevel = HTMLLEVEL_4;
3714 break;
3715 case 'x':
3716 htmllevel = HTMLLEVEL_X;
3717 break;
3718 default:
3719 fprintf(stderr, "Invalid HTML level in -x\n");
3720 lose++;
3721 }
3722 break;
3723 case 'h':
3724 fprintf(stdout, "%s", usage_str);
3725 return 0;
3726 default:
3727 if(isdigit(optc))
3728 {
3729 conf.expand[optc-'0'] = xstrdup(optarg);
3730 }
3731 else
3732 lose++;
3733 }
3734 }
3735
3736 if(lose)
3737 {
3738 fprintf(stderr, "%s", usage_str);
3739 return 1;
3740 }
3741
3742 if(debuglevel)
3743 {
3744 setvbuf(stdout, NULL, 0, _IONBF);
3745 setvbuf(stderr, NULL, 0, _IONBF);
3746 }
3747 rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
3748 rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
3749
3750 /* Set defaults */
3751 conf.tag_font.gdfont = gdFontTiny;
3752 conf.rev_font.gdfont = gdFontTiny;
3753 conf.branch_font.gdfont = gdFontTiny;
3754 conf.branch_tag_font.gdfont = gdFontTiny;
3755 conf.title_font.gdfont = gdFontTiny;
3756 conf.rev_text_font.gdfont = gdFontTiny;
3757 conf.msg_font.gdfont = gdFontTiny;
3758
3759 conf.anti_alias = 1;
3760 conf.thick_lines = 1;
3761 conf.branch_fold = 1;
3762
3763 conf.cvsroot = xstrdup("");
3764 conf.cvsmodule = xstrdup("");
3765 conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
3766 conf.title = xstrdup("");
3767 conf.map_name = xstrdup("CvsGraphImageMap");
3768 conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
3769 conf.map_branch_alt = xstrdup("alt=\"%B\"");
3770 conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
3771 conf.map_rev_alt = xstrdup("alt=\"%R\"");
3772 conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
3773 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
3774 conf.map_merge_href = xstrdup("href=\"unset: conf.map_merge_href\"");
3775 conf.map_merge_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
3776 conf.rev_text = xstrdup("%d");
3777 conf.branch_subtree = xstrdup("");
3778 conf.tag_ignore = xstrdup("");
3779 conf.merge_from = xstrdup("");
3780 conf.merge_to = xstrdup("");
3781 conf.merge_arrows = 1;
3782 conf.arrow_width = ARROW_WIDTH;
3783 conf.arrow_length = ARROW_LENGTH;
3784
3785 conf.color_bg = white_color;
3786 conf.branch_bgcolor = white_color;
3787 conf.branch_color = black_color;
3788 conf.branch_tag_color = black_color;
3789 conf.rev_color = black_color;
3790 conf.rev_bgcolor = white_color;
3791 conf.merge_color = black_color;
3792 conf.merge_cvsnt_color = black_color;
3793 conf.tag_color = black_color;
3794 conf.title_color = black_color;
3795 conf.rev_text_color = black_color;
3796 conf.msg_color = black_color;
3797
3798 conf.image_quality = 100;
3799 conf.image_compress = -1; /* Use default zlib setting */
3800 conf.rev_maxline = -1; /* Checked later to set to default */
3801
3802 read_config(confpath);
3803
3804 if(conf.rev_maxline == -1) conf.rev_maxline = 5 * conf.rev_minline;
3805
3806 /* Set overrides */
3807 if(cvsroot) conf.cvsroot = cvsroot;
3808 if(cvsmodule) conf.cvsmodule = cvsmodule;
3809 if(imgmapname) conf.map_name = imgmapname;
3810 if(upsidedown) conf.upside_down = !conf.upside_down;
3811 if(bdupbox) conf.branch_dupbox = !conf.branch_dupbox;
3812 if(stripuntag) conf.strip_untagged = !conf.strip_untagged;
3813 if(stripfirst) conf.strip_first_rev = !conf.strip_first_rev;
3814 if(autostretch) conf.auto_stretch = !conf.auto_stretch;
3815 if(htmllevel) conf.html_level = htmllevel;
3816
3817 if(conf.rev_minline >= conf.rev_maxline)
3818 {
3819 if(conf.auto_stretch)
3820 stack_msg(MSG_WARN, "Auto stretch is only possible if rev_minline < rev_maxline");
3821 conf.auto_stretch = 0;
3822 }
3823
3824 if(conf.thick_lines < 1)
3825 conf.thick_lines = 1;
3826 if(conf.thick_lines > 11)
3827 conf.thick_lines = 11;
3828
3829 if(conf.image_quality < 0 || conf.image_quality > 100)
3830 {
3831 stack_msg(MSG_WARN, "JPEG quality (image_quality) must be between 0 and 100");
3832 conf.image_quality = 100;
3833 }
3834
3835 if(conf.image_compress < -1 || conf.image_compress > 9)
3836 {
3837 stack_msg(MSG_WARN, "PNG compression (image_compress) must be between -1 and 9");
3838 conf.image_compress = -1;
3839 }
3840
3841 append_slash(&conf.cvsroot);
3842 append_slash(&conf.cvsmodule);
3843
3844 if(optind >= argc)
3845 {
3846 #ifdef __WIN32__
3847 /* Bad hack for DOS/Windows */
3848 if(setmode(fileno(stdin), O_BINARY) == -1)
3849 {
3850 perror("Set binary mode for stdin");
3851 return 1;
3852 }
3853 #endif
3854 rcsfilename = NULL;
3855 }
3856 else
3857 rcsfilename = argv[optind];
3858
3859 rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, rcsfilename);
3860 if(!rcs)
3861 return 1;
3862
3863 if(debuglevel & DEBUG_RCS_FILE)
3864 dump_rcsfile(rcs);
3865
3866 if(!reorganise_branches(rcs))
3867 return 1;
3868
3869 assign_tags(rcs);
3870 find_merges(rcs);
3871 find_merges_cvsnt(rcs);
3872
3873 if(outfile)
3874 {
3875 if((fp = fopen(outfile, "wb")) == NULL)
3876 {
3877 perror(outfile);
3878 return 1;
3879 }
3880 }
3881 else
3882 {
3883 fp = stdout;
3884 #ifdef __WIN32__
3885 /* Bad hack for DOS/Windows */
3886 if(setmode(fileno(fp), O_BINARY) == -1)
3887 {
3888 perror("Set binary mode for stdout");
3889 return 1;
3890 }
3891 #endif
3892 }
3893
3894 make_layout(rcs);
3895
3896 if(!imagemap)
3897 {
3898 /* Create an image */
3899 im = make_image(rcs);
3900
3901 if(conf.image_interlace)
3902 gdImageInterlace(im, 1);
3903
3904 #ifdef DEBUG_IMAGEMAP
3905 {
3906 FILE *nulfile = fopen("/dev/null", "w");
3907 make_imagemap(rcs, nulfile, im);
3908 fclose(nulfile);
3909 }
3910 #endif
3911 switch(conf.image_type)
3912 {
3913 #ifdef HAVE_IMAGE_GIF
3914 # ifndef HAVE_IMAGE_PNG
3915 default:
3916 # endif
3917 case IMAGE_GIF:
3918 gdImageGif(im, fp);
3919 break;
3920 #endif
3921 #ifdef HAVE_IMAGE_PNG
3922 default:
3923 case IMAGE_PNG:
3924 #ifdef HAVE_GDIMAGEPNGEX
3925 gdImagePngEx(im, fp, conf.image_compress);
3926 #else
3927 gdImagePng(im, fp);
3928 #endif
3929 break;
3930 #endif
3931 #ifdef HAVE_IMAGE_JPEG
3932 # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
3933 default:
3934 # endif
3935 case IMAGE_JPEG:
3936 gdImageJpeg(im, fp, conf.image_quality);
3937 break;
3938 #endif
3939 }
3940
3941 gdImageDestroy(im);
3942 }
3943 else
3944 {
3945 /* Create an imagemap */
3946 make_imagemap(rcs, fp, NULL);
3947 }
3948
3949 /* Also create imagemap to file if requested */
3950 if(imgmapfile)
3951 {
3952 FILE *ifp = fopen(imgmapfile, "wb");
3953 if(!ifp)
3954 {
3955 perror(imgmapfile);
3956 return 1;
3957 }
3958 make_imagemap(rcs, ifp, NULL);
3959 fclose(ifp);
3960 }
3961
3962 if(outfile)
3963 fclose(fp);
3964
3965 return 0;
3966 }

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0