1 | /* Pango |
2 | * test-ellipsize.c: Test Pango harfbuzz apis |
3 | * |
4 | * Copyright (C) 2019 Red Hat, Inc. |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Library General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Library General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Library General Public |
17 | * License along with this library; if not, write to the |
18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | * Boston, MA 02111-1307, USA. |
20 | */ |
21 | |
22 | #include <pango/pango.h> |
23 | #include <pango/pangocairo.h> |
24 | #include "test-common.h" |
25 | |
26 | static PangoContext *context; |
27 | |
28 | /* Test that ellipsization does not change the height of a layout. |
29 | * See https://gitlab.gnome.org/GNOME/pango/issues/397 |
30 | */ |
31 | static void |
32 | test_ellipsize_height (void) |
33 | { |
34 | PangoLayout *layout; |
35 | int height1, height2; |
36 | PangoFontDescription *desc; |
37 | |
38 | layout = pango_layout_new (context); |
39 | |
40 | desc = pango_font_description_from_string (str: "Fixed 7" ); |
41 | //pango_layout_set_font_description (layout, desc); |
42 | pango_font_description_free (desc); |
43 | |
44 | pango_layout_set_text (layout, text: "some text that should be ellipsized" , length: -1); |
45 | g_assert_cmpint (pango_layout_get_line_count (layout), ==, 1); |
46 | pango_layout_get_size (layout, NULL, height: &height1); |
47 | |
48 | pango_layout_set_width (layout, width: 100 * PANGO_SCALE); |
49 | pango_layout_set_ellipsize (layout, ellipsize: PANGO_ELLIPSIZE_END); |
50 | |
51 | g_assert_cmpint (pango_layout_get_line_count (layout), ==, 1); |
52 | g_assert_cmpint (pango_layout_is_ellipsized (layout), ==, 1); |
53 | pango_layout_get_size (layout, NULL, height: &height2); |
54 | |
55 | g_assert_cmpint (height1, ==, height2); |
56 | |
57 | g_object_unref (object: layout); |
58 | } |
59 | |
60 | /* Test that ellipsization without attributes does not crash |
61 | */ |
62 | static void |
63 | test_ellipsize_crash (void) |
64 | { |
65 | PangoLayout *layout; |
66 | |
67 | layout = pango_layout_new (context); |
68 | |
69 | pango_layout_set_text (layout, text: "some text that should be ellipsized" , length: -1); |
70 | g_assert_cmpint (pango_layout_get_line_count (layout), ==, 1); |
71 | |
72 | pango_layout_set_width (layout, width: 100 * PANGO_SCALE); |
73 | pango_layout_set_ellipsize (layout, ellipsize: PANGO_ELLIPSIZE_END); |
74 | |
75 | g_assert_cmpint (pango_layout_get_line_count (layout), ==, 1); |
76 | g_assert_cmpint (pango_layout_is_ellipsized (layout), ==, 1); |
77 | |
78 | g_object_unref (object: layout); |
79 | } |
80 | |
81 | /* Check that the width of a fully ellipsized paragraph |
82 | * is the same as that of an explicit ellipsis. |
83 | */ |
84 | static void |
85 | test_ellipsize_fully (void) |
86 | { |
87 | PangoLayout *layout; |
88 | PangoRectangle ink, logical; |
89 | PangoRectangle ink2, logical2; |
90 | |
91 | layout = pango_layout_new (context); |
92 | |
93 | pango_layout_set_text (layout, text: "…" , length: -1); |
94 | pango_layout_get_extents (layout, ink_rect: &ink, logical_rect: &logical); |
95 | |
96 | pango_layout_set_text (layout, text: "ellipsized" , length: -1); |
97 | |
98 | pango_layout_set_width (layout, width: 10 * PANGO_SCALE); |
99 | pango_layout_set_ellipsize (layout, ellipsize: PANGO_ELLIPSIZE_END); |
100 | |
101 | pango_layout_get_extents (layout, ink_rect: &ink2, logical_rect: &logical2); |
102 | |
103 | g_assert_cmpint (ink.width, ==, ink2.width); |
104 | g_assert_cmpint (logical.width, ==, logical2.width); |
105 | |
106 | g_object_unref (object: layout); |
107 | } |
108 | |
109 | int |
110 | main (int argc, char *argv[]) |
111 | { |
112 | PangoFontMap *fontmap; |
113 | |
114 | fontmap = pango_cairo_font_map_get_default (); |
115 | context = pango_font_map_create_context (fontmap); |
116 | |
117 | g_test_init (argc: &argc, argv: &argv, NULL); |
118 | |
119 | g_test_add_func (testpath: "/layout/ellipsize/height" , test_func: test_ellipsize_height); |
120 | g_test_add_func (testpath: "/layout/ellipsize/crash" , test_func: test_ellipsize_crash); |
121 | g_test_add_func (testpath: "/layout/ellipsize/fully" , test_func: test_ellipsize_fully); |
122 | |
123 | return g_test_run (); |
124 | } |
125 | |