summaryrefslogtreecommitdiff
path: root/tests/test-ellipsize.c
blob: 163184ff1f7c6c64db845a7a6518db3175ecfb69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Pango
 * test-ellipsize.c: Test Pango harfbuzz apis
 *
 * Copyright (C) 2019 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <pango/pango.h>
#include <pango/pangocairo.h>
#include "test-common.h"

static PangoContext *context;

/* Test that ellipsization does not change the height of a layout.
 * See https://gitlab.gnome.org/GNOME/pango/issues/397
 */
static void
test_ellipsize_height (void)
{
  PangoLayout *layout;
  int height1, height2;
  PangoFontDescription *desc;

  layout = pango_layout_new (context);

  desc = pango_font_description_from_string ("Fixed 7");
  //pango_layout_set_font_description (layout, desc);
  pango_font_description_free (desc);

  pango_layout_set_text (layout, "some text that should be ellipsized", -1);
  g_assert_cmpint (pango_layout_get_line_count (layout), ==, 1);
  pango_layout_get_size (layout, NULL, &height1);

  pango_layout_set_width (layout, 100 * PANGO_SCALE);
  pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);

  g_assert_cmpint (pango_layout_get_line_count (layout), ==, 1);
  g_assert_cmpint (pango_layout_is_ellipsized (layout), ==, 1);
  pango_layout_get_size (layout, NULL, &height2);

  g_assert_cmpint (height1, ==, height2);

  g_object_unref (layout);
}

/* Test that ellipsization without attributes does not crash
 */
static void
test_ellipsize_crash (void)
{
  PangoLayout *layout;

  layout = pango_layout_new (context);

  pango_layout_set_text (layout, "some text that should be ellipsized", -1);
  g_assert_cmpint (pango_layout_get_line_count (layout), ==, 1);

  pango_layout_set_width (layout, 100 * PANGO_SCALE);
  pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);

  g_assert_cmpint (pango_layout_get_line_count (layout), ==, 1);
  g_assert_cmpint (pango_layout_is_ellipsized (layout), ==, 1);

  g_object_unref (layout);
}

/* Check that the width of a fully ellipsized paragraph
 * is the same as that of an explicit ellipsis.
 */
static void
test_ellipsize_fully (void)
{
  PangoLayout *layout;
  PangoRectangle ink, logical;
  PangoRectangle ink2, logical2;

  layout = pango_layout_new (context);

  pango_layout_set_text (layout, "…", -1);
  pango_layout_get_extents (layout, &ink, &logical);

  pango_layout_set_text (layout, "ellipsized", -1);

  pango_layout_set_width (layout, 10 * PANGO_SCALE);
  pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);

  pango_layout_get_extents (layout, &ink2, &logical2);

  g_assert_cmpint (ink.width, ==, ink2.width);
  g_assert_cmpint (logical.width, ==, logical2.width);

  g_object_unref (layout);
}

int
main (int argc, char *argv[])
{
  PangoFontMap *fontmap;

  fontmap = pango_cairo_font_map_get_default ();
  context = pango_font_map_create_context (fontmap);

  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/layout/ellipsize/height", test_ellipsize_height);
  g_test_add_func ("/layout/ellipsize/crash", test_ellipsize_crash);
  g_test_add_func ("/layout/ellipsize/fully", test_ellipsize_fully);

  return g_test_run ();
}