1
0
mirror of https://github.com/pdewacht/brlaser synced 2025-04-07 05:06:45 +02:00
brlaser/src/block.h
Peter De Wachter 779f71e80f Reduce max block size to 64 lines (#40)
This fixes HL-1210W, maybe other HL printers as well?
2019-03-25 20:57:51 +01:00

69 lines
1.7 KiB
C++

// 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 <http://www.gnu.org/licenses/>.
#ifndef BLOCK_H
#define BLOCK_H
#include <assert.h>
#include <stdio.h>
#include <vector>
class block {
public:
block(): line_bytes_(0) {
lines_.reserve(max_lines_per_block_);
}
bool empty() const {
return line_bytes_ == 0;
}
void add_line(std::vector<uint8_t> &&line) {
assert(!line.empty());
assert(line_fits(line.size()));
line_bytes_ += line.size();
lines_.emplace_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 (!empty()) {
fprintf(f, "%dw%c%c",
line_bytes_ + 2, 0,
static_cast<int>(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_ = 64;
std::vector<std::vector<uint8_t>> lines_;
int line_bytes_;
};
#endif // BLOCK_H