%D \module
%D   [file=s-cgj.mkiv,
%D    version=2018.09.28,
%D    title=Context group style file,
%D    subtitle=CG-journal base style,
%D    author={Adrian Egger, W. Egger, Taco Hoekwater},
%D    date=\currentdate,
%D    copyright={Context Group}]

%D \type {\enablemode[draft]} has to come before loading the style. Maybe some
%D day I'll make something more official and then some \type {everydraft} or so.

% Fixme and todo ...

\startmodule[cgj]

\definecolor[NoteColor][g=1,r=.25,b=.25]
\definecolor[WarnColor][r=1,g=.25,b=.25]

\unexpanded\def\todo#1%
  {\startframedtext[background=color,backgroundcolor=NoteColor]
     \dontleavehmode\start\bf TODO:\stop~~#1\par
   \stopframedtext}

\unexpanded\def\fixme#1%
  {\startframedtext[background=color,backgroundcolor=WarnColor]
     \dontleavehmode\start\bf FIXME:\stop~~#1\par
   \stopframedtext }

%D Base set of variables. The actual values are set in the file
%D CG-journal.

\setvariables
  [CG-Journal]
  [Title={Journal},
   RunningTitle={Journal},
   SubTitle={From the base set of variables},
   Version=1.0,
   NOFColumns=2]

\startmode[onecolumn,fullwidth]
    \setvariables
      [CG-Journal]
      [NOFColumns=1]
\stopmode

% Fonts setup.

\usetypescriptfile[plex]

\starttypescript [cgj]
    \definetypeface [cgj] [rm] [serif][ibmplex-light] [default]
    \definetypeface [cgj] [ss] [sans] [ibmplex-light] [default]
    \definetypeface [cgj] [mm] [math] [palatino]      [default]
    \definetypeface [cgj] [tt] [mono] [ibmplex]       [default][rscale=0.9]
\stoptypescript

\starttypescript [cgj-light]
    \definetypeface [cgj-light] [rm] [serif][ibmplex-extralight] [default]
    \definetypeface [cgj-light] [ss] [sans] [ibmplex-extralight] [default]
    \definetypeface [cgj-light] [mm] [math] [palatino]           [default]
    \definetypeface [cgj-light] [tt] [mono] [ibmplex-extralight] [default][rscale=0.9]
\stoptypescript

\starttypescript [cgj-extralight]
    \definetypeface [cgj-extralight] [rm] [serif][ibmplex-thin] [default]
    \definetypeface [cgj-extralight] [ss] [sans] [ibmplex-thin] [default]
    \definetypeface [cgj-extralight] [mm] [math] [palatino]     [default]
    \definetypeface [cgj-extralight] [tt] [mono] [ibmplex-thin] [default][rscale=0.9]
\stoptypescript

\usebodyfont
  [cgj-light,cgj-extralight]

\setupbodyfont
  [cgj,ss,10pt]

\definebodyfontenvironment[10pt][interlinespace=13pt]
\definebodyfontenvironment[12pt][interlinespace=16pt]
\definebodyfontenvironment[16pt][interlinespace=20pt]

\setupinterlinespace
  [line=13pt]

\let\sl\it

%D Path to the logos

\setupexternalfigures
  [directory=./Logos,
   location={global,local,default}]

%D Logos: black logo only.

\useexternalfigure
  [Logo]
  [cg_corp_logo_loop_black_cmyk]
  [width=30mm,
   height=19mm]

%D Black logo with text

\useexternalfigure
  [LogoText]
  [cg_corp_logo_text_black_cmyk]
  [width=50mm,
   height=35.2mm]

%D Colors

\definecolor[CGlightblue][c=1,m=.15,y=0,k=0]
\definecolor[CGdeepblue] [c=1,m=.8,y=0,k=.3]
\definecolor[CGgray]     [c=0,m=0,y=0,k=.1]

%D Article styles

\definealternativestyle
   [Articleheading]
   [{\switchtobodyfont[16pt,ss]\bf}]

\definealternativestyle
   [Articlesubheading]
   [{\switchtobodyfont[16pt,ss]\tf}]

\definealternativestyle
   [Authorname]
   [{\switchtobodyfont[12pt,ss]\it}]

\definealternativestyle
   [Sectionheading]
   [{\switchtobodyfont[12pt,ss]\bf\setupinterlinespace[line=12pt]}]

\definealternativestyle
   [Subsectionheading]
   [{\switchtobodyfont[cgj-light,10pt,ss]\bf\setupinterlinespace[line=10pt]}]

\definealternativestyle
   [IntroCopy]
   [{\switchtobodyfont[12pt,ss]\tf}]

\definealternativestyle
   [PagenumberStyle]
   [{\switchtobodyfont[cgj-light,10pt,ss]\bf}]

%D Headerstyles:Breadcrumbs

\definealternativestyle
   [BreadcrumbMd]
   [{\switchtobodyfont[cgj-light,10pt,ss]\bf}]

\definealternativestyle
   [BreadcrumbRg]
   [{\switchtobodyfont[cgj,10pt,ss]}]

\definealternativestyle
   [BreadcrumbLt]
   [{\switchtobodyfont[10pt,ss]\tf}]

\definealternativestyle
   [BreadcrumbTh]
   [{\switchtobodyfont[cgj-light,10pt,ss]\tf}]

%D Captionstyles

\definealternativestyle
   [Captionheading]
   [{\switchtobodyfont[8pt,ss]\bf\setupinterlinespace[line=8pt]}]

\definealternativestyle
   [Captiontext]
   [{\switchtobodyfont[8pt,ss]\tf\setupinterlinespace[line=8pt]}]

%D Article signature

\definealternativestyle
   [Signaturestyle]
   [{\switchtobodyfont[10pt,ss]\it}]

%D Article footnotes

\definealternativestyle
   [Articlefootnotes]
   [{\switchtobodyfont[8pt,ss]\tf}]

%D Index and TOC styles

\definealternativestyle
   [IndexContents]
   [{\switchtobodyfont[16pt,ss]\bf\setupinterlinespace[line=18pt]}]

\definealternativestyle
   [IndexArticleTitle]
   [{\switchtobodyfont[12pt,ss]\tf\setupinterlinespace[line=20pt]}]

\definealternativestyle
   [IndexNumber]
   [{\switchtobodyfont[12pt,ss]\bf\setupinterlinespace[line=20pt]}]

\definealternativestyle
   [IndexAuthor]
   [{\switchtobodyfont[12pt,ss]\it\setupinterlinespace[line=20pt]}]

%D Math: still missing.

%D Verbatim

\definealternativestyle
   [DisplayMonospaced]
   [\tt]

\definealternativestyle
   [DisplayMonospacedX]
   [{\switchtobodyfont[8pt,tt]}]

\definealternativestyle
   [DisplayMonospacedS]
   [{\switchtobodyfont[9pt,tt]\setupinterlinespace[line=10pt]}]

%D Blank adjustment

\defineblank[CGblank]       [6pt]
\defineblank[BigCGblank]   [24pt]
\defineblank[MediumCGblank][12pt]

%D Paper definition

\definepapersize
  [Journal]
  [width=210mm,
   height=266mm]

\setuppapersize
  [Journal]
  [Journal]

%D General layout

\definelayout
  [General]
  [topspace=18mm,
   backspace=28mm,
   header=5mm,
   headerdistance=7mm,
   footer=5mm,
   footerdistance=5mm,
   width=157mm,
   height=224mm,
   marking=on]

\definelayout
  [Content]
  [topspace=18mm,
   backspace=28mm,
   header=5mm,
   headerdistance=7mm,
   footer=0mm,
   footerdistance=0mm,
   width=157mm,
   height=224mm,
   margindistance=4mm,
   rightmargin=21mm,
   leftmargin=21mm,
   marking=on]

\definelayout
  [Imprint]
  [topspace=18mm,
   backspace=28mm,
   header=5mm,
   headerdistance=7mm,
   footer=5mm,
   footerdistance=5mm,
   width=107mm,
   height=224mm,
   margindistance=4mm,
   rightmargin=45.3mm,
   leftmargin=21mm,
   marking=on]

\definelayout
  [SingleColumn]
  [topspace=18mm,
   backspace=55mm,
   header=5mm,
   headerdistance=7mm,
   footer=5mm,
   footerdistance=5mm,
   width=130mm,
   height=224mm,
   margindistance=4mm,
   leftmargin=48mm,
   rightmargin=21mm,
   marking=on]

%D Pagenumbering: Pagenumber is set in the footer

\setuppagenumbering
  [location=,
   alternative=doublesided]

%D Head-definitions

\setuphead
  [title]
  [style=Articleheading,
   after={\blank[BigCGblank]},
   page=yes]

\setuphead
  [part]
  [placehead=no,
   after=,
   before=,
   page=no,
   numbercommand=,
   expansion=yes]

\setuphead
  [section]
  [style=Sectionheading,
   before={\blank[BigCGblank]},
   after={\blank[CGblank]},
   sectionstopper={.},
   distance=4pt,
   align={flushleft,nothyphenated},
   resetnumber=yes,
   continue=yes]

\setuphead
  [subsection]
  [style=Subsectionheading,
   before={\blank[CGblank]},
   after={\blank[CGblank]},
   sectionstopper=,
   distance=4pt,
   align={flushleft,nothyphenated},
   resetnumber=yes,
   continue=yes]

\setuphead
  [subject]
  [style=Sectionheading,
   before={\blank[BigCGblank]},
   after=]

\setuphead
  [subsubject]
  [style=Subsectionheading,
   before={\blank[CGblank]},
   after={\blank[CGblank]},
   sectionstopper=]

%D A special head for the footnote section in multicolumn mode excuse the low-level
%D rule, I wanted it to look like the start of footnotes in single column mode. (TH)

\definehead
  [footnotesubject]
  [subject]

\setuphead
  [footnotesubject]
  [style=Sectionheading,
   before={\kern -13pt},
   after={\smash{\lower 13pt\hbox{\vrule width 2.5cm height 1pt depth 0pt}}}]


%D The setup of the columns is done at the moment columns are started (see end
%D of file)

%D Index/TOC page setups

\definelistalternative
  [CGJ:Index]
  [renderingsetup=CGJ:Indexheading]

\definelistalternative
  [CGJ:Index:Chapter]
  [renderingsetup=CGJ:Indexheading:Chapter]

\startsetups[CGJ:Indexheading:Chapter]
    \listparameter{before}
    \vbox \listboxproperties{all} {
        \forgetall
        \dontleavehmode
        \listrenderingsynchronize
        \hbox to 15mm{
        \bgroup
            \useliststyleandcolor{numberstyle}{numbercolor}
            \currentlistentrypagenumber
        \egroup}
        \bgroup
            \useliststyleandcolor{textstyle}{textcolor}
            \smash{\currentlistentrytitle}%
        \egroup
    }
    \par
    \listparameter{after}
\stopsetups

\startsetups[CGJ:Indexheading]
   \listparameter{before}
   \vbox \listboxproperties{all} {
       \forgetall
       \dontleavehmode
       \listrenderingsynchronize
       \hbox to 15mm{
       \bgroup
           \useliststyleandcolor{numberstyle}{numbercolor}
           \currentlistentrypagenumber
       \egroup}
       \bgroup
           \useliststyleandcolor{textstyle}{textcolor}
           \currentlistentrytitle
       \egroup
       \par
   }
   \par
   \listparameter{after}
\stopsetups

\setuplist
  [part]
  [before={\blank[CGblank]},
   after=,
   style=IndexContents,
   numberstyle=\IndexNumber,
   textstyle=\IndexArticleTitle,
   prefix=no,
   alternative=CGJ:Index:Chapter]

%D Captions

\setupcaptions
  [suffix={.},
   headstyle=\Captionheading,
   style=\Captiontext,
   distance=6pt]

\setupcaption[figure][way=bypart]
\setupcaption[table] [way=bypart]

%D Datacollection: article parameters (other fields and defaults)

\unexpanded\def\CGJBibData[#1]%
  {\getparameters
     [CGJ]
     [SubTitle=,
      RunningAuthor=,
      RunningTitle=Example,
      Email=,
      Address=,
      Page=1,
      Title={My Article},
      Author={Example Author},
      Period=,
      Number=,
      Year=,
      TocAuthor=,
      TocTitle=,
      #1]%
   \doifnothing
      {\CGJRunningTitle}
      {\let\CGJRunningTitle\CGJTitle}%
   \doifnothing
      {\CGJRunningAuthor}
      {\let\CGJRunningAuthor\CGJAuthor}%
   \doifnothing
      {\CGJTocAuthor}
      {\let\CGJTocAuthor\CGJAuthor}%
   \doifnothing
      {\CGJTocTitle}
      {\let\CGJTocTitle\CGJTitle}%
   \setvariables
     [CGJToc]
     [Author=\CGJTocAuthor,
      Title=\CGJTocTitle]% for TOC
  }

\unexpanded\def\dostartArticle[#1]
  {\CGJBibData[#1]
   \doifelse
     {\getvariable{CG-Journal}{NOFColumns}}
     {1}
     {\doifmodeelse{fullwidth}
        {\setuplayout[General]}
        {\setuplayout[SingleColumn]}}
     {\setupnotes[location=none]
      \setuplayout[General]}%
   \bgroup
     {\switchtobodyfont[16pt]\Articleheading\CGJTitle\par}
     {\doifsomething{\CGJSubTitle}
        {\switchtobodyfont[16pt]\Articlesubheading\CGJSubTitle\par}}
     {\doifsomething{\CGJAuthor}
        {\switchtobodyfont[12pt]\Authorname \CGJAuthor}\par}
     \part
       {\getvariable{CGJToc}{Title}%
        \doifsomething{\getvariable{CGJToc}{Author}}{ \emdash\ }%
        \IndexAuthor\getvariable{CGJToc}{Author}}
     \godown[13pt]%
   \egroup
   \par
   \hyphenpenalty1000\relax}

\unexpanded\def\startArticle
  {\dosingleempty\dostartArticle}

\def\signArticle
  {}

%D In multicolumn mode, footnotes come are at the end of the article:

\startsetups article:after
    \startfootnotesubject[title=]
        \placefootnotes
    \stopfootnotesubject
\stopsetups

\unexpanded\def\stopArticle
  {\doifelse{\getvariable{CG-Journal}{NOFColumns}}{1}
     {\par
      \signArticle}
     {\setups{article:after}
      \par
      \signArticle
      \stoppagecolumns}
   \page}

\unexpanded\def\startAbstract
  {\bgroup
   \switchtobodyfont[12pt]
   \IntroCopy}

\unexpanded\def\stopAbstract
  {\par
   \egroup
   \finishAbstract}

\unexpanded\def\finishAbstract
  {\doifelse {\getvariable{CG-Journal}{NOFColumns}} {1}
     {\blank[BigCGblank]}
     {\vbox{\blank[BigCGblank]}%
      \par
      \startpagecolumns[balance=yes,distance=12pt,page=no,n=\getvariable{CG-Journal}{NOFColumns}]
      \setupitemgroup[itemize][packed]
      \setuplayout[grid=yes]} % grid mode only in columns
  }


\unexpanded\def\noAbstract
  {\kern -24pt
   \finishAbstract}

%D Headertexts

\startsetups[Header:texts]
    \setupheadertexts
        [] [{{\BreadcrumbLt\CGJRunningTitle}{\BreadcrumbTh\doifsomething{\CGJRunningAuthor}{\ > }\CGJRunningAuthor}}] %odd
        [{\BreadcrumbMd contextgroup}{\BreadcrumbRg\ > \getvariable{CG-Journal}{RunningTitle}}] [] %even
\stopsetups

%D Footertexts

\startsetups[Footer:texts]
  \setupfootertexts
    [] [\PagenumberStyle\pagenumber]
    [\PagenumberStyle\pagenumber] []
\stopsetups

%D Setup tolerance, stretch.

\setuptolerance
  [tolerant,stretch]

%D Setting up footnotes.

\setupnotation
  [footnote]
  [way=bypart,
  rule=on]

%D Adjustments to the container containing the footnotes.

\setupnote
  [footnote]
  [frame=off,
   topframe=off ,
   framecolor=black,
   background=,
   rulecolor=black,    %% the line above the inserts
   rulethickness=1pt,
   next={ },
   split=verystrict,
   scope=page,
   style=Articlefootnotes]

\doifmodeelse {draft} {\setupnote[footnote][frame=on]}

% Setup typing.

\setupnarrower
  [middle=3mm,
   left=5mm]

\definetextbackground
  [verbatim]
  [frame=off,
   background=color,
   backgroundcolor=CGgray,
   leftoffset=2mm,rightoffset=2mm,
   topoffset=2mm,bottomoffset=2mm,
   location=paragraph,
   align=flushleft]

\doifmodeelse {draft} {\definetextbackground[verbatim][frame=on]}

\definetextbackground
  [verbatimitem]
  [verbatim]

\setuptextbackground
  [verbatimitem]
  [before=,
   after=,
   width=\the\dimexpr\hsize-10.5mm\relax]

\startsetups typing:before
    \blank[MediumCGblank]
    \starttextbackground[verbatim]
\stopsetups

\startsetups typing:before:small
    \blank[MediumCGblank]
    \starttextbackground[verbatim]
\stopsetups

\startsetups typing:after
    \stoptextbackground
    \blank[MediumCGblank]
\stopsetups

\startsetups typing:before:item
    \starttextbackground[verbatimitem]
\stopsetups

\startsetups typing:after:item
    \stoptextbackground
\stopsetups

\setuptyping
  [typing]
  [style=DisplayMonospaced,
   before=\setups{typing:before},
   after=\setups{typing:after}]

\setuptype
  [style=DisplayMonospaced]

%D When verbatim becomes too large:

\definetyping
  [smalltyping]

\setuptyping
  [smalltyping]
  [style=DisplayMonospacedX,
   before=\setups{typing:before:small},
   after=\setups{typing:after}]

\unexpanded\def\smalltypefile{\typefile[smalltyping][]}

%D Bullet lists.

\setupitemgroup
  [itemize]
  [each]
  []
  [symbol=1,
   indentnext=no,
   align=right,
   before={\blank[MediumCGblank]\startnarrower[left]},
   after={\stopnarrower\blank[MediumCGblank]},
   inbetween={\blank[CGblank]}]

\definedescription
  [description]
  [location=hanging,
   width=broad,
   before={\blank},
   after={\blank}]

\setupformula
  [align=flushleft,
   margin=5mm]

\setupquotation
  [before={\blank[CGblank]\switchtobodyfont[10pt]},
   after={\blank[CGblank]}]

\setupwhitespace
  [medium]

\usemodule[abr-02,abr-03,abr-04]

\unexpanded\def\Href#1{\underbar{\hyphenatedurl{#1}}}
\unexpanded\def\CG    {\ConTeXt\ group}

\setnewconstant\kindofpagetextareas\plusone % partial page. HH: low level, no high level switch (yet)

%D There is a draft mode, which enables all frames:

\doifmode {draft} {\showframe}

\stopmodule

