1 /*
2  * Copyright 2012 Google, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Google Author(s): Behdad Esfahbod, Maysum Panju
17  */
18 
19 /* Intentionally doesn't have include guards */
20 
21 #include "glyphy.h"
22 
23 #include <ft2build.h>
24 #include FT_FREETYPE_H
25 #include FT_OUTLINE_H
26 
27 
28 
29 #ifndef GLYPHY_FREETYPE_PREFIX
30 #define GLYPHY_FREETYPE_PREFIX glyphy_freetype_
31 #endif
32 
33 #ifndef glyphy_freetype
34 #define glyphy_freetype(name) GLYPHY_PASTE (GLYPHY_FREETYPE_PREFIX, name)
35 #endif
36 
37 
38 
39 static int
glyphy_freetype(move_to)40 glyphy_freetype(move_to) (FT_Vector *to,
41 			  glyphy_arc_accumulator_t *acc)
42 {
43   glyphy_point_t p1 = {to->x, to->y};
44   glyphy_arc_accumulator_close_path (acc);
45   glyphy_arc_accumulator_move_to (acc, &p1);
46   return glyphy_arc_accumulator_successful (acc) ? FT_Err_Ok : FT_Err_Out_Of_Memory;
47 }
48 
49 static int
glyphy_freetype(line_to)50 glyphy_freetype(line_to) (FT_Vector *to,
51 			  glyphy_arc_accumulator_t *acc)
52 {
53   glyphy_point_t p1 = {to->x, to->y};
54   glyphy_arc_accumulator_line_to (acc, &p1);
55   return glyphy_arc_accumulator_successful (acc) ? FT_Err_Ok : FT_Err_Out_Of_Memory;
56 }
57 
58 static int
glyphy_freetype(conic_to)59 glyphy_freetype(conic_to) (FT_Vector *control, FT_Vector *to,
60 			   glyphy_arc_accumulator_t *acc)
61 {
62   glyphy_point_t p1 = {control->x, control->y};
63   glyphy_point_t p2 = {to->x, to->y};
64   glyphy_arc_accumulator_conic_to (acc, &p1, &p2);
65   return glyphy_arc_accumulator_successful (acc) ? FT_Err_Ok : FT_Err_Out_Of_Memory;
66 }
67 
68 static int
glyphy_freetype(cubic_to)69 glyphy_freetype(cubic_to) (FT_Vector *control1, FT_Vector *control2, FT_Vector *to,
70 			   glyphy_arc_accumulator_t *acc)
71 {
72   glyphy_point_t p1 = {control1->x, control1->y};
73   glyphy_point_t p2 = {control2->x, control2->y};
74   glyphy_point_t p3 = {to->x, to->y};
75   glyphy_arc_accumulator_cubic_to (acc, &p1, &p2, &p3);
76   return glyphy_arc_accumulator_successful (acc) ? FT_Err_Ok : FT_Err_Out_Of_Memory;
77 }
78 
79 static FT_Error
glyphy_freetype(outline_decompose)80 glyphy_freetype(outline_decompose) (const FT_Outline         *outline,
81 				    glyphy_arc_accumulator_t *acc)
82 {
83   const FT_Outline_Funcs outline_funcs = {
84     (FT_Outline_MoveToFunc) glyphy_freetype(move_to),
85     (FT_Outline_LineToFunc) glyphy_freetype(line_to),
86     (FT_Outline_ConicToFunc) glyphy_freetype(conic_to),
87     (FT_Outline_CubicToFunc) glyphy_freetype(cubic_to),
88     0, /* shift */
89     0, /* delta */
90   };
91 
92   return FT_Outline_Decompose ((FT_Outline *) outline, &outline_funcs, acc);
93 }
94