Back to index

tetex-bin  3.0
epdf.c
Go to the documentation of this file.
00001 /*  $Header$
00002 
00003     This is dvipdfm, a DVI to PDF translator.
00004     Copyright (C) 1998, 1999 by Mark A. Wicks
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019     
00020     The author may be contacted via the e-mail address
00021 
00022        mwicks@kettering.edu
00023 */
00024 
00025 #include "system.h"
00026 #include "mem.h"
00027 #include "mfileio.h"
00028 #include "pdfobj.h"
00029 #include "pdfdoc.h"
00030 #include "pdfspecial.h"
00031 #include "epdf.h"
00032 
00033 pdf_obj *pdf_include_page(FILE *image_file, struct xform_info *p, char *res_name)
00034 {
00035   pdf_obj *trailer = NULL, *catalog = NULL, *page_tree = NULL;
00036   pdf_obj *kids_ref, *kids;
00037   pdf_obj *media_box = NULL, *crop_box = NULL, *resources = NULL,
00038     *contents = NULL, *contents_ref = NULL;
00039   pdf_obj *tmp1;
00040 #ifdef MEM_DEBUG
00041 MEM_START
00042 #endif
00043   if (!(trailer = pdf_open (image_file))) {
00044     fprintf (stderr, "\nCorrupt PDF file?\n");
00045   }
00046  
00047   /* Now just lookup catalog location */
00048   /* Deref catalog */
00049   if (trailer && (catalog = pdf_deref_obj(pdf_lookup_dict (trailer,"Root"))) ==
00050       NULL) {
00051     fprintf (stderr, "\nCatalog isn't where I expect it.\n");
00052   }
00053   if (trailer)
00054     pdf_release_obj (trailer);
00055 
00056   /* Lookup page tree in catalog */
00057   if (catalog) {
00058     page_tree = pdf_deref_obj (pdf_lookup_dict (catalog, "Pages"));
00059     /* Should be finished with catalog */
00060     pdf_release_obj (catalog);
00061   }
00062   /* Media box and resources can be inherited so start looking for
00063      them here */
00064   if (page_tree) {
00065     if ((tmp1 = pdf_lookup_dict (page_tree, "CropBox")))
00066       crop_box = pdf_deref_obj (tmp1);
00067     if ((tmp1 = pdf_lookup_dict (page_tree, "MediaBox")))
00068       media_box = pdf_deref_obj (tmp1);
00069     resources = pdf_deref_obj (pdf_lookup_dict (page_tree, "Resources"));
00070     if (resources == NULL) {
00071       resources = pdf_new_dict();
00072     }
00073     while ((kids_ref = pdf_lookup_dict (page_tree, "Kids")) != NULL) {
00074       kids = pdf_deref_obj (kids_ref);
00075       pdf_release_obj (page_tree);
00076       page_tree = pdf_deref_obj (pdf_get_array(kids, 0));
00077       pdf_release_obj (kids);
00078       /* Replace MediaBox if it's here */
00079       tmp1 = pdf_deref_obj(pdf_lookup_dict (page_tree, "MediaBox"));
00080       if (tmp1 && media_box)
00081        pdf_release_obj (media_box);
00082       if (tmp1) 
00083        media_box = tmp1;
00084       /* Do same for CropBox */
00085       tmp1 = pdf_deref_obj(pdf_lookup_dict (page_tree, "CropBox"));
00086       if (tmp1 && crop_box)
00087        pdf_release_obj (crop_box);
00088       if (tmp1) 
00089        crop_box = tmp1;
00090       /* Add resources if they're here */
00091       tmp1 = pdf_deref_obj (pdf_lookup_dict (page_tree, "Resources"));
00092       if (tmp1) {
00093        pdf_merge_dict (tmp1, resources);
00094        pdf_release_obj (resources);
00095        resources = tmp1;
00096       }
00097     }
00098     /* At this point, page_tree contains the first page.  media_box,
00099        crop_box,  and resources should also be set. */
00100     /* If there's a crop_box, replace media_box with crop_box.
00101        The rest of this routine assumes crop_box has been released */
00102     if (crop_box) {
00103       pdf_release_obj (media_box);
00104       media_box = crop_box;
00105       crop_box = NULL;
00106     }
00107     /* This gets bit confusing.  In the following code,
00108        media_box is the box the image is cropped to. 
00109        The bounding box is the box the image is scaled to */
00110     /* If user did not supply bounding box, use media_box
00111        (which may really be cropbox) as bounding box */
00112     /* Set the crop box parameters in the xform_info structure */
00113     p->c_llx = pdf_number_value (pdf_get_array (media_box, 0));
00114     p->c_lly = pdf_number_value (pdf_get_array (media_box, 1));
00115     p->c_urx = pdf_number_value (pdf_get_array (media_box, 2));
00116     p->c_ury = pdf_number_value (pdf_get_array (media_box, 3));
00117 
00118     /* Adjust scaling and clipping information as necessary */
00119     pdf_scale_image (p);
00120 
00121     /* Set the media box to whatever pdf_scale_image() decided
00122        for the crop box (which may be unchanged) */
00123     pdf_release_obj (media_box);
00124     media_box = pdf_new_array ();
00125     pdf_add_array (media_box, pdf_new_number (p->c_llx));
00126     pdf_add_array (media_box, pdf_new_number (p->c_lly));
00127     pdf_add_array (media_box, pdf_new_number (p->c_urx));
00128     pdf_add_array (media_box, pdf_new_number (p->c_ury));
00129 
00130     if ((contents =
00131         pdf_deref_obj(pdf_lookup_dict(page_tree,"Contents")))==NULL) {
00132       fprintf (stderr, "\nNo Contents found\n");
00133       return NULL;
00134     }
00135     pdf_release_obj (page_tree);
00136   }
00137   /* Arrays of contents must be handled very differently (not implemented) */
00138   if (contents && contents -> type != PDF_ARRAY) {
00139     doc_make_form_xobj (contents, media_box,
00140                      p->user_bbox? p->u_llx: 0.0,
00141                      p->user_bbox? p->u_lly: 0.0,
00142                      1.0, 1.0,
00143                      resources, res_name);
00144   } else {
00145     fprintf (stderr, "\nIgnoring stream with with multiple segments\n");
00146     contents = NULL;
00147   }
00148   if (contents) {
00149     contents_ref = pdf_ref_obj (contents);
00150     pdf_release_obj (contents);
00151   }
00152   pdf_close ();
00153 #ifdef MEM_DEBUG
00154 MEM_END
00155 #endif
00156   return (contents_ref);
00157 }
00158 
00159