Skip to content

Commit

Permalink
Add --media-pos and --media-type
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Jul 25, 2023
1 parent a08fe4c commit ca6a9e4
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bytestream
7 changes: 3 additions & 4 deletions lib/PwgPgHdr.codable
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ CODABLE(PwgPgHdr)
ENUM_LABEL(LongEdgeFirst))
PADDING(12)
ENUM(uint32_t, MediaPosition,
ENUM_VALUE(Auto, 0), ENUM_LABEL(Main), ENUM_LABEL(Alternate),
ENUM_LABEL(LargeCapacity), ENUM_LABEL(Manual), ENUM_LABEL(Envelope),
ENUM_LABEL(Disc), ENUM_LABEL(Photo), ENUM_LABEL(Hagaki),
ENUM_VALUE(AutomaticMediaPosition, 0), ENUM_LABEL(Main), ENUM_LABEL(Alternate),
ENUM_LABEL(LargeCapacity), ENUM_LABEL(Manual), ENUM_LABEL(EnvelopeMediaPosition),
ENUM_LABEL(DiscMediaPosition), ENUM_LABEL(PhotoMediaPosition), ENUM_LABEL(Hagaki),
ENUM_LABEL(MainRoll), ENUM_LABEL(AlternateRoll), ENUM_LABEL(Top),
ENUM_LABEL(Middle), ENUM_LABEL(Bottom), ENUM_LABEL(Side),
ENUM_LABEL(Left), ENUM_LABEL(Right), ENUM_LABEL(Center),
Expand All @@ -40,7 +40,6 @@ CODABLE(PwgPgHdr)
ENUM_LABEL(Roll3), ENUM_LABEL(Roll4), ENUM_LABEL(Roll5),
ENUM_LABEL(Roll6), ENUM_LABEL(Roll7), ENUM_LABEL(Roll8),
ENUM_LABEL(Roll9), ENUM_LABEL(Roll10))

FIELD(uint32_t, MediaWeightMetric)
PADDING(8)
FIELD(uint32_t, NumCopies)
Expand Down
34 changes: 32 additions & 2 deletions lib/UrfPgHdr.codable
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,38 @@ CODABLE(UrfPgHdr)
ENUM_VALUE(Draft, 3),
ENUM_VALUE(Normal, 4),
ENUM_VALUE(High, 5))
PADDING(1)
FIELD(uint8_t, MediaPos)
ENUM(uint8_t, MediaType, ENUM_VALUE(AutomaticMediaType, 0),
ENUM_LABEL(Stationery),
ENUM_LABEL(Transparency),
ENUM_LABEL(Envelope),
ENUM_LABEL(Cardstock),
ENUM_LABEL(Labels),
ENUM_LABEL(StationeryLetterhead),
ENUM_LABEL(Disc),
ENUM_LABEL(PhotographicMatte),
ENUM_LABEL(PhotographicSatin),
ENUM_LABEL(PhotographicSemiGloss),
ENUM_LABEL(PhotographicGlossy),
ENUM_LABEL(PhotographicHighGloss),
ENUM_LABEL(OtherMediaType))
ENUM(uint8_t, MediaPosition,
ENUM_VALUE(AutomaticMediaPosition, 0), ENUM_LABEL(Main), ENUM_LABEL(Alternate),
ENUM_LABEL(LargeCapacity), ENUM_LABEL(Manual), ENUM_LABEL(EnvelopeMediaPosition),
ENUM_LABEL(DiscMediaPosition), ENUM_LABEL(PhotoMediaPosition), ENUM_LABEL(Hagaki),
ENUM_LABEL(MainRoll), ENUM_LABEL(AlternateRoll), ENUM_LABEL(Top),
ENUM_LABEL(Middle), ENUM_LABEL(Bottom), ENUM_LABEL(Side),
ENUM_LABEL(Left), ENUM_LABEL(Right), ENUM_LABEL(Center),
ENUM_LABEL(Rear), ENUM_LABEL(ByPassTray), ENUM_LABEL(Tray1),
ENUM_LABEL(Tray2), ENUM_LABEL(Tray3), ENUM_LABEL(Tray4),
ENUM_LABEL(Tray5), ENUM_LABEL(Tray6), ENUM_LABEL(Tray7),
ENUM_LABEL(Tray8), ENUM_LABEL(Tray9), ENUM_LABEL(Tray10),
ENUM_LABEL(Tray11), ENUM_LABEL(Tray12), ENUM_LABEL(Tray13),
ENUM_LABEL(Tray14), ENUM_LABEL(Tray15), ENUM_LABEL(Tray16),
ENUM_LABEL(Tray17), ENUM_LABEL(Tray18), ENUM_LABEL(Tray19),
ENUM_LABEL(Tray20), ENUM_LABEL(Roll1), ENUM_LABEL(Roll2),
ENUM_LABEL(Roll3), ENUM_LABEL(Roll4), ENUM_LABEL(Roll5),
ENUM_LABEL(Roll6), ENUM_LABEL(Roll7), ENUM_LABEL(Roll8),
ENUM_LABEL(Roll9), ENUM_LABEL(Roll10))
PADDING(2)
PADDING(4)
FIELD(uint32_t, Width)
Expand Down
56 changes: 56 additions & 0 deletions lib/mediaposition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef MEDIAPOSITION_H
#define MEDIAPOSITION_H

#define MEDIA_POSITION_MAP \
{{"auto", PrintParameters::AutomaticMediaPosition}, \
{"main", PrintParameters::Main}, \
{"alternate", PrintParameters::Alternate}, \
{"large-capacity", PrintParameters::LargeCapacity}, \
{"manual", PrintParameters::Manual}, \
{"envelope", PrintParameters::Envelope}, \
{"disc", PrintParameters::Disc}, \
{"photo", PrintParameters::PhotoMediaPosition}, \
{"hagaki", PrintParameters::Hagaki}, \
{"main-roll", PrintParameters::MainRoll}, \
{"alternate-roll", PrintParameters::AlternateRoll}, \
{"top", PrintParameters::Top}, \
{"middle", PrintParameters::Middle}, \
{"bottom", PrintParameters::Bottom}, \
{"side", PrintParameters::Side}, \
{"left", PrintParameters::Left}, \
{"right", PrintParameters::Right}, \
{"center", PrintParameters::Center}, \
{"rear", PrintParameters::Rear}, \
{"by-pass-tray", PrintParameters::ByPassTray}, \
{"tray-1", PrintParameters::Tray1}, \
{"tray-2", PrintParameters::Tray2}, \
{"tray-3", PrintParameters::Tray3}, \
{"tray-4", PrintParameters::Tray4}, \
{"tray-5", PrintParameters::Tray5}, \
{"tray-6", PrintParameters::Tray6}, \
{"tray-7", PrintParameters::Tray7}, \
{"tray-8", PrintParameters::Tray8}, \
{"tray-9", PrintParameters::Tray9}, \
{"tray-10", PrintParameters::Tray10}, \
{"tray-11", PrintParameters::Tray11}, \
{"tray-12", PrintParameters::Tray12}, \
{"tray-13", PrintParameters::Tray13}, \
{"tray-14", PrintParameters::Tray14}, \
{"tray-15", PrintParameters::Tray15}, \
{"tray-16", PrintParameters::Tray16}, \
{"tray-17", PrintParameters::Tray17}, \
{"tray-18", PrintParameters::Tray18}, \
{"tray-19", PrintParameters::Tray19}, \
{"tray-20", PrintParameters::Tray20}, \
{"roll-1", PrintParameters::Roll1}, \
{"roll-2", PrintParameters::Roll2}, \
{"roll-3", PrintParameters::Roll3}, \
{"roll-4", PrintParameters::Roll4}, \
{"roll-5", PrintParameters::Roll5}, \
{"roll-6", PrintParameters::Roll6}, \
{"roll-7", PrintParameters::Roll7}, \
{"roll-8", PrintParameters::Roll8}, \
{"roll-9", PrintParameters::Roll9}, \
{"roll-10", PrintParameters::Roll10}}

#endif // MEDIAPOSITION_H
37 changes: 29 additions & 8 deletions lib/ppm2pwg.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <map>
#include <string.h>

#include <array.h>
Expand Down Expand Up @@ -162,13 +163,36 @@ void compress_line(uint8_t* raw, size_t len, Bytestream& outBts, int colors)
}
}

static std::map<std::string, UrfPgHdr::MediaType_enum>
UrfMediaTypeMappings {{"auto", UrfPgHdr::AutomaticMediaType},
{"stationery", UrfPgHdr::Stationery},
{"transparency", UrfPgHdr::Transparency},
{"envelope", UrfPgHdr::Envelope},
{"cardstock", UrfPgHdr::Cardstock},
{"labels", UrfPgHdr::Labels},
{"stationery-letterhead", UrfPgHdr::StationeryLetterhead},
{"disc", UrfPgHdr::Disc},
{"photographic-matte", UrfPgHdr::PhotographicMatte},
{"photographic-satin", UrfPgHdr::PhotographicSatin},
{"photographic-semi-gloss", UrfPgHdr::PhotographicSemiGloss},
{"photographic-glossy", UrfPgHdr::PhotographicGlossy},
{"photographic-high-gloss", UrfPgHdr::PhotographicHighGloss},
{"other", UrfPgHdr::OtherMediaType}};

bool isUrfMediaType(std::string mediaType)
{
return UrfMediaTypeMappings.find(mediaType) != UrfMediaTypeMappings.end();
}

void make_pwg_hdr(Bytestream& outBts, const PrintParameters& params, bool backside, bool verbose)
{
PwgPgHdr outHdr;

outHdr.MediaType = params.mediaType;
outHdr.Duplex = params.isTwoSided();
outHdr.HWResolutionX = params.hwResW;
outHdr.HWResolutionY = params.hwResH;
outHdr.setMediaPosition(params.mediaPosition);
outHdr.NumCopies = 1;
outHdr.PageSizeX = round(params.getPaperSizeWInPoints());
outHdr.PageSizeY = round(params.getPaperSizeHInPoints());
Expand All @@ -187,10 +211,7 @@ void make_pwg_hdr(Bytestream& outBts, const PrintParameters& params, bool backsi
outHdr.CrossFeedTransform = backside&&params.getBackHFlip() ? -1 : 1;
outHdr.FeedTransform = backside&&params.getBackVFlip() ? -1 : 1;
outHdr.AlternatePrimary = 0x00ffffff;
outHdr.PrintQuality = (params.quality == PrintParameters::DraftQuality ? PwgPgHdr::Draft
: (params.quality == PrintParameters::NormalQuality ? PwgPgHdr::Normal
: (params.quality == PrintParameters::HighQuality ? PwgPgHdr::High
: PwgPgHdr::DefaultPrintQuality)));
outHdr.setPrintQuality(params.quality);
outHdr.PageSizeName = params.paperSizeName;

if(verbose)
Expand Down Expand Up @@ -218,10 +239,10 @@ void make_urf_hdr(Bytestream& outBts, const PrintParameters& params, bool verbos
? UrfPgHdr::ShortSide
: UrfPgHdr::LongSide)
: UrfPgHdr::NoDuplex;
outHdr.Quality = (params.quality == PrintParameters::DraftQuality ? UrfPgHdr::Draft
: (params.quality == PrintParameters::NormalQuality ? UrfPgHdr::Normal
: (params.quality == PrintParameters::HighQuality ? UrfPgHdr::High
: UrfPgHdr::DefaultQuality)));
outHdr.setQuality(params.quality);
outHdr.MediaType = params.mediaType == "" ? UrfPgHdr::AutomaticMediaType
: UrfMediaTypeMappings.at(params.mediaType);
outHdr.setMediaPosition(params.mediaPosition);
outHdr.Width = params.getPaperSizeWInPixels();
outHdr.Height = params.getPaperSizeHInPixels();
outHdr.HWRes = params.hwResW;
Expand Down
3 changes: 3 additions & 0 deletions lib/ppm2pwg.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <bytestream.h>
#include "printparameters.h"
#include <string>

Bytestream make_pwg_file_hdr();

Expand All @@ -20,4 +21,6 @@ void compress_lines(Bytestream& bmpBts, Bytestream& outBts,

void compress_line(uint8_t* raw, size_t len, Bytestream& outBts, int Colors);

bool isUrfMediaType(std::string mediaType);

#endif //PPM2PWG_H
57 changes: 57 additions & 0 deletions lib/printparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,60 @@ class PrintParameters
ManualTumble
};

enum MediaPosition
{
AutomaticMediaPosition = 0,
Main,
Alternate,
LargeCapacity,
Manual,
Envelope,
Disc,
PhotoMediaPosition,
Hagaki,
MainRoll,
AlternateRoll,
Top,
Middle,
Bottom,
Side,
Left,
Right,
Center,
Rear,
ByPassTray,
Tray1,
Tray2,
Tray3,
Tray4,
Tray5,
Tray6,
Tray7,
Tray8,
Tray9,
Tray10,
Tray11,
Tray12,
Tray13,
Tray14,
Tray15,
Tray16,
Tray17,
Tray18,
Tray19,
Tray20,
Roll1,
Roll2,
Roll3,
Roll4,
Roll5,
Roll6,
Roll7,
Roll8,
Roll9,
Roll10
};

Format format = PDF;

ColorMode colorMode = sRGB24;
Expand Down Expand Up @@ -91,6 +145,9 @@ class PrintParameters

PageRangeList pageRangeList;

MediaPosition mediaPosition = AutomaticMediaPosition;
std::string mediaType;

size_t getPaperSizeWInPixels() const;
size_t getPaperSizeHInPixels() const;

Expand Down
23 changes: 19 additions & 4 deletions utils/pdf2printable_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <cstring>

#include "pdf2printable.h"
#include "ppm2pwg.h"
#include "argget.h"
#include "mediaposition.h"

#define HELPTEXT "Options from 'resolution' and onwards only affect raster output formats.\n" \
"Use \"-\" as filename for stdin/stdout."
Expand Down Expand Up @@ -83,14 +85,19 @@ int main(int argc, char** argv)
{"-q", "--quality"},
"Quality setting in raster header (draft/normal/high)");
SwitchArg<bool> antiAliasOpt(params.antiAlias, {"-aa", "--antaialias"}, "Enable antialiasing in rasterization");
EnumSwitchArg<PrintParameters::MediaPosition> mediaPositionOpt(params.mediaPosition, MEDIA_POSITION_MAP,
{"-mp", "--media-pos"},
"Media position, e.g.: main, top, left, roll-2 etc.");
SwitchArg<std::string> mediaTypeOpt(params.mediaType, {"-mt", "--media-type"}, "Media type, e.g.: stationery, cardstock etc.");

PosArg pdfArg(infile, "PDF-file");
PosArg outArg(outfile, "out-file");

ArgGet args({&helpOpt, &verboseOpt, &formatOpt, &pagesOpt,
&copiesOpt, /*&pageCopiesOpt,*/ &paperSizeOpt, &resolutionOpt,
&resolutionXOpt, &resolutionYOpt, &duplexOpt, &tumbleOpt,
&backXformOpt, &colorModeOpt, &qualityOpt, &antiAliasOpt},
&backXformOpt, &colorModeOpt, &qualityOpt, &antiAliasOpt,
&mediaPositionOpt, &mediaTypeOpt},
{&pdfArg, &outArg});

bool correctArgs = args.get_args(argc, argv);
Expand Down Expand Up @@ -170,10 +177,18 @@ int main(int argc, char** argv)
params.duplexMode = PrintParameters::TwoSidedLongEdge;
}

if(params.format == PrintParameters::URF && (params.getBitsPerColor() == 1 || params.isBlack()))
if(params.format == PrintParameters::URF)
{
print_error("URF does not support black or 1-bit color modes", args.argHelp());
return 1;
if(params.getBitsPerColor() == 1 || params.isBlack())
{
print_error("URF does not support black or 1-bit color modes", args.argHelp());
return 1;
}
if(mediaTypeOpt.isSet() && !isUrfMediaType(params.mediaType))
{
print_error("Invalid media type for URF", args.argHelp());
return 1;
}
}

std::ofstream ofs;
Expand Down
27 changes: 22 additions & 5 deletions utils/ppm2pwg_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

#include "ppm2pwg.h"
#include "argget.h"
#include "mediaposition.h"

#define HELPTEXT "Use \"-\" as filename for stdin/stdout."

inline void print_error(std::string hint, std::string argHelp)
{
std::cerr << hint << std::endl << std::endl << argHelp << std::endl << HELPTEXT << std::endl;
}

inline void ignore_comments(std::istream* in)
{
Expand Down Expand Up @@ -44,7 +52,7 @@ int PPM2PWG_MAIN(int argc, char** argv)
SwitchArg<bool> verboseOpt(verbose, {"-v", "--verbose"}, "Be verbose, print headers");
SwitchArg<bool> urfOpt(urf, {"-u", "--urf"}, "Output URF format (default is PWG)");
SwitchArg<int> pagesOpt(pages, {"--num-pages"}, "Number of pages to expect (for URF header)");
SwitchArg<std::string> paperSizeOpt(params.paperSizeName, {"--paper-size"}, "Paper size name to set in header, e.g.: iso_a4_210x297mm");
SwitchArg<std::string> paperSizeOpt(params.paperSizeName, {"--paper-size"}, "Paper size name in header, e.g.: iso_a4_210x297mm");
SwitchArg<int> resolutionOpt(hwRes, {"-r", "--resolution"}, "Resolution (in DPI) to set in header");
SwitchArg<int> resolutionXOpt(hwResX, {"-rx", "--resolution-x"}, "Resolution (in DPI) to set in header, x-axis");
SwitchArg<int> resolutionYOpt(hwResY, {"-ry", "--resolution-y"}, "Resolution (in DPI) to set in header, y-axis");
Expand All @@ -62,30 +70,39 @@ int PPM2PWG_MAIN(int argc, char** argv)
{"high", PrintParameters::HighQuality}},
{"-q", "--quality"},
"Quality setting in raster header (draft/normal/high)");
EnumSwitchArg<PrintParameters::MediaPosition> mediaPositionOpt(params.mediaPosition, MEDIA_POSITION_MAP,
{"-mp", "--media-pos"},
"Media position, e.g.: main, top, left, roll-2 etc.");
SwitchArg<std::string> mediaTypeOpt(params.mediaType, {"-mt", "--media-type"}, "Media type, e.g.: stationery, cardstock etc.");

PosArg inArg(inFile, "in-file");
PosArg outArg(outFile, "out-file");

ArgGet args({&helpOpt, &verboseOpt, &urfOpt, &pagesOpt, &paperSizeOpt,
&resolutionOpt, &resolutionXOpt, &resolutionYOpt,
&duplexOpt, &tumbleOpt, &backXformOpt, &qualityOpt},
&duplexOpt, &tumbleOpt, &backXformOpt, &qualityOpt,
&mediaPositionOpt, &mediaTypeOpt},
{&inArg, &outArg});

bool correctArgs = args.get_args(argc, argv);
if(help)
{
std::cout << args.argHelp() << std::endl
<< "Use \"-\" as filename for stdin/stdout" << std::endl;
std::cout << args.argHelp() << std::endl << HELPTEXT << std::endl;
return 0;
}
else if(!correctArgs)
{
std::cerr << args.errmsg() << std::endl << std::endl << args.argHelp() << std::endl;
print_error(args.errmsg(), args.argHelp());
return 1;
}

if(urf)
{
if(mediaTypeOpt.isSet() && !isUrfMediaType(params.mediaType))
{
print_error("Invalid media type for URF", args.argHelp());
return 1;
}
params.format = PrintParameters::URF;
}
else
Expand Down

0 comments on commit ca6a9e4

Please sign in to comment.