summaryrefslogtreecommitdiff
path: root/src/foomatic/foomatic-generator.in
blob: 626bc9c8d83a105e56e40564e2e6e9dcea0c48b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
#!@PERL@
# -*- perl -*-

# Get command line options
use Getopt::Long;
Getopt::Long::Configure("no_ignore_case", "pass_through");
GetOptions("t=s" => \$opt_t,         # Template directory
	   "f=s" => \$opt_f,         # Foomatic version
           "d=s" => \@opt_d,         # which Drivers?
	   "s"   => \$opt_s,         # Generate simplified data
	   "x"   => \$opt_x);	     # Generate printer list

my $simplified = 0;
my $simplified_subopt = "";
my $simplified_suffix = "";
if ($opt_s) {
    $simplified = 1;
    $simplified_subopt = "-s";
    $simplified_suffix = "-simplified";
}
    

my $templatedir;
$templatedir = $opt_t;

my @drivertypes = ("ijs");

exit 0 if ($#drivertypes < 0); # Nothing to be done, exit silently

# Do we have Foomatic 2.9 or newer
my $foomatic3 = ((defined($opt_f)) && ($opt_f >= 2.9));

# The following substitutions happen to the template XML files:
# @@STPVER@@         - the version number (e. g. "5.0.0-beta4")
# @@STPREL@@         - the version number (e. g. "5.0")
# @@REMAP@@          - option remappings (to shorten names)
# @@DRVNAME@@        - driver name (ie gutenprint)
# @@STPRINTERS@@     - <printers>...</printers> clause for the driver
# @@OPTCONSTRAINTS@@ - <constraints>...</constraints> object for the option
# @@ENUMVALS@@       - <enum_vals>...</enum_vals> section for the enum option
# @@MINVAL@@         - minimum value for numeric setting
# @@MAXVAL@@         - maximum value for numeric setting
# @@DEFVAL@@         - default value for numeric setting

# For some things, there are option-specific root-around-n-writer
# functions.  So we have a dispatch table:
#
# function arguments are: ('StpDriverOptionName')

my $funcs = { 'Model' => { 'OPTCONSTRAINTS' => \&build_model_cons,
			   'ENUMVALS' => \&build_model_ev },
	      'RenderResolution' => { 'OPTCONSTRAINTS' => \&build_resolution_cons,
				  'ENUMVALS' => \&build_resolution_ev },
	      'PrintoutMode' => { 'OPTCONSTRAINTS' =>
				      \&build_printoutmode_cons,
				  'ENUMVALS' =>
				      \&build_printoutmode_ev },
	  };

my $drivervals = { 'PageSize' => \&optmap_pagesize,
		   'Color' => \&optmap_color
	       };

my $debug = 0;

$colormap = { 'ijs' => { 'Grayscale' => 'DeviceGray',
			 'Color' => 'DeviceRGB',
			 'BlackAndWhite' =>
			     'DeviceGray -dBitsPerSample=1',
			 'RawCMYK' => 'DeviceCMYK' }
	    };

my $groupname_prefix = "Gutenprint";

my @parameter_class_names = (
			     "Printer Features",
			     "Output Control"
			     );

my @parameter_level_names = (
			     "Common",
			     "Extra 1",
			     "Extra 2",
			     "Extra 3",
			     "Extra 4",
			     "Extra 5"
			     );

my $option_remap = "";
my $opt_index = 0;

if (! $opt_x) {
    open PIPE, "./printer_options $simplified_subopt|" or die "Cannot run printer_options: $!\n";
    print STDERR "Loading options from ./printer_options $simplified_subopt...";
    while(<PIPE>) {
	#print $_;
	next if m!^#!;
	eval $_;
    }
    close PIPE or die "Cannot run printer_options: $!\n";
    print STDERR "done.\n";

    if ($foomatic3) {
	open PIPE, "./printer_margins $simplified_subopt|" or die "Cannot run printer_margins: $!\n";
	print STDERR "Loading margins from ./printer_margins $simplified_subopt...";
	while(<PIPE>) {
	    #print $_;
	    next if m!^#!;
	    eval $_;
	}
	close PIPE or die "Cannot run printer_margins: $!\n";
	print STDERR "done.\n";
    }
}

%foomap = ();
%mapfoo = ();
$errors = 0;

open PIPE, "./printers|" or die "Cannot run printers: $!\n";
print STDERR "Loading printers from ./printers...";
while(<PIPE>) {
    #print $_;
    next if m!^#!;
    eval $_ or die "\nCan't parse `$_' $@\n";
}
close PIPE or die "Cannot run printers: $!\n";
die "\nCannot continue\n" if ($errors > 0);
print STDERR "done.\n";

if ($opt_x) {
    my (%printer_long_name) = reverse %printer_name;
    for (sort keys %printer_long_name) {
	my ($pn) = $printer_long_name{$_};
	next if $printer_family{$pn} eq "ps" || $printer_family{$pn} eq "raw";
	printf("%s	%s	%s\n", $_, $pn, $foomap{$pn});
    }
    exit;
}

# OK, now %stpdata is a big honking thing, and %defaults is in there, too.

# Invert, to build %bar{$optionname} = [ choice1, choice2 ];
my ($a, $b, $otmp, $vtmp);
for $a (keys(%stpdata)) {
    for $otmp (keys %{$stpdata{$a}}) {
	for $vtmp (keys (%{$stpdata{$a}{$otmp}})) {
	    if (!$seen_evchoice{$otmp}{$vtmp}++) {
		push (@{$ev_choices{$otmp}}, [ $vtmp,
					       $stpdata{$a}{$otmp}{$vtmp}]);
	    }
	}
    }
}

#print Dumper(%ev_choices);

if ($foomatic3) {
    # Generate data for "PrintoutMode" option, only needed for
    # Foomatic 2.9.x or newer
    print STDERR "Generating data for \"PrintoutMode\" option...";
    ($printoutmode, $printoutmodechoices) = getprintoutmode();
    print STDERR "done.\n";
    # Foomatic >= 2.9: Make a list of all choice entries needed in the
    # "PrintoutMode" option XML file. Note: every choice ("Draft",
    # "Normal", ...) will appear several times, but with different
    # strings in "<ev_driverval>". Constraints will make only the
    # right choices getting into the PPD file. Assign a unique ID to
    # each entry.
    for $a (keys(%{$printoutmode})) {
	for $vtmp (keys %{$printoutmode->{$a}}) {
	    my $mode = $printoutmode->{$a}{$vtmp};
	    if (!$seen_modes{$vtmp}{$mode}++) {
		if (!defined($nums{$vtmp})) {
		    $nums{$vtmp} = 0;
		}
		$nums{$vtmp} ++;
		$modes{$vtmp}{$mode} = "$vtmp-$nums{$vtmp}";
	    }
	}
    }
}

# Make list of needed Foomatic entries for the numerical options. If
# for one and the same numerical option there are printers with
# different value ranges, there must be made an extra Foomatic entry
# for each value range. Therefore the filenames of numerical options
# are numbered (eg. Contrast-1.xml).
for $a (keys(%stp_float_values)) {
    for $otmp (keys %{$stp_float_values{$a}}) {
	for $vtmp (keys %{$stp_float_values{$a}{$otmp}}) {
	    my $min = $stp_float_values{$a}{$otmp}{'MINVAL'};
	    my $max = $stp_float_values{$a}{$otmp}{'MAXVAL'};
	    my $def = $stp_float_values{$a}{$otmp}{'DEFVAL'};
	    # Skip options with invalid values, the library contains such
	    # options in the case when only one constant value is allowed
	    next if (($min >= $max) || ($def < $min) || ($def > $max));
	    my $minmax = "${min}_${max}";
	    if (!$seen_fnumopt{$otmp}{$minmax}++) {
		if (!defined($fnums{$otmp})) {
		    $fnums{$otmp} = 0;
		}
		$fnums{$otmp} ++;
		push (@floatnumopts_list, "${otmp}-$fnums{$otmp}");
		$numopt_ranges{"${otmp}-$fnums{$otmp}"}{'MINVAL'} = $min;
		$numopt_ranges{"${otmp}-$fnums{$otmp}"}{'MAXVAL'} = $max;
	    }
	}
    }
}
for $a (keys(%stp_int_values)) {
    for $otmp (keys %{$stp_int_values{$a}}) {
	for $vtmp (keys %{$stp_int_values{$a}{$otmp}}) {
	    my $min = $stp_int_values{$a}{$otmp}{'MINVAL'};
	    my $max = $stp_int_values{$a}{$otmp}{'MAXVAL'};
	    my $def = $stp_int_values{$a}{$otmp}{'DEFVAL'};
	    # Skip options with invalid values, the library contains such
	    # options in the case when only one constant value is allowed
	    next if (($min >= $max) || ($def < $min) || ($def > $max));
	    my $minmax = "${min}_${max}";
	    if (!$seen_inumopt{$otmp}{$minmax}++) {
		if (!defined($inums{$otmp})) {
		    $inums{$otmp} = 0;
		}
		$inums{$otmp} ++;
		push (@intnumopts_list, "${otmp}-$inums{$otmp}");
		$numopt_ranges{"${otmp}-$inums{$otmp}"}{'MINVAL'} = $min;
		$numopt_ranges{"${otmp}-$inums{$otmp}"}{'MAXVAL'} = $max;
	    }
	}
    }
}

for $a (keys(%stp_dimension_values)) {
    for $otmp (keys %{$stp_dimension_values{$a}}) {
	for $vtmp (keys %{$stp_dimension_values{$a}{$otmp}}) {
	    my $min = $stp_dimension_values{$a}{$otmp}{'MINVAL'};
	    my $max = $stp_dimension_values{$a}{$otmp}{'MAXVAL'};
	    my $def = $stp_dimension_values{$a}{$otmp}{'DEFVAL'};
	    # Skip options with invalid values, the library contains such
	    # options in the case when only one constant value is allowed
	    next if (($min >= $max) || ($def < $min) || ($def > $max));
	    my $minmax = "${min}_${max}";
	    if (!$seen_inumopt{$otmp}{$minmax}++) {
		if (!defined($inums{$otmp})) {
		    $inums{$otmp} = 0;
		}
		$inums{$otmp} ++;
		push (@dimensionnumopts_list, "${otmp}-$inums{$otmp}");
		$numopt_ranges{"${otmp}-$inums{$otmp}"}{'MINVAL'} = $min;
		$numopt_ranges{"${otmp}-$inums{$otmp}"}{'MAXVAL'} = $max;
	    }
	}
    }
}

#print join("\n", @floatnumopts_list);
#print join("\n", @intnumopts_list);
#print join("\n", @dimensionnumopts_list);

# Figure out version etc
open PIPE, "./gutenprint-version|" or die "Cannot run gutenprint-version: $!\n";
my $stpvers = <PIPE>;
close PIPE or die "Cannot run gutenprint-version: $!\n";
chomp $stpvers;

open PIPE, "./gutenprint-version -|" or die "Cannot run gutenprint-version: $!\n";
my $stprel = <PIPE>;
close PIPE or die "Cannot run gutenprint-version: $!\n";
chomp $stprel;

# Build <printers> clause...
my @printerlist = ();
push (@printerlist, " <printers>\n");
my $p1;
for $p1 (keys(%mapstp)) {
    push (@printerlist, "  <!-- gutenprint driver: $p1 -->\n");
    for my $id (@{$mapstp{$p1}}) {
	if ($foomatic3) {
	    # Add unprintable margins (only Foomatic 2.9.x)
	    push(@printerlist, "  <printer>\n");
	    push(@printerlist, "    <id>$id</id>\n");
	    push(@printerlist, "    <margins>\n");
	    my ($cleft, $cright, $ctop, $cbottom) =
		(undef, undef, undef, undef);
	    if (defined($imageableareas{$p1}{'Custom'})) {
		$cleft = $imageableareas{$p1}{'Custom'}{'left'};
		$cright = $imageableareas{$p1}{'Custom'}{'right'};
		$ctop = $imageableareas{$p1}{'Custom'}{'top'};
		$cbottom = $imageableareas{$p1}{'Custom'}{'bottom'};
		push(@printerlist, "      <general>\n");
		push(@printerlist, "        <relative />\n");
		push(@printerlist, "        <left>$cleft</left>\n");
		push(@printerlist, "        <right>$cright</right>\n");
		push(@printerlist, "        <top>$ctop</top>\n");
		push(@printerlist, "        <bottom>$cbottom</bottom>\n");
		push(@printerlist, "      </general>\n");
	    }
	    for my $ps (keys %{$imageableareas{$p1}}) {
		next if $ps eq 'Custom'; # We have done "Custom" already
		my ($left, $right, $top, $bottom, $width, $height);
		$left = $imageableareas{$p1}{$ps}{'left'};
		$right = $imageableareas{$p1}{$ps}{'right'};
		$top = $imageableareas{$p1}{$ps}{'top'};
		$bottom = $imageableareas{$p1}{$ps}{'bottom'};
		$width = $imageableareas{$p1}{$ps}{'width'};
		$height = $imageableareas{$p1}{$ps}{'height'};
		# If the <general> section serves for this paper size,
		# do not define an <exception>
		next if ((defined($cleft)) &&
			 ($left == $cleft) &&
			 ($right == $width - $cright) &&
			 ($top == $height - $ctop) &&
			 ($bottom == $cbottom));
		push(@printerlist, "      <exception PageSize=\"$ps\">\n");
		push(@printerlist, "        <absolute />\n");
		if ($left != $cleft) {
		    push(@printerlist, "        <left>$left</left>\n");
		}
		if ($right != $width - $cright) {
		    push(@printerlist, "        <right>$right</right>\n");
		}
		if ($top != $height - $ctop) {
		    push(@printerlist, "        <top>$top</top>\n");
		}
		if ($bottom != $cbottom) {
		    push(@printerlist, "        <bottom>$bottom" .
			 "</bottom>\n");
		}
		push(@printerlist, "      </exception>\n");
	    }
	    push(@printerlist, "    </margins>\n");
	    push(@printerlist, "  </printer>\n");
	} else {
	    # Printer IDs only
	    push(@printerlist, "  <printer><id>$id</id></printer>\n");
	}
    }
}
push (@printerlist, " </printers>\n");

$drivernameprefix = "gutenprint";
print STDERR "Using driver name prefix \"$drivernameprefix\"\n";

my $generalsubs = { 'STPVER' => $stpvers,
		    'STPREL' => $stprel,
		    'REMAP' => $option_remap,
		    'DRVNAME' => $drivernameprefix,
		    'STPRINTERS' => join('', @printerlist) };

my $optiongroups = { 'PageSize' => 'General',
		     'STP_InputSlot' => 'General',
		     'STP_MediaType' => 'General',
		     'PrintoutMode' => 'General',
		     'STP_RenderResolution' => 'General',
		     'STP_Quality' => 'General',
		     'Color' => 'General',
		     'STP_ImageType' => 'General'};

my @numericsubs = ('MINVAL', 'MAXVAL');

my $specialoutputfilenames = { 'Resolution' => 'PrinterResolution',
			       'RenderResolution' => 'Resolution' };

# OK, make the db directory...
mkdir "foomatic-db", 0755 or
    die "Cannot create directory foomatic-db: $!\n"
     	unless -d "foomatic-db";

# Now do stuff, already.  Do the substitution into each file...
my $tmpl;
for $drivertype (@drivertypes) {
    $drivertypesuffix = "-$drivertype";
    $drivertypesuffix =~ s/-gs//;
    $drivertypesuffix .= "$simplified_suffix.$stprel";
    my $drvname = "$drivernameprefix$drivertypesuffix";
    $generalsubs->{'DRVNAME'} = $drvname;
    print "Generating Foomatic data for driver \"$drvname\"...\n";

    # OK, make the db heirarchy alongside the templates one...
    mkdir "foomatic-db/$drvname", 0755 or
	die "Cannot create directory foomatic-db/$drvname: $!\n"
     	    unless -d "foomatic-db/$drvname";
    mkdir "foomatic-db/$drvname/opt", 0755 or
	die "Cannot create directory foomatic-db/$drvname/opt: $!\n"
	    unless -d "foomatic-db/$drvname/opt";
    mkdir "foomatic-db/$drvname/driver", 0755 or
	die "Cannot create directory foomatic-db/$drvname/driver: $!\n"
	    unless -d 'foomatic-db/$drvname/driver';

    my $order = 1000;
    my %numoptionorders;

#    opendir TDIR, "$templatedir-$drivertype" or
#	die "Cannot open templates directory: $!\n";

    for $fooopt (sort(keys(%ev_choices)),
		 sort(@floatnumopts_list, @dimensionnumopts_list,
		      @intnumopts_list), 
                 "PrintoutMode", "Model",
		 "RenderResolution", "gutenprint") {

	# The "PrintoutMode" option is only supported by Foomatic 2.9.x or
	# newer (preliminary skipped always, do not remove the
	# infrastructure for "PrintoutMode", it will be re-activated later
	# on)
	next if ((!$foomatic3) && ($fooopt eq "PrintoutMode"));

	# "x_resolution" and "y_resolution" appear in %ev_choices but are
	# not option names, so skip them
	next if $fooopt =~ /^[xy]_resolution$/;

	my ($num_opt, $type, $tmpl);
	if (member($fooopt, @floatnumopts_list)) {
	    $num_opt = 1;
	    $type = "float";
	    $tmpl = "NumericalOptions.xml";
	} elsif (member($fooopt, @intnumopts_list)) {
	    $num_opt = 1;
	    $type = "int";
	    $tmpl = "NumericalOptions.xml";
	} elsif (member($fooopt, @dimensionnumopts_list)) {
	    $num_opt = 1;
	    $type = "dimension";
	    $tmpl = "NumericalOptions.xml";
	} else {
	    $num_opt = 0;
	    $type = "enum";
	    $tmpl = "${fooopt}.xml";
	    $tmpl = "OtherOptions.xml"
		if ! -r "$templatedir-$drivertype/$tmpl";
	}

	# Remove number appended to the end of the file name of
	# numerical options
	my $shortname = $fooopt;
	$shortname =~ s!-\d+$!!;

	#my $stpopt = $argnamemap{$shortname};
	#$stpopt = $shortname if ! defined ($stpopt);
	my $stpopt = $shortname;

#	print STDERR "Argnamemap '$fooopt' => '$stpopt'\n";

	open TMPL, "$templatedir-$drivertype/$tmpl";
	my @datafile = <TMPL>;
	close TMPL;

	print STDERR "Processing \"$fooopt\"...";

	my $template = join('',@datafile);

	# First, do the generic substitutions.

	my ($substr);
	for $substr (keys(%$generalsubs)) {
	    my $substitution = $generalsubs->{$substr};
	    $template =~ s!\@\@$substr\@\@!$substitution!g;
	}

	# Put the options into PPD groups (Foomatic >= 2.9)

	if ($foomatic3) {
	    my $group;
	    if (defined($optiongroups->{$shortname})) {
		# Group names given by table (esp. "General" for special
		# Options)
		$group = $optiongroups->{$shortname};
	    } elsif (defined($param_classes{$shortname}) &&
		     defined($param_levels{$shortname}) &&
		     $parameter_class_names[$param_classes{$shortname}] &&
		     $parameter_level_names[$param_levels{$shortname}]) {
		# Group names given by libgutenprint
		$group = $groupname_prefix . " " .
		    $parameter_class_names[$param_classes{$shortname}] . " " .
		    $parameter_level_names[$param_levels{$shortname}];
		# Remove the spaces (the name with spaces Foomatic will
		# generate automatically)
		$group =~ s! !!g;
	    } else {
		# All the above did not assign a group name? Use
		# "Miscellaneous" (Should usually not happen)
		$group = "Miscellaneous";
	    }
	    my $substitution = "\n   <arg_group>" .
		$group . "</arg_group>";
	    $template =~ s!\@\@GROUP\@\@!$substitution!g;
	} else {
	    $template =~ s!\@\@GROUP\@\@!!g;
	}

	# Now do the numeric substitutions

	$template =~ s!\@\@TYPE\@\@!$type!g;
	for $substr (@numericsubs) {
	    my $substitution;
	    my $substitution = $numopt_ranges{$fooopt}{$substr};
	    $template =~ s!\@\@$substr\@\@!$substitution!g;
	}

	# Substitutions for generic template files

	my $lowercaseshortname = lc($fooopt);
	my $subopt = $stpopt;

	# The maximum length of the short name of an option is 31
	# characters to be accepted in a PPD. The maximum length of a
	# main keyword in a PPD is 40 characters, with
	# "FoomaticRIPDefault" having 18, a short name of a numerical
	# option (only those have "FoomaticRIPDefault" entries) is 22.

	# We do not touch options whose names are short enough.

	# As this problem did not occur with Gimp Print 4.2.7, none of
	# the option names modified here already existed in
	# Gimp-Print, all options concerned are new Gutenprint
	# options. We also do not modify the parameters to be inserted
	# in the GhostScript command line, so the Gutenprint library
	# or the IJS server do not need to be modified.

	my $maxlen = ($type eq "enum" ? 31 : 22);
	my $shortopt = $subopt;
	if (length($subopt) > $maxlen) {
	    my $nostpopt = $stpopt;
	    $nostpopt =~ s/^STP_//;
	    my $stropt = sprintf("%03d", $opt_index);
	    $shortopt = "STP_OPT_$stropt";
	    $opt_index++;
	}
	if ($tmpl !~ /^$fooopt/) {
	    $template =~ s!\@\@LOWERCASESHORTNAME\@\@!$lowercaseshortname!g;
	    $template =~ s!\@\@LONGNAME\@\@!$longnames{$shortname}!g;
	    $template =~ s!\@\@SHORTNAME\@\@!$shortopt!g;
	    if ($shortname =~ /^STP_Enable(.*)$/) {
		$numoptionorders{$1} = $order;
		$template =~ s!\@\@ORDER\@\@!$order!g;
		$order += 10;
	    } else {
		$shortname =~ /^STP_(.*)$/;
		my $basename = $1;
		if (my $enableorder = $numoptionorders{$basename}) {
		    $enableorder ++;
		    $template =~ s!\@\@ORDER\@\@!$enableorder!g;
		} else {
		    $template =~ s!\@\@ORDER\@\@!$order!g;
		    $order += 10;
		}
	    }
	    $template =~ s!\@\@SPOT\@\@!B!g;
	    $template =~ s!\@\@PROTO\@\@!$subopt=\%s,!g;
	}

	# Now do special-purpose substitutions

	for $substr (keys(%{$funcs->{$fooopt}})) {
	    my $substitution = &{$funcs->{$fooopt}{$substr}}($stpopt);
	    if (defined($substitution)) {
		$template =~ s!\@\@$substr\@\@!$substitution!g;
	    }
	}

	if ($template =~ m!\@\@OPTCONSTRAINTS\@\@!) {
	    my $substitution = ($num_opt ? build_num_cons($fooopt) :
				build_cons($stpopt));
	    # Skip this option if there are no constraints (no printers
	    # using this option)
	    if ($substitution !~ m!</constraint>!s) {
		print STDERR "No printers using $fooopt\n";
		next;
	    }
	    if (defined($substitution)) {
		$template =~ s!\@\@OPTCONSTRAINTS\@\@!$substitution!g;
	    }
	}
	if ($template =~ m!\@\@ENUMVALS\@\@!) {
	    my $substitution = build_ev($stpopt);
	    # Skip this option if there are no choices (an enum option
	    # without choices does not make sense)
	    if ($substitution !~ m!</ev_shortname>!s) {
		print STDERR "No choices for $fooopt\n";
		next;
	    }
	    if (defined($substitution)) {
		$template =~ s!\@\@ENUMVALS\@\@!$substitution!g;
	    }
	}

	# Any more?
	grep (m!\@\@([^\@]+)\@\@!g
	      && do { warn "  Unknown substitution $1 in $fooopt!\n"; },
	      split("\n",$template));

	# File name for the option XML file

	$tmpl = "${fooopt}.xml" if $tmpl !~ /^$fooopt\.xml$/;

	if (my $f = $specialoutputfilenames->{$fooopt}) {
	    $tmpl = "${f}.xml";
	}

	# Finally, write out the new file

	# Options are under opt/
	my $dbfilename = lc("foomatic-db/$drvname/opt/$drvname-$tmpl");

	# Special case the actual driver file under driver/
	$dbfilename = "foomatic-db/$drvname/driver/$drvname.xml"
	    if ($tmpl eq 'gutenprint.xml');

	open NEWF, "> $dbfilename" or die "Cannot create $dbfilename: $!";
	print STDERR "writing $dbfilename...";
	print NEWF $template;
	print STDERR "done.\n";
	close NEWF;

    }

    closedir TDIR;

    # The paper size and resolution maps must be regenerated for the next
    # driver, because the "driverval"s are different for the different
    # drivers. So delete the caches.
    undef $pagemap;
    undef %rescache;

}


# member( $a, @b ) returns 1 if $a is in @b, 0 otherwise.
sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 };

sub get_ev_shortname {
    my ($val) = @_;
    $val =~ s/ //g;
    $val =~ s/\///g;
    $val =~ s/\://g;
    return $val;
}

sub get_ev_key {
    my ($val, $drv) = @_;
    return ("ev/$drv-" . get_ev_shortname($val));
}

sub build_ev {
    my ($stpopt) = @_;
    my $drvname = "$drivernameprefix$drivertypesuffix";

    my @vals = ();

    # OK, now for each enum_val
    my $ev;
    for $ev (@{$ev_choices{$stpopt}}) {
	#  Put in the basic choice info: ev names, etc
	my $ev_longname = @$ev[1];
	my $ev_shortname = @$ev[0];

	my $ev_id = get_ev_key($ev_shortname, $drvname);
	my $ev_driverval;

	# Either call a per-option function to get the driverval, or
	# just use the string choice name.
	if (defined($drivervals->{$stpopt})) {
	    $ev_driverval = &{$drivervals->{$stpopt}}($ev_shortname);
	    die "Undefined driverval for option $stpopt value $ev_shortname!\n"
		if (! defined($ev));
	} else {
	    $ev_driverval = $ev_shortname;
	}
	# Remove "Roll" paper sizes, user has to use "Custom" instead.
	next if (($stpopt eq "STP_PageSize") && ($ev_driverval eq ""));
	push (@vals,
	      "    <enum_val id='$ev_id'>\n",
	      "      <ev_longname><en>$ev_longname</en></ev_longname>\n",
	      "      <ev_shortname><en>$ev_shortname</en></ev_shortname>\n",
	      "      <ev_driverval>$ev_driverval</ev_driverval>\n",
	      "      <constraints>\n",
	      "        <!-- Assume the option doesn't apply... -->\n",
	      "        <constraint sense='false'>\n",
	      "          <driver>$drvname</driver>\n",
	      "        </constraint>\n");

	#   Build constraints for this particular choice
	my $stpprn;
	for $stpprn (keys(%stpdata)) {
	    my $fooprn;
	    for $fooprn (@{$mapstp{$stpprn}}) {
		if ($stpdata{$stpprn}{$stpopt}{$ev_shortname}) {
		    # OK, this choice applies to this printer
		    push (@vals,
			  "        <constraint sense='true'>\n",
			  "          <!-- $fooprn == $stpprn -->\n",
			  "          <driver>$drvname</driver>\n",
			  "          <printer>$fooprn</printer>\n",
			  "        </constraint>\n");
		}
	    }
	}

	push (@vals,
	      "      </constraints>\n",
	      "    </enum_val>\n");
    }

    return join('',
		"<enum_vals>\n",
		@vals,
		"  </enum_vals>\n");
}

sub build_cons {

    my ($stpopt) = @_;

    my $drvname = "$drivernameprefix$drivertypesuffix";

    my @PNCONS = ();

    # For each stp printer...
    my $stpname;
    for $stpname (keys(%stpdata)) {

	if (0) {
	    print STDERR "    Processing gutenprint printer $stpname...\n";
	    print STDERR
		"      There are no foomatic printers mapped to $stpname!?\n"
		    if !$mapstp{$stpname};
	    print STDERR "      \%stpdata key is {$stpname}{$stpopt}\n";
	}

	# Add this printer to argument constraints?
	if ($stpdata{$stpname}{$stpopt}) {

	    # What's the default value?
	    my $stpdef = $defaults{$stpname}{$stpopt};

	    # If there's no default, then this option doesn't apply to
	    # this printer.
	    if (defined($stpdef)) {

		my $foodefval = get_ev_key($stpdef, $drvname);

		if (0) {
		    print STDERR
			"      Default for $stpname/$stpopt is $stpdef aka $foodefval\n";
		}

		my $fooname;
		for $fooname (@{$mapstp{$stpname}}) {

		    if (0) {
			print STDERR
			    "      Printer $fooname takes option $stpopt.\n";
		    }

		    push (@PNCONS,
			  "    <constraint sense='true'>\n",
			  "      <driver>$drvname</driver>\n",
			  "      <printer>$fooname</printer><!-- gutenprint name: $stpname -->\n",
			  "      <arg_defval>$foodefval</arg_defval>\n",
			  "    </constraint>\n");
		}
	    }
	}

    }

    return join('',
		"<constraints>\n",
		@PNCONS,
		"  </constraints>\n");
}

sub build_num_cons {

    my ($foooptfile) = @_;

    my $drvname = "$drivernameprefix$drivertypesuffix";

    my @PNCONS = ();

    # Do we have a float or an int option?
    my $type;
    if (member($foooptfile, @floatnumopts_list)) {
       $type = "float";
    } elsif (member($foooptfile, @dimensionnumopts_list)) {
       $type = "dimension";
    } else {
       $type = "int";
    }

    # Name of the actual option
    my $stpopt = $foooptfile;
    $stpopt =~ s!-\d+$!!;

    # For each stp printer...
    my $stpname;
    for $stpname (keys(%stpdata)) {

	if (0) {
	    print STDERR "    Processing gutenprint printer $stpname...\n";
	    print STDERR
		"      There are no foomatic printers mapped to $stpname!?\n"
		    if !$mapstp{$stpname};
	}

	# Add this printer to argument constraints? The printer must provide
	# this option and a default value for it.
	my $stpdef;
	if ((defined($stpdef =
		     $stp_float_values{$stpname}{$stpopt}{"DEFVAL"})) ||
	    (defined($stpdef =
		     $stp_int_values{$stpname}{$stpopt}{"DEFVAL"})) ||
	    (defined($stpdef =
		     $stp_dimension_values{$stpname}{$stpopt}{"DEFVAL"}))) {
	    # Find minimum and maximum
	    my ($min, $max);
	    if ($type eq "float") {
		$min = $stp_float_values{$stpname}{$stpopt}{"MINVAL"};
		$max = $stp_float_values{$stpname}{$stpopt}{"MAXVAL"};
	    } elsif ($type eq "dimension") {
		$min = $stp_dimension_values{$stpname}{$stpopt}{"MINVAL"};
		$max = $stp_dimension_values{$stpname}{$stpopt}{"MAXVAL"};
	    } else {
		$min = $stp_int_values{$stpname}{$stpopt}{"MINVAL"};
		$max = $stp_int_values{$stpname}{$stpopt}{"MAXVAL"};
	    }
	    # Does the range of this option with this printer match
	    # the Foomatic option entry we are building currently?
	    if (defined($numopt_ranges{$foooptfile}{'MINVAL'}) &&
		defined($numopt_ranges{$foooptfile}{'MAXVAL'}) &&
		($min == $numopt_ranges{$foooptfile}{'MINVAL'}) &&
		($max == $numopt_ranges{$foooptfile}{'MAXVAL'})) {

		if (0) {
		    print STDERR
			"      Default for $stpname/$stpopt is $stpdef\n";
		}

		my $fooname;
		for $fooname (@{$mapstp{$stpname}}) {

		    if (0) {
			print STDERR
			    "      Printer $fooname takes option $stpopt.\n";
		    }

		    push (@PNCONS,
			  "    <constraint sense='true'>\n",
			  "      <driver>$drvname</driver>\n",
			  "      <printer>$fooname</printer><!-- gutenprint name: $stpname -->\n",
			  "      <arg_defval>$stpdef</arg_defval>\n",
			  "    </constraint>\n");
		}
	    }
	}

    }

    return join('',
		"<constraints>\n",
		@PNCONS,
		"  </constraints>\n");
}

sub optmap_pagesize {
    my ($value) = @_;

    if (!defined $pagemap) {
	open PUTIL, "./paper_sizes |" or die "Cannot run paper_sizes: $!\n";
	while (<PUTIL>) {
	    chomp;
	    $_ =~ m!^\s*(.+\S)\s+([0-9]+)\s+([0-9]+)\s*$!;
	    my ($name, $width, $height) = ($1, $2, $3);
	    if (($width > 0 and $height > 0) or
		($name eq "Custom")) {
		$pagemap->{$name} = "-dDEVICEWIDTHPOINTS=$width -dDEVICEHEIGHTPOINTS=$height";
#		print STDERR "PageSize '$name' driverval '$width $height'\n";
	    }
	}
	close PUTIL;
    }

    return $pagemap->{$value}
}

sub optmap_color {
    my ($value) = @_;
    if (defined $colormap->{$drivertype}{$value}) {
	return $colormap->{$drivertype}{$value};
    } else {
	die "Cannot map output type '$value'\n";
    }
}

sub build_model_cons {
    my ($stpopt) = @_;
    my $drvname = "$drivernameprefix$drivertypesuffix";


    # OK, this is funky.  For each stp model, we have a choice.  That
    # choice is valid for only the foo printers that correspond.  For
    # any given foo printer, there is *exactly one* available choice.
    # The defval is the one available choice.  Backends and
    # applications do not show options with only one choice; they just
    # select that choice.  So we don't bother to make pretty option
    # names or anything.
    #
    # See also build_model_ev()

    my @PNCONS = ();

    # For each stp printer...
    my $stpname;
    for $stpname (keys(%mapstp)) {

	# For each possible foo name
	my $fooname;
	for $fooname (@{$mapstp{$stpname}}) {

	    # What's the default value?
	    my $foodefval = get_ev_key($stpname, $drvname);

	    push (@PNCONS,
		  "    <constraint sense='true'>\n",
		  "      <driver>$drvname</driver>\n",
		  "      <printer>$fooname</printer>\n",
		  "      <arg_defval>$foodefval</arg_defval>\n",
		  "    </constraint>\n");
	}
    }

    return join('',
		"<constraints>\n",
		@PNCONS,
		"  </constraints>\n");


}

# See build_model_cons, above.
sub build_model_ev {
    my ($stpopt) = @_;
    my $drvname = "$drivernameprefix$drivertypesuffix";

    my @vals = ();

    # OK, now for each enum_val
    my $ev;
    for $ev (keys(%mapstp)) {
	#  Put in the basic choice info: ev names, etc
	my $ev_shortname = $ev;
	my $ev_longname = $printer_name{$ev};
	my $make = $printer_make{$ev};
	my $ev_shortname = get_ev_shortname($ev);
	my $ev_id = get_ev_key($ev, $drvname);
	my $ev_driverval;
	$ev_driverval = "-sDeviceManufacturer=$make -sDeviceModel=$ev";
	push (@vals,
	      "    <enum_val id='$ev_id'>\n",
	      "      <ev_longname><en>$ev_longname</en></ev_longname>\n",
	      "      <ev_shortname><en>$ev_shortname</en></ev_shortname>\n",
	      "      <ev_driverval>$ev_driverval</ev_driverval>\n",
	      "      <constraints>\n",
	      "        <!-- Assume the option doesn't apply... -->\n",
	      "        <constraint sense='false'>\n",
	      "          <driver>$drvname</driver>\n",
	      "        </constraint>\n",
	      "        <!-- ...except to these: -->\n",
	      );

	# This stp Model value applies only to mapped foo printers
	my $fooprn;
	for $fooprn (@{$mapstp{$ev}}) {

	    # OK, this choice applies to this enum
	    push (@vals,
		  "        <constraint sense='true'>\n",
		  "          <!-- Model $ev for $fooprn -->\n",
		  "          <driver>$drvname</driver>\n",
		  "          <printer>$fooprn</printer>\n",
		  "        </constraint>\n");
	}

	push (@vals,
	      "      </constraints>\n",
	      "    </enum_val>\n");
    }

    return join('',
		"<enum_vals>\n",
		@vals,
		"  </enum_vals>\n");
}


# Stuff for Resolution.
#
# printer_options gives us Quality information.  We examine this to
# determine what to do for the gs resolution argument.

sub compute_resolutions {
    my ($stpname) = @_;

    my $drvname = "$drivernameprefix$drivertypesuffix";

    if (!defined($rescache{$stpname})) {

	my @reslist = ();
	my %hash;
	my $defval;

	my $qual;
	for $qual (keys(%{$stpdata{$stpname}{'STP_Resolution'}})) {
	    my ($x) = $stpdata{$stpname}{'x_resolution'}{$qual};
	    my ($y) = $stpdata{$stpname}{'y_resolution'}{$qual};

	    my $r = {'x' => $x,
		     'y' => $y,
		     'driverval' => "${x}x${y}",
		     'ev_key' => get_ev_key("res-$x-$y", $drvname)
		     };
	    push (@reslist, $r);

	    # Default?
	    $defval = get_ev_key("res-$x-$y", $drvname)
		if ($qual eq $defaults{$stpname}{'STP_Resolution'});

	    # Note that this resolution value exists
	    $resolutions{"$x $y"} = { 'x' => $x,
				      'y' => $y };

	    # Note that this printer takes this resolution
	    $hash{$x}{$y} = 1;

	}

	$rescache{$stpname}{'list'} = \@reslist;
	$rescache{$stpname}{'defval'} = $defval;
	$rescache{$stpname}{'takesit'} = \%hash;
    }

    return $rescache{$stpname};
}

sub do_all_res {
    my $n;
    for $n (keys(%mapstp)) {
	compute_resolutions($n);
    }
}

sub build_resolution_ev {
    my ($stpopt) = @_;
    my $drvname = "$drivernameprefix$drivertypesuffix";

    my @vals = ();

    do_all_res();

    # OK, now for each possible resolution...
    my $ev;
    for $ev (keys(%resolutions)) {

	my ($x, $y) = ($resolutions{$ev}{'x'}, $resolutions{$ev}{'y'});

	#  Put in the basic choice info: ev names, etc
	my $ev_longname = "$x x $y dpi";
	my $ev_shortname = get_ev_shortname($ev_longname);
	my $ev_id = get_ev_key("res-$x-$y", $drvname);
	my $ev_driverval = "${x}x${y}";

	push (@vals,
	      "    <enum_val id='$ev_id'>\n",
	      "      <ev_longname><en>$ev_longname</en></ev_longname>\n",
	      "      <ev_shortname><en>$ev_shortname</en></ev_shortname>\n",
	      "      <ev_driverval>$ev_driverval</ev_driverval>\n",
	      "      <constraints>\n",
	      "        <!-- Assume the option doesn't apply... -->\n",
	      "        <constraint sense='false'>\n",
	      "          <driver>$drvname</driver>\n",
	      "        </constraint>\n",
	      "        <!-- ...except to these: -->\n",
	      );

	# Now, for each printer, put in a constraint if this
	# resolution makes sense or not...
	my $stpprn;
	for $stpprn (keys(%mapstp)) {

	    my $resobj = compute_resolutions($stpprn);
	    my $takesit = $resobj->{'takesit'}{$x}{$y};

	    if ($takesit) {
		my $fooprn;
		for $fooprn (@{$mapstp{$stpprn}}) {

#		    print STDERR "Printer $fooprn $stpprn uses ${x}x$y\n";

		    # OK, this choice applies to this enum
		    push (@vals,
			  "        <constraint sense='true'>\n",
			  "          <driver>$drvname</driver>\n",
			  "          <printer>$fooprn</printer><!-- gutenprint name: $stpprn -->\n",
			  "        </constraint>\n");
		}
	    }

	}

	push (@vals,
	      "      </constraints>\n",
	      "    </enum_val>\n");
    }

    return join('',
		"<enum_vals>\n",
		@vals,
		"  </enum_vals>\n");
}

sub build_resolution_cons {
    my ($stpopt) = @_;
    my $drvname = "$drivernameprefix$drivertypesuffix";

    my @PNCONS = ();

    # For each stp printer...
    my $stpname;
    for $stpname (keys(%mapstp)) {

	# Get some resolution info
	my $r = compute_resolutions($stpname);

	# For each possible foo name
	my $fooname;
	for $fooname (@{$mapstp{$stpname}}) {

	    # What's the default value?
	    my $foodefval = $r->{'defval'};

	    push (@PNCONS,
		  "    <constraint sense='true'>\n",
		  "      <driver>$drvname</driver>\n",
		  "      <printer>$fooname</printer><!-- gutenprint name: $stpname -->\n",
		  "      <arg_defval>$foodefval</arg_defval>\n",
		  "    </constraint>\n");
	}
    }

    return join('',
		"<constraints>\n",
		@PNCONS,
		"  </constraints>\n");
}

sub build_printoutmode_ev {
    my ($stpopt) = @_;
    my $drvname = "$drivernameprefix$drivertypesuffix";

    my @vals = ();

    # OK, now for each choice ("Draft", "Normal", ...) ...
    my $choice;
    for $choice (keys %modes) {
	# ... and each possible "<ev_driverval>" for it
	my $ev_driverval;
	for $ev_driverval (keys %{$modes{$choice}}) {
	    #  Put in the basic choice info: ev names, etc
	    my $ev_longname = $printoutmodechoices->{$choice};
	    my $ev_shortname = $choice;

	    my $ev_id =
		get_ev_key($modes{$choice}{$ev_driverval}, $drvname);

	    push (@vals,
		  "    <enum_val id='$ev_id'>\n",
		  "      <ev_longname><en>$ev_longname</en></ev_longname>\n",
		  "      <ev_shortname><en>$ev_shortname</en></ev_shortname>\n",
		  "      <ev_driverval>$ev_driverval</ev_driverval>\n",
		  "      <constraints>\n",
		  "        <!-- Assume the option doesn't apply... -->\n",
		  "        <constraint sense='false'>\n",
		  "          <driver>$drvname</driver>\n",
		  "        </constraint>\n");

	    #   Build constraints for this particular ev_driverval
	    my $stpprn;
	    for $stpprn (keys(%stpdata)) {
		my $fooprn;
		for $fooprn (@{$mapstp{$stpprn}}) {
		    if ($printoutmode->{$stpprn}{$choice} eq
			$ev_driverval) {
			# OK, this choice applies to this printer
			push (@vals,
			      "        <constraint sense='true'>\n",
			      "          <!-- $fooprn == $stpprn -->\n",
			      "          <driver>$drvname</driver>\n",
			      "          <printer>$fooprn</printer>\n",
			      "        </constraint>\n");
		    }
		}
	    }

	    push (@vals,
		  "      </constraints>\n",
		  "    </enum_val>\n");
	}
    }

    return join('',
		"<enum_vals>\n",
		@vals,
		"  </enum_vals>\n");
}

sub build_printoutmode_cons {
    my ($stpopt) = @_;
    my $drvname = "$drivernameprefix$drivertypesuffix";

    my @PNCONS = ();

    # For each stp printer...
    my $stpname;
    for $stpname (keys(%mapstp)) {

	# For each possible foo name
	my $fooname;
	for $fooname (@{$mapstp{$stpname}}) {

	    # What's the default value (always the "Normal" mode)?
	    my $normalmode = $printoutmode->{$stpname}{'Normal'};
	    my $foodefval = get_ev_key($modes{'Normal'}{$normalmode},
				       $drvname);

	    push (@PNCONS,
		  "    <constraint sense='true'>\n",
		  "      <driver>$drvname</driver>\n",
		  "      <printer>$fooname</printer>\n",
		  "      <arg_defval>$foodefval</arg_defval>\n",
		  "    </constraint>\n");
	}
    }

    return join('',
		"<constraints>\n",
		@PNCONS,
		"  </constraints>\n");
}

sub qualityorder {
    # List of suffixes of the Quality choices
    my @suffixes = (
		    # HP
		    "mono",
		    # General
		    "",
		    "dpi",
		    # Epson/Lexmark
		    "mw",
		    "mw2",
		    "sw",
		    "fol",
		    "fol2",
		    "fourp",
		    "uni",
		    "mwuni",
		    "mw2uni",
		    "swuni",
		    "foluni",
		    "fol2uni",
		    "fourpuni",
		    "hq",
		    "hquni",
		    "hq2",
		    "hq2uni",
		    "ov",
		    "oov",
		    # Canon
		    "dmt",
		    "dpi_high",
		    "dpi_high2",
		    "dpi_high3",
		    "dpi_high4",
		    "dpi_high5",
		    "dpi_highmono",
		    "dpi_highmono2",
		    "dpi_highmono3",
		    "dpi_highmono4",
		    "dpi_highmono5",
		    "dpi_mono",
		    "dpi_mono2",
		    "dpi_mono3",
		    "dpi_mono4",
		    "dpi_mono5",
		    "dpi_draft",
		    "dpi_draft2",
		    "dpi_draft3",
		    "dpi_draft4",
		    "dpi_draft5",
                    "dpi_std2",
                    "dpi_std3",
                    "dpi_std4",
                    "dpi_std5",
		    "dpi_draftmono",
		    "dpi_draftmono2",
		    "dpi_draftmono3",
		    "dpi_draftmono4",
		    "dpi_draftmono5",
		    "dpi_photohigh",
		    "dpi_photohigh2",
		    "dpi_photohigh3",
		    "dpi_photohigh4",
		    "dpi_photohigh5",
	            "dpi_tshirt",
		    "dpi_photomed",
		    "dpi_photomed2",
		    "dpi_photo",
		    "dpi_photo2",
		    "dpi_photo3",
		    "dpi_photo4",
		    "dpi_photo5",
		    "dpi_photodraft",
		    "dpi_photodraft2",
		    "dpi_photodraft3",
		    "dpi_photodraft4",
		    "dpi_photodraft5",
		    "dpi_photomonohigh",
		    "dpi_photomonohigh2",
		    "dpi_photomonomed",
		    "dpi_photomono",
		    "dpi_photomono2",
		    "dpi_photomonodraft",
		    "dpi_photomonodraft2",
                    "dpi_ohphigh",
                    "dpi_ohp"
		    );
    my ($a, $b) = @_;
    # Bring the suffixes to lower case
    my $first = lc($a);
    my $second = lc($b);
    # Check whether they are in the @suffixes list
    my $i;
    for ($i = 0; $i <= $#suffixes; $i++) {
	my $firstinlist = ($first eq $suffixes[$i]);
	my $secondinlist = ($second eq $suffixes[$i]);
	if (($firstinlist) && (!$secondinlist)) {return -1};
	if (($secondinlist) && (!$firstinlist)) {return 1};
	if (($firstinlist) && ($secondinlist)) {return 0};
    }

    # Unknown qualities
    die "The quality choice suffixes $a and $b are unknown!\n";
    return 0;

}

sub getprintoutmode {
    my $choicelongnames = {
	'Draft' => 'Draft (Economy)',
	'Draft.Gray' => 'Draft Grayscale (Economy)',
	'Normal' => 'Normal',
	'Normal.Gray' => 'Normal Grayscale',
	'High' => 'High Quality',
	'High.Gray' => 'High Quality Grayscale',
	'VeryHigh' => 'Very High Quality',
	'VeryHigh.Gray' => 'Very High Quality Grayscale',
	'Photo' => 'Photo',
	'Photo.Gray' => 'Photo Grayscale',
    };

    ### BASIC RULES

    # See mode-specific rules below

    # There must be always a "Normal" mode, this will be the default.

    # On black-and-white printers there are no modes with ".Gray"
    # specifier, the standard modes are already grayscale.

    # No "Photo" mode on laser printers.

    # If on a PCL printer "600mono" is the chose quality, it will be
    # replaced by "300dpi" in color mode (This can lead to a mode being
    # removed by the following two rules).

    # If "VeryHigh" has exactly the same settings as "High", "VeryHigh"
    # is left out.

    # If "High" has exactly the same settings as "Normal", "High"
    # is left out.

    # If nothing is found for a certain mode, this mode is left out.

    my $modes = {};
    # Treat all printers
    my $stpprn;
    for $stpprn (keys(%stpdata)) {
	my $modeinfo = {};
	my ($draftminres, $draftbestsymmetry, $draftlowestqualstr) =
	    (99999999, 99999999, "xxx");
	my ($normalminres, $normalbestsymmetry, $normaluni,
	    $normallowestqualstr) =
	    (99999999, 99999999, 0, "xxx");
	my ($highmaxres, $highbestsymmetry, $highbestqualstr) =
	    (0, 99999999, "");
	my ($veryhighmaxres, $veryhighbestsymmetry, $veryhighbestqualstr) =
	    (0, 99999999, "");
	my ($photomaxres, $photobestsymmetry, $photobestqualstr) =
	    (0, 99999999, "");
	# Go through all choices of the "Quality" option and find the
	# best values for the "PrintoutMode" option
	my $quality;
	for $quality (keys(%{$stpdata{$stpprn}{'STP_Resolution'}})) {

	    my ($xres, $yres, $qualstr);
	    if ($quality =~ /^(\d+)x(\d+)(\D.*)$/) {
		$xres = $1;
		$yres = $2;
		$qualstr = $3;
	    } elsif ($quality =~ /^(\d+)(\D.*)$/) {
		$xres = $1;
		$yres = $1;
		$qualstr = $2;
	    } else {
		$xres = $stpdata{$stpprn}{'x_resolution'};
		$yres = $stpdata{$stpprn}{'y_resolution'};
		$qualstr = $quality;
	    }
	    # Resolution in dots per square inch
	    my $respersquareinch = $xres * $yres;
	    # Symmetry: Shows how far from symmetric a resolution is,
	    # the smaller, the more symmetric, symmetric resolutions (as
	    # 300x300 dpi) give zero.
	    my $symmetry = abs(log($yres/$xres));

	    ### Mode: DRAFT

	    # Use always the lowest available resolution/quality,
	    # preferrably symmetric resolutions,

	    # Do not use resolutions with less than 150 dpi in both
	    # demensions.

	    # ImageType Text

	    my $qualitysetting =
		(defined($stpdata{$stpprn}{'STP_Quality'}) ?
		 (defined($stpdata{$stpprn}{'STP_Quality'}{'Draft'}) ?
		  'Draft' :
		  (defined($stpdata{$stpprn}{'STP_Quality'}{'Economy'}) ?
		   'Economy' :
		   (defined($stpdata{$stpprn}{'STP_Quality'}{'FastEconomy'}) ?
		    'FastEconomy' :
		    (defined($stpdata{$stpprn}{'STP_Quality'}{'Standard'}) ?
		     'Standard' : 'ERROR' )))) : '');
	    die ("\n\n'STP_Quality' option without 'Draft', 'Economy', " .
		 "'FastEconomy', and 'Standard' choices for the '$stpprn' " .
		 "device class!\n")
		if $qualitysetting eq 'ERROR';

	    my $imagesetting =
		(defined($stpdata{$stpprn}{'STP_ImageType'}) ?
		 (defined($stpdata{$stpprn}{'STP_ImageType'}{'TextGraphics'}) ?
		  'TextGraphics' :
		  (defined($stpdata{$stpprn}{'STP_ImageType'}{'TextGraphics'}) ?
		   'TextGraphics' :
		   (defined($stpdata{$stpprn}{'STP_ImageType'}{'LineArt'}) ?
		    'LineArt' :
		    (defined($stpdata{$stpprn}{'STP_ImageType'}{'Graphics'}) ?
		     'Graphics' : 'ERROR' )))) : '');
	    die ("\n\n'STP_ImageType' option without 'TextGraphics', " .
		 "'LineArt', and 'Graphics' choices for the '$stpprn' " .
		 "device class!\n")
		if $imagesetting eq 'ERROR';

	    if (($respersquareinch < $draftminres) ||
		(($respersquareinch == $draftminres) &&
		 ($symmetry < $draftbestsymmetry)) ||
		(($respersquareinch == $draftminres) &&
		 ($symmetry == $draftbestsymmetry) &&
		 (qualityorder($qualstr, $draftlowestqualstr) < 0))) {
		unless (($xres < 150) && # Resolution not lower than
			($yres < 150)) {  # 150x150, 360x120 allowed
		    $draftbestsymmetry = $symmetry;
		    $draftminres = $respersquareinch;
		    $draftlowestqualstr = $qualstr;
		    $modeinfo->{'Draft'} = {
			'stpres' => $quality,
			'quality' => $qualitysetting,
			'image' => $imagesetting,
			'xres' => $xres,
			'yres' => $yres
		    }
		}
	    }

	    ### Mode: NORMAL

	    # Lowest resolution which is at least 300x300 dpi (600x600 dpi
	    # for Lexmark Z..), low quality level, unidirectional if
	    # possible,

	    # ImageType TextGraphics

	    $qualitysetting =
		(defined($stpdata{$stpprn}{'STP_Quality'}) ?
		 (defined($stpdata{$stpprn}{'STP_Quality'}{'Standard'}) ?
		  'Standard' :
		  (defined($stpdata{$stpprn}{'STP_Quality'}{'High'}) ?
		   'High' :
		   (defined($stpdata{$stpprn}{'STP_Quality'}{'Draft'}) ?
		    'Draft' : 'ERROR' ))) : '');
	    die ("\n\n'STP_Quality' option without 'Draft', 'Standard', and " .
		 "'High' choices for the '$stpprn' device class!\n")
		if $qualitysetting eq 'ERROR';

	    $imagesetting =
		(defined($stpdata{$stpprn}{'STP_ImageType'}) ?
		 (defined($stpdata{$stpprn}{'STP_ImageType'}{'TextGraphics'}) ?
		  'TextGraphics' :
		  (defined($stpdata{$stpprn}{'STP_ImageType'}{'Graphics'}) ?
		   'Graphics' :
		   (defined($stpdata{$stpprn}{'STP_ImageType'}{'LineArt'}) ?
		    'LineArt' :
		    (defined($stpdata{$stpprn}{'STP_ImageType'}{'Photo'}) ?
		     'Photo' : 'ERROR' )))) : '');
	    die ("\n\n'STP_ImageType' option without 'TextGraphics', 'Graphics', " .
		 "'LineArt', and 'Photo' choices for the '$stpprn' " .
		 "device class!\n")
		if $imagesetting eq 'ERROR';

	    if (($respersquareinch < $normalminres) ||
		(($respersquareinch == $normalminres) &&
		 ($symmetry < $normalbestsymmetry)) ||
		(($respersquareinch == $normalminres) &&
		 ($symmetry == $normalbestsymmetry) &&
		 (($qualstr =~ /uni/) > $normaluni)) ||
		(($respersquareinch == $normalminres) &&
		 ($symmetry == $normalbestsymmetry) &&
		 (($qualstr =~ /uni/) == $normaluni) &&
		 (qualityorder($qualstr, $normallowestqualstr) < 0))) {
		unless ((($xres < 300) ||  # Resolution not lower than
			 ($yres < 300)) || # 300x300, (600x600 on Lexmark)
			(($stpprn =~ /^lexmark\-z/) &&
			 (($xres < 600) ||
			  ($yres < 600)))) {
		    $normalbestsymmetry = $symmetry;
		    $normalminres = $respersquareinch;
		    $normaluni = ($qualstr =~ /uni/);
		    $normallowestqualstr = $qualstr;
		    $modeinfo->{'Normal'} = {
			'stpres' => $quality,
			'quality' => $qualitysetting,
			'image' => $imagesetting,
			'xres' => $xres,
			'yres' => $yres
		    }
		}
	    }

	    ### Mode: HIGH

	    # High: The highest resolution which is not higher than
	    # 720x720 dpi (Lexmark Z..: 1200x1200 dpi),
	    # unidirectional if possible,
	    # not "fol", "fourp", "hq", "hq2"

	    # ImageType TextGraphics

	    $qualitysetting =
		(defined($stpdata{$stpprn}{'STP_Quality'}) ?
		 (defined($stpdata{$stpprn}{'STP_Quality'}{'High'}) ?
		  'High' :
		  (defined($stpdata{$stpprn}{'STP_Quality'}{'Best'}) ?
		   'Best' :
		   (defined($stpdata{$stpprn}{'STP_Quality'}{'Photo'}) ?
		    'Photo' :
		    (defined($stpdata{$stpprn}{'STP_Quality'}{'Standard'}) ?
		     'Standard' : 'ERROR' )))) : '');
	    die ("\n\n'STP_Quality' option without 'High', 'Best', " .
		 "'Photo', and 'Standard' choices for the '$stpprn' " .
		 "device class!\n")
		if $qualitysetting eq 'ERROR';

	    $imagesetting =
		(defined($stpdata{$stpprn}{'STP_ImageType'}) ?
		 (defined($stpdata{$stpprn}{'STP_ImageType'}{'TextGraphics'}) ?
		  'TextGraphics' :
		  (defined($stpdata{$stpprn}{'STP_ImageType'}{'Graphics'}) ?
		   'Graphics' :
		   (defined($stpdata{$stpprn}{'STP_ImageType'}{'Photo'}) ?
		    'Photo' :
		    (defined($stpdata{$stpprn}{'STP_ImageType'}{'LineArt'}) ?
		     'LineArt' : 'ERROR' )))) : '');
	    die ("\n\n'STP_ImageType' option without 'TextGraphics', 'Graphics', " .
		 "'LineArt', and 'Photo' choices for the '$stpprn' " .
		 "device class!\n")
		if $imagesetting eq 'ERROR';

	    if (($respersquareinch > $highmaxres) ||
		(($respersquareinch == $highmaxres) &&
		 ($symmetry < $highbestsymmetry)) ||
		(($respersquareinch == $highmaxres) &&
		 ($symmetry == $highbestsymmetry) &&
		 (qualityorder($qualstr, $highbestqualstr) > 0))) {
		unless ((($stpprn !~ /^lexmark\-z/) &&
			 (($xres > 720) || # Resolution not higher than
			  ($yres > 720))) || # 720x720 for non Lexmark
			($xres > 1200) || # not bigger than 1200x1200
			($yres > 1200) || # in general
			($qualstr =~ /^(hq.*|fo.*)$/)) { # Not "hq", "hq2",
		                                         # "fol", "fourp"
		    $highbestsymmetry = $symmetry;
		    $highmaxres = $respersquareinch;
		    $highbestqualstr = $qualstr;
		    $modeinfo->{'High'} = {
			'stpres' => $quality,
			'quality' => $qualitysetting,
			'image' => $imagesetting,
			'xres' => $xres,
			'yres' => $yres
		    }
		}
	    }

	    ### Mode: VERY HIGH

	    # Use always the highest available resolution/quality,
	    # preferrably symmetric resolutions,

	    # On Epsons: Maximum 1440x720, not "hq2".

	    # ImageType TextGraphics

	    $qualitysetting =
		(defined($stpdata{$stpprn}{'STP_Quality'}) ?
		 (defined($stpdata{$stpprn}{'STP_Quality'}{'Best'}) ?
		  'Best' :
		  (defined($stpdata{$stpprn}{'STP_Quality'}{'High'}) ?
		   'High' :
		   (defined($stpdata{$stpprn}{'STP_Quality'}{'Photo'}) ?
		    'Photo' :
		    (defined($stpdata{$stpprn}{'STP_Quality'}{'Standard'}) ?
		     'Standard' : 'ERROR' )))) : '');
	    die ("\n\n'STP_Quality' option without 'High', 'Best', " .
		 "'Photo', and 'Standard' choices for the '$stpprn' " .
		 "device class!\n")
		if $qualitysetting eq 'ERROR';

	    $imagesetting =
		(defined($stpdata{$stpprn}{'STP_ImageType'}) ?
		 (defined($stpdata{$stpprn}{'STP_ImageType'}{'TextGraphics'}) ?
		  'TextGraphics' :
		  (defined($stpdata{$stpprn}{'STP_ImageType'}{'Graphics'}) ?
		   'Graphics' :
		   (defined($stpdata{$stpprn}{'STP_ImageType'}{'Photo'}) ?
		    'Photo' :
		    (defined($stpdata{$stpprn}{'STP_ImageType'}{'LineArt'}) ?
		     'LineArt' : 'ERROR' )))) : '');
	    die ("\n\n'STP_ImageType' option without 'TextGraphics', 'Graphics', " .
		 "'LineArt', and 'Photo' choices for the '$stpprn' " .
		 "device class!\n")
		if $imagesetting eq 'ERROR';

	    if (($respersquareinch > $veryhighmaxres) ||
		(($respersquareinch == $veryhighmaxres) &&
		 ($symmetry < $veryhighbestsymmetry)) ||
		(($respersquareinch == $veryhighmaxres) &&
		 ($symmetry == $veryhighbestsymmetry) &&
		 (qualityorder($qualstr, $veryhighbestqualstr) > 0))) {
		unless (($xres > 1440) || # Resolution not higher than
			($yres > 720) ||  # 1440x720
			($qualstr eq "hq2")) { # Not "hq2"
		    $veryhighbestsymmetry = $symmetry;
		    $veryhighmaxres = $respersquareinch;
		    $veryhighbestqualstr = $qualstr;
		    $modeinfo->{'VeryHigh'} = {
			'stpres' => $quality,
			'quality' => $qualitysetting,
			'image' => $imagesetting,
			'xres' => $xres,
			'yres' => $yres
		    }
		}
	    }

	    ### Mode: PHOTO

	    # High: The highest resolution which is not higher than
	    # 720x720 dpi (Lexmark Z..: 1200x1200 dpi),
	    # unidirectional if possible,

	    # ImageType Photographs

	    $qualitysetting =
		(defined($stpdata{$stpprn}{'STP_Quality'}) ?
		 (defined($stpdata{$stpprn}{'STP_Quality'}{'HighPhoto'}) ?
		  'HighPhoto' :
		  (defined($stpdata{$stpprn}{'STP_Quality'}{'Photo'}) ?
		   'Photo' :
		   (defined($stpdata{$stpprn}{'STP_Quality'}{'UltraPhoto'}) ?
		    'UltraPhoto' :
		    (defined($stpdata{$stpprn}{'STP_Quality'}{'Best'}) ?
		     'Best' :
		     (defined($stpdata{$stpprn}{'STP_Quality'}{'High'}) ?
		      'High' :
		      (defined($stpdata{$stpprn}{'STP_Quality'}{'Standard'}) ?
		       'Standard' : 'ERROR' )))))) : '');
	    die ("\n\n'STP_Quality' option without 'HighPhoto', 'Photo', " .
		 "'UltraPhoto', 'Best', 'High', and 'Standard' choices " .
		 "for the '$stpprn' device class!\n")
		if $qualitysetting eq 'ERROR';

	    $imagesetting =
		(defined($stpdata{$stpprn}{'STP_ImageType'}) ?
		 (defined($stpdata{$stpprn}{'STP_ImageType'}{'Photo'}) ?
		  'Photo' :
		  (defined($stpdata{$stpprn}{'STP_ImageType'}{'Graphics'}) ?
		   'Graphics' :
		   (defined($stpdata{$stpprn}{'STP_ImageType'}{'TextGraphics'}) ?
		    'TextGraphics' :
		    (defined($stpdata{$stpprn}{'STP_ImageType'}{'LineArt'}) ?
		     'LineArt' : 'ERROR' )))) : '');
	    die ("\n\n'STP_ImageType' option without 'TextGraphics', 'Graphics', " .
		 "'LineArt', and 'Photo' choices for the '$stpprn' " .
		 "device class!\n")
		if $imagesetting eq 'ERROR';

	    if (($respersquareinch > $photomaxres) ||
		(($respersquareinch == $photomaxres) &&
		 ($symmetry < $photobestsymmetry)) ||
		(($respersquareinch == $photomaxres) &&
		 ($symmetry == $photobestsymmetry) &&
		 (qualityorder($qualstr, $photobestqualstr) > 0))) {
		unless ((($stpprn !~ /^lexmark\-z/) &&
			 (($xres > 720) || # Resolution not higher than
			  ($yres > 720))) || # 720x720 for non Lexmark
			($xres > 1200) || # not bigger than 1200x1200
			($yres > 1200) || # in general
			($qualstr =~ /^(hq2)$/)) { # Not "hq2"
		    $photobestsymmetry = $symmetry;
		    $photomaxres = $respersquareinch;
		    $photobestqualstr = $qualstr;
		    $modeinfo->{'Photo'} = {
			'stpres' => $quality,
			'quality' => $qualitysetting,
			'image' => $imagesetting,
			'xres' => $xres,
			'yres' => $yres
		    }
		}
	    }
	}

	# We must have a "Normal" mode for every printer.
	if (!defined($modeinfo->{'Normal'}{'stpres'})) {
	    die "No 'Normal' mode for $stpprn!\n";
	}

	# Build the strings with the settings for the "PrintoutMode"
	# option
	for my $m (keys(%{$modeinfo})) {
	    # If we didn't find anything for a certain mode, skip this
	    # mode
	    next if (!defined($modeinfo->{$m}{'stpres'}));
	    my $modestr =
		(defined($stpdata{$stpprn}{'STP_ImageType'}) ?
		 "STP_ImageType=$modeinfo->{$m}{'image'} " : "") .
		(defined($stpdata{$stpprn}{'STP_Quality'}) ?
		 "STP_Quality=$modeinfo->{$m}{'quality'} " : 
		 "STP_Resolution=$modeinfo->{$m}{'stpres'} ") .
		"Resolution=$modeinfo->{$m}{'xres'}x" .
		"$modeinfo->{$m}{'yres'}dpi";
	    if (defined($stpdata{$stpprn}{'Color'}{'Color'})) {
		# Color printer
		$modes->{$stpprn}{$m} = $modestr . " OutputType=Color";
		if (defined($stpdata{$stpprn}{'Color'}{'Grayscale'})) {
		    $modes->{$stpprn}{"$m.Gray"} =
			$modestr . " OutputType=Grayscale";
		} elsif (defined($stpdata{$stpprn}{'Color'}{'BlackAndWhite'})) {
		    $modes->{$stpprn}{"$m.Gray"} =
			$modestr . " OutputType=BlackAndWhite";
		}
		# Some HP inkjets have a "600mono" quality mode which
		# is only available in Grayscale, replace this mode by
		# "300dpi" in the settings for color printing
		if ($modes->{$stpprn}{$m} =~ /600mono/) {
		    if(!defined($stpdata{$stpprn}{'STP_Resolution'}{'300dpi'})){
			die "No '300dpi' mode for $stpprn!";
		    }
		    $modes->{$stpprn}{$m} =~ s/600x600dpi/300x300dpi/;
		    $modes->{$stpprn}{$m} =~ s/600mono/300dpi/;
		}
	    } else {
		# bw printer
		if ($stpprn =~ /^pcl\-[2-6][vls]?i?$/) { # Laser printer
		    # No 'Photo' mode on laser printers
		    next if ($m eq 'Photo');
		    # Always "VeryFast" dithering on laser printers
		    $modestr =~ s/(Dither=)\S+/$1VeryFast/;
		}
		$modes->{$stpprn}{$m} = $modestr . " OutputType=Grayscale";
	    }
	}
	# Remove 'VeryHigh' and 'High' if they are identical to lower
	# quality modes
	if ($modes->{$stpprn}{'VeryHigh'} eq
	    $modes->{$stpprn}{'High'}) {
	    delete($modes->{$stpprn}{'VeryHigh'});
	}
	if ($modes->{$stpprn}{'High'} eq
	    $modes->{$stpprn}{'Normal'}) {
	    delete($modes->{$stpprn}{'High'});
	}
	if (defined($stpdata{$stpprn}{'Color'}{'Color'})) {
	    # Color printer
	    if ($modes->{$stpprn}{'VeryHigh.Gray'} eq
		$modes->{$stpprn}{'High.Gray'}) {
		delete($modes->{$stpprn}{'VeryHigh.Gray'});
	    }
	    if ($modes->{$stpprn}{'High.Gray'} eq
		$modes->{$stpprn}{'Normal.Gray'}) {
		delete($modes->{$stpprn}{'High.Gray'});
	    }
	}
    }

    return ($modes, $choicelongnames)
}

exit(0);