std::shared_ptr
[dcpomatic.git] / src / lib / subtitle_analysis.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "subtitle_analysis.h"
22 #include "exceptions.h"
23 #include "warnings.h"
24 #include <libcxml/cxml.h>
25 #include <dcp/raw_convert.h>
26 DCPOMATIC_DISABLE_WARNINGS
27 #include <libxml++/libxml++.h>
28 DCPOMATIC_ENABLE_WARNINGS
29
30 using std::string;
31 using dcp::raw_convert;
32 using std::shared_ptr;
33
34 int const SubtitleAnalysis::_current_state_version = 1;
35
36
37 SubtitleAnalysis::SubtitleAnalysis (boost::filesystem::path path)
38 {
39         cxml::Document f ("SubtitleAnalysis");
40
41         f.read_file (path);
42
43         if (f.optional_number_child<int>("Version").get_value_or(1) < _current_state_version) {
44                 /* Too old.  Throw an exception so that this analysis is re-run. */
45                 throw OldFormatError ("Subtitle analysis file is too old");
46         }
47
48         cxml::NodePtr bounding_box = f.optional_node_child("BoundingBox");
49         if (bounding_box) {
50                 _bounding_box = dcpomatic::Rect<double> ();
51                 _bounding_box->x = bounding_box->number_child<double>("X");
52                 _bounding_box->y = bounding_box->number_child<double>("Y");
53                 _bounding_box->width = bounding_box->number_child<double>("Width");
54                 _bounding_box->height = bounding_box->number_child<double>("Height");
55         }
56
57         _analysis_x_offset = f.number_child<double>("AnalysisXOffset");
58         _analysis_y_offset = f.number_child<double>("AnalysisYOffset");
59 }
60
61
62 void
63 SubtitleAnalysis::write (boost::filesystem::path path) const
64 {
65         shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
66         xmlpp::Element* root = doc->create_root_node ("SubtitleAnalysis");
67
68         root->add_child("Version")->add_child_text (raw_convert<string>(_current_state_version));
69
70         if (_bounding_box) {
71                 xmlpp::Element* bounding_box = root->add_child("BoundingBox");
72                 bounding_box->add_child("X")->add_child_text(raw_convert<string>(_bounding_box->x));
73                 bounding_box->add_child("Y")->add_child_text(raw_convert<string>(_bounding_box->y));
74                 bounding_box->add_child("Width")->add_child_text(raw_convert<string>(_bounding_box->width));
75                 bounding_box->add_child("Height")->add_child_text(raw_convert<string>(_bounding_box->height));
76         }
77
78         root->add_child("AnalysisXOffset")->add_child_text(raw_convert<string>(_analysis_x_offset));
79         root->add_child("AnalysisYOffset")->add_child_text(raw_convert<string>(_analysis_y_offset));
80
81         doc->write_to_file_formatted (path.string());
82 }
83
84