+
+double
+db_to_linear (double db)
+{
+ return pow(10, db / 20);
+}
+
+double
+linear_to_db (double linear)
+{
+ return 20 * log10(linear);
+}
+
+
+dcp::Size
+scale_for_display (dcp::Size s, dcp::Size display_container, dcp::Size film_container)
+{
+ /* Now scale it down if the display container is smaller than the film container */
+ if (display_container != film_container) {
+ float const scale = min (
+ float (display_container.width) / film_container.width,
+ float (display_container.height) / film_container.height
+ );
+
+ s.width = lrintf (s.width * scale);
+ s.height = lrintf (s.height * scale);
+ }
+
+ return s;
+}
+
+
+dcp::DecryptedKDM
+decrypt_kdm_with_helpful_error (dcp::EncryptedKDM kdm)
+{
+ try {
+ return dcp::DecryptedKDM (kdm, Config::instance()->decryption_chain()->key().get());
+ } catch (dcp::KDMDecryptionError& e) {
+ /* Try to flesh out the error a bit */
+ string const kdm_subject_name = kdm.recipient_x509_subject_name();
+ bool on_chain = false;
+ shared_ptr<const dcp::CertificateChain> dc = Config::instance()->decryption_chain();
+ BOOST_FOREACH (dcp::Certificate i, dc->root_to_leaf()) {
+ if (i.subject() == kdm_subject_name) {
+ on_chain = true;
+ }
+ }
+ if (!on_chain) {
+ throw KDMError (_("This KDM was not made for DCP-o-matic's decryption certificate."), e.what());
+ } else if (on_chain && kdm_subject_name != dc->leaf().subject()) {
+ throw KDMError (_("This KDM was made for DCP-o-matic but not for its leaf certificate."), e.what());
+ } else {
+ throw;
+ }
+ }
+}
+
+
+boost::filesystem::path
+default_font_file ()
+{
+ boost::filesystem::path liberation_normal;
+ try {
+ liberation_normal = resources_path() / "LiberationSans-Regular.ttf";
+ if (!boost::filesystem::exists (liberation_normal)) {
+ /* Hack for unit tests */
+ liberation_normal = resources_path() / "fonts" / "LiberationSans-Regular.ttf";
+ }
+ } catch (boost::filesystem::filesystem_error& e) {
+
+ }
+
+ if (!boost::filesystem::exists(liberation_normal)) {
+ liberation_normal = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf";
+ }
+
+ return liberation_normal;
+}