1 /******************************************************************************
2 ** Filename: featdefs.c
3 ** Purpose: Definitions of currently defined feature types.
4 ** Author: Dan Johnson
5 ** History: Mon May 21 10:26:21 1990, DSJ, Created.
6 **
7 ** (c) Copyright Hewlett-Packard Company, 1988.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 ******************************************************************************/
18 /**----------------------------------------------------------------------------
19 Include Files and Type Defines
20 ----------------------------------------------------------------------------**/
21 #include "featdefs.h"
22 #include "emalloc.h"
23 #include "danerror.h"
24 #include "scanutils.h"
25
26 #include <string.h>
27 #include <stdio.h>
28
29 /* define errors triggered by this module */
30 #define ILLEGAL_NUM_SETS 3001
31
32 #define PICO_FEATURE_LENGTH 0.05
33 #define MAX_OUTLINE_FEATURES 100
34
35 /**----------------------------------------------------------------------------
36 Global Data Definitions and Declarations
37 ----------------------------------------------------------------------------**/
38 /* define all of the parameters for the MicroFeature type*/
39 StartParamDesc (MicroFeatureParams)
40 DefineParam (0, 0, -0.5, 0.5)
41 DefineParam (0, 0, -0.25, 0.75)
42 DefineParam (0, 1, 0.0, 1.0)
43 DefineParam (1, 0, 0.0, 1.0)
44 DefineParam (0, 1, -0.5, 0.5)
45 DefineParam (0, 1, -0.5, 0.5)
46 EndParamDesc
47 /* now define the feature type itself (see features.h for info about each
48 parameter).*/
49 DefineFeature (MicroFeatureDesc, 5, 1, 4, 50, "Micro", "mf", MicroFeatureParams)
50
51 // define all of the parameters for the PicoFeature type
52 /* define knob that can be used to adjust pico-feature length */
53 FLOAT32 PicoFeatureLength = PICO_FEATURE_LENGTH;
54 StartParamDesc (PicoFeatParams)
55 DefineParam (0, 0, -0.25, 0.75)
56 DefineParam (1, 0, 0.0, 1.0)
57 DefineParam (0, 0, -0.5, 0.5)
58 EndParamDesc
59 /* now define the feature type itself (see features.h for info about each
60 parameter).*/
61 DefineFeature (PicoFeatDesc, 2, 1, 1, MAX_UINT8, "Pico", "pf", PicoFeatParams)
62
63 /* define all of the parameters for the NormFeat type*/
64 StartParamDesc (CharNormParams)
65 DefineParam (0, 0, -0.25, 0.75)
66 DefineParam (0, 1, 0.0, 1.0)
67 DefineParam (0, 1, 0.0, 1.0)
68 DefineParam (0, 1, 0.0, 1.0)
69 EndParamDesc
70 /* now define the feature type itself (see features.h for info about each
71 parameter).*/
72 DefineFeature (CharNormDesc, 4, 0, 1, 1, "CharNorm", "cn", CharNormParams)
73
74 // define all of the parameters for the OutlineFeature type
75 StartParamDesc (OutlineFeatParams)
76 DefineParam (0, 0, -0.5, 0.5)
77 DefineParam (0, 0, -0.25, 0.75)
78 DefineParam (0, 0, 0.0, 1.0)
79 DefineParam (1, 0, 0.0, 1.0)
80 EndParamDesc
81 /* now define the feature type itself (see features.h for info about each
82 parameter).*/
83 DefineFeature (OutlineFeatDesc, 3, 1, 1, MAX_OUTLINE_FEATURES, "Outline",
84 "of", OutlineFeatParams)
85
86 /**----------------------------------------------------------------------------
87 Global Data Definitions and Declarations
88 ----------------------------------------------------------------------------**/
89 FEATURE_DEFS_STRUCT FeatureDefs = {
90 NUM_FEATURE_TYPES,
91 {
92 &MicroFeatureDesc,
93 &PicoFeatDesc,
94 &OutlineFeatDesc,
95 &CharNormDesc
96 }
97 };
98
99 /**----------------------------------------------------------------------------
100 Public Code
101 ----------------------------------------------------------------------------**/
102 /*---------------------------------------------------------------------------*/
FreeCharDescription(CHAR_DESC CharDesc)103 void FreeCharDescription(CHAR_DESC CharDesc) {
104 /*
105 ** Parameters:
106 ** CharDesc character description to be deallocated
107 ** Globals: none
108 ** Operation: Release the memory consumed by the specified character
109 ** description and all of the features in that description.
110 ** Return: none
111 ** Exceptions: none
112 ** History: Wed May 23 13:52:19 1990, DSJ, Created.
113 */
114 int i;
115
116 if (CharDesc) {
117 for (i = 0; i < CharDesc->NumFeatureSets; i++)
118 FreeFeatureSet (CharDesc->FeatureSets[i]);
119 Efree(CharDesc);
120 }
121 } /* FreeCharDescription */
122
123
124 /*---------------------------------------------------------------------------*/
NewCharDescription()125 CHAR_DESC NewCharDescription() {
126 /*
127 ** Parameters: none
128 ** Globals: none
129 ** Operation: Allocate a new character description, initialize its
130 ** feature sets to be empty, and return it.
131 ** Return: New character description structure.
132 ** Exceptions: none
133 ** History: Wed May 23 15:27:10 1990, DSJ, Created.
134 */
135 CHAR_DESC CharDesc;
136 int i;
137
138 CharDesc = (CHAR_DESC) Emalloc (sizeof (CHAR_DESC_STRUCT));
139 CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;
140
141 for (i = 0; i < CharDesc->NumFeatureSets; i++)
142 CharDesc->FeatureSets[i] = NULL;
143
144 return (CharDesc);
145
146 } /* NewCharDescription */
147
148
149 /*---------------------------------------------------------------------------*/
WriteCharDescription(FILE * File,CHAR_DESC CharDesc)150 void WriteCharDescription(FILE *File, CHAR_DESC CharDesc) {
151 /*
152 ** Parameters:
153 ** File open text file to write CharDesc to
154 ** CharDesc character description to write to File
155 ** Globals: none
156 ** Operation: Write a textual representation of CharDesc to File.
157 ** The format used is to write out the number of feature
158 ** sets which will be written followed by a representation of
159 ** each feature set.
160 ** Each set starts with the short name for that feature followed
161 ** by a description of the feature set. Feature sets which are
162 ** not present are not written.
163 ** Return: none
164 ** Exceptions: none
165 ** History: Wed May 23 17:21:18 1990, DSJ, Created.
166 */
167 int Type;
168 int NumSetsToWrite = 0;
169
170 for (Type = 0; Type < CharDesc->NumFeatureSets; Type++)
171 if (CharDesc->FeatureSets[Type])
172 NumSetsToWrite++;
173
174 fprintf (File, " %d\n", NumSetsToWrite);
175 for (Type = 0; Type < CharDesc->NumFeatureSets; Type++)
176 if (CharDesc->FeatureSets[Type]) {
177 fprintf (File, "%s ", (FeatureDefs.FeatureDesc[Type])->ShortName);
178 WriteFeatureSet (File, CharDesc->FeatureSets[Type]);
179 }
180 } /* WriteCharDescription */
181
182
183 /*---------------------------------------------------------------------------*/
ReadCharDescription(FILE * File)184 CHAR_DESC ReadCharDescription(FILE *File) {
185 /*
186 ** Parameters:
187 ** File open text file to read character description from
188 ** Globals: none
189 ** Operation: Read a character description from File, and return
190 ** a data structure containing this information. The data
191 ** is formatted as follows:
192 ** NumberOfSets
193 ** ShortNameForSet1 Set1
194 ** ShortNameForSet2 Set2
195 ** ...
196 ** Return: Character description read from File.
197 ** Exceptions: ILLEGAL_NUM_SETS
198 ** History: Wed May 23 17:32:48 1990, DSJ, Created.
199 */
200 int NumSetsToRead;
201 char ShortName[FEAT_NAME_SIZE];
202 CHAR_DESC CharDesc;
203 int Type;
204
205 if (fscanf (File, "%d", &NumSetsToRead) != 1 ||
206 NumSetsToRead < 0 || NumSetsToRead > FeatureDefs.NumFeatureTypes)
207 DoError (ILLEGAL_NUM_SETS, "Illegal number of feature sets");
208
209 CharDesc = NewCharDescription ();
210 for (; NumSetsToRead > 0; NumSetsToRead--) {
211 fscanf (File, "%s", ShortName);
212 Type = ShortNameToFeatureType (ShortName);
213 CharDesc->FeatureSets[Type] =
214 ReadFeatureSet (File, FeatureDefs.FeatureDesc[Type]);
215 }
216 return (CharDesc);
217
218 } // ReadCharDescription
219
220
221 /*---------------------------------------------------------------------------*/
ShortNameToFeatureType(const char * ShortName)222 int ShortNameToFeatureType(const char *ShortName) {
223 /*
224 ** Parameters:
225 ** ShortName short name of a feature type
226 ** Globals: none
227 ** Operation: Search thru all features currently defined and return
228 ** the feature type for the feature with the specified short
229 ** name. Trap an error if the specified name is not found.
230 ** Return: Feature type which corresponds to ShortName.
231 ** Exceptions: ILLEGAL_SHORT_NAME
232 ** History: Wed May 23 15:36:05 1990, DSJ, Created.
233 */
234 int i;
235
236 for (i = 0; i < FeatureDefs.NumFeatureTypes; i++)
237 if (!strcmp ((FeatureDefs.FeatureDesc[i]->ShortName), ShortName))
238 return (i);
239 DoError (ILLEGAL_SHORT_NAME, "Illegal short name for a feature");
240 return 0;
241
242 } // ShortNameToFeatureType
243