File: util.c

package info (click to toggle)
chinput 3.0.2-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,124 kB
  • ctags: 2,806
  • sloc: ansic: 52,549; makefile: 165; perl: 23
file content (154 lines) | stat: -rw-r--r-- 4,785 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* -*- mode: C; c-file-style: "bsd"; tab-width: 4 -*- */
/* util.c - Functions shared between handwriting engine and UI
 * JStroke 1.x - Japanese Kanji handwriting recognition technology demo.
 * Copyright (C) 1997  Robert E. Wells
 * http://wellscs.com/pilot
 * mailto:robert@wellscs.com
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program (gpl.html); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Derived from prior work by Todd David Rudick on JavaDict and StrokeDic.
 * Makes use of KANJIDIC data from Jim Breen of Monash University.
 * Further credit details available at http://wellscs.com/pilot
 * See readme.txt, changelo, and gpl.html for more information.
 * -------------------------------------------------------------------------*/

#include "jstroke.h"
#include "jstrokerc.h"

/* ----- AppEmptyList ------------------------------------------------------*/

ListMem*  AppEmptyList() {
	UInt      iTotSize;
	ListMem*  pListMem;
	CharPtr   cpList;
	CharPtr*  cppList;

	iTotSize = sizeof(ListMem) + 2 * sizeof(CharPtr) + 1;

	if (!(cpList = MemPtrNew(iTotSize))) {
		ErrBox("ERROR: no mem in top picks");
		return NULL;
	}

	pListMem = (ListMem *) cpList;
	cpList += sizeof(ListMem);
	
	cppList = (CharPtr *) cpList;
	cpList += 2 * sizeof(CharPtr);
	
	pListMem->m_argc = 1;
	pListMem->m_argv = cppList;

	cppList[0] = cpList;
	cppList[1] = NULL;
	
	*cpList = '\0';

	return pListMem;
}

/* ----- Angle32 -------------------------------------------------------------
 * For given int xdif and ydif, calculate atan2 (the angle from origin)
 * in 32nd's of a circle from 0 to 32, rather than radians.  Note that it 
 * returns 32 iff xdif and ydif are both zero, an ill-defined case.
 * Origin and direction are clockwise:
 * 0 => 12:00, 8 => 3:00, 16 => 6:00, 24 => 9:00.
 * Why 32nds?  So we can divide them into 8 pieces evenly, and so we get
 * 4x over-sampling for the 8th's in the path descriptions...
 * -rwells, 970713.
 */

Long Angle32(Long xdif, Long ydif) {
	Boolean xneg, yneg, xyflip;
	Long i32nd, xtmp, islope;

    if ((xneg = (xdif < 0)))
		xdif = -xdif;
	if ((yneg = (ydif < -0.1)))
		ydif = -ydif;
	if ((xyflip = (ydif < xdif))) {
		xtmp = xdif; xdif = ydif; ydif = xtmp;
	}

    if (xdif == 0) {
		if (ydif == 0)
			return 32;
		else
			i32nd = 0;
	}
    else {
		/* The 4 comparison values were generated with the accompanying
		 * perl script, then open coded here for speed and reasonable
		 * space efficiency.  The values were chosen to make the results 
		 * match those of atan2 in rounded double
		 * precision floating point.  -rwells, 970713.
		 */

		islope = (100 * xdif) / ydif;
		if (islope < 54) {             /* test #2, first test. */
			if (islope < 10)           /* test #0, second test. */
				i32nd = 0;             /*          got  #0 after 2 tests. */
			else if (islope < 31)      /* test #1, third test. */
				i32nd = 1;             /*          got  #1 after 3 tests. */
			else
				i32nd = 2;             /*          got  #2 after 3 tests. */
		}
		else if (islope < 83)          /* test #3, second test. */
			i32nd = 3;                 /*          got  #3 after 2 tests. */
		else
			i32nd = 4;                 /*          got  #4 after 2 tests. */
	}

	if (xyflip)
		i32nd = (8 - i32nd);
	if (yneg)
		i32nd = (16 - i32nd);
	if (xneg)
		i32nd = (32 - i32nd);

    return i32nd % 32;
}

#ifdef FOR_PILOT_COMPAT

/* ----- ErrBox ------------------------------------------------------------*/

void ErrBox(CharPtr msg) {
	fprintf(stderr,"%s\n",msg);
}

/* ----- ErrBox2 -----------------------------------------------------------*/

void ErrBox2(CharPtr msg1, CharPtr msg2) {
	fprintf(stderr,"%s\n%s\n",msg1,msg2);
}

#else /* !FOR_PILOT_COMPAT */

/* ----- ErrBox ------------------------------------------------------------*/

void ErrBox(CharPtr msg) {
	(void) FrmCustomAlert(alertID_ErrBox, msg, "", "");
}

/* ----- ErrBox2 -----------------------------------------------------------*/

void ErrBox2(CharPtr msg1, CharPtr msg2) {
	(void) FrmCustomAlert(alertID_ErrBox, msg1, msg2, "");
}

#endif /* FOR_PILOT_COMPAT */
/* ----- End of util.c ---------------------------------------------------- */