Updates to `NEWS' and documentation.
[ttfautohint.git] / lib / tacontrol.c
blob61dc4dc6589d50f5b17546a4be0d767e14903cdd
1 /* tacontrol.c */
3 /*
4 * Copyright (C) 2014 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
15 #include "ta.h"
17 #include <locale.h>
18 #include <limits.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include <math.h>
22 #include <stdbool.h> /* for llrb.h */
24 #include "llrb.h" /* a red-black tree implementation */
25 #include "tacontrol-bison.h"
27 Control*
28 TA_control_new(Control_Type type,
29 long font_idx,
30 long glyph_idx,
31 number_range* point_set,
32 double x_shift,
33 double y_shift,
34 number_range* ppem_set)
36 Control* control;
39 control = (Control*)malloc(sizeof (Control));
40 if (!control)
41 return NULL;
43 control->type = type;
45 control->font_idx = font_idx;
46 control->glyph_idx = glyph_idx;
47 control->points = number_set_reverse(point_set);
49 switch (control->type)
51 case Control_Delta_before_IUP:
52 case Control_Delta_after_IUP:
53 /* we round shift values to multiples of 1/(2^CONTROL_DELTA_SHIFT) */
54 control->x_shift = (int)(x_shift * CONTROL_DELTA_FACTOR
55 + (x_shift > 0 ? 0.5 : -0.5));
56 control->y_shift = (int)(y_shift * CONTROL_DELTA_FACTOR
57 + (y_shift > 0 ? 0.5 : -0.5));
58 break;
60 case Control_Segment_Left:
61 case Control_Segment_Right:
62 /* offsets */
63 control->x_shift = x_shift;
64 control->y_shift = y_shift;
65 break;
67 case Control_Segment_None:
68 control->x_shift = 0;
69 control->y_shift = 0;
70 break;
73 control->ppems = number_set_reverse(ppem_set);
74 control->next = NULL;
76 return control;
80 Control*
81 TA_control_prepend(Control* list,
82 Control* element)
84 if (!element)
85 return list;
87 element->next = list;
89 return element;
93 Control*
94 TA_control_reverse(Control* list)
96 Control* cur;
99 cur = list;
100 list = NULL;
102 while (cur)
104 Control* tmp;
107 tmp = cur;
108 cur = cur->next;
109 tmp->next = list;
110 list = tmp;
113 return list;
117 void
118 TA_control_free(Control* control)
120 while (control)
122 Control* tmp;
125 number_set_free(control->points);
126 number_set_free(control->ppems);
128 tmp = control;
129 control = control->next;
130 free(tmp);
136 control_show_line(FONT* font,
137 Control* control)
139 char glyph_name_buf[64];
140 char* points_buf = NULL;
141 char* ppems_buf = NULL;
143 sds s;
145 FT_Face face;
148 s = sdsempty();
150 if (!control)
151 goto Exit;
153 if (control->font_idx >= font->num_sfnts)
154 goto Exit;
156 face = font->sfnts[control->font_idx].face;
157 glyph_name_buf[0] = '\0';
158 if (FT_HAS_GLYPH_NAMES(face))
159 FT_Get_Glyph_Name(face, control->glyph_idx, glyph_name_buf, 64);
161 points_buf = number_set_show(control->points, -1, -1);
162 if (!points_buf)
163 goto Exit;
164 ppems_buf = number_set_show(control->ppems, -1, -1);
165 if (!ppems_buf)
166 goto Exit;
168 switch (control->type)
170 case Control_Delta_before_IUP:
171 /* not implemented yet */
172 break;
174 case Control_Delta_after_IUP:
175 /* display glyph index if we don't have a glyph name */
176 if (*glyph_name_buf)
177 s = sdscatprintf(s, "%ld %s p %s x %.20g y %.20g @ %s",
178 control->font_idx,
179 glyph_name_buf,
180 points_buf,
181 (double)control->x_shift / CONTROL_DELTA_FACTOR,
182 (double)control->y_shift / CONTROL_DELTA_FACTOR,
183 ppems_buf);
184 else
185 s = sdscatprintf(s, "%ld %ld p %s x %.20g y %.20g @ %s",
186 control->font_idx,
187 control->glyph_idx,
188 points_buf,
189 (double)control->x_shift / CONTROL_DELTA_FACTOR,
190 (double)control->y_shift / CONTROL_DELTA_FACTOR,
191 ppems_buf);
192 break;
194 case Control_Segment_Left:
195 case Control_Segment_Right:
196 /* display glyph index if we don't have a glyph name */
197 if (*glyph_name_buf)
198 s = sdscatprintf(s, "%ld %s %c %s",
199 control->font_idx,
200 glyph_name_buf,
201 control->type == Control_Segment_Left ? 'l' : 'r',
202 points_buf);
203 else
204 s = sdscatprintf(s, "%ld %ld %c %s",
205 control->font_idx,
206 control->glyph_idx,
207 control->type == Control_Segment_Left ? 'l' : 'r',
208 points_buf);
210 if (control->x_shift || control->y_shift)
211 s = sdscatprintf(s, " (%d,%d)", control->x_shift, control->y_shift);
212 break;
214 case Control_Segment_None:
215 /* display glyph index if we don't have a glyph name */
216 if (*glyph_name_buf)
217 s = sdscatprintf(s, "%ld %s n %s",
218 control->font_idx,
219 glyph_name_buf,
220 points_buf);
221 else
222 s = sdscatprintf(s, "%ld %ld n %s",
223 control->font_idx,
224 control->glyph_idx,
225 points_buf);
226 break;
229 Exit:
230 free(points_buf);
231 free(ppems_buf);
233 return s;
237 char*
238 TA_control_show(FONT* font)
240 sds s;
241 size_t len;
242 char* res;
244 Control* control = font->control;
247 s = sdsempty();
249 while (control)
251 sds d;
254 /* append current line to buffer, followed by a newline character */
255 d = control_show_line(font, control);
256 if (!d)
258 sdsfree(s);
259 return NULL;
261 s = sdscatsds(s, d);
262 sdsfree(d);
263 s = sdscat(s, "\n");
265 control = control->next;
268 if (!s)
269 return NULL;
271 /* we return an empty string if there is no data */
272 len = sdslen(s) + 1;
273 res = (char*)malloc(len);
274 if (res)
275 memcpy(res, s, len);
277 sdsfree(s);
279 return res;
283 /* Parse control instructions in `font->control_buf'. */
285 TA_Error
286 TA_control_parse_buffer(FONT* font,
287 char** error_string_p,
288 unsigned int* errlinenum_p,
289 char** errline_p,
290 char** errpos_p)
292 int bison_error;
294 Control_Context context;
297 /* nothing to do if no data */
298 if (!font->control_buf)
300 font->control = NULL;
301 return TA_Err_Ok;
304 TA_control_scanner_init(&context, font);
305 if (context.error)
306 goto Fail;
307 /* this is `yyparse' in disguise */
308 bison_error = TA_control_parse(&context);
309 TA_control_scanner_done(&context);
311 if (bison_error)
313 if (bison_error == 2)
314 context.error = TA_Err_Control_Allocation_Error;
316 Fail:
317 font->control = NULL;
319 if (context.error == TA_Err_Control_Allocation_Error
320 || context.error == TA_Err_Control_Flex_Error)
322 *errlinenum_p = 0;
323 *errline_p = NULL;
324 *errpos_p = NULL;
325 if (context.errmsg)
326 *error_string_p = strdup(context.errmsg);
327 else
328 *error_string_p = strdup(TA_get_error_message(context.error));
330 else
332 int i, ret;
333 char auxbuf[128];
335 char* buf_end;
336 char* p_start;
337 char* p_end;
340 /* construct data for `errline_p' */
341 buf_end = font->control_buf + font->control_len;
343 p_start = font->control_buf;
344 if (context.errline_num > 1)
346 i = 1;
347 while (p_start < buf_end)
349 if (*p_start++ == '\n')
351 i++;
352 if (i == context.errline_num)
353 break;
358 p_end = p_start;
359 while (p_end < buf_end)
361 if (*p_end == '\n')
362 break;
363 p_end++;
365 *errline_p = strndup(p_start, p_end - p_start);
367 /* construct data for `error_string_p' */
368 if (context.error == TA_Err_Control_Invalid_Font_Index)
369 sprintf(auxbuf, " (valid range is [%ld;%ld])",
371 font->num_sfnts);
372 else if (context.error == TA_Err_Control_Invalid_Glyph_Index)
373 sprintf(auxbuf, " (valid range is [%ld;%ld])",
375 font->sfnts[context.font_idx].face->num_glyphs);
376 else if (context.error == TA_Err_Control_Invalid_Shift)
377 sprintf(auxbuf, " (valid interval is [%g;%g])",
378 CONTROL_DELTA_SHIFT_MIN,
379 CONTROL_DELTA_SHIFT_MAX);
380 else if (context.error == TA_Err_Control_Invalid_Offset)
381 sprintf(auxbuf, " (valid interval is [%d;%d])",
382 SHRT_MIN,
383 SHRT_MAX);
384 else if (context.error == TA_Err_Control_Invalid_Range)
385 sprintf(auxbuf, " (values must be within [%ld;%ld])",
386 context.number_set_min,
387 context.number_set_max);
388 else
389 auxbuf[0] = '\0';
391 ret = asprintf(error_string_p, "%s%s",
392 *context.errmsg ? context.errmsg
393 : TA_get_error_message(context.error),
394 auxbuf);
395 if (ret == -1)
396 *error_string_p = NULL;
398 if (errline_p)
399 *errpos_p = *errline_p + context.errline_pos_left - 1;
400 else
401 *errpos_p = NULL;
403 *errlinenum_p = context.errline_num;
406 else
407 font->control = context.result;
409 return context.error;
413 /* node structure for control instruction data */
415 typedef struct Node Node;
416 struct Node
418 LLRB_ENTRY(Node) entry;
419 Ctrl ctrl;
423 /* comparison function for our red-black tree */
425 static int
426 nodecmp(Node* e1,
427 Node* e2)
429 long diff;
432 /* sort by font index ... */
433 diff = e1->ctrl.font_idx - e2->ctrl.font_idx;
434 if (diff)
435 goto Exit;
437 /* ... then by glyph index ... */
438 diff = e1->ctrl.glyph_idx - e2->ctrl.glyph_idx;
439 if (diff)
440 goto Exit;
442 /* ... then by ppem ... */
443 diff = e1->ctrl.ppem - e2->ctrl.ppem;
444 if (diff)
445 goto Exit;
447 /* ... then by point index */
448 diff = e1->ctrl.point_idx - e2->ctrl.point_idx;
450 Exit:
451 /* https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign */
452 return (diff > 0) - (diff < 0);
456 /* the red-black tree function body */
457 typedef struct control_data control_data;
459 LLRB_HEAD(control_data, Node);
461 /* no trailing semicolon in the next line */
462 LLRB_GENERATE_STATIC(control_data, Node, entry, nodecmp)
465 void
466 TA_control_free_tree(FONT* font)
468 control_data* control_data_head = (control_data*)font->control_data_head;
469 Control* control_segment_dirs_head = (Control*)font->control_segment_dirs_head;
471 Node* node;
472 Node* next_node;
475 if (!control_data_head)
476 return;
478 for (node = LLRB_MIN(control_data, control_data_head);
479 node;
480 node = next_node)
482 next_node = LLRB_NEXT(control_data, control_data_head, node);
483 LLRB_REMOVE(control_data, control_data_head, node);
484 free(node);
487 free(control_data_head);
488 TA_control_free(control_segment_dirs_head);
492 TA_Error
493 TA_control_build_tree(FONT* font)
495 Control* control = font->control;
496 control_data* control_data_head;
497 int emit_newline = 0;
500 font->control_segment_dirs_head = NULL;
501 font->control_segment_dirs_cur = NULL;
503 /* nothing to do if no data */
504 if (!control)
506 font->control_data_head = NULL;
507 font->control_data_cur = NULL;
508 return TA_Err_Ok;
511 control_data_head = (control_data*)malloc(sizeof (control_data));
512 if (!control_data_head)
513 return FT_Err_Out_Of_Memory;
515 LLRB_INIT(control_data_head);
517 while (control)
519 Control_Type type = control->type;
520 long font_idx = control->font_idx;
521 long glyph_idx = control->glyph_idx;
522 int x_shift = control->x_shift;
523 int y_shift = control->y_shift;
525 number_set_iter ppems_iter;
526 int ppem;
529 ppems_iter.range = control->ppems;
530 ppem = number_set_get_first(&ppems_iter);
532 /* ppem is always zero for one-point segments */
533 if (type == Control_Segment_Left
534 || type == Control_Segment_Right
535 || type == Control_Segment_None)
536 goto Points_Loop;
538 while (ppems_iter.range)
540 number_set_iter points_iter;
541 int point_idx;
544 Points_Loop:
545 points_iter.range = control->points;
546 point_idx = number_set_get_first(&points_iter);
548 while (points_iter.range)
550 Node* node;
551 Node* val;
554 node = (Node*)malloc(sizeof (Node));
555 if (!node)
556 return FT_Err_Out_Of_Memory;
558 node->ctrl.type = type;
559 node->ctrl.font_idx = font_idx;
560 node->ctrl.glyph_idx = glyph_idx;
561 node->ctrl.ppem = ppem;
562 node->ctrl.point_idx = point_idx;
563 node->ctrl.x_shift = x_shift;
564 node->ctrl.y_shift = y_shift;
566 val = LLRB_INSERT(control_data, control_data_head, node);
567 if (val)
568 free(node);
569 if (val && font->debug)
571 /* entry is already present; we ignore it */
572 Control d;
573 number_range ppems;
574 number_range points;
576 sds s;
579 /* construct Control entry for debugging output */
580 ppems.start = ppem;
581 ppems.end = ppem;
582 ppems.next = NULL;
583 points.start = point_idx;
584 points.end = point_idx;
585 points.next = NULL;
587 d.type = type;
588 d.font_idx = font_idx;
589 d.glyph_idx = glyph_idx;
590 d.points = &points;
591 d.x_shift = x_shift;
592 d.y_shift = y_shift;
593 d.ppems = &ppems;
594 d.next = NULL;
596 s = control_show_line(font, &d);
597 if (s)
599 fprintf(stderr, "Control instruction %s ignored.\n", s);
600 sdsfree(s);
603 emit_newline = 1;
606 point_idx = number_set_get_next(&points_iter);
609 ppem = number_set_get_next(&ppems_iter);
612 control = control->next;
615 if (font->debug && emit_newline)
616 fprintf(stderr, "\n");
618 font->control_data_head = control_data_head;
619 font->control_data_cur = LLRB_MIN(control_data, control_data_head);
621 return TA_Err_Ok;
625 /* the next functions are intended to restrict the use of LLRB stuff */
626 /* related to control instructions to this file, */
627 /* providing a means to access the data sequentially */
629 void
630 TA_control_get_next(FONT* font)
632 Node* node = (Node*)font->control_data_cur;
635 if (!node)
636 return;
638 node = LLRB_NEXT(control_data, /* unused */, node);
640 font->control_data_cur = node;
644 const Ctrl*
645 TA_control_get_ctrl(FONT* font)
647 Node* node = (Node*)font->control_data_cur;
650 return node ? &node->ctrl : NULL;
654 TA_Error
655 TA_control_segment_dir_collect(FONT* font,
656 long font_idx,
657 long glyph_idx)
659 Control* control_segment_dirs_head = (Control*)font->control_segment_dirs_head;
662 /* nothing to do if no data */
663 if (!font->control_data_head)
664 return TA_Err_Ok;
666 if (control_segment_dirs_head)
668 TA_control_free(control_segment_dirs_head);
669 control_segment_dirs_head = NULL;
673 * The PPEM value for one-point segments is always zero; such control
674 * instructions are thus sorted before other control instructions for the
675 * same glyph index -- this fits nicely with the call to
676 * `TA_control_get_next' in the loop of `TA_sfnt_build_delta_exceptions',
677 * assuring proper sequential access to the red-black tree.
679 for (;;)
681 const Ctrl* ctrl = TA_control_get_ctrl(font);
682 Control* elem;
685 if (!ctrl)
686 break;
688 /* check type */
689 if (!(ctrl->type == Control_Segment_Left
690 || ctrl->type == Control_Segment_Right
691 || ctrl->type == Control_Segment_None))
692 break;
694 /* too large values of font and glyph indices in `ctrl' */
695 /* are handled by later calls of this function */
696 if (font_idx < ctrl->font_idx
697 || glyph_idx < ctrl->glyph_idx)
698 break;
700 /* we simply use the `Control' structure again, */
701 /* abusing the `glyph_idx' field for the point index */
702 elem = TA_control_new(ctrl->type,
704 ctrl->point_idx,
705 NULL,
706 ctrl->x_shift,
707 ctrl->y_shift,
708 NULL);
709 if (!elem)
711 TA_control_free(control_segment_dirs_head);
712 return TA_Err_Control_Allocation_Error;
714 control_segment_dirs_head = TA_control_prepend(control_segment_dirs_head,
715 elem);
717 TA_control_get_next(font);
720 font->control_segment_dirs_head = TA_control_reverse(control_segment_dirs_head);
721 font->control_segment_dirs_cur = font->control_segment_dirs_head;
723 return TA_Err_Ok;
728 TA_control_segment_dir_get_next(FONT* font,
729 int* point_idx,
730 TA_Direction* dir,
731 int* left_offset,
732 int* right_offset)
734 Control* control_segment_dirs_head = (Control*)font->control_segment_dirs_head;
735 Control* control_segment_dirs_cur = (Control*)font->control_segment_dirs_cur;
738 /* nothing to do if no data */
739 if (!control_segment_dirs_head)
740 return 0;
742 if (!control_segment_dirs_cur)
744 font->control_segment_dirs_cur = control_segment_dirs_head;
745 return 0;
748 /* the `glyph_idx' field gets abused for `point_idx' */
749 *point_idx = control_segment_dirs_cur->glyph_idx;
750 *dir = control_segment_dirs_cur->type == Control_Segment_Left
751 ? TA_DIR_LEFT
752 : control_segment_dirs_cur->type == Control_Segment_Right
753 ? TA_DIR_RIGHT
754 : TA_DIR_NONE;
755 *left_offset = control_segment_dirs_cur->x_shift;
756 *right_offset = control_segment_dirs_cur->y_shift;
758 font->control_segment_dirs_cur = control_segment_dirs_cur->next;
760 return control_segment_dirs_cur != NULL;
763 /* end of tacontrol.c */