Run `update-copyright' script.
[ttfautohint.git] / lib / tasort.c
blobde43948c11a753645417d95c4a8e53ff95a0de23
1 /* tasort.c */
3 /*
4 * Copyright (C) 2011-2018 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 /* originally file `afangles.c' (2011-Mar-28) from FreeType */
18 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
20 #include "tatypes.h"
21 #include "tasort.h"
24 /* two bubble sort routines */
26 void
27 ta_sort_pos(FT_UInt count,
28 FT_Pos* table)
30 FT_UInt i, j;
31 FT_Pos swap;
34 for (i = 1; i < count; i++)
36 for (j = i; j > 0; j--)
38 if (table[j] >= table[j - 1])
39 break;
41 swap = table[j];
42 table[j] = table[j - 1];
43 table[j - 1] = swap;
49 void
50 ta_sort_and_quantize_widths(FT_UInt* count,
51 TA_Width table,
52 FT_Pos threshold)
54 FT_UInt i;
55 FT_UInt j;
56 FT_UInt cur_idx;
57 FT_Pos cur_val;
58 FT_Pos sum;
59 TA_WidthRec swap;
62 if (*count == 1)
63 return;
65 /* sort */
66 for (i = 1; i < *count; i++)
68 for (j = i; j > 0; j--)
70 if (table[j].org >= table[j - 1].org)
71 break;
73 swap = table[j];
74 table[j] = table[j - 1];
75 table[j - 1] = swap;
79 cur_idx = 0;
80 cur_val = table[cur_idx].org;
82 /* compute and use mean values for clusters not larger than `threshold'; */
83 /* this is very primitive and might not yield the best result, */
84 /* but normally, using reference character `o', `*count' is 2, */
85 /* so the code below is fully sufficient */
86 for (i = 1; i < *count; i++)
88 if (table[i].org - cur_val > threshold
89 || i == *count - 1)
91 sum = 0;
93 /* fix loop for end of array */
94 if (table[i].org - cur_val <= threshold
95 && i == *count - 1)
96 i++;
98 for (j = cur_idx; j < i; j++)
100 sum += table[j].org;
101 table[j].org = 0;
103 table[cur_idx].org = sum / (FT_Pos)j;
105 if (i < *count - 1)
107 cur_idx = i + 1;
108 cur_val = table[cur_idx].org;
113 cur_idx = 1;
115 /* compress array to remove zero values */
116 for (i = 1; i < *count; i++)
118 if (table[i].org)
119 table[cur_idx++] = table[i];
122 *count = cur_idx;
125 /* end of tasort.c */