desktop: use std::string to format subtitles

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-09 06:32:08 +02:00 committed by bstoeger
parent d05e289507
commit 6252d22adf
3 changed files with 33 additions and 30 deletions

View file

@ -5,6 +5,7 @@
#include "core/profile.h"
#include "core/errorhelper.h"
#include "core/file.h"
#include "core/format.h"
#include "core/membuffer.h"
#include "core/subsurface-string.h"
#include "core/version.h"
@ -35,13 +36,13 @@ static void put_double(struct membuffer *b, double val)
put_format(b, "\"%f\", ", val);
}
static void put_video_time(struct membuffer *b, int secs)
static std::string video_time(int secs)
{
int hours = secs / 3600;
secs -= hours * 3600;
int mins = secs / 60;
secs -= mins * 60;
put_format(b, "%d:%02d:%02d.000,", hours, mins, secs);
return format_string_std("%d:%02d:%02d.000,", hours, mins, secs);
}
static void put_pd(struct membuffer *b, const struct plot_info &pi, int idx)
@ -169,38 +170,39 @@ static void put_headers(struct membuffer *b, int nr_cylinders)
put_csv_string_with_nl(b, "icd_warning");
}
static void put_st_event(struct membuffer *b, const plot_data &entry, const plot_data &next_entry, int offset, int length)
static std::string format_st_event(const plot_data &entry, const plot_data &next_entry, int offset, int length)
{
double value;
int decimals;
const char *unit;
if (entry.sec < offset || entry.sec > offset + length)
return;
return {};
put_format(b, "Dialogue: 0,");
put_video_time(b, entry.sec - offset);
put_video_time(b, next_entry.sec - offset < length ? next_entry.sec - offset : length);
put_format(b, "Default,,0,0,0,,");
put_format(b, "%d:%02d ", FRACTION_TUPLE(entry.sec, 60));
std::string res = "Dialogue: 0,";
res += video_time(entry.sec - offset);
res += video_time(next_entry.sec - offset < length ? next_entry.sec - offset : length);
res += "Default,,0,0,0,,";
res += format_string_std("%d:%02d ", FRACTION_TUPLE(entry.sec, 60));
value = get_depth_units(entry.depth, &decimals, &unit);
put_format(b, "D=%02.2f %s ", value, unit);
res += format_string_std("D=%02.2f %s ", value, unit);
if (entry.temperature) {
value = get_temp_units(entry.temperature, &unit);
put_format(b, "T=%.1f%s ", value, unit);
res += format_string_std("T=%.1f%s ", value, unit);
}
// Only show NDL if it is not essentially infinite, show TTS for mandatory stops.
if (entry.ndl_calc < 3600) {
if (entry.ndl_calc > 0)
put_format(b, "NDL=%d:%02d ", FRACTION_TUPLE(entry.ndl_calc, 60));
res += format_string_std("NDL=%d:%02d ", FRACTION_TUPLE(entry.ndl_calc, 60));
else
if (entry.tts_calc > 0)
put_format(b, "TTS=%d:%02d ", FRACTION_TUPLE(entry.tts_calc, 60));
res += format_string_std("TTS=%d:%02d ", FRACTION_TUPLE(entry.tts_calc, 60));
}
if (entry.surface_gf > 0.0) {
put_format(b, "sGF=%.1f%% ", entry.surface_gf);
res += format_string_std("sGF=%.1f%% ", entry.surface_gf);
}
put_format(b, "\n");
res += "\n";
return res;
}
static void save_profiles_buffer(struct membuffer *b, bool select_only)
@ -219,22 +221,24 @@ static void save_profiles_buffer(struct membuffer *b, bool select_only)
}
}
void save_subtitles_buffer(struct membuffer *b, struct dive *dive, int offset, int length)
std::string save_subtitles_buffer(struct dive *dive, int offset, int length)
{
struct deco_state *planner_deco_state = NULL;
plot_info pi = create_plot_info_new(dive, &dive->dcs[0], planner_deco_state);
put_format(b, "[Script Info]\n");
put_format(b, "; Script generated by Subsurface %s\n", subsurface_canonical_version());
put_format(b, "ScriptType: v4.00+\nPlayResX: 384\nPlayResY: 288\n\n");
put_format(b, "[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n");
put_format(b, "Style: Default,Arial,12,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,7,10,10,10,0\n\n");
put_format(b, "[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n");
std::string res;
res += "[Script Info]\n";
res += "; Script generated by Subsurface " + std::string(subsurface_canonical_version()) + "%s\n";
res += "ScriptType: v4.00+\nPlayResX: 384\nPlayResY: 288\n\n";
res += "[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n";
res += "Style: Default,Arial,12,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,7,10,10,10,0\n\n";
res += "[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n";
for (int i = 0; i + 1 < pi.nr; i++)
put_st_event(b, pi.entry[i], pi.entry[i + 1], offset, length);
put_format(b, "\n");
res += format_st_event(pi.entry[i], pi.entry[i + 1], offset, length);
res += "\n";
return res;
}
int save_profiledata(const char *filename, bool select_only)

View file

@ -2,7 +2,9 @@
#ifndef SAVE_PROFILE_DATA_H
#define SAVE_PROFILE_DATA_H
#include <string>
int save_profiledata(const char *filename, bool selected_only);
void save_subtitles_buffer(struct membuffer *b, struct dive *dive, int offset, int length);
std::string save_subtitles_buffer(struct dive *dive, int offset, int length);
#endif // SAVE_PROFILE_DATA_H

View file

@ -12,7 +12,6 @@
#include <QUrl>
#include <QMessageBox>
#include <QFileInfo>
#include "core/membuffer.h"
#include "core/save-profiledata.h"
#include "core/selection.h"
@ -131,11 +130,9 @@ void TabDivePhotos::saveSubtitles()
// Only videos have non-zero duration
if (!duration)
continue;
membuffer b;
save_subtitles_buffer(&b, parent.currentDive, offset, duration);
const char *data = mb_cstring(&b);
std::string buffer = save_subtitles_buffer(parent.currentDive, offset, duration);
subtitlefile.open(QIODevice::WriteOnly);
subtitlefile.write(data, strlen(data));
subtitlefile.write(buffer.c_str(), buffer.size());
subtitlefile.close();
}
}