summaryrefslogtreecommitdiff
path: root/support/dktools/dk4fontc.c
blob: f970c2564942df6555a45eb22ac8f87682eb463d (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
Copyright (C) 2017-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4fontc.ctr
*/

/**	@file dk4fontc.c The dk4fontc module.
*/


#line 169 "dk4fontc.ctr"

#include "dk4conf.h"

#ifndef	DK4FONTC_H_INCLUDED
#include "dk4fontc.h"
#endif

#ifndef	DK4MEM_H_INCLUDED
#include "dk4mem.h"
#endif

#ifndef	DK4UNUSED_H_INCLUDED
#include "dk4unused.h"
#endif

#ifndef	DK4MATH_H_INCLUDED
#include "dk4math.h"
#endif

#ifndef	DK4NUMCO_H_INCLUDED
#include "dk4numco.h"
#endif

#if	DK4_HAVE_MATH_H
#ifndef	MATH_H_INCLUDED
#if	DK4_ON_WINDOWS
#ifndef	_USE_MATH_DEFINES
#define	_USE_MATH_DEFINES 1
#endif
#endif
#include <math.h>
#define	MATH_H_INCLUDED 1
#endif
#endif

#ifndef	GRA_H_INCLUDED
#include "gra.h"
#endif

#if DK4_HAVE_ASSERT_H
#ifndef	ASSERT_H_INCLUDED
#include <assert.h>
#define	ASSERT_H_INCLUDED 1
#endif
#endif


/**	Minimum size difference required to see different font sizes.
*/
#define	FNT_EPS	1.0e-4




#line 222 "dk4fontc.ctr"



/**	Initialize a font item.
	@param	ptr	Item to initialize.
*/
static
void
dk4fontc_item_init(dk4_font_item_t *ptr)
{
	

#line 233 "dk4fontc.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != ptr);
#endif
	dk4mem_reset(ptr, sizeof(dk4_font_item_t), NULL);
	ptr->fsz = 0.0;
	ptr->nf  = (size_t)0UL;
	ptr->fno = DK4_FONT_TIMES_ROMAN;
	ptr->fex = DK4_GRA_TF_NONE_NONE;
	

#line 242 "dk4fontc.ctr"
}



static
int
dk4_font_item_equal_fex(
	const dk4_font_item_t *pl,
	const dk4_font_item_t *pr
)
{
	int		back	= 0;
	if (DK4_GRA_TF_NONE_NONE != pl->fex) {
		switch (pl->fex) {
			case DK4_GRA_TF_NONE_NONE : {
				/* Intentionally empty to avoid compiler warnings. */
			} break;
			case DK4_GRA_TF_EXACT_SIZE : case DK4_GRA_TF_SIMILAR_SIZE : {
				if (pl->fno > pr->fno) {
					back = 1;
				}
				else {
					if (pl->fno < pr->fno) {
						back = -1;
					}
				}
				if (0 == back) {
					if (isgreater(pl->fsz, (FNT_EPS + pr->fsz))) {
						back = 1;
					}
					else {
						if (isgreater(pr->fsz, (FNT_EPS + pl->fsz))) {
							back = -1;
						}
					}
				}
			} break;
			case DK4_GRA_TF_EXACT_NONE : case DK4_GRA_TF_SIMILAR_NONE : {
				if (pl->fno > pr->fno) {
					back = 1;
				}
				else {
					if (pl->fno < pr->fno) {
						back = -1;
					}
				}
			} break;
			default : {	/* DK4_GRA_TF_NONE_SIZE */
				if (isgreater(pl->fsz, (FNT_EPS + pr->fsz))) {
					back = 1;
				}
				else {
					if (isgreater(pr->fsz, (FNT_EPS + pl->fsz))) {
						back = -1;
					}
				}
			} break;
		}
	}
	return back;
}



/**	Compare two font items by font number or by font id.
	@param	l	Left font item.
	@param	r	Right font item.
	@param	cr	Comparison criteria (1=by number,0=normal).
	@return	Comparison result.
*/
static
int
dk4_font_item_compare_by_fontno_and_size(
	const void	*l,
	const void	*r,
	int			 cr
)
{
	int		back	= 0;
	const dk4_font_item_t	*pl;		/* Left object */
	const dk4_font_item_t	*pr;		/* Right object */

	

#line 325 "dk4fontc.ctr"
	pl = (const dk4_font_item_t *)l;
	pr = (const dk4_font_item_t *)r;
	if (NULL != pl) {
		if (NULL != pr) {
			switch (cr) {
				case 1 : {
					if (pl->nf > pr->nf) {
						back = 1;
					}
					else {
						if (pl->nf < pr->nf) {
							back = -1;
						}
					}
				} break;
				default : {
					/*
						First check domain (exact font or similar font)
					*/
					if (pl->fex > pr->fex) {
						back = 1;
					}
					else {
						if (pl->fex < pr->fex) {
							back = -1;
						}
					}
					if (0 == back) {
						back = dk4_font_item_equal_fex(pl, pr);
					}
				} break;
			}
		}
		else {
			back = 1;
		}
	}
	else {
		if (NULL != pr) {
			back = -1;
		}
	}
	

#line 368 "dk4fontc.ctr"
	return back;
}



/**	Initialize new font collector.
	@param	ptr	Font collector to initialize.
*/
static
void
dk4fontc_init(dk4_font_collector_t *ptr)
{
	

#line 381 "dk4fontc.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != ptr);
#endif
	ptr->s_fo = ptr->s_fn = NULL;
	ptr->i_fo = ptr->i_fn = NULL;
	ptr->nfonts	= 0;
	

#line 388 "dk4fontc.ctr"
}



void
dk4fontc_close(
	dk4_font_collector_t	*fc
)
{
	dk4_font_item_t	*fi;		/* Current font item to release */

	

#line 400 "dk4fontc.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != fc);
#endif
	if (NULL != fc) {
		/*
			s_fn only contains copy
		*/
		if (NULL != fc->s_fn) {
			if (NULL != fc->i_fn) {
				dk4sto_it_close(fc->i_fn);
			}
			dk4sto_close(fc->s_fn);
		}
		fc->s_fn = NULL;
		fc->i_fn = NULL;
		/*
			s_fo contains the real data, we must delete here
		*/
		if (NULL != fc->s_fo) {
			if (NULL != fc->i_fo) {
				dk4sto_it_reset(fc->i_fo);
				do {
					fi = (dk4_font_item_t *)dk4sto_it_next(fc->i_fo);
					if (NULL != fi) {
						

#line 425 "dk4fontc.ctr"
						dk4mem_free(fi);
					}
				} while(NULL != fi);
			}
			dk4sto_close(fc->s_fo);
		}
		fc->s_fo = NULL;
		fc->i_fo = NULL;
		dk4fontc_init(fc);
		dk4mem_free(fc);
	}
	

#line 437 "dk4fontc.ctr"
}



dk4_font_collector_t *
dk4fontc_open(
	dk4_er_t				*erp
)
{
	dk4_font_collector_t	*back	= NULL;
	int						 ok		= 0;
	
	

#line 450 "dk4fontc.ctr"
	back = dk4mem_new(dk4_font_collector_t,1,erp);
	if (NULL != back) {
		dk4fontc_init(back);
		back->s_fo = dk4sto_open(erp);
		back->s_fn = dk4sto_open(erp);
		if ((NULL != back->s_fo) && (NULL != back->s_fn)) {
			dk4sto_set_comp(
				back->s_fo, dk4_font_item_compare_by_fontno_and_size, 0
			);
			dk4sto_set_comp(
				back->s_fn, dk4_font_item_compare_by_fontno_and_size, 1
			);
			back->i_fo = dk4sto_it_open(back->s_fo, erp);
			back->i_fn = dk4sto_it_open(back->s_fn, erp);
			if ((NULL != back->i_fo) && (NULL != back->i_fn)) {
				ok = 1;
			}
		}
		if (0 == ok) {
			dk4fontc_close(back);
			back = NULL;
		}
	}
	

#line 474 "dk4fontc.ctr"
	return back;
}



int
dk4fontc_add_font(
	size_t					*pdst,
	dk4_font_collector_t	*fc,
	int						 fno,
	double					 fsz,
	int						 fex,
	dk4_er_t				*erp
)
{
	dk4_font_item_t		 tv;			/* Test value */
	dk4_font_item_t		*ptr;			/* Item found */
	dk4_font_number_t	 rfno;			/* Real font number */
	int					 back	= 0;	/* Function result */

	

#line 495 "dk4fontc.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != fc);
	assert(NULL != pdst);
	assert(0 <= fno);
	assert(0.0 < fsz);
#endif
	/*
		Check command line arguments
	*/
	if ((NULL == pdst) || (NULL == fc) || (0 > fno) || (0.0 >= fsz)) {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	else {
		/*	Initialize search record
		*/
		dk4fontc_item_init(&tv);
		tv.fsz = fsz;
		tv.nf  = 0;
		tv.fex = fex;
		rfno = dk4font_number_from_int(fno);
		switch (fex) {
			case DK4_GRA_TF_SIMILAR_NONE : case DK4_GRA_TF_SIMILAR_SIZE : {
				tv.fno = dk4font_get_features(rfno);
			} break;
			default : {
				tv.fno = fno;
			} break;
		}
		/*
			Search
		*/
		ptr = (dk4_font_item_t *)dk4sto_it_find_like(fc->i_fo, &tv, 0);
		/*
			Success if found, otherwise create new entry
		*/
		if (NULL != ptr) {
			back = 1;
			*pdst = ptr->nf;
		}
		else {
			if (SIZE_MAX > fc->nfonts) {
				ptr = dk4mem_new(dk4_font_item_t,1,erp);
				if (NULL != ptr) {
					DK4_MEMCPY(ptr,&tv,sizeof(dk4_font_item_t));
					ptr->nf = fc->nfonts;
					if (0 != dk4sto_add(fc->s_fo, ptr, erp)) {
						fc->nfonts += 1;
						if (0 != dk4sto_add(fc->s_fn, ptr, erp)) {
							back = 1;
							*pdst = ptr->nf;
						}
					}
					else {
						dk4mem_free(ptr);
					}
				}
			}
			else {
				dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW);
			}
		}
	}
	

#line 558 "dk4fontc.ctr"
	return back;
}


/* vim: set ai sw=4 ts=4 : */