Reduce memory consumption.
[ttfautohint.git] / lib / tasort.c
blob3ed2d1a1537f1a64b3fcdad4c85f60df951bf141
1 /* tasort.c */
3 /*
4 * Copyright (C) 2011-2012 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;
31 FT_UInt j;
32 FT_Pos swap;
35 for (i = 1; i < count; i++)
37 for (j = i; j > 0; j--)
39 if (table[j] >= table[j - 1])
40 break;
42 swap = table[j];
43 table[j] = table[j - 1];
44 table[j - 1] = swap;
50 void
51 ta_sort_and_quantize_widths(FT_UInt* count,
52 TA_Width table,
53 FT_Pos threshold)
55 FT_UInt i;
56 FT_UInt j;
57 FT_UInt cur_idx;
58 FT_Pos cur_val;
59 FT_Pos sum;
60 TA_WidthRec swap;
63 if (*count == 1)
64 return;
66 /* sort */
67 for (i = 1; i < *count; i++)
69 for (j = i; j > 0; j--)
71 if (table[j].org >= table[j - 1].org)
72 break;
74 swap = table[j];
75 table[j] = table[j - 1];
76 table[j - 1] = swap;
80 cur_idx = 0;
81 cur_val = table[cur_idx].org;
83 /* compute and use mean values for clusters not larger than `threshold'; */
84 /* this is very primitive and might not yield the best result, */
85 /* but normally, using reference character `o', `*count' is 2, */
86 /* so the code below is fully sufficient */
87 for (i = 1; i < *count; i++)
89 if (table[i].org - cur_val > threshold
90 || i == *count - 1)
92 sum = 0;
94 /* fix loop for end of array */
95 if (table[i].org - cur_val <= threshold
96 && i == *count - 1)
97 i++;
99 for (j = cur_idx; j < i; j++)
101 sum += table[j].org;
102 table[j].org = 0;
104 table[cur_idx].org = sum / j;
106 if (i < *count - 1)
108 cur_idx = i + 1;
109 cur_val = table[cur_idx].org;
114 cur_idx = 1;
116 /* compress array to remove zero values */
117 for (i = 1; i < *count; i++)
119 if (table[i].org)
120 table[cur_idx++] = table[i];
123 *count = cur_idx;
126 /* end of tasort.c */