]>
git.ipfire.org Git - thirdparty/cups.git/blob - ppdc/ppdc-import.cxx
4 // PPD file import methods for the CUPS PPD Compiler.
6 // Copyright 2007 by Apple Inc.
7 // Copyright 2002-2006 by Easy Software Products.
9 // These coded instructions, statements, and computer programs are the
10 // property of Apple Inc. and are protected by Federal copyright
11 // law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 // which should have been included with this file. If this file is
13 // file is missing or damaged, see the license at "http://www.cups.org/".
17 // ppdcSource::import_ppd() - Import a PPD file.
18 // ppd_gets() - Get a line from a PPD file.
22 // Include necessary headers...
30 // 'ppdcSource::import_ppd()' - Import a PPD file.
33 int // O - 1 on success, 0 on failure
34 ppdcSource::import_ppd(const char *f
) // I - Filename
36 int i
, j
, k
; // Looping vars
37 cups_file_t
*fp
; // File
38 char line
[256], // Comment line
39 *ptr
; // Pointer into line
40 ppd_file_t
*ppd
; // PPD file data
41 ppd_group_t
*group
; // PPD group
42 ppd_option_t
*option
; // PPD option
43 ppd_choice_t
*choice
; // PPD choice
44 ppd_attr_t
*attr
; // PPD attribute
45 ppd_const_t
*constraint
; // PPD UI constraint
46 ppd_const_t
*constraint2
; // Temp PPD UI constraint
47 ppd_size_t
*size
; // PPD page size
48 ppdcDriver
*driver
; // Driver
49 ppdcFont
*font
; // Font
50 ppdcGroup
*cgroup
; // UI group
51 ppdcOption
*coption
; // UI option
52 ppdcChoice
*cchoice
; // UI choice
53 ppdcConstraint
*cconstraint
; // UI constraint
54 ppdcMediaSize
*csize
; // Media size
57 // Try opening the PPD file...
58 if ((ppd
= ppdOpenFile(f
)) == NULL
)
61 // All PPD files need a PCFileName attribute...
68 // See if the driver has already been imported...
69 if ((driver
= find_driver(ppd
->pcfilename
)) == NULL
)
71 // Create a new PPD file...
72 if ((fp
= cupsFileOpen(f
, "r")) == NULL
)
78 driver
= new ppdcDriver();
79 driver
->type
= PPDC_DRIVER_PS
;
83 // Read the initial comments from the PPD file and use them as the
84 // copyright/license text...
85 cupsFileGets(fp
, line
, sizeof(line
));
86 // Skip *PPD-Adobe-M.m
88 while (cupsFileGets(fp
, line
, sizeof(line
)))
89 if (strncmp(line
, "*%", 2))
93 for (ptr
= line
+ 2; isspace(*ptr
); ptr
++);
95 driver
->add_copyright(ptr
);
100 // Then add the stuff from the PPD file...
101 if (ppd
->modelname
&& ppd
->manufacturer
&&
102 !strncasecmp(ppd
->modelname
, ppd
->manufacturer
,
103 strlen(ppd
->manufacturer
)))
105 ptr
= ppd
->modelname
+ strlen(ppd
->manufacturer
);
107 while (isspace(*ptr
))
111 ptr
= ppd
->modelname
;
113 driver
->manufacturer
= new ppdcString(ppd
->manufacturer
);
114 driver
->model_name
= new ppdcString(ptr
);
115 driver
->pc_file_name
= new ppdcString(ppd
->pcfilename
);
116 attr
= ppdFindAttr(ppd
, "FileVersion", NULL
);
117 driver
->version
= new ppdcString(attr
? attr
->value
: NULL
);
118 driver
->model_number
= ppd
->model_number
;
119 driver
->manual_copies
= ppd
->manual_copies
;
120 driver
->color_device
= ppd
->color_device
;
121 driver
->throughput
= ppd
->throughput
;
123 attr
= ppdFindAttr(ppd
, "DefaultFont", NULL
);
124 driver
->default_font
= new ppdcString(attr
? attr
->value
: NULL
);
126 // Collect media sizes...
127 ppd_option_t
*region_option
, // PageRegion option
128 *size_option
; // PageSize option
129 ppd_choice_t
*region_choice
, // PageRegion choice
130 *size_choice
; // PageSize choice
132 region_option
= ppdFindOption(ppd
, "PageRegion");
133 size_option
= ppdFindOption(ppd
, "PageSize");
135 for (i
= ppd
->num_sizes
, size
= ppd
->sizes
; i
> 0; i
--, size
++)
137 // Don't do custom size here...
138 if (!strcasecmp(size
->name
, "Custom"))
141 // Get the code for the PageSize and PageRegion options...
142 region_choice
= ppdFindChoice(region_option
, size
->name
);
143 size_choice
= ppdFindChoice(size_option
, size
->name
);
145 // Create a new media size record and add it to the driver...
146 csize
= new ppdcMediaSize(size
->name
, size_choice
->text
, size
->width
,
147 size
->length
, size
->left
, size
->bottom
,
148 size
->width
- size
->right
,
149 size
->length
- size
->top
,
150 size_choice
->code
, region_choice
->code
);
152 driver
->add_size(csize
);
154 if (!strcasecmp(size_option
->defchoice
, size
->name
))
155 driver
->set_default_size(csize
);
158 // Now all of the options...
159 for (i
= ppd
->num_groups
, group
= ppd
->groups
; i
> 0; i
--, group
++)
161 cgroup
= new ppdcGroup(group
->name
, group
->text
);
162 driver
->add_group(cgroup
);
164 for (j
= group
->num_options
, option
= group
->options
; j
> 0; j
--, option
++)
166 if (!strcmp(option
->keyword
, "PageSize") || !strcmp(option
->keyword
, "PageRegion"))
169 coption
= new ppdcOption((ppdcOptType
)option
->ui
, option
->keyword
,
170 option
->text
, (ppdcOptSection
)option
->section
,
172 cgroup
->add_option(coption
);
174 for (k
= option
->num_choices
, choice
= option
->choices
; k
> 0; k
--, choice
++)
176 cchoice
= new ppdcChoice(choice
->choice
, choice
->text
, choice
->code
);
177 coption
->add_choice(cchoice
);
179 if (!strcasecmp(option
->defchoice
, choice
->choice
))
180 coption
->set_defchoice(cchoice
);
185 // Now the constraints...
186 for (i
= ppd
->num_consts
, constraint
= ppd
->consts
;
190 for (j
= i
- 1, constraint2
= constraint
;
192 j
--, constraint2
++)
193 if (constraint
!= constraint2
&&
194 !strcmp(constraint
->option1
, constraint2
->option2
) &&
195 (constraint
->choice1
== constraint2
->choice2
||
196 (constraint
->choice1
&& constraint2
->choice2
&&
197 !strcmp(constraint
->choice1
, constraint2
->choice2
))) &&
198 !strcmp(constraint
->option2
, constraint2
->option1
) &&
199 (constraint
->choice2
== constraint2
->choice1
||
200 (constraint
->choice2
&& constraint2
->choice1
&&
201 !strcmp(constraint
->choice2
, constraint2
->choice1
))))
207 cconstraint
= new ppdcConstraint(constraint
->option1
, constraint
->choice1
,
208 constraint
->option2
, constraint
->choice2
);
209 driver
->add_constraint(cconstraint
);
212 for (i
= 0; i
< ppd
->num_attrs
; i
++)
214 attr
= ppd
->attrs
[i
];
216 if (!strcmp(attr
->name
, "Font"))
219 char encoding
[256], // Encoding string
220 version
[256], // Version string
221 charset
[256], // Charset string
222 status
[256]; // Status string
223 ppdcFontStatus fstatus
; // Status enumeration
226 if (sscanf(attr
->value
, "%s%*[^\"]\"%[^\"]\"%s%s", encoding
, version
,
227 charset
, status
) != 4)
229 fprintf(stderr
, "Bad font attribute: %s\n", attr
->value
);
233 if (!strcmp(status
, "ROM"))
234 fstatus
= PPDC_FONT_ROM
;
236 fstatus
= PPDC_FONT_DISK
;
238 font
= new ppdcFont(attr
->spec
, encoding
, version
, charset
, fstatus
);
240 driver
->add_font(font
);
242 else if ((strncmp(attr
->name
, "Default", 7) ||
243 !strcmp(attr
->name
, "DefaultColorSpace")) &&
244 strcmp(attr
->name
, "ColorDevice") &&
245 strcmp(attr
->name
, "Manufacturer") &&
246 strcmp(attr
->name
, "ModelName") &&
247 strcmp(attr
->name
, "MaxMediaHeight") &&
248 strcmp(attr
->name
, "MaxMediaWidth") &&
249 strcmp(attr
->name
, "NickName") &&
250 strcmp(attr
->name
, "ShortNickName") &&
251 strcmp(attr
->name
, "Throughput") &&
252 strcmp(attr
->name
, "PCFileName") &&
253 strcmp(attr
->name
, "FileVersion") &&
254 strcmp(attr
->name
, "FormatVersion") &&
255 strcmp(attr
->name
, "VariablePaperSize") &&
256 strcmp(attr
->name
, "LanguageEncoding") &&
257 strcmp(attr
->name
, "LanguageVersion"))
260 driver
->add_attr(new ppdcAttr(attr
->name
, attr
->spec
, attr
->text
,
263 else if (!strncmp(attr
->name
, "Default", 7) &&
264 !ppdFindOption(ppd
, attr
->name
+ 7) &&
265 strcmp(attr
->name
, "DefaultFont") &&
266 strcmp(attr
->name
, "DefaultImageableArea") &&
267 strcmp(attr
->name
, "DefaultPaperDimension") &&
268 strcmp(attr
->name
, "DefaultFont"))
270 // Default attribute...
271 driver
->add_attr(new ppdcAttr(attr
->name
, attr
->spec
, attr
->text
,