2 Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "subtitle_analysis.h"
23 #include "exceptions.h"
24 #include <libcxml/cxml.h>
25 #include <dcp/filesystem.h>
26 #include <dcp/raw_convert.h>
27 #include <dcp/warnings.h>
28 LIBDCP_DISABLE_WARNINGS
29 #include <libxml++/libxml++.h>
30 LIBDCP_ENABLE_WARNINGS
33 using std::make_shared;
34 using std::shared_ptr;
36 using dcp::raw_convert;
39 int const SubtitleAnalysis::_current_state_version = 1;
42 SubtitleAnalysis::SubtitleAnalysis (boost::filesystem::path path)
44 cxml::Document f ("SubtitleAnalysis");
46 f.read_file(dcp::filesystem::fix_long_path(path));
48 if (f.optional_number_child<int>("Version").get_value_or(1) < _current_state_version) {
49 /* Too old. Throw an exception so that this analysis is re-run. */
50 throw OldFormatError ("Subtitle analysis file is too old");
53 cxml::NodePtr bounding_box = f.optional_node_child("BoundingBox");
55 _bounding_box = dcpomatic::Rect<double> ();
56 _bounding_box->x = bounding_box->number_child<double>("X");
57 _bounding_box->y = bounding_box->number_child<double>("Y");
58 _bounding_box->width = bounding_box->number_child<double>("Width");
59 _bounding_box->height = bounding_box->number_child<double>("Height");
62 _analysis_x_offset = f.number_child<double>("AnalysisXOffset");
63 _analysis_y_offset = f.number_child<double>("AnalysisYOffset");
68 SubtitleAnalysis::write (boost::filesystem::path path) const
70 auto doc = make_shared<xmlpp::Document>();
71 xmlpp::Element* root = doc->create_root_node ("SubtitleAnalysis");
73 root->add_child("Version")->add_child_text (raw_convert<string>(_current_state_version));
76 auto bounding_box = root->add_child("BoundingBox");
77 bounding_box->add_child("X")->add_child_text(raw_convert<string>(_bounding_box->x));
78 bounding_box->add_child("Y")->add_child_text(raw_convert<string>(_bounding_box->y));
79 bounding_box->add_child("Width")->add_child_text(raw_convert<string>(_bounding_box->width));
80 bounding_box->add_child("Height")->add_child_text(raw_convert<string>(_bounding_box->height));
83 root->add_child("AnalysisXOffset")->add_child_text(raw_convert<string>(_analysis_x_offset));
84 root->add_child("AnalysisYOffset")->add_child_text(raw_convert<string>(_analysis_y_offset));
86 doc->write_to_file_formatted (path.string());