2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG 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 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG 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 CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Niklas P Andersson, niklas.p.andersson@svt.se
22 #include "descriptor.h"
24 #include "util/pdf_reader.h"
26 #include <common/log.h>
28 #include <boost/property_tree/ptree.hpp>
29 #include <boost/property_tree/xml_parser.hpp>
30 #include <boost/locale.hpp>
34 namespace caspar { namespace psd {
36 class descriptor::context::scoped_holder
38 descriptor::context::ptr_type ctx_;
40 explicit scoped_holder(const std::wstring& key, descriptor::context::ptr_type ctx) : ctx_(ctx)
42 Ptree *parent = ctx_->stack.back();
43 Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
44 ctx_->stack.push_back(child);
48 ctx_->stack.pop_back();
52 descriptor::descriptor(const std::wstring& debug_name) : context_(std::make_shared<context>(debug_name))
54 context_->stack.push_back(&context_->root);
56 descriptor::descriptor(const std::wstring& key, context::ptr_type context) : context_(context)
58 Ptree *parent = context_->stack.back();
59 Ptree *child = &parent->push_back(std::make_pair(key, Ptree()))->second;
60 context->stack.push_back(child);
62 descriptor::~descriptor()
64 context_->stack.pop_back();
67 void descriptor::populate(bigendian_file_input_stream& stream)
69 stream.read_unicode_string();
70 stream.read_id_string();
71 int element_count = stream.read_long();
72 for (int element_index = 0; element_index < element_count; ++element_index)
74 std::wstring key = stream.read_id_string();
75 read_value(key, stream);
78 if (context_->stack.size() == 1)
79 log::print_child(boost::log::trivial::trace, context_->debug_name + L": ", L"", context_->root);
82 void descriptor::read_value(const std::wstring& key, bigendian_file_input_stream& stream)
84 auto value_type = stream.read_long();
90 descriptor desc(key, context_);
91 desc.populate(stream);
97 context_->stack.back()->put(key, stream.read_double());
103 context_->stack.back()->put(key, stream.read_unicode_string());
109 auto k = stream.read_id_string();
110 auto v = stream.read_id_string();
111 context_->stack.back()->put(k, v);
117 std::int32_t value = stream.read_long();
118 context_->stack.back()->put(key, value);
124 context_->stack.back()->put(key, stream.read_byte());
130 context::scoped_holder list(key, context_);
131 int count = stream.read_long();
133 for (int i = 0; i < count; ++i)
135 read_value(L"li", stream);
142 auto rawdata_length = stream.read_long();
143 std::vector<char> rawdata(rawdata_length);
144 stream.read(rawdata.data(), rawdata_length);
145 std::wstring data_str(rawdata.begin(), rawdata.end());
146 context_->stack.back()->put(key, data_str);
152 context::scoped_holder list(key, context_);
153 auto unit = stream.read_long();
154 std::string type(reinterpret_cast<char*>(&unit), 4);
155 std::wstring wtype(type.rbegin(), type.rend());
156 context_->stack.back()->put(wtype, stream.read_double());
166 //descriptor type not supported yet
167 CASPAR_THROW_EXCEPTION(psd_file_format_exception() << msg_info("descriptor type not supported yet"));