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