#!/usr/bin/perl
#require 5.008;
our $VERSION = "0.01"; # 2010-03-15

use utf8;
use strict;
use warnings;

use Image::Magick;

{
  my $image = Image::Magick->new(size => "720x480");
#  $image->ReadImage("xc:black");
  $image->Read("Background.jpg");
  my $mask = Image::Magick->new(size => "720x480");
  $mask->ReadImage("xc:transparent");

  open(FILE, ">", "menuC.xml");
  print FILE <<"EOT";
<subpictures>
  <stream>
    <spu
         force="yes"
         start="00:00:00.00"
         select="menuCh.png"
         highlight="menuCh.png"
     >
EOT

  foreach my $p (
		 ["タイトル", "Title.jpg", 0, 0],
		 ["第１楽章", "C1.jpg", 110, 0],
		 ["第２楽章", "C2.jpg", 220, 0],
		 ["第３楽章", "C3.jpg", 0, 120],
		 ["第４楽章", "C4.jpg", 110, 120],
		 ["第５楽章", "C5.jpg", 220, 120],
		) {
    my ($title, $file, $x, $y) = @$p;
    $x += 80;
    $y += 200;
    my $thumbnail = Image::Magick->new;
    $thumbnail->Read($file);
    $thumbnail->Crop(geometry => "706x478+6+1");
    $thumbnail->Resize(geometry => "100x68");
    $image->Composite(image => $thumbnail,
		      compose => "Atop",
		      x => $x, y => $y
		     );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     stroke => "black", strokewidth => "3",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => $title,
		    );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     fill => "white",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => $title,
		    );
    $mask->Draw(primitive => "roundRectangle",
		 points => sprintf("%d,%d %d,%d 4,4", 
				   $x-4, $y-4, $x + 100 + 4, $y + 90 + 4),
		 antialias => 0,
		 fill => "rgba(100%,0%,0%,0.5)",
	      );
    my $s = $file;
    $s =~ s/\.jpg$//;
    print FILE sprintf(<<"EOT", $s, $x-4, $y-4, $x+100+4, $y+90+4);
          <button name="BC%s" x0="%d" y0="%d" x1="%d" y1="%d" />
EOT
  }

  {
    my $x = 80;
    my $y = 80;
    my $title = "マーラー:交響曲\n    第２番《復活》";

    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "40",
		     interwordspacing => 0.1,
		     stroke => "black", strokewidth => "3",
		     x => $x, y => $y,
		     gravity => "NorthEast",
		     align => "Left",
		     text => $title,
		    );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "40",
		     fill => "white",
		     x => $x, y => $y,
		     gravity => "NorthEast",
		     align => "Left",
		     text => $title,
		    );
  }

  {
    my $x = 440;
    my $y = 300;
    my $title = "バーンスタイン指揮\nロンドン交響楽団\nシーラ・アームストロング\nジャネット・ベイカー\n\n1973年9月\n英国イーリー大聖堂";

    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18",
		     interwordspacing => 0.1,
		     stroke => "black", strokewidth => "3",
		     x => $x, y => $y,
		     gravity => "NorthEast",
		     align => "Left",
		     text => $title,
		    );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18",
		     fill => "white",
		     x => $x, y => $y,
		     gravity => "NorthEast",
		     align => "Left",
		     text => $title,
		    );
  }

  {
    my $x = 80;
    my $y = 80;
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     stroke => "black", strokewidth => "3",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "すべて見る",
		    );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     fill => "white",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "すべて見る",
		    );
    $mask->Draw(primitive => "roundRectangle",
		points => sprintf("%d,%d %d,%d 4,4", 
				  $x-4, $y - 4 + 50 + 20, 
				  $x + 100 + 4, $y + 90 + 6 + 4),
		antialias => 0,
		fill => "rgba(100%,0%,0%,0.5)",
	       );
    print FILE sprintf(<<"EOT", "All", $x - 4, $y-4+50+20, $x+100, $y+90+6+4);
          <button name="BC%s" x0="%d" y0="%d" x1="%d" y1="%d" />
EOT
  }

  my $image2 = $image->Clone;
  {
    my $x = 80 + 110;
    my $y = 80;
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     stroke => "black", strokewidth => "3",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "字幕ＯＮ",
		    );
    $image->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     fill => "white",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "字幕ＯＮ",
		    );
    $image2->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     stroke => "black", strokewidth => "3",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "字幕ＯＦＦ",
		    );
    $image2->Annotate(font => "c:/Windows/Fonts/MSgothic.ttf",
		     pointsize => "18", 
		     fill => "white",
		     x => $x + 50, y => $y + 90,
		     gravity => "Center",
		     align => "Center",
		     text => "字幕ＯＦＦ",
		    );
    $mask->Draw(primitive => "roundRectangle",
		points => sprintf("%d,%d %d,%d 4,4", 
				  $x-4, $y - 4 + 50 + 20, 
				  $x + 100 + 4, $y + 90 + 6 + 4),
		antialias => 0,
		fill => "rgba(100%,0%,0%,0.5)",
	       );
    print FILE sprintf(<<"EOT", "Subtitle", $x - 4, $y-4+50+20, $x+100, $y+90+6+4);
          <button name="BC%s" x0="%d" y0="%d" x1="%d" y1="%d" />
EOT
  }

  print FILE <<"EOT";
     </spu>
  </stream>
</subpictures>
EOT
  close(FILE);

  $image->Write("menuC.png");
  $image2->Write("menuC2.png");
  $mask->Write("menuCh.png");
}
