% ConTeXt file for typesetting of cd-cases.
% Source LaTeX:
%          Dennis van Dok
%          NTG MAPS No.33, Najaar 2005, p.4--13
% Translated and expanded for ConTeXt:
%          Hans van der Meer
%          Last change 2006-03-12
%
% Usage ========================================================================
%
% usage jewelcase:
%    \jewelcase [options]{maintitle}
%    \startcdcase         % contents of cdcase area
%    ....                 % input as TeX text, \input file, \getbuffer, etc.
%    \stopcdcase
%    \startcdcaseright    % automatically skipped
%    ....                 % input as TeX text, \input file, \getbuffer, etc.
%    \stopcdcaseright     % resume typesetting
% usage  foldcase:
%    \foldcase  [options]{maintitle}
%    \startcdcase         % contents of cdcase area
%    ....                 % input as TeX text, \input file, \getbuffer, etc.
%    \stopcdcase
%    \startcdcaseright    % contents right area
%    ....                 % input as TeX text, \input file, \getbuffer, etc.
%    \stopcdcaseright
%
% \setupfoldcasepage[options] (final) % page setup for foldcase
%
%  maintitle = general title tekst, including style and color
%              as macro \maintitle available
%     default is title's style and color
%     positioning:
%         content frame: above middle
%         jewelcase side: see parameter sidelocation
%
% The input must start with a \title-line, otherwise inline-mode doesn't start
% up correctly. To skip the title give an empty title with the line:
%       \title .
%
% Placing jewelcase aligned is done by the location-option.
% The frame is enclosed by the appropriate \hfil's followed by \break.
% For option = none (default), none of these are added.
% In the case of foldcase there is little use for it, that mode is meant
% to be used in conjunction with:
%     \setupfoldcasepage and \setupfoldcasepage[final]
% Then each part of the case has its own page, placed on an A4-sheet;
% landscape in concept mode, rotated to portrait for printing in [final].
%
% Change dimensions by redefining \jewelcaseheight, \jewelcasewidth, etc.
% Change color and size of guidelines by redefining the macros
% \grayrulecolor and \grayrulesize.
%
% By default the outer frame of the jewelcase is set in the same color and size
% as the folding lines, in order to use them as a guide for cutting the paper;
% Global change to black can be accomplished by \setupcdcase[framecolor=black].
% Suppress them with \setupcdcase[frame=off], for example in the case of
% a colored background.
%
% Overlaying with a picture hint: set picture brightness high and contrast 
% low in order to not having the picture dominating the text.
%
% Preset the cd-case parameters with: \setupcdcases[option=keyword,...]
%    all options are of the type key=value, default within <>
% Or do it locally with \jewelcase[options].. and \foldcase[options]..
% layout:            alternative =     <normal>=columns,packed=inline,none
% frame alignment:   location =        <none>,left,middle,right
% draw frame:        frame =           <on>,off
% frame thickness:   rulethickness =   (dimension) <.4pt>
% frame inset:       offset =          (dimension) <3mm>
% extra lr-space     margin =          (dimension) <3mm> jewel <0mm> fold
% number columns:    n = (number)      <3>
% column separation: distance =        (dimension) <3mm>
% extra lineskip:    lineskip =        (dimension) <1pt>
% size tracknumber:  tracksize =       (number) <3>
% enclosure of time: timebefore =      (character sequence) <{(}>
%                    timeafter  =      (character sequence) <{)}>
% placing maintitle: maintitle =       <on>,off
% side title:        sidelocation =    <left>,middle,right
% overall            text: style =     (font command) <\ss>
% title:             titlestyle =      (font command) <style+\bf>
% subtitle:          subtitlestyle =   (font command) <titlestyle>
% tracknumber:       numberstyle =     (font command) <style>
% track:             trackstyle =      (font command) <style>
% artist:            artiststyle =     (font command) <style>
% time:              timestyle =       (font command) <style>
% frame:             framecolor =      (color) <gray>
% overall text:      foregroundcolor = (color) <black>
% title:             titlecolor =      (color) <foregroundcolor>
% subtitle:          subtitlecolor =   (color) <foregroundcolor>
% tracknumber:       numbercolor =     (color) <foregroundcolor>
% track:             trackcolor =      (color) <foregroundcolor>
% time:              timecolor =       (color) <foregroundcolor>
% background:        background =      none,color <color>
%                    backgroundcolor = (color) <white>
% figure background: overlay =         <none>,(figure file)
%
% special macros
% \title    = precede items with title
% \subtitle = precede items with subtitle
% \artist   = switch to artiststyle (previously \dot)
%
% Nota bene: Setup coloring should be done at the top of users file with
% \setupcolors[state=start]
%
% Example file:
%
% \language[nl]\mainlanguage[nl]\setupcolors[state=start]
% \setupbodyfont[10pt]
% \input cdcases
% \startbuffer
% \title .
% 1  {\artist Giovanni Bassano} Frais et Gaillard (1591)...(3:06).
% 2  {\artist Giovanni Battista Bovicelli} Io son ferito (1594)...(6:36).
% 3  {\artist Antonio de Cabez\`on} Un gay bergier (1578)...(2:38).
% 4  {\artist Giovanni Battista Bovicelli} Anchor che co'l partire (1594)...(3:54).
% 5  {\artist Di\'ego Ortiz} Recercada segonda sobre Doulce memoire (1553)...(3:05).
% 6  {\artist Di\'ego Ortiz} Recercada tercera sobre Doulce memoire (1553)...(2:33).
% 7  {\artist P. Manuel Rodrigues Coelho} Susanne un jour (1620)...(4:50).
% 8  {\artist Francesco Rognoni Taeggio} Pulchra es, amica mea (1620)...(5:35).
% 9  {\artist Giovanni Bassano} Susanne un jour (1591)...(3:33).
% 10 {\artist Giovanni Battista Bovicelli} Angelus ad pastores ait (1594)...(4:32).
% 11 {\artist Antonio de Cabez\`on} Pour un plaisir (1578)...(2:04).
% 12 {\artist Richardo Rogniono} Un gay bergier (1592)...(4:17).
% 13 {\artist Bartolomeo de Selma e Salaverde} Susanne un jour (1638)...(6:53).
% 14 {\artist Luzzasco Luzzaschi} Aura soave (1601)...(2:53).
% 15 {\artist Luzzasco Luzzaschi} O Primavera (1601)...(2:53).
% 16 {\artist Giovanni Bassano} Anchor che co'l partire (1591)...(3:22).
% 17 {\artist Giovanni Bassano} La bella netta ignuda e bianca mano (1591)...(5:13).
% 18 {\artist Giovanni Bassano} Ricercata prima (1585)...(3:28).
% 19 {\artist Giovanni Bassano} Tirsi morir volea (1591)...(5:36).
% 20 {\artist Giovanni Bassano} Un gay bergier (1591)...(2:36).
% \stopbuffer
% \starttext
% \vfill
% \jewelcase[alternative=inline,n=2,maintitle=on,tracksize=2,
%            artiststyle=\it,titlestyle=\tfa\rm\sc,lineskip=2pt,
%%           overlay=pic.jpg,% if you have a picture pic.jpg
%            backgroundcolor=lightgray]{Virtuose Verzierungskunst um 1600}
% \startcdcase
% \getbuffer
% \stopcdcase
% \startcdcaseright
% \vfill
% \midaligned{\tfa\bf\maintitle}\blank
% Ausf\"uhrende:\crlf
% \startnarrower
% Montserrat Figueras, Sopran\crlf
% Ren\'e Jacobs, Altus\crlf
% Kurt Widmer, Bass\crlf
% Bruce Dickey, Cornetto\crlf
% Michel Piguet, Renaissance||Blockfl\"ote\crlf
% Jordi Savall, Viola da gamba\crlf
% Anthony Bailes, Laute\crlf
% Hopkinson Smith, Laute\crlf
% Aline Parker||Zylberajch, Cembalo, Orgel\crlf
% Viola||da||gamba||Ensemble: Jordi Savall, Christophe Coin, Sergi Casademunt, Roberto Gini
% \stopnarrower
% \blank
% Deutsche Harmonia Mundi LC 0761, 1980\crlf
% Aufnahmeort: Kirche Rothenfluh, Baselland (Schweiz)
% \vfill\null
% \stopcdcaseright
%\ stoptext
%
\unprotect
%
% Parameter setup ==============================================================
%
\def\jewelcaseheight{117mm}
\def\jewelcasewidth{138mm}
\def\jewelcaseside{6mm}
\def\foldcaseheight{120mm}
\def\foldcasewidth{120mm}
\def\grayrulecolor{darkgray}
\def\grayrulesize{.4pt}
%
% Internal parameter setup =====================================================
%
\def\c!tracksize{tracksize}
\def\c!titlestyle{titlestyle}
\def\c!subtitlestyle{subtitlestyle}
\def\c!numberstyle{numberstyle}
\def\c!trackstyle{trackstyle}
\def\c!artiststyle{artiststyle}
\def\c!timestyle{timestyle}
\def\c!titlecolor{titlecolor}
\def\c!subtitlecolor{subtitlecolor}
\def\c!numbercolor{numbercolor}
\def\c!trackcolor{trackcolor}
\def\c!timecolor{timecolor}
\def\c!timebefore{timebefore}
\def\c!timeafter{timeafter}
\def\c!lineskip{lineskip}
\def\c!maintitle{maintitle}
\def\c!sidelocation{sidelocation}
\def\c!overlay{overlay}
\def\c!casebefore{casebefore}
\def\c!caseafter{caseafter}
\def\v!inline{inline}
%
% Install default setup ========================================================
%
\def\setupcdcases{\getparameters[@@cdc]}
\setupcdcases[%
	\c!alternative=\v!normal,% 					(normal=columns,packed=inline)
	\c!n=3,%									(number)
	\c!distance=3mm,%							(dimension)
	\c!frame=\v!on,%							(on,off/yes,no)
	\c!rulethickness=\grayrulesize,%			(dimension)
	\c!lineskip=1pt,%							(dimension)
	\c!tracksize=3,%							(number)
	\c!maintitle=\v!on,%						(on,off/yes,no)
	\c!location=\v!none,%						(left,middle,right,none)
	\c!sidelocation=\v!left,%					(left,middle,right)
	\c!style=\ss,%								(font command)
	\c!titlestyle={\@@cdcstyle\bf},%			(font command)
	\c!subtitlestyle=\@@cdctitlestyle,%			(font command)
	\c!numberstyle=\@@cdcstyle,%				(font command)
	\c!trackstyle=\@@cdcstyle,%					(font command)
	\c!artiststyle=\@@cdcstyle,%				(font command)
	\c!timestyle=\@@cdcstyle,%					(font command)
	\c!framecolor=\grayrulecolor,%				(color)
	\c!foregroundcolor=\s!black,%				(color)
	\c!titlecolor=\@@cdcforegroundcolor,%		(color)
	\c!subtitlecolor=\@@cdcforegroundcolor,%	(color)
	\c!numbercolor=\@@cdcforegroundcolor,%		(color)
	\c!trackcolor=\@@cdcforegroundcolor,%		(color)
	\c!timecolor=\@@cdcforegroundcolor,%		(color)
	\c!background=\v!color,%					(color)
	\c!overlay=\v!none,%						(filename)
	\c!backgroundcolor=\s!white,%				(color)
	\c!offset=3mm,%								(dimension)
	\c!margin=5mm,%								(dimension)
	\c!casebefore=,%							(internal command)
	\c!caseafter=,%								(internal command)
	\c!timebefore={(},%							(string)
	\c!timeafter={)}%							(string)
]

% Code for all variants ========================================================

% place preceding zeroes before #1 until at least tracksize digits
\def\zeroes#1{%
	\count\z@\@ne\relax
	\ifnum#1>9\advance\count\z@\@ne\fi
	\ifnum#1>99\advance\count\z@\@ne\fi
	\ifnum#1>999\advance\count\z@\@ne\fi
	\loop\ifnum\count\z@<\@@cdctracksize\relax
		\advance\count\z@\@ne\relax
		0\repeat
	\count\z@#1\relax\the\count\z@}% reduce number to value (preceding zeroes)

% nota bene: \dot gives math name clash
\def\dot{\unskip\space\@@cdcartiststyle}
% \artist is style switch for the artist name; usage {\artist...}
\def\artist{\@@cdcartiststyle}

% a simple dotted line, at least half an em
\def\trackleaders{\color[\@@cdctrackcolor]%
	{\leaders\hbox to.5em{\hss.\hss}\hskip.5em plus 1fil}}

% typesetting the track number
\def\tracknr#1{\color[\@@cdcnumbercolor]{\@@cdcnumberstyle\zeroes{#1}}}

% inner typesetting of tracktime
\def\tracktime#1{\color[\@@cdctimecolor]%
	{\@@cdctimestyle\@@cdctimebefore#1\@@cdctimeafter}}
	
% typesetting maintitle
\def\domaintitle#1{\doifnotempty{#1}{%
	\color[\@@cdctitlecolor]{\@@cdctitlestyle\hfil#1\hfil}}}
\def\setmaintitle{\doif{\@@cdcmaintitle}{\v!on}%
	{\null\vfil\domaintitle{\maintitle}\expandafter\break}}

% Code specific for no special typesetting except maintitle ====================

\long\def\nonecdcase#1{#1}

% Code specific for columns variant ============================================

% The space between track number and track title
\def\trackspace{\kern.5em}

% Space for the tracknumber
\newdimen\digitspace

% Set space for tracknumber (according to the given tracksize)
\def\setdigitspace{\setbox\z@=\hbox{\zeroes{1}\trackspace}%
	\digitspace\wd\z@}
    
% There is a problem with inline mode when the data do not start with \title.
% It can be solved with an empty title line: "\title ."
% However, a blank line is then typeset in column mode.
% Solved by killing empty \title's, "\subtitle ." remains for vertical space.
\def\columntitle#1.{\doifnotempty{#1}{\docolumntitle{#1}}}
\def\docolumntitle#1{%
	\vskip\baselineskip\penalty-100{\everypar{}\leavevmode
    \rightskip\z@
    \leftskip\z@
    \hangafter\z@
    \hangindent\z@
    \parfillskip\z@ plus 1fill\relax
    \color[\@@cdctitlecolor]{\@@cdctitlestyle#1}\par
    \nobreak}}

% \subtitle is mostly the same as \title but no skip before
\def\columnsubtitle#1.{%
	{\everypar{}\leavevmode
    \global\rightskip\z@
    \global\leftskip\z@
    \global\hangafter\z@
    \global\hangindent\z@
    \global\parfillskip\z@ plus 1fill\relax
    \color[\@@cdcsubtitlecolor]{\@@cdcsubtitlestyle#1}%
    \vadjust{\nobreak}\par}}

% each line is preceded by \entry; trackleaders form the dotted line
\def\columnentry#1 #2...(#3).{\tracknr{#1}\trackspace
	\columntrack{#2}\columntracktime(#3)\par}%

% typeset the name of the track
\def\columntrack#1{\color[\@@cdctrackcolor]{\@@cdctrackstyle #1}}

% the real workhorse of this script. This odd mixture of penalties,
\def\columntracktime(#1){\rm\penalty100\null\nobreak\trackleaders\tracktime{#1}}

% typeset columns
\long\def\columncdcase#1{%
	\vfill
	\startsimplecolumns[n=\@@cdcn,distance=\@@cdcdistance]%
	\baselineskip\bodyfontsize
	\advance\baselineskip\@@cdclineskip\relax
	\everypar{\hangafter\@ne\hangindent\digitspace   % 'hanging' indent
		\leftskip\z@
		\rightskip2em plus 1.5em\relax
		\parfillskip -\rightskip        % makes the last line flush right
		\entry}%
	#1\stopsimplecolumns}

% Code specific for packed variant =============================================

% The title is followed by a dash except at beginning and ending of a line
\def\packedfirsttitle#1.{\dopackedtitle{#1}\let\title\packedtitle}
\def\packedtitle#1.{\leaddash\dopackedtitle{#1}}
\def\dopackedtitle#1{\doifnotempty{#1}%
	{\color[\@@cdctitlecolor]{\@@cdctitlestyle#1}\leaddash}}

% The subtitle is set in subtitle style and color
\def\packedsubtitle#1.{\color[\@@cdcsubtitlecolor]{\@@cdcsubtitlestyle#1}}

% This m-dash is defined as glue, and will disappear at line breaks
\def\leaddash{\cleaders\hbox to 1em{\hss---\hss}\hskip 1em}

% typeset the name of the track
\def\packedtrack#1{\nobreak\space\color[\@@cdctrackcolor]{\@@cdctrackstyle #1}}

% typeset the duration of the track
\def\packedtracktime(#1){\nobreak\space\tracktime{#1}}

% each line is preceded by \entry; trackleaders form the dotted line
\def\packedentry{\space\futurelet\next\bentry}
\def\bentry{\ifcat\noexpand\next0\expandafter\tentry\fi}
\def\tentry#1 #2...(#3).{\tracknr{#1}\packedtrack{#2}\packedtracktime(#3)}%

% typeset inline
\long\def\packedcdcase#1{%
	\hyphenpenalty=500\relax           % rather no hyphens
	\parfillskip\z@
	\baselineskip 100pt plus 100pt minus 100pt\relax
	\vfil
	\begingroup
		\let\par\entry\obeylines
		#1\unskip
	\endgroup}

% Code for typesetting cd-case =================================================

\long\def\cdcase#1#2{%
	% make maintitle accessible
	\def\maintitle{#1}%
	% unify some synonyms
	\doif{\@@cdcalternative}{\v!normal}{\def\@@cdcalternative{\v!columns}}%
	\doif{\@@cdcalternative}{\v!inline}{\def\@@cdcalternative{\v!packed}}%
	\doif{\@@cdcmaintitle}  {\v!yes}   {\def\@@cdcmaintitle  {\v!on}}%
	\doif{\@@cdcmaintitle}  {\v!no}    {\def\@@cdcmaintitle  {\v!off}}%
	\doif{\@@cdcframe}      {\v!yes}   {\def\@@cdcframe      {\v!on}}%
	\doif{\@@cdcframe}      {\v!no}    {\def\@@cdcframe      {\v!off}}%
	\doifnot{\@@cdcoverlay} {\v!none}  {\def\@@cdcbackground{cdcase}}%
	% setup typesetting of the inner frame with contents
	\setupframedtexts[\c!depthcorrection=\v!off,\c!linecorrection=\v!off,%
		\c!width=\@@cdcwidth,\c!height=\@@cdcheight,%
		\c!strut=\v!no,\c!offset=\@@cdcoffset,\c!location=\v!middle,%
		\c!background=\@@cdcbackground,%
		\c!backgroundcolor=\@@cdcbackgroundcolor,%
		\c!foregroundcolor=\@@cdcforegroundcolor]%
	% for variant none
	\doif{\@@cdcalternative}{\v!none}{\let\typesetcdcase\nonecdcase}%
	% for variant columns
	\doif{\@@cdcalternative}{\v!columns}{%
		\setdigitspace
		\let\title\columntitle
		\let\subtitle\columnsubtitle
		\let\entry\columnentry
		\let\typesetcdcase\columncdcase
		}%
	% for variant inline
	\doif{\@@cdcalternative}{\v!packed}{%
		\let\title\packedfirsttitle
		\let\subtitle\packedsubtitle
		\let\entry\packedentry
		\let\typesetcdcase\packedcdcase
		}%
	% for all variants
	\hsize=\@@cdcwidth	% restrict horizontal space, needed for l+r additions
	\@@cdccasebefore
	\startframedtext
		\scratchdimen\@@cdcwidth
		\advance\scratchdimen-\@@cdcmargin
		\advance\scratchdimen-\@@cdcmargin
		\advance\scratchdimen-\@@cdcoffset
		\advance\scratchdimen-\@@cdcoffset
		\edef\innerframewidth{\the\scratchdimen}%
		\scratchdimen\@@cdcheight
		\advance\scratchdimen-\@@cdcoffset
		\advance\scratchdimen-\@@cdcoffset
		\edef\innerframeheight{\the\scratchdimen}%
		\startframedtext[\c!depthcorrection=\v!off,\c!linecorrection=\v!off,%
			\c!width=\innerframewidth,\c!height=\innerframeheight,%
			\c!strut=\v!no,\c!offset=\z@,\c!rulethickness=\z@,%
			\c!background=\v!none]%
			\setmaintitle
			\typesetcdcase{#2}%
		\stopframedtext
	\stopframedtext
	\@@cdccaseafter
	}
	
% defining overlay for cdcase
\def\setcaseoverlay#1#2#3{\defineoverlay[cdcase]%
	[{\externalfigure[#1][\c!width=#2,\c!height=#3]}]}

% Code for typesetting jewelcase backside ======================================

\def\jewelcase{\dosingleargument\dojewelcase}

\long\def\dojewelcase[#1]#2\startcdcase#3\stopcdcase{%
	\begingroup
	% calculate size of case plus the two folded edges
	\scratchdimen\jewelcaseside
	\advance\scratchdimen\jewelcaseside
	\advance\scratchdimen\jewelcasewidth
	\edef\totaljewelcasewidth{\the\scratchdimen}%
	% setup various frames
	\setupframed[\c!width=\totaljewelcasewidth,% nb. height must not be set
		\c!strut=\v!no,\c!offset=\z@,\c!framecolor=\@@cdcframecolor,%
		\c!frame=\@@cdcframe,\c!rulethickness=\@@cdcrulethickness,%
		% restrict sides to backgroundcolor, do not extend a figure into
		\c!background=\v!color,\c!backgroundcolor=\@@cdcbackgroundcolor]%
	\setupframedtexts[\c!frame=\v!off]%
	% setup typesetting of the interior
	\setupcdcases[#1,\c!width=\jewelcasewidth,\c!height=\jewelcaseheight,%
		\c!casebefore={%
			\doif{\@@cdclocation}{\v!middle}{\hfil}%
			\doif{\@@cdclocation}{\v!right}{\hfil}%
			\framed\bgroup\rotate[rotation=90]{\jewelsidetitle{#2}}},%
		\c!caseafter={\rotate[rotation=-90]{\jewelsidetitle{#2}}\egroup
			\doif{\@@cdclocation}{\v!left}{\hfil\break}%
			\doif{\@@cdclocation}{\v!middle}{\hfil\break}%
			\doif{\@@cdclocation}{\v!right}{\break}}%
		]%
	% after \setupcdcases[#1,..] we now know the value of overlay
	\setcaseoverlay{\@@cdcoverlay}{\jewelcasewidth}{\jewelcaseheight}%
	% initialiaze the style, we now know its value
	\@@cdcstyle
	\cdcase{#2}{#3}%
	\endgroup
	% ignore following right page material, if any
	\let\startcdcaseright\gobblecdcaseright
	}

% Titles for sides of jewelcase
\def\jewelsidetitle#1{%
	\doifelse{\@@cdcframe}{\v!on}%
		{\let\localskip\grayrulesize}%
		{\let\localskip\z@}%
	\framed[\c!height=\jewelcaseside,\c!width=\@@cdcheight,%
		\c!strut=\v!no,\c!offset=\z@,%
		\c!frame=\v!off,\c!bottomframe=\v!on,%
		\c!framecolor=\grayrulecolor,\c!rulethickness=\grayrulesize]%
		% vskip corrects vertical centering for omitted frame edge
		{\vskip\localskip\relax\hbox to\jewelcaseheight{%
				\doif{\@@cdcsidelocation}{\v!left}{\quad}%
				\doif{\@@cdcsidelocation}{\v!right}{\hfill}%
				\domaintitle{#1\strut}%
				\doif{\@@cdcsidelocation}{\v!left}{\hfill}%
				\doif{\@@cdcsidelocation}{\v!right}{\quad}%
		}}}

% Define skipping of \startcdcaseright .. \stopcdcaseright
\def\gobblecdcaseright#1\stopcdcaseright{}

% Code for typesetting folded front case =======================================

\def\foldcase{\dosingleargument\dofoldcase}
\long\def\dofoldcase[#1]#2\startcdcase#3\stopcdcase\startcdcaseright#4\stopcdcaseright{%
	\begingroup
	\setupcdcases[\c!margin=\z@,#1,% space more cramped: default to 0mm margin
		\c!width=\foldcasewidth,\c!height=\foldcaseheight,%
		\c!casebefore={\setupframedtexts[\c!rightframe=\v!on]}]%
	\doifelse{\@@cdcframe}{\v!off}%
		{\let\localcolor\grayrulecolor\let\localrule\grayrulesize}%
		{\let\localcolor\@@cdcframecolor\let\localrule\@@cdcrulethickness}%
	\setupframedtexts[\c!rulethickness=\localrule,\c!framecolor=\localcolor,%
		\c!location=\v!middle]%
	% after \setupcdcases[#1,..] we now know the value of overlay
	\setcaseoverlay{\@@cdcoverlay}{\foldcasewidth}{\foldcaseheight}%
	% initialize the style, we now know its value
	\@@cdcstyle
	\cdcase{#2}{#3}%
	\let\startcdcaseright\empty
	\startframedtext[\c!leftframe=\v!on]%
	\setmaintitle
	#4\stopframedtext\endgroup}

% Code to setup the page dimensions for folded case
\def\setupfoldcasepage{\dosingleargument\dosetupfoldcasepage}
\def\dosetupfoldcasepage[#1]{%
	\doifelse{#1}{\v!final}%
		{\setuppapersize[CD][A4]\setuparranging[2SIDE,rotated]}%
		{\setuppapersize[CD][A4,landscape]\setuparranging[2SIDE]}%
	\setuplayout[\c!location=\v!middle,\c!marking=\v!on,% centered, cutmarks
		\c!backspace=\z@,%						% left edge at correct position
		\c!header=\z@,\c!footer=\z@,%			% collaps header and footer
		\c!width=\foldcasewidth,margin=\z@,%	% collaps left and right margin
		\c!height=\foldcaseheight,topspace=\z@]%% above text 5mm, below 5mm
	\setuppagenumbering[state=stop]}

% Code for typesetting folded dvd film case ====================================

% usage: \dvdfoldcase[options]{title}{genre}{picturefile/empty}
% \startcdcase ... \stopcdcase\startcdcaseright ... \stopcdcaseright
% typesets:
% left:                           right:
%            <title>                              <title>
%            <genre>                              <genre>
% \startcdcase                    \startcdcaseright
%              ...                                  ...
%              ...                                  ...
% \stopcdcase                     \stopcdcaseright
%
\def\dvdfoldcase{\dosingleargument\dodvdfoldcase}
\def\dodvdfoldcase[#1]#2#3#4{%
	\let\setmaintitle\dvdfoldtitle
	\def\FILMNAME{#2}%					% film name
	\def\FILMTYPE{#3}%      			% film genre
	\def\FILMPICT{#4}%      			% film picture
	\global\let\dvdpicture\empty
	\foldcase[frame=off,offset=5mm,%
		style=\ss,titlestyle=\tfd\bf,subtitlestyle=\tfb,%		
		#1,alternative=none]{\FILMNAME}%
	}

% typesetting of maintitle is expanded to title+genre+picture
\def\dvdfoldtitle{%
	\midaligned{\color[\@@cdctitlecolor]%
		{\@@cdctitlestyle\hfil\FILMNAME\hfil}}%
	\blank
	\midaligned{\color[\@@cdcsubtitlecolor]%
		{\@@cdcsubtitlestyle\hfil\FILMTYPE\hfil}}%
	\blank
	\ifx\dvdpicture\empty			% picture on second page
		\global\let\dvdpicture\FILMPICT
	\else
		\vfill\midaligned{\externalfigure[\dvdpicture]}\blank
		\global\let\dvdpicture\empty
	\fi}

% ==============================================================================
\protect
\endinput
