From eef23108ceb07b56cde9f0393aeaca5b429444e6 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Fri, 20 Jan 2023 23:53:45 +0100 Subject: [PATCH] Add bounding_box(). --- src/lib/render_text.cc | 30 ++++++++++++++++++++++++++++++ src/lib/render_text.h | 4 +++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/lib/render_text.cc b/src/lib/render_text.cc index 0b6a30e80..0e2b024d6 100644 --- a/src/lib/render_text.cc +++ b/src/lib/render_text.cc @@ -427,6 +427,36 @@ render_text (list subtitles, dcp::Size target, DCPTime time, int fra } +list> +bounding_box(list subtitles, dcp::Size target) +{ + list pending; + list> rects; + + auto use_pending = [&pending, &rects, target]() { + /* We can provide dummy values for time and frame rate here as they are only used to calculate fades */ + auto layout = setup_layout(pending, target, DCPTime(), 24); + int const x = x_position(pending.front(), target.width, layout.size.width); + int const y = y_position(pending.front(), target.height, layout.position.y, layout.size.height); + rects.push_back({Position(x, y), layout.size.width, layout.size.height}); + }; + + for (auto const& i: subtitles) { + if (!pending.empty() && (i.v_align() != pending.back().v_align() || fabs(i.v_position() - pending.back().v_position()) > 1e-4)) { + use_pending(); + pending.clear(); + } + pending.push_back(i); + } + + if (!pending.empty()) { + use_pending(); + } + + return rects; +} + + float FontMetrics::height(StringText const& subtitle) { diff --git a/src/lib/render_text.h b/src/lib/render_text.h index 762d79446..bceee964e 100644 --- a/src/lib/render_text.h +++ b/src/lib/render_text.h @@ -19,8 +19,9 @@ */ -#include "position_image.h" #include "dcpomatic_time.h" +#include "position_image.h" +#include "rect.h" #include "string_text.h" #include #include @@ -33,6 +34,7 @@ namespace dcpomatic { std::string marked_up (std::list subtitles, int target_height, float fade_factor, std::string font_name); std::list render_text (std::list, dcp::Size, dcpomatic::DCPTime, int); +std::list> bounding_box(std::list subtitles, dcp::Size target); class FontMetrics -- 2.30.2