123d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod/*
223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * Copyright © 2011  Google, Inc.
323d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod *
423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
523d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod *
623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * Permission is hereby granted, without written agreement and without
723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * software and its documentation for any purpose, provided that the
923d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
1023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * all copies of this software.
1123d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod *
1223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
1323d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
1423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
1523d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
1623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * DAMAGE.
1723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod *
1823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
1923d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
2023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
2123d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
2223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
2323d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod *
2423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod * Google Author(s): Behdad Esfahbod
2523d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod */
2623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
2723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod#include "hb-test.h"
2823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
2923d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod/* Unit tests for hb-shape.h */
3023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
31a0359485c9997e3a211f7c00d40c09074d906c4fBehdad Esfahbod/*
32a0359485c9997e3a211f7c00d40c09074d906c4fBehdad Esfahbod * This test provides a framework to test aspects of hb_shape() that are
33a0359485c9997e3a211f7c00d40c09074d906c4fBehdad Esfahbod * font-independent.  Please add tests for any feature that fits that
34a0359485c9997e3a211f7c00d40c09074d906c4fBehdad Esfahbod * description.
35a0359485c9997e3a211f7c00d40c09074d906c4fBehdad Esfahbod */
36a0359485c9997e3a211f7c00d40c09074d906c4fBehdad Esfahbod
375bf56ea056b30fba8e07e82ec818c430cab8cafdBehdad Esfahbod/* TODO Make this test data-driven and add some real test data */
38fc551edbf236d71a522ae7c2c9461aa71c5f7d66Behdad Esfahbod/* TODO Test positions too. And test non-native direction.  Test commit 2e18c6dbdfb */
395bf56ea056b30fba8e07e82ec818c430cab8cafdBehdad Esfahbod
4023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
4123d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbodstatic const char test_data[] = "test\0data";
4223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
432d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbodstatic hb_position_t
44d31691296f7d3051fcd345bf1325d17835484b50Behdad Esfahbodglyph_h_advance_func (hb_font_t *font, void *font_data,
45d31691296f7d3051fcd345bf1325d17835484b50Behdad Esfahbod		      hb_codepoint_t glyph,
46d31691296f7d3051fcd345bf1325d17835484b50Behdad Esfahbod		      void *user_data)
4723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod{
4823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  switch (glyph) {
492d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbod  case 1: return 10;
502d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbod  case 2: return 6;
512d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbod  case 3: return 5;
5223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  }
532d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbod  return 0;
5423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod}
5523d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
560fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbodstatic hb_bool_t
5723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbodglyph_func (hb_font_t *font, void *font_data,
5823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod	    hb_codepoint_t unicode, hb_codepoint_t variant_selector,
590fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod	    hb_codepoint_t *glyph,
6023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod	    void *user_data)
6123d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod{
6223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  switch (unicode) {
630fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod  case 'T': *glyph = 1; return TRUE;
640fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod  case 'e': *glyph = 2; return TRUE;
650fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod  case 's': *glyph = 3; return TRUE;
6623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  }
670fd8c2f1be693616f19f2f1526369874763d6cf6Behdad Esfahbod  return FALSE;
6823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod}
6923d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
702d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbodstatic hb_position_t
71d31691296f7d3051fcd345bf1325d17835484b50Behdad Esfahbodglyph_h_kerning_func (hb_font_t *font, void *font_data,
72d31691296f7d3051fcd345bf1325d17835484b50Behdad Esfahbod		      hb_codepoint_t left, hb_codepoint_t right,
73d31691296f7d3051fcd345bf1325d17835484b50Behdad Esfahbod		      void *user_data)
7423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod{
752d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbod  if (left == 1 && right == 2)
762d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbod    return -2;
772d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbod
782d8ebcb9d089c2cfbefac71ca6350e2703ab13e3Behdad Esfahbod  return 0;
7923d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod}
8023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
8123d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbodstatic const char TesT[] = "TesT";
8223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
8323d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbodstatic void
848e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbodtest_shape (void)
8523d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod{
8623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_blob_t *blob;
8723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_face_t *face;
8823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_font_funcs_t *ffuncs;
8923d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_font_t *font;
9023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_buffer_t *buffer;
9123d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  unsigned int len;
9223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_glyph_info_t *glyphs;
9323d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_glyph_position_t *positions;
9423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
9523d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
9623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  face = hb_face_create (blob, 0);
9723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_blob_destroy (blob);
9823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  font = hb_font_create (face);
9923d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_face_destroy (face);
10023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_font_set_scale (font, 10, 10);
10123d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
10223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  ffuncs = hb_font_funcs_create ();
103d31691296f7d3051fcd345bf1325d17835484b50Behdad Esfahbod  hb_font_funcs_set_glyph_h_advance_func (ffuncs, glyph_h_advance_func, NULL, NULL);
10423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_font_funcs_set_glyph_func (ffuncs, glyph_func, NULL, NULL);
105d31691296f7d3051fcd345bf1325d17835484b50Behdad Esfahbod  hb_font_funcs_set_glyph_h_kerning_func (ffuncs, glyph_h_kerning_func, NULL, NULL);
10623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_font_set_funcs (font, ffuncs, NULL, NULL);
10723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_font_funcs_destroy (ffuncs);
10823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
109e6c09cdf43201ff1b7f38e411ae1f9977e4f9271Behdad Esfahbod  buffer =  hb_buffer_create ();
11023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_buffer_set_direction (buffer, HB_DIRECTION_LTR);
11123d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_buffer_add_utf8 (buffer, TesT, 4, 0, 4);
11223d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
1133ca6c4ecc299295b6682fa2b6b9f83b213223badBehdad Esfahbod  hb_shape (font, buffer, NULL, 0);
11423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
11523d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  len = hb_buffer_get_length (buffer);
11623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  glyphs = hb_buffer_get_glyph_infos (buffer, NULL);
11723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  positions = hb_buffer_get_glyph_positions (buffer, NULL);
11823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
1198e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod  {
1208e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod    const hb_codepoint_t output_glyphs[] = {1, 2, 3, 1};
1218e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod    const hb_position_t output_x_advances[] = {9, 5, 5, 10};
1228e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod    const hb_position_t output_x_offsets[] = {0, -1, 0, 0};
1238e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod    unsigned int i;
1248e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod    g_assert_cmpint (len, ==, 4);
1258e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod    for (i = 0; i < len; i++) {
1268e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod      g_assert_cmphex (glyphs[i].codepoint, ==, output_glyphs[i]);
1278e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod      g_assert_cmphex (glyphs[i].cluster,   ==, i);
1288e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod    }
1298e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod    for (i = 0; i < len; i++) {
1308e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod      g_assert_cmpint (output_x_advances[i], ==, positions[i].x_advance);
1318e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod      g_assert_cmpint (output_x_offsets [i], ==, positions[i].x_offset);
1328e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod      g_assert_cmpint (0, ==, positions[i].y_advance);
1338e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod      g_assert_cmpint (0, ==, positions[i].y_offset);
1348e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod    }
1358e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod  }
13623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
13723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_buffer_destroy (buffer);
13823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_font_destroy (font);
13923d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod}
14023d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
1419da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbodstatic void
1429da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbodtest_shape_list (void)
1439da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod{
1449da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod  const char **shapers = hb_shape_list_shapers ();
1459da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod
1469da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod  unsigned int i;
1479da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod  for (i = 0; shapers[i]; i++)
1489da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod    ;
1499da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod
1509da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod  g_assert_cmpint (i, >, 1);
1519da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod  g_assert (!strcmp (shapers[i - 1], "fallback"));
1529da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod}
15323d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
15423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbodint
15523d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbodmain (int argc, char **argv)
15623d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod{
15723d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  hb_test_init (&argc, &argv);
15823d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
1598e07f93ab4a3ef9adc7942727ef21f2f9a141d10Behdad Esfahbod  hb_test_add (test_shape);
1609da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod  /* TODO test fallback shaper */
1619da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod  /* TODO test shaper_full */
1629da554504e30a326fc57b28cdb0e57108bfa9555Behdad Esfahbod  hb_test_add (test_shape_list);
16323d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod
16423d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod  return hb_test_run();
16523d2432219a91c6328efa9e041b1ecf137752ac3Behdad Esfahbod}
166