indent postscript_print.c postscript_print.h

This commit is contained in:
Bill Kendrick 2017-10-15 11:04:11 -07:00
parent 10c9976e39
commit bc78db547f
2 changed files with 91 additions and 105 deletions

View file

@ -62,7 +62,7 @@
#include "pixels.h" #include "pixels.h"
#define MARGIN 36 /* Margin to put around image, in points (inch/72) (36pt = 0.5") */ #define MARGIN 36 /* Margin to put around image, in points (inch/72) (36pt = 0.5") */
#define my_min(x,y) ((x < y) ? (x) : (y)) #define my_min(x,y) ((x < y) ? (x) : (y))
@ -78,12 +78,9 @@ static int f2dec(float f)
/* Actually save the PostScript data to the file stream: */ /* Actually save the PostScript data to the file stream: */
int do_ps_save(FILE * fi, int do_ps_save(FILE * fi,
const char *restrict const fname, const char *restrict const fname, SDL_Surface * surf, const char *restrict pprsize, int is_pipe)
SDL_Surface * surf,
const char *restrict pprsize,
int is_pipe)
{ {
const struct paper * ppr; const struct paper *ppr;
int img_w = surf->w; int img_w = surf->w;
int img_h = surf->h; int img_h = surf->h;
int r_img_w, r_img_h; int r_img_w, r_img_h;
@ -94,8 +91,8 @@ int do_ps_save(FILE * fi,
int plane; int plane;
Uint8 r, g, b; Uint8 r, g, b;
char buf[256]; char buf[256];
Uint32(*getpixel) (SDL_Surface *, int, int) =
getpixels[surf->format->BytesPerPixel]; Uint32(*getpixel) (SDL_Surface *, int, int) = getpixels[surf->format->BytesPerPixel];
int printed_img_w, printed_img_h; int printed_img_w, printed_img_h;
time_t t = time(NULL); time_t t = time(NULL);
int rotate; int rotate;
@ -104,38 +101,38 @@ int do_ps_save(FILE * fi,
/* Determine paper size: */ /* Determine paper size: */
paperinit(); // FIXME: Should we do this at startup? -bjk 2007.06.25 paperinit(); // FIXME: Should we do this at startup? -bjk 2007.06.25
if (pprsize == NULL) if (pprsize == NULL)
{
/* User did not request a specific paper size (on command-line or
in config file), ask the system. It will return either their
$PAPER env. var., the value from /etc/papersize, or NULL: */
pprsize = systempapername();
if (pprsize == NULL)
{ {
/* No setting, env. var. or /etc/ file; use the default! */ /* User did not request a specific paper size (on command-line or
in config file), ask the system. It will return either their
$PAPER env. var., the value from /etc/papersize, or NULL: */
pprsize = defaultpapername(); pprsize = systempapername();
if (pprsize == NULL)
{
/* No setting, env. var. or /etc/ file; use the default! */
pprsize = defaultpapername();
#ifdef DEBUG #ifdef DEBUG
printf("Using default paper\n"); printf("Using default paper\n");
#endif #endif
} }
#ifdef DEBUG #ifdef DEBUG
else else
{ {
printf("Using system paper\n"); printf("Using system paper\n");
} }
#endif #endif
} }
#ifdef DEBUG #ifdef DEBUG
else else
{ {
printf("Using user paper\n"); printf("Using user paper\n");
} }
#endif #endif
#ifdef DEBUG #ifdef DEBUG
@ -151,28 +148,26 @@ int do_ps_save(FILE * fi,
ppr_h = paperpsheight(ppr); ppr_h = paperpsheight(ppr);
#ifdef DEBUG #ifdef DEBUG
printf("Paper is %d x %d (%.2f\" x %.2f\")\n", ppr_w, ppr_h, printf("Paper is %d x %d (%.2f\" x %.2f\")\n", ppr_w, ppr_h, (float)ppr_w / 72.0, (float)ppr_h / 72.0);
(float) ppr_w / 72.0, (float) ppr_h / 72.0);
#endif #endif
paperdone(); // FIXME: Should we do this at quit? -bjk 2007.06.25 paperdone(); // FIXME: Should we do this at quit? -bjk 2007.06.25
/* Determine whether it's best to rotate the image: */ /* Determine whether it's best to rotate the image: */
if ((ppr_w >= ppr_h && img_w >= img_h) || if ((ppr_w >= ppr_h && img_w >= img_h) || (ppr_w <= ppr_h && img_w <= img_h))
(ppr_w <= ppr_h && img_w <= img_h)) {
{ rotate = 0;
rotate = 0; r_img_w = img_w;
r_img_w = img_w; r_img_h = img_h;
r_img_h = img_h; }
}
else else
{ {
rotate = 1; rotate = 1;
r_img_w = img_h; r_img_w = img_h;
r_img_h = img_w; r_img_h = img_w;
} }
#ifdef DEBUG #ifdef DEBUG
printf("Image is %d x %d\n", img_w, img_h); printf("Image is %d x %d\n", img_w, img_h);
@ -183,15 +178,13 @@ int do_ps_save(FILE * fi,
/* Determine scale: */ /* Determine scale: */
scale = my_min(((float) (ppr_w - (MARGIN * 2)) / (float) r_img_w), scale = my_min(((float)(ppr_w - (MARGIN * 2)) / (float)r_img_w), ((float)(ppr_h - (MARGIN * 2)) / (float)r_img_h));
((float) (ppr_h - (MARGIN * 2)) / (float) r_img_h));
printed_img_w = r_img_w * scale; printed_img_w = r_img_w * scale;
printed_img_h = r_img_h * scale; printed_img_h = r_img_h * scale;
#ifdef DEBUG #ifdef DEBUG
printf("Scaling image by %.2f (to %d x %d)\n", scale, printf("Scaling image by %.2f (to %d x %d)\n", scale, printed_img_w, printed_img_h);
printed_img_w, printed_img_h);
#endif #endif
@ -206,7 +199,7 @@ int do_ps_save(FILE * fi,
/* Begin PostScript output with some useful meta info in comments: */ /* Begin PostScript output with some useful meta info in comments: */
fprintf(fi, "%%!PS-Adobe-2.0 EPSF-2.0\n"); // we need LanguageLevel2 for color fprintf(fi, "%%!PS-Adobe-2.0 EPSF-2.0\n"); // we need LanguageLevel2 for color
fprintf(fi, "%%%%Title: (%s)\n", fname); fprintf(fi, "%%%%Title: (%s)\n", fname);
@ -217,8 +210,7 @@ int do_ps_save(FILE * fi,
fprintf(fi, "%%%%Pages: 1\n"); fprintf(fi, "%%%%Pages: 1\n");
fprintf(fi, "%%%%BoundingBox: 0 0 %d %d\n", (int) (ppr_w + 0.5), (int) fprintf(fi, "%%%%BoundingBox: 0 0 %d %d\n", (int)(ppr_w + 0.5), (int)(ppr_h + 0.5));
(ppr_h + 0.5));
fprintf(fi, "%%%%EndComments\n"); fprintf(fi, "%%%%EndComments\n");
@ -237,24 +229,20 @@ int do_ps_save(FILE * fi,
fprintf(fi, "%%%%Page: 1 1\n"); fprintf(fi, "%%%%Page: 1 1\n");
fprintf(fi, "<< /PageSize [ %d %d ] /ImagingBBox null >> setpagedevice\n", fprintf(fi, "<< /PageSize [ %d %d ] /ImagingBBox null >> setpagedevice\n", ppr_w, ppr_h);
ppr_w, ppr_h);
fprintf(fi, "gsave\n"); fprintf(fi, "gsave\n");
/* 'translate' moves the user space origin to a new position with /* 'translate' moves the user space origin to a new position with
respect to the current page, leaving the orientation of the axes and respect to the current page, leaving the orientation of the axes and
the unit lengths unchanged. */ the unit lengths unchanged. */
fprintf(fi, "%d.%02d %d.%02d translate\n", fprintf(fi, "%d.%02d %d.%02d translate\n", f2int(tlate_x), f2dec(tlate_x), f2int(tlate_y), f2dec(tlate_y));
f2int(tlate_x), f2dec(tlate_x),
f2int(tlate_y), f2dec(tlate_y));
/* 'scale' modifies the unit lengths independently along the current /* 'scale' modifies the unit lengths independently along the current
x and y axes, leaving the origin location and the orientation of the x and y axes, leaving the origin location and the orientation of the
axes unchanged. */ axes unchanged. */
fprintf(fi, "%d.%02d %d.%02d scale\n", fprintf(fi, "%d.%02d %d.%02d scale\n",
f2int(printed_img_w), f2dec(printed_img_w), f2int(printed_img_w), f2dec(printed_img_w), f2int(printed_img_h), f2dec(printed_img_h));
f2int(printed_img_h), f2dec(printed_img_h));
/* Rotate the image */ /* Rotate the image */
if (rotate) if (rotate)
@ -274,23 +262,23 @@ int do_ps_save(FILE * fi,
cur_line_len = 0; cur_line_len = 0;
for (y = 0; y < img_h; y++) for (y = 0; y < img_h; y++)
{
for (plane = 0; plane < 3; plane++)
{ {
for (x = 0; x < img_w; x++) for (plane = 0; plane < 3; plane++)
{
SDL_GetRGB(getpixel(surf, x, y), surf->format, &r, &g, &b);
fprintf(fi, "%02x", (plane == 0 ? r : (plane == 1 ? g : b)));
cur_line_len++;
if (cur_line_len >= 30)
{ {
fprintf(fi, "\n"); for (x = 0; x < img_w; x++)
cur_line_len = 0; {
SDL_GetRGB(getpixel(surf, x, y), surf->format, &r, &g, &b);
fprintf(fi, "%02x", (plane == 0 ? r : (plane == 1 ? g : b)));
cur_line_len++;
if (cur_line_len >= 30)
{
fprintf(fi, "\n");
cur_line_len = 0;
}
}
} }
}
} }
}
fprintf(fi, "\n"); fprintf(fi, "\n");
fprintf(fi, "grestore\n"); fprintf(fi, "grestore\n");
@ -299,16 +287,16 @@ int do_ps_save(FILE * fi,
fprintf(fi, "%%%%EOF\n"); fprintf(fi, "%%%%EOF\n");
if (!is_pipe) if (!is_pipe)
{ {
fclose(fi); fclose(fi);
return 1; return 1;
} }
else else
{ {
pid_t child_pid, w; pid_t child_pid, w;
int status; int status;
child_pid = pclose(fi); child_pid = pclose(fi);
/* debug */ /* debug */
/* /*
@ -316,15 +304,18 @@ int do_ps_save(FILE * fi,
printf("errno = %d\n", errno); fflush(stdout); printf("errno = %d\n", errno); fflush(stdout);
*/ */
if (child_pid < 0 || (errno != 0 && errno != EAGAIN)) { /* FIXME: This right? */ if (child_pid < 0 || (errno != 0 && errno != EAGAIN))
return 0; { /* FIXME: This right? */
} else if (child_pid == 0) { return 0;
return 1; }
} else if (child_pid == 0)
{
return 1;
}
do do
{ {
w = waitpid(child_pid, &status, 0); w = waitpid(child_pid, &status, 0);
/* debug */ /* debug */
/* /*
@ -339,15 +330,14 @@ int do_ps_save(FILE * fi,
printf("continued\n"); printf("continued\n");
} }
*/ */
}
while (w != -1 && !WIFEXITED(status) && !WIFSIGNALED(status));
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) /* Not happy exit */
return 0;
return 1;
} }
while (w != -1 && !WIFEXITED(status) && !WIFSIGNALED(status));
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) /* Not happy exit */
return 0;
return 1;
}
} }
#endif #endif

View file

@ -79,12 +79,8 @@
#ifdef PRINTMETHOD_PS #ifdef PRINTMETHOD_PS
int do_ps_save(FILE * fi, int do_ps_save(FILE * fi,
const char *restrict const fname, const char *restrict const fname, SDL_Surface * surf, const char *restrict pprsize, int is_pipe);
SDL_Surface * surf,
const char *restrict pprsize,
int is_pipe);
#endif #endif
#endif /* POSTSCRIPT_PRINT_H */ #endif /* POSTSCRIPT_PRINT_H */