[go: up one dir, main page]

File: lprint-testpage.c

package info (click to toggle)
lprint 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,612 kB
  • sloc: ansic: 7,987; sh: 3,267; makefile: 180
file content (322 lines) | stat: -rw-r--r-- 9,174 bytes parent folder | download
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
//
// Test page generator for LPrint, a Label Printer Application
//
// Copyright © 2021-2023 by Michael R Sweet.
//
// Licensed under Apache License v2.0.  See the file "LICENSE" for more
// information.
//

#include "lprint.h"


//
// 'lprintTestFilterCB()' - Print a test page.
//

bool					// O - `true` on success, `false` on failure
lprintTestFilterCB(
    pappl_job_t    *job,		// I - Job
    pappl_device_t *device,		// I - Output device
    void           *cbdata)		// I - Callback data (not used)
{
  pappl_pr_driver_data_t data;		// Printer driver data
  pappl_pr_options_t	*options = papplJobCreatePrintOptions(job, 1, false);
					// Print options
  unsigned char	*line = NULL,		// Output line
		*lineptr;		// Pointer into line
  unsigned	width,			// Width accounting for margins
		height,			// Height accounting for margins
		y,			// Current position in page
		ytop, yend, ybottom,	// Top/end-of-image/bottom lines in page
		xc,			// X count
		x1mm, y1mm,		// Number of columns/rows per pixel (nominally 1mm)
		xleft,			// Start for line
		pw, ph;			// Image width and height
  const char	**pixels,		// Output image pixels
		*pixel;			// Current output pixel
  bool		ret = false;		// Return value
  static const char *portrait[] =	// Portrait label image
  {
    "....            ",
    ".TTT.           ",
    ".TT.            ",
    ".TT.          ..",
    ".TT...........T.",
    ".TTTTTTTTTTTTTT.",
    ".TTTTTTTTTTTTTT.",
    ".TT...........T.",
    ".TT.          ..",
    ".TT.            ",
    ".TTT.           ",
    "....            ",
    "   ..     ...   ",
    "  .SS.   .SSS.  ",
    " .SS.   .SSSSS. ",
    " .SS.  .SS..SS. ",
    ".SS.   .SS. .SS.",
    ".SS.  .SS.  .SS.",
    ".SS.  .SS.  .SS.",
    ".SS. .SS.   .SS.",
    " .SS..SS.  .SS. ",
    " .SSSSS.   .SS. ",
    "  .SSS.   .SS.  ",
    "   ...     ..   ",
    "....        ....",
    ".EEE.      .EEE.",
    ".EE.  ....  .EE.",
    ".EE. .EEEE. .EE.",
    ".EE.  .EE.  .EE.",
    ".EE.  .EE.  .EE.",
    ".EE.  .EE.  .EE.",
    ".EE.  .EE.  .EE.",
    ".EE....EE....EE.",
    ".EEEEEEEEEEEEEE.",
    ".EEEEEEEEEEEEEE.",
    "................",
    "....            ",
    ".TTT.           ",
    ".TT.            ",
    ".TT.          ..",
    ".TT...........T.",
    ".TTTTTTTTTTTTTT.",
    ".TTTTTTTTTTTTTT.",
    ".TT...........T.",
    ".TT.          ..",
    ".TT.            ",
    ".TTT.           ",
    "....            "
  };
  static const char *landscape[] =	// Landscape label image
  {
    "........................    ....    ............",
    ".TTTTTTTTTT..EEEEEEEEEE.  ..SSSS..  .TTTTTTTTTT.",
    ".TTTTTTTTTT..EEEEEEEEEE. .SSSSSSSS. .TTTTTTTTTT.",
    ".T...TT...T..EE.......E..SSS....SSS..T...TT...T.",
    " .  .TT.  . .EE.      . .SS.    ..S. .  .TT.  . ",
    "    .TT.    .EE.    .   .SS..     .     .TT.    ",
    "    .TT.    .EE.....E.   .SSS..         .TT.    ",
    "    .TT.    .EEEEEEEE.    .SSSS..       .TT.    ",
    "    .TT.    .EEEEEEEE.     ..SSSS.      .TT.    ",
    "    .TT.    .EE.....E.       ..SSS.     .TT.    ",
    "    .TT.    .EE.    .    .     ..SS.    .TT.    ",
    "    .TT.    .EE.      . .S.     .SS.    .TT.    ",
    "    .TT.    .EE.......E..SSS....SSS.    .TT.    ",
    "    .TT.    .EEEEEEEEEE. .SSSSSSSS.     .TT.    ",
    "   .TTTT.   .EEEEEEEEEE.  ..SSSS..     .TTTT.   ",
    "   ......   ............    ....       ......   "
  };


  (void)cbdata;

  // Validate options and allocate a line buffer...
  if (!options)
    goto done;

  if ((line = malloc(options->header.cupsBytesPerLine)) == NULL)
  {
    papplLogJob(job, PAPPL_LOGLEVEL_ERROR, "Unable to allocate %u bytes for test page: %s", options->header.cupsBytesPerLine, strerror(errno));
    goto done;
  }

  width  = options->header.HWResolution[0] * (options->media.size_width - options->media.left_margin - options->media.right_margin) / 2540;
  height = options->header.HWResolution[1] * (options->media.size_length - options->media.bottom_margin - options->media.top_margin) / 2540;

  papplLogJob(job, PAPPL_LOGLEVEL_DEBUG, "Printable area for test page is %ux%u pixels.", width, height);

  if (width > height)
  {
    // Send landscape raster...
    pixels = landscape;
    pw     = 48;
    ph     = 16;
  }
  else
  {
    // Send portrait raster...
    pixels = portrait;
    pw     = 16;
    ph     = 48;
  }

  y1mm = 10 * options->header.HWResolution[1] / 254;
  if ((y1mm * ph) > height)
  {
    if ((y1mm = height / ph) == 0)
      y1mm = 1;
  }

  if ((x1mm = y1mm * options->header.HWResolution[0] / options->header.HWResolution[1]) == 0)
  {
    x1mm = 1;
    y1mm = x1mm * options->header.HWResolution[1] / options->header.HWResolution[0];
  }

  if ((pw * x1mm) > width)
  {
    // Too wide, try scaling to fit the width...
    x1mm = 10 * options->header.HWResolution[0] / 254;
    if ((x1mm * pw) > width)
      x1mm = width / pw;

    if ((y1mm = x1mm * options->header.HWResolution[1] / options->header.HWResolution[0]) == 0)
    {
      y1mm = 1;
      x1mm = y1mm * options->header.HWResolution[0] / options->header.HWResolution[1];
    }
  }

  papplLogJob(job, PAPPL_LOGLEVEL_DEBUG, "pw=%u, ph=%u, x1mm=%u, y1mm=%u", pw, ph, x1mm, y1mm);

  if (((pw + 2) * x1mm) > options->header.cupsWidth || ((ph + 2) * y1mm) > options->header.cupsHeight)
  {
    papplLogJob(job, PAPPL_LOGLEVEL_ERROR, "Label too small to print test page.");
    goto done;
  }

  pw *= x1mm;
  ph *= y1mm;

  xleft   = (width - pw) / 2 - x1mm + options->header.HWResolution[0] * options->media.left_margin / 2540;
  ytop    = (height - ph) / 2 + options->header.HWResolution[1] * options->media.top_margin / 2540;
  yend    = ytop + ph;
  ybottom = options->header.cupsHeight - y1mm;

  papplLogJob(job, PAPPL_LOGLEVEL_DEBUG, "pw=%u, ph=%u, ytop=%u, yend=%u, ybottom=%u", pw, ph, ytop, yend, ybottom);

  // Start the page...
  papplPrinterGetDriverData(papplJobGetPrinter(job), &data);

  papplJobSetImpressions(job, 1);

  if (!(data.rstartjob_cb)(job, options, device))
    goto done;

  if (!(data.rstartpage_cb)(job, options, device, 1))
    goto done;

  // Send lines for the test label:
  //
  // +---------------+
  // |...............|
  // |...         ...|
  // |... T E S T ...|
  // |...         ...|
  // |...............|
  // +---------------+

  // Top border
  memset(line, 0, options->header.cupsBytesPerLine);
  for (y = 0; y < y1mm; y ++)
  {
    if (!(data.rwriteline_cb)(job, options, device, y, line))
      goto done;
  }

  // Side borders and gray shading on upper half
  memset(line, 0, x1mm);
  memset(line + x1mm, 128, options->header.cupsBytesPerLine - 2 * x1mm);
  memset(line + options->header.cupsBytesPerLine - x1mm, 0, x1mm);

  for (; y < ytop; y ++)
  {
    if (!(data.rwriteline_cb)(job, options, device, y, line))
      goto done;
  }

  // Side borders, gray shading, and image pixels in middle
  for (; y < yend; y ++)
  {
    // Draw a 50% gray pattern with black borders...
    memset(line, 0, x1mm);
    memset(line + x1mm, 128, options->header.cupsBytesPerLine - 2 * x1mm);
    memset(line + options->header.cupsBytesPerLine - x1mm, 0, x1mm);

    // Render the interior text...
    for (pixel = pixels[(y - ytop) / y1mm], lineptr = line + xleft; *pixel; pixel ++)
    {
      for (xc = x1mm; xc > 0; xc --, lineptr ++)
      {
        if (*pixel == '.')
	  *lineptr = 255;
        else if (!isspace(*pixel))
	  *lineptr = 0;
      }
    }

    if (!(data.rwriteline_cb)(job, options, device, y, line))
      goto done;
  }

  // Side borders and gray shading on lower half
  memset(line, 0, x1mm);
  memset(line + x1mm, 128, options->header.cupsBytesPerLine - 2 * x1mm);
  memset(line + options->header.cupsBytesPerLine - x1mm, 0, x1mm);

  for (; y < ybottom; y ++)
  {
    if (!(data.rwriteline_cb)(job, options, device, y, line))
      goto done;
  }

  // Bottom border
  memset(line, 0, options->header.cupsBytesPerLine);
  for (; y < options->header.cupsHeight; y ++)
  {
    if (!(data.rwriteline_cb)(job, options, device, y, line))
      goto done;
  }

  // Finish up...
  if (!(data.rendpage_cb)(job, options, device, 1))
    goto done;

  if (!(data.rendjob_cb)(job, options, device))
    goto done;

  ret = true;

  papplJobSetImpressionsCompleted(job, 1);

  done:

  free(line);
  papplJobDeletePrintOptions(options);

  return (ret);
}


//
// 'lprintTestPageCB()' - Print a test page.
//

const char *				// O - Test page file
lprintTestPageCB(
    pappl_printer_t *printer,		// I - Printer
    char            *buffer,		// I - Filename buffer
    size_t          bufsize)		// I - Size of filename buffer
{
  int	fd;				// File descriptor
  char	testpage[] = LPRINT_TESTPAGE_HEADER;
					// Test page file header


  if ((fd = papplCreateTempFile(buffer, bufsize, "testpage", "tst")) < 0)
  {
    papplLogPrinter(printer, PAPPL_LOGLEVEL_ERROR, "Unable to create temporary file for test page: %s", strerror(errno));
    return (NULL);
  }

  if (write(fd, testpage, sizeof(testpage)) < 0)
  {
    papplLogPrinter(printer, PAPPL_LOGLEVEL_ERROR, "Unable to write temporary file for test page: %s", strerror(errno));
    close(fd);
    return (NULL);
  }

  close(fd);

  return (buffer);
}