Initial check-in
[him-cellwriter] / src / recognize.h
1
2 /*
3
4 cellwriter -- a character recognition input method
5 Copyright (C) 2007 Michael Levin <risujin@risujin.org>
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 */
22
23 /*
24         Stroke data
25 */
26
27 /* Maximum number of points a stroke can have */
28 #define POINTS_MAX 256
29
30 /* Scale of the point coordinates */
31 #define SCALE    256
32 #define MAX_DIST 362 /* sqrt(2) * SCALE */
33
34 /* Maximum number of strokes a sample can have */
35 #define STROKES_MAX 32
36
37 /* Largest value the gluable matrix entries can take */
38 #define GLUABLE_MAX 255
39
40 typedef struct {
41         signed char x, y;
42         ANGLE angle;
43 } Point;
44
45 typedef struct {
46         Vec2 center;
47         float distance;
48         int len, size, spread;
49         unsigned char processed,
50                       gluable_start[STROKES_MAX], gluable_end[STROKES_MAX];
51         signed char min_x, max_x, min_y, max_y;
52         Point points[];
53 } Stroke;
54
55 /* Stroke allocation */
56 Stroke *stroke_new(int size);
57 Stroke *stroke_clone(const Stroke *src, int reverse);
58 void stroke_free(Stroke *stroke);
59 void clear_stroke(Stroke *stroke);
60
61 /* Stroke manipulation */
62 void process_stroke(Stroke *stroke);
63 void draw_stroke(Stroke **stroke, int x, int y);
64 void smooth_stroke(Stroke *s);
65 void simplify_stroke(Stroke *s);
66 Stroke *sample_stroke(Stroke *out, Stroke *in, int points, int size);
67 void sample_strokes(Stroke *a, Stroke *b, Stroke **as, Stroke **bs);
68 void glue_stroke(Stroke **a, const Stroke *b, int reverse);
69 void dump_stroke(Stroke *stroke);
70
71 /*
72         Recognition engines
73 */
74
75 /* This will prevent the word frequency table from loading */
76 /* #define DISABLE_WORDFREQ */
77
78 /* Largest allowed engine weight */
79 #define MAX_RANGE 100
80
81 /* Range of the scale value for engines */
82 #define ENGINE_SCALE STROKES_MAX
83
84 /* Minimum stroke spread distance for angle measurements */
85 #define DOT_SPREAD (SCALE / 10)
86
87 /* Maximum distance between glue points */
88 #define GLUE_DIST (SCALE / 6)
89
90 enum {
91         ENGINE_PREP,
92         ENGINE_AVGDIST,
93         ENGINE_AVGANGLE,
94 #ifndef DISABLE_WORDFREQ
95         ENGINE_WORDFREQ,
96 #endif
97         ENGINES
98 };
99
100 typedef struct {
101         const char *name;
102         void (*func)(void);
103         int range, ignore_zeros, scale, average, max;
104 } Engine;
105
106 typedef struct Cell Cell;
107
108 /* Generalized measure function */
109 typedef float (*MeasureFunc)(Stroke *a, int i, Stroke *b, int j, void *extra);
110
111 extern int ignore_stroke_order, ignore_stroke_dir, ignore_stroke_num,
112            elasticity, no_latin_alpha, wordfreq_enable;
113 extern Engine engines[ENGINES];
114
115 void engine_average(void);
116 void engine_wordfreq(void);
117 void load_wordfreq(void);
118 float measure_distance(const Stroke *a, int i, const Stroke *b, int j,
119                        const Vec2 *offset);
120 float measure_strokes(Stroke *a, Stroke *b, MeasureFunc func,
121                       void *extra, int points, int elasticity);
122
123 /*
124         Samples and characters
125 */
126
127 /* Highest range a rating can have */
128 #define RATING_MAX 32767
129 #define RATING_MIN -32767
130
131 /* Maximum number of samples we can have per character */
132 #define SAMPLES_MAX 16
133
134 /* Fine sampling parameters */
135 #define FINE_RESOLUTION 8.f
136 #define FINE_ELASTICITY 2
137
138 /* Rough sampling parameters */
139 #define ROUGH_RESOLUTION 24.f
140 #define ROUGH_ELASTICITY 0
141
142 typedef struct {
143         unsigned char valid, order[STROKES_MAX], reverse[STROKES_MAX],
144                       glue[STROKES_MAX];
145         float reach;
146 } Transform;
147
148 typedef struct {
149         int used;
150         gunichar2 ch;
151         unsigned short len;
152         short rating, ratings[ENGINES];
153         unsigned char enabled, disqualified, processed;
154         Transform transform;
155         Vec2 center;
156         float distance, penalty;
157         Stroke *strokes[STROKES_MAX], *roughs[STROKES_MAX];
158 } Sample;
159
160 extern Sample *input;
161 extern int num_disqualified, training_block, samples_max;
162
163 /* Sample list iteration */
164 void sampleiter_reset(void);
165 Sample *sampleiter_next(void);
166
167 /* Properties */
168 void process_sample(Sample *sample);
169 void center_samples(Vec2 *ac_to_bc, Sample *a, Sample *b);
170 int sample_disqualified(const Sample *sample);
171 int sample_valid(const Sample *sample, int used);
172 int char_trained(int ch);
173 int char_disabled(int ch);
174
175 /* Processing */
176 void clear_sample(Sample *sample);
177 void recognize_sample(Sample *cell, Sample **alts, int num_alts);
178 void train_sample(const Sample *cell, int trusted);
179 void untrain_char(int ch);
180 void update_enabled_samples(void);
181 void promote_sample(Sample *sample);
182 void demote_sample(Sample *sample);
183 Stroke *transform_stroke(Sample *src, Transform *tfm, int i);
184