1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation.  Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26/*
27 *
28 * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
29 *
30 */
31
32#include "LETypes.h"
33#include "LEGlyphStorage.h"
34#include "CanonShaping.h"
35#include "GlyphDefinitionTables.h"
36#include "ClassDefinitionTables.h"
37
38U_NAMESPACE_BEGIN
39
40void CanonShaping::sortMarks(le_int32 *indices, const le_int32 *combiningClasses, le_int32 index, le_int32 limit)
41{
42    for (le_int32 j = index + 1; j < limit; j += 1) {
43        le_int32 i;
44        le_int32 v = indices[j];
45        le_int32 c = combiningClasses[v];
46
47        for (i = j - 1; i >= index; i -= 1) {
48            if (c >= combiningClasses[indices[i]]) {
49                break;
50            }
51
52            indices[i + 1] = indices[i];
53        }
54
55        indices[i + 1] = v;
56    }
57}
58
59void CanonShaping::reorderMarks(const LEUnicode *inChars, le_int32 charCount, le_bool rightToLeft,
60                                LEUnicode *outChars, LEGlyphStorage &glyphStorage)
61{
62    LEErrorCode success = LE_NO_ERROR;
63    LEReferenceTo<GlyphDefinitionTableHeader> gdefTable(LETableReference::kStaticData, CanonShaping::glyphDefinitionTable, CanonShaping::glyphDefinitionTableLen);
64    LEReferenceTo<ClassDefinitionTable> classTable = gdefTable->getMarkAttachClassDefinitionTable(gdefTable, success);
65    le_int32 *combiningClasses = LE_NEW_ARRAY(le_int32, charCount);
66    le_int32 *indices = LE_NEW_ARRAY(le_int32, charCount);
67    le_int32 i;
68
69    if (combiningClasses == NULL || indices == NULL) {
70        if (combiningClasses != NULL) {
71            LE_DELETE_ARRAY(combiningClasses);
72        }
73        if (indices != NULL) {
74            LE_DELETE_ARRAY(indices);
75        }
76        return;
77    }
78
79    for (i = 0; i < charCount; i += 1) {
80      combiningClasses[i] = classTable->getGlyphClass(classTable, (LEGlyphID) inChars[i], success);
81        indices[i] = i;
82    }
83
84    for (i = 0; i < charCount; i += 1) {
85        if (combiningClasses[i] != 0) {
86            le_int32 mark;
87
88            for (mark = i; mark < charCount; mark += 1) {
89                if (combiningClasses[mark] == 0) {
90                    break;
91                }
92            }
93
94            sortMarks(indices, combiningClasses, i, mark);
95        }
96    }
97
98    le_int32 out = 0, dir = 1;
99
100    if (rightToLeft) {
101        out = charCount - 1;
102        dir = -1;
103    }
104
105    for (i = 0; i < charCount; i += 1, out += dir) {
106        le_int32 index = indices[i];
107
108        outChars[i] = inChars[index];
109        glyphStorage.setCharIndex(out, index, success);
110    }
111
112    LE_DELETE_ARRAY(indices);
113    LE_DELETE_ARRAY(combiningClasses);
114}
115
116U_NAMESPACE_END
117