home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / gdevpdfd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-02  |  11.8 KB  |  382 lines

  1. /* Copyright (C) 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevpdfd.c */
  20. /* Driver drawing procedures for PDF-writing driver */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gxdevice.h"
  24. #include "gxfixed.h"
  25. #include "gxistate.h"
  26. #include "gxpaint.h"
  27. #include "gzpath.h"
  28. #include "gzcpath.h"
  29. #include "gdevpdfx.h"
  30.  
  31. /* ---------------- Drawing ---------------- */
  32.  
  33. /* Fill a rectangle. */
  34. int
  35. gdev_pdf_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  36.   gx_color_index color)
  37. {    gx_device_pdf *pdev = (gx_device_pdf *)dev;
  38.     int code;
  39.  
  40.     /* Make a special check for the initial fill with white, */
  41.     /* which shouldn't cause the page to be opened. */
  42.     if ( color == 0xffffff && !is_in_page(pdev) )
  43.       return 0;
  44.     code = pdf_open_page(pdev, pdf_in_stream);
  45.     if ( code < 0 )
  46.       return code;
  47.     /* Make sure we aren't being clipped. */
  48.     pdf_put_clip_path(pdev, NULL);
  49.     pdf_set_color(pdev, color, &pdev->fill_color, "rg");
  50.     pprintd4(pdev->strm, "%d %d %d %d re f\n", x, y, w, h);
  51.     return 0;
  52. }
  53.  
  54. /* ---------------- Path drawing ---------------- */
  55.  
  56. /* ------ Utilities ------ */
  57.  
  58. /* Put a path on the output file.  If do_close is false and the last */
  59. /* path component is a closepath, omit it and return 1. */
  60. private int
  61. pdf_put_path(gx_device_pdf *pdev, const gx_path *ppath, bool do_close,
  62.   const gs_matrix *pmat)
  63. {    stream *s = pdev->strm;
  64.     gs_fixed_rect rbox;
  65.     const subpath *next;
  66.     gs_path_enum cenum;
  67.  
  68.     /* If do_close is false, we recognize rectangles specially. */
  69.     if ( !do_close && ppath->subpath_count == 1 &&
  70.          ppath->curve_count == 0 &&
  71.          gx_subpath_is_rectangle(ppath->first_subpath, &rbox, &next) &&
  72.          (pmat == 0 || is_xxyy(pmat) || is_xyyx(pmat))
  73.        )
  74.       {    gs_point p, q;
  75.  
  76.         p.x = fixed2float(rbox.p.x), p.y = fixed2float(rbox.p.y);
  77.         q.x = fixed2float(rbox.q.x), q.y = fixed2float(rbox.q.y);
  78.         if ( pmat )
  79.           { gs_point_transform_inverse(p.x, p.y, pmat, &p);
  80.             gs_point_transform_inverse(q.x, q.y, pmat, &q);
  81.           }
  82.         pprintg4(s, "%g %g %g %g re\n",
  83.              p.x, p.y, q.x - p.x, q.y - p.y);
  84.         return 0;
  85.       }
  86.     gx_path_enum_init(&cenum, ppath);
  87.     for ( ; ; )
  88.       { gs_fixed_point vs[3];
  89.         gs_point vp[3];
  90.         const char *format;
  91.         int pe_op = gx_path_enum_next(&cenum, vs);
  92.  
  93. sw:        switch ( pe_op )
  94.           {
  95.           case 0:        /* done */
  96.         return 0;
  97.           case gs_pe_moveto:
  98.         format = "%g %g m\n";
  99.         goto do1;
  100.           case gs_pe_lineto:
  101.         format = "%g %g l\n";
  102. do1:        vp[0].x = fixed2float(vs[0].x), vp[0].y = fixed2float(vs[0].y);
  103.         if ( pmat )
  104.           gs_point_transform_inverse(vp[0].x, vp[0].y, pmat, &vp[0]);
  105.         pprintg2(s, format, vp[0].x, vp[0].y);
  106.         break;
  107.           case gs_pe_curveto:
  108.         vp[0].x = fixed2float(vs[0].x), vp[0].y = fixed2float(vs[0].y);
  109.         vp[1].x = fixed2float(vs[1].x), vp[1].y = fixed2float(vs[1].y);
  110.         vp[2].x = fixed2float(vs[2].x), vp[2].y = fixed2float(vs[2].y);
  111.         if ( pmat )
  112.           { gs_point_transform_inverse(vp[0].x, vp[0].y, pmat, &vp[0]);
  113.             gs_point_transform_inverse(vp[1].x, vp[1].y, pmat, &vp[1]);
  114.             gs_point_transform_inverse(vp[2].x, vp[2].y, pmat, &vp[2]);
  115.           }
  116.         pprintg6(s, "%g %g %g %g %g %g c\n",
  117.              vp[0].x, vp[0].y, vp[1].x, vp[1].y, vp[2].x, vp[2].y);
  118.         break;
  119.           case gs_pe_closepath:
  120.         if ( do_close )
  121.           { pputs(s, "h\n");
  122.             break;
  123.           }
  124.         pe_op = gx_path_enum_next(&cenum, vs);
  125.         if ( pe_op != 0 )
  126.           { pputs(s, "h\n");
  127.             goto sw;
  128.           }
  129.         return 1;
  130.           }
  131.       }
  132. }
  133.  
  134. /* Test whether we will need to put the clipping path. */
  135. bool
  136. pdf_must_put_clip_path(gx_device_pdf *pdev, const gx_clip_path *pcpath)
  137. {    if ( pcpath == NULL )
  138.       return pdev->clip_path_id != pdev->no_clip_path_id;
  139.     if ( pdev->clip_path_id == pcpath->id )
  140.       return false;
  141.     if ( gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0,
  142.                      int2fixed(pdev->width),
  143.                      int2fixed(pdev->height))
  144.        )
  145.       return pdev->clip_path_id != pdev->no_clip_path_id;
  146.     return true;
  147. }
  148.  
  149. /* Put a clipping path on the output file. */
  150. int
  151. pdf_put_clip_path(gx_device_pdf *pdev, const gx_clip_path *pcpath)
  152. {    stream *s = pdev->strm;
  153.  
  154.     if ( pcpath == NULL ) {
  155.       if ( pdev->clip_path_id == pdev->no_clip_path_id )
  156.         return 0;
  157.       pputs(s, "Q\nq\n");
  158.       pdev->clip_path_id = pdev->no_clip_path_id;
  159.     } else {
  160.       if ( pdev->clip_path_id == pcpath->id )
  161.         return 0;
  162.       if ( gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0,
  163.                        int2fixed(pdev->width),
  164.                        int2fixed(pdev->height))
  165.          ) {
  166.         if ( pdev->clip_path_id == pdev->no_clip_path_id )
  167.           return 0;
  168.         pputs(s, "Q\nq\n");
  169.         pdev->clip_path_id = pdev->no_clip_path_id;
  170.       } else {
  171.         pputs(s, "Q\nq\nW\n");
  172.         if ( pcpath->segments_valid )
  173.           pdf_put_path(pdev, &pcpath->path, true, NULL);
  174.         else
  175.           { /* Write out the rectangles. */
  176.         const gx_clip_rect *prect = pcpath->list.head;
  177.  
  178.         if ( prect == 0 )
  179.           prect = &pcpath->list.single;
  180.         for ( ; prect != 0; prect = prect->next )
  181.           if ( prect->xmax > prect->xmin && prect->ymax > prect->ymin )
  182.             pprintg4(s, "%g %g %g %g re\n",
  183.                  prect->xmin, prect->ymin,
  184.                  prect->xmax - prect->xmin,
  185.                  prect->ymax - prect->ymin);
  186.           }
  187.         pputs(s, "n\n");
  188.         pdev->clip_path_id = pcpath->id;
  189.       }
  190.     }
  191.     pdev->text.font = 0;
  192.     if ( pdev->context == pdf_in_text )
  193.       pdev->context = pdf_in_stream;
  194.     pdf_reset_graphics(pdev);
  195.     return 0;
  196. }
  197.  
  198. /* ------ Driver procedures ------ */
  199.  
  200. /* Fill a path. */
  201. int
  202. gdev_pdf_fill_path(gx_device *dev, const gs_imager_state *pis, gx_path *ppath,
  203.   const gx_fill_params *params,
  204.   const gx_drawing_color *pdcolor, const gx_clip_path *pcpath)
  205. {    gx_device_pdf *pdev = (gx_device_pdf *)dev;
  206.     int code;
  207.     /*
  208.      * HACK: we fill an empty path in order to set the clipping path
  209.      * and the color for writing text.  If it weren't for this, we
  210.      * could detect and skip empty paths before putting out the clip
  211.      * path or the color.  We also clip with an empty path in order
  212.      * to advance currentpoint for show operations without actually
  213.      * drawing anything.
  214.      */
  215.     bool have_path;
  216.  
  217.     /*
  218.      * Check for an empty clipping path.
  219.      */
  220.     if ( pcpath ) {
  221.       gs_fixed_rect box;
  222.  
  223.       gx_cpath_outer_box(pcpath, &box);
  224.       if ( box.p.x >= box.q.x || box.p.y >= box.q.y )
  225.         return 0;        /* empty clipping path */
  226.     }
  227.     if ( !gx_dc_is_pure(pdcolor) )
  228.       return gx_default_fill_path(dev, pis, ppath, params, pdcolor,
  229.                       pcpath);
  230.     /*
  231.      * Make a special check for the initial fill with white,
  232.      * which shouldn't cause the page to be opened.
  233.      */
  234.     if ( gx_dc_pure_color(pdcolor) == 0xffffff && !is_in_page(pdev) )
  235.       return 0;
  236.     have_path = !gx_path_is_void(ppath);
  237.     if ( have_path || pdev->context == pdf_in_none ||
  238.          pdf_must_put_clip_path(pdev, pcpath)
  239.        )
  240.       { code = pdf_open_page(pdev, pdf_in_stream);
  241.         if ( code < 0 )
  242.           return code;
  243.       }
  244.     pdf_put_clip_path(pdev, pcpath);
  245.     pdf_set_color(pdev, gx_dc_pure_color(pdcolor), &pdev->fill_color, "rg");
  246.     if ( have_path )
  247.       { stream *s = pdev->strm;
  248.  
  249.         if ( params->flatness != pdev->flatness )
  250.           { pprintg1(s, "%g i\n", params->flatness);
  251.             pdev->flatness = params->flatness;
  252.           }
  253.         pdf_put_path(pdev, ppath, false, NULL);
  254.         pputs(s, (params->rule < 0 ? "f\n" : "f*\n"));
  255.       }
  256.     return 0;
  257. }
  258.  
  259. /* Compare two dash patterns. */
  260. private bool
  261. pdf_dash_pattern_eq(const float *stored, const gx_dash_params *set, float scale)
  262. {    int i;
  263.     for ( i = 0; i < set->pattern_size; ++i )
  264.       if ( stored[i] != (float)(set->pattern[i] * scale) )
  265.         return false;
  266.     return true;
  267. }
  268.  
  269. /* Stroke a path. */
  270. int
  271. gdev_pdf_stroke_path(gx_device *dev, const gs_imager_state *pis,
  272.   gx_path *ppath, const gx_stroke_params *params,
  273.   const gx_drawing_color *pdcolor, const gx_clip_path *pcpath)
  274. {    gx_device_pdf *pdev = (gx_device_pdf *)dev;
  275.     stream *s;
  276.     int code;
  277.     int pattern_size = pis->line_params.dash.pattern_size;
  278.     double scale;
  279.     bool set_ctm;
  280.     gs_matrix mat;
  281.     const gs_matrix *pmat;
  282.     int i;
  283.  
  284.     if ( gx_path_is_void(ppath) )
  285.       return 0;        /* won't mark the page */
  286.     if ( !gx_dc_is_pure(pdcolor) )
  287.       return gx_default_stroke_path(dev, pis, ppath, params, pdcolor,
  288.                     pcpath);
  289.     code = pdf_open_page(pdev, pdf_in_stream);
  290.     if ( code < 0 )
  291.       return code;
  292.     /*
  293.      * If the CTM is not uniform, stroke width depends on angle.
  294.      * We'd like to avoid resetting the CTM, so we check for uniform
  295.      * CTMs explicitly.  Note that in PDF, unlike PostScript, it is
  296.      * the CTM at the time of the stroke operation, not the CTM at
  297.      * the time the path was constructed, that is used for transforming
  298.      * the points of the path; so if we have to reset the CTM, we must
  299.      * do it before constructing the path, and inverse-transform all
  300.      * the coordinates.
  301.      */
  302.     if ( pis->ctm.xy == 0 && pis->ctm.yx == 0 )
  303.       { scale = fabs(pis->ctm.xx);
  304.         set_ctm = fabs(pis->ctm.yy) != scale;
  305.       }
  306.     else if ( pis->ctm.xx == 0 && pis->ctm.yy == 0 )
  307.       { scale = fabs(pis->ctm.xy);
  308.         set_ctm = fabs(pis->ctm.yx) != scale;
  309.       }
  310.     else if ( (pis->ctm.xx == pis->ctm.yy && pis->ctm.xy == -pis->ctm.yx)||
  311.           (pis->ctm.xx == -pis->ctm.yy && pis->ctm.xy == pis->ctm.yx)
  312.         )
  313.       { scale = hypot(pis->ctm.xx, pis->ctm.xy);
  314.         set_ctm = false;
  315.       }
  316.     else
  317.       set_ctm = true;
  318.     if ( set_ctm ) {
  319.       scale = 1;
  320.       mat.xx = pis->ctm.xx / pdev->scale.x;
  321.       mat.xy = pis->ctm.xy / pdev->scale.y;
  322.       mat.yx = pis->ctm.yx / pdev->scale.x;
  323.       mat.yy = pis->ctm.yy / pdev->scale.y;
  324.       mat.tx = mat.ty = 0;
  325.       pmat = &mat;
  326.     } else {
  327.       pmat = 0;
  328.     }
  329.         
  330.     pdf_put_clip_path(pdev, pcpath);
  331.     pdf_set_color(pdev, gx_dc_pure_color(pdcolor), &pdev->stroke_color, "RG");
  332.     s = pdev->strm;
  333.     if ( (float)(pis->line_params.dash.offset * scale) != pdev->line_params.dash.offset ||
  334.          pattern_size != pdev->line_params.dash.pattern_size ||
  335.          pattern_size > max_dash ||
  336.          (pattern_size != 0 &&
  337.           !pdf_dash_pattern_eq(pdev->dash_pattern, &pis->line_params.dash,
  338.                    scale))
  339.        )
  340.       { pputs(s, "[ ");
  341.         pdev->line_params.dash.pattern_size = pattern_size;
  342.         for ( i = 0; i < pattern_size; ++i )
  343.           { float element = pis->line_params.dash.pattern[i] * scale;
  344.         if ( i < max_dash )
  345.           pdev->dash_pattern[i] = element;
  346.         pprintg1(s, "%g ", element);
  347.           }
  348.         pdev->line_params.dash.offset =
  349.           pis->line_params.dash.offset * scale;
  350.         pprintg1(s, "] %g d\n", pdev->line_params.dash.offset);
  351.       }
  352.     if ( params->flatness != pdev->flatness )
  353.       { pprintg1(s, "%g i\n", params->flatness);
  354.         pdev->flatness = params->flatness;
  355.       }
  356.     if ( (float)(pis->line_params.half_width * scale) != pdev->line_params.half_width )
  357.       { pdev->line_params.half_width = pis->line_params.half_width * scale;
  358.         pprintg1(s, "%g w\n", pdev->line_params.half_width * 2);
  359.       }
  360.     if ( pis->line_params.miter_limit != pdev->line_params.miter_limit )
  361.       { pprintg1(s, "%g M\n", pis->line_params.miter_limit);
  362.         gx_set_miter_limit(&pdev->line_params,
  363.                    pis->line_params.miter_limit);
  364.       }
  365.     if ( pis->line_params.cap != pdev->line_params.cap )
  366.       { pprintd1(s, "%d J\n", pis->line_params.cap);
  367.         pdev->line_params.cap = pis->line_params.cap;
  368.       }
  369.     if ( pis->line_params.join != pdev->line_params.join )
  370.       { pprintd1(s, "%d j\n", pis->line_params.join);
  371.         pdev->line_params.join = pis->line_params.join;
  372.       }
  373.     if ( set_ctm )
  374.       pdf_put_matrix(pdev, "q ", &mat, "cm\n");
  375.     code = pdf_put_path(pdev, ppath, false, pmat);
  376.     if ( code < 0 )
  377.       return code;
  378.     pputs(s, (code ? "s" : "S"));
  379.     pputs(s, (set_ctm ? " Q\n" : "\n"));
  380.     return 0;
  381. }
  382.