Blame src/hb-ot-shape-complex-hangul.cc

Packit 874993
/*
Packit 874993
 * Copyright © 2013  Google, Inc.
Packit 874993
 *
Packit 874993
 *  This is part of HarfBuzz, a text shaping library.
Packit 874993
 *
Packit 874993
 * Permission is hereby granted, without written agreement and without
Packit 874993
 * license or royalty fees, to use, copy, modify, and distribute this
Packit 874993
 * software and its documentation for any purpose, provided that the
Packit 874993
 * above copyright notice and the following two paragraphs appear in
Packit 874993
 * all copies of this software.
Packit 874993
 *
Packit 874993
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
Packit 874993
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
Packit 874993
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
Packit 874993
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
Packit 874993
 * DAMAGE.
Packit 874993
 *
Packit 874993
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
Packit 874993
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
Packit 874993
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
Packit 874993
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
Packit 874993
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Packit 874993
 *
Packit 874993
 * Google Author(s): Behdad Esfahbod
Packit 874993
 */
Packit 874993
Packit 874993
#include "hb-ot-shape-complex-private.hh"
Packit 874993
Packit 874993
Packit 874993
/* Hangul shaper */
Packit 874993
Packit 874993
Packit 874993
/* Same order as the feature array below */
Packit 874993
enum {
Packit 874993
  _JMO,
Packit 874993
Packit 874993
  LJMO,
Packit 874993
  VJMO,
Packit 874993
  TJMO,
Packit 874993
Packit 874993
  FIRST_HANGUL_FEATURE = LJMO,
Packit 874993
  HANGUL_FEATURE_COUNT = TJMO + 1
Packit 874993
};
Packit 874993
Packit 874993
static const hb_tag_t hangul_features[HANGUL_FEATURE_COUNT] =
Packit 874993
{
Packit 874993
  HB_TAG_NONE,
Packit 874993
  HB_TAG('l','j','m','o'),
Packit 874993
  HB_TAG('v','j','m','o'),
Packit 874993
  HB_TAG('t','j','m','o')
Packit 874993
};
Packit 874993
Packit 874993
static void
Packit 874993
collect_features_hangul (hb_ot_shape_planner_t *plan)
Packit 874993
{
Packit 874993
  hb_ot_map_builder_t *map = &plan->map;
Packit 874993
Packit 874993
  for (unsigned int i = FIRST_HANGUL_FEATURE; i < HANGUL_FEATURE_COUNT; i++)
Packit 874993
    map->add_feature (hangul_features[i], 1, F_NONE);
Packit 874993
}
Packit 874993
Packit 874993
static void
Packit 874993
override_features_hangul (hb_ot_shape_planner_t *plan)
Packit 874993
{
Packit 874993
  /* Uniscribe does not apply 'calt' for Hangul, and certain fonts
Packit 874993
   * (Noto Sans CJK, Source Sans Han, etc) apply all of jamo lookups
Packit 874993
   * in calt, which is not desirable. */
Packit 874993
  plan->map.add_feature (HB_TAG('c','a','l','t'), 0, F_GLOBAL);
Packit 874993
}
Packit 874993
Packit 874993
struct hangul_shape_plan_t
Packit 874993
{
Packit 874993
  ASSERT_POD ();
Packit 874993
Packit 874993
  hb_mask_t mask_array[HANGUL_FEATURE_COUNT];
Packit 874993
};
Packit 874993
Packit 874993
static void *
Packit 874993
data_create_hangul (const hb_ot_shape_plan_t *plan)
Packit 874993
{
Packit 874993
  hangul_shape_plan_t *hangul_plan = (hangul_shape_plan_t *) calloc (1, sizeof (hangul_shape_plan_t));
Packit 874993
  if (unlikely (!hangul_plan))
Packit 874993
    return NULL;
Packit 874993
Packit 874993
  for (unsigned int i = 0; i < HANGUL_FEATURE_COUNT; i++)
Packit 874993
    hangul_plan->mask_array[i] = plan->map.get_1_mask (hangul_features[i]);
Packit 874993
Packit 874993
  return hangul_plan;
Packit 874993
}
Packit 874993
Packit 874993
static void
Packit 874993
data_destroy_hangul (void *data)
Packit 874993
{
Packit 874993
  free (data);
Packit 874993
}
Packit 874993
Packit 874993
/* Constants for algorithmic hangul syllable [de]composition. */
Packit 874993
#define LBase 0x1100u
Packit 874993
#define VBase 0x1161u
Packit 874993
#define TBase 0x11A7u
Packit 874993
#define LCount 19u
Packit 874993
#define VCount 21u
Packit 874993
#define TCount 28u
Packit 874993
#define SBase 0xAC00u
Packit 874993
#define NCount (VCount * TCount)
Packit 874993
#define SCount (LCount * NCount)
Packit 874993
Packit 874993
#define isCombiningL(u) (hb_in_range<hb_codepoint_t> ((u), LBase, LBase+LCount-1))
Packit 874993
#define isCombiningV(u) (hb_in_range<hb_codepoint_t> ((u), VBase, VBase+VCount-1))
Packit 874993
#define isCombiningT(u) (hb_in_range<hb_codepoint_t> ((u), TBase+1, TBase+TCount-1))
Packit 874993
#define isCombinedS(u) (hb_in_range<hb_codepoint_t> ((u), SBase, SBase+SCount-1))
Packit 874993
Packit 874993
#define isL(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1100u, 0x115Fu, 0xA960u, 0xA97Cu))
Packit 874993
#define isV(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x1160u, 0x11A7u, 0xD7B0u, 0xD7C6u))
Packit 874993
#define isT(u) (hb_in_ranges<hb_codepoint_t> ((u), 0x11A8u, 0x11FFu, 0xD7CBu, 0xD7FBu))
Packit 874993
Packit 874993
#define isHangulTone(u) (hb_in_range<hb_codepoint_t> ((u), 0x302Eu, 0x302Fu))
Packit 874993
Packit 874993
/* buffer var allocations */
Packit 874993
#define hangul_shaping_feature() complex_var_u8_0() /* hangul jamo shaping feature */
Packit 874993
Packit 874993
static bool
Packit 874993
is_zero_width_char (hb_font_t *font,
Packit 874993
		    hb_codepoint_t unicode)
Packit 874993
{
Packit 874993
  hb_codepoint_t glyph;
Packit 874993
  return hb_font_get_glyph (font, unicode, 0, &glyph) && hb_font_get_glyph_h_advance (font, glyph) == 0;
Packit 874993
}
Packit 874993
Packit 874993
static void
Packit 874993
preprocess_text_hangul (const hb_ot_shape_plan_t *plan,
Packit 874993
			hb_buffer_t              *buffer,
Packit 874993
			hb_font_t                *font)
Packit 874993
{
Packit 874993
  HB_BUFFER_ALLOCATE_VAR (buffer, hangul_shaping_feature);
Packit 874993
Packit 874993
  /* Hangul syllables come in two shapes: LV, and LVT.  Of those:
Packit 874993
   *
Packit 874993
   *   - LV can be precomposed, or decomposed.  Lets call those
Packit 874993
   *     <LV> and <L,V>,
Packit 874993
   *   - LVT can be fully precomposed, partically precomposed, or
Packit 874993
   *     fully decomposed.  Ie. <LVT>, <LV,T>, or <L,V,T>.
Packit 874993
   *
Packit 874993
   * The composition / decomposition is mechanical.  However, not
Packit 874993
   * all <L,V> sequences compose, and not all <LV,T> sequences
Packit 874993
   * compose.
Packit 874993
   *
Packit 874993
   * Here are the specifics:
Packit 874993
   *
Packit 874993
   *   - <L>: U+1100..115F, U+A960..A97F
Packit 874993
   *   - <V>: U+1160..11A7, U+D7B0..D7C7
Packit 874993
   *   - <T>: U+11A8..11FF, U+D7CB..D7FB
Packit 874993
   *
Packit 874993
   *   - Only the <L,V> sequences for the 11xx ranges combine.
Packit 874993
   *   - Only <LV,T> sequences for T in U+11A8..11C3 combine.
Packit 874993
   *
Packit 874993
   * Here is what we want to accomplish in this shaper:
Packit 874993
   *
Packit 874993
   *   - If the whole syllable can be precomposed, do that,
Packit 874993
   *   - Otherwise, fully decompose and apply ljmo/vjmo/tjmo features.
Packit 874993
   *   - If a valid syllable is followed by a Hangul tone mark, reorder the tone
Packit 874993
   *     mark to precede the whole syllable - unless it is a zero-width glyph, in
Packit 874993
   *     which case we leave it untouched, assuming it's designed to overstrike.
Packit 874993
   *
Packit 874993
   * That is, of the different possible syllables:
Packit 874993
   *
Packit 874993
   *   <L>
Packit 874993
   *   <L,V>
Packit 874993
   *   <L,V,T>
Packit 874993
   *   <LV>
Packit 874993
   *   <LVT>
Packit 874993
   *   <LV, T>
Packit 874993
   *
Packit 874993
   * - <L> needs no work.
Packit 874993
   *
Packit 874993
   * - <LV> and <LVT> can stay the way they are if the font supports them, otherwise we
Packit 874993
   *   should fully decompose them if font supports.
Packit 874993
   *
Packit 874993
   * - <L,V> and <L,V,T> we should compose if the whole thing can be composed.
Packit 874993
   *
Packit 874993
   * - <LV,T> we should compose if the whole thing can be composed, otherwise we should
Packit 874993
   *   decompose.
Packit 874993
   */
Packit 874993
Packit 874993
  buffer->clear_output ();
Packit 874993
  unsigned int start = 0, end = 0; /* Extent of most recently seen syllable;
Packit 874993
				    * valid only if start < end
Packit 874993
				    */
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
Packit 874993
  for (buffer->idx = 0; buffer->idx < count && !buffer->in_error;)
Packit 874993
  {
Packit 874993
    hb_codepoint_t u = buffer->cur().codepoint;
Packit 874993
Packit 874993
    if (isHangulTone (u))
Packit 874993
    {
Packit 874993
      /*
Packit 874993
       * We could cache the width of the tone marks and the existence of dotted-circle,
Packit 874993
       * but the use of the Hangul tone mark characters seems to be rare enough that
Packit 874993
       * I didn't bother for now.
Packit 874993
       */
Packit 874993
      if (start < end && end == buffer->out_len)
Packit 874993
      {
Packit 874993
	/* Tone mark follows a valid syllable; move it in front, unless it's zero width. */
Packit 874993
	buffer->next_glyph ();
Packit 874993
	if (!is_zero_width_char (font, u))
Packit 874993
	{
Packit 874993
	  buffer->merge_out_clusters (start, end + 1);
Packit 874993
	  hb_glyph_info_t *info = buffer->out_info;
Packit 874993
	  hb_glyph_info_t tone = info[end];
Packit 874993
	  memmove (&info[start + 1], &info[start], (end - start) * sizeof (hb_glyph_info_t));
Packit 874993
	  info[start] = tone;
Packit 874993
	}
Packit 874993
      }
Packit 874993
      else
Packit 874993
      {
Packit 874993
	/* No valid syllable as base for tone mark; try to insert dotted circle. */
Packit 874993
	if (font->has_glyph (0x25CCu))
Packit 874993
	{
Packit 874993
	  hb_codepoint_t chars[2];
Packit 874993
	  if (!is_zero_width_char (font, u)) {
Packit 874993
	    chars[0] = u;
Packit 874993
	    chars[1] = 0x25CCu;
Packit 874993
	  } else {
Packit 874993
	    chars[0] = 0x25CCu;
Packit 874993
	    chars[1] = u;
Packit 874993
	  }
Packit 874993
	  buffer->replace_glyphs (1, 2, chars);
Packit 874993
	}
Packit 874993
	else
Packit 874993
	{
Packit 874993
	  /* No dotted circle available in the font; just leave tone mark untouched. */
Packit 874993
	  buffer->next_glyph ();
Packit 874993
	}
Packit 874993
      }
Packit 874993
      start = end = buffer->out_len;
Packit 874993
      continue;
Packit 874993
    }
Packit 874993
Packit 874993
    start = buffer->out_len; /* Remember current position as a potential syllable start;
Packit 874993
			      * will only be used if we set end to a later position.
Packit 874993
			      */
Packit 874993
Packit 874993
    if (isL (u) && buffer->idx + 1 < count)
Packit 874993
    {
Packit 874993
      hb_codepoint_t l = u;
Packit 874993
      hb_codepoint_t v = buffer->cur(+1).codepoint;
Packit 874993
      if (isV (v))
Packit 874993
      {
Packit 874993
	/* Have <L,V> or <L,V,T>. */
Packit 874993
	hb_codepoint_t t = 0;
Packit 874993
	unsigned int tindex = 0;
Packit 874993
	if (buffer->idx + 2 < count)
Packit 874993
	{
Packit 874993
	  t = buffer->cur(+2).codepoint;
Packit 874993
	  if (isT (t))
Packit 874993
	    tindex = t - TBase; /* Only used if isCombiningT (t); otherwise invalid. */
Packit 874993
	  else
Packit 874993
	    t = 0; /* The next character was not a trailing jamo. */
Packit 874993
	}
Packit 874993
Packit 874993
	/* We've got a syllable <L,V,T?>; see if it can potentially be composed. */
Packit 874993
	if (isCombiningL (l) && isCombiningV (v) && (t == 0 || isCombiningT (t)))
Packit 874993
	{
Packit 874993
	  /* Try to compose; if this succeeds, end is set to start+1. */
Packit 874993
	  hb_codepoint_t s = SBase + (l - LBase) * NCount + (v - VBase) * TCount + tindex;
Packit 874993
	  if (font->has_glyph (s))
Packit 874993
	  {
Packit 874993
	    buffer->replace_glyphs (t ? 3 : 2, 1, &s);
Packit 874993
	    if (unlikely (buffer->in_error))
Packit 874993
	      return;
Packit 874993
	    end = start + 1;
Packit 874993
	    continue;
Packit 874993
	  }
Packit 874993
	}
Packit 874993
Packit 874993
	/* We didn't compose, either because it's an Old Hangul syllable without a
Packit 874993
	 * precomposed character in Unicode, or because the font didn't support the
Packit 874993
	 * necessary precomposed glyph.
Packit 874993
	 * Set jamo features on the individual glyphs, and advance past them.
Packit 874993
	 */
Packit 874993
	buffer->cur().hangul_shaping_feature() = LJMO;
Packit 874993
	buffer->next_glyph ();
Packit 874993
	buffer->cur().hangul_shaping_feature() = VJMO;
Packit 874993
	buffer->next_glyph ();
Packit 874993
	if (t)
Packit 874993
	{
Packit 874993
	  buffer->cur().hangul_shaping_feature() = TJMO;
Packit 874993
	  buffer->next_glyph ();
Packit 874993
	  end = start + 3;
Packit 874993
	}
Packit 874993
	else
Packit 874993
	  end = start + 2;
Packit 874993
	if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
Packit 874993
	  buffer->merge_out_clusters (start, end);
Packit 874993
	continue;
Packit 874993
      }
Packit 874993
    }
Packit 874993
Packit 874993
    else if (isCombinedS (u))
Packit 874993
    {
Packit 874993
      /* Have <LV>, <LVT>, or <LV,T> */
Packit 874993
      hb_codepoint_t s = u;
Packit 874993
      bool has_glyph = font->has_glyph (s);
Packit 874993
      unsigned int lindex = (s - SBase) / NCount;
Packit 874993
      unsigned int nindex = (s - SBase) % NCount;
Packit 874993
      unsigned int vindex = nindex / TCount;
Packit 874993
      unsigned int tindex = nindex % TCount;
Packit 874993
Packit 874993
      if (!tindex &&
Packit 874993
	  buffer->idx + 1 < count &&
Packit 874993
	  isCombiningT (buffer->cur(+1).codepoint))
Packit 874993
      {
Packit 874993
	/* <LV,T>, try to combine. */
Packit 874993
	unsigned int new_tindex = buffer->cur(+1).codepoint - TBase;
Packit 874993
	hb_codepoint_t new_s = s + new_tindex;
Packit 874993
	if (font->has_glyph (new_s))
Packit 874993
	{
Packit 874993
	  buffer->replace_glyphs (2, 1, &new_s);
Packit 874993
	  if (unlikely (buffer->in_error))
Packit 874993
	    return;
Packit 874993
	  end = start + 1;
Packit 874993
	  continue;
Packit 874993
	}
Packit 874993
      }
Packit 874993
Packit 874993
      /* Otherwise, decompose if font doesn't support <LV> or <LVT>,
Packit 874993
       * or if having non-combining <LV,T>.  Note that we already handled
Packit 874993
       * combining <LV,T> above. */
Packit 874993
      if (!has_glyph ||
Packit 874993
	  (!tindex &&
Packit 874993
	   buffer->idx + 1 < count &&
Packit 874993
	   isT (buffer->cur(+1).codepoint)))
Packit 874993
      {
Packit 874993
	hb_codepoint_t decomposed[3] = {LBase + lindex,
Packit 874993
					VBase + vindex,
Packit 874993
					TBase + tindex};
Packit 874993
	if (font->has_glyph (decomposed[0]) &&
Packit 874993
	    font->has_glyph (decomposed[1]) &&
Packit 874993
	    (!tindex || font->has_glyph (decomposed[2])))
Packit 874993
	{
Packit 874993
	  unsigned int s_len = tindex ? 3 : 2;
Packit 874993
	  buffer->replace_glyphs (1, s_len, decomposed);
Packit 874993
	  if (unlikely (buffer->in_error))
Packit 874993
	    return;
Packit 874993
Packit 874993
	  /* We decomposed S: apply jamo features to the individual glyphs
Packit 874993
	   * that are now in buffer->out_info.
Packit 874993
	   */
Packit 874993
	  hb_glyph_info_t *info = buffer->out_info;
Packit 874993
Packit 874993
	  /* If we decomposed an LV because of a non-combining T following,
Packit 874993
	   * we want to include this T in the syllable.
Packit 874993
	   */
Packit 874993
	  if (has_glyph && !tindex)
Packit 874993
	  {
Packit 874993
            buffer->next_glyph ();
Packit 874993
            s_len++;
Packit 874993
          }
Packit 874993
          end = start + s_len;
Packit 874993
Packit 874993
	  unsigned int i = start;
Packit 874993
	  info[i++].hangul_shaping_feature() = LJMO;
Packit 874993
	  info[i++].hangul_shaping_feature() = VJMO;
Packit 874993
	  if (i < end)
Packit 874993
	    info[i++].hangul_shaping_feature() = TJMO;
Packit 874993
	  if (buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
Packit 874993
	    buffer->merge_out_clusters (start, end);
Packit 874993
	  continue;
Packit 874993
	}
Packit 874993
      }
Packit 874993
Packit 874993
      if (has_glyph)
Packit 874993
      {
Packit 874993
        /* We didn't decompose the S, so just advance past it. */
Packit 874993
	end = start + 1;
Packit 874993
	buffer->next_glyph ();
Packit 874993
	continue;
Packit 874993
      }
Packit 874993
    }
Packit 874993
Packit 874993
    /* Didn't find a recognizable syllable, so we leave end <= start;
Packit 874993
     * this will prevent tone-mark reordering happening.
Packit 874993
     */
Packit 874993
    buffer->next_glyph ();
Packit 874993
  }
Packit 874993
  buffer->swap_buffers ();
Packit 874993
}
Packit 874993
Packit 874993
static void
Packit 874993
setup_masks_hangul (const hb_ot_shape_plan_t *plan,
Packit 874993
		    hb_buffer_t              *buffer,
Packit 874993
		    hb_font_t                *font HB_UNUSED)
Packit 874993
{
Packit 874993
  const hangul_shape_plan_t *hangul_plan = (const hangul_shape_plan_t *) plan->data;
Packit 874993
Packit 874993
  if (likely (hangul_plan))
Packit 874993
  {
Packit 874993
    unsigned int count = buffer->len;
Packit 874993
    hb_glyph_info_t *info = buffer->info;
Packit 874993
    for (unsigned int i = 0; i < count; i++, info++)
Packit 874993
      info->mask |= hangul_plan->mask_array[info->hangul_shaping_feature()];
Packit 874993
  }
Packit 874993
Packit 874993
  HB_BUFFER_DEALLOCATE_VAR (buffer, hangul_shaping_feature);
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
const hb_ot_complex_shaper_t _hb_ot_complex_shaper_hangul =
Packit 874993
{
Packit 874993
  "hangul",
Packit 874993
  collect_features_hangul,
Packit 874993
  override_features_hangul,
Packit 874993
  data_create_hangul,
Packit 874993
  data_destroy_hangul,
Packit 874993
  preprocess_text_hangul,
Packit 874993
  NULL, /* postprocess_glyphs */
Packit 874993
  HB_OT_SHAPE_NORMALIZATION_MODE_NONE,
Packit 874993
  NULL, /* decompose */
Packit 874993
  NULL, /* compose */
Packit 874993
  setup_masks_hangul,
Packit 874993
  NULL, /* disable_otl */
Packit 874993
  HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
Packit 874993
  false, /* fallback_position */
Packit 874993
};