Wed, 21 Sep 2011 06:45:26 +0000
Apply conversation theme when opening the GTK conversation. All the
parsing stuff was moved out of the theme code and into the conversation
code.
Someone (not me!) needs to check the code I commented out and see if
we really need that stuff (and then port it to WebKit/styling).
We also need to determine where to place Template.html and the rest
of our (not-yet-written) default theme.
| 4390 | 1 | /* |
| 2 | libgstroke - a GNOME stroke interface library | |
| 3 | Copyright (c) 1996,1997,1998,1999,2000,2001 Mark F. Willey, ETLA Technical | |
| 4 | ||
| 5 | See the file COPYING for distribution information. | |
| 6 | ||
| 7 | This file contains the stroke recognition algorithm. | |
| 8 | */ | |
| 9 | ||
| 10 | #include "config.h" | |
| 11 | ||
| 12 | #include <unistd.h> | |
| 13 | #include <stdlib.h> | |
| 14 | #include <stdio.h> | |
| 15 | #include <math.h> | |
| 16 | #include <glib.h> | |
| 17 | #include <gtk/gtk.h> | |
| 18 | #include "gstroke.h" | |
| 19 | #include "gstroke-internal.h" | |
| 20 | ||
| 21 | ||
| 22 | void | |
| 23 | _gstroke_init (struct gstroke_metrics *metrics) | |
| 24 | { | |
| 25 | if (metrics->pointList != NULL) { | |
| 26 | /* FIXME: does this free the data too?*/ | |
| 27 | g_slist_free (metrics->pointList); | |
| 28 | metrics->pointList = NULL; | |
| 29 | metrics->point_count = 0; | |
| 30 | } | |
| 31 | } | |
| 32 | ||
| 33 | /* figure out which bin the point falls in */ | |
| 34 | static gint | |
| 35 | _gstroke_bin (p_point point_p, gint bound_x_1, gint bound_x_2, | |
| 36 | gint bound_y_1, gint bound_y_2) | |
| 37 | { | |
| 38 | ||
| 39 | gint bin_num = 1; | |
| 40 | ||
| 41 | if (point_p->x > bound_x_1) bin_num += 1; | |
| 42 | if (point_p->x > bound_x_2) bin_num += 1; | |
| 43 | if (point_p->y > bound_y_1) bin_num += 3; | |
| 44 | if (point_p->y > bound_y_2) bin_num += 3; | |
| 45 | ||
| 46 | return bin_num; | |
| 47 | } | |
| 48 | ||
| 49 | gint | |
| 50 | _gstroke_trans (gchar *sequence, struct gstroke_metrics *metrics) | |
| 51 | { | |
| 52 | GSList *crt_elem; | |
| 53 | /* number of bins recorded in the stroke */ | |
| 54 | guint sequence_count = 0; | |
| 55 | ||
| 56 | /* points-->sequence translation scratch variables */ | |
| 57 | gint prev_bin = 0; | |
| 58 | gint current_bin = 0; | |
| 59 | gint bin_count = 0; | |
| 60 | ||
| 61 | /* flag indicating the start of a stroke - always count it in the sequence */ | |
| 62 | gint first_bin = TRUE; | |
| 63 | ||
| 64 | /* bin boundary and size variables */ | |
| 65 | gint delta_x, delta_y; | |
| 66 | gint bound_x_1, bound_x_2; | |
| 67 | gint bound_y_1, bound_y_2; | |
| 68 | ||
| 69 | ||
| 70 | /* determine size of grid */ | |
| 71 | delta_x = metrics->max_x - metrics->min_x; | |
| 72 | delta_y = metrics->max_y - metrics->min_y; | |
| 73 | ||
| 74 | /* calculate bin boundary positions */ | |
| 75 | bound_x_1 = metrics->min_x + (delta_x / 3); | |
| 76 | bound_x_2 = metrics->min_x + 2 * (delta_x / 3); | |
| 77 | ||
| 78 | bound_y_1 = metrics->min_y + (delta_y / 3); | |
| 79 | bound_y_2 = metrics->min_y + 2 * (delta_y / 3); | |
| 80 | ||
| 81 | if (delta_x > GSTROKE_SCALE_RATIO * delta_y) { | |
| 82 | bound_y_1 = (metrics->max_y + metrics->min_y - delta_x) / 2 + (delta_x / 3); | |
| 83 | bound_y_2 = (metrics->max_y + metrics->min_y - delta_x) / 2 + 2 * (delta_x / 3); | |
| 84 | } else if (delta_y > GSTROKE_SCALE_RATIO * delta_x) { | |
| 85 | bound_x_1 = (metrics->max_x + metrics->min_x - delta_y) / 2 + (delta_y / 3); | |
| 86 | bound_x_2 = (metrics->max_x + metrics->min_x - delta_y) / 2 + 2 * (delta_y / 3); | |
| 87 | } | |
| 88 | ||
| 89 | #if 0 | |
| 90 | printf ("DEBUG:: point count: %d\n", metrics->point_count); | |
| 91 | printf ("DEBUG:: metrics->min_x: %d\n", metrics->min_x); | |
| 92 | printf ("DEBUG:: metrics->max_x: %d\n", metrics->max_x); | |
| 93 | printf ("DEBUG:: metrics->min_y: %d\n", metrics->min_y); | |
| 94 | printf ("DEBUG:: metrics->max_y: %d\n", metrics->max_y); | |
| 95 | printf ("DEBUG:: delta_x: %d\n", delta_x); | |
| 96 | printf ("DEBUG:: delta_y: %d\n", delta_y); | |
| 97 | printf ("DEBUG:: bound_x_1: %d\n", bound_x_1); | |
| 98 | printf ("DEBUG:: bound_x_2: %d\n", bound_x_2); | |
| 99 | printf ("DEBUG:: bound_y_1: %d\n", bound_y_1); | |
| 100 | printf ("DEBUG:: bound_y_2: %d\n", bound_y_2); | |
| 101 | #endif | |
| 102 | ||
| 103 | /* | |
| 104 | build string by placing points in bins, collapsing bins and | |
| 105 | discarding those with too few points... */ | |
| 106 | ||
| 107 | crt_elem = metrics->pointList; | |
| 108 | while (crt_elem != NULL) | |
| 109 | { | |
| 110 | /* figure out which bin the point falls in */ | |
| 111 | ||
| 112 | /*printf ("X = %d Y = %d\n", ((p_point)crt_elem->data)->x, | |
| 113 | ((p_point)crt_elem->data)->y); */ | |
| 114 | ||
| 115 | ||
| 116 | current_bin = _gstroke_bin ((p_point)crt_elem->data, bound_x_1, | |
| 117 | bound_x_2, bound_y_1, bound_y_2); | |
| 118 | ||
| 119 | /* if this is the first point, consider it the previous bin, too. */ | |
| 120 | if (prev_bin == 0) | |
| 121 | prev_bin = current_bin; | |
| 122 | ||
| 123 | /*printf ("DEBUG:: current bin: %d x=%d y = %d\n", current_bin, | |
| 124 | ((p_point)crt_elem->data)->x, | |
| 125 | ((p_point)crt_elem->data)->y); */ | |
| 126 | ||
| 127 | if (prev_bin == current_bin) | |
| 128 | bin_count++; | |
| 129 | else { | |
| 130 | /* we are moving to a new bin -- consider adding to the sequence */ | |
| 131 | if ((bin_count > (metrics->point_count * GSTROKE_BIN_COUNT_PERCENT)) | |
| 132 | || (first_bin == TRUE)) { | |
| 133 | ||
| 6063 | 134 | /* |
| 135 | gchar val = '0' + prev_bin; | |
| 136 | printf ("%c", val);fflush (stdout); | |
| 137 | g_string_append (&sequence, &val); | |
| 138 | */ | |
| 4390 | 139 | |
| 140 | first_bin = FALSE; | |
| 141 | sequence[sequence_count++] = '0' + prev_bin; | |
| 142 | /* printf ("DEBUG:: adding sequence: %d\n", prev_bin); */ | |
| 143 | ||
| 144 | } | |
| 145 | ||
| 146 | /* restart counting points in the new bin */ | |
| 147 | bin_count=0; | |
| 148 | prev_bin = current_bin; | |
| 149 | } | |
| 150 | ||
| 151 | /* move to next point, freeing current point from list */ | |
| 152 | ||
| 153 | free (crt_elem->data); | |
| 154 | crt_elem = g_slist_next (crt_elem); | |
| 155 | } | |
| 156 | /* add the last run of points to the sequence */ | |
| 157 | sequence[sequence_count++] = '0' + current_bin; | |
| 158 | /* printf ("DEBUG:: adding final sequence: %d\n", current_bin); */ | |
| 159 | ||
| 160 | _gstroke_init (metrics); | |
| 161 | ||
| 162 | { | |
| 6063 | 163 | /* FIXME: get rid of this block |
| 164 | gchar val = '0' + current_bin; | |
| 165 | printf ("%c\n", val);fflush (stdout); | |
| 166 | g_string_append (&sequence, '\0'); | |
| 167 | */ | |
| 4390 | 168 | sequence[sequence_count] = '\0'; |
| 169 | } | |
| 170 | ||
| 171 | return TRUE; | |
| 172 | } | |
| 173 | ||
| 174 | /* my plan is to make a stroke training program where you can enter all of | |
| 175 | the variations of slop that map to a canonical set of strokes. When the | |
| 176 | application calls gstroke_canonical, it gets one of the recognized strokes, | |
| 177 | or "", if it's not a recognized variation. I will probably use a hash | |
| 178 | table. Right now, it just passes the values through to gstroke_trans */ | |
| 179 | gint | |
| 180 | _gstroke_canonical (gchar *sequence, struct gstroke_metrics *metrics) | |
| 181 | { | |
| 182 | return _gstroke_trans (sequence, metrics); | |
| 183 | } | |
| 184 | ||
| 185 | ||
| 186 | void | |
| 187 | _gstroke_record (gint x, gint y, struct gstroke_metrics *metrics) | |
| 188 | { | |
| 189 | p_point new_point_p; | |
| 190 | gint delx, dely; | |
| 191 | float ix, iy; | |
| 192 | ||
|
9843
4daa0a6b2dd0
[gaim-migrate @ 10721]
Dave West <kat@users.sourceforge.net>
parents:
6063
diff
changeset
|
193 | g_return_if_fail( metrics != NULL ); |
|
4daa0a6b2dd0
[gaim-migrate @ 10721]
Dave West <kat@users.sourceforge.net>
parents:
6063
diff
changeset
|
194 | |
| 4390 | 195 | #if 0 |
| 196 | printf ("%d:%d ", x, y); fflush (stdout); | |
| 197 | #endif | |
| 198 | ||
| 199 | if (metrics->point_count < GSTROKE_MAX_POINTS) { | |
| 200 | new_point_p = (p_point) g_malloc (sizeof (struct s_point)); | |
| 201 | ||
| 202 | if (metrics->pointList == NULL) { | |
| 203 | ||
| 204 | /* first point in list - initialize metrics */ | |
| 205 | metrics->min_x = 10000; | |
| 206 | metrics->min_y = 10000; | |
| 207 | metrics->max_x = -1; | |
| 208 | metrics->max_y = -1; | |
| 209 | ||
|
28417
43c84621ba83
GSList internally doesn't use the same allocator as gmalloc. Refs #400.
Paul Aurich <darkrain42@pidgin.im>
parents:
15435
diff
changeset
|
210 | metrics->pointList = g_slist_prepend(metrics->pointList, new_point_p); |
| 4390 | 211 | metrics->point_count = 0; |
| 212 | ||
| 213 | } else { | |
| 214 | ||
| 215 | #define LAST_POINT ((p_point)(g_slist_last (metrics->pointList)->data)) | |
| 216 | ||
| 217 | /* interpolate between last and current point */ | |
| 218 | delx = x - LAST_POINT->x; | |
| 219 | dely = y - LAST_POINT->y; | |
| 220 | ||
| 221 | if (abs(delx) > abs(dely)) { /* step by the greatest delta direction */ | |
| 222 | iy = LAST_POINT->y; | |
| 223 | ||
| 224 | /* go from the last point to the current, whatever direction it may be */ | |
| 225 | for (ix = LAST_POINT->x; (delx > 0) ? (ix < x) : (ix > x); ix += (delx > 0) ? 1 : -1) { | |
| 226 | ||
| 227 | /* step the other axis by the correct increment */ | |
| 228 | iy += fabs(((float) dely / (float) delx)) * (float) ((dely < 0) ? -1.0 : 1.0); | |
| 229 | ||
| 230 | /* add the interpolated point */ | |
| 231 | new_point_p->x = ix; | |
| 232 | new_point_p->y = iy; | |
| 12551 | 233 | metrics->pointList = g_slist_append (metrics->pointList, new_point_p); |
| 4390 | 234 | |
| 235 | /* update metrics */ | |
| 236 | if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix; | |
| 237 | if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; | |
| 238 | if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; | |
| 239 | if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; | |
| 240 | metrics->point_count++; | |
| 241 | ||
| 242 | new_point_p = (p_point) malloc (sizeof(struct s_point)); | |
| 243 | } | |
| 244 | } else { /* same thing, but for dely larger than delx case... */ | |
| 245 | ix = LAST_POINT->x; | |
| 246 | ||
| 247 | /* go from the last point to the current, whatever direction it may be | |
| 248 | */ | |
| 249 | for (iy = LAST_POINT->y; (dely > 0) ? (iy < y) : (iy > y); iy += (dely > 0) ? 1 : -1) { | |
| 250 | ||
| 251 | /* step the other axis by the correct increment */ | |
| 252 | ix += fabs(((float) delx / (float) dely)) * (float) ((delx < 0) ? -1.0 : 1.0); | |
| 253 | ||
| 254 | /* add the interpolated point */ | |
| 255 | new_point_p->y = iy; | |
| 256 | new_point_p->x = ix; | |
| 12551 | 257 | metrics->pointList = g_slist_append(metrics->pointList, new_point_p); |
| 4390 | 258 | |
| 259 | /* update metrics */ | |
| 260 | if (((gint) ix) < metrics->min_x) metrics->min_x = (gint) ix; | |
| 261 | if (((gint) ix) > metrics->max_x) metrics->max_x = (gint) ix; | |
| 262 | if (((gint) iy) < metrics->min_y) metrics->min_y = (gint) iy; | |
| 263 | if (((gint) iy) > metrics->max_y) metrics->max_y = (gint) iy; | |
| 264 | metrics->point_count++; | |
| 265 | ||
| 266 | new_point_p = (p_point) malloc (sizeof(struct s_point)); | |
| 267 | } | |
| 268 | } | |
| 269 | ||
| 270 | /* add the sampled point */ | |
| 12551 | 271 | metrics->pointList = g_slist_append(metrics->pointList, new_point_p); |
| 4390 | 272 | } |
| 273 | ||
| 274 | /* record the sampled point values */ | |
| 275 | new_point_p->x = x; | |
| 276 | new_point_p->y = y; | |
| 277 | ||
| 278 | #if 0 | |
| 279 | { | |
| 280 | GSList *crt = metrics->pointList; | |
| 281 | printf ("Record "); | |
| 282 | while (crt != NULL) | |
| 283 | { | |
| 284 | printf ("(%d,%d)", ((p_point)crt->data)->x, ((p_point)crt->data)->y); | |
| 285 | crt = g_slist_next (crt); | |
| 286 | } | |
| 287 | printf ("\n"); | |
| 288 | } | |
| 289 | #endif | |
| 290 | } | |
| 291 | } |