Imported Upstream version 8.2.2
[platform/upstream/harfbuzz.git] / test / api / test-ft.c
1 /*
2  * Copyright © 2022 Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Author: Matthias Clasen
25  */
26
27
28 #include "hb-test.h"
29
30 #include "hb-ft.h"
31
32 #include FT_FONT_FORMATS_H
33
34 static FT_Library ft_library;
35
36 static void
37 init_freetype (void)
38 {
39   FT_Error ft_error;
40   if ((ft_error = FT_Init_FreeType (&ft_library)))
41     abort ();
42 }
43
44 static void
45 cleanup_freetype (void)
46 {
47   FT_Done_FreeType (ft_library);
48 }
49
50 static FT_Face
51 get_ft_face (const char *file)
52 {
53   FT_Face ft_face;
54
55 #if GLIB_CHECK_VERSION(2,37,2)
56   char* path = g_test_build_filename (G_TEST_DIST, file, NULL);
57 #else
58   char* path = g_strdup (file);
59 #endif
60
61   FT_Error ft_error;
62   if ((ft_error = FT_New_Face (ft_library, path, 0, &ft_face))) {
63     g_free (path);
64     abort();
65   }
66   g_free (path);
67
68   if ((ft_error = FT_Set_Char_Size (ft_face, 2000, 1000, 0, 0)))
69     abort ();
70
71   return ft_face;
72 }
73
74 static void
75 test_native_ft_basic (void)
76 {
77   FT_Face ft_face;
78   hb_font_t *font;
79   FT_Face ft_face2;
80
81   init_freetype ();
82
83   ft_face = get_ft_face ("fonts/Cantarell.A.otf");
84
85   g_assert_nonnull (ft_face);
86   g_assert_nonnull (FT_Get_Font_Format (ft_face));
87
88   font = hb_ft_font_create_referenced (ft_face);
89
90   ft_face2 = hb_ft_font_get_face (font);
91
92   g_assert_true (ft_face2 == ft_face);
93
94   ft_face2 = hb_ft_font_lock_face (font);
95
96   g_assert_true (ft_face2 == ft_face);
97
98   hb_ft_font_unlock_face (font);
99
100   hb_ft_font_set_load_flags (font, FT_LOAD_NO_SCALE | FT_LOAD_NO_AUTOHINT);
101   int load_flags = hb_ft_font_get_load_flags (font);
102
103   g_assert_true (load_flags == (FT_LOAD_NO_SCALE | FT_LOAD_NO_AUTOHINT));
104
105   hb_font_destroy (font);
106
107   FT_Done_Face (ft_face);
108
109   cleanup_freetype ();
110 }
111
112 int
113 main (int argc, char **argv)
114 {
115   hb_test_init (&argc, &argv);
116
117   hb_test_add (test_native_ft_basic);
118
119   return hb_test_run ();
120 }