Fix various SNAFUs with Font ID handling.
[libdcp.git] / tools / dcpdumpsub.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "smpte_subtitle_asset.h"
21 #include "util.h"
22 #include <getopt.h>
23 #include <cstdlib>
24 #include <string>
25 #include <iostream>
26
27 using std::string;
28 using std::cout;
29 using std::cerr;
30 using std::map;
31
32 static void
33 help (string n)
34 {
35         cerr << "Syntax: " << n << " [OPTION] <MXF>\n"
36              << "  -h, --help                   show this help\n"
37              << "  -n, --no-fonts               don't extract fonts\n";
38 }
39
40 int
41 main (int argc, char* argv[])
42 {
43         bool extract_fonts = true;
44
45         int option_index = 0;
46         while (1) {
47                 static struct option long_options[] = {
48                         { "help", no_argument, 0, 'h'},
49                         { "no-fonts", no_argument, 0, 'n'},
50                         { 0, 0, 0, 0 }
51                 };
52
53                 int c = getopt_long (argc, argv, "hn", long_options, &option_index);
54
55                 if (c == -1) {
56                         break;
57                 }
58
59                 switch (c) {
60                 case 'h':
61                         help (argv[0]);
62                         exit (EXIT_SUCCESS);
63                 case 'n':
64                         extract_fonts = false;
65                         break;
66                 }
67         }
68
69         if (argc <= optind || argc > (optind + 2)) {
70                 help (argv[0]);
71                 exit (EXIT_FAILURE);
72         }
73
74         dcp::SMPTESubtitleAsset sub (argv[optind]);
75
76         cout << sub.xml_as_string() << "\n";
77
78         if (extract_fonts) {
79                 map<string, dcp::Data> fonts = sub.fonts_with_load_ids ();
80                 for (map<string, dcp::Data>::const_iterator i = fonts.begin(); i != fonts.end(); ++i) {
81                         FILE* f = dcp::fopen_boost (i->first + ".ttf", "wb");
82                         if (!f) {
83                                 cerr << "Could not open font file " << i->first << ".ttf for writing";
84                                 exit (EXIT_FAILURE);
85                         }
86                         fwrite (i->second.data.get(), 1, i->second.size, f);
87                         fclose (f);
88                 }
89         }
90 }