test-break.c 9.45 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
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
/* Pango
 * test-break.c: Test Pango line breaking
 *
 * 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 <glib.h>
#include <string.h>
#include <locale.h>

#ifndef G_OS_WIN32
#include <unistd.h>
#endif

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


static PangoContext *context;

37
static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
38 39 40 41 42 43 44 45 46 47 48
test_file (const gchar *filename, GString *string)
{
  gchar *contents;
  gsize  length;
  GError *error = NULL;
  PangoLogAttr *attrs;
  int len;
  char *p;
  int i;
  GString *s1, *s2, *s3, *s4;
  int m;
49
  char *test;
50 51 52
  char *text;
  PangoAttrList *attributes;
  PangoLayout *layout;
Matthias Clasen's avatar
Matthias Clasen committed
53

54 55
  g_file_get_contents (filename, &contents, &length, &error);
  g_assert_no_error (error);
Matthias Clasen's avatar
Matthias Clasen committed
56

57 58 59 60 61 62 63 64
  test = contents;

  /* Skip initial comments */
  while (test[0] == '#')
    test = strchr (test, '\n') + 1;

  length = strlen (test);
  len = g_utf8_strlen (test, -1) + 1;
Matthias Clasen's avatar
Matthias Clasen committed
65 66
  attrs = g_new (PangoLogAttr, len);

67 68
  pango_parse_markup (test, -1, 0, &attributes, &text, NULL, &error);
  g_assert_no_error (error);
69 70 71 72

  layout = pango_layout_new (context);
  pango_layout_set_text (layout, text, length);
  pango_layout_set_attributes (layout, attributes);
Matthias Clasen's avatar
Matthias Clasen committed
73 74 75

  if (pango_layout_get_unknown_glyphs_count (layout) > 0)
    {
76 77
#if 0
      // See https://github.com/mesonbuild/meson/issues/7515
Matthias Clasen's avatar
Matthias Clasen committed
78 79 80
      char *msg = g_strdup_printf ("Missing glyphs - skipping %s. Maybe fonts are missing?", filename);
      g_test_skip (msg);
      g_free (msg);
81 82
#endif
      return FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
83 84
    }

85
  pango_layout_get_log_attrs (layout, &attrs, &len);
Matthias Clasen's avatar
Matthias Clasen committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

  s1 = g_string_new ("Breaks: ");
  s2 = g_string_new ("Whitespace: ");
  s3 = g_string_new ("Words:");
  s4 = g_string_new ("Sentences:");

  g_string_append (string, "Text: ");

  m = MAX (MAX (s1->len, s2->len), MAX (s3->len, s4->len));

  g_string_append_printf (s1, "%*s", (int)(m - s1->len), "");
  g_string_append_printf (s2, "%*s", (int)(m - s2->len), "");
  g_string_append_printf (s3, "%*s", (int)(m - s3->len), "");
  g_string_append_printf (s4, "%*s", (int)(m - s4->len), "");
  g_string_append_printf (string, "%*s", (int)(m - strlen ("Text: ")), "");

102
  for (i = 0, p = text; i < len; i++, p = g_utf8_next_char (p))
Matthias Clasen's avatar
Matthias Clasen committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    {
      PangoLogAttr log = attrs[i];
      int b = 0;
      int w = 0;
      int o = 0;
      int s = 0;

      if (log.is_mandatory_break)
        {
          g_string_append (s1, "L");
          b++;
        }
      else if (log.is_line_break)
        {
          g_string_append (s1, "l");
          b++;
        }
      if (log.is_char_break)
        {
          g_string_append (s1, "c");
          b++;
        }

      if (log.is_expandable_space)
        {
          g_string_append (s2, "x");
          w++;
        }
      else if (log.is_white)
        {
          g_string_append (s2, "w");
          w++;
        }

137 138 139 140 141
      if (log.is_word_boundary)
        {
          g_string_append (s3, "b");
          o++;
        }
Matthias Clasen's avatar
Matthias Clasen committed
142 143 144 145 146 147 148 149 150 151 152
      if (log.is_word_start)
        {
          g_string_append (s3, "s");
          o++;
        }
      if (log.is_word_end)
        {
          g_string_append (s3, "e");
          o++;
        }

153 154 155 156 157
      if (log.is_sentence_boundary)
        {
          g_string_append (s4, "b");
          s++;
        }
Matthias Clasen's avatar
Matthias Clasen committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
      if (log.is_sentence_start)
        {
          g_string_append (s4, "s");
          s++;
        }
      if (log.is_sentence_end)
        {
          g_string_append (s4, "e");
          s++;
        }

      m = MAX (MAX (b, w), MAX (o, s));

      g_string_append_printf (string, "%*s", m, "");
      g_string_append_printf (s1, "%*s", m - b, "");
      g_string_append_printf (s2, "%*s", m - w, "");
      g_string_append_printf (s3, "%*s", m - o, "");
      g_string_append_printf (s4, "%*s", m - s, "");

      if (i < len - 1)
        {
          gunichar ch = g_utf8_get_char (p);
          if (ch == 0x20)
            {
              g_string_append (string, "[ ]");
              g_string_append (s1, "   ");
              g_string_append (s2, "   ");
              g_string_append (s3, "   ");
              g_string_append (s4, "   ");
            }
188 189 190
          else if (g_unichar_isgraph (ch) &&
                   !(g_unichar_type (ch) == G_UNICODE_LINE_SEPARATOR ||
                     g_unichar_type (ch) == G_UNICODE_PARAGRAPH_SEPARATOR))
Matthias Clasen's avatar
Matthias Clasen committed
191 192 193 194 195 196 197 198 199 200
            {
              g_string_append_unichar (string, ch);
              g_string_append (s1, " ");
              g_string_append (s2, " ");
              g_string_append (s3, " ");
              g_string_append (s4, " ");
            }
          else
            {
              char *str = g_strdup_printf ("[%#04x]", ch);
201
              g_string_append (string, str);
Matthias Clasen's avatar
Matthias Clasen committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
              g_string_append_printf (s1, "%*s", (int)strlen (str), "");
              g_string_append_printf (s2, "%*s", (int)strlen (str), "");
              g_string_append_printf (s3, "%*s", (int)strlen (str), "");
              g_string_append_printf (s4, "%*s", (int)strlen (str), "");
              g_free (str);
            }
        }
    }
  g_string_append (string, "\n");
  g_string_append_len (string, s1->str, s1->len);
  g_string_append (string, "\n");
  g_string_append_len (string, s2->str, s2->len);
  g_string_append (string, "\n");
  g_string_append_len (string, s3->str, s3->len);
  g_string_append (string, "\n");
  g_string_append_len (string, s4->str, s4->len);
  g_string_append (string, "\n");

  g_string_free (s1, TRUE);
  g_string_free (s2, TRUE);
  g_string_free (s3, TRUE);
  g_string_free (s4, TRUE);

225
  g_object_unref (layout);
Matthias Clasen's avatar
Matthias Clasen committed
226 227
  g_free (attrs);
  g_free (contents);
228
  pango_attr_list_unref (attributes);
229 230

  return TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
}

static gchar *
get_expected_filename (const gchar *filename)
{
  gchar *f, *p, *expected;

  f = g_strdup (filename);
  p = strstr (f, ".break");
  if (p)
    *p = 0;
  expected = g_strconcat (f, ".expected", NULL);

  g_free (f);

  return expected;
}

static void
test_break (gconstpointer d)
{
  const gchar *filename = d;
  gchar *expected_file;
  GError *error = NULL;
  GString *dump;
  gchar *diff;

258
  const char *old_locale = setlocale (LC_ALL, NULL);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
259
  setlocale (LC_ALL, "en_US.utf8");
260 261
  if (strstr (setlocale (LC_ALL, NULL), "en_US") == NULL)
    {
262 263
#if 0
      // See https://github.com/mesonbuild/meson/issues/7515
264 265 266
      char *msg = g_strdup_printf ("Locale en_US.UTF-8 not available, skipping break %s", filename);
      g_test_skip (msg);
      g_free (msg);
267
#endif
268 269 270
      return;
    }

Matthias Clasen's avatar
Matthias Clasen committed
271 272 273 274
  expected_file = get_expected_filename (filename);

  dump = g_string_sized_new (0);

275 276
  if (!test_file (filename, dump))
    return;
Matthias Clasen's avatar
Matthias Clasen committed
277

278
  diff = diff_with_file (expected_file, dump->str, dump->len, &error);
Matthias Clasen's avatar
Matthias Clasen committed
279 280
  g_assert_no_error (error);

281 282
  setlocale (LC_ALL, old_locale);

Matthias Clasen's avatar
Matthias Clasen committed
283 284
  if (diff && diff[0])
    {
285 286 287 288
      char **lines = g_strsplit (diff, "\n", -1);
      const char *line;
      int i = 0;

289
      g_test_message ("Contents don't match expected contents");
290 291 292 293

      for (line = lines[0]; line != NULL; line = lines[++i])
        g_test_message ("%s", line);

Matthias Clasen's avatar
Matthias Clasen committed
294
      g_test_fail ();
295 296

      g_strfreev (lines);
Matthias Clasen's avatar
Matthias Clasen committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
      g_free (diff);
    }

  g_string_free (dump, TRUE);
  g_free (expected_file);
}

int
main (int argc, char *argv[])
{
  GDir *dir;
  GError *error = NULL;
  const gchar *name;
  gchar *path;

Matthias Clasen's avatar
Matthias Clasen committed
312 313 314 315
  setlocale (LC_ALL, "");

  context = pango_font_map_create_context (pango_cairo_font_map_get_default ());

Matthias Clasen's avatar
Matthias Clasen committed
316 317 318
  /* allow to easily generate expected output for new test cases */
  if (argc > 1)
    {
Matthias Clasen's avatar
Matthias Clasen committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
      if (strcmp (argv[1], "--help") == 0)
        {
          g_print ("test-break uses the following symbols for log attrs\n\n");
          g_print ("Breaks:                 Words:\n"
                   " L - mandatory break     b - word boundary\n"
                   " l - line break          s - word start\n"
                   " c - char break          e - word end\n"
                   "\n"
                   "Whitespace:             Sentences:\n"
                   " x - expandable space    s - sentence start\n"
                   " w - whitespace          e - sentence end\n");
        }
      else
        {
          GString *string;
Matthias Clasen's avatar
Matthias Clasen committed
334

Matthias Clasen's avatar
Matthias Clasen committed
335 336 337 338
          string = g_string_sized_new (0);
          test_file (argv[1], string);
          g_print ("%s", string->str);
        }
Matthias Clasen's avatar
Matthias Clasen committed
339 340 341 342

      return 0;
    }

Matthias Clasen's avatar
Matthias Clasen committed
343 344
  g_test_init (&argc, &argv, NULL);

Matthias Clasen's avatar
Matthias Clasen committed
345 346 347 348 349 350 351 352 353
  path = g_test_build_filename (G_TEST_DIST, "breaks", NULL);
  dir = g_dir_open (path, 0, &error);
  g_free (path);
  g_assert_no_error (error);
  while ((name = g_dir_read_name (dir)) != NULL)
    {
      if (!strstr (name, "break"))
        continue;

354 355 356 357 358 359
#ifndef HAVE_LIBTHAI
      /* four.break involves Thai, so only test it when we have libthai */
      if (strstr (name, "four.break"))
        continue;
#endif

Matthias Clasen's avatar
Matthias Clasen committed
360 361 362 363 364 365 366 367 368
      path = g_strdup_printf ("/break/%s", name);
      g_test_add_data_func_full (path, g_test_build_filename (G_TEST_DIST, "breaks", name, NULL),
                                 test_break, g_free);
      g_free (path);
    }
  g_dir_close (dir);

  return g_test_run ();
}