comparison libass/ass.c @ 19652:2c016957360a

Add -ass-styles option. It allows to load styles from a file and use them for plain text subtitles rendering.
author eugeni
date Sun, 03 Sep 2006 17:42:31 +0000
parents bbe600db7b83
children 5a24682d9dd5
comparison
equal deleted inserted replaced
19651:8135cbd2dbc5 19652:2c016957360a
26 #include "ass_utils.h" 26 #include "ass_utils.h"
27 #include "libvo/sub.h" // for utf8_get_char 27 #include "libvo/sub.h" // for utf8_get_char
28 28
29 char *get_path(char *); 29 char *get_path(char *);
30 30
31 typedef enum {PST_UNKNOWN = 0, PST_INFO, PST_STYLES, PST_EVENTS, PST_FONTS} parser_state_t;
32
31 struct parser_priv_s { 33 struct parser_priv_s {
32 enum {PST_UNKNOWN = 0, PST_INFO, PST_STYLES, PST_EVENTS, PST_FONTS} state; 34 parser_state_t state;
33 char* fontname; 35 char* fontname;
34 char* fontdata; 36 char* fontdata;
35 int fontdata_size; 37 int fontdata_size;
36 int fontdata_used; 38 int fontdata_used;
37 }; 39 };
842 return outbuf; 844 return outbuf;
843 } 845 }
844 #endif // ICONV 846 #endif // ICONV
845 847
846 /** 848 /**
847 * \brief Read subtitles from file. 849 * \brief read file contents into newly allocated buffer, recoding to utf-8
848 * \param fname file name 850 */
849 * \return newly allocated track 851 static char* read_file(char* fname)
850 */
851 ass_track_t* ass_read_file(char* fname)
852 { 852 {
853 int res; 853 int res;
854 long sz; 854 long sz;
855 long bytes_read; 855 long bytes_read;
856 char* buf; 856 char* buf;
857 ass_track_t* track; 857
858
859 FILE* fp = fopen(fname, "rb"); 858 FILE* fp = fopen(fname, "rb");
860 if (!fp) { 859 if (!fp) {
861 mp_msg(MSGT_GLOBAL, MSGL_WARN, "ass_read_file(%s): fopen failed\n", fname); 860 mp_msg(MSGT_GLOBAL, MSGL_WARN, "ass_read_file(%s): fopen failed\n", fname);
862 return 0; 861 return 0;
863 } 862 }
897 896
898 #ifdef USE_ICONV 897 #ifdef USE_ICONV
899 if (sub_cp) { 898 if (sub_cp) {
900 char* tmpbuf = sub_recode(buf, sz); 899 char* tmpbuf = sub_recode(buf, sz);
901 free(buf); 900 free(buf);
902 if (!tmpbuf)
903 return 0;
904 buf = tmpbuf; 901 buf = tmpbuf;
905 } 902 }
906 #endif 903 #endif
904 return buf;
905 }
906
907 /**
908 * \brief Read subtitles from file.
909 * \param fname file name
910 * \return newly allocated track
911 */
912 ass_track_t* ass_read_file(char* fname)
913 {
914 char* buf;
915 ass_track_t* track;
916
917 buf = read_file(fname);
918 if (!buf)
919 return 0;
907 920
908 track = ass_new_track(); 921 track = ass_new_track();
909 track->name = strdup(fname); 922 track->name = strdup(fname);
910 923
911 // process header 924 // process header
926 939
927 mp_msg(MSGT_GLOBAL, MSGL_INFO, "LIBASS: added subtitle file: %s (%d styles, %d events)\n", fname, track->n_styles, track->n_events); 940 mp_msg(MSGT_GLOBAL, MSGL_INFO, "LIBASS: added subtitle file: %s (%d styles, %d events)\n", fname, track->n_styles, track->n_events);
928 941
929 // dump_events(forced_tid); 942 // dump_events(forced_tid);
930 return track; 943 return track;
944 }
945
946 /**
947 * \brief read styles from file into already initialized track
948 */
949 int ass_read_styles(ass_track_t* track, char* fname)
950 {
951 char* buf;
952 parser_state_t old_state;
953
954 buf = read_file(fname);
955 if (!buf)
956 return 1;
957
958 old_state = track->parser_priv->state;
959 track->parser_priv->state = PST_STYLES;
960 process_text(track, buf);
961 track->parser_priv->state = old_state;
962
963 return 0;
931 } 964 }
932 965
933 static char* validate_fname(char* name) 966 static char* validate_fname(char* name)
934 { 967 {
935 char* fname; 968 char* fname;