diff --git a/Makefile.am b/Makefile.am index 42198e0..d21d560 100644 --- a/Makefile.am +++ b/Makefile.am @@ -17,6 +17,7 @@ rastertobrlaser_SOURCES = \ src/debug.cc \ src/job.h \ src/job.cc \ + src/block.h \ src/line.h \ src/line.cc rastertobrlaser_CPPFLAGS = $(CUPS_CFLAGS) diff --git a/src/block.h b/src/block.h new file mode 100644 index 0000000..cd3a48e --- /dev/null +++ b/src/block.h @@ -0,0 +1,63 @@ +// This file is part of the brlaser printer driver. +// +// Copyright 2013 Peter De Wachter +// +// brlaser is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 2 of the License, or +// (at your option) any later version. +// +// brlaser 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with brlaser. If not, see . + +#ifndef BLOCK_H +#define BLOCK_H + +#include +#include +#include + +class block { + public: + block(): line_bytes_(0) { + } + + void add_line(std::vector &&line) { + assert(!line.empty()); + assert(line_fits(line.size())); + line_bytes_ += line.size(); + lines_.push_back(line); + } + + bool line_fits(unsigned size) { + return lines_.size() != max_lines_per_block_ + && line_bytes_ + size < max_block_size_; + } + + void flush(FILE *f) { + if (line_bytes_) { + fprintf(f, "%dw%c%c", + line_bytes_ + 2, 0, + static_cast(lines_.size())); + for (auto &line : lines_) { + fwrite(line.data(), 1, line.size(), f); + } + line_bytes_ = 0; + lines_.clear(); + } + } + + private: + static const unsigned max_block_size_ = 16350; + static const unsigned max_lines_per_block_ = 128; + + std::vector> lines_; + int line_bytes_; +}; + +#endif // BLOCK_H diff --git a/src/job.cc b/src/job.cc index 535d60d..cc8e201 100644 --- a/src/job.cc +++ b/src/job.cc @@ -20,49 +20,8 @@ #include #include #include "line.h" +#include "block.h" -using std::vector; - -namespace { - -class block { - public: - block(): line_bytes_(0) { - } - - void add_line(vector &&line) { - assert(!line.empty()); - line_bytes_ += line.size(); - lines_.push_back(line); - } - - bool line_fits(unsigned size) { - return lines_.size() != max_lines_per_block_ - && line_bytes_ + size < max_block_size_; - } - - void flush(FILE *f) { - if (line_bytes_) { - fprintf(f, "%dw%c%c", - line_bytes_ + 2, 0, - static_cast(lines_.size())); - for (auto &line : lines_) { - fwrite(line.data(), 1, line.size(), f); - } - line_bytes_ = 0; - lines_.clear(); - } - } - - private: - static const unsigned max_block_size_ = 16350; - static const unsigned max_lines_per_block_ = 128; - - vector> lines_; - int line_bytes_; -}; - -} // namespace job::job(FILE *out, const std::string &job_name) : out_(out), @@ -126,8 +85,8 @@ void job::encode_page(const page_params &page_params, write_page_header(); } - vector line(linesize); - vector reference(linesize); + std::vector line(linesize); + std::vector reference(linesize); block block; if (!nextline(line.data())) { @@ -141,7 +100,7 @@ void job::encode_page(const page_params &page_params, fputs("\033*b1030m", out_); for (int i = 1; i < lines && nextline(line.data()); ++i) { - vector encoded = encode_line(line, reference); + std::vector encoded = encode_line(line, reference); if (!block.line_fits(encoded.size())) { block.flush(out_); }