\definefontfeature
  [korean-base]
  [goodies=hanbatanglvt,
   colorscheme=default,
   mode=node,
   script=hang,
   language=kor]

\definefont[KoreanFont][hanbatanglvt*korean-base]

\starttext

\startMPdefinitions
    string KoreanColors[] ;

    KoreanColors[1] := "darkred" ;
    KoreanColors[2] := "darkgreen" ;
    KoreanColors[3] := "darkblue" ;
    KoreanColors[4] := "darkyellow" ;
    KoreanColors[5] := "darkgray" ;

    newinternal KoreanSplit ; KoreanSplit := -1 ;
    newinternal KoreanCode  ; KoreanCode  := -2 ;
    newinternal KoreanMode  ; KoreanMode  := KoreanSplit ;

    def KoreanOutline(expr txt) =
        picture p ; p := outlinetext.p(txt) ;
        numeric n ; n := 0 ;
        string old, new ; old := "" ;
        for i within p :
            if KoreanMode == KoreanSplit :
                n := n + 1 ;
            elseif KoreanMode == KoreanCode :
              new := prescriptpart i ;
              if new <> old :
                  old := new ;
                  n := n + 1 ;
              fi ;
            else :
                n := KoreanMode ;
            fi ;
            if unknown KoreanColors[n] :
                n := 1 ;
            fi ;
            draw pathpart i
                withpen pencircle scaled 1/10
                withcolor KoreanColors[n] ;
        endfor ;
    enddef ;

    def KoreanTest(expr txt) =
        KoreanMode  := KoreanSplit ; KoreanOutline(txt) ;
        currentpicture := currentpicture shifted (- xpart urcorner currentpicture, 0);
        KoreanMode  := KoreanCode ; KoreanOutline(txt) ;
        currentpicture := currentpicture shifted (- xpart urcorner currentpicture, 0);
        KoreanMode  := 3 ; KoreanOutline(txt) ;
    enddef ;

\stopMPdefinitions

% entered as three characters: ᄅ  ᅡ  ᆺ (mail collapses)

\startMPpage
    KoreanTest("\KoreanFont 랏") ;
\stopMPpage
\startMPpage
    KoreanTest("\KoreanFont 랏") ;
\stopMPpage
\startMPpage
    KoreanTest("\KoreanFont 논") ;
\stopMPpage
\startMPpage
    KoreanTest("\KoreanFont 왕") ;
\stopMPpage
\startMPpage
    KoreanTest("\KoreanFont 닭") ;
\stopMPpage
\startMPpage
    KoreanTest("\KoreanFont 박") ;
\stopMPpage
\startMPpage
    KoreanTest("\KoreanFont 서") ;
\stopMPpage

\stoptext
