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.
22 #include <stdbool.h> /* for llrb.h */
24 #include "llrb.h" /* a red-black tree implementation */
25 #include "tacontrol-bison.h"
28 TA_control_new(Control_Type type
,
31 number_range
* point_set
,
34 number_range
* ppem_set
)
39 control
= (Control
*)malloc(sizeof (Control
));
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));
60 case Control_Segment_Left
:
61 case Control_Segment_Right
:
63 control
->x_shift
= x_shift
;
64 control
->y_shift
= y_shift
;
67 case Control_Segment_None
:
73 control
->ppems
= number_set_reverse(ppem_set
);
81 TA_control_prepend(Control
* list
,
94 TA_control_reverse(Control
* list
)
118 TA_control_free(Control
* control
)
125 number_set_free(control
->points
);
126 number_set_free(control
->ppems
);
129 control
= control
->next
;
136 control_show_line(FONT
* font
,
139 char glyph_name_buf
[64];
140 char* points_buf
= NULL
;
141 char* ppems_buf
= NULL
;
153 if (control
->font_idx
>= font
->num_sfnts
)
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);
164 ppems_buf
= number_set_show(control
->ppems
, -1, -1);
168 switch (control
->type
)
170 case Control_Delta_before_IUP
:
171 /* not implemented yet */
174 case Control_Delta_after_IUP
:
175 /* display glyph index if we don't have a glyph name */
177 s
= sdscatprintf(s
, "%ld %s p %s x %.20g y %.20g @ %s",
181 (double)control
->x_shift
/ CONTROL_DELTA_FACTOR
,
182 (double)control
->y_shift
/ CONTROL_DELTA_FACTOR
,
185 s
= sdscatprintf(s
, "%ld %ld p %s x %.20g y %.20g @ %s",
189 (double)control
->x_shift
/ CONTROL_DELTA_FACTOR
,
190 (double)control
->y_shift
/ CONTROL_DELTA_FACTOR
,
194 case Control_Segment_Left
:
195 case Control_Segment_Right
:
196 /* display glyph index if we don't have a glyph name */
198 s
= sdscatprintf(s
, "%ld %s %c %s",
201 control
->type
== Control_Segment_Left
? 'l' : 'r',
204 s
= sdscatprintf(s
, "%ld %ld %c %s",
207 control
->type
== Control_Segment_Left
? 'l' : 'r',
210 if (control
->x_shift
|| control
->y_shift
)
211 s
= sdscatprintf(s
, " (%d,%d)", control
->x_shift
, control
->y_shift
);
214 case Control_Segment_None
:
215 /* display glyph index if we don't have a glyph name */
217 s
= sdscatprintf(s
, "%ld %s n %s",
222 s
= sdscatprintf(s
, "%ld %ld n %s",
238 TA_control_show(FONT
* font
)
244 Control
* control
= font
->control
;
254 /* append current line to buffer, followed by a newline character */
255 d
= control_show_line(font
, control
);
265 control
= control
->next
;
271 /* we return an empty string if there is no data */
273 res
= (char*)malloc(len
);
283 /* Parse control instructions in `font->control_buf'. */
286 TA_control_parse_buffer(FONT
* font
,
287 char** error_string_p
,
288 unsigned int* errlinenum_p
,
294 Control_Context context
;
297 /* nothing to do if no data */
298 if (!font
->control_buf
)
300 font
->control
= NULL
;
304 TA_control_scanner_init(&context
, font
);
307 /* this is `yyparse' in disguise */
308 bison_error
= TA_control_parse(&context
);
309 TA_control_scanner_done(&context
);
313 if (bison_error
== 2)
314 context
.error
= TA_Err_Control_Allocation_Error
;
317 font
->control
= NULL
;
319 if (context
.error
== TA_Err_Control_Allocation_Error
320 || context
.error
== TA_Err_Control_Flex_Error
)
326 *error_string_p
= strdup(context
.errmsg
);
328 *error_string_p
= strdup(TA_get_error_message(context
.error
));
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)
347 while (p_start
< buf_end
)
349 if (*p_start
++ == '\n')
352 if (i
== context
.errline_num
)
359 while (p_end
< buf_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])",
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])",
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
);
391 ret
= asprintf(error_string_p
, "%s%s",
392 *context
.errmsg
? context
.errmsg
393 : TA_get_error_message(context
.error
),
396 *error_string_p
= NULL
;
399 *errpos_p
= *errline_p
+ context
.errline_pos_left
- 1;
403 *errlinenum_p
= context
.errline_num
;
407 font
->control
= context
.result
;
409 return context
.error
;
413 /* node structure for control instruction data */
415 typedef struct Node Node
;
418 LLRB_ENTRY(Node
) entry
;
423 /* comparison function for our red-black tree */
432 /* sort by font index ... */
433 diff
= e1
->ctrl
.font_idx
- e2
->ctrl
.font_idx
;
437 /* ... then by glyph index ... */
438 diff
= e1
->ctrl
.glyph_idx
- e2
->ctrl
.glyph_idx
;
442 /* ... then by ppem ... */
443 diff
= e1
->ctrl
.ppem
- e2
->ctrl
.ppem
;
447 /* ... then by point index */
448 diff
= e1
->ctrl
.point_idx
- e2
->ctrl
.point_idx
;
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
)
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
;
475 if (!control_data_head
)
478 for (node
= LLRB_MIN(control_data
, control_data_head
);
482 next_node
= LLRB_NEXT(control_data
, control_data_head
, node
);
483 LLRB_REMOVE(control_data
, control_data_head
, node
);
487 free(control_data_head
);
488 TA_control_free(control_segment_dirs_head
);
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 */
506 font
->control_data_head
= NULL
;
507 font
->control_data_cur
= NULL
;
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
);
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
;
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
)
538 while (ppems_iter
.range
)
540 number_set_iter points_iter
;
545 points_iter
.range
= control
->points
;
546 point_idx
= number_set_get_first(&points_iter
);
548 while (points_iter
.range
)
554 node
= (Node
*)malloc(sizeof (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
);
569 if (val
&& font
->debug
)
571 /* entry is already present; we ignore it */
579 /* construct Control entry for debugging output */
583 points
.start
= point_idx
;
584 points
.end
= point_idx
;
588 d
.font_idx
= font_idx
;
589 d
.glyph_idx
= glyph_idx
;
596 s
= control_show_line(font
, &d
);
599 fprintf(stderr
, "Control instruction %s ignored.\n", s
);
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
);
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 */
630 TA_control_get_next(FONT
* font
)
632 Node
* node
= (Node
*)font
->control_data_cur
;
638 node
= LLRB_NEXT(control_data
, /* unused */, node
);
640 font
->control_data_cur
= node
;
645 TA_control_get_ctrl(FONT
* font
)
647 Node
* node
= (Node
*)font
->control_data_cur
;
650 return node
? &node
->ctrl
: NULL
;
655 TA_control_segment_dir_collect(FONT
* font
,
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
)
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.
681 const Ctrl
* ctrl
= TA_control_get_ctrl(font
);
689 if (!(ctrl
->type
== Control_Segment_Left
690 || ctrl
->type
== Control_Segment_Right
691 || ctrl
->type
== Control_Segment_None
))
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
)
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
,
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
,
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
;
728 TA_control_segment_dir_get_next(FONT
* font
,
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
)
742 if (!control_segment_dirs_cur
)
744 font
->control_segment_dirs_cur
= control_segment_dirs_head
;
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
752 : control_segment_dirs_cur
->type
== Control_Segment_Right
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 */