Skip to content

Commit

Permalink
Clean up PrintParameters a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Nov 9, 2024
1 parent e646136 commit 88f0276
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
8 changes: 4 additions & 4 deletions lib/pdf2printable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Error pdf_to_printable(std::string inFile, WriteFun writeFun, const PrintParamet
}

size_t pages = poppler_document_get_n_pages(doc);
PageSequence seq = params.getPageSequence(pages);
PageSequence pageSequence = params.getPageSequence(pages);

size_t outPageNo = 0;

Expand All @@ -108,7 +108,7 @@ Error pdf_to_printable(std::string inFile, WriteFun writeFun, const PrintParamet
params.getPaperSizeHInPixels());
if(params.format == PrintParameters::URF)
{
outBts = make_urf_file_hdr(seq.size());
outBts = make_urf_file_hdr(pageSequence.size());
}
else
{
Expand Down Expand Up @@ -140,7 +140,7 @@ Error pdf_to_printable(std::string inFile, WriteFun writeFun, const PrintParamet
return Error("Unknown format");
}

for(size_t pageNo : seq)
for(size_t pageNo : pageSequence)
{
outPageNo++;

Expand Down Expand Up @@ -204,7 +204,7 @@ Error pdf_to_printable(std::string inFile, WriteFun writeFun, const PrintParamet
CHECK(writeFun(std::move(outBts)));
outBts.reset();

progressFun(outPageNo, seq.size());
progressFun(outPageNo, pageSequence.size());
}

cairo_surface_finish(surface);
Expand Down
26 changes: 14 additions & 12 deletions lib/printparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "stringutils.h"

#include <charconv>
#include <regex>

#define MM_PER_IN 25.4
Expand Down Expand Up @@ -143,7 +144,7 @@ PageSequence PrintParameters::getPageSequence(size_t pages) const
PageSequence copy = seq;
for(size_t dc = copies; dc > 1; dc--)
{
seq.insert(seq.cend(), copy.cbegin(), copy.cend());
seq += copy;
}
}
else
Expand All @@ -153,18 +154,20 @@ PageSequence PrintParameters::getPageSequence(size_t pages) const
{
if(isTwoSided())
{
size_t front = *it;
size_t back = *(++it);
for(size_t pc = copies; pc > 0; pc--)
{
copy.push_back(*it);
copy.push_back(*(it+1));
copy.push_back(front);
copy.push_back(back);
}
it++;
}
else
{
size_t page = *it;
for(size_t pc = copies; pc > 0; pc--)
{
copy.push_back(*it);
copy.push_back(page);
}
}
}
Expand All @@ -187,7 +190,7 @@ PageRangeList PrintParameters::parsePageRange(const std::string& rangesStr)
{
if(std::regex_match(rangeStr, match, single))
{
size_t singleValue = stol(match[1]);
size_t singleValue = stoul(match[1]);
if(singleValue <= prevMax)
{
return {};
Expand All @@ -197,8 +200,8 @@ PageRangeList PrintParameters::parsePageRange(const std::string& rangesStr)
}
else if(std::regex_match(rangeStr, match, range))
{
size_t from = stol(match[1]);
size_t to = match[2] != "" ? stol(match[2]) : std::numeric_limits<size_t>::max();
size_t from = stoul(match[1]);
size_t to = match[2] != "" ? stoul(match[2]) : std::numeric_limits<size_t>::max();
if(to < from || from <= prevMax)
{
return {};
Expand Down Expand Up @@ -228,15 +231,14 @@ bool PrintParameters::setPageRange(const std::string& rangeStr)

bool PrintParameters::setPaperSize(const std::string& sizeStr)
{
const std::regex nameRegex("^[0-9a-z_-]+_(([0-9]+[.])?[0-9]+)x(([0-9]+[.])?[0-9]+)(mm|in)$");
const std::regex nameRegex("^[0-9a-z_-]+_([0-9]+([.][0-9]+)?)x([0-9]+([.][0-9]+)?)(mm|in)$");
std::cmatch match;
locale_t c_locale = newlocale(LC_ALL_MASK, "C", nullptr);

if(std::regex_match(sizeStr.c_str(), match, nameRegex))
{
paperSizeName = sizeStr;
paperSizeW = strtod_l(match[1].first, nullptr, c_locale);
paperSizeH = strtod_l(match[3].first, nullptr, c_locale);
std::from_chars(match[1].first, match[1].second, paperSizeW);
std::from_chars(match[3].first, match[3].second, paperSizeH);
if(match[5] == "in")
{
paperSizeUnits = Inches;
Expand Down
7 changes: 4 additions & 3 deletions lib/printparameters.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#ifndef PRINTPARAMETERS_H
#define PRINTPARAMETERS_H

#include "list.h"

#include <cmath>
#include <cstdint>
#include <string>
#include <vector>

#define INVALID_PAGE 0

typedef std::vector<size_t> PageSequence;
typedef std::vector<std::pair<size_t, size_t>> PageRangeList;
typedef List<size_t> PageSequence;
typedef List<std::pair<size_t, size_t>> PageRangeList;

class PrintParameters
{
Expand Down

0 comments on commit 88f0276

Please sign in to comment.