Skip to content

Commit ef60c3a

Browse files
committed
CPS customizable
1 parent 6b4a340 commit ef60c3a

File tree

5 files changed

+59
-16
lines changed

5 files changed

+59
-16
lines changed

src/grid_column.cpp

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ class GridColumnCPS final : public GridColumn {
289289
const agi::OptionValue *cps_warn = OPT_GET("Subtitle/Character Counter/CPS Warning Threshold");
290290
const agi::OptionValue *cps_error = OPT_GET("Subtitle/Character Counter/CPS Error Threshold");
291291
const agi::OptionValue *bg_color = OPT_GET("Colour/Subtitle Grid/CPS Error");
292+
const agi::OptionValue *disp_fmt = OPT_GET("Subtitle/Character Counter/Display Format");
293+
const agi::OptionValue *col_align = OPT_GET("Subtitle/Character Counter/Column Alignment");
292294

293295
public:
294296
COLUMN_HEADER(_("CPS"))
@@ -300,7 +302,7 @@ class GridColumnCPS final : public GridColumn {
300302
return wxS("");
301303
}
302304

303-
int CPS(const AssDialogue *d) const {
305+
double CPS(const AssDialogue *d) const {
304306
int duration = d->End - d->Start;
305307
auto const& text = d->Text.get();
306308

@@ -313,32 +315,60 @@ class GridColumnCPS final : public GridColumn {
313315
if (ignore_punctuation->GetBool())
314316
ignore |= agi::IGNORE_PUNCTUATION;
315317

316-
return agi::CharacterCount(text, ignore) * 1000 / duration;
318+
return agi::CharacterCount(text, ignore) * 1000. / duration;
317319
}
318320

319321
int Width(const agi::Context *c, WidthHelper &helper) const override {
320-
return helper(wxS("999"));
322+
return helper(wxS("99.9"));
321323
}
322324

323325
void Paint(wxDC &dc, int x, int y, const AssDialogue *d, const agi::Context *) const override {
324-
int cps = CPS(d);
325-
if (cps < 0 || cps > 100) return;
326+
double cps = CPS(d);
327+
if (cps < 0) return;
328+
329+
double round_cps;
330+
wxString str;
331+
if (disp_fmt->GetInt() == 0 || (disp_fmt->GetInt() == 2 && cps >= 10)) {
332+
round_cps = std::lround(cps);
333+
if (round_cps >= 100) return;
334+
str = std::to_wstring(int(round_cps));
335+
} else {
336+
round_cps = std::round(cps*10) / 10.0;
337+
if (round_cps >= 100) return;
338+
if (round_cps >= 10)
339+
str = std::to_wstring(round_cps).substr(0, 4);
340+
else
341+
str = std::to_wstring(round_cps).substr(0, 3);
342+
}
343+
344+
wxSize ext;
345+
int w0 = dc.GetTextExtent(wxString(L"0")).GetWidth();
346+
if (col_align->GetInt() == 0 || (disp_fmt->GetInt() != 2 && round_cps >= 10))
347+
ext = dc.GetTextExtent(str);
348+
else {
349+
wxString str0;
350+
if (cps >= 10)
351+
str0 = str + wxString(L".0");
352+
else
353+
str0 = wxString(L"0") + str;
354+
ext = dc.GetTextExtent(str0);
355+
}
326356

327-
wxString str = std::to_wstring(cps);
328-
wxSize ext = dc.GetTextExtent(str);
329357
auto tc = dc.GetTextForeground();
330358

331-
int cps_min = cps_warn->GetInt();
332-
int cps_max = std::max<int>(cps_min, cps_error->GetInt());
359+
double cps_min = cps_warn->GetDouble();
360+
double cps_max = std::max<float>(cps_min, cps_error->GetDouble());
333361
if (cps > cps_min) {
334-
double alpha = std::min((double)(cps - cps_min + 1) / (cps_max - cps_min + 1), 1.0);
362+
double alpha = std::min((cps - cps_min + 1) / (cps_max - cps_min + 1), 1.0);
335363
dc.SetBrush(wxBrush(blend(to_wx(bg_color->GetColor()), dc.GetBrush().GetColour(), alpha)));
336364
dc.SetPen(*wxTRANSPARENT_PEN);
337365
dc.DrawRectangle(x, y + 1, width, ext.GetHeight() + 3);
338366
dc.SetTextForeground(blend(*wxBLACK, tc, alpha));
339367
}
340368

341369
x += (width + 2 - ext.GetWidth()) / 2;
370+
if (col_align->GetInt() == 1 && round_cps < 10)
371+
x += w0;
342372
dc.DrawText(str, x, y + 2);
343373
dc.SetTextForeground(tc);
344374
}

src/grid_column.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "flyweight_hash.h"
1818

19+
#include <cmath>
1920
#include <memory>
2021
#include <string>
2122
#include <vector>

src/libresrc/default_config.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,10 @@
361361
"Character Counter" : {
362362
"Ignore Whitespace" : true,
363363
"Ignore Punctuation" : true,
364-
"CPS Warning Threshold" : 15,
365-
"CPS Error Threshold" : 30
364+
"CPS Warning Threshold" : 15.0,
365+
"CPS Error Threshold" : 30.0,
366+
"Display Format" : 0,
367+
"Column Alignment": 0
366368
},
367369
"Character Limit" : 40,
368370
"Default Resolution" : {

src/libresrc/osx/default_config.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,10 @@
361361
"Character Counter" : {
362362
"Ignore Whitespace" : true,
363363
"Ignore Punctuation" : true,
364-
"CPS Warning Threshold" : 15,
365-
"CPS Error Threshold" : 30
364+
"CPS Warning Threshold" : 15.0,
365+
"CPS Error Threshold" : 30.0,
366+
"Display Format" : 0,
367+
"Column Alignment": 0
366368
},
367369
"Character Limit" : 40,
368370
"Default Resolution" : {

src/preferences.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,19 @@ void Interface(wxTreebook *book, Preferences *parent) {
214214

215215
auto character_count = p->PageSizer(_("Character Counter"));
216216
p->OptionAdd(character_count, _("Maximum characters per line"), "Subtitle/Character Limit", 0, 1000);
217-
p->OptionAdd(character_count, _("Characters Per Second Warning Threshold"), "Subtitle/Character Counter/CPS Warning Threshold", 0, 1000);
218-
p->OptionAdd(character_count, _("Characters Per Second Error Threshold"), "Subtitle/Character Counter/CPS Error Threshold", 0, 1000);
217+
p->OptionAdd(character_count, _("Characters Per Second Warning Threshold"), "Subtitle/Character Counter/CPS Warning Threshold", 0.1, 1000., 0.1);
218+
p->OptionAdd(character_count, _("Characters Per Second Error Threshold"), "Subtitle/Character Counter/CPS Error Threshold", 0.1, 1000., 0.1);
219219
p->OptionAdd(character_count, _("Ignore whitespace"), "Subtitle/Character Counter/Ignore Whitespace");
220220
p->OptionAdd(character_count, _("Ignore punctuation"), "Subtitle/Character Counter/Ignore Punctuation");
221221

222+
const wxString ccpsf_arr[3] = {_("Nearest integer"), _("Nearest 0.1"), _("2 sig figs")};
223+
wxArrayString cpsf_res(3, ccpsf_arr);
224+
p->OptionChoice(character_count, _("CPS display format"), cpsf_res, "Subtitle/Character Counter/Display Format");
225+
226+
const wxString calign_arr[2] = {_("Center"), _("Center with virtual 0")};
227+
wxArrayString calign_res(2, calign_arr);
228+
p->OptionChoice(character_count, _("CPS column alignment"), calign_res, "Subtitle/Character Counter/Column Alignment");
229+
222230
auto grid = p->PageSizer(_("Grid"));
223231
p->OptionAdd(grid, _("Focus grid on click"), "Subtitle/Grid/Focus Allow");
224232
p->OptionAdd(grid, _("Highlight visible subtitles"), "Subtitle/Grid/Highlight Subtitles in Frame");

0 commit comments

Comments
 (0)