Kaynağa Gözat

(#88) Add font filepath to video params

rexim 5 yıl önce
ebeveyn
işleme
16cfdf15ab

+ 0 - 8
src/vodus_emotes.cpp

@@ -102,14 +102,6 @@ String_View file_extension(String_View filename)
     return ext;
 }
 
-const char *string_view_as_cstr(String_View sv)
-{
-    char *cstr = (char *) malloc(sv.count + 1);
-    if (!cstr) return cstr;
-    memcpy(cstr, sv.data, sv.count);
-    cstr[sv.count] = '\0';
-    return cstr;
-}
 
 Emote load_gif_emote(String_View file_path, size_t size)
 {

+ 11 - 8
src/vodus_main.cpp

@@ -451,7 +451,6 @@ int main()
 int main_(int argc, char *argv[])
 {
     const char *log_filepath = nullptr;
-    const char *face_filepath = nullptr;
     const char *output_filepath = nullptr;
     size_t messages_limit = VODUS_MESSAGES_CAPACITY;
 
@@ -490,6 +489,13 @@ int main_(int argc, char *argv[])
             END_PARAMETER;                      \
         } while (0)
 
+#define SV_PARAMETER(variable)                      \
+        do {                                        \
+            BEGIN_PARAMETER(cstr);                  \
+            variable = cstr_as_string_view(cstr);   \
+            END_PARAMETER;                          \
+        } while(0)
+
 #define COLOR_PARAMETER(place)                                          \
         do {                                                            \
             BEGIN_PARAMETER(cstr);                                      \
@@ -507,7 +513,7 @@ int main_(int argc, char *argv[])
             usage(stdout);
             exit(0);
         } else if (arg == "--font"_sv) {
-            CSTR_PARAMETER(face_filepath);
+            SV_PARAMETER(params.font);
         } else if (arg == "--output"_sv || arg == "-o"_sv) {
             CSTR_PARAMETER(output_filepath);
         } else if (arg == "--limit"_sv) {
@@ -558,12 +564,6 @@ int main_(int argc, char *argv[])
         exit(1);
     }
 
-    if (face_filepath == nullptr) {
-        println(stderr, "Error: Font was not provided. Please use `--font` flag.");
-        usage(stderr);
-        exit(1);
-    }
-
     FT_Library library;
     auto error = FT_Init_FreeType(&library);
     if (error) {
@@ -571,6 +571,9 @@ int main_(int argc, char *argv[])
         exit(1);
     }
 
+    const char *face_filepath = string_view_as_cstr(params.font);
+    defer(free((void*) face_filepath));
+
     FT_Face face;
     error = FT_New_Face(library,
                         face_filepath,

+ 13 - 0
src/vodus_video_params.cpp

@@ -1,5 +1,14 @@
 #include "./vodus_video_params.hpp"
 
+const char *string_view_as_cstr(String_View sv)
+{
+    char *cstr = (char *) malloc(sv.count + 1);
+    if (!cstr) return cstr;
+    memcpy(cstr, sv.data, sv.count);
+    cstr[sv.count] = '\0';
+    return cstr;
+}
+
 void print1(FILE *stream, Video_Params params)
 {
     println(stream, "{");
@@ -11,6 +20,7 @@ void print1(FILE *stream, Video_Params params)
     println(stream, "    .nickname_color = ", params.nickname_color, ",");
     println(stream, "    .text_color = ", params.text_color, ",");
     println(stream, "    .bitrate = ", params.bitrate, ",");
+    println(stream, "    .font = ", params.font, ",");
     print(stream, "}");
 }
 
@@ -24,6 +34,7 @@ Video_Params default_video_params() {
     params.nickname_color   = {255, 100, 100, 255};
     params.text_color       = {200, 200, 200, 255};
     params.bitrate           = 400'000;
+    params.font              = ""_sv;
     return params;
 }
 
@@ -84,6 +95,8 @@ Video_Params video_params_from_file(const char *filepath)
                 expect_into(
                     result.bitrate, value.as_integer<int>(),
                     filepath, ":", line_number, ": `", value, "` is not a number");
+            } else if ("font"_sv == key) {
+                result.font = value;
             } else {
                 println(stderr, "Unknown Video Parameter `", key, "`");
                 abort();

+ 1 - 0
src/vodus_video_params.hpp

@@ -11,6 +11,7 @@ struct Video_Params
     Pixel32 nickname_color;
     Pixel32 text_color;
     int bitrate;
+    String_View font;
 };
 
 void print1(FILE *stream, Video_Params params);

+ 5 - 0
video.params

@@ -0,0 +1,5 @@
+fps = 69
+width = 690
+height = 420
+background_color = FFFF00FF
+font = assets/ComicNeue_Bold.otf