-
Notifications
You must be signed in to change notification settings - Fork 3
/
subtitle.h
153 lines (125 loc) · 3.84 KB
/
subtitle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once
#ifdef WIN32
#pragma warning( push )
#pragma warning( disable : 26495) // uninitialized variable
#endif
#include <boost/function.hpp>
#ifdef WIN32
#pragma warning( pop )
#endif
#include <glm/glm.hpp>
#include <map>
#include "result.h"
namespace subtitle
{
typedef boost::function<Result (const std::string& text, const std::string& fontName, uint32_t fontSize, float& w, float& h)> GetTextSizeCb;
enum BorderStyle
{
OUTLINE,
OPAQUE
};
enum Alignment
{
LEFT,
CENTERED,
RIGHT,
LEFT_TOPTITLE,
CENTERED_TOPTITLE,
RIGHT_TOPTITLE,
LEFT_MIDDLETITLE,
RIGHT_MIDDLETITLE,
CENTERED_MIDDLETITLE
};
struct Line
{
std::string text;
float x = 0.0f;
float y = 0.0f;
float w = 0.0f;
float h = 0.0f;
};
typedef std::vector<Line> SubLineList;
// https://www.matroska.org/technical/specs/subtitles/ssa.html
struct SubStationAlphaStyle
{
// style
std::string name;
std::string fontName = "Arial";
uint32_t fontSize = 48;
// rgba colors
glm::vec4 primaryColor = {1.0f, 1.0f, 1.0f, 1.0f};
glm::vec4 secondaryColor = {1.0f, 1.0f, 1.0f, 1.0f};
glm::vec4 outlineColor {1.0f, 1.0f, 1.0f, 1.0f};
glm::vec4 backColor = {1.0f, 1.0f, 1.0f, 1.0f};
bool bold = false;
bool italic = false;
bool underline = false;
bool strikeout = false;
uint32_t scaleXPercent = 100;
uint32_t scaleYPercent = 100;
uint32_t pixelSpacing = 0;
uint32_t degreAngle = 0;
BorderStyle borderStyle = OUTLINE;
uint32_t outline = 0;
uint32_t shadow = 0;
Alignment alignment = CENTERED;
uint32_t marginL = 10;
uint32_t marginR = 10;
uint32_t marginV = 10;
float alphaLevel = 1.0f;
std::string encoding;
};
struct SubStationAlphaHeader
{
// scrypt info
uint32_t playResX = 0;
uint32_t playResY = 0;
std::map<std::string, SubStationAlphaStyle> styles;
std::map<std::string, uint32_t> styleFormatFieldPos;
std::map<std::string, uint32_t> eventFormatFieldPos;
};
struct SubStationAlphaDialogue
{
std::string layer;
std::string text;
uint64_t startTimeUs = 0;
uint64_t endTimeUs = 0;
std::string style;
std::string name;
uint32_t marginL = 0;
uint32_t marginR = 0;
uint32_t marginV = 0;
std::string effect;
};
typedef std::vector<Line> SubStationAlphaLineList;
// .srt files https://en.wikipedia.org/wiki/SubRip
struct SubRipDialogue
{
uint32_t counter = 0;
uint64_t startTimeUs = 0;
uint64_t endTimeUs = 0;
SubLineList lines;
};
typedef std::vector<SubRipDialogue> SubRipDialogueList;
struct SubRip
{
SubRipDialogueList diags;
};
// ssa / ass
Result Parse(const std::string& ssa, SubStationAlphaHeader*& header);
Result Parse(const std::string& ssa, SubStationAlphaHeader* header, SubStationAlphaDialogue*& dialogue);
// src
Result Parse(const std::string& path, SubRip*& subRip);
Result GetDisplayInfo(const std::vector<std::string>& lines,
GetTextSizeCb& textSizeCb,
uint32_t windowWidth,
uint32_t windowHeight,
SubStationAlphaHeader* header,
SubStationAlphaDialogue* dialogue,
uint64_t& startTimeUs,
uint64_t& endTimeUs,
std::string& fontName,
uint32_t& fontSize,
glm::vec3& color,
SubLineList& sublines);
};