Example #1
0
static void
pdf_init_all(PDF *p)
{
    pdf_init_document(p);
    pdf_init_info(p);
    pdf_init_images(p);
    pdf_init_xobjects(p);
    pdf_init_fonts(p);
    pdf_init_transition(p);
    pdf_init_outlines(p);
    pdf_init_annots(p);
    pdf_init_stream(p);
    pdf_init_colorspaces(p);
    pdf_init_pattern(p);

    /* clients may set char/word spacing and horizontal scaling outside pages
    ** for PDF_stringwidth() calculations, and they may set a color for use
    ** in PDF_makespotcolor(). here we set the defaults.
    */
    p->sl = 0;			
    pdf_init_tstate(p);
    pdf_init_cstate(p);

#ifdef HAVE_PDI
    pdf_reset_pdi(p);		/* reset all PDI contexts */
#endif
}
Example #2
0
PDFLIB_API void PDFLIB_CALL
PDF_end_page(PDF *p)
{
    static const char fn[] = "PDF_end_page";
    int index;


    PDF_TRACE(("%s\t(pdf[%p]);\n", fn, (void *) p));

    if (PDF_SANITY_CHECK_FAILED(p))
	return;

    PDF_CHECK_SCOPE(p, fn, pdf_state_page);

    /* check whether PDF_save() and PDF_restore() calls are balanced */
    if (p->sl > 0)
	pdf_error(p, PDF_RuntimeError, "Unmatched save level at end of page");


    /* restore text parameter and color defaults for out-of-page usage.
    */
    pdf_init_tstate(p);
    pdf_init_cstate(p);

    pdf_end_contents_section(p);

    /* Page object */
    pdf_begin_obj(p, p->pages[p->current_page]);

    pdf_begin_dict(p);
    pdf_puts(p, "/Type/Page\n");
    pdf_printf(p, "/Parent %ld 0 R\n", pdf_get_pnode_id(p));

    p->res_id = pdf_alloc_id(p);
    pdf_printf(p, "/Resources %ld 0 R\n", p->res_id);

    pdf_printf(p, "/MediaBox[0 0 %f %f]\n", p->width, p->height);

    if (p->CropBox.llx != (float) 0 || p->CropBox.lly != (float) 0 ||
        p->CropBox.urx != (float) 0 || p->CropBox.ury != (float) 0 )
    {
	if (p->CropBox.urx <= p->CropBox.llx ||
	    p->CropBox.ury <= p->CropBox.lly)
		pdf_error(p, PDF_ValueError, "Illegal CropBox dimensions");

	pdf_printf(p, "/CropBox[%f %f %f %f]\n",
	    p->CropBox.llx, p->CropBox.lly, p->CropBox.urx, p->CropBox.ury);
    }

    if (p->BleedBox.llx != (float) 0 || p->BleedBox.lly != (float) 0 ||
        p->BleedBox.urx != (float) 0 || p->BleedBox.ury != (float) 0 )
    {
	if (p->BleedBox.urx <= p->BleedBox.llx ||
	    p->BleedBox.ury <= p->BleedBox.lly)
		pdf_error(p, PDF_ValueError, "Illegal BleedBox dimensions");

	pdf_printf(p, "/BleedBox[%f %f %f %f]\n",
	    p->BleedBox.llx, p->BleedBox.lly, p->BleedBox.urx, p->BleedBox.ury);
    }

    if (p->TrimBox.llx != (float) 0 || p->TrimBox.lly != (float) 0 ||
        p->TrimBox.urx != (float) 0 || p->TrimBox.ury != (float) 0 )
    {
	if (p->TrimBox.urx <= p->TrimBox.llx ||
	    p->TrimBox.ury <= p->TrimBox.lly)
		pdf_error(p, PDF_ValueError, "Illegal TrimBox dimensions");

	pdf_printf(p, "/TrimBox[%f %f %f %f]\n",
	    p->TrimBox.llx, p->TrimBox.lly, p->TrimBox.urx, p->TrimBox.ury);
    }

    if (p->ArtBox.llx != (float) 0 || p->ArtBox.lly != (float) 0 ||
        p->ArtBox.urx != (float) 0 || p->ArtBox.ury != (float) 0 )
    {
	if (p->ArtBox.urx <= p->ArtBox.llx ||
	    p->ArtBox.ury <= p->ArtBox.lly)
		pdf_error(p, PDF_ValueError, "Illegal ArtBox dimensions");

	pdf_printf(p, "/ArtBox[%f %f %f %f]\n",
	    p->ArtBox.llx, p->ArtBox.lly, p->ArtBox.urx, p->ArtBox.ury);
    }

    /*
     * The duration can be placed in the transition dictionary (/D)
     * or in the page dictionary (/Dur). We put it here so it can
     * be used without setting a transition effect.
     */

    if (p->duration > 0)
	pdf_printf(p, "/Dur %f\n", p->duration);

    pdf_write_page_transition(p);

    pdf_puts(p, "/Contents[");
    for (index = 0; index < p->next_content; index++) {
	pdf_printf(p, "%ld 0 R", p->contents_ids[index]);
	pdf_putc(p, (char) (index + 1 % 8 ? PDF_SPACE : PDF_NEWLINE));
    }
    pdf_puts(p, "]\n");

    /* Thumbnail image */
    if (p->thumb_id != BAD_ID)
	pdf_printf(p, "/Thumb %ld 0 R\n", p->thumb_id);

    pdf_write_annots_root(p);

    pdf_end_dict(p);				/* Page object */
    pdf_end_obj(p);

    pdf_write_page_annots(p);			/* Annotation dicts */
    
    pdf_begin_obj(p, p->res_id);		/* resource object */
    pdf_begin_dict(p);				/* resource dict */

    pdf_write_page_procsets(p);			/* ProcSet resources */

    pdf_write_page_fonts(p);			/* Font resources */

    pdf_write_page_colorspaces(p);		/* Color space resources */

    pdf_write_page_pattern(p);			/* Pattern resources */

    pdf_write_xobjects(p);			/* XObject resources */

    pdf_end_dict(p);				/* resource dict */
    pdf_end_obj(p);				/* resource object */

    pdf_cleanup_page(p);

    PDF_SET_STATE(p, fn, pdf_state_document);

    if (p->flush & PDF_FLUSH_PAGE)
	pdf_flush_stream(p);
}
Example #3
0
PDFLIB_API void PDFLIB_CALL
PDF_begin_page(PDF *p, float width, float height)
{
    static const char fn[] = "PDF_begin_page";

    PDF_TRACE(("%s\t(pdf[%p], %f, %f);\n", fn, (void *) p, width, height));

    if (PDF_SANITY_CHECK_FAILED(p))
	return;

    PDF_CHECK_SCOPE(p, fn, pdf_state_document);
    
    if (width <= 0 || height <= 0)
	pdf_error(p, PDF_ValueError, "Page size must be positive");

    if (p->compatibility >= PDF_1_3 &&
	(height < PDF_ACRO4_MINPAGE || width < PDF_ACRO4_MINPAGE ||
	height > PDF_ACRO4_MAXPAGE || width > PDF_ACRO4_MAXPAGE))
	pdf_error(p, PDF_NonfatalError,
	    "Page size incompatible with Acrobat 4");

    else if (p->compatibility == PDF_1_2 &&
	(height < PDF_ACRO3_MINPAGE || width < PDF_ACRO3_MINPAGE ||
	height > PDF_ACRO3_MAXPAGE || width > PDF_ACRO3_MAXPAGE))
	pdf_error(p, PDF_RuntimeError, "Page size incompatible with Acrobat 3");

    if (++(p->current_page) >= p->pages_capacity)
	pdf_grow_pages(p);

    /* no id has been preallocated */
    if (p->pages[p->current_page] == BAD_ID)
	p->pages[p->current_page] = pdf_alloc_id(p);

    p->height		= height;
    p->width		= width;
    p->thumb_id		= BAD_ID;
    p->next_content	= 0;
    p->contents 	= c_none;
    p->procset		= 0;
    p->sl		= 0;

    p->CropBox.llx	= (float) 0;
    p->CropBox.lly	= (float) 0;
    p->CropBox.urx	= (float) 0;
    p->CropBox.ury	= (float) 0;

    p->BleedBox.llx	= (float) 0;
    p->BleedBox.lly	= (float) 0;
    p->BleedBox.urx	= (float) 0;
    p->BleedBox.ury	= (float) 0;

    p->TrimBox.llx	= (float) 0;
    p->TrimBox.lly	= (float) 0;
    p->TrimBox.urx	= (float) 0;
    p->TrimBox.ury	= (float) 0;

    p->ArtBox.llx	= (float) 0;
    p->ArtBox.lly	= (float) 0;
    p->ArtBox.urx	= (float) 0;
    p->ArtBox.ury	= (float) 0;

    PDF_SET_STATE(p, fn, pdf_state_page);

    pdf_init_page_annots(p);
    pdf_init_tstate(p);
    pdf_init_gstate(p);
    pdf_init_cstate(p);

    pdf_begin_contents_section(p);
}
Example #4
0
void
pdf__begin_glyph(
    PDF *p,
    const char *glyphname,
    pdc_scalar wx,
    pdc_scalar llx, pdc_scalar lly, pdc_scalar urx, pdc_scalar ury)
{
    static const char fn[] = "pdf__begin_glyph";
    pdf_font *font;
    pdf_t3font *t3font;
    pdf_t3glyph *glyph = NULL;
    pdc_scalar tbc;
    int ig;

    if (glyphname == NULL || *glyphname == '\0')
        pdc_error(p->pdc, PDC_E_ILLARG_EMPTY, "glyphname", 0, 0, 0);

    font = &p->fonts[p->t3slot];
    t3font = font->t3font;

    /* error message prefix */
    pdc_push_errmsg(p->pdc, PDF_E_T3_FONT_PREFIX, font->apiname, 0, 0, 0);

    for (ig = 0; ig < t3font->next_glyph; ig++)
    {
        glyph = &t3font->glyphs[ig];
        if (!pdc_strcmp(glyph->name, glyphname))
        {
            if (t3font->pass == glyph->pass)
                pdc_error(p->pdc, PDF_E_T3_GLYPH, glyphname, 0, 0, 0);
            else
                break;
        }
    }

    /* new glyph */
    if (ig == t3font->next_glyph)
    {
        if (t3font->pass == 2)
            pdc_error(p->pdc, PDF_E_T3_UNKOWNGLYPH, glyphname, 0, 0, 0);

        pdc_check_number(p->pdc, "wx", wx);
        pdc_check_number(p->pdc, "llx", llx);
        pdc_check_number(p->pdc, "lly", lly);
        pdc_check_number(p->pdc, "urx", urx);
        pdc_check_number(p->pdc, "ury", ury);

        if (t3font->colorized == pdc_true &&
            (llx != 0 || lly != 0 ||
             urx != 0 || ury != 0))
            pdc_error(p->pdc, PDF_E_T3_BADBBOX, 0, 0, 0, 0);


        if (urx < llx)
        {
            tbc = llx;
            llx = urx;
            urx = tbc;
        }

        if (ury < lly)
        {
            tbc = lly;
            lly = ury;
            ury = tbc;
        }

        if (ig == t3font->capacity)
        {
            t3font->capacity *= 2;
            t3font->glyphs = (pdf_t3glyph *) pdc_realloc(p->pdc, t3font->glyphs,
                t3font->capacity * sizeof (pdf_t3glyph), fn);
        }

        glyph = &t3font->glyphs[ig];
        glyph->charproc_id = PDC_BAD_ID;
        glyph->name = pdc_strdup(p->pdc, glyphname);
        glyph->wx = wx;
        glyph->llx = llx;
        glyph->lly = lly;
        glyph->urx = urx;
        glyph->ury = ury;

        /* see comment in p_font.c for explanation */
        glyph->width = 1000 * wx * font->ft.matrix.a;

        /* if the strdup above fails, cleanup won't touch this slot. */
        t3font->next_glyph++;
    }
    glyph->pass = t3font->pass;
    t3font->curr_glyph = ig;

    pdc_logg_cond(p->pdc, 1, trc_font,
        "\tBegin of Type3 font glyph \"%s\"\n", glyphname);

    if (t3font->pass != 1)
    {
            if (t3font->pass == 2)
                pdc_logg_cond(p->pdc, 2, trc_font,
                              "\t\tglyph [%d] was used in text\n", ig);

            glyph->charproc_id = pdc_begin_obj(p->out, PDC_NEW_ID);
            pdc_begin_dict(p->out);

            p->length_id = pdc_alloc_id(p->out);
            pdc_objref(p->out, "/Length", p->length_id);

            if (pdc_get_compresslevel(p->out))
                pdc_puts(p->out, "/Filter/FlateDecode\n");

            pdc_end_dict(p->out);

            pdc_begin_pdfstream(p->out);

            if (t3font->colorized == pdc_true)
                pdc_printf(p->out, "%f 0 d0\n", glyph->wx);
            else
            {
                pdc_printf(p->out, "%f 0 %f %f %f %f d1\n",
                    glyph->wx, glyph->llx, glyph->lly, glyph->urx, glyph->ury);

                /* adjust the font's bounding box */
                if (glyph->llx < font->ft.bbox.llx)
                    font->ft.bbox.llx = glyph->llx;
                if (glyph->lly < font->ft.bbox.lly)
                    font->ft.bbox.lly = glyph->lly;
                if (glyph->urx > font->ft.bbox.urx)
                    font->ft.bbox.urx = glyph->urx;
                if (glyph->ury > font->ft.bbox.ury)
                    font->ft.bbox.ury = glyph->ury;
            }

            /* we must initialize the text, graphics and color state
             * otherwise the user get unpredictable appearance of a
             * glyph because of optimizing outputting graphics properties.
             * Following statements were inserted due to bug #719
             */
            pdf_init_tstate(p);
            pdf_init_gstate(p);
            pdf_init_cstate(p);

            PDF_SET_STATE(p, pdf_state_glyph);
    }
    else
    {
        PDF_SET_STATE(p, pdf_state_glyphmetrics);
    }

    pdc_pop_errmsg(p->pdc);

    if (!p->pdc->smokerun)
        pdc_logg_cond(p->pdc, 1, trc_api, "[Begin glyph %d]\n", ig);
}
Example #5
0
void
pdf__end_font(PDF *p)
{
    int ig;
    pdf_font *font;
    pdf_t3font *t3font;

    PDF_SET_STATE(p, pdf_state_document);

    font = &p->fonts[p->t3slot];
    t3font = font->t3font;

    /* error message prefix */
    pdc_push_errmsg(p->pdc, PDF_E_T3_FONT_PREFIX, font->apiname, 0, 0, 0);

    if (t3font->pass == 0)
    {
        pdf_t3glyph glyph0 = t3font->glyphs[0];

        /* search for .notdef glyph */
        if (pdc_strcmp(glyph0.name, (char *) pdc_get_notdef_glyphname()))
        {
            for (ig = 0; ig < t3font->next_glyph; ig++)
            {
                if (!pdc_strcmp(t3font->glyphs[ig].name,
                                (char *) pdc_get_notdef_glyphname()))
                    break;
            }

            if (ig < t3font->next_glyph)
            {
                pdc_logg_cond(p->pdc, 2, trc_font,
                    "\tGlyph id %d: \"%s\" will be exchanged "
                    "with glyph id 0: \"%s\"\n",
                    ig, t3font->glyphs[ig].name, glyph0.name);

                t3font->glyphs[0] = t3font->glyphs[ig];
                t3font->glyphs[ig] = glyph0;
            }
            else
            {
                pdc_warning(p->pdc, PDF_E_T3_MISSNOTDEF, 0, 0, 0, 0);
            }
        }
    }

    if (t3font->pass != 1)
    {
        t3font->charprocs_id = pdc_alloc_id(p->out);
        pdc_begin_obj(p->out, t3font->charprocs_id); /* CharProcs dict */
        pdc_begin_dict(p->out);

        for (ig = 0; ig < t3font->next_glyph; ig++)
        {
            pdf_t3glyph *glyph = &t3font->glyphs[ig];

            if (glyph->charproc_id != PDC_BAD_ID)
            {
                pdf_put_pdfname(p, glyph->name);
                pdc_objref(p->out, "", glyph->charproc_id);
            }
        }

        pdc_end_dict(p->out);
        pdc_end_obj(p->out);                        /* CharProcs dict */

        pdc_begin_obj(p->out, t3font->res_id);
        pdc_begin_dict(p->out);                     /* Resource dict */

        pdf_write_page_fonts(p);                    /* Font resources */

        pdf_write_page_colorspaces(p);              /* Color space resources */

        pdf_write_page_pattern(p);                  /* Pattern resources */

        pdf_write_xobjects(p);                      /* XObject resources */

        pdc_end_dict(p->out);                       /* Resource dict */
        pdc_end_obj(p->out);                        /* Resource object */

        pdf_pg_resume(p, -1);

        if (p->flush & pdc_flush_content)
            pdc_flush_stream(p->out);

        /* see pdf__begin_glyph */
        pdf_init_tstate(p);
        pdf_init_gstate(p);
        pdf_init_cstate(p);
    }

    pdc_logg_cond(p->pdc, 1, trc_font,
        "\tEnd of Type3 font \"%s\"\n", font->apiname);

    pdc_pop_errmsg(p->pdc);

    if (!p->pdc->smokerun)
        pdc_logg_cond(p->pdc, 1, trc_api, "[End font %d]\n", p->t3slot);

    p->t3slot = -1;
}
Example #6
0
/* Start a new pattern definition. */
PDFLIB_API int PDFLIB_CALL
PDF_begin_pattern(
    PDF *p,
    float width,
    float height,
    float xstep,
    float ystep,
    int painttype)
{
    static const char fn[] = "PDF_begin_pattern";

    PDF_TRACE(("%s\t(pdf[%p], %f, %f, %f, %f, %d);\n",
	fn, (void *) p, width, height, xstep, ystep, painttype));

    if (PDF_SANITY_CHECK_FAILED(p))
	return -1;

    PDF_CHECK_SCOPE(p, fn, pdf_state_document);
    
    if (width <= 0 || height <= 0)
	pdf_error(p, PDF_ValueError, "Pattern size must be positive");
    
    /* Acrobat 3 doesn't display patterns on screen so we disable it */
    if (p->compatibility == PDF_1_2)
	pdf_error(p, PDF_RuntimeError,
	    "Patterns not fully supported in Acrobat 3");

    if (painttype != 1 && painttype != 2)
	pdf_error(p, PDF_ValueError,
	    "Bad pattern paint type %d in PDF_begin_pattern", painttype);

    if (p->pattern_number == p->pattern_capacity)
	pdf_grow_pattern(p);

    p->pattern[p->pattern_number].obj_id = pdf_begin_obj(p, NEW_ID);
    p->pattern[p->pattern_number].painttype = painttype;

    p->height		= height;
    p->width		= width;
    p->thumb_id		= BAD_ID;
    PDF_PUSH_STATE(p, fn, pdf_state_pattern);
    p->next_content	= 0;
    p->contents 	= c_page;
    p->procset		= 0;
    p->sl		= 0;

    pdf_init_tstate(p);
    pdf_init_gstate(p);
    pdf_init_cstate(p);

    pdf_begin_dict(p);				/* pattern dict*/

    p->res_id = pdf_alloc_id(p);
	
    pdf_puts(p, "/PatternType 1\n");

    /* colored or uncolored pattern */
    pdf_printf(p, "/PaintType %d\n", painttype);
    pdf_puts(p, "/TilingType 1\n");		/* constant spacing */

    pdf_printf(p, "/BBox[0 0 %f %f]\n", p->width, p->height);

    pdf_printf(p, "/XStep %f\n", xstep);
    pdf_printf(p, "/YStep %f\n", ystep);

    pdf_printf(p, "/Resources %ld 0 R\n", p->res_id);

    p->contents_length_id = pdf_alloc_id(p);
    pdf_printf(p, "/Length %ld 0 R\n", p->contents_length_id);

    if (p->compresslevel)
	pdf_puts(p, "/Filter/FlateDecode\n");

    pdf_end_dict(p);				/* pattern dict*/
    pdf_begin_stream(p);

    pdf_compress_init(p);

    p->start_contents_pos = pdf_tell(p);
    p->next_content++;

    return p->pattern_number++;
}
Example #7
0
void
pdf__begin_font(
    PDF *p,
    const char *fontname,
    float a, float b, float c, float d, float e, float f,
    const char *optlist)
{
    static const char fn[] = "pdf__begin_font";
    pdc_resopt *results;
    pdc_font *font;
    float det;
    int colorized = pdc_false;
    int slot;

    /* parsing optlist */
    results = pdc_parse_optionlist(p->pdc, optlist, pdf_begin_font_options,
                                   NULL, pdc_true);
    pdc_get_optvalues(p->pdc, "colorized", results, &colorized, NULL);
    pdc_cleanup_optionlist(p->pdc, results);

    det = a*d - b*c;

    if (det == 0)
        pdc_error(p->pdc, PDC_E_ILLARG_MATRIX,
            pdc_errprintf(p->pdc, "%f %f %f %f %f %f", a, b, c, d, e, f),
            0, 0, 0);

    /* look for an already existing font */
    for (slot = 0; slot < p->fonts_number; slot++)
    {
        if (!strcmp(p->fonts[slot].apiname, fontname))
        {
            pdc_error(p->pdc, PDF_E_T3_FONTEXISTS, fontname, 0, 0, 0);
        }
    }

    PDF_SET_STATE(p, pdf_state_font);

    pdf_init_tstate(p);
    pdf_init_gstate(p);
    pdf_init_cstate(p);

    /* slot for new font struct */
    slot = pdf_init_newfont(p);
    font = &p->fonts[slot];
    p->fonts_number++;

    /*
     * We store the new font in a font slot marked with "invalidenc" encoding.
     * When the font is used for the first time we modify the encoding.
     * Later uses will make a copy if the encoding is different.
     */

    font->type          = pdc_Type3;
    font->obj_id        = pdc_alloc_id(p->out);

    font->name          = NULL;
    font->apiname       = pdc_strdup(p->pdc, fontname);
    font->encoding      = pdc_invalidenc;  /* initially there is no encoding */
    font->widths        = (int *) pdc_calloc(p->pdc,
                                 font->numOfCodes * sizeof(int), fn);

    font->t3font = (pdc_t3font*) pdc_malloc(p->pdc, sizeof(pdc_t3font), fn);
    pdf_init_t3font_struct(p, font->t3font, T3GLYPHS_CHUNKSIZE);

    font->t3font->fontname      = pdc_strdup(p->pdc, fontname);
    font->t3font->colorized     = colorized;

    /* the resource id is needed until the font dict is written */
    font->t3font->res_id        = pdc_alloc_id(p->out);

    font->t3font->matrix.a      = a;
    font->t3font->matrix.b      = b;
    font->t3font->matrix.c      = c;
    font->t3font->matrix.d      = d;
    font->t3font->matrix.e      = e;
    font->t3font->matrix.f      = f;

    /* We won't receive glyph bounding box values for colorized true,
     * so we use the font matrix to get an approximation instead.
     *
     * Writing the font matrix should be optional according to the
     * PDF reference, but we must write it in order to work around a
     * display bug in Acrobat.
     */
    if (font->t3font->colorized == pdc_true) {
        font->t3font->bbox.llx = 0;
        font->t3font->bbox.lly = 0;
        font->t3font->bbox.urx = (d-b)/det;
        font->t3font->bbox.ury = (a-c)/det;
    }

    /*
     * We must store a pointer to the current font because its glyph
     * definitions may use other fonts and we would be unable to find
     * "our" current font again. This pointer lives throughout the
     * font definition, and will be reset in PDF_end_font() below.
     */
    p->t3font = font->t3font;
}