File: linebreak.c

package info (click to toggle)
sombok 2.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 4,520 kB
  • sloc: ansic: 38,009; sh: 3,059; perl: 769; makefile: 134
file content (591 lines) | stat: -rw-r--r-- 17,173 bytes parent folder | download | duplicates (7)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
 * linebreak.c - implementation of Linebreak object.
 * 
 * Copyright (C) 2009-2012 by Hatuka*nezumi - IKEDA Soji.
 *
 * This file is part of the Sombok Package.  This program is free
 * software; you can redistribute it and/or modify it under the terms of
 * either the GNU General Public License or the Artistic License, as
 * specified in the README file.
 *
 */

#include "sombok_constants.h"
#include "sombok.h"

/** @defgroup linebreak linebreak
 * @brief Handle linebreak object.
 *
 *@{*/

/** Constructor
 *
 * Creates new linebreak object.
 * Reference count of it will be set to 1.
 * @param[in] ref_func function to handle reference count of external objects,
 * or NULL.
 * @return New linebreak object.
 * If error occurred, errno is set then NULL is returned.
 */
linebreak_t *linebreak_new(linebreak_ref_func_t ref_func)
{
    linebreak_t *obj;
    if ((obj = malloc(sizeof(linebreak_t))) == NULL)
	return NULL;
    memset(obj, 0, sizeof(linebreak_t));

#ifdef USE_LIBTHAI
    obj->options = LINEBREAK_OPTION_COMPLEX_BREAKING;
#endif				/* USE_LIBTHAI */
    obj->ref_func = ref_func;
    obj->refcount = 1UL;
    return obj;
}

/** Increase Reference Count
 *
 * Increse reference count of linebreak object.
 * @param[in] obj linebreak object, must not be NULL.
 * @return linebreak object itself.
 * If error occurred, errno is set then NULL is returned.
 */
linebreak_t *linebreak_incref(linebreak_t * obj)
{
    obj->refcount += 1UL;
    return obj;
}

/** Copy Constructor
 *
 * Create deep copy of linebreak object.
 * Reference count of new object will be set to 1.
 * If ref_func member of object is not NULL, it will be executed to increase
 * reference count of prep_data, format_data, sizing_data, urgent_data and
 * stash members.
 * @param[in] obj linebreak object, must not be NULL.
 * @return New linebreak object.
 * If error occurred, errno is set then NULL is returned.
 */
linebreak_t *linebreak_copy(linebreak_t * obj)
{
    linebreak_t *newobj;
    mapent_t *newmap;
    unichar_t *newstr;

    if (obj == NULL)
	return (errno = EINVAL), NULL;
    if ((newobj = malloc(sizeof(linebreak_t))) == NULL)
	return NULL;
    memcpy(newobj, obj, sizeof(linebreak_t));

    if (obj->map != NULL && obj->mapsiz) {
	if ((newmap = malloc(sizeof(mapent_t) * obj->mapsiz)) == NULL) {
	    free(newobj);
	    return NULL;
	}
	memcpy(newmap, obj->map, sizeof(mapent_t) * obj->mapsiz);
	newobj->map = newmap;
    } else
	newobj->map = NULL;

    if (obj->newline.str != NULL && obj->newline.len) {
	if ((newstr =
	     malloc(sizeof(unichar_t) * obj->newline.len)) == NULL) {
	    free(newobj->map);
	    free(newobj);
	    return NULL;
	}
	memcpy(newstr, obj->newline.str,
	       sizeof(unichar_t) * obj->newline.len);
	newobj->newline.str = newstr;
    } else
	newobj->newline.str = NULL;

    if (obj->bufstr.str != NULL && obj->bufstr.len) {
	if ((newstr = malloc(sizeof(unichar_t) * obj->bufstr.len)) == NULL) {
	    free(newobj->map);
	    free(newobj->newline.str);
	    free(newobj);
	    return NULL;
	}
	memcpy(newstr, obj->bufstr.str,
	       sizeof(unichar_t) * obj->bufstr.len);
	newobj->bufstr.str = newstr;
    } else
	newobj->bufstr.str = NULL;

    if (obj->bufspc.str != NULL && obj->bufspc.len) {
	if ((newstr = malloc(sizeof(unichar_t) * obj->bufspc.len)) == NULL) {
	    free(newobj->map);
	    free(newobj->newline.str);
	    free(newobj->bufstr.str);
	    free(newobj);
	    return NULL;
	}
	memcpy(newstr, obj->bufspc.str,
	       sizeof(unichar_t) * obj->bufspc.len);
	newobj->bufspc.str = newstr;
    } else
	newobj->bufspc.str = NULL;

    if (obj->unread.str != NULL && obj->unread.len) {
	if ((newstr = malloc(sizeof(unichar_t) * obj->unread.len)) == NULL) {
	    free(newobj->map);
	    free(newobj->newline.str);
	    free(newobj->bufstr.str);
	    free(newobj->bufspc.str);
	    free(newobj);
	    return NULL;
	}
	memcpy(newstr, obj->unread.str,
	       sizeof(unichar_t) * obj->unread.len);
	newobj->unread.str = newstr;
    } else
	newobj->unread.str = NULL;

    if (obj->prep_func != NULL) {
	size_t i;
	for (i = 0; obj->prep_func[i] != NULL; i++);
	if ((newobj->prep_func =
	     malloc(sizeof(linebreak_prep_func_t) * (i + 1)))
	    == NULL) {
	    free(newobj->map);
	    free(newobj->newline.str);
	    free(newobj->bufstr.str);
	    free(newobj->bufspc.str);
	    free(newobj->unread.str);
	    free(newobj);
	    return NULL;
	}
	memcpy(newobj->prep_func, obj->prep_func,
	       sizeof(linebreak_prep_func_t) * (i + 1));
	if ((newobj->prep_data = malloc(sizeof(void *) * (i + 1))) == NULL) {
	    free(newobj->map);
	    free(newobj->newline.str);
	    free(newobj->bufstr.str);
	    free(newobj->bufspc.str);
	    free(newobj->unread.str);
	    free(newobj->prep_func);
	    free(newobj);
	    return NULL;
	}
	if (obj->prep_data == NULL)
	    memset(newobj->prep_data, 0, sizeof(void *) * (i + 1));
	else
	    memcpy(newobj->prep_data, obj->prep_data,
		   sizeof(void *) * (i + 1));
    }

    if (newobj->ref_func != NULL) {
	if (newobj->stash != NULL)
	    (*newobj->ref_func) (newobj->stash, LINEBREAK_REF_STASH, +1);
	if (newobj->format_data != NULL)
	    (*newobj->ref_func) (newobj->format_data, LINEBREAK_REF_FORMAT,
				 +1);
	if (newobj->prep_data != NULL) {
	    size_t i;
	    for (i = 0; newobj->prep_func[i] != NULL; i++)
		if (newobj->prep_data[i] != NULL)
		    (*newobj->ref_func) (newobj->prep_data[i],
					 LINEBREAK_REF_PREP, +1);
	}
	if (newobj->sizing_data != NULL)
	    (*newobj->ref_func) (newobj->sizing_data, LINEBREAK_REF_SIZING,
				 +1);
	if (newobj->urgent_data != NULL)
	    (*newobj->ref_func) (newobj->urgent_data, LINEBREAK_REF_URGENT,
				 +1);
	if (newobj->user_data != NULL)
	    (*newobj->ref_func) (newobj->user_data, LINEBREAK_REF_USER,
				 +1);
    }

    newobj->refcount = 1UL;
    return newobj;
}

/** Decrease Reference Count; Destructor
 *
 * Decrement reference count of linebreak object.
 * When reference count becomes zero, free memories allocated for
 * object and then, if ref_func member of object was not NULL,
 * it will be executed to decrease reference count of prep_data, format_data,
 * sizing_data, urgent_data and stash members.
 * @param[in] obj linebreak object.
 * @return none.
 * If obj was NULL, do nothing.
 */
void linebreak_destroy(linebreak_t * obj)
{
    if (obj == NULL)
	return;
    if ((obj->refcount -= 1UL))
	return;
    free(obj->map);
    free(obj->newline.str);
    free(obj->bufstr.str);
    free(obj->bufspc.str);
    free(obj->unread.str);
    if (obj->ref_func != NULL) {
	if (obj->stash != NULL)
	    (*obj->ref_func) (obj->stash, LINEBREAK_REF_STASH, -1);
	if (obj->format_data != NULL)
	    (*obj->ref_func) (obj->format_data, LINEBREAK_REF_FORMAT, -1);
	if (obj->prep_func != NULL) {
	    size_t i;
	    for (i = 0; obj->prep_func[i] != NULL; i++)
		if (obj->prep_data[i] != NULL)
		    (*obj->ref_func) (obj->prep_data[i],
				      LINEBREAK_REF_PREP, -1);
	}
	if (obj->sizing_data != NULL)
	    (*obj->ref_func) (obj->sizing_data, LINEBREAK_REF_SIZING, -1);
	if (obj->urgent_data != NULL)
	    (*obj->ref_func) (obj->urgent_data, LINEBREAK_REF_URGENT, -1);
	if (obj->user_data != NULL)
	    (*obj->ref_func) (obj->user_data, LINEBREAK_REF_USER, -1);
    }
    free(obj->prep_func);
    free(obj->prep_data);
    free(obj);
}

/** Setter: Update newline member
 *
 * @param[in] lbobj target linebreak object, must not be NULL.
 * @param[in] newline pointer to Unicode string.
 * @return none.
 * Copy of newline is set.
 * If error occurred, lbobj->errnum is set.
 */
void linebreak_set_newline(linebreak_t * lbobj, unistr_t * newline)
{
    unichar_t *str;
    size_t len;

    if (newline != NULL && newline->str != NULL && newline->len != 0) {
	if ((str = malloc(sizeof(unichar_t) * newline->len)) == NULL) {
	    lbobj->errnum = errno ? errno : ENOMEM;
	    return;
	}
	memcpy(str, newline->str, sizeof(unichar_t) * newline->len);
	len = newline->len;
    } else {
	str = NULL;
	len = 0;
    }
    free(lbobj->newline.str);
    lbobj->newline.str = str;
    lbobj->newline.len = len;
}

/** Setter: Update stash Member
 *
 * @param[in] lbobj target linebreak object, must not be NULL.
 * @param[in] stash new stash value or NULL.
 * @return none.
 * New stash value is set.
 * Reference count of stash member will be handled appropriately.
 */
void linebreak_set_stash(linebreak_t * lbobj, void *stash)
{
    if (lbobj->ref_func != NULL) {
	if (stash != NULL)
	    (*(lbobj->ref_func)) (stash, LINEBREAK_REF_STASH, +1);
	if (lbobj->stash != NULL)
	    (*(lbobj->ref_func)) (lbobj->stash, LINEBREAK_REF_STASH, -1);
    }
    lbobj->stash = stash;
}

/** Setter: Update format_func/format_data Member
 *
 * @param[in] lbobj target linebreak object.
 * @param[in] format_func format callback function or NULL.
 * @param[in] format_data new format_data value.
 * @return none.
 * New format callback is set.
 * Reference count of format_data member will be handled appropriately.
 */
void linebreak_set_format(linebreak_t * lbobj,
			  linebreak_format_func_t format_func,
			  void *format_data)
{
    if (lbobj->ref_func != NULL) {
	if (format_data != NULL)
	    (*(lbobj->ref_func)) (format_data, LINEBREAK_REF_FORMAT, +1);
	if (lbobj->format_data != NULL)
	    (*(lbobj->ref_func)) (lbobj->format_data, LINEBREAK_REF_FORMAT,
				  -1);
    }
    lbobj->format_func = format_func;
    lbobj->format_data = format_data;
}

/** Setter: Add/clear prep_func/prep_data Member
 *
 * @param[in] lbobj target linebreak object.
 * @param[in] prep_func preprocessing callback function or NULL.
 * @param[in] prep_data new prep_data value.
 * @return none.
 * New preprocessing callback is added.
 * Reference count of prep_data item will be handled appropriately.
 * if prep_func was NULL, all data are cleared.
 */
void linebreak_add_prep(linebreak_t * lbobj,
			linebreak_prep_func_t prep_func, void *prep_data)
{
    size_t i;
    linebreak_prep_func_t *p;
    void **q;

    if (prep_func == NULL) {
	if (lbobj->prep_data != NULL) {
	    for (i = 0; lbobj->prep_func[i] != NULL; i++)
		if (lbobj->prep_data[i] != NULL)
		    (*lbobj->ref_func) (lbobj->prep_data[i],
					LINEBREAK_REF_PREP, -1);
	    free(lbobj->prep_data);
	    lbobj->prep_data = NULL;
	}
	free(lbobj->prep_func);
	lbobj->prep_func = NULL;
	return;
    }

    if (lbobj->prep_func == NULL)
	i = 0;
    else
	for (i = 0; lbobj->prep_func[i] != NULL; i++);

    if ((p =
	 realloc(lbobj->prep_func,
		 sizeof(linebreak_prep_func_t) * (i + 2)))
	== NULL) {
	lbobj->errnum = errno;
	return;
    }
    p[i] = NULL;
    lbobj->prep_func = p;

    if ((q = realloc(lbobj->prep_data, sizeof(void *) * (i + 2))) == NULL) {
	lbobj->errnum = errno;
	return;
    }
    lbobj->prep_data = q;

    if (lbobj->ref_func != NULL && prep_data != NULL)
	(*(lbobj->ref_func)) (prep_data, LINEBREAK_REF_PREP, +1);
    p[i] = prep_func;
    p[i + 1] = NULL;
    q[i] = prep_data;
    q[i + 1] = NULL;
}

/** Setter: Update sizing_func/sizing_data Member
 *
 * @param[in] lbobj target linebreak object.
 * @param[in] sizing_func sizing callback function or NULL.
 * @param[in] sizing_data new sizing_data value.
 * @return none.
 * New sizing callback is set.
 * Reference count of sizing_data member will be handled appropriately.
 */
void linebreak_set_sizing(linebreak_t * lbobj,
			  linebreak_sizing_func_t sizing_func,
			  void *sizing_data)
{
    if (lbobj->ref_func != NULL) {
	if (sizing_data != NULL)
	    (*(lbobj->ref_func)) (sizing_data, LINEBREAK_REF_SIZING, +1);
	if (lbobj->sizing_data != NULL)
	    (*(lbobj->ref_func)) (lbobj->sizing_data, LINEBREAK_REF_SIZING,
				  -1);
    }
    lbobj->sizing_func = sizing_func;
    lbobj->sizing_data = sizing_data;
}

/** Setter: Update urgent_func/urgent_data Member
 *
 * @param[in] lbobj target linebreak object.
 * @param[in] urgent_func urgent breaking callback function or NULL.
 * @param[in] urgent_data new urgent_data value.
 * @return none.
 * New urgent breaking callback is set.
 * Reference count of urgent_data member will be handled appropriately.
 */
void linebreak_set_urgent(linebreak_t * lbobj,
			  linebreak_urgent_func_t urgent_func,
			  void *urgent_data)
{
    if (lbobj->ref_func != NULL) {
	if (urgent_data != NULL)
	    (*(lbobj->ref_func)) (urgent_data, LINEBREAK_REF_URGENT, +1);
	if (lbobj->urgent_data != NULL)
	    (*(lbobj->ref_func)) (lbobj->urgent_data, LINEBREAK_REF_URGENT,
				  -1);
    }
    lbobj->urgent_func = urgent_func;
    lbobj->urgent_data = urgent_data;
}

/** Setter: Update user_func/user_data Member
 * @deprecated Use linebreak_add_prep() instead.
 *
 * @param[in] lbobj target linebreak object.
 * @param[in] user_func preprocessing callback function or NULL.
 * @param[in] user_data new user_data value.
 * @return none.
 * New preprocessing callback is set.
 * Reference count of user_data member will be handled appropriately.
 */
void linebreak_set_user(linebreak_t * lbobj,
			linebreak_obs_prep_func_t user_func,
			void *user_data)
{
    if (lbobj->ref_func != NULL) {
	if (user_data != NULL)
	    (*(lbobj->ref_func)) (user_data, LINEBREAK_REF_USER, +1);
	if (lbobj->user_data != NULL)
	    (*(lbobj->ref_func)) (lbobj->user_data, LINEBREAK_REF_USER,
				  -1);
    }
    lbobj->user_func = user_func;
    lbobj->user_data = user_data;
}

/** Reset State
 *
 * Reset internal state of linebreak object.
 * Internal state is set by linebreak_break_partial() function.
 * @param[in] lbobj linebreak object.
 * @return none.
 * If lbobj was NULL, do nothing.
 */
void linebreak_reset(linebreak_t * lbobj)
{
    if (lbobj == NULL)
	return;
    free(lbobj->unread.str);
    lbobj->unread.str = NULL;
    lbobj->unread.len = 0;
    free(lbobj->bufstr.str);
    lbobj->bufstr.str = NULL;
    lbobj->bufstr.len = 0;
    free(lbobj->bufspc.str);
    lbobj->bufspc.str = NULL;
    lbobj->bufspc.len = 0;
    lbobj->bufcols = 0.0;
    lbobj->state = LINEBREAK_STATE_NONE;
    lbobj->errnum = 0;
}

/** Get breaking rule between two classes
 *
 * From given two line breaking classes, get breaking rule determined by
 * internal data.
 * @param[in] obj linebreak object, must not be NULL.
 * @param[in] albc line breaking class.
 * @param[in] blbc line breaking class.
 * @return line breaking action: MANDATORY, DIRECT, INDIRECT or PROHIBITED.
 * If action was not determined, returns DIRECT.
 *
 * @note This method gives just approximate description of line breaking
 * behavior.  Class AI and CJ will be resolved to approppriate classes.
 * See also linebreak_lbrule().
 *
 * @note This method was introduced by Sombok 2.0.6. 
 * @note LEGACY_CM and HANGUL_AS_AL options are concerned as of Sombok 2.1.2.
 * @note Only HANGUL_AS_AL is concerned as of Sombok 2.2.
 *
 */
propval_t linebreak_get_lbrule(linebreak_t * obj, propval_t blbc,
			       propval_t albc)
{
    switch (blbc) {
    case LB_AI:
	blbc = (obj->options & LINEBREAK_OPTION_EASTASIAN_CONTEXT) ?
	    LB_ID : LB_AL;
	break;
    case LB_CJ:
	blbc = (obj->options & LINEBREAK_OPTION_NONSTARTER_LOOSE) ?
	    LB_ID : LB_NS;
	break;
    /* Optionally, treat hangul syllable as if it were AL. */
    case LB_H2:
    case LB_H3:
    case LB_JL:
    case LB_JV:
    case LB_JT:
	if ((albc == LB_H2 || albc == LB_H3 || albc == LB_JL ||
	     albc == LB_JV || albc == LB_JT) &&
	    obj->options & LINEBREAK_OPTION_HANGUL_AS_AL)
	    return LINEBREAK_ACTION_INDIRECT;
	break;
    }

    switch (albc) {
    case LB_AI:
	albc = (obj->options & LINEBREAK_OPTION_EASTASIAN_CONTEXT) ?
	    LB_ID : LB_AL;
	break;
    case LB_CJ:
	albc = (obj->options & LINEBREAK_OPTION_NONSTARTER_LOOSE) ?
	    LB_ID : LB_NS;
	break;
    }

    return linebreak_lbrule(blbc, albc);
}

/** Get Line Breaking Class
 * @deprecated Use gcstring_lbclass() or gcstring_lbclass_ext() instead.
 *
 * Get UAX #14 line breaking class of Unicode character.
 * Classes XX and SG will be resolved to AL.
 * @param[in] obj linebreak object, must not be NULL.
 * @param[in] c Unicode character.
 * @return line breaking class property value.
 */
propval_t linebreak_lbclass(linebreak_t * obj, unichar_t c)
{
    propval_t lbc, gcb, scr;

    linebreak_charprop(obj, c, &lbc, NULL, &gcb, &scr);
    if (lbc == LB_AI)
	lbc = (obj->options & LINEBREAK_OPTION_EASTASIAN_CONTEXT) ?
	    LB_ID : LB_AL;
    else if (lbc == LB_CJ)
	lbc = (obj->options & LINEBREAK_OPTION_NONSTARTER_LOOSE) ?
	    LB_ID : LB_NS;
    else if (lbc == LB_SA) {
#ifdef USE_LIBTHAI
	if (scr != SC_Thai)
#endif				/* USE_LIBTHAI */
	    lbc = (gcb == GB_Extend || gcb == GB_SpacingMark
		   || gcb == GB_Virama) ? LB_CM : LB_AL;
    }
    return lbc;
}

/** Get East_Asian_Width Property
 * @deprecated Use gcstring_columns() instead.
 *
 * Get UAX #11 East_Asian_Width property value of Unicode character.
 * Class A will be resolved to appropriate property F or N.
 * @param[in] obj linebreak object, must not be NULL.
 * @param[in] c Unicode character.
 * @return East_Asian_Width property value.
 */
propval_t linebreak_eawidth(linebreak_t * obj, unichar_t c)
{
    propval_t eaw;

    linebreak_charprop(obj, c, NULL, &eaw, NULL, NULL);
    if (eaw == EA_A)
	eaw = (obj->options & LINEBREAK_OPTION_EASTASIAN_CONTEXT) ?
	    EA_F : EA_N;

    return eaw;
}