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

Packit 874993
/*
Packit 874993
 * Copyright © 2010,2012  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
/* Thai / Lao shaper */
Packit 874993
Packit 874993
Packit 874993
/* PUA shaping */
Packit 874993
Packit 874993
Packit 874993
enum thai_consonant_type_t
Packit 874993
{
Packit 874993
  NC,
Packit 874993
  AC,
Packit 874993
  RC,
Packit 874993
  DC,
Packit 874993
  NOT_CONSONANT,
Packit 874993
  NUM_CONSONANT_TYPES = NOT_CONSONANT
Packit 874993
};
Packit 874993
Packit 874993
static thai_consonant_type_t
Packit 874993
get_consonant_type (hb_codepoint_t u)
Packit 874993
{
Packit 874993
  if (u == 0x0E1Bu || u == 0x0E1Du || u == 0x0E1Fu/* || u == 0x0E2Cu*/)
Packit 874993
    return AC;
Packit 874993
  if (u == 0x0E0Du || u == 0x0E10u)
Packit 874993
    return RC;
Packit 874993
  if (u == 0x0E0Eu || u == 0x0E0Fu)
Packit 874993
    return DC;
Packit 874993
  if (hb_in_range<hb_codepoint_t> (u, 0x0E01u, 0x0E2Eu))
Packit 874993
    return NC;
Packit 874993
  return NOT_CONSONANT;
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
enum thai_mark_type_t
Packit 874993
{
Packit 874993
  AV,
Packit 874993
  BV,
Packit 874993
  T,
Packit 874993
  NOT_MARK,
Packit 874993
  NUM_MARK_TYPES = NOT_MARK
Packit 874993
};
Packit 874993
Packit 874993
static thai_mark_type_t
Packit 874993
get_mark_type (hb_codepoint_t u)
Packit 874993
{
Packit 874993
  if (u == 0x0E31u || hb_in_range<hb_codepoint_t> (u, 0x0E34u, 0x0E37u) ||
Packit 874993
      u == 0x0E47u || hb_in_range<hb_codepoint_t> (u, 0x0E4Du, 0x0E4Eu))
Packit 874993
    return AV;
Packit 874993
  if (hb_in_range<hb_codepoint_t> (u, 0x0E38u, 0x0E3Au))
Packit 874993
    return BV;
Packit 874993
  if (hb_in_range<hb_codepoint_t> (u, 0x0E48u, 0x0E4Cu))
Packit 874993
    return T;
Packit 874993
  return NOT_MARK;
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
enum thai_action_t
Packit 874993
{
Packit 874993
  NOP,
Packit 874993
  SD,  /* Shift combining-mark down */
Packit 874993
  SL,  /* Shift combining-mark left */
Packit 874993
  SDL, /* Shift combining-mark down-left */
Packit 874993
  RD   /* Remove descender from base */
Packit 874993
};
Packit 874993
Packit 874993
static hb_codepoint_t
Packit 874993
thai_pua_shape (hb_codepoint_t u, thai_action_t action, hb_font_t *font)
Packit 874993
{
Packit 874993
  struct thai_pua_mapping_t {
Packit 874993
    hb_codepoint_t u;
Packit 874993
    hb_codepoint_t win_pua;
Packit 874993
    hb_codepoint_t mac_pua;
Packit 874993
  } const *pua_mappings = NULL;
Packit 874993
  static const thai_pua_mapping_t SD_mappings[] = {
Packit 874993
    {0x0E48u, 0xF70Au, 0xF88Bu}, /* MAI EK */
Packit 874993
    {0x0E49u, 0xF70Bu, 0xF88Eu}, /* MAI THO */
Packit 874993
    {0x0E4Au, 0xF70Cu, 0xF891u}, /* MAI TRI */
Packit 874993
    {0x0E4Bu, 0xF70Du, 0xF894u}, /* MAI CHATTAWA */
Packit 874993
    {0x0E4Cu, 0xF70Eu, 0xF897u}, /* THANTHAKHAT */
Packit 874993
    {0x0E38u, 0xF718u, 0xF89Bu}, /* SARA U */
Packit 874993
    {0x0E39u, 0xF719u, 0xF89Cu}, /* SARA UU */
Packit 874993
    {0x0E3Au, 0xF71Au, 0xF89Du}, /* PHINTHU */
Packit 874993
    {0x0000u, 0x0000u, 0x0000u}
Packit 874993
  };
Packit 874993
  static const thai_pua_mapping_t SDL_mappings[] = {
Packit 874993
    {0x0E48u, 0xF705u, 0xF88Cu}, /* MAI EK */
Packit 874993
    {0x0E49u, 0xF706u, 0xF88Fu}, /* MAI THO */
Packit 874993
    {0x0E4Au, 0xF707u, 0xF892u}, /* MAI TRI */
Packit 874993
    {0x0E4Bu, 0xF708u, 0xF895u}, /* MAI CHATTAWA */
Packit 874993
    {0x0E4Cu, 0xF709u, 0xF898u}, /* THANTHAKHAT */
Packit 874993
    {0x0000u, 0x0000u, 0x0000u}
Packit 874993
  };
Packit 874993
  static const thai_pua_mapping_t SL_mappings[] = {
Packit 874993
    {0x0E48u, 0xF713u, 0xF88Au}, /* MAI EK */
Packit 874993
    {0x0E49u, 0xF714u, 0xF88Du}, /* MAI THO */
Packit 874993
    {0x0E4Au, 0xF715u, 0xF890u}, /* MAI TRI */
Packit 874993
    {0x0E4Bu, 0xF716u, 0xF893u}, /* MAI CHATTAWA */
Packit 874993
    {0x0E4Cu, 0xF717u, 0xF896u}, /* THANTHAKHAT */
Packit 874993
    {0x0E31u, 0xF710u, 0xF884u}, /* MAI HAN-AKAT */
Packit 874993
    {0x0E34u, 0xF701u, 0xF885u}, /* SARA I */
Packit 874993
    {0x0E35u, 0xF702u, 0xF886u}, /* SARA II */
Packit 874993
    {0x0E36u, 0xF703u, 0xF887u}, /* SARA UE */
Packit 874993
    {0x0E37u, 0xF704u, 0xF888u}, /* SARA UEE */
Packit 874993
    {0x0E47u, 0xF712u, 0xF889u}, /* MAITAIKHU */
Packit 874993
    {0x0E4Du, 0xF711u, 0xF899u}, /* NIKHAHIT */
Packit 874993
    {0x0000u, 0x0000u, 0x0000u}
Packit 874993
  };
Packit 874993
  static const thai_pua_mapping_t RD_mappings[] = {
Packit 874993
    {0x0E0Du, 0xF70Fu, 0xF89Au}, /* YO YING */
Packit 874993
    {0x0E10u, 0xF700u, 0xF89Eu}, /* THO THAN */
Packit 874993
    {0x0000u, 0x0000u, 0x0000u}
Packit 874993
  };
Packit 874993
Packit 874993
  switch (action) {
Packit 874993
    case NOP: return u;
Packit 874993
    case SD:  pua_mappings = SD_mappings; break;
Packit 874993
    case SDL: pua_mappings = SDL_mappings; break;
Packit 874993
    case SL:  pua_mappings = SL_mappings; break;
Packit 874993
    case RD:  pua_mappings = RD_mappings; break;
Packit 874993
  }
Packit 874993
  for (; pua_mappings->u; pua_mappings++)
Packit 874993
    if (pua_mappings->u == u)
Packit 874993
    {
Packit 874993
      hb_codepoint_t glyph;
Packit 874993
      if (hb_font_get_glyph (font, pua_mappings->win_pua, 0, &glyph))
Packit 874993
	return pua_mappings->win_pua;
Packit 874993
      if (hb_font_get_glyph (font, pua_mappings->mac_pua, 0, &glyph))
Packit 874993
	return pua_mappings->mac_pua;
Packit 874993
      break;
Packit 874993
    }
Packit 874993
  return u;
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
static enum thai_above_state_t
Packit 874993
{     /* Cluster above looks like: */
Packit 874993
  T0, /*  ⣤                      */
Packit 874993
  T1, /*     ⣼                   */
Packit 874993
  T2, /*        ⣾                */
Packit 874993
  T3, /*           ⣿             */
Packit 874993
  NUM_ABOVE_STATES
Packit 874993
} thai_above_start_state[NUM_CONSONANT_TYPES + 1/* For NOT_CONSONANT */] =
Packit 874993
{
Packit 874993
  T0, /* NC */
Packit 874993
  T1, /* AC */
Packit 874993
  T0, /* RC */
Packit 874993
  T0, /* DC */
Packit 874993
  T3, /* NOT_CONSONANT */
Packit 874993
};
Packit 874993
Packit 874993
static const struct thai_above_state_machine_edge_t {
Packit 874993
  thai_action_t action;
Packit 874993
  thai_above_state_t next_state;
Packit 874993
} thai_above_state_machine[NUM_ABOVE_STATES][NUM_MARK_TYPES] =
Packit 874993
{        /*AV*/    /*BV*/    /*T*/
Packit 874993
/*T0*/ {{NOP,T3}, {NOP,T0}, {SD, T3}},
Packit 874993
/*T1*/ {{SL, T2}, {NOP,T1}, {SDL,T2}},
Packit 874993
/*T2*/ {{NOP,T3}, {NOP,T2}, {SL, T3}},
Packit 874993
/*T3*/ {{NOP,T3}, {NOP,T3}, {NOP,T3}},
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
static enum thai_below_state_t
Packit 874993
{
Packit 874993
  B0, /* No descender */
Packit 874993
  B1, /* Removable descender */
Packit 874993
  B2, /* Strict descender */
Packit 874993
  NUM_BELOW_STATES
Packit 874993
} thai_below_start_state[NUM_CONSONANT_TYPES + 1/* For NOT_CONSONANT */] =
Packit 874993
{
Packit 874993
  B0, /* NC */
Packit 874993
  B0, /* AC */
Packit 874993
  B1, /* RC */
Packit 874993
  B2, /* DC */
Packit 874993
  B2, /* NOT_CONSONANT */
Packit 874993
};
Packit 874993
Packit 874993
static const struct thai_below_state_machine_edge_t {
Packit 874993
  thai_action_t action;
Packit 874993
  thai_below_state_t next_state;
Packit 874993
} thai_below_state_machine[NUM_BELOW_STATES][NUM_MARK_TYPES] =
Packit 874993
{        /*AV*/    /*BV*/    /*T*/
Packit 874993
/*B0*/ {{NOP,B0}, {NOP,B2}, {NOP, B0}},
Packit 874993
/*B1*/ {{NOP,B1}, {RD, B2}, {NOP, B1}},
Packit 874993
/*B2*/ {{NOP,B2}, {SD, B2}, {NOP, B2}},
Packit 874993
};
Packit 874993
Packit 874993
Packit 874993
static void
Packit 874993
do_thai_pua_shaping (const hb_ot_shape_plan_t *plan HB_UNUSED,
Packit 874993
		     hb_buffer_t              *buffer,
Packit 874993
		     hb_font_t                *font)
Packit 874993
{
Packit 874993
  thai_above_state_t above_state = thai_above_start_state[NOT_CONSONANT];
Packit 874993
  thai_below_state_t below_state = thai_below_start_state[NOT_CONSONANT];
Packit 874993
  unsigned int base = 0;
Packit 874993
Packit 874993
  hb_glyph_info_t *info = buffer->info;
Packit 874993
  unsigned int count = buffer->len;
Packit 874993
  for (unsigned int i = 0; i < count; i++)
Packit 874993
  {
Packit 874993
    thai_mark_type_t mt = get_mark_type (info[i].codepoint);
Packit 874993
Packit 874993
    if (mt == NOT_MARK) {
Packit 874993
      thai_consonant_type_t ct = get_consonant_type (info[i].codepoint);
Packit 874993
      above_state = thai_above_start_state[ct];
Packit 874993
      below_state = thai_below_start_state[ct];
Packit 874993
      base = i;
Packit 874993
      continue;
Packit 874993
    }
Packit 874993
Packit 874993
    const thai_above_state_machine_edge_t &above_edge = thai_above_state_machine[above_state][mt];
Packit 874993
    const thai_below_state_machine_edge_t &below_edge = thai_below_state_machine[below_state][mt];
Packit 874993
    above_state = above_edge.next_state;
Packit 874993
    below_state = below_edge.next_state;
Packit 874993
Packit 874993
    /* At least one of the above/below actions is NOP. */
Packit 874993
    thai_action_t action = above_edge.action != NOP ? above_edge.action : below_edge.action;
Packit 874993
Packit 874993
    if (action == RD)
Packit 874993
      info[base].codepoint = thai_pua_shape (info[base].codepoint, action, font);
Packit 874993
    else
Packit 874993
      info[i].codepoint = thai_pua_shape (info[i].codepoint, action, font);
Packit 874993
  }
Packit 874993
}
Packit 874993
Packit 874993
Packit 874993
static void
Packit 874993
preprocess_text_thai (const hb_ot_shape_plan_t *plan,
Packit 874993
		      hb_buffer_t              *buffer,
Packit 874993
		      hb_font_t                *font)
Packit 874993
{
Packit 874993
  /* This function implements the shaping logic documented here:
Packit 874993
   *
Packit 874993
   *   http://linux.thai.net/~thep/th-otf/shaping.html
Packit 874993
   *
Packit 874993
   * The first shaping rule listed there is needed even if the font has Thai
Packit 874993
   * OpenType tables.  The rest do fallback positioning based on PUA codepoints.
Packit 874993
   * We implement that only if there exist no Thai GSUB in the font.
Packit 874993
   */
Packit 874993
Packit 874993
  /* The following is NOT specified in the MS OT Thai spec, however, it seems
Packit 874993
   * to be what Uniscribe and other engines implement.  According to Eric Muller:
Packit 874993
   *
Packit 874993
   * When you have a SARA AM, decompose it in NIKHAHIT + SARA AA, *and* move the
Packit 874993
   * NIKHAHIT backwards over any tone mark (0E48-0E4B).
Packit 874993
   *
Packit 874993
   * <0E14, 0E4B, 0E33> -> <0E14, 0E4D, 0E4B, 0E32>
Packit 874993
   *
Packit 874993
   * This reordering is legit only when the NIKHAHIT comes from a SARA AM, not
Packit 874993
   * when it's there to start with. The string <0E14, 0E4B, 0E4D> is probably
Packit 874993
   * not what a user wanted, but the rendering is nevertheless nikhahit above
Packit 874993
   * chattawa.
Packit 874993
   *
Packit 874993
   * Same for Lao.
Packit 874993
   *
Packit 874993
   * Note:
Packit 874993
   *
Packit 874993
   * Uniscribe also does some below-marks reordering.  Namely, it positions U+0E3A
Packit 874993
   * after U+0E38 and U+0E39.  We do that by modifying the ccc for U+0E3A.
Packit 874993
   * See unicode->modified_combining_class ().  Lao does NOT have a U+0E3A
Packit 874993
   * equivalent.
Packit 874993
   */
Packit 874993
Packit 874993
Packit 874993
  /*
Packit 874993
   * Here are the characters of significance:
Packit 874993
   *
Packit 874993
   *			Thai	Lao
Packit 874993
   * SARA AM:		U+0E33	U+0EB3
Packit 874993
   * SARA AA:		U+0E32	U+0EB2
Packit 874993
   * Nikhahit:		U+0E4D	U+0ECD
Packit 874993
   *
Packit 874993
   * Testing shows that Uniscribe reorder the following marks:
Packit 874993
   * Thai:	<0E31,0E34..0E37,0E47..0E4E>
Packit 874993
   * Lao:	<0EB1,0EB4..0EB7,0EC7..0ECE>
Packit 874993
   *
Packit 874993
   * Note how the Lao versions are the same as Thai + 0x80.
Packit 874993
   */
Packit 874993
Packit 874993
  /* We only get one script at a time, so a script-agnostic implementation
Packit 874993
   * is adequate here. */
Packit 874993
#define IS_SARA_AM(x) (((x) & ~0x0080u) == 0x0E33u)
Packit 874993
#define NIKHAHIT_FROM_SARA_AM(x) ((x) - 0x0E33u + 0x0E4Du)
Packit 874993
#define SARA_AA_FROM_SARA_AM(x) ((x) - 1)
Packit 874993
#define IS_TONE_MARK(x) (hb_in_ranges<hb_codepoint_t> ((x) & ~0x0080u, 0x0E34u, 0x0E37u, 0x0E47u, 0x0E4Eu, 0x0E31u, 0x0E31u))
Packit 874993
Packit 874993
  buffer->clear_output ();
Packit 874993
  unsigned int count = buffer->len;
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
    if (likely (!IS_SARA_AM (u))) {
Packit 874993
      buffer->next_glyph ();
Packit 874993
      continue;
Packit 874993
    }
Packit 874993
Packit 874993
    /* Is SARA AM. Decompose and reorder. */
Packit 874993
    hb_codepoint_t decomposed[2] = {hb_codepoint_t (NIKHAHIT_FROM_SARA_AM (u)),
Packit 874993
				    hb_codepoint_t (SARA_AA_FROM_SARA_AM (u))};
Packit 874993
    buffer->replace_glyphs (1, 2, decomposed);
Packit 874993
    if (unlikely (buffer->in_error))
Packit 874993
      return;
Packit 874993
Packit 874993
    /* Make Nikhahit be recognized as a ccc=0 mark when zeroing widths. */
Packit 874993
    unsigned int end = buffer->out_len;
Packit 874993
    _hb_glyph_info_set_general_category (&buffer->out_info[end - 2], HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK);
Packit 874993
Packit 874993
    /* Ok, let's see... */
Packit 874993
    unsigned int start = end - 2;
Packit 874993
    while (start > 0 && IS_TONE_MARK (buffer->out_info[start - 1].codepoint))
Packit 874993
      start--;
Packit 874993
Packit 874993
    if (start + 2 < end)
Packit 874993
    {
Packit 874993
      /* Move Nikhahit (end-2) to the beginning */
Packit 874993
      buffer->merge_out_clusters (start, end);
Packit 874993
      hb_glyph_info_t t = buffer->out_info[end - 2];
Packit 874993
      memmove (buffer->out_info + start + 1,
Packit 874993
	       buffer->out_info + start,
Packit 874993
	       sizeof (buffer->out_info[0]) * (end - start - 2));
Packit 874993
      buffer->out_info[start] = t;
Packit 874993
    }
Packit 874993
    else
Packit 874993
    {
Packit 874993
      /* Since we decomposed, and NIKHAHIT is combining, merge clusters with the
Packit 874993
       * previous cluster. */
Packit 874993
      if (start && buffer->cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES)
Packit 874993
	buffer->merge_out_clusters (start - 1, end);
Packit 874993
    }
Packit 874993
  }
Packit 874993
  buffer->swap_buffers ();
Packit 874993
Packit 874993
  /* If font has Thai GSUB, we are done. */
Packit 874993
  if (plan->props.script == HB_SCRIPT_THAI && !plan->map.found_script[0])
Packit 874993
    do_thai_pua_shaping (plan, buffer, font);
Packit 874993
}
Packit 874993
Packit 874993
const hb_ot_complex_shaper_t _hb_ot_complex_shaper_thai =
Packit 874993
{
Packit 874993
  "thai",
Packit 874993
  NULL, /* collect_features */
Packit 874993
  NULL, /* override_features */
Packit 874993
  NULL, /* data_create */
Packit 874993
  NULL, /* data_destroy */
Packit 874993
  preprocess_text_thai,
Packit 874993
  NULL, /* postprocess_glyphs */
Packit 874993
  HB_OT_SHAPE_NORMALIZATION_MODE_DEFAULT,
Packit 874993
  NULL, /* decompose */
Packit 874993
  NULL, /* compose */
Packit 874993
  NULL, /* setup_masks */
Packit 874993
  NULL, /* disable_otl */
Packit 874993
  HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE,
Packit 874993
  false,/* fallback_position */
Packit 874993
};