~ubuntu-branches/ubuntu/vivid/ghostscript/vivid-updates

« back to all changes in this revision

Viewing changes to devices/vector/gdevpdf.c

  • Committer: Package Import Robot
  • Author(s): Till Kamppeter
  • Date: 2014-05-02 15:48:44 UTC
  • mfrom: (1.1.43)
  • Revision ID: package-import@ubuntu.com-20140502154844-c401rxx0cce8gm0q
Tags: 9.14~dfsg-0ubuntu1
* New upstream release
   - Ghostscript 9.14
   - pdfwrite now uses the same color management engine as Ghostscript
     rendering devices (by default LCMS2). This provides much better
     control over color conversion and color management generally, but
     will result in some small color differences when compared to the
     old system.
     It is no longer necessary to specify UseCIEColor (and we very much
     encourage you not to do this) or the ProcessColorModel if you want
     to convert a PDF file to a specific color space, simply set the
     ColorConversionStrategy appropriately.
     We do not expect any major problems to arise with this new code,
     but for the duration of this release a new switch -dPDFUseOldCMS
     is available which will restore the old color management. See:
     Color Conversion and Management
     Please note that due to constraints of the PDF/A-1 specification,
     the new color management does not yet apply when producing PDF/A
     files.
   - A new device 'eps2write' has been added which allows for the
     creation of EPS files using the ps2write device instead of the old
     (deprecated and removed) pswrite device. This produces
     considerably better quality EPS files than the old epswrite device
     which is now also deprecated and will be removed in a future
     release.
   - ps2write now has a feature to allow customisation of the output
     for specific devices. Please see PSDocOptions and PSPageOptions
     described in ps2ps2.htm Additional Distiller Params
   - Ghostscript now reduces memory usage when processing PDF files
     that use transparency and output is to display devices such as
     Windows display or x11 (i.e. devices that are strictly full
     framebuffer devices), and to high level vector devices that cannot
     reproduce the PDF transparency model, such as the ps2write device
     or pdfwrite when 'flattening' to PDF 1.3 or earlier
     (-dCompatibilityLevel=1.3). This uses banding (clist) files to
     render the transparent areas in bands to reduce memory use, and
     unlike the existing page level banding, this is hidden from the
     target device.
   - Ghostscript can now collect information for pages in temp files
     (in Ghostscript's clist format), then render and output pages for
     the job in arbitrary order, including normal, reverse, odd, even,
     or any order or subset of the pages. This is controlled with the
     --saved-page=___ option. (Note that this only applies to rendering
     devices, and not high level devices such as pdfwrite and
     ps2write.) See: Deferred Page Rendering
   - The Ghostscript device architecture has been extended so that,
     when rendering bands into multiple threads, it is now possible to
     perform post-processing in multiple threads, such as downscale,
     post-render halftoning, or compression. Previously, post
     processing was only possible in the single main thread. This can
     improve performance significantly.
   - Ghostscript has a new "pwgraster" output device for PWG Raster
     output
   - The CUPS device now has improved support for PPD-less printing
   - Plus the usual round of bug fixes, compatibility changes, and
     incremental improvements.
* debian/patches/020140502-0ccf329-fix-fpng-device-build-handle-shared-zlib.patch:
  Fix fpng device build handle for shared zlib (Upstream bug #695113).
* debian/patches/020130903-5ae4180-ps-interpreter-dont-interpolate-imagemask-data-for-high-level-devices.patch,
  debian/patches/020131023-ad3e3ed-handle-type-0-font-in-annotation.patch,
  debian/patches/020131127-87a7fd8-cups-pwg-raster-output-ppd-less-support.patch,
  debian/patches/020131218-5ddd13e-ps2write-dont-emit-a-page-size-change-if-the-last-request-failed.patch,
  debian/patches/020131219-d997bc4-pwgraster-output-device.patch,
  debian/patches/020140313-095ae57-ps2write-fix-missing-beginresource-comment-for-fontfile-objects.patch,
  debian/patches/020140313-5d6b18a-set-correct-portrait-landscape-orientation-on-pcl-5ce.patch,
  debian/patches/020140313-6498483-ps2write-fix-a-dsc-comment.patch,
  debian/patches/020140428-f4584b0-skip-unsupported-font-files.patch:
  Removed patches backported from upstream.
* debian/patches/020140324-b780ff0-protection-against-pxl-segfault-with-image-data-without-colorspace-info.patch,
  debian/patches/020140331-4b44b41-pxlcolor-support-jpeg-in-output.patch,
  debian/patches/020140331-41ab485-pxl-transform-deep-images-with-icc-transform-to-emit-high-level-images.patch,
  debian/patches/1002_pxl-make-dicctransform-default.patch: Refreshed with
  quilt.
* debian/symbols.common: Updated for new upstream source. Applied patch
  which dpkg-gensymbols generated for debian/libgs9.symbols to this file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
    if (index < NUM_RESOURCE_TYPES * NUM_RESOURCE_CHAINS)
66
66
        ENUM_RETURN(pdev->resources[index / NUM_RESOURCE_CHAINS].chains[index % NUM_RESOURCE_CHAINS]);
67
67
    index -= NUM_RESOURCE_TYPES * NUM_RESOURCE_CHAINS;
68
 
    if (index <= pdev->outline_depth)
 
68
    if (index <= pdev->outline_depth && pdev->outline_levels)
69
69
        ENUM_RETURN(pdev->outline_levels[index].first.action);
70
70
    index -= pdev->outline_depth + 1;
71
 
    if (index <= pdev->outline_depth)
 
71
    if (index <= pdev->outline_depth && pdev->outline_levels)
72
72
        ENUM_RETURN(pdev->outline_levels[index].last.action);
73
73
    index -= pdev->outline_depth + 1;
74
74
    ENUM_PREFIX(st_device_psdf, 0);
102
102
        for (i = 0; i < NUM_RESOURCE_TYPES; ++i)
103
103
            for (j = 0; j < NUM_RESOURCE_CHAINS; ++j)
104
104
                RELOC_PTR(gx_device_pdf, resources[i].chains[j]);
105
 
        for (i = 0; i <= pdev->outline_depth; ++i) {
106
 
            RELOC_PTR(gx_device_pdf, outline_levels[i].first.action);
107
 
            RELOC_PTR(gx_device_pdf, outline_levels[i].last.action);
 
105
        if (pdev->outline_levels) {
 
106
            for (i = 0; i <= pdev->outline_depth; ++i) {
 
107
                RELOC_PTR(gx_device_pdf, outline_levels[i].first.action);
 
108
                RELOC_PTR(gx_device_pdf, outline_levels[i].last.action);
 
109
            }
108
110
        }
109
111
    }
110
112
}
137
139
#define PDF_DEVICE_IDENT gs_pdfwrite_device
138
140
#define PDF_DEVICE_MaxInlineImageSize 4000
139
141
#define PDF_FOR_OPDFREAD 0
 
142
#define PDF_FOR_EPS2WRITE 0
140
143
 
141
144
#include "gdevpdfb.h"
142
145
 
149
152
#define PDF_DEVICE_IDENT gs_ps2write_device
150
153
#define PDF_DEVICE_MaxInlineImageSize max_long
151
154
#define PDF_FOR_OPDFREAD 1
152
 
 
153
 
#include "gdevpdfb.h"
154
 
 
155
 
#undef PDF_DEVICE_NAME
156
 
#undef PDF_DEVICE_IDENT
157
 
#undef PDF_DEVICE_MaxInlineImageSize
158
 
#undef PDF_FOR_OPDFREAD
 
155
#define PDF_FOR_EPS2WRITE 0
 
156
 
 
157
#include "gdevpdfb.h"
 
158
 
 
159
#undef PDF_DEVICE_NAME
 
160
#undef PDF_DEVICE_IDENT
 
161
#undef PDF_DEVICE_MaxInlineImageSize
 
162
#undef PDF_FOR_OPDFREAD
 
163
#undef PDF_FOR_EPS2WRITE
 
164
 
 
165
#define PDF_DEVICE_NAME "eps2write"
 
166
#define PDF_DEVICE_IDENT gs_eps2write_device
 
167
#define PDF_DEVICE_MaxInlineImageSize max_long
 
168
#define PDF_FOR_OPDFREAD 1
 
169
#define PDF_FOR_EPS2WRITE 1
 
170
 
 
171
#include "gdevpdfb.h"
 
172
 
 
173
#undef PDF_DEVICE_NAME
 
174
#undef PDF_DEVICE_IDENT
 
175
#undef PDF_DEVICE_MaxInlineImageSize
 
176
#undef PDF_FOR_OPDFREAD
 
177
#undef PDF_FOR_EPS2WRITE
159
178
/* ---------------- Device open/close ---------------- */
160
179
 
161
180
/* Close and remove temporary files. */
347
366
        return code;
348
367
    sclose(s);
349
368
    gs_free_object(mem, s, "pdf_compute_fileID");
350
 
#ifdef DEPRECATED_906
351
 
    memcpy(pdev->fileID, "xxxxxxxxxxxxxxxx", sizeof(pdev->fileID)); /* Debug */
352
 
#endif
353
369
    return 0;
354
370
}
355
371
 
606
622
    gs_memory_t *mem = pdev->pdf_memory = gs_memory_stable(pdev->memory);
607
623
    int code;
608
624
 
 
625
    pdev->InOutputPage = false;
 
626
 
609
627
    if ((code = pdf_open_temp_file(pdev, &pdev->xref)) < 0 ||
610
628
        (code = pdf_open_temp_stream(pdev, &pdev->asides)) < 0 ||
611
629
        (code = pdf_open_temp_stream(pdev, &pdev->streams)) < 0 ||
689
707
            for (j = 0; j < NUM_RESOURCE_CHAINS; ++j)
690
708
                pdev->resources[i].chains[j] = 0;
691
709
    }
 
710
    pdev->outline_levels = (pdf_outline_level_t *)gs_alloc_bytes(mem, INITIAL_MAX_OUTLINE_DEPTH * sizeof(pdf_outline_level_t), "outline_levels array");
 
711
    memset(pdev->outline_levels, 0x00, INITIAL_MAX_OUTLINE_DEPTH * sizeof(pdf_outline_level_t));
 
712
    pdev->max_outline_depth = INITIAL_MAX_OUTLINE_DEPTH;
692
713
    pdev->outline_levels[0].first.id = 0;
693
714
    pdev->outline_levels[0].left = max_int;
694
715
    pdev->outline_levels[0].first.action = 0;
698
719
    pdev->outlines_open = 0;
699
720
    pdev->articles = 0;
700
721
    pdev->Dests = 0;
 
722
    pdev->EmbeddedFiles = 0;
701
723
    /* {global,local}_named_objects was initialized above */
702
724
    pdev->PageLabels = 0;
703
725
    pdev->PageLabels_current_page = 0;
704
726
    pdev->PageLabels_current_label = 0;
705
727
    pdev->pte = NULL;
706
728
    pdf_reset_page(pdev);
 
729
    pdev->BBox.p.x = pdev->width;
 
730
    pdev->BBox.p.y = pdev->height;
 
731
    pdev->BBox.q.x = 0;
 
732
    pdev->BBox.q.y = 0;
 
733
 
 
734
    if(pdev->UseCIEColor) {
 
735
        emprintf(pdev->memory, "\n\nUse of -dUseCIEColor detected!\nSince the release of version 9.11 of Ghostscript we recommend you do not set\n-dUseCIEColor with the pdfwrite/ps2write device family.\n\n");
 
736
    }
707
737
    return 0;
708
738
  fail:
709
739
    gdev_vector_close_file((gx_device_vector *) pdev);
767
797
            (page != NULL ? &page->text_rotation : &pdev->text_rotation);
768
798
        int angle = -1;
769
799
 
770
 
#ifdef DEPRECATED_906
771
 
        /* Bug 687800 together with Bug687489.ps . */
772
 
        const gs_point *pbox = &(page != NULL ? page : &pdev->pages[0])->MediaBox;
773
 
 
774
 
        if (dsc_orientation >= 0 && pbox->x > pbox->y) {
775
 
            /* The page is in landscape format. Adjust the rotation accordingly. */
776
 
            dsc_orientation ^= 1;
777
 
        }
778
 
#endif
779
 
 
780
800
        /* Combine DSC rotation with text rotation : */
781
801
        if (dsc_orientation == 0) {
782
802
            if (ptr->Rotate == 0 || ptr->Rotate == 180)
863
883
        if (code < 0)
864
884
            return code;
865
885
 
866
 
        /* Save viewer's memory with cleaning resources. */
867
 
 
868
 
#ifdef DEPRECATED_906
869
 
        if (pdev->MaxViewerMemorySize < 10000000) {
870
 
            /* fixme: the condition above and the cleaning algorithm
871
 
                may be improved with counting stored resource size
872
 
                and creating multiple streams per page. */
873
 
 
874
 
            if (pdev->ForOPDFRead) {
875
 
                pdf_resource_t *pres = pdf_find_resource_by_resource_id(pdev, resourcePage, pdev->contents_id);
876
 
 
877
 
                if (pres != NULL) {
878
 
                    code = cos_dict_put_c_strings((cos_dict_t *)pres->object, "/.CleanResources", "/All");
879
 
                    if (code < 0)
880
 
                        return code;
881
 
                }
882
 
            }
883
 
            code = pdf_close_text_document(pdev);
884
 
            if (code < 0)
885
 
                return code;
886
 
            code = pdf_write_and_free_all_resource_objects(pdev);
887
 
            if (code < 0)
888
 
                return code;
889
 
        }
890
 
#endif
891
 
 
892
886
        /* Close use of text on the page. */
893
887
 
894
888
        pdf_close_text_page(pdev);
930
924
 
931
925
/* Write the page object. */
932
926
static double
933
 
round_box_coord(floatp xy)
 
927
round_box_coord(double xy)
934
928
{
935
929
    return (int)(xy * 100 + 0.5) / 100.0;
936
930
}
939
933
{
940
934
    long page_id = pdf_page_id(pdev, page_num);
941
935
    pdf_page_t *page = &pdev->pages[page_num - 1];
942
 
    floatp mediabox[4] = {0, 0};
 
936
    double mediabox[4] = {0, 0};
943
937
    stream *s;
944
938
 
945
939
    mediabox[2] = round_box_coord(page->MediaBox.x);
953
947
        const cos_value_t *v_artbox = cos_dict_find_c_key(page->Page, "/ArtBox");
954
948
        const cos_value_t *v_cropbox = cos_dict_find_c_key(page->Page, "/CropBox");
955
949
        const cos_value_t *v_bleedbox = cos_dict_find_c_key(page->Page, "/BleedBox");
956
 
        floatp trimbox[4] = {0, 0}, bleedbox[4] = {0, 0};
 
950
        double trimbox[4] = {0, 0}, bleedbox[4] = {0, 0};
957
951
        bool print_bleedbox = false;
958
952
 
959
953
        trimbox[2] = bleedbox[2] = mediabox[2];
1222
1216
 
1223
1217
    if (code < 0)
1224
1218
        return code;
 
1219
    if(pdev->UseCIEColor) {
 
1220
        emprintf(pdev->memory, "\n\nUse of -dUseCIEColor detected!\nSince the release of version 9.11 of Ghostscript we recommend you do not set\n-dUseCIEColor with the pdfwrite/ps2write device family.\n\n");
 
1221
    }
1225
1222
    if (pdf_ferror(pdev))
1226
1223
        gs_note_error(gs_error_ioerror);
1227
1224
 
1229
1226
        return code;
1230
1227
 
1231
1228
    if (gx_outputfile_is_separate_pages(((gx_device_vector *)dev)->fname, dev->memory)) {
 
1229
        pdev->InOutputPage = true;
1232
1230
        if ((code = pdf_close(dev)) < 0)
1233
1231
            return code;
1234
1232
        code = pdf_open(dev);
1400
1398
    return code;
1401
1399
}
1402
1400
 
1403
 
static int write_hint_stream(pdf_linearisation_t *linear_params, unsigned int *val, char size_bits)
 
1401
static int write_hint_stream(pdf_linearisation_t *linear_params, gs_offset_t val, char size_bits)
1404
1402
{
1405
1403
    unsigned int input_mask, output_mask;
1406
1404
 
1410
1408
    while(size_bits) {
1411
1409
        input_mask = 1 << (size_bits - 1);
1412
1410
        output_mask = 0x80 >> linear_params->HintBits;
1413
 
        if (input_mask & *val)
 
1411
        if (input_mask & val)
1414
1412
            linear_params->HintBuffer[linear_params->HintByte] |= output_mask;
1415
1413
        else
1416
1414
            linear_params->HintBuffer[linear_params->HintByte] &= ~output_mask;
1807
1805
            linear_params->PageHintHeader.MostSharedObjects = hint->NumSharedObjects;
1808
1806
    }
1809
1807
 
1810
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.LeastObjectsPerPage, 32);
1811
 
    write_hint_stream(linear_params, (unsigned int *)&pdev->ResourceUsage[pdev->pages[0].Page->id].LinearisedOffset, 32);
 
1808
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.LeastObjectsPerPage, 32);
 
1809
    write_hint_stream(linear_params, (gs_offset_t)pdev->ResourceUsage[pdev->pages[0].Page->id].LinearisedOffset, 32);
1812
1810
    i = (linear_params->PageHintHeader.MostObjectsPerPage - linear_params->PageHintHeader.MostObjectsPerPage + 1);
1813
1811
    j = 0;
1814
1812
    while (i) {
1816
1814
        j++;
1817
1815
    }
1818
1816
    linear_params->PageHintHeader.ObjectNumBits = j;
1819
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.ObjectNumBits, 16);
1820
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.LeastPageLength, 32);
 
1817
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.ObjectNumBits, 16);
 
1818
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.LeastPageLength, 32);
1821
1819
    i = (linear_params->PageHintHeader.MostPageLength - linear_params->PageHintHeader.LeastPageLength + 1);
1822
1820
    j = 0;
1823
1821
    while (i) {
1825
1823
        j++;
1826
1824
    }
1827
1825
    linear_params->PageHintHeader.PageLengthNumBits = j;
1828
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.PageLengthNumBits, 16);
1829
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.LeastPageOffset, 32);
 
1826
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.PageLengthNumBits, 16);
 
1827
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.LeastPageOffset, 32);
1830
1828
    i = (linear_params->PageHintHeader.MostPageOffset - linear_params->PageHintHeader.LeastPageOffset + 1);
1831
1829
    j = 0;
1832
1830
    while (i) {
1834
1832
        j++;
1835
1833
    }
1836
1834
    linear_params->PageHintHeader.PageOffsetNumBits = j;
1837
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.PageOffsetNumBits, 16);
1838
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.LeastContentLength, 32);
 
1835
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.PageOffsetNumBits, 16);
 
1836
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.LeastContentLength, 32);
1839
1837
    i = (linear_params->PageHintHeader.MostContentLength - linear_params->PageHintHeader.LeastContentLength + 1);
1840
1838
    j = 0;
1841
1839
    while (i) {
1843
1841
        j++;
1844
1842
    }
1845
1843
    linear_params->PageHintHeader.ContentLengthNumBits = j;
1846
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.ContentLengthNumBits, 16);
1847
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.MostSharedObjects, 16);
 
1844
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.ContentLengthNumBits, 16);
 
1845
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.MostSharedObjects, 16);
1848
1846
    i = (linear_params->PageHintHeader.LargestSharedObject + 1);
1849
1847
    j = 0;
1850
1848
    while (i) {
1852
1850
        j++;
1853
1851
    }
1854
1852
    linear_params->PageHintHeader.SharedObjectNumBits = j;
1855
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->PageHintHeader.SharedObjectNumBits, 16);
 
1853
    write_hint_stream(linear_params, (gs_offset_t)linear_params->PageHintHeader.SharedObjectNumBits, 16);
1856
1854
    j = 1;
1857
 
    write_hint_stream(linear_params, (unsigned int *)&j, 16);
1858
 
    write_hint_stream(linear_params, (unsigned int *)&j, 16);
 
1855
    write_hint_stream(linear_params, (gs_offset_t)j, 16);
 
1856
    write_hint_stream(linear_params, (gs_offset_t)j, 16);
1859
1857
 
1860
1858
#ifdef LINEAR_DEBUGGING
1861
1859
    dmprintf1(pdev->pdf_memory, "LeastObjectsPerPage %d\n", linear_params->PageHintHeader.LeastObjectsPerPage);
1862
 
    dmprintf1(pdev->pdf_memory, "Page 1 Offset %ld\n", pdev->ResourceUsage[pdev->pages[0].Page->id].LinearisedOffset);
 
1860
    dmprintf1(pdev->pdf_memory, "Page 1 Offset %"PRId64"\n", pdev->ResourceUsage[pdev->pages[0].Page->id].LinearisedOffset);
1863
1861
    dmprintf1(pdev->pdf_memory, "ObjectNumBits %d\n", linear_params->PageHintHeader.ObjectNumBits);
1864
1862
    dmprintf1(pdev->pdf_memory, "LeastPageLength %d\n", linear_params->PageHintHeader.LeastPageLength);
1865
1863
    dmprintf1(pdev->pdf_memory, "MostPagelength %d\n", linear_params->PageHintHeader.MostPageLength);
1882
1880
        int Num;
1883
1881
 
1884
1882
        Num = hint->NumUniqueObjects - linear_params->PageHintHeader.LeastObjectsPerPage;
1885
 
        write_hint_stream(linear_params, (unsigned int *)&Num, linear_params->PageHintHeader.ObjectNumBits);
 
1883
        write_hint_stream(linear_params, (gs_offset_t)Num, linear_params->PageHintHeader.ObjectNumBits);
1886
1884
        dmprintf2(pdev->pdf_memory, "Page %d NumUniqueObjects %d\n", i, Num);
1887
1885
    }
1888
1886
    for (i=0;i < pdev->next_page;i++) {
1890
1888
        int Num;
1891
1889
 
1892
1890
        Num = hint->PageLength - linear_params->PageHintHeader.LeastPageLength;
1893
 
        write_hint_stream(linear_params, (unsigned int *)&Num, linear_params->PageHintHeader.PageLengthNumBits);
 
1891
        write_hint_stream(linear_params, (gs_offset_t)Num, linear_params->PageHintHeader.PageLengthNumBits);
1894
1892
        dmprintf2(pdev->pdf_memory, "Page %d PageLength %d\n", i, Num);
1895
1893
    }
1896
1894
    for (i=0;i < pdev->next_page;i++) {
1897
1895
        page_hint_stream_t *hint = &linear_params->PageHints[i];
1898
1896
 
1899
1897
        if (i == 0) {
1900
 
            write_hint_stream(linear_params, (unsigned int *)&i, linear_params->PageHintHeader.SharedObjectNumBits);
 
1898
            write_hint_stream(linear_params, (gs_offset_t)i, linear_params->PageHintHeader.SharedObjectNumBits);
1901
1899
            dmprintf2(pdev->pdf_memory, "Page %d NumSharedObjects %d\n", i, 1);
1902
1900
        }
1903
1901
        else {
1904
 
            write_hint_stream(linear_params, (unsigned int *)&hint->NumSharedObjects, linear_params->PageHintHeader.SharedObjectNumBits);
 
1902
            write_hint_stream(linear_params, (gs_offset_t)hint->NumSharedObjects, linear_params->PageHintHeader.SharedObjectNumBits);
1905
1903
            dmprintf2(pdev->pdf_memory, "Page %d NumSharedObjects %d\n", i, hint->NumSharedObjects);
1906
1904
        }
1907
1905
    }
1909
1907
        page_hint_stream_t *hint = &linear_params->PageHints[i];
1910
1908
 
1911
1909
        for (j=0;j < hint->NumSharedObjects;j++) {
1912
 
            write_hint_stream(linear_params, (unsigned int *)&hint->SharedObjectRef[j], linear_params->PageHintHeader.SharedObjectNumBits);
 
1910
            write_hint_stream(linear_params, (gs_offset_t)hint->SharedObjectRef[j], linear_params->PageHintHeader.SharedObjectNumBits);
1913
1911
            dmprintf3(pdev->pdf_memory, "Page %d SharedObject %d ObjectRef %d\n", i, j, hint->SharedObjectRef[j]);
1914
1912
        }
1915
1913
    }
1916
 
    j = 1;
 
1914
 
1917
1915
    for (i=0;i < pdev->next_page;i++) {
1918
1916
        page_hint_stream_t *hint = &linear_params->PageHints[i];
1919
1917
 
1920
1918
        for (j=0;j < hint->NumSharedObjects;j++) {
1921
 
            write_hint_stream(linear_params, (unsigned int *)&j, 1);
 
1919
            write_hint_stream(linear_params, (gs_offset_t)j, 1);
1922
1920
            dmprintf2(pdev->pdf_memory, "Page %d SharedObject %d Position Numerator 1\n", i, j);
1923
1921
        }
1924
1922
    }
1927
1925
        int Num;
1928
1926
 
1929
1927
        Num = hint->ContentOffset - linear_params->PageHintHeader.LeastPageOffset;
1930
 
        write_hint_stream(linear_params, (unsigned int *)&Num, linear_params->PageHintHeader.PageOffsetNumBits);
 
1928
        write_hint_stream(linear_params, (gs_offset_t)Num, linear_params->PageHintHeader.PageOffsetNumBits);
1931
1929
        dmprintf2(pdev->pdf_memory, "Page %d ContentStreamOffset %d\n", i, Num);
1932
1930
    }
1933
1931
    for (i=1;i < pdev->next_page;i++) {
1935
1933
        int Num;
1936
1934
 
1937
1935
        Num = hint->ContentLength - linear_params->PageHintHeader.LeastContentLength;
1938
 
        write_hint_stream(linear_params, (unsigned int *)&Num, linear_params->PageHintHeader.ContentLengthNumBits);
 
1936
        write_hint_stream(linear_params, (gs_offset_t)Num, linear_params->PageHintHeader.ContentLengthNumBits);
1939
1937
        dmprintf2(pdev->pdf_memory, "Page %d ContentStreamLength %d\n", i, Num);
1940
1938
    }
1941
1939
    flush_hint_stream(linear_params);
1951
1949
            linear_params->SharedHintHeader.FirstObjectOffset = linear_params->SharedHints[1].ObjectOffset;
1952
1950
        }
1953
1951
        if (linear_params->SharedHints[i].ObjectLength < linear_params->SharedHintHeader.LeastObjectLength) {
1954
 
            linear_params->SharedHints[i].ObjectLength = linear_params->SharedHintHeader.LeastObjectLength;
 
1952
            linear_params->SharedHintHeader.LeastObjectLength = linear_params->SharedHints[i].ObjectLength;
1955
1953
        }
1956
1954
        if (linear_params->SharedHints[i].ObjectLength > linear_params->SharedHintHeader.MostObjectLength) {
1957
 
            linear_params->SharedHints[i].ObjectLength = linear_params->SharedHintHeader.MostObjectLength;
 
1955
            linear_params->SharedHintHeader.MostObjectLength = linear_params->SharedHints[i].ObjectLength;
1958
1956
        }
1959
1957
    }
1960
1958
 
1961
1959
    linear_params->SharedHintHeader.FirstPageEntries = linear_params->NumPage1Resources;
1962
1960
    linear_params->SharedHintHeader.NumSharedObjects = linear_params->NumSharedResources + linear_params->SharedHintHeader.FirstPageEntries;
1963
1961
 
1964
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->SharedHintHeader.FirstSharedObject, 32);
 
1962
    write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.FirstSharedObject, 32);
1965
1963
    dmprintf1(pdev->pdf_memory, "\nFirstSharedObject %d\n", linear_params->SharedHintHeader.FirstSharedObject);
1966
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->SharedHintHeader.FirstObjectOffset, 32);
1967
 
    dmprintf1(pdev->pdf_memory, "FirstObjectOffset %ld\n", linear_params->SharedHintHeader.FirstObjectOffset);
1968
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->SharedHintHeader.FirstPageEntries, 32);
 
1964
    write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.FirstObjectOffset, 32);
 
1965
    dmprintf1(pdev->pdf_memory, "FirstObjectOffset %"PRId64"\n", linear_params->SharedHintHeader.FirstObjectOffset);
 
1966
    write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.FirstPageEntries, 32);
1969
1967
    dmprintf1(pdev->pdf_memory, "FirstPageEntries %d\n", linear_params->SharedHintHeader.FirstPageEntries);
1970
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->SharedHintHeader.NumSharedObjects, 32);
 
1968
    write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.NumSharedObjects, 32);
1971
1969
    dmprintf1(pdev->pdf_memory, "NumSharedObjects %d\n", linear_params->SharedHintHeader.NumSharedObjects);
1972
1970
    j = 1;
1973
 
    write_hint_stream(linear_params, (unsigned int *)&j, 32);
 
1971
    write_hint_stream(linear_params, (gs_offset_t)j, 32);
1974
1972
    dmprintf(pdev->pdf_memory, "GreatestObjectsNumBits 1\n");
1975
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->SharedHintHeader.FirstObjectOffset, 16);
1976
 
    dmprintf1(pdev->pdf_memory, "FirstObjectOffset %ld\n", linear_params->SharedHintHeader.FirstObjectOffset);
1977
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->SharedHintHeader.LeastObjectLength, 32);
 
1973
    write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.FirstObjectOffset, 16);
 
1974
    dmprintf1(pdev->pdf_memory, "FirstObjectOffset %"PRId64"\n", linear_params->SharedHintHeader.FirstObjectOffset);
 
1975
    write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.LeastObjectLength, 32);
1978
1976
    dmprintf1(pdev->pdf_memory, "LeastObjectLength %d\n", linear_params->SharedHintHeader.LeastObjectLength);
1979
1977
 
1980
1978
    i = (linear_params->SharedHintHeader.MostObjectLength - linear_params->SharedHintHeader.LeastObjectLength + 1) / 2;
1984
1982
        j++;
1985
1983
    }
1986
1984
    linear_params->SharedHintHeader.LengthNumBits = j + 1;
1987
 
    write_hint_stream(linear_params, (unsigned int *)&linear_params->SharedHintHeader.LengthNumBits, 16);
 
1985
    write_hint_stream(linear_params, (gs_offset_t)linear_params->SharedHintHeader.LengthNumBits, 16);
1988
1986
 
1989
1987
    for (i = 0; i< linear_params->NumSharedHints; i++) {
1990
1988
        unsigned int Length = linear_params->SharedHints[i].ObjectLength - linear_params->SharedHintHeader.LeastObjectLength;
1991
1989
 
1992
 
        write_hint_stream(linear_params, (unsigned int *)&Length, linear_params->SharedHintHeader.LengthNumBits);
 
1990
        write_hint_stream(linear_params, (gs_offset_t)Length, linear_params->SharedHintHeader.LengthNumBits);
1993
1991
        dmprintf2(pdev->pdf_memory, "Shared Object group %d, Length %d\n", i, Length);
1994
1992
    }
1995
1993
 
1996
1994
    j = 0;
1997
1995
    for (i = 0; i< linear_params->NumSharedHints; i++) {
1998
 
        write_hint_stream(linear_params, (unsigned int *)&j, 1);
 
1996
        write_hint_stream(linear_params, (gs_offset_t)j, 1);
1999
1997
        dmprintf1(pdev->pdf_memory, "Shared Object group %d, SignatureFlag false\n", i);
2000
1998
    }
2001
1999
    for (i = 0; i< linear_params->NumSharedHints; i++) {
2002
 
        write_hint_stream(linear_params, (unsigned int *)&j, 1);
 
2000
        write_hint_stream(linear_params, (gs_offset_t)j, 1);
2003
2001
        dmprintf1(pdev->pdf_memory, "Shared Object group %d, NumObjects 1\n", i);
2004
2002
    }
2005
2003
 
2049
2047
    for (i = 1;i < LDictObj; i++) {
2050
2048
        for (j = 0; j < pdev->ResourceUsageSize;j++) {
2051
2049
            if (pdev->ResourceUsage[j].NewObjectNumber == i) {
2052
 
                gs_sprintf(Header, "%010ld 00000 n \n", pdev->ResourceUsage[j].LinearisedOffset + HintStreamLen);
 
2050
                gs_sprintf(Header, "%010"PRId64" 00000 n \n", pdev->ResourceUsage[j].LinearisedOffset + HintStreamLen);
2053
2051
                fwrite(Header, 20, 1, linear_params->sfile);
2054
2052
            }
2055
2053
        }
2056
2054
    }
2057
2055
 
2058
 
    gs_sprintf(LDict, "trailer\n<</Size %d>>\nstartxref\n%ld\n%%%%EOF\n",
 
2056
    gs_sprintf(LDict, "trailer\n<</Size %d>>\nstartxref\n%"PRId64"\n%%%%EOF\n",
2059
2057
        LDictObj, linear_params->FirstxrefOffset);
2060
2058
    fwrite(LDict, strlen(LDict), 1, linear_params->sfile);
2061
2059
 
2069
2067
     * versions.
2070
2068
     */
2071
2069
    gp_fseek_64(linear_params->sfile, linear_params->LDictOffset, SEEK_SET);
2072
 
    gs_sprintf(LDict, "%d 0 obj\n<</Linearized 1/L %ld/H[ ", LDictObj, linear_params->FileLength);
 
2070
    gs_sprintf(LDict, "%d 0 obj\n<</Linearized 1/L %"PRId64"/H[ ", LDictObj, linear_params->FileLength);
2073
2071
    fwrite(LDict, strlen(LDict), 1, linear_params->sfile);
2074
2072
 
2075
 
    gs_sprintf(LDict, "%ld", pdev->ResourceUsage[HintStreamObj].LinearisedOffset);
 
2073
    gs_sprintf(LDict, "%"PRId64"", pdev->ResourceUsage[HintStreamObj].LinearisedOffset);
2076
2074
    fwrite(LDict, strlen(LDict), 1, linear_params->sfile);
2077
 
    gs_sprintf(LDict, " %ld]", HintStreamLen);
 
2075
    gs_sprintf(LDict, " %"PRId64"]", HintStreamLen);
2078
2076
    fwrite(LDict, strlen(LDict), 1, linear_params->sfile);
2079
2077
    /* Implementation Note 180 in hte PDF Reference 1.7 says that Acrobat
2080
2078
     * gets the 'E' value wrong. So its probably not important....
2081
2079
     */
2082
 
    gs_sprintf(LDict, "/O %d/E %ld",pdev->ResourceUsage[pdev->pages[0].Page->id].NewObjectNumber, linear_params->E);
 
2080
    gs_sprintf(LDict, "/O %d/E %"PRId64"",pdev->ResourceUsage[pdev->pages[0].Page->id].NewObjectNumber, linear_params->E);
2083
2081
    fwrite(LDict, strlen(LDict), 1, linear_params->sfile);
2084
 
    gs_sprintf(LDict, "/N %d/T %ld>>\nendobj\n", pdev->next_page, linear_params->T);
 
2082
    gs_sprintf(LDict, "/N %d/T %"PRId64">>\nendobj\n", pdev->next_page, linear_params->T);
2085
2083
    fwrite(LDict, strlen(LDict), 1, linear_params->sfile);
2086
2084
 
2087
2085
    /* Return to the secondary xref and write it again filling
2094
2092
    for (i = LDictObj;i <= linear_params->LastResource + 2; i++) {
2095
2093
        for (j = 0; j < pdev->ResourceUsageSize;j++) {
2096
2094
            if (pdev->ResourceUsage[j].NewObjectNumber == i) {
2097
 
                gs_sprintf(Header, "%010ld 00000 n \n", pdev->ResourceUsage[j].LinearisedOffset);
 
2095
                gs_sprintf(Header, "%010"PRId64" 00000 n \n", pdev->ResourceUsage[j].LinearisedOffset);
2098
2096
                fwrite(Header, 20, 1, linear_params->sfile);
2099
2097
            }
2100
2098
        }
2104
2102
     * in the missing values.
2105
2103
     */
2106
2104
    code = gp_fseek_64(linear_params->sfile, linear_params->FirsttrailerOffset, SEEK_SET);
2107
 
    gs_sprintf(LDict, "\ntrailer\n<</Size %ld/Info %d 0 R/Root %d 0 R/ID[%s%s]/Prev %ld>>\nstartxref\r\n0\n%%%%EOF\n",
2108
 
        linear_params->LastResource + 3, pdev->ResourceUsage[linear_params->Info_id].NewObjectNumber, pdev->ResourceUsage[linear_params->Catalog_id].NewObjectNumber, fileID, fileID, (unsigned long)mainxref);
 
2105
    gs_sprintf(LDict, "\ntrailer\n<</Size %ld/Info %d 0 R/Root %d 0 R/ID[%s%s]/Prev %"PRId64">>\nstartxref\r\n0\n%%%%EOF\n",
 
2106
        linear_params->LastResource + 3, pdev->ResourceUsage[linear_params->Info_id].NewObjectNumber, pdev->ResourceUsage[linear_params->Catalog_id].NewObjectNumber, fileID, fileID, mainxref);
2109
2107
    fwrite(LDict, strlen(LDict), 1, linear_params->sfile);
2110
2108
 
2111
2109
    code = gp_fseek_64(linear_params->sfile, pdev->ResourceUsage[HintStreamObj].LinearisedOffset, SEEK_SET);
2112
 
    gs_sprintf(LDict, "%d 0 obj\n<</Length %10ld", HintStreamObj, HintLength);
 
2110
    gs_sprintf(LDict, "%d 0 obj\n<</Length %10"PRId64"", HintStreamObj, HintLength);
2113
2111
    fwrite(LDict, strlen(LDict), 1, linear_params->sfile);
2114
 
    gs_sprintf(LDict, "\n/S %10ld>>\nstream\n", SharedHintOffset);
 
2112
    gs_sprintf(LDict, "\n/S %10"PRId64">>\nstream\n", SharedHintOffset);
2115
2113
    fwrite(LDict, strlen(LDict), 1, linear_params->sfile);
2116
2114
 
2117
2115
error:
2224
2222
    long Catalog_id = pdev->Catalog->id, Info_id = pdev->Info->id,
2225
2223
        Pages_id = pdev->Pages->id, Encrypt_id = 0;
2226
2224
    long Threads_id = 0;
 
2225
    bool partial_page = (pdev->contents_id != 0 && pdev->next_page != 0);
2227
2226
    int code = 0, code1, pagecount=0;
2228
2227
    int64_t start_section, end_section;
 
2228
    char str[256];
 
2229
 
2229
2230
 
2230
2231
    pdf_linearisation_t linear_params;
2231
2232
    memset(&linear_params, 0x00, sizeof(linear_params));
2241
2242
     * marks.
2242
2243
     */
2243
2244
    if (pdev->next_page == 0) {
 
2245
        /* If we didn't get called from pdf_output_page, and we are doign file-per-page
 
2246
         * output, then the call from close_device will leave an empty file which we don't
 
2247
         * want. So here we delete teh file.
 
2248
         */
 
2249
        if (!pdev->InOutputPage && gx_outputfile_is_separate_pages(pdev->fname, pdev->memory)) {
 
2250
            code = gdev_vector_close_file((gx_device_vector *) pdev);
 
2251
            if (code != 0)
 
2252
                return code;
 
2253
            code = gx_device_delete_output_file((const gx_device *)pdev, pdev->fname);
 
2254
            if (code != 0)
 
2255
                return gs_note_error(gs_error_ioerror);
 
2256
            return code;
 
2257
        }
2244
2258
        code = pdf_open_page(pdev, PDF_IN_STREAM);
2245
2259
 
2246
2260
        if (code < 0)
2277
2291
    code1 = pdf_free_resource_objects(pdev, resourceSoftMaskDict);
2278
2292
    if (code >= 0)
2279
2293
        code = code1;
2280
 
#ifdef DEPRECATED_906
2281
 
    code1 = pdf_close_text_document(pdev);
2282
 
    if (code >= 0)
2283
 
        code = code1;
2284
 
#endif
2285
2294
    /* This was in pdf_close_document, but that made no sense, so moved here
2286
2295
     * for more consistency (and ease of finding it). This code deals with
2287
2296
     * emitting fonts and FontDescriptors
2341
2350
 
2342
2351
        s = pdev->strm;
2343
2352
        stream_puts(s, "<< /Type /Pages /Kids [\n");
 
2353
        /* Omit the last page if it was incomplete. */
 
2354
        if (partial_page)
 
2355
            --(pdev->next_page);
2344
2356
        {
2345
2357
            int i;
2346
2358
 
2391
2403
 
2392
2404
        /* Write named destinations.  (We can't free them yet.) */
2393
2405
 
2394
 
        if (pdev->Dests) {
2395
 
            pdf_record_usage(pdev, pdev->Dests->id, resource_usage_part9_structure);
2396
 
            COS_WRITE_OBJECT(pdev->Dests, pdev, resourceDests);
 
2406
        if (pdev->CompatibilityLevel < 1.2) {
 
2407
            if (pdev->Dests) {
 
2408
                pdf_record_usage(pdev, pdev->Dests->id, resource_usage_part9_structure);
 
2409
                COS_WRITE_OBJECT(pdev->Dests, pdev, resourceDests);
 
2410
            }
 
2411
        } else {
 
2412
            if (pdev->Dests) {
 
2413
                pdf_record_usage(pdev, pdev->Dests->id, resource_usage_part9_structure);
 
2414
                cos_write_dict_as_ordered_array((cos_object_t *)pdev->Dests, pdev, resourceDests);
 
2415
            }
 
2416
            /* Write Embedded files.  (We can't free them yet.) */
 
2417
 
 
2418
            if (pdev->EmbeddedFiles) {
 
2419
                pdf_record_usage(pdev, pdev->EmbeddedFiles->id, resource_usage_part9_structure);
 
2420
                cos_write_dict_as_ordered_array((cos_object_t *)pdev->EmbeddedFiles, pdev, resourceEmbeddedFiles);
 
2421
            }
2397
2422
        }
2398
2423
 
2399
2424
        /* Write the PageLabel array */
2442
2467
            pprintld1(s, "/Threads %ld 0 R\n", Threads_id);
2443
2468
            pdf_record_usage(pdev, Threads_id, resource_usage_part1_structure);
2444
2469
        }
2445
 
        if (pdev->Dests)
2446
 
            pprintld1(s, "/Dests %ld 0 R\n", pdev->Dests->id);
 
2470
        if (pdev->CompatibilityLevel < 1.2) {
 
2471
            if (pdev->Dests)
 
2472
                pprintld1(s, "/Dests %ld 0 R\n", pdev->Dests->id);
 
2473
        } else {
 
2474
            if (pdev->Dests || pdev->EmbeddedFiles) {
 
2475
                stream_puts(s, "/Names <<\n");
 
2476
                if (pdev->Dests)
 
2477
                    pprintld1(s, "/Dests <</Kids [%ld 0 R]>>\n", pdev->Dests->id);
 
2478
                if (pdev->EmbeddedFiles)
 
2479
                    pprintld1(s, "/EmbeddedFiles << /Kids [%ld 0 R]>>\n", pdev->EmbeddedFiles->id);
 
2480
                stream_puts(s, ">>\n");
 
2481
            }
 
2482
        }
2447
2483
        if (pdev->PageLabels)
2448
2484
            pprintld1(s, "/PageLabels << /Nums  %ld 0 R >>\n",
2449
2485
                  pdev->PageLabels->id);
2454
2490
            COS_FREE(pdev->Dests, "pdf_close(Dests)");
2455
2491
            pdev->Dests = 0;
2456
2492
        }
 
2493
        if (pdev->EmbeddedFiles) {
 
2494
            COS_FREE(pdev->EmbeddedFiles, "pdf_close(EmbeddFiles)");
 
2495
            pdev->EmbeddedFiles = 0;
 
2496
        }
2457
2497
        if (pdev->PageLabels) {
2458
2498
            COS_FREE(pdev->PageLabels, "pdf_close(PageLabels)");
2459
2499
            pdev->PageLabels = 0;
2511
2551
        /* All resources and procsets written, end the prolog */
2512
2552
        stream_puts(pdev->strm, "%%EndProlog\n");
2513
2553
 
 
2554
        if (pdev->params.PSDocOptions.data) {
 
2555
            int i;
 
2556
            char *p = (char *)pdev->params.PSDocOptions.data;
 
2557
 
 
2558
            stream_puts(pdev->strm, "%%BeginSetup\n");
 
2559
            for (i=0;i<pdev->params.PSDocOptions.size;i++)
 
2560
                stream_putc(s, *p++);
 
2561
            stream_puts(pdev->strm, "\n");
 
2562
            stream_puts(pdev->strm, "\n%%EndSetup\n");
 
2563
        }
 
2564
 
2514
2565
        if (pdev->ResourcesBeforeUsage)
2515
2566
            pdf_reverse_resource_chain(pdev, resourcePage);
2516
2567
        for (j = 0; j < NUM_RESOURCE_CHAINS && code >= 0; ++j) {
2523
2574
 
2524
2575
                    pprintd2(pdev->strm, "%%%%Page: %d %d\n",
2525
2576
                        pagecount, pagecount);
2526
 
                    pprintd2(pdev->strm, "%%%%PageBoundingBox: 0 0 %d %d\n", (int)page->MediaBox.x, (int)page->MediaBox.y);
 
2577
                    if (!pdev->Eps2Write)
 
2578
                        pprintd2(pdev->strm, "%%%%PageBoundingBox: 0 0 %d %d\n", (int)page->MediaBox.x, (int)page->MediaBox.y);
2527
2579
                    stream_puts(pdev->strm, "%%BeginPageSetup\n");
2528
2580
                    stream_puts(pdev->strm, "/pagesave save def\n");
 
2581
 
 
2582
                    if (pdev->params.PSPageOptions.size) {
 
2583
                        int i, index = (pagecount - 1) % pdev->params.PSPageOptions.size;
 
2584
                        char *p = (char *)pdev->params.PSPageOptions.data[index].data;
 
2585
 
 
2586
                        for (i=0;i<pdev->params.PSPageOptions.data[index].size;i++)
 
2587
                            stream_putc(s, *p++);
 
2588
                        stream_puts(pdev->strm, "\n");
 
2589
                    }
 
2590
 
2529
2591
                    pdf_write_page(pdev, pagecount++);
 
2592
 
2530
2593
                    stream_puts(pdev->strm, "%%EndPageSetup\n");
2531
2594
                    pprintld1(pdev->strm, "%ld 0 obj\n", pres->object->id);
2532
2595
                    code = cos_write(pres->object, pdev, pres->object->id);
2580
2643
        if (pdev->Linearise)
2581
2644
            linear_params.xref = xref;
2582
2645
 
2583
 
        if (pdev->FirstObjectNumber == 1)
2584
 
            pprintld1(s, "xref\n0 %ld\n0000000000 65535 f \n",
 
2646
        if (pdev->FirstObjectNumber == 1) {
 
2647
            gs_sprintf(str, "xref\n0 %"PRId64"\n0000000000 65535 f \n",
2585
2648
                  end_section);
2586
 
        else
2587
 
            pprintld2(s, "xref\n0 1\n0000000000 65535 f \n%ld %ld\n",
 
2649
            stream_puts(s, str);
 
2650
        }
 
2651
        else {
 
2652
            gs_sprintf(str, "xref\n0 1\n0000000000 65535 f \n%"PRId64" %"PRId64"\n",
2588
2653
                  start_section,
2589
2654
                  end_section - start_section);
 
2655
            stream_puts(s, str);
 
2656
        }
2590
2657
 
2591
2658
        do {
2592
2659
            write_xref_section(pdev, tfile, start_section, end_section, resource_pos, linear_params.Offsets);
2594
2661
                break;
2595
2662
            start_section = end_section + 1;
2596
2663
            end_section = find_end_xref_section(pdev, tfile, start_section, resource_pos);
2597
 
            pprintld2(s, "%ld %ld\n", start_section, end_section - start_section);
 
2664
            if (end_section < 0)
 
2665
                return end_section;
 
2666
            gs_sprintf(str, "%"PRId64" %"PRId64"\n", start_section, end_section - start_section);
 
2667
            stream_puts(s, str);
2598
2668
        } while (1);
2599
2669
 
2600
2670
        /* Write the trailer. */
2841
2911
 
2842
2912
    /* Free named objects. */
2843
2913
 
 
2914
    cos_release((cos_object_t *)pdev->NI_stack, "Release Name Index stack");
 
2915
    gs_free_object(mem, pdev->NI_stack, "Free Name Index stack");
 
2916
    pdev->NI_stack = 0;
 
2917
 
2844
2918
    cos_dict_objects_delete(pdev->local_named_objects);
2845
2919
    COS_FREE(pdev->local_named_objects, "pdf_close(local_named_objects)");
2846
2920
    pdev->local_named_objects = 0;
2880
2954
    gs_free_object(mem, pdev->Pages, "Free Pages dict");
2881
2955
    pdev->Pages = 0;
2882
2956
 
2883
 
    cos_release((cos_object_t *)pdev->NI_stack, "Release Name Index stack");
2884
 
    gs_free_object(mem, pdev->NI_stack, "Free Name Index stack");
2885
 
    pdev->NI_stack = 0;
2886
 
 
 
2957
    {
 
2958
        int i;
 
2959
        for (i=0;i < pdev->vgstack_depth;i++)
 
2960
            gs_free_object(pdev->memory->non_gc_memory, pdev->vgstack[i].dash_pattern, "pdfwrite final free stored dash in gstate");
 
2961
    }
2887
2962
    gs_free_object(pdev->pdf_memory, pdev->vgstack, "pdf_close(graphics state stack)");
2888
2963
 
2889
2964
    cos_release((cos_object_t *)pdev->Namespace_stack, "release Name space stack");
2893
2968
    pdev->Catalog = 0;
2894
2969
    pdev->Info = 0;
2895
2970
 
2896
 
    memset(&pdev->outline_levels, 0x00, MAX_OUTLINE_DEPTH * sizeof(pdf_outline_level_t));
 
2971
    gs_free_object(mem, pdev->outline_levels, "outline_levels array");
 
2972
    pdev->outline_levels = 0;
 
2973
    pdev->outline_depth = -1;
 
2974
    pdev->max_outline_depth = 0;
2897
2975
 
2898
2976
    {
2899
2977
        /* pdf_open_dcument could set up filters for entire document.
2919
2997
        emprintf2(pdev->memory, "ERROR: A pdfmark destination page %d "
2920
2998
                  "points beyond the last page %d.\n",
2921
2999
                  pdev->max_referred_page, pdev->next_page);
2922
 
#ifdef DEPRECATED_906 /* Temporary disabled due to Bug 687686. */
2923
 
        if (code >= 0)
2924
 
            code = gs_note_error(gs_error_rangecheck);
2925
 
#endif
2926
3000
    }
2927
3001
    code = pdf_close_files(pdev, code);
2928
3002
    if (code < 0)