Wed, 12 Mar 2003 11:13:28 +0000
don't declare variable in middle of block, makes older C compilers unhappy.
eric@49 | 1 | /* |
eric@49 | 2 | * t2p: Create a PDF file from the contents of one or more TIFF |
eric@49 | 3 | * bilevel image files. The images in the resulting PDF file |
eric@49 | 4 | * will be compressed using ITU-T T.6 (G4) fax encoding. |
eric@49 | 5 | * |
eric@49 | 6 | * Semantic routines for spec file parser |
eric@104 | 7 | * $Id: semantics.c,v 1.18 2003/03/12 00:38:04 eric Exp $ |
eric@49 | 8 | * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> |
eric@49 | 9 | * |
eric@49 | 10 | * This program is free software; you can redistribute it and/or modify |
eric@49 | 11 | * it under the terms of the GNU General Public License version 2 as |
eric@49 | 12 | * published by the Free Software Foundation. Note that permission is |
eric@49 | 13 | * not granted to redistribute this program under the terms of any |
eric@49 | 14 | * other version of the General Public License. |
eric@49 | 15 | * |
eric@49 | 16 | * This program is distributed in the hope that it will be useful, |
eric@49 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
eric@49 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eric@49 | 19 | * GNU General Public License for more details. |
eric@49 | 20 | * |
eric@49 | 21 | * You should have received a copy of the GNU General Public License |
eric@49 | 22 | * along with this program; if not, write to the Free Software |
eric@104 | 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA |
eric@104 | 24 | */ |
eric@49 | 25 | |
eric@49 | 26 | |
eric@48 | 27 | #include <stdbool.h> |
eric@48 | 28 | #include <stdint.h> |
eric@18 | 29 | #include <stdlib.h> |
eric@18 | 30 | #include <string.h> |
eric@16 | 31 | #include <stdio.h> |
eric@16 | 32 | |
eric@18 | 33 | #include "semantics.h" |
eric@16 | 34 | #include "parser.tab.h" |
eric@44 | 35 | #include "t2p.h" |
eric@16 | 36 | |
eric@16 | 37 | |
eric@22 | 38 | typedef struct |
eric@22 | 39 | { |
eric@48 | 40 | bool has_page_size; |
eric@32 | 41 | page_size_t page_size; |
eric@22 | 42 | |
eric@48 | 43 | bool has_rotation; |
eric@22 | 44 | int rotation; |
eric@22 | 45 | |
eric@48 | 46 | bool has_crop; |
eric@22 | 47 | crop_t crop; |
eric@22 | 48 | } input_modifiers_t; |
eric@22 | 49 | |
eric@22 | 50 | |
eric@22 | 51 | typedef struct input_context_t |
eric@22 | 52 | { |
eric@22 | 53 | struct input_context_t *parent; |
eric@22 | 54 | struct input_context_t *next; |
eric@22 | 55 | |
eric@22 | 56 | int image_count; /* how many pages reference this context, |
eric@22 | 57 | including those from subcontexts */ |
eric@22 | 58 | |
eric@22 | 59 | char *input_file; |
eric@22 | 60 | |
eric@22 | 61 | input_modifiers_t modifiers [INPUT_MODIFIER_TYPE_COUNT]; |
eric@22 | 62 | } input_context_t; |
eric@22 | 63 | |
eric@22 | 64 | |
eric@22 | 65 | typedef struct input_image_t |
eric@22 | 66 | { |
eric@22 | 67 | struct input_image_t *next; |
eric@22 | 68 | input_context_t *input_context; |
eric@22 | 69 | range_t range; |
eric@22 | 70 | } input_image_t; |
eric@22 | 71 | |
eric@22 | 72 | |
eric@22 | 73 | typedef struct output_context_t |
eric@22 | 74 | { |
eric@22 | 75 | struct output_context_t *parent; |
eric@22 | 76 | struct output_context_t *next; |
eric@22 | 77 | |
eric@22 | 78 | int page_count; /* how many pages reference this context, |
eric@22 | 79 | including those from subcontexts */ |
eric@22 | 80 | |
eric@22 | 81 | char *output_file; |
eric@30 | 82 | pdf_file_attributes_t file_attributes; |
eric@30 | 83 | |
eric@22 | 84 | bookmark_t *first_bookmark; |
eric@22 | 85 | bookmark_t *last_bookmark; |
eric@25 | 86 | |
eric@48 | 87 | bool has_page_label; |
eric@25 | 88 | page_label_t page_label; |
eric@22 | 89 | } output_context_t; |
eric@22 | 90 | |
eric@22 | 91 | |
eric@22 | 92 | typedef struct output_page_t |
eric@22 | 93 | { |
eric@22 | 94 | struct output_page_t *next; |
eric@22 | 95 | output_context_t *output_context; |
eric@22 | 96 | range_t range; |
eric@22 | 97 | bookmark_t *bookmark_list; |
eric@22 | 98 | } output_page_t; |
eric@22 | 99 | |
eric@22 | 100 | |
eric@36 | 101 | #undef SEMANTIC_DEBUG |
eric@36 | 102 | |
eric@19 | 103 | #ifdef SEMANTIC_DEBUG |
eric@19 | 104 | #define SDBG(x) printf x |
eric@19 | 105 | #else |
eric@19 | 106 | #define SDBG(x) |
eric@19 | 107 | #endif |
eric@19 | 108 | |
eric@19 | 109 | |
eric@16 | 110 | FILE *yyin; |
eric@16 | 111 | int line; /* line number in spec file */ |
eric@16 | 112 | |
eric@24 | 113 | int bookmark_level; |
eric@16 | 114 | |
eric@20 | 115 | input_context_t *first_input_context; |
eric@20 | 116 | input_context_t *last_input_context; |
eric@20 | 117 | |
eric@20 | 118 | input_modifier_type_t current_modifier_context; |
eric@18 | 119 | |
eric@20 | 120 | input_image_t *first_input_image; |
eric@20 | 121 | input_image_t *last_input_image; |
eric@18 | 122 | |
eric@20 | 123 | output_context_t *first_output_context; |
eric@20 | 124 | output_context_t *last_output_context; |
eric@20 | 125 | |
eric@20 | 126 | output_page_t *first_output_page; |
eric@20 | 127 | output_page_t *last_output_page; |
eric@16 | 128 | |
eric@16 | 129 | |
eric@19 | 130 | void input_push_context (void) |
eric@16 | 131 | { |
eric@18 | 132 | input_context_t *new_input_context; |
eric@18 | 133 | |
eric@18 | 134 | new_input_context = malloc (sizeof (input_context_t)); |
eric@18 | 135 | if (! new_input_context) |
eric@18 | 136 | { |
eric@20 | 137 | fprintf (stderr, "failed to malloc an input context\n"); |
eric@18 | 138 | return; |
eric@18 | 139 | } |
eric@18 | 140 | |
eric@20 | 141 | if (last_input_context) |
eric@18 | 142 | { |
eric@20 | 143 | memcpy (new_input_context, last_input_context, sizeof (input_context_t)); |
eric@20 | 144 | new_input_context->image_count = 0; |
eric@18 | 145 | } |
eric@18 | 146 | else |
eric@20 | 147 | { |
eric@20 | 148 | memset (new_input_context, 0, sizeof (input_context_t)); |
eric@20 | 149 | first_input_context = new_input_context; |
eric@20 | 150 | } |
eric@18 | 151 | |
eric@20 | 152 | new_input_context->parent = last_input_context; |
eric@20 | 153 | last_input_context = new_input_context; |
eric@16 | 154 | }; |
eric@16 | 155 | |
eric@16 | 156 | void input_pop_context (void) |
eric@16 | 157 | { |
eric@20 | 158 | if (! last_input_context) |
eric@18 | 159 | { |
eric@18 | 160 | fprintf (stderr, "failed to pop an input context\n"); |
eric@18 | 161 | return; |
eric@18 | 162 | } |
eric@18 | 163 | |
eric@20 | 164 | last_input_context = last_input_context->parent; |
eric@16 | 165 | }; |
eric@16 | 166 | |
eric@19 | 167 | void input_set_modifier_context (input_modifier_type_t type) |
eric@19 | 168 | { |
eric@19 | 169 | current_modifier_context = type; |
eric@19 | 170 | #ifdef SEMANTIC_DEBUG |
eric@19 | 171 | SDBG(("modifier type ")); |
eric@19 | 172 | switch (type) |
eric@19 | 173 | { |
eric@19 | 174 | case INPUT_MODIFIER_ALL: SDBG(("all")); break; |
eric@19 | 175 | case INPUT_MODIFIER_ODD: SDBG(("odd")); break; |
eric@19 | 176 | case INPUT_MODIFIER_EVEN: SDBG(("even")); break; |
eric@19 | 177 | default: SDBG(("unknown %d", type)); |
eric@19 | 178 | } |
eric@19 | 179 | SDBG(("\n")); |
eric@19 | 180 | #endif /* SEMANTIC_DEBUG */ |
eric@19 | 181 | } |
eric@19 | 182 | |
eric@22 | 183 | static void input_clone (void) |
eric@20 | 184 | { |
eric@20 | 185 | input_context_t *new_input_context; |
eric@20 | 186 | |
eric@20 | 187 | if (! last_input_context->image_count) |
eric@20 | 188 | return; |
eric@20 | 189 | |
eric@20 | 190 | new_input_context = malloc (sizeof (input_context_t)); |
eric@20 | 191 | if (! new_input_context) |
eric@20 | 192 | { |
eric@20 | 193 | fprintf (stderr, "failed to malloc an input context\n"); |
eric@20 | 194 | return; |
eric@20 | 195 | } |
eric@20 | 196 | |
eric@20 | 197 | memcpy (new_input_context, last_input_context, sizeof (input_context_t)); |
eric@20 | 198 | new_input_context->image_count = 0; |
eric@20 | 199 | last_input_context->next = new_input_context; |
eric@45 | 200 | last_input_context = new_input_context; |
eric@20 | 201 | } |
eric@20 | 202 | |
eric@16 | 203 | void input_set_file (char *name) |
eric@16 | 204 | { |
eric@20 | 205 | input_clone (); |
eric@20 | 206 | last_input_context->input_file = name; |
eric@16 | 207 | }; |
eric@16 | 208 | |
eric@19 | 209 | void input_set_rotation (int rotation) |
eric@19 | 210 | { |
eric@20 | 211 | last_input_context->modifiers [current_modifier_context].has_rotation = 1; |
eric@20 | 212 | last_input_context->modifiers [current_modifier_context].rotation = rotation; |
eric@19 | 213 | SDBG(("rotation %d\n", rotation)); |
eric@19 | 214 | } |
eric@19 | 215 | |
eric@32 | 216 | void input_set_page_size (page_size_t size) |
eric@32 | 217 | { |
eric@32 | 218 | last_input_context->modifiers [current_modifier_context].has_page_size = 1; |
eric@32 | 219 | last_input_context->modifiers [current_modifier_context].page_size = size; |
eric@32 | 220 | SDBG(("page size %f, %f\n", size.width, size.height)); |
eric@32 | 221 | } |
eric@32 | 222 | |
eric@22 | 223 | static void increment_input_image_count (int count) |
eric@16 | 224 | { |
eric@20 | 225 | input_context_t *context; |
eric@20 | 226 | |
eric@20 | 227 | for (context = last_input_context; context; context = context->parent) |
eric@20 | 228 | context->image_count += count; |
eric@20 | 229 | } |
eric@20 | 230 | |
eric@20 | 231 | void input_images (range_t range) |
eric@20 | 232 | { |
eric@20 | 233 | input_image_t *new_image; |
eric@20 | 234 | int count = ((range.last - range.first) + 1); |
eric@20 | 235 | |
eric@19 | 236 | #ifdef SEMANTIC_DEBUG |
eric@20 | 237 | if (range.first == range.last) |
eric@20 | 238 | SDBG(("image %d\n", range.first)); |
eric@16 | 239 | else |
eric@20 | 240 | SDBG(("images %d..%d\n", range.first, range.last)); |
eric@19 | 241 | #endif /* SEMANTIC_DEBUG */ |
eric@20 | 242 | |
eric@20 | 243 | new_image = calloc (1, sizeof (input_image_t)); |
eric@20 | 244 | if (! new_image) |
eric@20 | 245 | { |
eric@20 | 246 | fprintf (stderr, "failed to malloc an input image struct\n"); |
eric@20 | 247 | return; |
eric@20 | 248 | } |
eric@20 | 249 | if (first_input_image) |
eric@20 | 250 | { |
eric@20 | 251 | last_input_image->next = new_image; |
eric@20 | 252 | last_input_image = new_image; |
eric@20 | 253 | } |
eric@20 | 254 | else |
eric@20 | 255 | { |
eric@20 | 256 | first_input_image = last_input_image = new_image; |
eric@20 | 257 | } |
eric@20 | 258 | new_image->range = range; |
eric@20 | 259 | new_image->input_context = last_input_context; |
eric@20 | 260 | increment_input_image_count (count); |
eric@16 | 261 | } |
eric@16 | 262 | |
eric@18 | 263 | |
eric@18 | 264 | void output_push_context (void) |
eric@18 | 265 | { |
eric@20 | 266 | output_context_t *new_output_context; |
eric@20 | 267 | |
eric@20 | 268 | new_output_context = malloc (sizeof (output_context_t)); |
eric@20 | 269 | if (! new_output_context) |
eric@20 | 270 | { |
eric@20 | 271 | fprintf (stderr, "failed to malloc an output context\n"); |
eric@20 | 272 | return; |
eric@20 | 273 | } |
eric@20 | 274 | |
eric@20 | 275 | if (last_output_context) |
eric@20 | 276 | { |
eric@20 | 277 | memcpy (new_output_context, last_output_context, sizeof (output_context_t)); |
eric@20 | 278 | new_output_context->page_count = 0; |
eric@21 | 279 | new_output_context->first_bookmark = NULL; |
eric@21 | 280 | new_output_context->last_bookmark = NULL; |
eric@20 | 281 | } |
eric@20 | 282 | else |
eric@20 | 283 | { |
eric@20 | 284 | memset (new_output_context, 0, sizeof (output_context_t)); |
eric@20 | 285 | first_output_context = new_output_context; |
eric@20 | 286 | } |
eric@20 | 287 | |
eric@20 | 288 | new_output_context->parent = last_output_context; |
eric@20 | 289 | last_output_context = new_output_context; |
eric@18 | 290 | }; |
eric@18 | 291 | |
eric@20 | 292 | void output_pop_context (void) |
eric@20 | 293 | { |
eric@20 | 294 | if (! last_output_context) |
eric@20 | 295 | { |
eric@20 | 296 | fprintf (stderr, "failed to pop an output context\n"); |
eric@20 | 297 | return; |
eric@20 | 298 | } |
eric@20 | 299 | |
eric@20 | 300 | last_output_context = last_output_context->parent; |
eric@20 | 301 | }; |
eric@20 | 302 | |
eric@22 | 303 | static void output_clone (void) |
eric@20 | 304 | { |
eric@20 | 305 | output_context_t *new_output_context; |
eric@20 | 306 | |
eric@20 | 307 | if (! last_output_context->page_count) |
eric@20 | 308 | return; |
eric@20 | 309 | |
eric@20 | 310 | new_output_context = malloc (sizeof (output_context_t)); |
eric@20 | 311 | if (! new_output_context) |
eric@20 | 312 | { |
eric@20 | 313 | fprintf (stderr, "failed to malloc an output context\n"); |
eric@20 | 314 | return; |
eric@20 | 315 | } |
eric@20 | 316 | |
eric@20 | 317 | memcpy (new_output_context, last_output_context, sizeof (output_context_t)); |
eric@20 | 318 | new_output_context->page_count = 0; |
eric@20 | 319 | last_output_context->next = new_output_context; |
eric@20 | 320 | } |
eric@20 | 321 | |
eric@16 | 322 | void output_set_file (char *name) |
eric@16 | 323 | { |
eric@20 | 324 | output_clone (); |
eric@20 | 325 | last_output_context->output_file = name; |
eric@30 | 326 | last_output_context->file_attributes.author = NULL; |
eric@30 | 327 | last_output_context->file_attributes.creator = NULL; |
eric@30 | 328 | last_output_context->file_attributes.title = NULL; |
eric@30 | 329 | last_output_context->file_attributes.subject = NULL; |
eric@30 | 330 | last_output_context->file_attributes.keywords = NULL; |
eric@16 | 331 | }; |
eric@16 | 332 | |
eric@30 | 333 | void output_set_author (char *author) |
eric@30 | 334 | { |
eric@30 | 335 | last_output_context->file_attributes.author = author; |
eric@30 | 336 | } |
eric@30 | 337 | |
eric@30 | 338 | void output_set_creator (char *creator) |
eric@30 | 339 | { |
eric@30 | 340 | last_output_context->file_attributes.creator = creator; |
eric@30 | 341 | } |
eric@30 | 342 | |
eric@30 | 343 | void output_set_title (char *title) |
eric@30 | 344 | { |
eric@30 | 345 | last_output_context->file_attributes.title = title; |
eric@30 | 346 | } |
eric@30 | 347 | |
eric@30 | 348 | void output_set_subject (char *subject) |
eric@30 | 349 | { |
eric@30 | 350 | last_output_context->file_attributes.subject = subject; |
eric@30 | 351 | } |
eric@30 | 352 | |
eric@30 | 353 | void output_set_keywords (char *keywords) |
eric@30 | 354 | { |
eric@30 | 355 | last_output_context->file_attributes.keywords = keywords; |
eric@30 | 356 | } |
eric@30 | 357 | |
eric@20 | 358 | void output_set_bookmark (char *name) |
eric@16 | 359 | { |
eric@20 | 360 | bookmark_t *new_bookmark; |
eric@20 | 361 | |
eric@20 | 362 | /* As the language is defined (parser.y), a bookmark can only appear |
eric@20 | 363 | at the beginning of a context! */ |
eric@20 | 364 | if (last_output_context->page_count) |
eric@20 | 365 | { |
eric@20 | 366 | fprintf (stderr, "internal error, bookmark not at beginning of context\n"); |
eric@20 | 367 | exit (2); |
eric@20 | 368 | } |
eric@20 | 369 | |
eric@20 | 370 | new_bookmark = calloc (1, sizeof (bookmark_t)); |
eric@20 | 371 | if (! new_bookmark) |
eric@20 | 372 | { |
eric@20 | 373 | fprintf (stderr, "failed to calloc a bookmark\n"); |
eric@20 | 374 | return; |
eric@20 | 375 | } |
eric@20 | 376 | |
eric@24 | 377 | new_bookmark->level = bookmark_level; |
eric@20 | 378 | new_bookmark->name = name; |
eric@20 | 379 | if (last_output_context->first_bookmark) |
eric@20 | 380 | last_output_context->last_bookmark->next = new_bookmark; |
eric@20 | 381 | else |
eric@20 | 382 | last_output_context->first_bookmark = new_bookmark; |
eric@20 | 383 | last_output_context->last_bookmark = new_bookmark; |
eric@20 | 384 | } |
eric@20 | 385 | |
eric@25 | 386 | void output_set_page_label (page_label_t label) |
eric@20 | 387 | { |
eric@20 | 388 | output_clone (); |
eric@25 | 389 | last_output_context->has_page_label = 1; |
eric@25 | 390 | last_output_context->page_label = label; |
eric@20 | 391 | } |
eric@20 | 392 | |
eric@22 | 393 | static void increment_output_page_count (int count) |
eric@20 | 394 | { |
eric@20 | 395 | output_context_t *context; |
eric@20 | 396 | |
eric@20 | 397 | for (context = last_output_context; context; context = context->parent) |
eric@20 | 398 | context->page_count += count; |
eric@20 | 399 | } |
eric@20 | 400 | |
eric@21 | 401 | |
eric@20 | 402 | void output_pages (range_t range) |
eric@20 | 403 | { |
eric@20 | 404 | output_page_t *new_page; |
eric@21 | 405 | output_context_t *context; |
eric@20 | 406 | int count = ((range.last - range.first) + 1); |
eric@20 | 407 | |
eric@19 | 408 | #ifdef SEMANTIC_DEBUG |
eric@20 | 409 | if (range.first == range.last) |
eric@20 | 410 | SDBG(("page %d\n", range.first)); |
eric@16 | 411 | else |
eric@20 | 412 | SDBG(("pages %d..%d\n", range.first, range.last)); |
eric@19 | 413 | #endif /* SEMANTIC_DEBUG */ |
eric@20 | 414 | |
eric@20 | 415 | new_page = calloc (1, sizeof (output_page_t)); |
eric@20 | 416 | if (! new_page) |
eric@20 | 417 | { |
eric@20 | 418 | fprintf (stderr, "failed to malloc an output page struct\n"); |
eric@20 | 419 | return; |
eric@20 | 420 | } |
eric@20 | 421 | if (first_output_page) |
eric@20 | 422 | { |
eric@20 | 423 | last_output_page->next = new_page; |
eric@20 | 424 | last_output_page = new_page; |
eric@20 | 425 | } |
eric@20 | 426 | else |
eric@20 | 427 | { |
eric@20 | 428 | first_output_page = last_output_page = new_page; |
eric@20 | 429 | } |
eric@20 | 430 | new_page->range = range; |
eric@20 | 431 | new_page->output_context = last_output_context; |
eric@20 | 432 | |
eric@21 | 433 | /* transfer bookmarks from context(s) to page */ |
eric@21 | 434 | for (context = last_output_context; context; context = context->parent) |
eric@21 | 435 | if (context->first_bookmark) |
eric@21 | 436 | { |
eric@21 | 437 | context->last_bookmark->next = new_page->bookmark_list; |
eric@21 | 438 | new_page->bookmark_list = context->first_bookmark; |
eric@21 | 439 | context->first_bookmark = NULL; |
eric@21 | 440 | context->last_bookmark = NULL; |
eric@21 | 441 | } |
eric@20 | 442 | |
eric@20 | 443 | increment_output_page_count (count); |
eric@16 | 444 | } |
eric@16 | 445 | |
eric@16 | 446 | |
eric@16 | 447 | void yyerror (char *s) |
eric@16 | 448 | { |
eric@16 | 449 | fprintf (stderr, "%d: %s\n", line, s); |
eric@16 | 450 | } |
eric@16 | 451 | |
eric@16 | 452 | |
eric@55 | 453 | static char *get_input_filename (input_context_t *context) |
eric@20 | 454 | { |
eric@20 | 455 | for (; context; context = context->parent) |
eric@20 | 456 | if (context->input_file) |
eric@20 | 457 | return (context->input_file); |
eric@20 | 458 | fprintf (stderr, "no input file name found\n"); |
eric@20 | 459 | exit (2); |
eric@20 | 460 | } |
eric@20 | 461 | |
eric@48 | 462 | static bool get_input_rotation (input_context_t *context, |
eric@48 | 463 | input_modifier_type_t type, |
eric@48 | 464 | int *rotation) |
eric@20 | 465 | { |
eric@20 | 466 | for (; context; context = context->parent) |
eric@20 | 467 | { |
eric@20 | 468 | if (context->modifiers [type].has_rotation) |
eric@32 | 469 | { |
eric@32 | 470 | * rotation = context->modifiers [type].rotation; |
eric@32 | 471 | return (1); |
eric@32 | 472 | } |
eric@20 | 473 | if (context->modifiers [INPUT_MODIFIER_ALL].has_rotation) |
eric@32 | 474 | { |
eric@32 | 475 | * rotation = context->modifiers [INPUT_MODIFIER_ALL].rotation; |
eric@32 | 476 | return (1); |
eric@32 | 477 | } |
eric@32 | 478 | } |
eric@32 | 479 | return (0); /* default */ |
eric@32 | 480 | } |
eric@32 | 481 | |
eric@48 | 482 | static bool get_input_page_size (input_context_t *context, |
eric@48 | 483 | input_modifier_type_t type, |
eric@48 | 484 | page_size_t *page_size) |
eric@32 | 485 | { |
eric@32 | 486 | for (; context; context = context->parent) |
eric@32 | 487 | { |
eric@32 | 488 | if (context->modifiers [type].has_page_size) |
eric@32 | 489 | { |
eric@32 | 490 | * page_size = context->modifiers [type].page_size; |
eric@32 | 491 | return (1); |
eric@32 | 492 | } |
eric@32 | 493 | if (context->modifiers [INPUT_MODIFIER_ALL].has_page_size) |
eric@32 | 494 | { |
eric@32 | 495 | * page_size = context->modifiers [INPUT_MODIFIER_ALL].page_size; |
eric@32 | 496 | return (1); |
eric@32 | 497 | } |
eric@20 | 498 | } |
eric@20 | 499 | return (0); /* default */ |
eric@20 | 500 | } |
eric@20 | 501 | |
eric@55 | 502 | static char *get_output_filename (output_context_t *context) |
eric@20 | 503 | { |
eric@20 | 504 | for (; context; context = context->parent) |
eric@20 | 505 | if (context->output_file) |
eric@20 | 506 | return (context->output_file); |
eric@30 | 507 | fprintf (stderr, "no output file found\n"); |
eric@30 | 508 | exit (2); |
eric@30 | 509 | } |
eric@30 | 510 | |
eric@30 | 511 | static pdf_file_attributes_t *get_output_file_attributes (output_context_t *context) |
eric@30 | 512 | { |
eric@30 | 513 | for (; context; context = context->parent) |
eric@30 | 514 | if (context->output_file) |
eric@30 | 515 | return (& context->file_attributes); |
eric@30 | 516 | fprintf (stderr, "no output file found\n"); |
eric@20 | 517 | exit (2); |
eric@20 | 518 | } |
eric@20 | 519 | |
eric@25 | 520 | static page_label_t *get_output_page_label (output_context_t *context) |
eric@20 | 521 | { |
eric@20 | 522 | for (; context; context = context->parent) |
eric@25 | 523 | if (context->has_page_label) |
eric@25 | 524 | return (& context->page_label); |
eric@20 | 525 | return (NULL); /* default */ |
eric@20 | 526 | } |
eric@20 | 527 | |
eric@20 | 528 | |
eric@20 | 529 | #ifdef SEMANTIC_DEBUG |
eric@20 | 530 | void dump_input_tree (void) |
eric@20 | 531 | { |
eric@20 | 532 | input_image_t *image; |
eric@20 | 533 | int i; |
eric@20 | 534 | |
eric@20 | 535 | printf ("input images:\n"); |
eric@20 | 536 | for (image = first_input_image; image; image = image->next) |
eric@20 | 537 | for (i = image->range.first; i <= image->range.last; i++) |
eric@20 | 538 | { |
eric@20 | 539 | input_modifier_type_t parity = (i % 2) ? INPUT_MODIFIER_ODD : INPUT_MODIFIER_EVEN; |
eric@48 | 540 | bool has_rotation, has_page_size; |
eric@32 | 541 | int rotation; |
eric@32 | 542 | page_size_t page_size; |
eric@32 | 543 | |
eric@32 | 544 | has_rotation = get_input_rotation (image->input_context, |
eric@32 | 545 | parity, |
eric@32 | 546 | & rotation); |
eric@32 | 547 | has_page_size = get_input_page_size (image->input_context, |
eric@32 | 548 | parity, |
eric@32 | 549 | & page_size); |
eric@32 | 550 | printf ("file '%s' image %d", |
eric@55 | 551 | get_input_filename (image->input_context), |
eric@32 | 552 | i); |
eric@32 | 553 | if (has_rotation) |
eric@32 | 554 | printf (" rotation %d", rotation); |
eric@32 | 555 | if (has_page_size) |
eric@32 | 556 | printf (" size %f, %f", page_size.width, page_size.height); |
eric@32 | 557 | printf ("\n"); |
eric@45 | 558 | printf ("context: %08x\n", image->input_context); |
eric@20 | 559 | } |
eric@20 | 560 | } |
eric@20 | 561 | |
eric@20 | 562 | void dump_output_tree (void) |
eric@20 | 563 | { |
eric@20 | 564 | int i; |
eric@20 | 565 | output_page_t *page; |
eric@20 | 566 | bookmark_t *bookmark; |
eric@20 | 567 | |
eric@20 | 568 | printf ("output pages:\n"); |
eric@20 | 569 | for (page = first_output_page; page; page = page->next) |
eric@20 | 570 | { |
eric@20 | 571 | if (page->bookmark_list) |
eric@20 | 572 | { |
eric@20 | 573 | for (bookmark = page->bookmark_list; bookmark; bookmark = bookmark->next) |
eric@25 | 574 | printf ("bookmark %d \"%s\"\n", bookmark->level, bookmark->name); |
eric@20 | 575 | } |
eric@20 | 576 | for (i = page->range.first; i <= page->range.last; i++) |
eric@20 | 577 | { |
eric@25 | 578 | page_label_t *label = get_output_page_label (page->output_context); |
eric@55 | 579 | printf ("file \"%s\" ", get_output_filename (page->output_context)); |
eric@25 | 580 | if (label) |
eric@25 | 581 | { |
eric@25 | 582 | printf ("label "); |
eric@25 | 583 | if (label->prefix) |
eric@25 | 584 | printf ("\"%s\" ", label->prefix); |
eric@25 | 585 | if (label->style) |
eric@25 | 586 | printf ("'%c' ", label->style); |
eric@25 | 587 | } |
eric@20 | 588 | printf ("page %d\n", i); |
eric@20 | 589 | } |
eric@20 | 590 | } |
eric@20 | 591 | } |
eric@20 | 592 | #endif /* SEMANTIC_DEBUG */ |
eric@20 | 593 | |
eric@25 | 594 | |
eric@25 | 595 | static inline int range_count (range_t range) |
eric@25 | 596 | { |
eric@25 | 597 | return ((range.last - range.first) + 1); |
eric@25 | 598 | } |
eric@25 | 599 | |
eric@25 | 600 | |
eric@48 | 601 | bool parse_spec_file (char *fn) |
eric@16 | 602 | { |
eric@48 | 603 | bool result = 0; |
eric@16 | 604 | |
eric@16 | 605 | yyin = fopen (fn, "r"); |
eric@16 | 606 | if (! yyin) |
eric@16 | 607 | { |
eric@16 | 608 | fprintf (stderr, "can't open spec file '%s'\n", fn); |
eric@16 | 609 | goto fail; |
eric@16 | 610 | } |
eric@16 | 611 | |
eric@16 | 612 | line = 1; |
eric@16 | 613 | |
eric@20 | 614 | input_push_context (); /* create root input context */ |
eric@20 | 615 | input_push_context (); /* create inital input context */ |
eric@20 | 616 | |
eric@20 | 617 | output_push_context (); /* create root output context */ |
eric@18 | 618 | output_push_context (); /* create initial output context */ |
eric@18 | 619 | |
eric@16 | 620 | yyparse (); |
eric@16 | 621 | |
eric@20 | 622 | if (first_input_context->image_count != first_output_context->page_count) |
eric@18 | 623 | { |
eric@20 | 624 | fprintf (stderr, "input image count %d != output page count %d\n", |
eric@20 | 625 | first_input_context->image_count, |
eric@20 | 626 | first_output_context->page_count); |
eric@18 | 627 | goto fail; |
eric@18 | 628 | } |
eric@18 | 629 | |
eric@20 | 630 | fprintf (stderr, "%d pages specified\n", first_input_context->image_count); |
eric@18 | 631 | |
eric@16 | 632 | result = 1; |
eric@16 | 633 | |
eric@20 | 634 | #ifdef SEMANTIC_DEBUG |
eric@20 | 635 | dump_input_tree (); |
eric@20 | 636 | dump_output_tree (); |
eric@20 | 637 | #endif /* SEMANTIC_DEBUG */ |
eric@20 | 638 | |
eric@16 | 639 | fail: |
eric@16 | 640 | if (yyin) |
eric@16 | 641 | fclose (yyin); |
eric@16 | 642 | |
eric@16 | 643 | return (result); |
eric@16 | 644 | } |
eric@26 | 645 | |
eric@26 | 646 | |
eric@48 | 647 | bool process_specs (void) |
eric@26 | 648 | { |
eric@26 | 649 | input_image_t *image = NULL; |
eric@26 | 650 | output_page_t *page = NULL; |
eric@26 | 651 | int i = 0; |
eric@26 | 652 | int p = 0; |
eric@26 | 653 | int page_index = 0; |
eric@26 | 654 | input_attributes_t input_attributes; |
eric@26 | 655 | input_modifier_type_t parity; |
eric@26 | 656 | page_label_t *page_label; |
eric@26 | 657 | |
eric@26 | 658 | for (;;) |
eric@26 | 659 | { |
eric@26 | 660 | if ((! image) || (i >= range_count (image->range))) |
eric@26 | 661 | { |
eric@55 | 662 | char *input_fn; |
eric@26 | 663 | if (image) |
eric@26 | 664 | image = image->next; |
eric@26 | 665 | else |
eric@26 | 666 | image = first_input_image; |
eric@26 | 667 | if (! image) |
eric@55 | 668 | return (1); /* done */ |
eric@26 | 669 | i = 0; |
eric@55 | 670 | input_fn = get_input_filename (image->input_context); |
eric@55 | 671 | if (verbose) |
eric@55 | 672 | fprintf (stderr, "opening TIFF file '%s'\n", input_fn); |
eric@55 | 673 | if (! open_tiff_input_file (input_fn)) |
eric@55 | 674 | { |
eric@55 | 675 | fprintf (stderr, "error opening TIFF file '%s'\n", input_fn); |
eric@55 | 676 | return (0); |
eric@55 | 677 | } |
eric@26 | 678 | } |
eric@26 | 679 | |
eric@26 | 680 | if ((! page) || (p >= range_count (page->range))) |
eric@26 | 681 | { |
eric@55 | 682 | char *output_fn; |
eric@26 | 683 | if (page) |
eric@26 | 684 | page = page->next; |
eric@26 | 685 | else |
eric@26 | 686 | page = first_output_page; |
eric@26 | 687 | p = 0; |
eric@55 | 688 | output_fn = get_output_filename (page->output_context); |
eric@55 | 689 | if (verbose) |
eric@55 | 690 | fprintf (stderr, "opening PDF file '%s'\n", output_fn); |
eric@55 | 691 | if (! open_pdf_output_file (output_fn, |
eric@30 | 692 | get_output_file_attributes (page->output_context))) |
eric@55 | 693 | { |
eric@55 | 694 | fprintf (stderr, "error opening PDF file '%s'\n", output_fn); |
eric@55 | 695 | return (0); |
eric@55 | 696 | } |
eric@26 | 697 | page_label = get_output_page_label (page->output_context); |
eric@26 | 698 | process_page_numbers (page_index, |
eric@26 | 699 | range_count (page->range), |
eric@26 | 700 | page->range.first, |
eric@26 | 701 | page_label); |
eric@26 | 702 | } |
eric@26 | 703 | |
eric@26 | 704 | parity = ((image->range.first + i) % 2) ? INPUT_MODIFIER_ODD : INPUT_MODIFIER_EVEN; |
eric@26 | 705 | |
eric@26 | 706 | memset (& input_attributes, 0, sizeof (input_attributes)); |
eric@32 | 707 | |
eric@32 | 708 | input_attributes.rotation = 0; |
eric@32 | 709 | input_attributes.has_rotation = get_input_rotation (image->input_context, |
eric@32 | 710 | parity, |
eric@32 | 711 | & input_attributes.rotation); |
eric@32 | 712 | |
eric@32 | 713 | input_attributes.has_page_size = get_input_page_size (image->input_context, |
eric@32 | 714 | parity, |
eric@32 | 715 | & input_attributes.page_size); |
eric@26 | 716 | |
eric@55 | 717 | if (verbose) |
eric@55 | 718 | fprintf (stderr, "processing image %d\n", image->range.first + i); |
eric@49 | 719 | if (! process_page (image->range.first + i, |
eric@49 | 720 | input_attributes, |
eric@49 | 721 | page->bookmark_list)) |
eric@55 | 722 | { |
eric@55 | 723 | fprintf (stderr, "error processing image %d\n", image->range.first + i); |
eric@55 | 724 | return (0); |
eric@55 | 725 | } |
eric@26 | 726 | i++; |
eric@26 | 727 | p++; |
eric@26 | 728 | page_index++; |
eric@26 | 729 | } |
eric@26 | 730 | } |