t2p.c

changeset 74
12bc5088172e
parent 66
6e0551b59dba
child 75
29d7cbc7c251
     1.1 --- a/t2p.c	Wed Mar 05 01:58:31 2003 +0000
     1.2 +++ b/t2p.c	Wed Mar 05 01:58:36 2003 +0000
     1.3 @@ -4,7 +4,7 @@
     1.4   *      will be compressed using ITU-T T.6 (G4) fax encoding.
     1.5   *
     1.6   * Main program
     1.7 - * $Id: t2p.c,v 1.24 2003/02/21 01:25:47 eric Exp $
     1.8 + * $Id: t2p.c,v 1.25 2003/03/04 17:58:36 eric Exp $
     1.9   * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
    1.10   *
    1.11   * This program is free software; you can redistribute it and/or modify
    1.12 @@ -88,7 +88,7 @@
    1.13    fprintf (stderr, "    -v   verbose\n");
    1.14    fprintf (stderr, "    -b fmt  create bookmarks\n");
    1.15    fprintf (stderr, "bookmark format:\n");
    1.16 -  fprintf (stderr, "    %%F  file name\n");
    1.17 +  fprintf (stderr, "    %%F  file name (sans suffix)\n");
    1.18    fprintf (stderr, "    %%p  page number\n");
    1.19  }
    1.20  
    1.21 @@ -311,7 +311,7 @@
    1.22  					  points */
    1.23  
    1.24    Rect rect;
    1.25 -  Bitmap *bitmap;
    1.26 +  Bitmap *bitmap = NULL;
    1.27  
    1.28    int row;
    1.29  
    1.30 @@ -464,6 +464,7 @@
    1.31  
    1.32    page = pdf_new_page (out->pdf, width_points, height_points);
    1.33  
    1.34 +#if 0
    1.35    pdf_write_g4_fax_image (page,
    1.36  			  0, 0,  /* x, y */
    1.37  			  width_points, height_points,
    1.38 @@ -471,21 +472,102 @@
    1.39  			  0, /* ImageMask */
    1.40  			  0, 0, 0,  /* r, g, b */
    1.41  			  0); /* BlackIs1 */
    1.42 +#endif
    1.43  
    1.44 -  free_bitmap (bitmap);
    1.45 +  while (bookmarks)
    1.46 +    {
    1.47 +      /* $$$ need to handle level here */
    1.48 +      pdf_new_bookmark (NULL, bookmarks->name, 0, page);
    1.49 +      bookmarks = bookmarks->next;
    1.50 +    }
    1.51  
    1.52    result = 1;
    1.53  
    1.54   fail:
    1.55 +  if (bitmap)
    1.56 +    free_bitmap (bitmap);
    1.57 +
    1.58    return (result);
    1.59  }
    1.60  
    1.61  
    1.62 -void main_args (char *out_fn, int inf_count, char **in_fn)
    1.63 +#define MAX_BOOKMARK_NAME_LEN 500
    1.64 +
    1.65 +
    1.66 +static int filename_length_without_suffix (char *in_fn)
    1.67 +{
    1.68 +  char *p;
    1.69 +  int len = strlen (in_fn);
    1.70 +
    1.71 +  p = strrchr (in_fn, '.');
    1.72 +  if (p && ((strcasecmp (p, ".tif") == 0) ||
    1.73 +	    (strcasecmp (p, ".tiff") == 0)))
    1.74 +    return (p - in_fn);
    1.75 +  return (len);
    1.76 +}
    1.77 +
    1.78 +
    1.79 +/* $$$ this function should ensure that it doesn't overflow the name string! */
    1.80 +static void generate_bookmark_name (char *name,
    1.81 +				    char *bookmark_fmt, 
    1.82 +				    char *in_fn,
    1.83 +				    int page)
    1.84 +{
    1.85 +  bool meta = 0;
    1.86 +  int len;
    1.87 +
    1.88 +  while (*bookmark_fmt)
    1.89 +    {
    1.90 +      if (meta)
    1.91 +	{
    1.92 +	  meta = 0;
    1.93 +	  switch (*bookmark_fmt)
    1.94 +	    {
    1.95 +	    case '%':
    1.96 +	      *(name++) = '%';
    1.97 +	      break;
    1.98 +	    case 'F':
    1.99 +	      len = filename_length_without_suffix (in_fn);
   1.100 +	      strncpy (name, in_fn, len);
   1.101 +	      name += len;
   1.102 +	      break;
   1.103 +	    case 'p':
   1.104 +	      sprintf (name, "%d", page);
   1.105 +	      name += strlen (name);
   1.106 +	      break;
   1.107 +	    default:
   1.108 +	      break;
   1.109 +	    }
   1.110 +	}
   1.111 +      else
   1.112 +	switch (*bookmark_fmt)
   1.113 +	  {
   1.114 +	  case '%':
   1.115 +	    meta = 1;
   1.116 +	    break;
   1.117 +	  default:
   1.118 +	    *(name++) = *bookmark_fmt;
   1.119 +	  }
   1.120 +      bookmark_fmt++;
   1.121 +    }
   1.122 +  *bookmark_fmt = '\0';
   1.123 +}
   1.124 +
   1.125 +
   1.126 +void main_args (char *out_fn,
   1.127 +		int inf_count,
   1.128 +		char **in_fn,
   1.129 +		char *bookmark_fmt)
   1.130  {
   1.131    int i, ip;
   1.132    input_attributes_t input_attributes;
   1.133    pdf_file_attributes_t output_attributes;
   1.134 +  bookmark_t bookmark;
   1.135 +  char bookmark_name [MAX_BOOKMARK_NAME_LEN];
   1.136 +
   1.137 +  bookmark.next = NULL;
   1.138 +  bookmark.level = 1;
   1.139 +  bookmark.name = & bookmark_name [0];
   1.140  
   1.141    memset (& input_attributes, 0, sizeof (input_attributes));
   1.142    memset (& output_attributes, 0, sizeof (output_attributes));
   1.143 @@ -499,7 +581,13 @@
   1.144        for (ip = 1;; ip++)
   1.145  	{
   1.146  	  fprintf (stderr, "processing page %d of file \"%s\"\r", ip, in_fn [i]);
   1.147 -	  if (! process_page (ip, input_attributes, NULL))
   1.148 +	  if (bookmark_fmt)
   1.149 +	    generate_bookmark_name (& bookmark_name [0],
   1.150 +				    bookmark_fmt, 
   1.151 +				    in_fn [i],
   1.152 +				    ip);
   1.153 +	  if (! process_page (ip, input_attributes,
   1.154 +			      bookmark_fmt ? & bookmark : NULL))
   1.155  	    fatal (3, "error processing page %d of input file \"%s\"\n", ip, in_fn [i]);
   1.156  	  if (last_tiff_page ())
   1.157  	    break;
   1.158 @@ -596,7 +684,7 @@
   1.159    if (spec_fn)
   1.160      main_spec (spec_fn);
   1.161    else
   1.162 -    main_args (out_fn, inf_count, in_fn);
   1.163 +    main_args (out_fn, inf_count, in_fn, bookmark_fmt);
   1.164    
   1.165    close_tiff_input_file ();
   1.166    close_pdf_output_files ();