Debian Codegraph @

contrib/pcl3/src/pclsize.c

raw
1 /******************************************************************************
2 File: $Id: pclsize.c,v 1.10 2001/08/18 17:41:49 Martin Rel $
3 Contents: Maps between PCL Page Size codes und size information
4 Author: Martin Lottermoser, Greifswaldstrasse 28, 38124 Braunschweig,
5 Germany. E-mail: [email protected].
6
7 *******************************************************************************
8 * *
9 * Copyright (C) 1999, 2000 by Martin Lottermoser *
10 * All rights reserved *
11 * *
12 ******************************************************************************/
13
14 /*****************************************************************************/
15
16 #ifndef _XOPEN_SOURCE
17 #define _XOPEN_SOURCE 500
18 #endif
19
20 /* Standard headers */
21 #include <stdlib.h>
22 #include <string.h>
23
24 /* Specific headers */
25 #include "pclsize.h"
26
27 /*****************************************************************************/
28
29 /* Number of elements in an array */
30 #define array_size(a) (sizeof(a)/sizeof(a[0]))
31
32 /*****************************************************************************/
33
34 /* Map from media code to PCL Page Size code
35
36 This list is used to find a PCL Page Size code for a particular media code.
37 It will be sorted by media code before the first use.
38
39 */
40
41 #define me(keyword) {ms_##keyword, pcl_ps_##keyword}
42
43 static CodeEntry code_map[] = {
44 me(Executive),
45 me(Letter),
46 me(Legal),
47 me(Tabloid),
48 me(Statement),
49 me(HPSuperB),
50 me(A6),
51 me(A5),
52 me(A4),
53 me(A3),
54 me(JISB5),
55 me(JISB4),
56 me(Postcard),
57 me(DoublePostcard),
58 {ms_A6 | PCL_CARD_FLAG, pcl_ps_A6Card},
59 me(Index4x6in),
60 me(Index5x8in),
61 me(Index3x5in),
62 me(EnvMonarch),
63 me(Env10),
64 /* I've used 'pcl_ps_Env10' instead of 'pcl_ps_Env10_Negative' because of the
65 DJ500 and DJ500C models which do not accept 'pcl_ps_Env10_Negative'. */
66 me(EnvDL),
67 me(EnvC5),
68 me(EnvC6),
69 me(ISOB5),
70 me(CustomPageSize),
71 me(EnvUS_A2),
72 me(EnvChou3),
73 me(EnvChou4),
74 me(EnvKaku2)
75 };
76
77 #undef me
78
79 /* If the following gives a compile error, then MAX_CODEENTRIES in pclsize.h
80 * must be increased. */
81 typedef struct
82 {
83 char compile_time_assert[array_size(code_map) <= MAX_CODEENTRIES ? 1 : -1];
84 } compile_time_assert_for_codeentry;
85
86 /******************************************************************************
87
88 Function: cmp_by_size
89
90 This function compares two 'CodeEntry' instances by their media codes.
91
92 ******************************************************************************/
93
94 static int cmp_by_size(const void *a, const void *b)
95 {
96 return ((const CodeEntry *)a)->mc - ((const CodeEntry *)b)->mc;
97 }
98
99 /******************************************************************************
100
101 Function: pcl3_page_size
102
103 If it knows a PCL Page Size code for the specified 'code', the function will
104 return it. Otherwise the return value will be 'pcl_ps_default'.
105
106 Flags in 'code' will be ignored except for PCL_CARD_FLAG.
107
108 ******************************************************************************/
109
110 pcl_PageSize pcl3_page_size(pcl3_sizetable *table, ms_MediaCode code)
111 {
112 CodeEntry key;
113 const CodeEntry *result;
114
115 /* Sort the table if necessary */
116 if (!table->inited_code_map) {
117 memcpy(table->code_map, code_map, array_size(code_map) * sizeof(CodeEntry));
118 qsort(table->code_map, array_size(code_map), sizeof(CodeEntry), cmp_by_size);
119 table->inited_code_map = TRUE;
120 }
121
122 key.mc = ms_without_flags(code) |( code & PCL_CARD_FLAG);
123 result = (const CodeEntry *)bsearch(&key, table->code_map, array_size(code_map),
124 sizeof(CodeEntry), cmp_by_size);
125
126 return result == NULL? pcl_ps_default: result->ps;
127 }
128
129 /******************************************************************************
130
131 Function: cmp_by_code
132
133 This function compares two 'CodeEntry' instances by their PCL Page Size codes.
134
135 ******************************************************************************/
136
137 static int cmp_by_code(const void *a, const void *b)
138 {
139 return ((const CodeEntry *)a)->ps - ((const CodeEntry *)b)->ps;
140 }
141
142 /******************************************************************************
143
144 Function: pcl3_media_code
145
146 This function will return a media code for the given PCL page size code.
147 If the code is unknown, 'ms_none' will be returned.
148
149 ******************************************************************************/
150
151 ms_MediaCode pcl3_media_code(pcl3_sizetable *table, pcl_PageSize code)
152 {
153 CodeEntry key;
154 const CodeEntry *result;
155
156 /* Construct the table if necessary */
157 if (!table->inited_inverse_map) {
158 memcpy(&table->inverse_map, &code_map, sizeof(code_map));
159 qsort(table->inverse_map, array_size(code_map), sizeof(CodeEntry), cmp_by_code);
160 table->inited_inverse_map = TRUE;
161 }
162
163 key.ps = code;
164 result = (const CodeEntry *)bsearch(&key, table->inverse_map,
165 array_size(code_map), sizeof(CodeEntry), cmp_by_code);
166 if (result == NULL) {
167 key.ps = -code;
168 /* Actually, this is a generalization on my part: I am assuming that any
169 two valid PCL Page Size codes with the same absolute value refer to the
170 same media extension irrespective of sheet orientation in raster space.
171 I have found negative Page Size codes in HP documentation only for
172 Env10 and EnvDL. */
173 result = (const CodeEntry *)bsearch(&key, table->inverse_map,
174 array_size(code_map), sizeof(CodeEntry), cmp_by_code);
175 }
176
177 return result == NULL? ms_none: result->mc;
178 }
179
180 /******************************************************************************
181
182 Function: pcl3_size_description
183
184 This function will try to find a media size description for the specified
185 'code'. If the code is unknown, the function will return a NULL pointer.
186
187 ******************************************************************************/
188
189 const ms_SizeDescription *pcl3_size_description(pcl3_sizetable *table, pcl_PageSize code)
190 {
191 return ms_find_size_from_code(pcl3_media_code(table, code));
192 }