Move buffer methods into the object
[apps/core/preloaded/video-player.git] / src / hb-ot-shape-normalize.cc
1 /*
2  * Copyright © 2011  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-ot-shape-private.hh"
28 #include "hb-ot-shape-complex-private.hh"
29
30 HB_BEGIN_DECLS
31
32 /*
33  * HIGHLEVEL DESIGN:
34  *
35  * This file exports one main function: _hb_ot_shape_normalize().
36  *
37  * This function closely reflects the Unicode Normalization Algorithm,
38  * yet it's different.  The shaper an either prefer decomposed (NFD) or
39  * composed (NFC).
40  *
41  * In general what happens is that: each grapheme is decomposed in a chain
42  * of 1:2 decompositions, marks reordered, and then recomposed if desires,
43  * so far it's like Unicode Normalization.  However, the decomposition and
44  * recomposition only happens if the font supports the resulting characters.
45  *
46  * The goals are:
47  *
48  *   - Try to render all canonically equivalent strings similarly.  To really
49  *     achieve this we have to always do the full decomposition and then
50  *     selectively recompose from there.  It's kinda too expensive though, so
51  *     we skip some cases.  For example, if composed is desired, we simply
52  *     don't touch 1-character clusters that are supported by the font, even
53  *     though their NFC may be different.
54  *
55  *   - When a font has a precomposed character for a sequence but the 'ccmp'
56  *     feature in the font is not adequate, form use the precomposed character
57  *     which typically has better mark positioning.
58  *
59  *   - When a font does not support a character but supports its decomposition,
60  *     well, use the decomposition.
61  *
62  *   - The Indic shaper requests decomposed output.  This will handle splitting
63  *     matra for the Indic shaper.
64  */
65
66
67 static bool
68 decompose (hb_ot_shape_context_t *c,
69            bool recompose,
70            hb_codepoint_t ab)
71 {
72   hb_codepoint_t a, b, glyph;
73   bool has_this = hb_font_get_glyph (c->font, ab, 0, &glyph);
74
75   /* If recomposing and the single char is supported by the font, we're good. */
76   if (recompose && has_this)
77     return TRUE;
78
79   if (hb_unicode_decompose (c->buffer->unicode, ab, &a, &b) &&
80       hb_font_get_glyph (c->font, b, 0, &glyph) &&
81       decompose (c, recompose, a))
82   {
83     /* Successfully decomposed. */
84
85     if (recompose) {
86       /* Try composing b with base if not blocked */
87
88     }
89
90     return TRUE;
91   }
92
93   return has_this;
94 }
95
96 static bool
97 decompose_single_char_cluster (hb_ot_shape_context_t *c,
98                                bool recompose,
99                                unsigned int i)
100 {
101 //  c->buffer->copy ();
102 //  bool ret = decompose (c, recompose, c->buffer->info[i].codepoint);
103 //  c->buffer->skip ();
104 //  return ret;
105   return FALSE;
106 }
107
108 static void
109 handle_single_char_cluster (hb_ot_shape_context_t *c,
110                             bool recompose,
111                             unsigned int i)
112 {
113   /* Decompose */
114   decompose_single_char_cluster (c, recompose, i);
115 }
116
117 static void
118 handle_multi_char_cluster (hb_ot_shape_context_t *c,
119                            bool recompose,
120                            unsigned int start,
121                            unsigned int end)
122 {
123   /* TODO Currently if there's a variation-selector we give-up, it's just too hard. */
124   for (unsigned int i = start; i < end; i++)
125     if (unlikely (is_variation_selector (c->buffer->info[i].codepoint)))
126       return;
127
128 }
129
130 void
131 _hb_ot_shape_normalize (hb_ot_shape_context_t *c)
132 {
133   hb_buffer_t *buffer = c->buffer;
134   bool recompose = !hb_ot_shape_complex_prefer_decomposed (c->plan->shaper);
135
136   buffer->clear_output ();
137
138   unsigned int count = buffer->len;
139   for (buffer->idx = 0; buffer->idx < count;)
140   {
141
142     unsigned int end;
143     for (end = buffer->idx + 1; end < count; end++)
144       if (buffer->info[buffer->idx].cluster != buffer->info[end].cluster)
145         break;
146
147     if (buffer->idx + 1 == end)
148       handle_single_char_cluster (c, recompose, buffer->idx);
149     else
150       handle_multi_char_cluster (c, recompose, buffer->idx, end);
151     while (buffer->idx < end)
152       c->buffer->next_glyph ();
153   }
154
155   buffer->swap_buffers ();
156 }
157
158 HB_END_DECLS