/[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.26 - (show annotations)
Thu Jul 18 23:08:50 2002 UTC (15 years, 4 months ago) by bertho
Branch: MAIN
Changes since 1.25: +149 -46 lines
File MIME type: text/plain
Finished implementing left_right drawing.
1 /*
2 * CvsGraph graphical representation generator of brances and revisions
3 * of a file in cvs/rcs.
4 *
5 * Copyright (C) 2001,2002 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 <unistd.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <fcntl.h>
33 #include <regex.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <time.h>
37 #include <limits.h>
38
39 #ifdef HAVE_GETOPT_H
40 # include <getopt.h>
41 #endif
42
43 #include <gd.h>
44 #include <gdfontt.h>
45
46 #include "cvsgraph.h"
47 #include "utils.h"
48 #include "readconf.h"
49 #include "rcs.h"
50
51 #if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG) && !defined(HAVE_IMAGE_JPEG)
52 # error No image output format available. Check libgd
53 #endif
54
55
56 /*#define DEBUG 1*/
57 /*#define NOGDFILL 1*/
58 /*#define DEBUG_IMAGEMAP 1*/
59
60 #define LOOPSAFEGUARD 100 /* Max itterations in possible infinite loops */
61
62 #ifndef MAX
63 # define MAX(a,b) ((a) > (b) ? (a) : (b))
64 #endif
65
66 #ifndef MIN
67 # define MIN(a,b) ((a) < (b) ? (a) : (b))
68 #endif
69
70 #define ALIGN_HL 0x00
71 #define ALIGN_HC 0x01
72 #define ALIGN_HR 0x02
73 #define ALIGN_HX 0x0f
74 #define ALIGN_VT 0x00
75 #define ALIGN_VC 0x10
76 #define ALIGN_VB 0x20
77 #define ALIGN_VX 0xf0
78
79 /*
80 **************************************************************************
81 * Globals
82 **************************************************************************
83 */
84
85 config_t conf;
86 int debuglevel;
87 color_t white_color = {255, 255, 255, 0};
88 color_t black_color = {0, 0, 0, 0};
89
90
91 /*
92 **************************************************************************
93 * Debug routines
94 **************************************************************************
95 */
96 static void dump_rev(char *p, rev_t *r)
97 {
98 printf("%s", p);
99 if(r)
100 printf("'%s', '%s', %d\n", r->rev, r->branch, r->isbranch);
101 else
102 printf("<null>\n");
103 }
104
105 static void dump_id(char *p, char *d)
106 {
107 printf("%s", p);
108 if(d)
109 printf("'%s'\n", d);
110 else
111 printf("<null>\n");
112 }
113
114 static void dump_idrev(char *p, idrev_t *t)
115 {
116 printf("%s", p);
117 if(t)
118 {
119 printf("'%s' -> ", t->id);
120 dump_rev("", t->rev);
121 }
122 else
123 printf("<null>\n");
124 }
125
126 static void dump_tag(char *p, tag_t *t)
127 {
128 printf("%s", p);
129 if(t)
130 {
131 printf("'%s' -> ", t->tag);
132 dump_rev("", t->rev);
133 }
134 else
135 printf("<null>\n");
136 }
137
138 static void dump_delta(char *p, delta_t *d)
139 {
140 int i;
141 printf("%sdelta.rev : ", p);
142 dump_rev("", d->rev);
143 printf("%sdelta.date : %s\n", p, d->date);
144 printf("%sdelta.author: %s\n", p, d->author);
145 printf("%sdelta.state : %s\n", p, d->state);
146 for(i = 0; d->branches && i < d->branches->nrevs; i++)
147 {
148 printf("%sdelta.branch: ", p);
149 dump_rev("", d->branches->revs[i]);
150 }
151 printf("%sdelta.next : ", p);
152 dump_rev("", d->next);
153 printf("\n");
154 }
155
156 static void dump_dtext(char *p, dtext_t *d)
157 {
158 printf("%sdtext.rev : ", p);
159 dump_rev("", d->rev);
160 printf("%sdtext.log : %d bytes\n", p, d->log ? strlen(d->log) : -1);
161 printf("%sdtext.text : %d bytes\n", p, d->text ? strlen(d->text) : -1);
162 printf("\n");
163 }
164
165 static void dump_rcsfile(rcsfile_t *rcs)
166 {
167 int i;
168 printf("root : '%s'\n", rcs->root);
169 printf("module : '%s'\n", rcs->module);
170 printf("file : '%s'\n", rcs->file);
171 dump_rev("head : ", rcs->head);
172 dump_rev("branch : ", rcs->branch);
173 printf("access :\n");
174 for(i = 0; rcs->access && i < rcs->access->nids; i++)
175 dump_id("\t", rcs->access->ids[i]);
176 printf("tags :\n");
177 for(i = 0; rcs->tags && i < rcs->tags->ntags; i++)
178 dump_tag("\t", rcs->tags->tags[i]);
179 printf("locks :%s\n", rcs->strict ? " (strict)" : "");
180 for(i = 0; rcs->locks && i < rcs->locks->nidrevs; i++)
181 dump_idrev("\t", rcs->locks->idrevs[i]);
182 printf("comment: '%s'\n", rcs->comment);
183 printf("expand : '%s'\n", rcs->expand ? rcs->expand : "(default -kv)");
184 printf("deltas :\n");
185 for(i = 0; rcs->deltas && i < rcs->deltas->ndeltas; i++)
186 dump_delta("\t", rcs->deltas->deltas[i]);
187 printf("desc : '%s'\n", rcs->desc);
188 printf("dtexts :\n");
189 for(i = 0; rcs->dtexts && i < rcs->dtexts->ndtexts; i++)
190 dump_dtext("\t", rcs->dtexts->dtexts[i]);
191
192 fflush(stdout);
193 }
194
195 /*
196 **************************************************************************
197 * Read the rcs file
198 **************************************************************************
199 */
200 rcsfile_t *get_rcsfile(const char *cvsroot, const char *module, const char *file)
201 {
202 char *cmd = NULL;
203 int rv;
204
205 if(file)
206 {
207 cmd = xmalloc(strlen(cvsroot) + strlen(module) + strlen(file) + 1);
208 sprintf(cmd, "%s%s%s", cvsroot, module, file);
209 if(!(rcsin = fopen(cmd, "rb")))
210 {
211 perror(cmd);
212 return NULL;
213 }
214 input_file = cmd;
215 }
216 else
217 {
218 rcsin = stdin;
219 input_file = "<stdin>";
220 }
221 line_number = 1;
222 rv = rcsparse();
223 if(file)
224 {
225 fclose(rcsin);
226 xfree(cmd);
227 }
228 if(rv)
229 return NULL;
230 input_file = NULL;
231 if(file)
232 {
233 rcsfile->root = xstrdup(cvsroot);
234 rcsfile->module = xstrdup(module);
235 rcsfile->file = xstrdup(file);
236 }
237 else
238 {
239 rcsfile->root = xstrdup("");
240 rcsfile->module = xstrdup("");
241 rcsfile->file = xstrdup("<stdin>");
242 }
243 return rcsfile;
244 }
245
246 /*
247 **************************************************************************
248 * Sort and find helpers
249 **************************************************************************
250 */
251 int count_dots(const char *s)
252 {
253 int i;
254 for(i = 0; *s; s++)
255 {
256 if(*s == '.')
257 i++;
258 }
259 return i;
260 }
261
262 int compare_rev(int bcmp, const rev_t *r1, const rev_t *r2)
263 {
264 int d1, d2;
265 char *c1, *c2;
266 char *v1, *v2;
267 char *s1, *s2;
268 int retval = 0;
269 assert(r1 != NULL);
270 assert(r2 != NULL);
271 if(bcmp)
272 {
273 assert(r1->branch != NULL);
274 assert(r2->branch != NULL);
275 c1 = r1->branch;
276 c2 = r2->branch;
277 }
278 else
279 {
280 assert(r1->rev != NULL);
281 assert(r2->rev != NULL);
282 c1 = r1->rev;
283 c2 = r2->rev;
284 }
285
286 d1 = count_dots(c1);
287 d2 = count_dots(c2);
288 if(d1 != d2)
289 {
290 return d1 - d2;
291 }
292
293 s1 = v1 = xstrdup(c1);
294 s2 = v2 = xstrdup(c2);
295 while(1)
296 {
297 char *vc1 = strchr(s1, '.');
298 char *vc2 = strchr(s2, '.');
299 if(vc1 && vc2)
300 *vc1 = *vc2 = '\0';
301 if(*s1 && *s2)
302 {
303 d1 = atoi(s1);
304 d2 = atoi(s2);
305 if(d1 != d2)
306 {
307 retval = d1 - d2;
308 break;
309 }
310 }
311 if(!vc1 || !vc2)
312 break;
313 s1 = vc1 + 1;
314 s2 = vc2 + 1;
315 }
316 xfree(v1);
317 xfree(v2);
318 return retval;
319 }
320
321 /*
322 **************************************************************************
323 * Reorganise the rcsfile for the branches
324 *
325 * Basically, we have a list of deltas (i.e. administrative info on
326 * revisions) and a list of delta text (the actual logs and diffs).
327 * The deltas are linked through the 'next' and the 'branches' fields
328 * which describe the tree-structure of revisions.
329 * The reorganisation means that we put each delta and corresponding
330 * delta text in a revision structure and assign it to a specific
331 * branch. This is required because we want to be able to calculate
332 * the bounding boxes of each branch. The revisions expand vertically
333 * and the branches expand horizontally.
334 * The reorganisation is performed in these steps:
335 * 1 - sort deltas and delta text on revision number for quick lookup
336 * 2 - start at the denoted head revision:
337 * * create a branch structure and add this revision
338 * * for each 'branches' in the delta do:
339 * - walk all 'branches' of the delta and recursively goto 2
340 * with the denoted branch delta as new head
341 * - backlink the newly create sub-branch to the head revision
342 * so that we can draw them recursively
343 * * set head to the 'next' field and goto 2 until no next is
344 * available
345 * 3 - update the administration
346 **************************************************************************
347 */
348 static int sort_delta(const void *d1, const void *d2)
349 {
350 return compare_rev(0, (*(delta_t **)d1)->rev, (*(delta_t **)d2)->rev);
351 }
352
353 static int search_delta(const void *r, const void *d)
354 {
355 return compare_rev(0, (rev_t *)r, (*(delta_t **)d)->rev);
356 }
357
358 static delta_t *find_delta(delta_t **dl, int n, rev_t *r)
359 {
360 delta_t **d;
361 if(!n)
362 return NULL;
363 d = bsearch(r, dl, n, sizeof(*dl), search_delta);
364 if(!d)
365 return NULL;
366 return *d;
367 }
368
369 static int sort_dtext(const void *d1, const void *d2)
370 {
371 return compare_rev(0, (*(dtext_t **)d1)->rev, (*(dtext_t **)d2)->rev);
372 }
373
374 static int search_dtext(const void *r, const void *d)
375 {
376 return compare_rev(0, (rev_t *)r, (*(dtext_t **)d)->rev);
377 }
378
379 static dtext_t *find_dtext(dtext_t **dl, int n, rev_t *r)
380 {
381 dtext_t **d;
382 if(!n)
383 return NULL;
384 d = bsearch(r, dl, n, sizeof(*dl), search_dtext);
385 if(!d)
386 return NULL;
387 return *d;
388 }
389
390 static rev_t *dup_rev(const rev_t *r)
391 {
392 rev_t *t = xmalloc(sizeof(*t));
393 t->rev = xstrdup(r->rev);
394 t->branch = xstrdup(r->branch);
395 t->isbranch = r->isbranch;
396 return t;
397 }
398
399 static branch_t *new_branch(delta_t *d, dtext_t *t)
400 {
401 branch_t *b = xmalloc(sizeof(*b));
402 revision_t *r = xmalloc(sizeof(*r));
403 r->delta = d;
404 r->dtext = t;
405 r->rev = d->rev;
406 r->branch = b;
407 b->branch = dup_rev(d->rev);
408 b->branch->isbranch = 1;
409 b->nrevs = 1;
410 b->revs = xmalloc(sizeof(b->revs[0]));
411 b->revs[0] = r;
412 return b;
413 }
414
415 static revision_t *add_to_branch(branch_t *b, delta_t *d, dtext_t *t)
416 {
417 revision_t *r = xmalloc(sizeof(*r));
418 r->delta = d;
419 r->dtext = t;
420 r->rev = d->rev;
421 r->branch = b;
422 b->revs = xrealloc(b->revs, (b->nrevs+1) * sizeof(b->revs[0]));
423 b->revs[b->nrevs] = r;
424 b->nrevs++;
425 return r;
426 }
427
428 void build_branch(branch_t ***bl, int *nbl, delta_t **sdl, int nsdl, dtext_t **sdt, int nsdt, delta_t *head)
429 {
430 branch_t *b;
431 dtext_t *text;
432 revision_t *currev;
433
434 assert(head != NULL);
435
436 if(head->flag)
437 {
438 fprintf(stderr, "Circular reference on '%s' in branchpoint\n", head->rev->rev);
439 return;
440 }
441 head->flag++;
442 text = find_dtext(sdt, nsdt, head->rev);
443
444 /* Create a new branch for this head */
445 b = new_branch(head, text);
446 *bl = xrealloc(*bl, (*nbl+1)*sizeof((*bl)[0]));
447 (*bl)[*nbl] = b;
448 (*nbl)++;
449 currev = b->revs[0];
450 while(1)
451 {
452 /* Process all sub-branches */
453 if(head->branches)
454 {
455 int i;
456 for(i = 0; i < head->branches->nrevs; i++)
457 {
458 delta_t *d = find_delta(sdl, nsdl, head->branches->revs[i]);
459 int btag = *nbl;
460 if(!d)
461 continue;
462 build_branch(bl, nbl, sdl, nsdl, sdt, nsdt, d);
463
464 /* Set the new branch's origin */
465 (*bl)[btag]->branchpoint = currev;
466
467 /* Add branch to this revision */
468 currev->branches = xrealloc(currev->branches, (currev->nbranches+1)*sizeof(currev->branches[0]));
469 currev->branches[currev->nbranches] = (*bl)[btag];
470 currev->nbranches++;
471 }
472 }
473
474 /* Walk through the next list */
475 if(!head->next)
476 return;
477
478 head = find_delta(sdl, nsdl, head->next);
479 if(!head)
480 {
481 fprintf(stderr, "Next revision (%s) not found in deltalist\n", head->next->rev);
482 return;
483 }
484 if(head->flag)
485 {
486 fprintf(stderr, "Circular reference on '%s'\n", head->rev->rev);
487 return;
488 }
489 head->flag++;
490 text = find_dtext(sdt, nsdt, head->rev);
491 currev = add_to_branch(b, head, text);
492 }
493 }
494
495 int reorganise_branches(rcsfile_t *rcs)
496 {
497 delta_t **sdelta;
498 int nsdelta;
499 dtext_t **sdtext;
500 int nsdtext;
501 delta_t *head;
502 branch_t **bl;
503 int nbl;
504 int i;
505
506 assert(rcs->deltas != NULL);
507 assert(rcs->head != NULL);
508
509 /* Make a new list for quick lookup */
510 nsdelta = rcs->deltas->ndeltas;
511 sdelta = xmalloc(nsdelta * sizeof(sdelta[0]));
512 memcpy(sdelta, rcs->deltas->deltas, nsdelta * sizeof(sdelta[0]));
513 qsort(sdelta, nsdelta, sizeof(sdelta[0]), sort_delta);
514
515 /* Do the same for the delta text */
516 if(rcs->dtexts)
517 {
518 nsdtext = rcs->dtexts->ndtexts;
519 sdtext = xmalloc(nsdtext * sizeof(sdtext[0]));
520 memcpy(sdtext, rcs->dtexts->dtexts, nsdtext * sizeof(sdtext[0]));
521 qsort(sdtext, nsdtext, sizeof(sdtext[0]), sort_dtext);
522 }
523 else
524 {
525 nsdtext = 0;
526 sdtext = NULL;
527 }
528
529 /* Start from the head of the trunk */
530 head = find_delta(sdelta, nsdelta, rcs->head);
531 if(!head)
532 {
533 fprintf(stderr, "Head revision (%s) not found in deltalist\n", rcs->head->rev);
534 return 0;
535 }
536 bl = NULL;
537 nbl = 0;
538 build_branch(&bl, &nbl, sdelta, nsdelta, sdtext, nsdtext, head);
539
540 /* Reverse the head branch */
541 for(i = 0; i < bl[0]->nrevs/2; i++)
542 {
543 revision_t *r;
544 r = bl[0]->revs[i];
545 bl[0]->revs[i] = bl[0]->revs[bl[0]->nrevs-i-1];
546 bl[0]->revs[bl[0]->nrevs-i-1] = r;
547 }
548
549 /* Update the branch-number of the head because it was reversed */
550 xfree(bl[0]->branch->branch);
551 bl[0]->branch->branch = xstrdup(bl[0]->revs[0]->rev->branch);
552
553 /* Keep the admin */
554 rcs->branches = bl;
555 rcs->nbranches = nbl;
556 rcs->sdelta = sdelta;
557 rcs->nsdelta = nsdelta;
558 rcs->sdtext = sdtext;
559 rcs->nsdtext = nsdtext;
560 rcs->active = bl[0];
561 return 1;
562 }
563
564 /*
565 **************************************************************************
566 * Assign the symbolic tags to the revisions and branches
567 *
568 * The tags point to revision numbers somewhere in the tree structure
569 * of branches and revisions. First we make a sorted list of all
570 * revisions and then we assign each tag to the proper revision.
571 **************************************************************************
572 */
573 static int sort_revision(const void *r1, const void *r2)
574 {
575 return compare_rev(0, (*(revision_t **)r1)->delta->rev, (*(revision_t **)r2)->delta->rev);
576 }
577
578 static int search_revision(const void *t, const void *r)
579 {
580 return compare_rev(0, (rev_t *)t, (*(revision_t **)r)->delta->rev);
581 }
582
583 static int sort_branch(const void *b1, const void *b2)
584 {
585 return compare_rev(1, (*(branch_t **)b1)->branch, (*(branch_t **)b2)->branch);
586 }
587
588 static int search_branch(const void *t, const void *b)
589 {
590 return compare_rev(1, (rev_t *)t, (*(branch_t **)b)->branch);
591 }
592
593 static char *previous_rev(const char *c)
594 {
595 int dots = count_dots(c);
596 char *cptr;
597 char *r;
598 if(!dots)
599 {
600 fprintf(stderr, "FIXME: previous_rev(\"%s\"): Cannot determine parent branch revision\n", c);
601 return xstrdup("1.0"); /* FIXME: don't know what the parent is */
602 }
603 if(dots & 1)
604 {
605 /* Is is a revision we want the parent of */
606 r = xstrdup(c);
607 cptr = strrchr(r, '.');
608 assert(cptr != NULL);
609 if(dots == 1)
610 {
611 fprintf(stderr, "FIXME: previous_rev(\"%s\"): Going beyond top-level?\n", c);
612 /* FIXME: What is the parent of 1.1? */
613 cptr[1] = '\0';
614 strcat(r, "0");
615 return r;
616 }
617 /* Here we have a "x.x[.x.x]+" case */
618 *cptr = '\0';
619 cptr = strrchr(r, '.');
620 assert(cptr != NULL);
621 *cptr = '\0';
622 return r;
623 }
624 /* It is a branch we want the parent of */
625 r = xstrdup(c);
626 cptr = strrchr(r, '.');
627 assert(cptr != NULL);
628 *cptr = '\0';
629 return r;
630 }
631
632 int assign_tags(rcsfile_t *rcs)
633 {
634 int i;
635 int nr;
636
637 for(i = nr = 0; i < rcs->nbranches; i++)
638 nr += rcs->branches[i]->nrevs;
639
640 rcs->srev = xmalloc(nr * sizeof(rcs->srev[0]));
641 rcs->nsrev = nr;
642 for(i = nr = 0; i < rcs->nbranches; i++)
643 {
644 memcpy(&rcs->srev[nr], rcs->branches[i]->revs, rcs->branches[i]->nrevs * sizeof(rcs->branches[i]->revs[0]));
645 nr += rcs->branches[i]->nrevs;
646 }
647
648 qsort(rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), sort_revision);
649 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
650
651 if(!rcs->branch)
652 {
653 /* The main trunk is the active trunk */
654 rcs->tags->tags = xrealloc(rcs->tags->tags, (rcs->tags->ntags+1)*sizeof(rcs->tags->tags[0]));
655 rcs->tags->tags[rcs->tags->ntags] = xmalloc(sizeof(tag_t));
656 rcs->tags->tags[rcs->tags->ntags]->tag = xstrdup("MAIN");
657 rcs->tags->tags[rcs->tags->ntags]->rev = xmalloc(sizeof(rev_t));
658 rcs->tags->tags[rcs->tags->ntags]->rev->rev = xstrdup(rcs->active->branch->rev);
659 rcs->tags->tags[rcs->tags->ntags]->rev->branch = xstrdup(rcs->active->branch->branch);
660 rcs->tags->tags[rcs->tags->ntags]->rev->isbranch = 1;
661 rcs->tags->ntags++;
662 }
663
664 /* We should have at least two tags (HEAD and MAIN) */
665 assert(rcs->tags != 0);
666
667 for(i = 0; i < rcs->tags->ntags; i++)
668 {
669 tag_t *t = rcs->tags->tags[i];
670 if(t->rev->isbranch)
671 {
672 branch_t **b;
673 add_btag:
674 b = bsearch(t->rev, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
675 if(!b)
676 {
677 rev_t rev;
678 revision_t **r;
679 /* This happens for the magic branch numbers if there are
680 * no commits within the new branch yet. So, we add the
681 * branch and try to continue.
682 */
683 rev.rev = previous_rev(t->rev->branch);
684 rev.branch = NULL;
685 rev.isbranch = 0;
686 r = bsearch(&rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
687 xfree(rev.rev);
688 if(!r)
689 {
690 if(!quiet)
691 fprintf(stderr, "No branch found for tag '%s:%s'\n", t->tag, t->rev->branch);
692 }
693 else
694 {
695 rcs->branches = xrealloc(rcs->branches, (rcs->nbranches+1)*sizeof(rcs->branches[0]));
696 rcs->branches[rcs->nbranches] = xmalloc(sizeof(branch_t));
697 rcs->branches[rcs->nbranches]->branch = dup_rev(t->rev);
698 rcs->branches[rcs->nbranches]->branchpoint = *r;
699 (*r)->branches = xrealloc((*r)->branches, ((*r)->nbranches+1)*sizeof((*r)->branches[0]));
700 (*r)->branches[(*r)->nbranches] = rcs->branches[rcs->nbranches];
701 (*r)->nbranches++;
702 rcs->nbranches++;
703 /* Resort the branches */
704 qsort(rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), sort_branch);
705 goto add_btag;
706 }
707 }
708 else
709 {
710 branch_t *bb = *b;
711 bb->tags = xrealloc(bb->tags, (bb->ntags+1)*sizeof(bb->tags[0]));
712 bb->tags[bb->ntags] = t;
713 bb->ntags++;
714 }
715 }
716 else
717 {
718 revision_t **r = bsearch(t->rev, rcs->srev, rcs->nsrev, sizeof(rcs->srev[0]), search_revision);
719 if(!r)
720 {
721 if(!quiet)
722 fprintf(stderr, "No revision found for tag '%s:%s'\n", t->tag, t->rev->rev);
723 }
724 else
725 {
726 revision_t *rr = *r;
727 rr->tags = xrealloc(rr->tags, (rr->ntags+1)*sizeof(rr->tags[0]));
728 rr->tags[rr->ntags] = t;
729 rr->ntags++;
730 }
731 }
732 }
733
734 /* We need to reset the first in the list of branches to the
735 * active branch to ensure the drawing of all
736 */
737 if(rcs->active != rcs->branches[0])
738 {
739 branch_t **b = bsearch(rcs->active->branch, rcs->branches, rcs->nbranches, sizeof(rcs->branches[0]), search_branch);
740 branch_t *t;
741 assert(b != NULL);
742 t = *b;
743 *b = rcs->branches[0];
744 rcs->branches[0] = t;
745 }
746 return 1;
747 }
748
749 /*
750 **************************************************************************
751 * String expansion
752 **************************************************************************
753 */
754 static char *_string;
755 static int _nstring;
756 static int _nastring;
757
758 static void add_string_str(const char *s)
759 {
760 int l = strlen(s) + 1;
761 if(_nstring + l > _nastring)
762 {
763 _nastring += MAX(128, l);
764 _string = xrealloc(_string, _nastring * sizeof(_string[0]));
765 }
766 memcpy(_string+_nstring, s, l);
767 _nstring += l-1;
768 }
769
770 static void add_string_ch(int ch)
771 {
772 char buf[2];
773 buf[0] = ch;
774 buf[1] = '\0';
775 add_string_str(buf);
776 }
777
778 static void add_string_date(const char *d)
779 {
780 struct tm tm, *tmp;
781 int n;
782 time_t t;
783 char *buf;
784 int nbuf;
785
786 memset(&tm, 0, sizeof(tm));
787 n = sscanf(d, "%d.%d.%d.%d.%d.%d",
788 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
789 &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
790 tm.tm_mon--;
791 if(tm.tm_year > 1900)
792 tm.tm_year -= 1900;
793 t = mktime(&tm);
794 if(n != 6 || t == (time_t)(-1))
795 {
796 add_string_str("<invalid date>");
797 return;
798 }
799
800 tmp = localtime(&t);
801 nbuf = strlen(conf.date_format) * 16; /* Should be enough to hold all types of expansions */
802 buf = xmalloc(nbuf);
803 strftime(buf, nbuf, conf.date_format, tmp);
804 add_string_str(buf);
805 xfree(buf);
806 }
807
808 static void add_string_str_html(const char *s, int maxlen)
809 {
810 int l = 0;
811 char *str = xmalloc(6 * strlen(s) + 1); /* Should hold all char entity-expand */
812 char *cptr = str;
813 for(; *s; s++)
814 {
815 if(maxlen && l > abs(maxlen))
816 {
817 cptr += sprintf(cptr, "...");
818 break;
819 }
820 if(*s < 0x20)
821 {
822 if(*s == '\n')
823 {
824 if(maxlen < 0)
825 *cptr++ = ' ';
826 else
827 cptr += sprintf(cptr, "<br>");
828 }
829 }
830 else if(*s >= 0x7f)
831 cptr += sprintf(cptr, "&#%d;", (int)(unsigned char)*s);
832 else if(*s == '<')
833 cptr += sprintf(cptr, "&lt;");
834 else if(*s == '>')
835 cptr += sprintf(cptr, "&gt;");
836 else
837 *cptr++ = *s;
838 l++;
839 }
840 *cptr = '\0';
841 add_string_str(str);
842 xfree(str);
843 }
844
845 static void add_string_str_len(const char *s, int maxlen)
846 {
847 int l = strlen(s);
848 char *str = xmalloc(l + 1 + 3);
849 strcpy(str, s);
850 if(maxlen < l)
851 sprintf(&str[maxlen], "...");
852 add_string_str(str);
853 xfree(str);
854 }
855
856 char *expand_string(const char *s, rcsfile_t *rcs, revision_t *r, rev_t *rev, rev_t *prev, tag_t *tag)
857 {
858 char nb[32];
859 char nr[32];
860 char *base;
861 int l;
862 char ch;
863
864 if(!s)
865 return xstrdup("");
866
867 _nstring = 0;
868 if(_string)
869 _string[0] = '\0';
870
871 sprintf(nb, "%d", rcs->nbranches);
872 sprintf(nr, "%d", rcs->nsrev);
873 for(; *s; s++)
874 {
875 if(*s == '%')
876 {
877 switch(*++s)
878 {
879 case 'c':
880 case 'C':
881 add_string_str(conf.cvsroot);
882 if(*s == 'C' && conf.cvsroot[0] && conf.cvsroot[strlen(conf.cvsroot)-1] == '/')
883 {
884 /* Strip the trailing '/' */
885 _nstring--;
886 _string[_nstring] = '\0';
887 }
888 break;
889 case 'f':
890 case 'F':
891 base = strrchr(rcs->file, '/');
892 if(!base)
893 add_string_str(rcs->file);
894 else
895 add_string_str(base+1);
896 if(*s == 'F' && _string[_nstring-1] == 'v' && _string[_nstring-2] == ',')
897 {
898 _nstring -= 2;
899 _string[_nstring] = '\0';
900 }
901 break;
902 case 'p':
903 base = strrchr(rcs->file, '/');
904 if(base)
905 {
906 char *c = xstrdup(rcs->file);
907 base = strrchr(c, '/');
908 assert(base != NULL);
909 base[1] = '\0';
910 add_string_str(c);
911 xfree(c);
912 }
913 /*
914 * We should not add anything here because we can encounter
915 * a completely empty path, in which case we do not want
916 * to add any slash. This prevents an inadvertent root redirect.
917 *
918 * else
919 * add_string_str("/");
920 */
921 break;
922 case 'm':
923 case 'M':
924 add_string_str(conf.cvsmodule);
925 if(*s == 'M' && conf.cvsmodule[0] && conf.cvsmodule[strlen(conf.cvsmodule)-1] == '/')
926 {
927 /* Strip the trailing '/' */
928 _nstring--;
929 _string[_nstring] = '\0';
930 }
931 break;
932 case 'r': add_string_str(nr); break;
933 case 'b': add_string_str(nb); break;
934 case '%': add_string_ch('%'); break;
935 case '0': if(conf.expand[0]) add_string_str(conf.expand[0]); break;
936 case '1': if(conf.expand[1]) add_string_str(conf.expand[1]); break;
937 case '2': if(conf.expand[2]) add_string_str(conf.expand[2]); break;
938 case '3': if(conf.expand[3]) add_string_str(conf.expand[3]); break;
939 case '4': if(conf.expand[4]) add_string_str(conf.expand[4]); break;
940 case '5': if(conf.expand[5]) add_string_str(conf.expand[5]); break;
941 case '6': if(conf.expand[6]) add_string_str(conf.expand[6]); break;
942 case '7': if(conf.expand[7]) add_string_str(conf.expand[7]); break;
943 case '8': if(conf.expand[8]) add_string_str(conf.expand[8]); break;
944 case '9': if(conf.expand[9]) add_string_str(conf.expand[9]); break;
945 case 'R': if(rev && rev->rev) add_string_str(rev->rev); break;
946 case 'P': if(prev && prev->rev) add_string_str(prev->rev); break;
947 case 'B': if(rev && rev->branch) add_string_str(rev->branch); break;
948 case 't': if(tag && tag->tag) add_string_str(tag->tag); break;
949 case 'd': if(r && r->delta && r->delta->date) add_string_date(r->delta->date); break;
950 case 's': if(r && r->delta && r->delta->state) add_string_str(r->delta->state); break;
951 case 'a': if(r && r->delta && r->delta->author) add_string_str(r->delta->author); break;
952 case 'L':
953 case 'l':
954 ch = *s;
955 l = 0;
956 if(s[1] == '[')
957 {
958 char *cptr = strchr(s, ']');
959 char *eptr;
960 if(cptr)
961 {
962 l = strtol(&s[2], &eptr, 10);
963 if(eptr != cptr)
964 l = 0;
965 else
966 s = cptr;
967 }
968 }
969 if(!conf.parse_logs)
970 add_string_str("N/A");
971 else if(r && r->dtext && r->dtext->log)
972 {
973 if(ch == 'l')
974 add_string_str_html(r->dtext->log, l);
975 else
976 add_string_str_len(r->dtext->log, abs(l));
977 }
978 break;
979 default:
980 add_string_ch('%');
981 add_string_ch(*s);
982 break;
983 }
984 }
985 else
986 add_string_ch(*s);
987 }
988 return xstrdup(_string);
989 }
990
991 /*
992 **************************************************************************
993 * Drawing routines
994 **************************************************************************
995 */
996 static int get_swidth(const char *s, font_t *f)
997 {
998 int n;
999 int m;
1000 if(!s || !*s)
1001 return 0;
1002
1003 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1004 if(conf.use_ttf && f->ttfont)
1005 {
1006 int bb[8];
1007 char *e;
1008 #ifdef HAVE_GDIMAGESTRINGFT
1009 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1010 #else
1011 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1012 #endif
1013 if(!e)
1014 return bb[2] - bb[6];
1015 }
1016 #endif
1017 for(n = m = 0; *s; n++, s++)
1018 {
1019 if(*s == '\n')
1020 {
1021 if(n > m)
1022 m = n;
1023 n = 0;
1024 }
1025 }
1026 if(n > m)
1027 m = n;
1028 return f->gdfont ? m * f->gdfont->w : m;
1029 }
1030
1031 static int get_sheight(const char *s, font_t *f)
1032 {
1033 int nl;
1034 if(!s || !*s)
1035 return 0;
1036
1037 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1038 if(conf.use_ttf && f->ttfont)
1039 {
1040 int bb[8];
1041 char *e;
1042 #ifdef HAVE_GDIMAGESTRINGFT
1043 e = gdImageStringFT(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1044 #else
1045 e = gdImageStringTTF(NULL, bb, 0, f->ttfont, f->ttsize, 0.0, 0, 0, (char *)s);
1046 #endif
1047 if(!e)
1048 return bb[3] - bb[7] + 4;
1049 }
1050 #endif
1051 for(nl = 1; *s; s++)
1052 {
1053 if(*s == '\n' && s[1])
1054 nl++;
1055 }
1056 return nl * f->gdfont->h;
1057 }
1058
1059 static void draw_rbox(gdImagePtr im, int x1, int y1, int x2, int y2, int r, color_t *color, color_t *bgcolor)
1060 {
1061 int r2 = 2*r;
1062 gdImageLine(im, x1+r, y1, x2-r, y1, color->id);
1063 gdImageLine(im, x1+r, y2, x2-r, y2, color->id);
1064 gdImageLine(im, x1, y1+r, x1, y2-r, color->id);
1065 gdImageLine(im, x2, y1+r, x2, y2-r, color->id);
1066 if(conf.box_shadow)
1067 {
1068 gdImageLine(im, x1+r+1, y2+1, x2-r, y2+1, black_color.id);
1069 gdImageLine(im, x2+1, y1+r+1, x2+1, y2-r, black_color.id);
1070 }
1071 if(r)
1072 {
1073 /* FIXME: Pixelization is not perfect */
1074 gdImageArc(im, x1+r, y1+r, r2, r2, 180, 270, color->id);
1075 gdImageArc(im, x2-r, y1+r, r2, r2, 270, 360, color->id);
1076 gdImageArc(im, x1+r, y2-r, r2, r2, 90, 180, color->id);
1077 if(conf.box_shadow)
1078 {
1079 gdImageArc(im, x2-r+1, y2-r+1, r2, r2, 0, 90, black_color.id);
1080 gdImageArc(im, x2-r+1, y2-r, r2, r2, 0, 90, black_color.id);
1081 gdImageArc(im, x2-r, y2-r+1, r2, r2, 0, 90, black_color.id);
1082 }
1083 gdImageArc(im, x2-r, y2-r, r2, r2, 0, 90, color->id);
1084 }
1085 #ifndef NOGDFILL
1086 gdImageFillToBorder(im, (x1+x2)/2, (y1+y2)/2, color->id, bgcolor->id);
1087 #endif
1088 }
1089
1090 static void draw_string(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1091 {
1092 int h = get_sheight(s, f);
1093 int xx, yy;
1094 switch(align & ALIGN_HX)
1095 {
1096 default:
1097 case ALIGN_HL: xx = 0; break;
1098 case ALIGN_HC: xx = -get_swidth(s, f)/2; break;
1099 case ALIGN_HR: xx = -get_swidth(s, f); break;
1100 }
1101 switch(align & ALIGN_VX)
1102 {
1103 default:
1104 case ALIGN_VT: yy = 0; break;
1105 case ALIGN_VC: yy = h/2; break;
1106 case ALIGN_VB: yy = h; break;
1107 }
1108 #if defined(HAVE_GDIMAGESTRINGFT) || defined(HAVE_GDIMAGESTRINGTTF)
1109 if(conf.use_ttf && f->ttfont)
1110 {
1111 int bb[8];
1112 char *e;
1113 int cid = conf.anti_alias ? c->id : -c->id;
1114 #ifdef HAVE_GDIMAGESTRINGFT
1115 e = gdImageStringFT(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1116 #else
1117 e = gdImageStringTTF(im, bb, cid, f->ttfont, f->ttsize, 0.0, x+xx, y+yy+h-2, (char *)s);
1118 #endif
1119 if(!e)
1120 return;
1121 }
1122 #endif
1123 yy = -yy;
1124 gdImageString(im, f->gdfont, x+xx+1, y+yy, s, c->id);
1125 }
1126
1127 static void draw_stringnl(gdImagePtr im, char *s, font_t *f, int x, int y, int align, color_t *c)
1128 {
1129 char *t;
1130 char *d;
1131 d = s = xstrdup(s);
1132 do
1133 {
1134 t = strchr(s, '\n');
1135 if(t)
1136 *t = '\0';
1137 draw_string(im, s, f, x, y, align, c);
1138 y += get_sheight(s, f);
1139 s = t+1;
1140 } while(t);
1141 xfree(d);
1142 }
1143
1144 static void draw_rev(gdImagePtr im, revision_t *r)
1145 {
1146 int lx;
1147 int rx;
1148 int x2;
1149 int i;
1150 int ty;
1151
1152 if(conf.left_right)
1153 {
1154 lx = r->cx;
1155 rx = r->cx + r->w;
1156 ty = r->y - r->h/2;
1157 x2 = r->cx + r->w/2;
1158 }
1159 else
1160 {
1161 lx = r->cx - r->w/2;
1162 rx = lx + r->w;
1163 ty = r->y;
1164 x2 = r->cx;
1165 }
1166 draw_rbox(im, lx, ty, rx, ty+r->h, 0, &conf.rev_color, &conf.rev_bgcolor);
1167 ty += conf.rev_tspace;
1168 draw_string(im, r->rev->rev, &conf.rev_font, x2, ty, ALIGN_HC, &conf.rev_color);
1169 ty += get_sheight(r->rev->rev, &conf.rev_font);
1170 draw_stringnl(im, r->revtext, &conf.rev_text_font, x2, ty, ALIGN_HC, &conf.rev_text_color);
1171 ty += get_sheight(r->revtext, &conf.rev_text_font);
1172 for(i = 0; i < r->ntags; i++)
1173 {
1174 draw_string(im, r->tags[i]->tag, &conf.tag_font, x2, ty, ALIGN_HC, &conf.tag_color);
1175 ty += get_sheight(r->tags[i]->tag, &conf.tag_font) + conf.rev_separator;
1176 }
1177 }
1178
1179 static void draw_branch_box(gdImagePtr im, branch_t *b, int xp, int yp)
1180 {
1181 int lx;
1182 int rx;
1183 int i;
1184 int yy;
1185 int x2;
1186
1187 if(conf.left_right)
1188 {
1189 lx = b->cx;
1190 rx = lx + b->w;
1191 x2 = b->cx + b->w/2;
1192 }
1193 else
1194 {
1195 lx = b->cx - b->w/2;
1196 rx = lx + b->w;
1197 x2 = b->cx;
1198 }
1199 draw_rbox(im, lx+xp, yp, rx+xp, yp+b->h, 5, &conf.branch_color, &conf.branch_bgcolor);
1200 yy = conf.branch_tspace;
1201 draw_string(im, b->branch->branch, &conf.branch_font, x2+xp, yp+yy, ALIGN_HC, &conf.branch_color);
1202 yy += get_sheight(b->branch->branch, &conf.branch_font);
1203 for(i = 0; i < b->ntags; i++)
1204 {
1205 draw_string(im, b->tags[i]->tag, &conf.branch_tag_font, x2+xp, yp+yy, ALIGN_HC, &conf.branch_tag_color);
1206 yy += get_sheight(b->tags[i]->tag, &conf.branch_font);
1207 }
1208 }
1209
1210 static void draw_branch(gdImagePtr im, branch_t *b)
1211 {
1212 int yy, xx;
1213 int i;
1214 int line[4];
1215 int l;
1216 int sign;
1217
1218 line[0] = conf.rev_color.id;
1219 line[1] = gdTransparent;
1220 line[1] = gdTransparent;
1221 line[3] = conf.rev_color.id;
1222
1223 draw_branch_box(im, b, 0, conf.left_right ? b->y - b->h/2 : b->y);
1224
1225 if(conf.left_right)
1226 {
1227 if(conf.upside_down)
1228 {
1229 xx = b->cx;
1230 for(i = 0; i < b->nrevs; i++)
1231 {
1232 revision_t *r = b->revs[i];
1233 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1234 gdImageLine(im, xx, r->y, r->cx+r->w, r->y, gdStyled);
1235 for(sign = l = 1; l < conf.thick_lines; l++)
1236 {
1237 int pp = (l+1)/2*sign;
1238 gdImageLine(im, xx, r->y+pp, r->cx+r->w, r->y+pp, gdStyled);
1239 sign *= -1;
1240 }
1241 draw_rev(im, r);
1242 xx = r->cx;
1243 }
1244 if(conf.branch_dupbox)
1245 {
1246 i = b->cx - b->tw + b->w;
1247 gdImageLine(im, xx, b->y, i+b->w, b->y, conf.rev_color.id);
1248 for(sign = l = 1; l < conf.thick_lines; l++)
1249 {
1250 int pp = (l+1)/2*sign;
1251 gdImageLine(im, xx, b->y+pp, i+b->w, b->y+pp, conf.rev_color.id);
1252 sign *= -1;
1253 }
1254 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1255 }
1256 }
1257 else
1258 {
1259 xx = b->cx + b->w;
1260 for(i = 0; i < b->nrevs; i++)
1261 {
1262 revision_t *r = b->revs[i];
1263 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1264 gdImageLine(im, xx, r->y, r->cx, r->y, gdStyled);
1265 for(sign = l = 1; l < conf.thick_lines; l++)
1266 {
1267 int pp = (l+1)/2*sign;
1268 gdImageLine(im, xx, r->y+pp, r->cx, r->y+pp, gdStyled);
1269 sign *= -1;
1270 }
1271 draw_rev(im, r);
1272 xx = r->cx + r->w;
1273 }
1274 if(conf.branch_dupbox)
1275 {
1276 i = b->cx + b->tw - b->w;
1277 gdImageLine(im, xx, b->y, i, b->y, conf.rev_color.id);
1278 for(sign = l = 1; l < conf.thick_lines; l++)
1279 {
1280 int pp = (l+1)/2*sign;
1281 gdImageLine(im, xx, b->y+pp, i, b->y+pp, conf.rev_color.id);
1282 sign *= -1;
1283 }
1284 draw_branch_box(im, b, i - b->cx, b->y - b->h/2);
1285 }
1286 }
1287 }
1288 else
1289 {
1290 if(conf.upside_down)
1291 {
1292 yy = b->y;
1293 for(i = 0; i < b->nrevs; i++)
1294 {
1295 revision_t *r = b->revs[i];
1296 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1297 gdImageLine(im, r->cx, yy, r->cx, r->y+r->h, gdStyled);
1298 for(sign = l = 1; l < conf.thick_lines; l++)
1299 {
1300 int pp = (l+1)/2*sign;
1301 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y+r->h, gdStyled);
1302 sign *= -1;
1303 }
1304 draw_rev(im, r);
1305 yy = r->y;
1306 }
1307 if(conf.branch_dupbox)
1308 {
1309 i = b->y - b->th + b->h;
1310 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1311 for(sign = l = 1; l < conf.thick_lines; l++)
1312 {
1313 int pp = (l+1)/2*sign;
1314 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1315 sign *= -1;
1316 }
1317 draw_branch_box(im, b, 0, i);
1318 }
1319 }
1320 else
1321 {
1322 yy = b->y + b->h;
1323 for(i = 0; i < b->nrevs; i++)
1324 {
1325 revision_t *r = b->revs[i];
1326 gdImageSetStyle(im, line, r->stripped ? 4 : 1);
1327 gdImageLine(im, r->cx, yy, r->cx, r->y, gdStyled);
1328 for(sign = l = 1; l < conf.thick_lines; l++)
1329 {
1330 int pp = (l+1)/2*sign;
1331 gdImageLine(im, r->cx+pp, yy, r->cx+pp, r->y, gdStyled);
1332 sign *= -1;
1333 }
1334 draw_rev(im, r);
1335 yy = r->y + r->h;
1336 }
1337 if(conf.branch_dupbox)
1338 {
1339 i = b->y + b->th - b->h;
1340 gdImageLine(im, b->cx, yy, b->cx, i, conf.rev_color.id);
1341 for(sign = l = 1; l < conf.thick_lines; l++)
1342 {
1343 int pp = (l+1)/2*sign;
1344 gdImageLine(im, b->cx+pp, yy, b->cx+pp, i, conf.rev_color.id);
1345 sign *= -1;
1346 }
1347 draw_branch_box(im, b, 0, i);
1348 }
1349 }
1350 }
1351 }
1352
1353 static void draw_connector(gdImagePtr im, branch_t *b)
1354 {
1355 int l;
1356 int sign;
1357 revision_t *r = b->branchpoint;
1358 int x1 = r->cx + r->w/2 + 2;
1359 int y1 = r->y + r->h/2;
1360 int x2 = b->cx;
1361 int y2 = b->y;
1362
1363 if(conf.left_right)
1364 {
1365 x2 = r->cx + r->w/2;
1366 y2 = r->y + r->h/2 + 3;
1367 x1 = b->cx;
1368 y1 = b->y;
1369 if(conf.upside_down)
1370 x1 += b->w;
1371 }
1372 else
1373 {
1374 x1 = r->cx + r->w/2 + 2;
1375 y1 = r->y + r->h/2;
1376 x2 = b->cx;
1377 y2 = b->y;
1378 if(conf.upside_down)
1379 y2 += b->h;
1380 }
1381 gdImageLine(im, x1, y1, x2, y1, conf.branch_color.id);
1382 gdImageLine(im, x2, y1, x2, y2, conf.branch_color.id);
1383 for(sign = l = 1; l < conf.thick_lines; l++)
1384 {
1385 int pp = (l+1)/2*sign;
1386 gdImageLine(im, x1, y1+pp, x2, y1+pp, conf.branch_color.id);
1387 gdImageLine(im, x2+pp, y1, x2+pp, y2, conf.branch_color.id);
1388 sign *= -1;
1389 }
1390 }
1391
1392 static void alloc_color(gdImagePtr im, color_t *c)
1393 {
1394 c->id = gdImageColorAllocate(im, c->r, c->g, c->b);
1395 }
1396
1397 gdImagePtr make_image(rcsfile_t *rcs)
1398 {
1399 gdImagePtr im;
1400 int i;
1401 char *cptr;
1402
1403 cptr = expand_string(conf.title, rcs, NULL, NULL, NULL, NULL);
1404 i = get_swidth(cptr, &conf.title_font);
1405 if(rcs->tw+conf.margin_left+conf.margin_right > i)
1406 i = rcs->tw+conf.margin_left+conf.margin_right;
1407 im = gdImageCreate(i, rcs->th+conf.margin_top+conf.margin_bottom);
1408 alloc_color(im, &conf.color_bg);
1409 alloc_color(im, &conf.tag_color);
1410 alloc_color(im, &conf.rev_color);
1411 alloc_color(im, &conf.rev_bgcolor);
1412 alloc_color(im, &conf.rev_text_color);
1413 alloc_color(im, &conf.branch_color);
1414 alloc_color(im, &conf.branch_tag_color);
1415 alloc_color(im, &conf.branch_bgcolor);
1416 alloc_color(im, &conf.title_color);
1417 alloc_color(im, &black_color);
1418 alloc_color(im, &white_color);
1419
1420 if(conf.transparent_bg)
1421 gdImageColorTransparent(im, conf.color_bg.id);
1422
1423 for(i = 0; i < rcs->nbranches; i++)
1424 draw_branch(im, rcs->branches[i]);
1425 for(i = 0; i < rcs->nbranches; i++)
1426 {
1427 if(rcs->branches[i]->branchpoint)
1428 draw_connector(im, rcs->branches[i]);
1429 }
1430 draw_stringnl(im, cptr, &conf.title_font, conf.title_x, conf.title_y, conf.title_align, &conf.title_color);
1431 xfree(cptr);
1432
1433 return im;
1434 }
1435
1436 /*
1437 **************************************************************************
1438 * Layout routines
1439 *
1440 * Branch BBox:
1441 * left = center_x - total_width / 2 (cx-tw)/2
1442 * right = center_x + total_width / 2 (cx+tw)/2
1443 * top = y_pos (y)
1444 * bottom = y_pos + total_height (y+th)
1445 *
1446 * Margins of branches:
1447 *
1448 * . .
1449 * . .
1450 * +--------------+
1451 * ^
1452 * | branch_margin .
1453 * v .
1454 * ----------------+ .
1455 * | ^ |
1456 * | | branch_connect |
1457 * | v |
1458 *..-+ +t-----+------+ +------+------+
1459 * | l | | |
1460 * | <--> | branch bbox | <--> | branch bbox |
1461 * | | | r | | |
1462 *..-+ | +------------b+ | +-------------+
1463 * | ^ branch_margin
1464 * | | branch_margin
1465 * | v
1466 * | +-------------+
1467 * | . .
1468 * | . .
1469 * |
1470 * branch_margin
1471 *
1472 * FIXME: There are probable som +/-1 errors in the code...
1473 * (notably shadows are not calculated in the margins)
1474 **************************************************************************
1475 */
1476 static void move_branch(branch_t *b, int x, int y)
1477 {
1478 int i;
1479 b->cx += x;
1480 b->y += y;
1481 for(i = 0; i < b->nrevs; i++)
1482 {
1483 b->revs[i]->cx += x;
1484 b->revs[i]->y += y;
1485 }
1486 }
1487
1488 static void initial_reposition_branch(revision_t *r, int *x, int *w)
1489 {
1490 int i, j;
1491 for(j = 0; j < r->nbranches; j++)
1492 {
1493 branch_t *b = r->branches[j];
1494 *x += *w + conf.rev_minline + b->tw/2 - b->cx;
1495 *w = b->tw/2;
1496 move_branch(b, *x, r->y + r->h/2 + conf.branch_connect);
1497 *x = b->cx;
1498 /* Recurse to move branches of branched revisions */
1499 for(i = b->nrevs-1; i >= 0; i--)
1500 {
1501 initial_reposition_branch(b->revs[i], x, w);
1502 }
1503 }
1504 }
1505
1506 static void initial_reposition_branch_lr(revision_t *r, int *y, int *h)
1507 {
1508 int i, j;
1509 for(j = 0; j < r->nbranches; j++)
1510 {
1511 branch_t *b = r->branches[j];
1512 *y += *h + conf.rev_minline + b->th/2 - b->y;
1513 *h = b->th/2;
1514 move_branch(b, r->cx + r->w/2 + conf.branch_connect, *y);
1515 *y = b->y;
1516 /* Recurse to move branches of branched revisions */
1517 for(i = b->nrevs-1; i >= 0; i--)
1518 {
1519 initial_reposition_branch(b->revs[i], y, h);
1520 }
1521 }
1522 }
1523
1524 static void rect_union(int *x, int *y, int *w, int *h, branch_t *b)
1525 {
1526 int x1 = *x;
1527 int x2 = x1 + *w;
1528 int y1 = *y;
1529 int y2 = y1 + *h;
1530 int xx1;
1531 int xx2;
1532 int yy1;
1533 int yy2;
1534
1535 if(conf.left_right)
1536 {
1537 xx1 = b->cx;
1538 yy1 = b->y - b->th/2;
1539 }
1540 else
1541 {
1542 xx1 = b->cx - b->tw/2;
1543 yy1 = b->y;
1544 }
1545 xx2 = xx1 + b->tw;
1546 yy2 = yy1 + b->th;
1547
1548 x1 = MIN(x1, xx1);
1549 x2 = MAX(x2, xx2);
1550 y1 = MIN(y1, yy1);
1551 y2 = MAX(y2, yy2);
1552 *x = x1;
1553 *y = y1;
1554 *w = x2 - x1;
1555 *h = y2 - y1;
1556 }
1557
1558 static int branch_intersects(int top, int bottom, int left, branch_t *b)
1559 {
1560 int br = b->cx + b->tw/2;
1561 int bt = b->y - conf.branch_connect - conf.branch_margin/2;
1562 int bb = b->y + b->th + conf.branch_margin/2;
1563 return !(bt > bottom || bb < top || br >= left);
1564 }
1565
1566 static int branch_intersects_lr(int left, int right, int top, branch_t *b)
1567 {
1568 int bt = b->y + b->th/2;
1569 int bl = b->cx - conf.branch_connect - conf.branch_margin/2;
1570 int br = b->cx + b->tw + conf.branch_margin/2;
1571 return !(bl > right || br < left || bt >= top);
1572 }
1573
1574 static int kern_branch(rcsfile_t *rcs, branch_t *b)
1575 {
1576 int left = b->cx - b->tw/2;
1577 int top = b->y - conf.branch_connect - conf.branch_margin/2;
1578 int bottom = b->y + b->th + conf.branch_margin/2;
1579 int i;
1580 int xpos = 0;
1581
1582 for(i = 0; i < rcs->nbranches; i++)
1583 {
1584 branch_t *bp = rcs->branches[i];
1585 if(bp == b)
1586 continue;
1587 if(branch_intersects(top, bottom, left, bp))
1588 {
1589 int m = bp->cx + bp->tw/2 + conf.branch_margin;
1590 if(m > xpos)
1591 xpos = m;
1592 }
1593 }
1594 if(xpos && (b->cx - b->tw/2) - xpos > 0)
1595 {
1596 move_branch(b, xpos - (b->cx - b->tw/2), 0);
1597 return 1;
1598 }
1599 return 0;
1600 }
1601
1602 static int kern_branch_lr(rcsfile_t *rcs, branch_t *b)
1603 {
1604 int top = b->y - b->th/2;
1605 int left = b->cx - conf.branch_connect - conf.branch_margin/2;
1606 int right = b->cx + b->tw + conf.branch_margin/2;
1607 int i;
1608 int ypos = 0;
1609
1610 for(i = 0; i < rcs->nbranches; i++)
1611 {
1612 branch_t *bp = rcs->branches[i];
1613 if(bp == b)
1614 continue;
1615 if(branch_intersects_lr(left, right, top, bp))
1616 {
1617 int m = bp->y + bp->th/2 + conf.branch_margin;
1618 if(m > ypos)
1619 ypos = m;
1620 }
1621 }
1622 if(ypos && (b->y - b->th/2) - ypos > 0)
1623 {
1624 move_branch(b, 0, ypos - (b->y - b->th/2));
1625 return 1;
1626 }
1627 return 0;
1628 }
1629
1630 static int kern_tree(rcsfile_t *rcs)
1631 {
1632 int i;
1633 int moved;
1634 int safeguard;
1635 int totalmoved = 0;
1636 for(moved = 1, safeguard = LOOPSAFEGUARD; moved && safeguard; safeguard--)
1637 {
1638 moved = 0;
1639 for(i = 1; i < rcs->nbranches; i++)
1640 {
1641 if(conf.left_right)
1642 moved += kern_branch_lr(rcs, rcs->branches[i]);
1643 else
1644 moved += kern_branch(rcs, rcs->branches[i]);
1645 }
1646 totalmoved += moved;
1647 #ifdef DEBUG
1648 fprintf(stderr, "kern_tree: moved=%d\n", moved);
1649 #endif
1650 }
1651 if(!quiet && !safeguard)
1652 fprintf(stderr, "kern_tree: safeguard terminated possible infinite loop; please report.\n");
1653 return totalmoved;
1654 }
1655
1656 static int index_of_revision(revision_t *r)
1657 {
1658 branch_t *b = r->branch;
1659 int i;
1660 for(i = 0; i < b->nrevs; i++)
1661 {
1662 if(r == b->revs[i])
1663 return i;
1664 }
1665 fprintf(stderr, "index_of_revision: Cannot find revision in branch\n");
1666 return 0;
1667 }
1668
1669 static void branch_bbox(branch_t *br, int *l, int *r, int *t, int *b)
1670 {
1671 if(l) *l = br->cx - br->tw/2;
1672 if(r) *r = br->cx + br->tw/2;
1673 if(t) *t = br->y;
1674 if(b) *b = br->y + br->th + (conf.branch_dupbox ? conf.rev_minline + br->h : 0);
1675 }
1676
1677 static void branch_ext_bbox(branch_t *br, int *l, int *r, int *t, int *b)
1678 {
1679 int extra = conf.branch_margin & 1; /* Correct +/-1 error on div 2 */
1680 branch_bbox(br, l, r, t, b);
1681 if(l) *l -= conf.branch_margin/2;
1682 if(r) *r += conf.branch_margin/2 + extra;
1683 if(t) *t -= conf.branch_connect + conf.branch_margin/2;
1684 if(b) *b += conf.branch_margin/2 + extra;
1685 }
1686
1687 static int branch_distance(branch_t *br1, branch_t *br2)
1688 {
1689 int l1, r1, t1, b1;
1690 int l2, r2, t2, b2;
1691 assert(br1 != NULL);
1692 assert(br2 != NULL);
1693 branch_bbox(br1, &l1, &r1, NULL, NULL);
1694 branch_bbox(br2, &l2, &r2, NULL, NULL);
1695 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
1696 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
1697 /* Return:
1698 * - 0 if branches have no horizontal overlap
1699 * - positive if b1 is left of b2
1700 * - negative if b2 is left of b1
1701 */
1702 if((t1 > t2 && t1 < b2) || (b1 > t2 && b1 < b2))
1703 return l1 < l2 ? l2 - r1 : -(l1 - r2);
1704 else
1705 return 0;
1706 }
1707
1708 static int space_needed(branch_t *br1, branch_t *br2)
1709 {
1710 int t1, b1;
1711 int t2, b2;
1712 assert(br1 != NULL);
1713 assert(br2 != NULL);
1714 assert(br1->cx < br2->cx); /* br1 must be left of br2 */
1715 branch_ext_bbox(br1, NULL, NULL, &t1, &b1);
1716 branch_ext_bbox(br2, NULL, NULL, &t2, &b2);
1717 /* Return:
1718 * - positive if top br1 is located lower than br2
1719 * - negatve is top br2 is located lower than br1
1720 */
1721 if(t1 > t2)
1722 return -(t1 - b2);
1723 else
1724 return t2 - b1;
1725 }
1726
1727 static void move_yr_branch(branch_t *b, int dy)
1728 {
1729 int i, j;
1730 #ifdef DEBUG
1731 /* fprintf(stderr, "move_yr_branch: b=%s, dy=%d\n", b->branch->branch, dy);*/
1732 #endif
1733 b->y += dy;
1734 for(i = 0; i < b->nrevs; i++)
1735 {
1736 b->revs[i]->y += dy;
1737 for(j = 0; j < b->revs[i]->nbranches; j++)
1738 {
1739 #ifdef DEBUG
1740 /* fprintf(stderr, ".");*/
1741 #endif
1742 move_yr_branch(b->revs[i]->branches[j], dy);
1743 }
1744 }
1745 }
1746
1747 static void move_trunk(revision_t *r, int dy)
1748 {
1749 int i, j;
1750 branch_t *b = r->branch;
1751 b->th += dy;
1752 for(i = index_of_revision(r); i < b->nrevs; i++)
1753 {
1754 #ifdef DEBUG
1755 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);
1756 #endif
1757 b->revs[i]->y += dy;
1758 for(j = 0; j < b->revs[i]->nbranches; j++)
1759 {
1760 move_yr_branch(b->revs[i]->branches[j], dy);
1761 }
1762 }
1763 }
1764
1765 static int space_below(rcsfile_t *rcs, revision_t *r)
1766 {
1767 int i, j;
1768 int bl, br, bb;
1769 int space = INT_MAX;
1770 branch_t *b = r->branch;
1771 branch_t *minb = NULL;
1772
1773 branch_ext_bbox(b, &bl, &br, NULL, &bb);
1774 for(i = 0; i < rcs->nbranches; i++)
1775 {
1776 int tbl, tbr, tbt;
1777 branch_t *tb = rcs->branches[i];
1778 branch_ext_bbox(tb, &tbl, &tbr, &tbt, NULL);
1779 if(tb == b)
1780 continue;
1781 if(tbt > bb) /* Must be below our branch */
1782 {
1783 if(tb->branchpoint) /* Take account for the horiz connector */
1784 tbl = tb->branchpoint->cx + tb->branchpoint->branch->tw/2;
1785 if((bl >= tbl && bl <= tbr) || (br <= tbr && br >= tbl))
1786 {
1787 int s = tbt - bb - conf.branch_connect;
1788 if(s < space)
1789 {
1790 space = s;
1791 minb = tb;
1792 }
1793 }
1794 }
1795 }
1796 if(b->branchpoint)
1797 {
1798 for(i = index_of_revision(r); i < b->nrevs; i++)
1799 {
1800 for(j = 0; j < b->revs[i]->nbranches; j++)
1801 {
1802 int s = space_below(rcs, b->revs[i]->branches[j]->revs[0]);
1803 if(s < space)
1804 space = s;
1805 }
1806 }
1807 }
1808 #ifdef DEBUG
1809 fprintf(stderr, "space_below: from %s have %d to %s\n", b->branch->branch, space, minb ? minb->branch->branch : "<recursed>");
1810 #endif
1811 return space;
1812 }
1813
1814 static int space_available(rcsfile_t *rcs, branch_t *colbr, branch_t *tagbr, int *nl, revision_t **bpcommon)
1815 {
1816 int i;
1817 int space = 0;
1818 int nlinks = 0;
1819 revision_t *r;
1820 branch_t *b;
1821 branch_t *ancestor;
1822 revision_t *branchpoint;
1823
1824 if(!tagbr->branchpoint || !colbr->branchpoint)
1825 {
1826 if(!quiet)
1827 fprintf(stderr, "space_available: Trying to stretch the top?\n");
1828 return 0;
1829 }
1830
1831 r = colbr->branchpoint;
1832 b = r->branch;
1833 branchpoint = tagbr->branchpoint;
1834 ancestor = branchpoint->branch;
1835 assert(b != NULL);
1836 assert(ancestor != NULL);
1837
1838 while(1)
1839 {
1840 int s;
1841 int rtag = b == ancestor ? index_of_revision(branchpoint)+1 : 0;
1842 for(i = index_of_revision(r); i >= rtag; i--)
1843 {
1844 if(i > 0)
1845 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
1846 else
1847 s = b->revs[i]->y - (b->y + b->h);
1848 if(s < conf.rev_maxline)
1849 {
1850 space += conf.rev_maxline - s;
1851 nlinks++;
1852 }
1853 }
1854 s = space_below(rcs, r);
1855 if(s < space)
1856 space = s;
1857 #ifdef DEBUG
1858 if(space < 0)
1859 return -1;
1860 #endif
1861 if(b == ancestor)
1862 break;
1863 r = b->branchpoint;
1864 if(!r)
1865 {
1866 /* Not a common ancestor */
1867 r = colbr->branchpoint;
1868 b = r->branch;
1869 branchpoint = ancestor->branchpoint;
1870 if(!branchpoint)
1871 {
1872 if(!quiet)
1873 fprintf(stderr, "space_available: No common ancestor?\n");
1874 return 0;
1875 }
1876 ancestor = branchpoint->branch;
1877 assert(ancestor != NULL);
1878 nlinks = 0;
1879 space = 0;
1880 continue; /* Restart with a new ancestor */
1881 }
1882 b = r->branch;
1883 }
1884 if(nl)
1885 *nl = nlinks; /* Return the number of links that can stretch */
1886 if(bpcommon)
1887 *bpcommon = branchpoint; /* Return the ancestral branchpoint on the common branch */
1888 return space;
1889 }
1890
1891 static int stretch_branches(rcsfile_t *rcs, branch_t *br1, branch_t *br2, int totalstretch)
1892 {
1893 revision_t *r;
1894 revision_t *bpcommon = NULL;
1895 branch_t *ancestor = NULL;
1896 branch_t *b;
1897 int i;
1898 int space;
1899 int nlinks;
1900 int dy;
1901 int rest;
1902
1903 space = space_available(rcs, br1, br2, &nlinks, &bpcommon);
1904 if(bpcommon)
1905 ancestor = bpcommon->branch;
1906
1907 #ifdef DEBUG
1908 if(space == -1)
1909 return 0;
1910 fprintf(stderr, "stretch_branches: space available %d over %d links common %s\n", space, nlinks, ancestor->branch->branch);
1911 #endif
1912 if(space < totalstretch)
1913 return 0;
1914
1915 dy = totalstretch / nlinks;
1916 rest = totalstretch - dy * nlinks;
1917
1918 r = br1->branchpoint;
1919 b = r->branch;
1920 while(1)
1921 {
1922 int rtag = b == ancestor ? index_of_revision(bpcommon)+1 : 0;
1923 for(i = index_of_revision(r); i >= rtag; i--)
1924 {
1925 int s, q;
1926 if(i > 0)
1927 s = b->revs[i]->y - (b->revs[i-1]->y + b->revs[i-1]->h);
1928 else
1929 s = b->revs[i]->y - (b->y + b->h);
1930 q = conf.rev_maxline - s;
1931 if(q > 0)
1932 {
1933 int d = rest ? rest/nlinks+1 : 0;
1934 if(q >= dy+d)
1935 {
1936 move_trunk(b->revs[i], dy+d);
1937 }
1938 else
1939 {
1940 move_trunk(b->revs[i], q);
1941 rest += dy+d - q;
1942 }
1943 rest -= d;
1944 nlinks--;
1945 }
1946 }
1947 if(b == ancestor)
1948 break;
1949 r = b->branchpoint;
1950 assert(r != NULL); /* else 'space_available' wouldn't have returned positively */
1951 b = r->branch;
1952 }
1953 return 1;
1954 }
1955
1956 static branch_t *find_collision_branch(rcsfile_t *rcs, branch_t *b)
1957 {
1958 int i;
1959 int dist = INT_MAX;
1960 branch_t *col = NULL;
1961
1962 for(i = 0; i < rcs->nbranches; i++)
1963 {
1964 int t = branch_distance(rcs->branches[i], b);
1965 if(t > 0 && t < dist)
1966 {
1967 dist = t;
1968 col = rcs->branches[i];
1969 }
1970 }
1971 return col;
1972 }
1973
1974 void auto_stretch(rcsfile_t *rcs)
1975 {
1976 int i;
1977 int safeguard;
1978
1979 for(i = 0, safeguard = LOOPSAFEGUARD; i < rcs->nbranches && safeguard; i++)
1980 {
1981 int bl, pr;
1982 branch_t *b = rcs->branches[i];
1983 if(!b->branchpoint)
1984 continue;
1985 branch_bbox(b, &bl, NULL, NULL, NULL);
1986 branch_bbox(b->branchpoint->branch, NULL, &pr, NULL, NULL);
1987 if(bl - conf.branch_margin - pr > 0)
1988 {
1989 branch_t *col;
1990 int spaceneeded;
1991 /* There is a potential to move branch b further left.
1992 * All branches obstructing this one from moving further
1993 * left must be originating from revisions below
1994 * b->branchpoint until a common ancester.
1995 * So, we search all branches for a branch that lies left
1996 * of b and is closest to b. This is then the collission
1997 * branch that needs to be moved.
1998 */
1999 col = find_collision_branch(rcs, b);
2000 if(!col)
2001 continue;
2002 spaceneeded = space_needed(col, b);
2003 if(spaceneeded < 0)
2004 continue;
2005 #ifdef DEBUG
2006 fprintf(stderr, "auto_stretch: %s collides %s need %d\n", b->branch->branch, col->branch->branch, spaceneeded);
2007 #endif
2008 /* Trace the collision branch back to find the common ancester
2009 * of both col and b. All revisions encountered while traversing
2010 * backwards must be stretched, including all revisions on the
2011 * common ancester from where the branches sprout.
2012 */
2013 if(stretch_branches(rcs, col, b, spaceneeded))
2014 {
2015 if(kern_tree(rcs))
2016 {
2017 /* Restart the process because movement can
2018 * cause more movement.
2019 */
2020 i = 0 - 1; /* -1 for the i++ of the loop */
2021 safeguard--; /* Prevent infinite loop, just in case */
2022 }
2023 /*return;*/
2024 }
2025 }
2026 }
2027 if(!quiet && !safeguard)
2028 fprintf(stderr, "auto_stretch: safeguard terminated possible infinite loop; please report.\n");
2029 }
2030
2031 void make_layout(rcsfile_t *rcs)
2032 {
2033 int i, j;
2034 int x, y;
2035 int w, h;
2036 int w2;
2037
2038 /* Remove all unwanted revisions */
2039 if(conf.strip_untagged)
2040 {
2041 int fr = conf.strip_first_rev ? 0 : 1;
2042 for(i = 0; i < rcs->nbranches; i++)
2043 {
2044 branch_t *bp = rcs->branches[i];
2045 for(j = fr; j < bp->nrevs-1; j++)
2046 {
2047 if(!bp->revs[j]->ntags && !bp->revs[j]->nbranches)
2048 {
2049 memmove(&bp->revs[j], &bp->revs[j+1], (bp->nrevs-j-1) * sizeof(bp->revs[0]));
2050 bp->nrevs--;
2051 bp->revs[j]->stripped = 1;
2052 j--;
2053 }
2054 }
2055 }
2056 }
2057
2058 /* Calculate the box-sizes of the revisions */
2059 for(i = 0; i < rcs->nsrev; i++)
2060 {
2061 revision_t *rp;
2062 int w;
2063 int h;
2064 rp = rcs->srev[i];
2065 rp->revtext = expand_string(conf.rev_text, rcs, rp, rp->rev, NULL, rp->ntags ? rp->tags[0] : NULL);
2066 w = get_swidth(rp->revtext, &conf.rev_text_font);
2067 j = get_swidth(rp->rev->rev, &conf.rev_font);
2068 if(j > w)
2069 w = j;
2070 h = get_sheight(rp->revtext, &conf.rev_text_font) + get_sheight(rp->rev->rev, &conf.rev_font);
2071 for(j = 0; j < rp->ntags; j++)
2072 {
2073 int ww = get_swidth(rp->tags[j]->tag, &conf.tag_font);
2074 if(ww > w) w = ww;
2075 h += get_sheight(rp->tags[j]->tag, &conf.tag_font) + conf.rev_separator;
2076 }
2077 rp->w = w + conf.rev_lspace + conf.rev_rspace;
2078 rp->h = h + conf.rev_tspace + conf.rev_bspace;
2079 }
2080
2081 /* Calculate the box-sizes of the branches */
2082 for(i = 0; i < rcs->nbranches; i++)
2083 {
2084 branch_t *bp = rcs->branches[i];
2085 int w;
2086 int h;
2087 w = get_swidth(bp->branch->branch, &conf.branch_font);
2088 h = get_sheight(bp->branch->branch, &conf.branch_font);
2089 for(j = 0; j < bp->ntags; j++)
2090 {
2091 int ww = get_swidth(bp->tags[j]->tag, &conf.branch_tag_font);
2092 if(ww > w) w = ww;
2093 h += get_sheight(bp->tags[j]->tag, &conf.branch_tag_font);
2094 }
2095 w += conf.branch_lspace + conf.branch_rspace;
2096 h += conf.branch_tspace + conf.branch_bspace;
2097 bp->w = w;
2098 bp->h = h;
2099 if(conf.left_right)
2100 {
2101 for(j = 0; j < bp->nrevs; j++)
2102 {
2103 if(bp->revs[j]->h > h)
2104 h = bp->revs[j]->h;
2105 w += bp->revs[j]->w + conf.rev_minline;
2106 }
2107 if(conf.branch_dupbox)
2108 w += bp->w + conf.rev_minline;
2109 }
2110 else
2111 {
2112 for(j = 0; j < bp->nrevs; j++)
2113 {
2114 if(bp->revs[j]->w > w)
2115 w = bp->revs[j]->w;
2116 h += bp->revs[j]->h + conf.rev_minline;
2117 }
2118 if(conf.branch_dupbox)
2119 h += bp->h + conf.rev_minline;
2120 }
2121 bp->th = h;
2122 bp->tw = w;
2123 }
2124
2125 /* Calculate the relative positions of revs in a branch */
2126 if(conf.left_right)
2127 {
2128 for(i = 0; i < rcs->nbranches; i++)
2129 {
2130 branch_t *b = rcs->branches[i];
2131 y = b->th/2;
2132 x = b->w;
2133 b->y = y;
2134 b->cx = 0;
2135 for(j = 0; j < b->nrevs; j++)
2136 {
2137 x += conf.rev_minline;
2138 b->revs[j]->y = y;
2139 b->revs[j]->cx = x;
2140 x += b->revs[j]->w;
2141 }
2142 }
2143 }
2144 else
2145 {
2146 for(i = 0; i < rcs->nbranches; i++)
2147 {
2148 branch_t *b = rcs->branches[i];
2149 x = b->tw/2;
2150 y = b->h;
2151 b->cx = x;
2152 b->y = 0;
2153 for(j = 0; j < b->nrevs; j++)
2154 {
2155 y += conf.rev_minline;
2156 b->revs[j]->cx = x;
2157 b->revs[j]->y = y;
2158 y += b->revs[j]->h;
2159 }
2160 }
2161 }
2162
2163 /* Initially reposition the branches from bottom to top progressively right */
2164 if(conf.left_right)
2165 {
2166 x = rcs->branches[0]->y;
2167 w2 = rcs->branches[0]->th / 2;
2168 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
2169 {
2170 initial_reposition_branch_lr(rcs->branches[0]->revs[i], &x, &w2);
2171 }
2172 }
2173 else
2174 {
2175 x = rcs->branches[0]->cx;
2176 w2 = rcs->branches[0]->tw / 2;
2177 for(i = rcs->branches[0]->nrevs-1; i >= 0; i--)
2178 {
2179 initial_reposition_branch(rcs->branches[0]->revs[i], &x, &w2);
2180 }
2181 }
2182
2183 /* Initially move branches left if there is room */
2184 kern_tree(rcs);
2185
2186 /* Try to kern the branches more by expanding the inter-revision spacing */
2187 if(conf.auto_stretch && !conf.left_right)
2188 auto_stretch(rcs);
2189
2190 /* Move everything w.r.t. the top-left margin */
2191 for(i = 0; i < rcs->nbranches; i++)
2192 move_branch(rcs->branches[i], conf.margin_left, conf.margin_top);
2193
2194 /* Calculate overall image size */
2195 if(conf.left_right)
2196 {
2197 x = rcs->branches[0]->cx;
2198 y = rcs->branches[0]->y - rcs->branches[0]->th/2;
2199 }
2200 else
2201 {
2202 x = rcs->branches[0]->cx - rcs->branches[0]->tw/2;
2203 y = rcs->branches[0]->y;
2204 }
2205 w = rcs->branches[0]->tw;
2206 h = rcs->branches[0]->th;
2207 for(i = 1; i < rcs->nbranches; i++)
2208 rect_union(&x, &y, &w, &h, rcs->branches[i]);
2209 rcs->tw = w;
2210 rcs->th = h;
2211
2212 /* Flip the entire tree */
2213 if(conf.upside_down)
2214 {
2215 if(conf.left_right)
2216 {
2217 x += rcs->tw;
2218 for(i = 0; i < rcs->nbranches; i++)
2219 {
2220 branch_t *b = rcs->branches[i];
2221 for(j = 0; j < b->nrevs; j++)
2222 {
2223 revision_t *r = b->revs[j];
2224 r->cx = x - r->cx - r->w + conf.margin_left;
2225 }
2226 b->cx = x - b->cx - b->w + conf.margin_left;
2227 }
2228 }
2229 else
2230 {
2231 y += rcs->th;
2232 for(i = 0; i < rcs->nbranches; i++)
2233 {
2234 branch_t *b = rcs->branches[i];
2235 for(j = 0; j < b->nrevs; j++)
2236 {
2237 revision_t *r = b->revs[j];
2238 r->y = y - r->y - r->h + conf.margin_top;
2239 }
2240 b->y = y - b->y - b->h + conf.margin_top;
2241 }
2242 }
2243 }
2244 }
2245
2246 /*
2247 **************************************************************************
2248 * Imagemap functions
2249 **************************************************************************
2250 */
2251 void make_imagemap(rcsfile_t *rcs, FILE *fp, gdImagePtr im)
2252 {
2253 int i, j;
2254 fprintf(fp, "<map name=\"%s\">\n", conf.map_name);
2255 for(i = 0; i < rcs->nbranches; i++)
2256 {
2257 branch_t *b = rcs->branches[i];
2258 tag_t *tag = b->ntags ? b->tags[0] : NULL;
2259 char *bhref = expand_string(conf.map_branch_href, rcs, NULL, b->branch, NULL, tag);
2260 char *balt = expand_string(conf.map_branch_alt, rcs, NULL, b->branch, NULL, tag);
2261 int x1;
2262 int x2;
2263 int y1;
2264 int y2;
2265
2266 if(conf.left_right)
2267 {
2268 x1 = b->cx;
2269 y1 = b->y - b->h/2;
2270 x2 = b->cx + b->w;
2271 y2 = b->y + b->h/2;
2272 }
2273 else
2274 {
2275 x1 = b->cx - b->w/2;
2276 y1 = b->y;
2277 x2 = b->cx + b->w/2;
2278 y2 = b->y + b->h;
2279 }
2280 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2281 bhref, x1, y1, x2, y2, balt);
2282 if(im)
2283 {
2284 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2285 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2286 gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2287 }
2288 for(j = 0; j < b->nrevs; j++)
2289 {
2290 revision_t *r = b->revs[j];
2291 revision_t* r1;
2292 int xoff = 1;
2293 int yoff = 1;
2294 char *href;
2295 char *alt;
2296
2297 tag = r->ntags ? r->tags[0] : NULL;
2298 href = expand_string(conf.map_rev_href, rcs, r, r->rev, NULL, tag);
2299 alt = expand_string(conf.map_rev_alt, rcs, r, r->rev, NULL, tag);
2300 if(conf.left_right)
2301 {
2302 x1 = r->cx;
2303 y1 = r->y - r->h/2;
2304 x2 = r->cx + r->w;
2305 y2 = r->y + r->h/2;
2306 }
2307 else
2308 {
2309 x1 = r->cx - r->w/2;
2310 y1 = r->y;
2311 x2 = r->cx + r->w/2;
2312 y2 = r->y + r->h;
2313 }
2314 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2315 href, x1, y1, x2, y2, alt);
2316 if(im)
2317 {
2318 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2319 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2320 gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2321 }
2322 xfree(href);
2323 xfree(alt);
2324 if(j > 0 || b->branchpoint)
2325 {
2326 if(j > 0)
2327 {
2328 r1 = b->revs[j-1];
2329 if(conf.left_right)
2330 {
2331 yoff = MIN(r->h, r1->h)/4;
2332 x1 = conf.upside_down ? r1->cx : r1->cx + r1->w;
2333 }
2334 else
2335 {
2336 xoff = MIN(r->w, r1->w)/4;
2337 y1 = conf.upside_down ? r1->y : r1->y + r1->h;
2338 }
2339 }
2340 else
2341 {
2342 r1 = b->branchpoint;
2343 if(conf.left_right)
2344 {
2345 yoff = MIN(r->h, b->h)/4;
2346 x1 = conf.upside_down ? b->cx : b->cx + b->w;
2347 }
2348 else
2349 {
2350 xoff = MIN(r->w, b->w)/4;
2351 y1 = conf.upside_down ? b->y : b->y + b->h;
2352 }
2353 }
2354 if(conf.left_right)
2355 {
2356 y1 = r->y - yoff;
2357 y2 = r->y + yoff;
2358 x2 = conf.upside_down ? r->cx + r->w : r->cx;
2359 yoff = 0;
2360 }
2361 else
2362 {
2363 x1 = r->cx - xoff;
2364 x2 = r->cx + xoff;
2365 y2 = conf.upside_down ? r->y + r->h : r->y;
2366 xoff = 0;
2367 }
2368 if(x1 > x2)
2369 {
2370 int tt = x1;
2371 x1 = x2;
2372 x2 = tt;
2373 }
2374 if(y1 > y2)
2375 {
2376 int tt = y1;
2377 y1 = y2;
2378 y2 = tt;
2379 }
2380 href = expand_string(conf.map_diff_href, rcs, r, r->rev, r1->rev, tag);
2381 alt = expand_string(conf.map_diff_alt, rcs, r, r->rev, r1->rev, tag);
2382 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2383 href,
2384 x1+xoff, y1+yoff, x2-xoff, y2-yoff,
2385 alt);
2386 if(im)
2387 {
2388 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2389 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2390 gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2391 }
2392 xfree(href);
2393 xfree(alt);
2394 }
2395 }
2396 if(conf.branch_dupbox)
2397 {
2398 if(conf.left_right)
2399 {
2400 x1 = conf.upside_down ? b->cx + b->w - b->tw : b->cx - b->w + b->tw;
2401 y1 = b->y - b->h/2;
2402 x2 = x1 + b->w;
2403 y2 = b->y + b->h/2;
2404 }
2405 else
2406 {
2407 x1 = b->cx - b->w/2;
2408 y1 = conf.upside_down ? b->y + b->h - b->th : b->y - b->h + b->th;
2409 x2 = b->cx + b->w/2;
2410 y2 = y1 + b->h;
2411 }
2412 fprintf(fp, "\t<area shape=\"rect\" %s coords=\"%d,%d,%d,%d\" %s>\n",
2413 bhref, x1, y1, x2, y2, balt);
2414 if(im)
2415 {
2416 gdImageFilledRectangle(im, x1-2, y1-2, x1+2, y1+2, conf.title_color.id);
2417 gdImageFilledRectangle(im, x2-2, y2-2, x2+2, y2+2, conf.tag_color.id);
2418 gdImageLine(im, x1, y1, x2, y2, conf.title_color.id);
2419 }
2420 }
2421 xfree(bhref);
2422 xfree(balt);
2423 }
2424 fprintf(fp, "</map>\n");
2425 }
2426
2427 /*
2428 **************************************************************************
2429 * Program entry
2430 **************************************************************************
2431 */
2432 static const char usage_str[] =
2433 "Usage: cvsgraph [options] <file>\n"
2434 " -b Add a branch box at both sides of the trunk (config value is negated)\n"
2435 " -c <file> Read alternative config from <file>\n"
2436 " -d <level> Enable debug mode at <level>\n"
2437 " -h This message\n"
2438 " -i Generate an imagemap instead of image\n"
2439 " -I <file> Also write the imagemap to <file>\n"
2440 " -k Auto stretch the tree (config value is negated)\n"
2441 " -M <name> Use <name> as imagemap name\n"
2442 " -m <mod> Use <mod> as cvs module\n"
2443 " -o <file> Output to <file>\n"
2444 " -O <opt=val> Set option opt to value val\n"
2445 " -q Be quiet (i.e. no warnings)\n"
2446 " -r <path> Use <path> as cvsroot path\n"
2447 " -s Strip untagged revisions (config value is negated)\n"
2448 " -S Also strip the first revision (config value is negated)\n"
2449 " -u Upside down image (mirror vertically; config value is negated)\n"
2450 " -V Print version and exit\n"
2451 " -[0-9] <txt> Use <txt> for expansion\n"
2452 ;
2453
2454 #define VERSION_STR "1.2.1"
2455 #define NOTICE_STR "Copyright (c) 2001,2002 B.Stultiens"
2456
2457 static void append_slash(char **path)
2458 {
2459 int l;
2460 assert(path != NULL);
2461 assert(*path != NULL);
2462 l = strlen(*path);
2463 if(!l || (*path)[l-1] == '/')
2464 return;
2465 *path = xrealloc(*path, l+2);
2466 strcat(*path, "/");
2467 }
2468
2469 int main(int argc, char *argv[])
2470 {
2471 extern int rcs_flex_debug;
2472 extern int rcsdebug;
2473 int optc;
2474 char *confpath = NULL;
2475 char *outfile = NULL;
2476 char *cvsroot = NULL;
2477 char *cvsmodule = NULL;
2478 int imagemap = 0;
2479 int upsidedown = 0;
2480 int bdupbox = 0;
2481 int stripuntag = 0;
2482 int stripfirst = 0;
2483 int autostretch = 0;
2484 char *imgmapname = NULL;
2485 char *imgmapfile = NULL;
2486 int lose = 0;
2487 FILE *fp;
2488 char *rcsfilename;
2489 rcsfile_t *rcs;
2490 gdImagePtr im;
2491
2492 while((optc = getopt(argc, argv, "0:1:2:3:4:5:6:7:8:9:bc:d:hI:ikM:m:O:o:qr:SsuV")) != EOF)
2493 {
2494 switch(optc)
2495 {
2496 case 'b':
2497 bdupbox = 1;
2498 break;
2499 case 'c':
2500 confpath = xstrdup(optarg);
2501 break;
2502 case 'd':
2503 debuglevel = strtol(optarg, NULL, 0);
2504 break;
2505 case 'I':
2506 imgmapfile = xstrdup(optarg);
2507 break;
2508 case 'i':
2509 imagemap = 1;
2510 break;
2511 case 'k':
2512 autostretch = 1;
2513 break;
2514 case 'M':
2515 imgmapname = xstrdup(optarg);
2516 break;
2517 case 'm':
2518 cvsmodule = xstrdup(optarg);
2519 break;
2520 case 'O':
2521 stack_option(optarg);
2522 break;
2523 case 'o':
2524 outfile = xstrdup(optarg);
2525 break;
2526 case 'q':
2527 quiet = 1;
2528 break;
2529 case 'r':
2530 cvsroot = xstrdup(optarg);
2531 break;
2532 case 'S':
2533 stripfirst = 1;
2534 break;
2535 case 's':
2536 stripuntag = 1;
2537 break;
2538 case 'u':
2539 upsidedown = 1;
2540 break;
2541 case 'V':
2542 fprintf(stdout, "cvsgraph v%s, %s\n", VERSION_STR, NOTICE_STR);
2543 return 0;
2544 case 'h':
2545 fprintf(stdout, "%s", usage_str);
2546 return 0;
2547 default:
2548 if(isdigit(optc))
2549 {
2550 conf.expand[optc-'0'] = xstrdup(optarg);
2551 }
2552 else
2553 lose++;
2554 }
2555 }
2556
2557 if(lose)
2558 {
2559 fprintf(stderr, "%s", usage_str);
2560 return 1;
2561 }
2562
2563 if(debuglevel)
2564 {
2565 setvbuf(stdout, NULL, 0, _IONBF);
2566 setvbuf(stderr, NULL, 0, _IONBF);
2567 }
2568 rcs_flex_debug = (debuglevel & DEBUG_RCS_LEX) != 0;
2569 rcsdebug = (debuglevel & DEBUG_RCS_YACC) != 0;
2570
2571 /* Set defaults */
2572 conf.tag_font.gdfont = gdFontTiny;
2573 conf.rev_font.gdfont = gdFontTiny;
2574 conf.branch_font.gdfont = gdFontTiny;
2575 conf.branch_tag_font.gdfont = gdFontTiny;
2576 conf.title_font.gdfont = gdFontTiny;
2577 conf.rev_text_font.gdfont = gdFontTiny;
2578
2579 conf.anti_alias = 1;
2580 conf.thick_lines = 1;
2581
2582 conf.cvsroot = xstrdup("");
2583 conf.cvsmodule = xstrdup("");
2584 conf.date_format = xstrdup("%d-%b-%Y %H:%M:%S");
2585 conf.title = xstrdup("");
2586 conf.map_name = xstrdup("CvsGraphImageMap");
2587 conf.map_branch_href = xstrdup("href=\"unset: conf.map_branch_href\"");
2588 conf.map_branch_alt = xstrdup("alt=\"%B\"");
2589 conf.map_rev_href = xstrdup("href=\"unset: conf.map_rev_href\"");
2590 conf.map_rev_alt = xstrdup("alt=\"%R\"");
2591 conf.map_diff_href = xstrdup("href=\"unset: conf.map_diff_href\"");
2592 conf.map_diff_alt = xstrdup("alt=\"%P &lt;-&gt; %R\"");
2593 conf.rev_text = xstrdup("%d");
2594
2595 conf.color_bg = white_color;
2596 conf.branch_bgcolor = white_color;
2597 conf.branch_color = black_color;
2598 conf.branch_tag_color = black_color;
2599 conf.rev_color = black_color;
2600 conf.rev_bgcolor = white_color;
2601 conf.tag_color = black_color;
2602 conf.title_color = black_color;
2603 conf.rev_text_color = black_color;
2604
2605 conf.image_quality = 100;
2606 conf.rev_maxline = -1; /* Checked later to set to default */
2607
2608 read_config(confpath);
2609
2610 if(conf.rev_maxline == -1) conf.rev_maxline = 5 * conf.rev_minline;
2611
2612 /* Set overrides */
2613 if(cvsroot) conf.cvsroot = cvsroot;
2614 if(cvsmodule) conf.cvsmodule = cvsmodule;
2615 if(imgmapname) conf.map_name = imgmapname;
2616 if(upsidedown) conf.upside_down = !conf.upside_down;
2617 if(bdupbox) conf.branch_dupbox = !conf.branch_dupbox;
2618 if(stripuntag) conf.strip_untagged = !conf.strip_untagged;
2619 if(stripfirst) conf.strip_first_rev = !conf.strip_first_rev;
2620 if(autostretch) conf.auto_stretch = !conf.auto_stretch;
2621
2622 if(conf.rev_minline >= conf.rev_maxline)
2623 {
2624 if(conf.auto_stretch && !quiet)
2625 fprintf(stderr, "Auto stretch is only possible if rev_minline < rev_maxline\n");
2626 conf.auto_stretch = 0;
2627 }
2628
2629 if(conf.thick_lines < 1)
2630 conf.thick_lines = 1;
2631 if(conf.thick_lines > 11)
2632 conf.thick_lines = 11;
2633
2634 append_slash(&conf.cvsroot);
2635 append_slash(&conf.cvsmodule);
2636
2637 if(optind >= argc)
2638 {
2639 #ifdef __WIN32__
2640 /* Bad hack for DOS/Windows */
2641 if(setmode(fileno(stdin), O_BINARY) == -1)
2642 {
2643 perror("Set binary mode for stdin");
2644 return 1;
2645 }
2646 #endif
2647 rcsfilename = NULL;
2648 }
2649 else
2650 rcsfilename = argv[optind];
2651
2652 rcs = get_rcsfile(conf.cvsroot, conf.cvsmodule, rcsfilename);
2653 if(!rcs)
2654 return 1;
2655
2656 if(debuglevel & DEBUG_RCS_FILE)
2657 dump_rcsfile(rcs);
2658
2659 if(!reorganise_branches(rcs))
2660 return 1;
2661
2662 if(!assign_tags(rcs))
2663 return 1;
2664
2665 if(outfile)
2666 {
2667 if((fp = fopen(outfile, "wb")) == NULL)
2668 {
2669 perror(outfile);
2670 return 1;
2671 }
2672 }
2673 else
2674 {
2675 fp = stdout;
2676 #ifdef __WIN32__
2677 /* Bad hack for DOS/Windows */
2678 if(setmode(fileno(fp), O_BINARY) == -1)
2679 {
2680 perror("Set binary mode for stdout");
2681 return 1;
2682 }
2683 #endif
2684 }
2685
2686 make_layout(rcs);
2687
2688 if(!imagemap)
2689 {
2690 /* Create an image */
2691 im = make_image(rcs);
2692 #ifdef DEBUG_IMAGEMAP
2693 {
2694 FILE *nulfile = fopen("/dev/null", "w");
2695 make_imagemap(rcs, nulfile, im);
2696 fclose(nulfile);
2697 }
2698 #endif
2699 switch(conf.image_type)
2700 {
2701 #ifdef HAVE_IMAGE_GIF
2702 # ifndef HAVE_IMAGE_PNG
2703 default:
2704 # endif
2705 case IMAGE_GIF:
2706 gdImageGif(im, fp);
2707 break;
2708 #endif
2709 #ifdef HAVE_IMAGE_PNG
2710 default:
2711 case IMAGE_PNG:
2712 gdImagePng(im, fp);
2713 break;
2714 #endif
2715 #ifdef HAVE_IMAGE_JPEG
2716 # if !defined(HAVE_IMAGE_GIF) && !defined(HAVE_IMAGE_PNG)
2717 default:
2718 # endif
2719 case IMAGE_JPEG:
2720 gdImageJpeg(im, fp, conf.image_quality);
2721 break;
2722 #endif
2723 }
2724
2725 gdImageDestroy(im);
2726 }
2727 else
2728 {
2729 /* Create an imagemap */
2730 make_imagemap(rcs, fp, NULL);
2731 }
2732
2733 /* Also create imagemap to file if requested */
2734 if(imgmapfile)
2735 {
2736 FILE *ifp = fopen(imgmapfile, "wb");
2737 if(!ifp)
2738 {
2739 perror(imgmapfile);
2740 return 1;
2741 }
2742 make_imagemap(rcs, ifp, NULL);
2743 fclose(ifp);
2744 }
2745
2746 if(outfile)
2747 fclose(fp);
2748
2749 return 0;
2750 }

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0