forked from iizukanao/picam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtitle.c
172 lines (149 loc) · 3.93 KB
/
subtitle.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include "text.h"
#include "subtitle.h"
const static char *default_font_name = "sans";
static int text_id = -1;
static int64_t hide_time = 0;
/**
* Initializes the timestamp library with a font name.
*/
void subtitle_init_with_font_name(const char *font_name, int points, int dpi) {
char *font_file;
int face_index;
if (font_name != NULL) {
text_select_font_file(font_name, &font_file, &face_index);
} else {
text_select_font_file(default_font_name, &font_file, &face_index);
}
subtitle_init(font_file, face_index, points, dpi);
free(font_file);
}
/**
* Initializes the timestamp library with a font file and face index.
*/
void subtitle_init(const char *font_file, long face_index, int points, int dpi) {
if (text_id != -1) {
text_destroy(text_id);
}
text_id = text_create(
font_file, face_index,
points,
dpi
);
text_set_stroke_color(text_id, 0x000000);
text_set_letter_spacing(text_id, 1);
text_set_color(text_id, 0xffffff);
text_set_layout(text_id,
LAYOUT_ALIGN_BOTTOM | LAYOUT_ALIGN_CENTER, // layout alignment for the box
0, // horizontal margin from the right edge
30); // vertical margin from the bottom edge
text_set_align(text_id, TEXT_ALIGN_CENTER); // text alignment inside the box
}
/**
* Destroys the resources used by timestamp library.
*/
void subtitle_shutdown() {
if (text_id != -1) {
text_destroy(text_id);
text_id = -1;
}
}
/**
* Sets text color.
*/
void subtitle_set_color(int color) {
text_set_color(text_id, color);
}
/**
* Sets text stroke color.
*/
void subtitle_set_stroke_color(uint32_t color) {
text_set_stroke_color(text_id, color);
}
/**
* Sets text stroke border width in points.
*/
void subtitle_set_stroke_width(float stroke_width) {
text_set_stroke_width(text_id, stroke_width);
}
/**
* Sets letter spacing in pixels.
*/
void subtitle_set_letter_spacing(int letter_spacing) {
text_set_letter_spacing(text_id, letter_spacing);
}
/**
* Sets multiplying factor for line spacing.
* If this is set to 1, default line spacing is used.
*/
void subtitle_set_line_height_multiply(float multiply) {
text_set_line_height_multiply(text_id, multiply);
}
/**
* Sets the scale of a tab (\t) character.
* Tab width will be multiplied by the given number.
*/
void subtitle_set_tab_scale(float multiply) {
text_set_tab_scale(text_id, multiply);
}
/**
* Sets the absolute position for the timestamp.
*/
void subtitle_set_position(int x, int y) {
text_set_position(text_id, x, y);
}
/**
* Sets the relative layout for the text in the screen.
*/
void subtitle_set_layout(LAYOUT_ALIGN layout_align, int horizontal_margin, int vertical_margin) {
text_set_layout(text_id, layout_align, horizontal_margin, vertical_margin);
}
/**
* Sets the text alignment inside a positioned box.
*/
void subtitle_set_align(TEXT_ALIGN text_align) {
text_set_align(text_id, text_align);
}
/**
* Call this function before calling text_draw_all().
*/
void subtitle_update() {
struct timespec ts;
if (hide_time > 0) {
clock_gettime(CLOCK_MONOTONIC, &ts);
int64_t current_time = ts.tv_sec * INT64_C(1000000000) + ts.tv_nsec;
if (current_time > hide_time) {
text_clear(text_id);
hide_time = 0;
}
}
}
/**
* Show subtitle text for duration_sec.
* When duration_sec is 0, the text will be displayed indefinitely.
*/
void subtitle_show(const char *text, size_t text_len, float duration_sec) {
struct timespec ts;
text_set_text(text_id, text, text_len);
redraw_text(text_id);
if (duration_sec > 0.0f) {
// hide the text after duration_sec
clock_gettime(CLOCK_MONOTONIC, &ts);
hide_time = ts.tv_sec * INT64_C(1000000000) +
ts.tv_nsec + duration_sec * INT64_C(1000000000);
} else {
// show the text indefinitely
hide_time = 0;
}
}
/**
* Hide the subtitle.
*/
void subtitle_clear() {
if (text_id != -1) {
text_clear(text_id);
}
}