hb-ot-shape-complex-misc.cc 6.1 KB
Newer Older
B
Minor  
Behdad Esfahbod 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright © 2010  Google, Inc.
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Google Author(s): Behdad Esfahbod
 */

#include "hb-ot-shape-complex-private.hh"


30
/* TODO Add kana, and other small shapers here */
B
Minor  
Behdad Esfahbod 已提交
31

32

33
/* The default shaper *only* adds additional per-script features.*/
34

35 36 37 38 39
static const hb_tag_t hangul_features[] =
{
  HB_TAG('l','j','m','o'),
  HB_TAG('v','j','m','o'),
  HB_TAG('t','j','m','o'),
40 41 42 43 44 45 46 47 48 49
  HB_TAG_NONE
};

static const hb_tag_t tibetan_features[] =
{
  HB_TAG('a','b','v','s'),
  HB_TAG('b','l','w','s'),
  HB_TAG('a','b','v','m'),
  HB_TAG('b','l','w','m'),
  HB_TAG_NONE
50 51
};

52 53 54 55
static void
collect_features_default (const hb_ot_complex_shaper_t  *shaper,
			  hb_ot_map_builder_t           *map,
			  const hb_segment_properties_t *props)
56
{
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  const hb_tag_t *script_features = NULL;

  switch ((hb_tag_t) props->script)
  {
    /* Unicode-1.1 additions */
    case HB_SCRIPT_HANGUL:
      script_features = hangul_features;
      break;

    /* Unicode-2.0 additions */
    case HB_SCRIPT_TIBETAN:
      script_features = tibetan_features;
      break;
  }

  for (; script_features && *script_features; script_features++)
    map->add_bool_feature (*script_features);
74 75
}

76 77 78
static hb_ot_shape_normalization_mode_t
normalization_preference_default (const hb_ot_complex_shaper_t  *shaper,
				  const hb_segment_properties_t *props)
79
{
80 81 82 83 84 85
  switch ((hb_tag_t) props->script)
  {
    /* Unicode-1.1 additions */
    case HB_SCRIPT_HANGUL:
      return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_FULL;
  }
86
  return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS;
87 88
}

89
const hb_ot_complex_shaper_t _hb_ot_complex_shaper_default =
90
{
91 92 93 94 95
  "default",
  collect_features_default,
  NULL, /* override_features */
  normalization_preference_default,
  NULL, /* setup_masks */
96
  true, /* zero_width_marks */
97
};
98 99 100 101


/* Thai / Lao shaper */

102 103 104 105 106
static void
setup_masks_thai (const hb_ot_complex_shaper_t *shaper,
		  const hb_ot_map_t            *map,
		  hb_buffer_t                  *buffer,
		  hb_font_t                    *font)
107 108 109 110
{
  /* The following is NOT specified in the MS OT Thai spec, however, it seems
   * to be what Uniscribe and other engines implement.  According to Eric Muller:
   *
111 112
   * When you have a SARA AM, decompose it in NIKHAHIT + SARA AA, *and* move the
   * NIKHAHIT backwards over any tone mark (0E48-0E4B).
113 114 115
   *
   * <0E14, 0E4B, 0E33> -> <0E14, 0E4D, 0E4B, 0E32>
   *
116
   * This reordering is legit only when the NIKHAHIT comes from a SARA AM, not
117
   * when it's there to start with. The string <0E14, 0E4B, 0E4D> is probably
118
   * not what a user wanted, but the rendering is nevertheless nikhahit above
119 120 121
   * chattawa.
   *
   * Same for Lao.
122 123 124 125 126 127 128
   *
   * Note:
   *
   * Uniscribe also does so below-marks reordering.  Namely, it positions U+0E3A
   * after U+0E38 and U+0E39.  We do that by modifying the ccc for U+0E3A.
   * See _hb_unicode_modified_combining_class ().  Lao does NOT have a U+0E3A
   * equivalent.
129 130
   */

131

132 133 134 135 136 137 138 139
  /*
   * Here are the characters of significance:
   *
   *			Thai	Lao
   * SARA AM:		U+0E33	U+0EB3
   * SARA AA:		U+0E32	U+0EB2
   * Nikhahit:		U+0E4D	U+0ECD
   *
140
   * Testing shows that Uniscribe reorder the following marks:
B
Behdad Esfahbod 已提交
141 142
   * Thai:	<0E31,0E34..0E37,0E47..0E4E>
   * Lao:	<0EB1,0EB4..0EB7,0EC7..0ECE>
143 144 145 146 147 148 149 150 151
   *
   * Note how the Lao versions are the same as Thai + 0x80.
   */

  /* We only get one script at a time, so a script-agnostic implementation
   * is adequate here. */
#define IS_SARA_AM(x) (((x) & ~0x0080) == 0x0E33)
#define NIKHAHIT_FROM_SARA_AM(x) ((x) - 0xE33 + 0xE4D)
#define SARA_AA_FROM_SARA_AM(x) ((x) - 1)
B
Behdad Esfahbod 已提交
152
#define IS_TONE_MARK(x) (hb_in_ranges<hb_codepoint_t> ((x) & ~0x0080, 0x0E34, 0x0E37, 0x0E47, 0x0E4E, 0x0E31, 0x0E31))
153 154 155 156 157

  buffer->clear_output ();
  unsigned int count = buffer->len;
  for (buffer->idx = 0; buffer->idx < count;)
  {
158
    hb_codepoint_t u = buffer->cur().codepoint;
B
Behdad Esfahbod 已提交
159
    if (likely (!IS_SARA_AM (u))) {
160 161 162 163 164
      buffer->next_glyph ();
      continue;
    }

    /* Is SARA AM. Decompose and reorder. */
B
Minor  
Behdad Esfahbod 已提交
165 166
    hb_codepoint_t decomposed[2] = {hb_codepoint_t (NIKHAHIT_FROM_SARA_AM (u)),
				    hb_codepoint_t (SARA_AA_FROM_SARA_AM (u))};
167
    buffer->replace_glyphs (1, 2, decomposed);
168
    if (unlikely (buffer->in_error))
169 170 171 172 173 174 175 176
      return;

    /* Ok, let's see... */
    unsigned int end = buffer->out_len;
    unsigned int start = end - 2;
    while (start > 0 && IS_TONE_MARK (buffer->out_info[start - 1].codepoint))
      start--;

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    if (start + 2 < end)
    {
      /* Move Nikhahit (end-2) to the beginning */
      buffer->merge_out_clusters (start, end);
      hb_glyph_info_t t = buffer->out_info[end - 2];
      memmove (buffer->out_info + start + 1,
	       buffer->out_info + start,
	       sizeof (buffer->out_info[0]) * (end - start - 2));
      buffer->out_info[start] = t;
    }
    else
    {
      /* Since we decomposed, and NIKHAHIT is combining, merge clusters with the
       * previous cluster. */
      if (start)
	buffer->merge_out_clusters (start - 1, end);
    }
194 195 196
  }
  buffer->swap_buffers ();
}
197 198 199 200 201 202 203 204

const hb_ot_complex_shaper_t _hb_ot_complex_shaper_thai =
{
  "thai",
  NULL, /* collect_features */
  NULL, /* override_features */
  NULL, /* normalization_preference */
  setup_masks_thai,
205
  true, /* zero_width_marks */
206
};