ps.c 25.9 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1
/*
Alexandre Julliard's avatar
Alexandre Julliard committed
2
 *	PostScript output functions
Alexandre Julliard's avatar
Alexandre Julliard committed
3 4 5
 *
 *	Copyright 1998  Huw D M Davies
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

Alexandre Julliard's avatar
Alexandre Julliard committed
21
#include <ctype.h>
22
#include <stdio.h>
23
#include <stdlib.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <string.h>
25
#include <stdarg.h>
26
#include <locale.h>
27 28 29

#define NONAMELESSUNION
#define NONAMELESSSTRUCT
30 31 32
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
33
#include "psdrv.h"
34
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
35

36
WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
37

38 39 40 41 42 43 44 45 46 47 48 49 50
static const char psadobe[] =
"%!PS-Adobe-3.0\n";

static const char media[] = "%cupsJobTicket: media=";
static const char cups_one_sided[] = "%cupsJobTicket: sides=one-sided\n";
static const char cups_two_sided_long[] = "%cupsJobTicket: sides=two-sided-long-edge\n";
static const char cups_two_sided_short[] = "%cupsJobTicket: sides=two-sided-short-edge\n";
static const char *cups_duplexes[3] =
{
    cups_one_sided,         /* DMDUP_SIMPLEX */
    cups_two_sided_long,    /* DMDUP_VERTICAL */
    cups_two_sided_short    /* DMDUP_HORIZONTAL */
};
51 52
static const char cups_collate_false[] = "%cupsJobTicket: collate=false\n";
static const char cups_collate_true[] = "%cupsJobTicket: collate=true\n";
53
static const char cups_ap_d_inputslot[] = "%cupsJobTicket: AP_D_InputSlot=\n"; /* intentionally empty value */
54

55
static const char psheader[] = /* title llx lly urx ury orientation */
Alexandre Julliard's avatar
Alexandre Julliard committed
56
"%%%%Creator: Wine PostScript Driver\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
57
"%%%%Title: %s\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
58
"%%%%BoundingBox: %d %d %d %d\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
59
"%%%%Pages: (atend)\n"
60
"%%%%Orientation: %s\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
61 62
"%%%%EndComments\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
63
static const char psbeginprolog[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
64 65
"%%BeginProlog\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
66
static const char psendprolog[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
67 68
"%%EndProlog\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
69
static const char psprolog[] =
70 71 72 73 74 75 76 77 78
"/tmpmtrx matrix def\n"
"/hatch {\n"
"  pathbbox\n"
"  /b exch def /r exch def /t exch def /l exch def /gap 32 def\n"
"  l cvi gap idiv gap mul\n"
"  gap\n"
"  r cvi gap idiv gap mul\n"
"  {t moveto 0 b t sub rlineto}\n"
"  for\n"
79
"} bind def\n"
80
"/B {pop pop pop pop} def\n"
81
"/N {newpath} def\n"
82
"/havetype42gdir {version cvi 2015 ge} bind def\n";
Alexandre Julliard's avatar
Alexandre Julliard committed
83

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
84
static const char psbeginsetup[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
85 86
"%%BeginSetup\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
87
static const char psendsetup[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
88 89
"%%EndSetup\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
90
static const char psbeginfeature[] = /* feature, value */
Alexandre Julliard's avatar
Alexandre Julliard committed
91 92 93
"mark {\n"
"%%%%BeginFeature: %s %s\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
94
static const char psendfeature[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
95 96
"\n%%EndFeature\n"
"} stopped cleartomark\n";
Alexandre Julliard's avatar
Alexandre Julliard committed
97

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
98
static const char psnewpage[] = /* name, number, xres, yres, xtrans, ytrans, rot */
Alexandre Julliard's avatar
Alexandre Julliard committed
99 100 101
"%%%%Page: %s %d\n"
"%%%%BeginPageSetup\n"
"/pgsave save def\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
102 103
"72 %d div 72 %d div scale\n"
"%d %d translate\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
104
"1 -1 scale\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
105
"%d rotate\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
106 107
"%%%%EndPageSetup\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
108
static const char psendpage[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
109 110 111
"pgsave restore\n"
"showpage\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
112
static const char psfooter[] = /* pages */
Alexandre Julliard's avatar
Alexandre Julliard committed
113 114 115 116
"%%%%Trailer\n"
"%%%%Pages: %d\n"
"%%%%EOF\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
117
static const char psmoveto[] = /* x, y */
Alexandre Julliard's avatar
Alexandre Julliard committed
118 119
"%d %d moveto\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
120
static const char pslineto[] = /* x, y */
Alexandre Julliard's avatar
Alexandre Julliard committed
121 122
"%d %d lineto\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
123
static const char psstroke[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
124 125
"stroke\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
126
static const char psrectangle[] = /* x, y, width, height, -width */
Alexandre Julliard's avatar
Alexandre Julliard committed
127 128 129 130 131 132
"%d %d moveto\n"
"%d 0 rlineto\n"
"0 %d rlineto\n"
"%d 0 rlineto\n"
"closepath\n";

133 134 135
static const char psglyphshow[] = /* glyph name */
"/%s glyphshow\n";

136 137 138 139 140 141 142 143 144 145 146 147 148
static const char psfindfont[] = /* fontname */
"/%s findfont\n";

static const char psfakeitalic[] =
"[1 0 0.25 1 0 0]\n";

static const char pssizematrix[] =
"[%d %d %d %d 0 0]\n";

static const char psconcat[] =
"matrix concatmatrix\n";

static const char psrotatefont[] = /* escapement */
Alexandre Julliard's avatar
Alexandre Julliard committed
149
"%d 10 div matrix rotate\n"
150 151 152
"matrix concatmatrix\n";

static const char pssetfont[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
153 154
"makefont setfont\n";

155 156
static const char pssetline[] = /* width, join, endcap */
"%d setlinewidth %u setlinejoin %u setlinecap\n";
Alexandre Julliard's avatar
Alexandre Julliard committed
157

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
158
static const char pssetgray[] = /* gray */
Alexandre Julliard's avatar
Alexandre Julliard committed
159 160
"%.2f setgray\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
161
static const char pssetrgbcolor[] = /* r, g, b */
Alexandre Julliard's avatar
Alexandre Julliard committed
162 163
"%.2f %.2f %.2f setrgbcolor\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
164
static const char psarc[] = /* x, y, w, h, ang1, ang2 */
Alexandre Julliard's avatar
Alexandre Julliard committed
165 166 167
"tmpmtrx currentmatrix pop\n"
"%d %d translate\n"
"%d %d scale\n"
168
"0 0 0.5 %.1f %.1f arc\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
169 170
"tmpmtrx setmatrix\n";

171 172 173
static const char pscurveto[] = /* x1, y1, x2, y2, x3, y3 */
"%d %d %d %d %d %d curveto\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
174
static const char psgsave[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
175 176
"gsave\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
177
static const char psgrestore[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
178 179
"grestore\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
180
static const char psfill[] =
Alexandre Julliard's avatar
Alexandre Julliard committed
181 182
"fill\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
183
static const char pseofill[] =
184 185
"eofill\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
186
static const char psnewpath[] =
187 188
"newpath\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
189
static const char psclosepath[] =
190 191
"closepath\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
192
static const char psclip[] =
193 194
"clip\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
195
static const char pseoclip[] =
196 197
"eoclip\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
198
static const char psrectclip[] =
199
"%d %d %d %d rectclip\n";
200

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
201
static const char psrectclip2[] =
202
"%s rectclip\n";
203

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
204
static const char pshatch[] =
205 206
"hatch\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
207
static const char psrotate[] = /* ang */
208 209
"%.1f rotate\n";

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
210
static const char psarrayput[] =
211
"%s %d %d put\n";
212

Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
213
static const char psarraydef[] =
214 215
"/%s %d array def\n";

216 217
static const char psenddocument[] =
"\n%%EndDocument\n";
Alexandre Julliard's avatar
Alexandre Julliard committed
218

219
DWORD PSDRV_WriteSpool(PHYSDEV dev, LPCSTR lpData, DWORD cch)
Alexandre Julliard's avatar
Alexandre Julliard committed
220
{
221
    PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
222 223
    int num, num_left = cch;

224 225 226 227 228
    if(physDev->job.quiet) {
        TRACE("ignoring output\n");
	return 0;
    }

229
    if(physDev->job.in_passthrough) { /* Was in PASSTHROUGH mode */
230
        write_spool( dev, psenddocument, sizeof(psenddocument)-1 );
231 232 233
        physDev->job.in_passthrough = physDev->job.had_passthrough_rect = FALSE;
    }

234
    if(physDev->job.OutOfPage) { /* Will get here after NEWFRAME Escape */
235
        if( !PSDRV_StartPage(dev) )
236
	    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
237
    }
238 239 240

    do {
        num = min(num_left, 0x8000);
241
        if(write_spool( dev, lpData, num ) != num)
242 243 244 245 246 247
            return 0;
        lpData += num;
        num_left -= num;
    } while(num_left);

    return cch;
Alexandre Julliard's avatar
Alexandre Julliard committed
248 249 250
}


251
static INT PSDRV_WriteFeature(PHYSDEV dev, LPCSTR feature, LPCSTR value, LPCSTR invocation)
Alexandre Julliard's avatar
Alexandre Julliard committed
252 253
{

254
    char *buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psbeginfeature) +
255
                           strlen(feature) + strlen(value));
Alexandre Julliard's avatar
Alexandre Julliard committed
256

Alexandre Julliard's avatar
Alexandre Julliard committed
257
    sprintf(buf, psbeginfeature, feature, value);
258 259 260
    write_spool( dev, buf, strlen(buf) );
    write_spool( dev, invocation, strlen(invocation) );
    write_spool( dev, psendfeature, strlen(psendfeature) );
261

262
    HeapFree( GetProcessHeap(), 0, buf );
Alexandre Julliard's avatar
Alexandre Julliard committed
263 264 265
    return 1;
}

266 267 268 269 270 271 272 273
/********************************************************
 *         escape_title
 *
 * Helper for PSDRV_WriteHeader.  Escape any non-printable characters
 * as octal.  If we've had to use an escape then surround the entire string
 * in brackets.  Truncate string to represent at most 0x80 characters.
 *
 */
274
static char *escape_title(LPCWSTR wstr)
275
{
276
    char *ret, *cp, *str;
277 278
    int i, extra = 0;

279
    if(!wstr)
280 281 282 283 284
    {
        ret = HeapAlloc(GetProcessHeap(), 0, 1);
        *ret = '\0';
        return ret;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
285

286 287 288 289 290
    i = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
    str = HeapAlloc( GetProcessHeap(), 0, i );
    if (!str) return NULL;
    WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, i, NULL, NULL );

291 292 293 294 295 296 297 298 299 300 301
    for(i = 0; i < 0x80 && str[i]; i++)
    {
        if(!isprint(str[i]))
           extra += 3;
    }

    if(!extra)
    {
        ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
        memcpy(ret, str, i);
        ret[i] = '\0';
302
        goto done;
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    }

    extra += 2; /* two for the brackets */
    cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
    *cp++ = '(';
    for(i = 0; i < 0x80 && str[i]; i++)
    {
        if(!isprint(str[i]))
        {
            BYTE b = (BYTE)str[i];
            *cp++ = '\\';
            *cp++ = ((b >> 6) & 0x7) + '0';
            *cp++ = ((b >> 3) & 0x7) + '0';
            *cp++ = ((b)      & 0x7) + '0';
        }
        else
            *cp++ = str[i];
    }
    *cp++ = ')';
    *cp = '\0';
323 324 325

done:
    HeapFree( GetProcessHeap(), 0, str );
326 327
    return ret;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
328

329 330 331 332 333 334 335 336
struct ticket_info
{
    PAGESIZE *page;
    DUPLEX *duplex;
};

static void write_cups_job_ticket( PHYSDEV dev, const struct ticket_info *info )
{
337
    PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
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
    char buf[256];
    int len;

    if (info->page && info->page->InvocationString)
    {
        len = sizeof(media) + strlen( info->page->Name ) + 1;
        if (len <= sizeof(buf))
        {
            memcpy( buf, media, sizeof(media) );
            strcat( buf, info->page->Name );
            strcat( buf, "\n");
            write_spool( dev, buf, len - 1 );
        }
        else
            WARN( "paper name %s will be too long for DSC\n", info->page->Name );
    }

    if (info->duplex && info->duplex->InvocationString)
    {
        if (info->duplex->WinDuplex >= 1 && info->duplex->WinDuplex <= 3)
        {
            const char *str = cups_duplexes[ info->duplex->WinDuplex - 1 ];
            write_spool( dev, str, strlen( str ) );
        }
    }
363 364 365 366 367 368 369

    if (physDev->Devmode->dmPublic.u1.s1.dmCopies > 1)
    {
        len = snprintf( buf, sizeof(buf), "%%cupsJobTicket: copies=%d\n",
                        physDev->Devmode->dmPublic.u1.s1.dmCopies );
        if (len > 0 && len < sizeof(buf))
            write_spool( dev, buf, len );
370 371 372 373 374 375 376 377

        if (physDev->Devmode->dmPublic.dmFields & DM_COLLATE)
        {
            if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_FALSE)
                write_spool( dev, cups_collate_false, sizeof(cups_collate_false) - 1 );
            else if (physDev->Devmode->dmPublic.dmCollate == DMCOLLATE_TRUE)
                write_spool( dev, cups_collate_true, sizeof(cups_collate_true) - 1 );
        }
378
    }
379 380 381 382

    if (!(physDev->Devmode->dmPublic.dmFields & DM_DEFAULTSOURCE) ||
        physDev->Devmode->dmPublic.u1.s1.dmDefaultSource == DMBIN_AUTO)
        write_spool( dev, cups_ap_d_inputslot, sizeof(cups_ap_d_inputslot) - 1 );
383
}
384

385
INT PSDRV_WriteHeader( PHYSDEV dev, LPCWSTR title )
Alexandre Julliard's avatar
Alexandre Julliard committed
386
{
387
    PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
388
    char *buf, *escaped_title;
389
    INPUTSLOT *slot = find_slot( physDev->pi->ppd, physDev->Devmode );
390
    PAGESIZE *page = find_pagesize( physDev->pi->ppd, physDev->Devmode );
391
    DUPLEX *duplex = find_duplex( physDev->pi->ppd, physDev->Devmode );
392
    int llx, lly, urx, ury;
393
    int ret, len;
394 395
    const char * dmOrientation;

396
    struct ticket_info ticket_info = { page, duplex };
Alexandre Julliard's avatar
Alexandre Julliard committed
397

398
    TRACE("%s\n", debugstr_w(title));
Alexandre Julliard's avatar
Alexandre Julliard committed
399

400 401 402 403 404 405 406 407 408 409
    len = strlen( psadobe );
    ret = write_spool( dev, psadobe, len );
    if (ret != len)
    {
        WARN("WriteSpool error\n");
        return 0;
    }

    write_cups_job_ticket( dev, &ticket_info );

410
    escaped_title = escape_title(title);
411
    buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psheader) +
412
                     strlen(escaped_title) + 30 );
Alexandre Julliard's avatar
Alexandre Julliard committed
413
    if(!buf) {
414
        WARN("HeapAlloc failed\n");
415
        HeapFree(GetProcessHeap(), 0, escaped_title);
Alexandre Julliard's avatar
Alexandre Julliard committed
416 417
        return 0;
    }
418

419 420
    /* BBox co-ords are in default user co-ord system so urx < ury even in
       landscape mode */
421 422 423 424
    llx = physDev->ImageableArea.left * 72.0 / physDev->logPixelsX;
    lly = physDev->ImageableArea.bottom * 72.0 / physDev->logPixelsY;
    urx = physDev->ImageableArea.right * 72.0 / physDev->logPixelsX;
    ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
Alexandre Julliard's avatar
Alexandre Julliard committed
425 426
    /* FIXME should do something better with BBox */

427 428
    dmOrientation = (physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE ? "Landscape" : "Portrait");
    sprintf(buf, psheader, escaped_title, llx, lly, urx, ury, dmOrientation);
Alexandre Julliard's avatar
Alexandre Julliard committed
429

430
    HeapFree(GetProcessHeap(), 0, escaped_title);
431 432

    len = strlen( buf );
433
    write_spool( dev, buf, len );
434
    HeapFree( GetProcessHeap(), 0, buf );
Alexandre Julliard's avatar
Alexandre Julliard committed
435

436 437 438 439
    write_spool( dev, psbeginprolog, strlen(psbeginprolog) );
    write_spool( dev, psprolog, strlen(psprolog) );
    write_spool( dev, psendprolog, strlen(psendprolog) );
    write_spool( dev, psbeginsetup, strlen(psbeginsetup) );
Alexandre Julliard's avatar
Alexandre Julliard committed
440

441 442
    if (slot && slot->InvocationString)
        PSDRV_WriteFeature( dev, "*InputSlot", slot->Name, slot->InvocationString );
Alexandre Julliard's avatar
Alexandre Julliard committed
443

444 445
    if (page && page->InvocationString)
        PSDRV_WriteFeature( dev, "*PageSize", page->Name, page->InvocationString );
Alexandre Julliard's avatar
Alexandre Julliard committed
446

447 448
    if (duplex && duplex->InvocationString)
        PSDRV_WriteFeature( dev, "*Duplex", duplex->Name, duplex->InvocationString );
449

450
    write_spool( dev, psendsetup, strlen(psendsetup) );
Alexandre Julliard's avatar
Alexandre Julliard committed
451 452


Alexandre Julliard's avatar
Alexandre Julliard committed
453 454 455 456
    return 1;
}


457
INT PSDRV_WriteFooter( PHYSDEV dev )
Alexandre Julliard's avatar
Alexandre Julliard committed
458
{
459
    PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
Alexandre Julliard's avatar
Alexandre Julliard committed
460
    char *buf;
461
    int ret = 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
462

463
    buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psfooter) + 100 );
Alexandre Julliard's avatar
Alexandre Julliard committed
464
    if(!buf) {
465
        WARN("HeapAlloc failed\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
466 467 468
        return 0;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
469
    sprintf(buf, psfooter, physDev->job.PageNo);
Alexandre Julliard's avatar
Alexandre Julliard committed
470

471
    if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
472
        WARN("WriteSpool error\n");
473
        ret = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
474
    }
475 476
    HeapFree( GetProcessHeap(), 0, buf );
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
477 478 479 480
}



481
INT PSDRV_WriteEndPage( PHYSDEV dev )
Alexandre Julliard's avatar
Alexandre Julliard committed
482
{
483
    if( write_spool( dev, psendpage, sizeof(psendpage)-1 ) != sizeof(psendpage)-1 ) {
484
        WARN("WriteSpool error\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
485 486 487 488 489 490 491 492
	return 0;
    }
    return 1;
}




493
INT PSDRV_WriteNewPage( PHYSDEV dev )
Alexandre Julliard's avatar
Alexandre Julliard committed
494
{
495
    PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
Alexandre Julliard's avatar
Alexandre Julliard committed
496 497
    char *buf;
    char name[100];
Alexandre Julliard's avatar
Alexandre Julliard committed
498
    signed int xtrans, ytrans, rotation;
499
    int ret = 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
500 501

    sprintf(name, "%d", physDev->job.PageNo);
Alexandre Julliard's avatar
Alexandre Julliard committed
502

503
    buf = HeapAlloc( GetProcessHeap(), 0, sizeof(psnewpage) + 200 );
Alexandre Julliard's avatar
Alexandre Julliard committed
504
    if(!buf) {
505
        WARN("HeapAlloc failed\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
506 507 508
        return 0;
    }

509
    if(physDev->Devmode->dmPublic.u1.s1.dmOrientation == DMORIENT_LANDSCAPE) {
Alexandre Julliard's avatar
Alexandre Julliard committed
510
        if(physDev->pi->ppd->LandscapeOrientation == -90) {
511 512
	    xtrans = physDev->ImageableArea.right;
	    ytrans = physDev->ImageableArea.top;
Alexandre Julliard's avatar
Alexandre Julliard committed
513 514
	    rotation = 90;
	} else {
515 516
	    xtrans = physDev->ImageableArea.left;
	    ytrans = physDev->ImageableArea.bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
517 518 519
	    rotation = -90;
	}
    } else {
520 521
        xtrans = physDev->ImageableArea.left;
	ytrans = physDev->ImageableArea.top;
Alexandre Julliard's avatar
Alexandre Julliard committed
522 523 524 525
	rotation = 0;
    }

    sprintf(buf, psnewpage, name, physDev->job.PageNo,
526
	    physDev->logPixelsX, physDev->logPixelsY,
Alexandre Julliard's avatar
Alexandre Julliard committed
527 528
	    xtrans, ytrans, rotation);

529
    if( write_spool( dev, buf, strlen(buf) ) != strlen(buf) ) {
530
        WARN("WriteSpool error\n");
531
        ret = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
532
    }
533 534
    HeapFree( GetProcessHeap(), 0, buf );
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
535 536 537
}


538
BOOL PSDRV_WriteMoveTo(PHYSDEV dev, INT x, INT y)
Alexandre Julliard's avatar
Alexandre Julliard committed
539 540 541
{
    char buf[100];

Alexandre Julliard's avatar
Alexandre Julliard committed
542
    sprintf(buf, psmoveto, x, y);
543
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
Alexandre Julliard's avatar
Alexandre Julliard committed
544 545
}

546
BOOL PSDRV_WriteLineTo(PHYSDEV dev, INT x, INT y)
Alexandre Julliard's avatar
Alexandre Julliard committed
547 548 549
{
    char buf[100];

Alexandre Julliard's avatar
Alexandre Julliard committed
550
    sprintf(buf, pslineto, x, y);
551
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
Alexandre Julliard's avatar
Alexandre Julliard committed
552 553 554
}


555
BOOL PSDRV_WriteStroke(PHYSDEV dev)
Alexandre Julliard's avatar
Alexandre Julliard committed
556
{
557
    return PSDRV_WriteSpool(dev, psstroke, sizeof(psstroke)-1);
Alexandre Julliard's avatar
Alexandre Julliard committed
558 559 560 561
}



562
BOOL PSDRV_WriteRectangle(PHYSDEV dev, INT x, INT y, INT width,
563
			INT height)
Alexandre Julliard's avatar
Alexandre Julliard committed
564 565 566
{
    char buf[100];

Alexandre Julliard's avatar
Alexandre Julliard committed
567
    sprintf(buf, psrectangle, x, y, width, height, -width);
568
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
Alexandre Julliard's avatar
Alexandre Julliard committed
569 570
}

571
BOOL PSDRV_WriteArc(PHYSDEV dev, INT x, INT y, INT w, INT h, double ang1,
572
		      double ang2)
Alexandre Julliard's avatar
Alexandre Julliard committed
573 574 575
{
    char buf[256];

576 577
    /* Make angles -ve and swap order because we're working with an upside
       down y-axis */
578
    push_lc_numeric("C");
579
    sprintf(buf, psarc, x, y, w, h, -ang2, -ang1);
580
    pop_lc_numeric();
581
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
Alexandre Julliard's avatar
Alexandre Julliard committed
582 583
}

584 585 586 587 588 589 590 591
BOOL PSDRV_WriteCurveTo(PHYSDEV dev, POINT pts[3])
{
    char buf[256];

    sprintf(buf, pscurveto, pts[0].x, pts[0].y, pts[1].x, pts[1].y, pts[2].x, pts[2].y );
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
}

592
BOOL PSDRV_WriteSetFont(PHYSDEV dev, const char *name, matrix size, INT escapement, BOOL fake_italic)
Alexandre Julliard's avatar
Alexandre Julliard committed
593
{
594
    char *buf;
Alexandre Julliard's avatar
Alexandre Julliard committed
595

596
    buf = HeapAlloc( GetProcessHeap(), 0, strlen(name) + 256 );
Alexandre Julliard's avatar
Alexandre Julliard committed
597 598

    if(!buf) {
599
        WARN("HeapAlloc failed\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
600 601
        return FALSE;
    }
602

603 604 605 606
    sprintf( buf, psfindfont, name );
    PSDRV_WriteSpool( dev, buf, strlen(buf) );

    if (fake_italic) PSDRV_WriteSpool( dev, psfakeitalic, sizeof(psfakeitalic) - 1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
607

608 609 610 611 612 613 614 615 616 617 618 619
    sprintf( buf, pssizematrix, size.xx, size.xy, size.yx, size.yy );
    PSDRV_WriteSpool( dev, buf, strlen(buf) );

    if (fake_italic) PSDRV_WriteSpool( dev, psconcat, sizeof(psconcat) - 1 );

    if (escapement)
    {
        sprintf( buf, psrotatefont, -escapement );
        PSDRV_WriteSpool( dev, buf, strlen(buf) );
    }

    PSDRV_WriteSpool( dev, pssetfont, sizeof(pssetfont) - 1 );
620
    HeapFree( GetProcessHeap(), 0, buf );
621

Alexandre Julliard's avatar
Alexandre Julliard committed
622
    return TRUE;
623
}
Alexandre Julliard's avatar
Alexandre Julliard committed
624

625
BOOL PSDRV_WriteSetColor(PHYSDEV dev, PSCOLOR *color)
Alexandre Julliard's avatar
Alexandre Julliard committed
626
{
627
    PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
Alexandre Julliard's avatar
Alexandre Julliard committed
628 629 630 631 632
    char buf[256];

    PSDRV_CopyColor(&physDev->inkColor, color);
    switch(color->type) {
    case PSCOLOR_RGB:
633
        push_lc_numeric("C");
Alexandre Julliard's avatar
Alexandre Julliard committed
634 635
        sprintf(buf, pssetrgbcolor, color->value.rgb.r, color->value.rgb.g,
		color->value.rgb.b);
636
        pop_lc_numeric();
637
	return PSDRV_WriteSpool(dev, buf, strlen(buf));
Alexandre Julliard's avatar
Alexandre Julliard committed
638

639
    case PSCOLOR_GRAY:
640
        push_lc_numeric("C");
Alexandre Julliard's avatar
Alexandre Julliard committed
641
        sprintf(buf, pssetgray, color->value.gray.i);
642
        pop_lc_numeric();
643
	return PSDRV_WriteSpool(dev, buf, strlen(buf));
644

Alexandre Julliard's avatar
Alexandre Julliard committed
645
    default:
646
        ERR("Unknown colour type %d\n", color->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
647 648 649 650 651 652
	break;
    }

    return FALSE;
}

653
BOOL PSDRV_WriteSetPen(PHYSDEV dev)
Alexandre Julliard's avatar
Alexandre Julliard committed
654
{
655
    PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
Alexandre Julliard's avatar
Alexandre Julliard committed
656
    char buf[256];
657
    DWORD i, pos;
Alexandre Julliard's avatar
Alexandre Julliard committed
658

659
    sprintf(buf, pssetline, physDev->pen.width, physDev->pen.join, physDev->pen.endcap);
660
    PSDRV_WriteSpool(dev, buf, strlen(buf));
Alexandre Julliard's avatar
Alexandre Julliard committed
661

662 663 664 665 666 667
    if (physDev->pen.dash_len)
    {
        for (i = pos = 0; i < physDev->pen.dash_len; i++)
            pos += sprintf( buf + pos, " %u", physDev->pen.dash[i] );
        buf[0] = '[';
        sprintf(buf + pos, "] %u setdash\n", 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
668
    }
669
    else
670 671
        sprintf(buf, "[] %u setdash\n", 0);

672
   PSDRV_WriteSpool(dev, buf, strlen(buf));
673 674
	
   return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
675 676
}

677
BOOL PSDRV_WriteGlyphShow(PHYSDEV dev, LPCSTR g_name)
678 679
{
    char    buf[128];
680
    int     l;
681

682
    l = snprintf(buf, sizeof(buf), psglyphshow, g_name);
683

684 685 686
    if (l < sizeof(psglyphshow) - 2 || l > sizeof(buf) - 1) {
	WARN("Unusable glyph name '%s' - ignoring\n", g_name);
	return FALSE;
687
    }
688

689
    PSDRV_WriteSpool(dev, buf, l);
690 691
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
692

693
BOOL PSDRV_WriteFill(PHYSDEV dev)
Alexandre Julliard's avatar
Alexandre Julliard committed
694
{
695
    return PSDRV_WriteSpool(dev, psfill, sizeof(psfill)-1);
Alexandre Julliard's avatar
Alexandre Julliard committed
696 697
}

698
BOOL PSDRV_WriteEOFill(PHYSDEV dev)
699
{
700
    return PSDRV_WriteSpool(dev, pseofill, sizeof(pseofill)-1);
701 702
}

703
BOOL PSDRV_WriteGSave(PHYSDEV dev)
Alexandre Julliard's avatar
Alexandre Julliard committed
704
{
705
    return PSDRV_WriteSpool(dev, psgsave, sizeof(psgsave)-1);
Alexandre Julliard's avatar
Alexandre Julliard committed
706 707
}

708
BOOL PSDRV_WriteGRestore(PHYSDEV dev)
Alexandre Julliard's avatar
Alexandre Julliard committed
709
{
710
    return PSDRV_WriteSpool(dev, psgrestore, sizeof(psgrestore)-1);
Alexandre Julliard's avatar
Alexandre Julliard committed
711 712
}

713
BOOL PSDRV_WriteNewPath(PHYSDEV dev)
714
{
715
    return PSDRV_WriteSpool(dev, psnewpath, sizeof(psnewpath)-1);
716 717
}

718
BOOL PSDRV_WriteClosePath(PHYSDEV dev)
719
{
720
    return PSDRV_WriteSpool(dev, psclosepath, sizeof(psclosepath)-1);
721
}
Alexandre Julliard's avatar
Alexandre Julliard committed
722

723
BOOL PSDRV_WriteClip(PHYSDEV dev)
724
{
725
    return PSDRV_WriteSpool(dev, psclip, sizeof(psclip)-1);
726 727
}

728
BOOL PSDRV_WriteEOClip(PHYSDEV dev)
729
{
730
    return PSDRV_WriteSpool(dev, pseoclip, sizeof(pseoclip)-1);
731 732
}

733
BOOL PSDRV_WriteHatch(PHYSDEV dev)
734
{
735
    return PSDRV_WriteSpool(dev, pshatch, sizeof(pshatch)-1);
736 737
}

738
BOOL PSDRV_WriteRotate(PHYSDEV dev, float ang)
739 740
{
    char buf[256];
Alexandre Julliard's avatar
Alexandre Julliard committed
741

742
    push_lc_numeric("C");
743
    sprintf(buf, psrotate, ang);
744
    pop_lc_numeric();
745
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
746
}
Alexandre Julliard's avatar
Alexandre Julliard committed
747

748
BOOL PSDRV_WriteIndexColorSpaceBegin(PHYSDEV dev, int size)
749 750 751
{
    char buf[256];
    sprintf(buf, "[/Indexed /DeviceRGB %d\n<\n", size);
752
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
753 754
}

755
BOOL PSDRV_WriteIndexColorSpaceEnd(PHYSDEV dev)
756
{
757
    static const char buf[] = ">\n] setcolorspace\n";
758
    return PSDRV_WriteSpool(dev, buf, sizeof(buf) - 1);
759
}
760

761
static BOOL PSDRV_WriteRGB(PHYSDEV dev, COLORREF *map, int number)
762
{
763
    char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
764 765 766 767
    int i;

    ptr = buf;
    for(i = 0; i < number; i++) {
768
        sprintf(ptr, "%02x%02x%02x%c", (int)GetRValue(map[i]),
769 770 771 772
		(int)GetGValue(map[i]), (int)GetBValue(map[i]),
		((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');
	ptr += 7;
    }
773
    PSDRV_WriteSpool(dev, buf, number * 7);
774
    HeapFree( GetProcessHeap(), 0, buf );
775 776 777
    return TRUE;
}

778 779
BOOL PSDRV_WriteRGBQUAD(PHYSDEV dev, const RGBQUAD *rgb, int number)
{
780
    char *buf = HeapAlloc( GetProcessHeap(), 0, number * 7 + 1 ), *ptr;
781 782 783
    int i;

    ptr = buf;
784
    for(i = 0; i < number; i++, rgb++)
785 786 787 788
        ptr += sprintf(ptr, "%02x%02x%02x%c", rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue,
                       ((i & 0x7) == 0x7) || (i == number - 1) ? '\n' : ' ');

    PSDRV_WriteSpool(dev, buf, ptr - buf);
789
    HeapFree( GetProcessHeap(), 0, buf );
790 791 792
    return TRUE;
}

793
static BOOL PSDRV_WriteImageDict(PHYSDEV dev, WORD depth, BOOL grayscale,
794
				 INT widthSrc, INT heightSrc, char *bits, BOOL top_down)
795
{
796
    static const char start[] = "<<\n"
797 798 799
      " /ImageType 1\n /Width %d\n /Height %d\n /BitsPerComponent %d\n"
      " /ImageMatrix [%d 0 0 %d 0 %d]\n";

800 801
    static const char decode1[] = " /Decode [0 %d]\n";
    static const char decode3[] = " /Decode [0 1 0 1 0 1]\n";
802

803 804
    static const char end[] = " /DataSource currentfile /ASCII85Decode filter /RunLengthDecode filter\n>>\n";
    static const char endbits[] = " /DataSource <%s>\n>>\n";
805
    char buf[1000];
806

807 808 809 810 811 812
    if (top_down)
        sprintf(buf, start, widthSrc, heightSrc,
                (depth < 8) ? depth : 8, widthSrc, heightSrc, 0);
    else
        sprintf(buf, start, widthSrc, heightSrc,
                (depth < 8) ? depth : 8, widthSrc, -heightSrc, heightSrc);
Alexandre Julliard's avatar
Alexandre Julliard committed
813

814
    PSDRV_WriteSpool(dev, buf, strlen(buf));
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

    switch(depth) {
    case 8:
        sprintf(buf, decode1, 255);
	break;

    case 4:
        sprintf(buf, decode1, 15);
	break;

    case 1:
        sprintf(buf, decode1, 1);
	break;

    default:
830 831 832 833
        if (grayscale)
            sprintf(buf, decode1, 1);
        else
            strcpy(buf, decode3);
834 835 836
	break;
    }

837
    PSDRV_WriteSpool(dev, buf, strlen(buf));
838

839
    if(!bits) {
840
        PSDRV_WriteSpool(dev, end, sizeof(end) - 1);
841
    } else {
842
        sprintf(buf, endbits, bits);
843
        PSDRV_WriteSpool(dev, buf, strlen(buf));
844
    }
845 846 847 848

    return TRUE;
}

849
BOOL PSDRV_WriteImage(PHYSDEV dev, WORD depth, BOOL grayscale, INT xDst, INT yDst,
850
		      INT widthDst, INT heightDst, INT widthSrc,
851
		      INT heightSrc, BOOL mask, BOOL top_down)
852 853 854 855 856 857 858
{
    static const char start[] = "%d %d translate\n%d %d scale\n";
    static const char image[] = "image\n";
    static const char imagemask[] = "imagemask\n";
    char buf[100];

    sprintf(buf, start, xDst, yDst, widthDst, heightDst);
859
    PSDRV_WriteSpool(dev, buf, strlen(buf));
860
    PSDRV_WriteImageDict(dev, depth, grayscale, widthSrc, heightSrc, NULL, top_down);
861
    if(mask)
862
        PSDRV_WriteSpool(dev, imagemask, sizeof(imagemask) - 1);
863
    else
864
        PSDRV_WriteSpool(dev, image, sizeof(image) - 1);
865 866 867
    return TRUE;
}

868

869
BOOL PSDRV_WriteBytes(PHYSDEV dev, const BYTE *bytes, DWORD number)
870
{
871
    char *buf = HeapAlloc( GetProcessHeap(), 0, number * 3 + 1 );
872
    char *ptr;
873
    unsigned int i;
874

875
    ptr = buf;
876

877
    for(i = 0; i < number; i++) {
878 879 880 881 882 883
        sprintf(ptr, "%02x", bytes[i]);
        ptr += 2;
        if(((i & 0xf) == 0xf) || (i == number - 1)) {
            strcpy(ptr, "\n");
            ptr++;
        }
884
    }
885
    PSDRV_WriteSpool(dev, buf, ptr - buf);
886
    HeapFree( GetProcessHeap(), 0, buf );
887 888 889
    return TRUE;
}

890
BOOL PSDRV_WriteData(PHYSDEV dev, const BYTE *data, DWORD number)
891
{
892
    int num, num_left = number;
893

894 895
    do {
        num = min(num_left, 60);
896 897
        PSDRV_WriteSpool(dev, (LPCSTR)data, num);
        PSDRV_WriteSpool(dev, "\n", 1);
898 899 900
        data += num;
        num_left -= num;
    } while(num_left);
901 902 903

    return TRUE;
}
904

905
BOOL PSDRV_WriteArrayPut(PHYSDEV dev, CHAR *pszArrayName, INT nIndex, LONG lObject)
906 907 908 909
{
    char buf[100];

    sprintf(buf, psarrayput, pszArrayName, nIndex, lObject);
910
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
911 912
}

913
BOOL PSDRV_WriteArrayDef(PHYSDEV dev, CHAR *pszArrayName, INT nSize)
914 915 916 917
{
    char buf[100];

    sprintf(buf, psarraydef, pszArrayName, nSize);
918
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
919 920
}

921
BOOL PSDRV_WriteRectClip(PHYSDEV dev, INT x, INT y, INT w, INT h)
922 923 924
{
    char buf[100];

925
    sprintf(buf, psrectclip, x, y, w, h);
926
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
927 928
}

929
BOOL PSDRV_WriteRectClip2(PHYSDEV dev, CHAR *pszArrayName)
930 931 932 933
{
    char buf[100];

    sprintf(buf, psrectclip2, pszArrayName);
934
    return PSDRV_WriteSpool(dev, buf, strlen(buf));
935
}
936

937
BOOL PSDRV_WriteDIBPatternDict(PHYSDEV dev, const BITMAPINFO *bmi, BYTE *bits, UINT usage)
938
{
939 940 941 942
    static const char mypat[] = "/mypat\n";
    static const char do_pattern[] = "<<\n /PaintType 1\n /PatternType 1\n /TilingType 1\n "
      "/BBox [0 0 %d %d]\n /XStep %d\n /YStep %d\n /PaintProc {\n  begin\n  0 0 translate\n"
      "  %d %d scale\n  mypat image\n  end\n }\n>>\n matrix makepattern setpattern\n";
943
    PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
944
    char *buf, *ptr;
945
    INT w, h, x, y, w_mult, h_mult, abs_height = abs( bmi->bmiHeader.biHeight );
946 947
    COLORREF map[2];

948 949
    TRACE( "size %dx%dx%d\n",
           bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bmi->bmiHeader.biBitCount);
950 951 952 953 954 955 956

    if(bmi->bmiHeader.biBitCount != 1) {
        FIXME("dib depth %d not supported\n", bmi->bmiHeader.biBitCount);
	return FALSE;
    }

    w = bmi->bmiHeader.biWidth & ~0x7;
957
    h = abs_height & ~0x7;
958

959
    buf = HeapAlloc( GetProcessHeap(), 0, sizeof(do_pattern) + 100 );
960 961 962 963 964 965 966 967
    ptr = buf;
    for(y = h-1; y >= 0; y--) {
        for(x = 0; x < w/8; x++) {
	    sprintf(ptr, "%02x", *(bits + x/8 + y *
				   (bmi->bmiHeader.biWidth + 31) / 32 * 4));
	    ptr += 2;
	}
    }
968
    PSDRV_WriteSpool(dev, mypat, sizeof(mypat) - 1);
969
    PSDRV_WriteImageDict(dev, 1, FALSE, 8, 8, buf, bmi->bmiHeader.biHeight < 0);
970
    PSDRV_WriteSpool(dev, "def\n", 4);
971

972 973 974 975 976
    PSDRV_WriteIndexColorSpaceBegin(dev, 1);
    map[0] = GetTextColor( dev->hdc );
    map[1] = GetBkColor( dev->hdc );
    PSDRV_WriteRGB(dev, map, 2);
    PSDRV_WriteIndexColorSpaceEnd(dev);
977 978 979 980 981

    /* Windows seems to scale patterns so that a one pixel corresponds to 1/300" */
    w_mult = (physDev->logPixelsX + 150) / 300;
    h_mult = (physDev->logPixelsY + 150) / 300;
    sprintf(buf, do_pattern, w * w_mult, h * h_mult, w * w_mult, h * h_mult, w * w_mult, h * h_mult);
982
    PSDRV_WriteSpool(dev,  buf, strlen(buf));
983
    HeapFree( GetProcessHeap(), 0, buf );
984 985
    return TRUE;
}