blob: a7d1c0b44bb6ba6bd6af51f9db33456da5cfb25b [file] [edit]
/*
* Copyright © 2019 Ebrahim Byagowi
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "hb-test.h"
#include <hb.h>
/* Unit tests for hb-style.h */
#define assert_cmpfloat(n1, n2) g_assert_cmpint ((int) (n1 * 100.f), ==, (int) (n2 * 100.f))
static void
test_empty_face (void)
{
hb_font_t *empty = hb_font_get_empty ();
assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_WEIGHT), 400);
}
static void
test_regular_face (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
hb_font_t *font = hb_font_create (face);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 400);
hb_font_destroy (font);
hb_face_destroy (face);
}
static void
test_optical_size_os2 (void)
{
char os2_data[100] = {0};
os2_data[1] = 5; /* Version 5. */
os2_data[97] = (char) 160; /* Lower optical size: 8 points in TWIPs. */
os2_data[98] = 1;
os2_data[99] = (char) 224; /* Upper optical size: 24 points in TWIPs. */
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "OS/2", os2_data);
hb_font_t *font = hb_font_create (face);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 16);
hb_font_set_ptem (font, 10);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 10);
hb_font_destroy (font);
hb_face_destroy (face);
}
static void
test_bold_face (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf");
hb_font_t *font = hb_font_create (face);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 700);
hb_font_destroy (font);
hb_face_destroy (face);
}
static void
test_face_user_setting (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/AdobeVFPrototype_vsindex.otf");
hb_font_t *font = hb_font_create (face);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 389.34f); /* its default weight */
assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
hb_font_set_var_named_instance (font, 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 200);
assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
hb_font_set_var_named_instance (font, 1);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 300);
assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
hb_font_set_var_named_instance (font, 2);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 400);
assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
hb_font_set_var_named_instance (font, 3);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT),600);
assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
hb_font_set_var_named_instance (font, 4);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 700);
assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
hb_font_set_var_named_instance (font, 5);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 900);
assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
hb_font_set_var_named_instance (font, 6);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 900);
assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 50);
hb_font_set_var_named_instance (font, 7);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 900);
assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 100);
hb_font_destroy (font);
hb_face_destroy (face);
}
static void
test_synthetic_slant (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/AdobeVFPrototype_vsindex.otf");
hb_font_t *font = hb_font_create (face);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO), 0);
hb_font_set_synthetic_slant (font, 0.2);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO), 0.2);
hb_font_destroy (font);
hb_face_destroy (face);
face = hb_test_open_font_file ("fonts/notosansitalic.ttf");
font = hb_font_create (face);
/* We expect a negative angle for a typical italic font,
* which should give us a positive ratio
*/
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), -12);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO), 0.21);
hb_font_destroy (font);
hb_face_destroy (face);
}
static void
test_synthetic_slant_style_tables (void)
{
static const char stat[] = {
0, 1, 0, 1, 0, 8, 0, 1, 0, 0, 0, 20, 0, 1, 0, 0, 0, 28, 0, 0,
's', 'l', 'n', 't', 1, 0, 0, 0,
0, 2, /* Axis value offset. */
0, 1, 0, 0, 0, 0, 1, 1, (char) 0xFF, (char) 0xF6, 0, 0 /* -10 degrees. */
};
unsigned sources = 1;
#ifndef HB_NO_VAR
static const char fvar[] = {
0, 1, 0, 0, 0, 16, 0, 2, 0, 1, 0, 20, 0, 0, 0, 8,
's', 'l', 'n', 't',
(char) 0xFF, (char) 0xE2, 0, 0, /* Minimum -30. */
(char) 0xFF, (char) 0xF6, 0, 0, /* Default -10. */
0, 0, 0, 0, 0, 0, 1, 0 /* Maximum 0, flags, name ID. */
};
sources++;
#endif
for (unsigned source = 0; source < sources; source++)
{
hb_face_t *face = hb_face_builder_create ();
if (!source)
HB_FACE_ADD_TABLE (face, "STAT", stat);
#ifndef HB_NO_VAR
else
HB_FACE_ADD_TABLE (face, "fvar", fvar);
#endif
hb_font_t *font = hb_font_create (face);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), -10);
float ratio = hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO);
hb_font_set_synthetic_slant (font, 0.2f);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO), (ratio + 0.2f));
g_assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), <, -10);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 400);
#ifndef HB_NO_VAR
if (source)
{
hb_variation_t variation = {HB_TAG ('s','l','n','t'), -20};
hb_font_set_variations (font, &variation, 1);
hb_font_set_synthetic_slant (font, 0);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), -20);
ratio = hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO);
hb_font_set_synthetic_slant (font, 0.2f);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO), (ratio + 0.2f));
}
#endif
hb_font_destroy (font);
hb_face_destroy (face);
}
}
static void
test_stat_current_font_values (void)
{
static const struct {
const char *font;
float optical_size, slant;
} cases[] = {
{"fonts/stat-current-scalar.ttf", 18, -12},
{"fonts/stat-current-combined.ttf", 18, -12},
{"fonts/stat-current-partial.ttf", 18, 0},
{"fonts/stat-current-unknown.ttf", 12, 0},
};
for (unsigned i = 0; i < G_N_ELEMENTS (cases); i++)
{
hb_face_t *face = hb_test_open_font_file (cases[i].font);
hb_font_t *font = hb_font_create (face);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 700);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 75);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), cases[i].optical_size);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), cases[i].slant);
float ratio = hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO);
hb_font_set_synthetic_slant (font, 0.2f);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO), (ratio + 0.2f));
hb_font_set_ptem (font, 24);
assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 24);
hb_font_destroy (font);
hb_face_destroy (face);
}
}
int
main (int argc, char **argv)
{
hb_test_init (&argc, &argv);
hb_test_add (test_empty_face);
hb_test_add (test_regular_face);
hb_test_add (test_optical_size_os2);
hb_test_add (test_bold_face);
hb_test_add (test_face_user_setting);
hb_test_add (test_synthetic_slant);
hb_test_add (test_synthetic_slant_style_tables);
hb_test_add (test_stat_current_font_values);
return hb_test_run ();
}