Back to index

tetex-bin  3.0
colorsp.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 <stdlib.h>
00026 #include "system.h"
00027 #include "mem.h"
00028 #include "pdfdev.h"
00029 #include "pdfparse.h"
00030 #include "pdfspecial.h"
00031 #include "dvipdfm.h"
00032 
00033 static char ignore_colors = 0;
00034 void color_special_ignore_colors(void)
00035 {
00036   ignore_colors = 1;
00037 }
00038 
00039 static void do_color_special (char **start, char *end)
00040 {
00041   char *command, *token;
00042   char *c1=NULL, *c2=NULL, *c3=NULL, *c4=NULL;
00043   skip_white (start, end);
00044   if ((command = parse_ident (start, end))) {
00045     if (!strcmp (command, "push")) { /* Handle a push */ 
00046       skip_white (start, end);
00047       if ((token = parse_ident (start, end))) {
00048        if (!strcmp (token, "rgb")) { /* Handle rgb color */
00049          if ((c1=parse_number(start, end)) &&
00050              (c2=parse_number(start, end)) &&
00051              (c3=parse_number(start, end))) {
00052            dev_begin_rgb_color (atof(c1), atof(c2), atof(c3));
00053          }
00054        }
00055        else if (!strcmp (token, "cmyk")) { /* Handle rgb color */
00056          if ((c1=parse_number(start, end)) &&
00057              (c2=parse_number(start, end)) &&
00058              (c3=parse_number(start, end)) &&
00059              (c4=parse_number(start, end))) {
00060            dev_begin_cmyk_color (atof(c1), atof(c2), atof(c3), atof(c4));
00061          }
00062        } else if (!strcmp (token, "gray")) { /* Handle gray */
00063          if ((c1=parse_number(start, end))) {
00064            dev_begin_gray (atof(c1));
00065          }
00066        } else if (!strcmp (token, "hsb")) {
00067          fprintf (stderr, "\ncolor special: hsb not implemented\n");
00068        } else { /* Must be a "named" color */
00069          dev_begin_named_color (token);
00070        }
00071        if (c1) RELEASE(c1);
00072        if (c2) RELEASE(c2);
00073        if (c3) RELEASE(c3);
00074        if (c4) RELEASE(c4);
00075        RELEASE (token);
00076       }
00077     } else if (!strcmp (command, "pop")) { /* Handle a pop */
00078       dev_end_color();
00079     } else { /* Assume this is a default color change */
00080       token = command;
00081       if (!strcmp (token, "rgb")) { /* Handle rgb color */
00082        if ((c1=parse_number(start, end)) &&
00083            (c2=parse_number(start, end)) &&
00084            (c3=parse_number(start, end))) {
00085          dev_set_def_rgb_color (atof(c1), atof(c2), atof(c3));
00086        }
00087       }
00088       else if (!strcmp (token, "cmyk")) { /* Handle rgb color */
00089        if ((c1=parse_number(start, end)) &&
00090            (c2=parse_number(start, end)) &&
00091            (c3=parse_number(start, end)) &&
00092            (c4=parse_number(start, end))) {
00093          dev_set_def_cmyk_color (atof(c1), atof(c2), atof(c3), atof(c4));
00094        }
00095       } else if (!strcmp (token, "gray")) { /* Handle gray */
00096        if ((c1=parse_number(start, end))) {
00097          dev_set_def_gray (atof(c1));
00098        }
00099       } else if (!strcmp (token, "hsb")) {
00100        fprintf (stderr, "\ncolor special: hsb not implemented\n");
00101       } else { /* Must be a "named" color */
00102        dev_set_def_named_color (token);
00103       }
00104       if (c1) RELEASE(c1);
00105       if (c2) RELEASE(c2);
00106       if (c3) RELEASE(c3);
00107       if (c4) RELEASE(c4);
00108     }
00109   }
00110   RELEASE (command);
00111 }
00112 
00113 static void do_background_special (char **start, char *end)
00114 {
00115   char *token;
00116   char *c1=NULL, *c2=NULL, *c3=NULL, *c4=NULL;
00117   skip_white (start, end);
00118   if ((token = parse_ident (start, end))) {
00119     if (!strcmp (token, "rgb")) { /* Handle rgb color */
00120       if ((c1=parse_number(start, end)) &&
00121          (c2=parse_number(start, end)) &&
00122          (c3=parse_number(start, end))) {
00123        dev_bg_rgb_color (atof(c1), atof(c2), atof(c3));
00124       }
00125     }
00126     else if (!strcmp (token, "cmyk")) { /* Handle rgb color */
00127       if ((c1=parse_number(start, end)) &&
00128          (c2=parse_number(start, end)) &&
00129          (c3=parse_number(start, end)) &&
00130          (c4=parse_number(start, end))) {
00131        dev_bg_cmyk_color (atof(c1), atof(c2), atof(c3), atof(c4));
00132       }
00133     } else if (!strcmp (token, "gray")) { /* Handle gray */
00134       if ((c1=parse_number(start, end))) {
00135        dev_bg_gray (atof(c1));
00136       }
00137     } else if (!strcmp (token, "hsb")) {
00138       fprintf (stderr, "\ncolor special: hsb not implemented\n");
00139     } else { /* Must be a "named" color */
00140       dev_bg_named_color (token);
00141     }
00142     if (c1) RELEASE(c1);
00143     if (c2) RELEASE(c2);
00144     if (c3) RELEASE(c3);
00145     if (c4) RELEASE(c4);
00146     RELEASE (token);
00147   }
00148 }
00149 
00150 static void local_set_paper_size (char **start, char *end)
00151 {
00152   char *key, *val, *val_start, *val_end, *number;
00153   char *save = *start;
00154   double width = 0.0, height = 0.0, unit;
00155   parse_key_val (start, end, &key, &val);
00156   if (key && val) {
00157     val_start = val;
00158     val_end = val+strlen(val);
00159     skip_white (&val_start, val_end);
00160     if ((number=parse_number(&val_start, val_end))) {
00161       width = atof (number);
00162       RELEASE (number);
00163       if ((unit = parse_one_unit (&val_start, val_end)) > 0.0) {
00164        width *= unit;
00165       } else {
00166        width = 0.0;
00167       }
00168     } else {
00169       fprintf (stderr, "\nExpecting a number here\n");
00170       dump (val_start, end);
00171     }
00172     skip_white (&val_start, val_end);
00173     if (val_start < val_end && *(val_start++) == ',') {
00174       skip_white (&val_start, val_end);
00175     }
00176     if ((number=parse_number(&val_start, val_end))) {
00177       height = atof (number);
00178       RELEASE (number);
00179       if ((unit = parse_one_unit (&val_start, val_end)) > 0.0) {
00180        height *= unit;
00181       } else {
00182        height = 0.0;
00183       }
00184     } else {
00185       fprintf (stderr, "\nExpecting a number here\n");
00186       dump (val_start, val_end);
00187     }
00188     if (key) RELEASE (key);
00189     if (val) RELEASE (val);
00190   }
00191   if (width != 0.0 && height != 0.0) {
00192     dev_set_page_size (width, height);
00193   } else{
00194     fprintf (stderr, "\nError parsing papersize special\n");
00195     dump (save, end);
00196   }
00197 }
00198 
00199 /* Color_special() handles color specials and also
00200    handles papersize and landscape specials since
00201    there's no good place to put them  */
00202 
00203 int color_special (char *buffer, UNSIGNED_QUAD size)
00204 {
00205   char *start = buffer, *end;
00206   int result = 0;
00207   end = buffer + size;
00208   skip_white (&start, end);
00209   if (!strncmp (start, "color", strlen("color"))) {
00210     start += strlen("color");
00211     result = 1; /* This is a color special */
00212     if (!ignore_colors)
00213       do_color_special (&start, end);
00214   } else if (!strncmp (start, "background", strlen("background"))) {
00215     start += strlen("background");
00216     result = 1; /* This is a color special */
00217     if (!ignore_colors)
00218       do_background_special (&start, end);
00219   } else if (!strncmp (start, "landscape", strlen("landscape"))) {
00220     set_landscape_mode();
00221     result = 1;
00222   } else if (!strncmp (start, "papersize", strlen("papersize"))) {
00223     local_set_paper_size(&start, end);
00224     result = 1;
00225   }
00226   return result;
00227 }
00228 
00229 
00230