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 
  38 U_NAMESPACE_BEGIN
  39 
  40 void 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 
  59 void 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(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     for (i = 0; i < charCount; i += 1) {
  70       combiningClasses[i] = classTable->getGlyphClass(classTable, (LEGlyphID) inChars[i], success);
  71         indices[i] = i;
  72     }
  73 
  74     for (i = 0; i < charCount; i += 1) {
  75         if (combiningClasses[i] != 0) {
  76             le_int32 mark;
  77 
  78             for (mark = i; mark < charCount; mark += 1) {
  79                 if (combiningClasses[mark] == 0) {
  80                     break;
  81                 }
  82             }
  83 
  84             sortMarks(indices, combiningClasses, i, mark);
  85         }
  86     }
  87 
  88     le_int32 out = 0, dir = 1;
  89 
  90     if (rightToLeft) {
  91         out = charCount - 1;
  92         dir = -1;
  93     }
  94 
  95     for (i = 0; i < charCount; i += 1, out += dir) {
  96         le_int32 index = indices[i];
  97 
  98         outChars[i] = inChars[index];
  99         glyphStorage.setCharIndex(out, index, success);
 100     }
 101 
 102     LE_DELETE_ARRAY(indices);
 103     LE_DELETE_ARRAY(combiningClasses);
 104 }
 105 
 106 U_NAMESPACE_END