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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0