+size_t
+utf8_strlen (string s)
+{
+ size_t const len = s.length ();
+ int N = 0;
+ for (size_t i = 0; i < len; ++i) {
+ unsigned char c = s[i];
+ if ((c & 0xe0) == 0xc0) {
+ ++i;
+ } else if ((c & 0xf0) == 0xe0) {
+ i += 2;
+ } else if ((c & 0xf8) == 0xf0) {
+ i += 3;
+ }
+ ++N;
+ }
+ return N;
+}
+
+string
+day_of_week_to_string (boost::gregorian::greg_weekday d)
+{
+ switch (d.as_enum()) {
+ case boost::date_time::Sunday:
+ return _("Sunday");
+ case boost::date_time::Monday:
+ return _("Monday");
+ case boost::date_time::Tuesday:
+ return _("Tuesday");
+ case boost::date_time::Wednesday:
+ return _("Wednesday");
+ case boost::date_time::Thursday:
+ return _("Thursday");
+ case boost::date_time::Friday:
+ return _("Friday");
+ case boost::date_time::Saturday:
+ return _("Saturday");
+ }
+
+ return d.as_long_string ();
+}
+
+/** @param size Size of picture that the subtitle will be overlaid onto */
+void
+emit_subtitle_image (ContentTimePeriod period, dcp::SubtitleImage sub, dcp::Size size, shared_ptr<TextDecoder> decoder)
+{
+ /* XXX: this is rather inefficient; decoding the image just to get its size */
+ FFmpegImageProxy proxy (sub.png_image());
+ shared_ptr<Image> image = proxy.image().image;
+ /* set up rect with height and width */
+ dcpomatic::Rect<double> rect(0, 0, image->size().width / double(size.width), image->size().height / double(size.height));
+
+ /* add in position */
+
+ switch (sub.h_align()) {
+ case dcp::HALIGN_LEFT:
+ rect.x += sub.h_position();
+ break;
+ case dcp::HALIGN_CENTER:
+ rect.x += 0.5 + sub.h_position() - rect.width / 2;
+ break;
+ case dcp::HALIGN_RIGHT:
+ rect.x += 1 - sub.h_position() - rect.width;
+ break;
+ }
+
+ switch (sub.v_align()) {
+ case dcp::VALIGN_TOP:
+ rect.y += sub.v_position();
+ break;
+ case dcp::VALIGN_CENTER:
+ rect.y += 0.5 + sub.v_position() - rect.height / 2;
+ break;
+ case dcp::VALIGN_BOTTOM:
+ rect.y += 1 - sub.v_position() - rect.height;
+ break;
+ }
+
+ decoder->emit_bitmap (period, image, rect);
+}
+
+bool
+show_jobs_on_console (bool progress)
+{
+ bool first = true;
+ bool error = false;
+ while (true) {
+
+ dcpomatic_sleep_seconds (5);
+
+ list<shared_ptr<Job> > jobs = JobManager::instance()->get();
+
+ if (!first && progress) {
+ for (size_t i = 0; i < jobs.size(); ++i) {
+ cout << "\033[1A\033[2K";
+ }
+ cout.flush ();
+ }
+
+ first = false;
+
+ BOOST_FOREACH (shared_ptr<Job> i, jobs) {
+ if (progress) {
+ cout << i->name();
+ if (!i->sub_name().empty()) {
+ cout << "; " << i->sub_name();
+ }
+ cout << ": ";
+
+ if (i->progress ()) {
+ cout << i->status() << " \n";
+ } else {
+ cout << ": Running \n";
+ }
+ }
+
+ if (!progress && i->finished_in_error()) {
+ /* We won't see this error if we haven't been showing progress,
+ so show it now.
+ */
+ cout << i->status() << "\n";
+ }
+
+ if (i->finished_in_error()) {
+ error = true;
+ }
+ }
+
+ if (!JobManager::instance()->work_to_do()) {
+ break;
+ }
+ }
+
+ return error;
+}
+
+/** XXX: could use mmap? */
+void
+copy_in_bits (boost::filesystem::path from, boost::filesystem::path to, boost::function<void (float)> progress)
+{
+ FILE* f = fopen_boost (from, "rb");
+ if (!f) {
+ throw OpenFileError (from, errno, OpenFileError::READ);
+ }
+ FILE* t = fopen_boost (to, "wb");
+ if (!t) {
+ fclose (f);
+ throw OpenFileError (to, errno, OpenFileError::WRITE);
+ }
+
+ /* on the order of a second's worth of copying */
+ boost::uintmax_t const chunk = 20 * 1024 * 1024;
+
+ uint8_t* buffer = static_cast<uint8_t*> (malloc(chunk));
+ if (!buffer) {
+ throw std::bad_alloc ();
+ }
+
+ boost::uintmax_t const total = boost::filesystem::file_size (from);
+ boost::uintmax_t remaining = total;
+
+ while (remaining) {
+ boost::uintmax_t this_time = min (chunk, remaining);
+ size_t N = fread (buffer, 1, chunk, f);
+ if (N < this_time) {
+ fclose (f);
+ fclose (t);
+ free (buffer);
+ throw ReadFileError (from, errno);
+ }
+
+ N = fwrite (buffer, 1, this_time, t);
+ if (N < this_time) {
+ fclose (f);
+ fclose (t);
+ free (buffer);
+ throw WriteFileError (to, errno);
+ }
+
+ progress (1 - float(remaining) / total);
+ remaining -= this_time;
+ }
+
+ fclose (f);
+ fclose (t);
+ free (buffer);
+}
+