/* 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 <stdlib.h>

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

/* _jamo_norm_map_comp () :
 * the comparison function used to bsearch() for a Jamo sequence
 * in an array of JamoNormalizationMap type. In hangul-xft.c,
 * it's aliased to 'oj_ns_comp()'.  
 */

int
_jamo_norm_map_comp (const void *p1, const void *p2)
{
  if (((JamoNormalizationMap *) p1) -> seq[0] != 
      ((JamoNormalizationMap *) p2) -> seq[0])
    return (((JamoNormalizationMap *) p1) -> seq[0] - 
	    ((JamoNormalizationMap *) p2) -> seq[0]);
  else if (((JamoNormalizationMap *) p1) -> seq[1] != 
           ((JamoNormalizationMap *) p2) -> seq[1])
    return (((JamoNormalizationMap *) p1) -> seq[1] - 
	    ((JamoNormalizationMap *) p2) -> seq[1]);
  else 
    return (((JamoNormalizationMap *) p1) -> seq[2] - 
	    ((JamoNormalizationMap *) p2) -> seq[2]);
}

/*
 * look up cluster array for all possible cluster_len-long Jamo sequences 
 * in 'in' and  replace it with match->liga in place if match is found.
 * returns the difference in length between before and after the replacement.
 * XXX : 1. Do we need caching here? 
 *       2. Is it worth separating L,V,T clusters from each other
 *          and treat them as such in 'in' buffer? 
 */

int 
_jamo_srch_repl (const JamoNormalizationMap *cluster, size_t clusters_size, 
    		 gunichar *in, int *len, int cluster_len)
{
  int i, j;
  JamoNormalizationMap key, *match;
  int len0 = *len;

  for (i = 0; i <= *len - cluster_len; i++)
  {
    key.seq[0] = in[i];
    key.seq[1] = in[i + 1];
    key.seq[2] = cluster_len == 3 ? in[i + 2] : 0;
    match = bsearch (&key, cluster, clusters_size,
		     sizeof (key), &_jamo_norm_map_comp);

    if (match)
    {
      in[i] = match->liga;
      for (j = i + cluster_len ; j < *len; j++)
        in[j - cluster_len + 1] = in[j];
      *len -= (cluster_len - 1);
    }
  }

  return len0 - *len;
}

/*
 *  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;

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

	/* 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, (*len - 1) * sizeof (gunichar));
      (*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)
    {
      _jamo_srch_repl (__jamo_clusters_group1, 
	  	       G_N_ELEMENTS (__jamo_clusters_group1), med, len, 3);
      _jamo_srch_repl (__jamo_clusters_group234, 
	  	       G_N_ELEMENTS (__jamo_clusters_group234), med, len, 2);
    }

/* 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, *len * sizeof (gunichar));
      med[0] = LFILL;
      (*len)++;
    }
  /* prepend a leading T with LfVf */
  else if (IS_T (med[0])) 
    {
      memmove (med + 2, med, *len * sizeof(gunichar));
      med[0] = LFILL;
      med[1] = VFILL;
      *len += 2;
    }
  return med;
}
