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