/* Pango
 * hangul-utils.c:
 *
 * Copyright (C) 2002 Jungshik Shin
 * Author: Jungshik Shin <jshin@mailaps.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <string.h>

#include "hangul-defs.h"
#include "hangul-utils.h"
#include "tables-jamos2.i"

/*
 * search for cluster->seq in 'in' and replace it with cluster->liga in place. 
 * returns the difference in length between before and after the replacement.
 */

int 
jamo_srch_repl(__jamo_norm_map *cluster, gunichar *in, int *len)
{
  int i,j;
  gboolean matched=FALSE;
  int mstart=0;
  int mlen=0;

  for (i=0; i<*len; i++)
  {
    matched=TRUE;
    for (j=0; i+j < *len && j<MAX_BAS_JAMOS && cluster->seq[j] ;j++)
      if ( in[i+j] != cluster->seq[j] ) 
      {
        matched=FALSE;
        break;
      }
    if ( i+j==*len && j<MAX_BAS_JAMOS && cluster->seq[j] )
      matched=FALSE;
    if (matched) 
    {
      mstart=i;
      mlen=j;
      break;
    }
  }

  if ( !matched ) return 0;

  /* replace the match with liga */
  in[mstart]=cluster->liga; 
  /* shift the rest of in[] by mlen-1 */
  for (i=mstart+mlen; i<*len; i++)
    in[i-mlen+1]=in[i];

  *len-= (mlen - 1);
  return mlen-1;
}


/*
 *  1. Normalize (regularize) a jamo sequence to the regular
 *     syllable form defined in Unicode 3.2 section 3.11 to the extent
 *     that it's useful in rendering by render_func's().
 *
 *  2. Replace a compatibly decomposed Jamo sequence (unicode 2.0 
 *     definition) with a 'precomposed' Jamo cluster (with codepoint
 *     of its own in U+1100 block). For instance, a seq.
 *     of U+1100, U+1100 is replaced by U+1101. It actually
 *     more than Unicode 2.0 decomposition map suggests.
 *     For a Jamo cluster made up of three basic Jamos
 *     (e.g. U+1133 : Sios, Piup, Kiyeok), not only
 *      a sequence of Sios(U+1109), Piup(U+1107) and 
 *     Kiyeok(U+1100) but also two more sequences,
 *     {U+1132(Sios-Pieup), U+1100(Kiyeok) and {Sios(U+1109),
 *      U+111E(Piup-Kiyeok)} are mapped to U+1133.
 *
 *  3. the result is returned in a newly malloced(g_new'd)
 *     gunichar*. A calling function has to g_free it.
 */


gunichar* normalize_jamo(const gunichar* in, int *len)
{
  gunichar *med;
  int i;

  med = g_new(gunichar, *len + 2); /* make a room for Lf and Vf */
  memcpy(med,in, sizeof(gunichar) * *len);


	/* remove Lf in LfL sequence that may occur in an interim cluster during
	 * a simple Xkb-based input. 
	 */
	if (med[0] == LFILL && *len > 1 && IS_L(med[1]))
	  {
      memmove(med,med+1,sizeof(gunichar) * *len-1);
      (*len)--;
    }
  /* LV or LVT : no need to search for and replace jamo sequences */
  if ((*len == 2 && IS_L(med[0]) && IS_V(med[1])) || 
      (*len == 3 && IS_L(med[0]) && IS_V(med[1]) && IS_T(med[2])))
    return med;
    

  if (*len > 1)
    for (i=0; __u1100_jamo_clusters[i].seq[0]; i++)
      jamo_srch_repl(&__u1100_jamo_clusters[i],med,len);

/* XXX:  should insert Vf  between L+ and T+ in L+T+ */

#if 0
  if (IS_L(med[*len - 1]))
      med[*len] = VFILL;
  else if (IS_V(med[0])) 
#endif
  /* prepend a leading V with Lf */
  if (IS_V(med[0])) 
    {
      memmove(med+1,med,sizeof(gunichar) * *len);
      med[0] = LFILL;
      (*len)++;
    }
  /* prepend a leading T with LfVf */
  else if (IS_T(med[0])) 
    {
      memmove(med+2,med,sizeof(gunichar) * *len);
      med[0] = LFILL;
      med[1] = VFILL;
      *len += 2;
    }
  return med;
}
