From 9f3e7a43d57221208f39766e3f62fad536028f5e Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 14 Apr 2022 16:54:18 +0800 Subject: Add text_policy for bitmap::print, supporting auto newline. --- bitmap.h | 26 +++++++++++++++++++++----- main_simple_scanner.cpp | 10 +++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/bitmap.h b/bitmap.h index c955803..64f69a6 100644 --- a/bitmap.h +++ b/bitmap.h @@ -16,6 +16,11 @@ #define COLORMIX_OVERFLOW_CHECK +enum text_policy { + hard = 0, // cut off overflown parts + newline = 1, // write overflown characters to new line +}; + //// T is some unsigned integer //template //T saturate_add(T a, T b) { @@ -219,7 +224,7 @@ public: // Draw text on the image. Supports limited visual ASCII characters. void print(const std::string &s, const pixel &color, - unsigned x, unsigned y, unsigned scale = 1, double alpha = 1.0); + unsigned x, unsigned y, text_policy policy, unsigned scale = 1, double alpha = 1.0); bool normalized() const { return false; @@ -269,17 +274,28 @@ void bitmap::write_plain_ppm(std::ostream &out) const { template void bitmap::print(const std::string &s, const pixel &color, - unsigned x, unsigned y, unsigned scale, double alpha) { + unsigned x, unsigned y, text_policy policy, unsigned scale, double alpha) { assert(alpha >= 0); assert(alpha <= 1); const unsigned char_w = 8 * scale, char_h = 13 * scale; // char width and height - size_t n = 0; + size_t n = 0; // written characters for (const auto &c: s) { unsigned int idx = c - 32U; if (idx >= 95) idx = 1; // replace invisible chars with '!' - const unsigned char_x = x + n * char_w + ((n > 1) ? ((n - 1) * scale) : 0); - const unsigned char_y = y; + unsigned char_x, char_y; + const unsigned spacing = ((n > 1) ? ((n - 1) * scale) : 0); // total spacing between characters + if (policy == text_policy::hard) { + char_x = x + n * char_w + spacing; + char_y = y; + } else if (policy == text_policy::newline) { + const auto newlines = (n * char_w + spacing + char_w - 1) / (width - x); + char_x = (x + n * char_w + spacing) % (width - x); + char_y = y + newlines * char_h + ((newlines > 0) ? ((newlines - 1) * scale) : 0); + } else { + abort(); // unknown policy + } + // char size is 13x8, stored line by line, from the bottom line to the top line // every line is represented in a single byte diff --git a/main_simple_scanner.cpp b/main_simple_scanner.cpp index 91c036e..67a41d9 100644 --- a/main_simple_scanner.cpp +++ b/main_simple_scanner.cpp @@ -53,7 +53,7 @@ void generate_image(uint16_t image_width, uint16_t image_height, double viewport if (!caption.empty()) { image.print(caption, pixel::from_normalized(1.0, 0.0, 0.0), - 10, 10, caption_scale, 0.8); + 10, 10, text_policy::newline, caption_scale, 0.8); } const auto image8b = bitmap8b::from(image); if (!std::getenv("NOPRINT")) { @@ -81,8 +81,8 @@ int main(int argc, char **argv) { } const auto image_width = std::stoul(iw); generate_image(image_width, std::stoul(ih), - std::stod(vw), std::stod(fl), - std::stod(sz), std::stod(sr), - std::stoul(sp), cap, - std::max((int) (1.0 * image_width * 0.015 / 8), 1)); + std::stod(vw), std::stod(fl), + std::stod(sz), std::stod(sr), + std::stoul(sp), cap, + std::max((int) (1.0 * image_width * 0.015 / 8), 1)); } \ No newline at end of file -- cgit v1.2.3