Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright  2009,2010  Red Hat, Inc.
      3  * Copyright  2010,2011  Google, Inc.
      4  *
      5  *  This is part of HarfBuzz, a text shaping library.
      6  *
      7  * Permission is hereby granted, without written agreement and without
      8  * license or royalty fees, to use, copy, modify, and distribute this
      9  * software and its documentation for any purpose, provided that the
     10  * above copyright notice and the following two paragraphs appear in
     11  * all copies of this software.
     12  *
     13  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
     14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
     15  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
     16  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
     17  * DAMAGE.
     18  *
     19  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
     20  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
     21  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
     22  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
     23  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
     24  *
     25  * Red Hat Author(s): Behdad Esfahbod
     26  * Google Author(s): Behdad Esfahbod
     27  */
     28 
     29 #include "hb-ot-map-private.hh"
     30 
     31 
     32 void
     33 hb_ot_map_t::add_lookups (hb_face_t    *face,
     34 			  unsigned int  table_index,
     35 			  unsigned int  feature_index,
     36 			  hb_mask_t     mask,
     37 			  bool          auto_zwj)
     38 {
     39   unsigned int lookup_indices[32];
     40   unsigned int offset, len;
     41 
     42   offset = 0;
     43   do {
     44     len = ARRAY_LENGTH (lookup_indices);
     45     hb_ot_layout_feature_get_lookups (face,
     46 				      table_tags[table_index],
     47 				      feature_index,
     48 				      offset, &len,
     49 				      lookup_indices);
     50 
     51     for (unsigned int i = 0; i < len; i++) {
     52       hb_ot_map_t::lookup_map_t *lookup = lookups[table_index].push ();
     53       if (unlikely (!lookup))
     54         return;
     55       lookup->mask = mask;
     56       lookup->index = lookup_indices[i];
     57       lookup->auto_zwj = auto_zwj;
     58     }
     59 
     60     offset += len;
     61   } while (len == ARRAY_LENGTH (lookup_indices));
     62 }
     63 
     64 hb_ot_map_builder_t::hb_ot_map_builder_t (hb_face_t *face_,
     65 					  const hb_segment_properties_t *props_)
     66 {
     67   memset (this, 0, sizeof (*this));
     68 
     69   face = face_;
     70   props = *props_;
     71 
     72 
     73   /* Fetch script/language indices for GSUB/GPOS.  We need these later to skip
     74    * features not available in either table and not waste precious bits for them. */
     75 
     76   hb_tag_t script_tags[3] = {HB_TAG_NONE, HB_TAG_NONE, HB_TAG_NONE};
     77   hb_tag_t language_tag;
     78 
     79   hb_ot_tags_from_script (props.script, &script_tags[0], &script_tags[1]);
     80   language_tag = hb_ot_tag_from_language (props.language);
     81 
     82   for (unsigned int table_index = 0; table_index < 2; table_index++) {
     83     hb_tag_t table_tag = table_tags[table_index];
     84     found_script[table_index] = hb_ot_layout_table_choose_script (face, table_tag, script_tags, &script_index[table_index], &chosen_script[table_index]);
     85     hb_ot_layout_script_find_language (face, table_tag, script_index[table_index], language_tag, &language_index[table_index]);
     86   }
     87 }
     88 
     89 void hb_ot_map_builder_t::add_feature (hb_tag_t tag, unsigned int value,
     90 				       hb_ot_map_feature_flags_t flags)
     91 {
     92   feature_info_t *info = feature_infos.push();
     93   if (unlikely (!info)) return;
     94   info->tag = tag;
     95   info->seq = feature_infos.len;
     96   info->max_value = value;
     97   info->flags = flags;
     98   info->default_value = (flags & F_GLOBAL) ? value : 0;
     99   info->stage[0] = current_stage[0];
    100   info->stage[1] = current_stage[1];
    101 }
    102 
    103 /* Keep the next two functions in sync. */
    104 
    105 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
    106 {
    107   const unsigned int table_index = 0;
    108   unsigned int i = 0;
    109 
    110   for (unsigned int pause_index = 0; pause_index < pauses[table_index].len; pause_index++) {
    111     const pause_map_t *pause = &pauses[table_index][pause_index];
    112     for (; i < pause->num_lookups; i++)
    113       hb_ot_layout_substitute_lookup (font, buffer,
    114 				      lookups[table_index][i].index,
    115 				      lookups[table_index][i].mask,
    116 				      lookups[table_index][i].auto_zwj);
    117 
    118     buffer->clear_output ();
    119 
    120     if (pause->callback)
    121       pause->callback (plan, font, buffer);
    122   }
    123 
    124   for (; i < lookups[table_index].len; i++)
    125     hb_ot_layout_substitute_lookup (font, buffer, lookups[table_index][i].index,
    126 				    lookups[table_index][i].mask,
    127 				    lookups[table_index][i].auto_zwj);
    128 }
    129 
    130 void hb_ot_map_t::position (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
    131 {
    132   const unsigned int table_index = 1;
    133   unsigned int i = 0;
    134 
    135   for (unsigned int pause_index = 0; pause_index < pauses[table_index].len; pause_index++) {
    136     const pause_map_t *pause = &pauses[table_index][pause_index];
    137     for (; i < pause->num_lookups; i++)
    138       hb_ot_layout_position_lookup (font, buffer, lookups[table_index][i].index,
    139 				    lookups[table_index][i].mask,
    140 				    lookups[table_index][i].auto_zwj);
    141 
    142     if (pause->callback)
    143       pause->callback (plan, font, buffer);
    144   }
    145 
    146   for (; i < lookups[table_index].len; i++)
    147     hb_ot_layout_position_lookup (font, buffer, lookups[table_index][i].index,
    148 				  lookups[table_index][i].mask,
    149 				  lookups[table_index][i].auto_zwj);
    150 }
    151 
    152 void hb_ot_map_t::collect_lookups (unsigned int table_index, hb_set_t *lookups_out) const
    153 {
    154   for (unsigned int i = 0; i < lookups[table_index].len; i++)
    155     hb_set_add (lookups_out, lookups[table_index][i].index);
    156 }
    157 
    158 void hb_ot_map_builder_t::add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func)
    159 {
    160   pause_info_t *p = pauses[table_index].push ();
    161   if (likely (p)) {
    162     p->stage = current_stage[table_index];
    163     p->callback = pause_func;
    164   }
    165 
    166   current_stage[table_index]++;
    167 }
    168 
    169 void
    170 hb_ot_map_builder_t::compile (hb_ot_map_t &m)
    171 {
    172   m.global_mask = 1;
    173 
    174   for (unsigned int table_index = 0; table_index < 2; table_index++) {
    175     m.chosen_script[table_index] = chosen_script[table_index];
    176     m.found_script[table_index] = found_script[table_index];
    177   }
    178 
    179   if (!feature_infos.len)
    180     return;
    181 
    182   /* Sort features and merge duplicates */
    183   {
    184     feature_infos.sort ();
    185     unsigned int j = 0;
    186     for (unsigned int i = 1; i < feature_infos.len; i++)
    187       if (feature_infos[i].tag != feature_infos[j].tag)
    188 	feature_infos[++j] = feature_infos[i];
    189       else {
    190 	if (feature_infos[i].flags & F_GLOBAL) {
    191 	  feature_infos[j].flags |= F_GLOBAL;
    192 	  feature_infos[j].max_value = feature_infos[i].max_value;
    193 	  feature_infos[j].default_value = feature_infos[i].default_value;
    194 	} else {
    195 	  feature_infos[j].flags &= ~F_GLOBAL;
    196 	  feature_infos[j].max_value = MAX (feature_infos[j].max_value, feature_infos[i].max_value);
    197 	  /* Inherit default_value from j */
    198 	}
    199 	feature_infos[j].flags |= (feature_infos[i].flags & F_HAS_FALLBACK);
    200 	feature_infos[j].stage[0] = MIN (feature_infos[j].stage[0], feature_infos[i].stage[0]);
    201 	feature_infos[j].stage[1] = MIN (feature_infos[j].stage[1], feature_infos[i].stage[1]);
    202       }
    203     feature_infos.shrink (j + 1);
    204   }
    205 
    206 
    207   /* Allocate bits now */
    208   unsigned int next_bit = 1;
    209   for (unsigned int i = 0; i < feature_infos.len; i++) {
    210     const feature_info_t *info = &feature_infos[i];
    211 
    212     unsigned int bits_needed;
    213 
    214     if ((info->flags & F_GLOBAL) && info->max_value == 1)
    215       /* Uses the global bit */
    216       bits_needed = 0;
    217     else
    218       bits_needed = _hb_bit_storage (info->max_value);
    219 
    220     if (!info->max_value || next_bit + bits_needed > 8 * sizeof (hb_mask_t))
    221       continue; /* Feature disabled, or not enough bits. */
    222 
    223 
    224     bool found = false;
    225     unsigned int feature_index[2];
    226     for (unsigned int table_index = 0; table_index < 2; table_index++)
    227       found |= hb_ot_layout_language_find_feature (face,
    228 						   table_tags[table_index],
    229 						   script_index[table_index],
    230 						   language_index[table_index],
    231 						   info->tag,
    232 						   &feature_index[table_index]);
    233     if (!found && !(info->flags & F_HAS_FALLBACK))
    234       continue;
    235 
    236 
    237     hb_ot_map_t::feature_map_t *map = m.features.push ();
    238     if (unlikely (!map))
    239       break;
    240 
    241     map->tag = info->tag;
    242     map->index[0] = feature_index[0];
    243     map->index[1] = feature_index[1];
    244     map->stage[0] = info->stage[0];
    245     map->stage[1] = info->stage[1];
    246     map->auto_zwj = !(info->flags & F_MANUAL_ZWJ);
    247     if ((info->flags & F_GLOBAL) && info->max_value == 1) {
    248       /* Uses the global bit */
    249       map->shift = 0;
    250       map->mask = 1;
    251     } else {
    252       map->shift = next_bit;
    253       map->mask = (1 << (next_bit + bits_needed)) - (1 << next_bit);
    254       next_bit += bits_needed;
    255       m.global_mask |= (info->default_value << map->shift) & map->mask;
    256     }
    257     map->_1_mask = (1 << map->shift) & map->mask;
    258     map->needs_fallback = !found;
    259 
    260   }
    261   feature_infos.shrink (0); /* Done with these */
    262 
    263 
    264   add_gsub_pause (NULL);
    265   add_gpos_pause (NULL);
    266 
    267   for (unsigned int table_index = 0; table_index < 2; table_index++) {
    268     hb_tag_t table_tag = table_tags[table_index];
    269 
    270     /* Collect lookup indices for features */
    271 
    272     unsigned int required_feature_index;
    273     if (hb_ot_layout_language_get_required_feature_index (face,
    274 							  table_tag,
    275 							  script_index[table_index],
    276 							  language_index[table_index],
    277 							  &required_feature_index))
    278       m.add_lookups (face, table_index, required_feature_index, 1, true);
    279 
    280     unsigned int pause_index = 0;
    281     unsigned int last_num_lookups = 0;
    282     for (unsigned stage = 0; stage < current_stage[table_index]; stage++)
    283     {
    284       for (unsigned i = 0; i < m.features.len; i++)
    285         if (m.features[i].stage[table_index] == stage)
    286 	  m.add_lookups (face, table_index,
    287 			 m.features[i].index[table_index],
    288 			 m.features[i].mask,
    289 			 m.features[i].auto_zwj);
    290 
    291       /* Sort lookups and merge duplicates */
    292       if (last_num_lookups < m.lookups[table_index].len)
    293       {
    294 	m.lookups[table_index].sort (last_num_lookups, m.lookups[table_index].len);
    295 
    296 	unsigned int j = last_num_lookups;
    297 	for (unsigned int i = j + 1; i < m.lookups[table_index].len; i++)
    298 	  if (m.lookups[table_index][i].index != m.lookups[table_index][j].index)
    299 	    m.lookups[table_index][++j] = m.lookups[table_index][i];
    300 	  else
    301 	  {
    302 	    m.lookups[table_index][j].mask |= m.lookups[table_index][i].mask;
    303 	    m.lookups[table_index][j].auto_zwj &= m.lookups[table_index][i].auto_zwj;
    304 	  }
    305 	m.lookups[table_index].shrink (j + 1);
    306       }
    307 
    308       last_num_lookups = m.lookups[table_index].len;
    309 
    310       if (pause_index < pauses[table_index].len && pauses[table_index][pause_index].stage == stage) {
    311 	hb_ot_map_t::pause_map_t *pause_map = m.pauses[table_index].push ();
    312 	if (likely (pause_map)) {
    313 	  pause_map->num_lookups = last_num_lookups;
    314 	  pause_map->callback = pauses[table_index][pause_index].callback;
    315 	}
    316 
    317 	pause_index++;
    318       }
    319     }
    320   }
    321 }
    322