[test] Adopt test-ot-color with ot-glyph and remove freetype/cairo dependecy
This commit is contained in:
parent
e171beeb5f
commit
b72337e57e
|
@ -377,14 +377,10 @@ test_gsub_would_substitute_SOURCES = test-gsub-would-substitute.cc
|
||||||
test_gsub_would_substitute_CPPFLAGS = $(HBCFLAGS) $(FREETYPE_CFLAGS)
|
test_gsub_would_substitute_CPPFLAGS = $(HBCFLAGS) $(FREETYPE_CFLAGS)
|
||||||
test_gsub_would_substitute_LDADD = libharfbuzz.la $(HBLIBS) $(FREETYPE_LIBS)
|
test_gsub_would_substitute_LDADD = libharfbuzz.la $(HBLIBS) $(FREETYPE_LIBS)
|
||||||
|
|
||||||
if HAVE_FREETYPE
|
|
||||||
if HAVE_CAIRO_FT
|
|
||||||
noinst_PROGRAMS += test-ot-color
|
noinst_PROGRAMS += test-ot-color
|
||||||
test_ot_color_SOURCES = test-ot-color.cc
|
test_ot_color_SOURCES = test-ot-color.cc
|
||||||
test_ot_color_CPPFLAGS = $(HBCFLAGS) $(FREETYPE_CFLAGS) $(CAIRO_FT_CFLAGS)
|
test_ot_color_CPPFLAGS = $(HBCFLAGS)
|
||||||
test_ot_color_LDADD = libharfbuzz.la $(HBLIBS) $(FREETYPE_LIBS) $(CAIRO_LIBS) $(CAIRO_FT_LIBS)
|
test_ot_color_LDADD = libharfbuzz.la $(HBLIBS)
|
||||||
endif # HAVE_CAIRO_FT
|
|
||||||
endif # HAVE_FREETYPE
|
|
||||||
|
|
||||||
dist_check_SCRIPTS = \
|
dist_check_SCRIPTS = \
|
||||||
check-c-linkage-decls.sh \
|
check-c-linkage-decls.sh \
|
||||||
|
|
|
@ -25,25 +25,14 @@
|
||||||
|
|
||||||
#include "hb.hh"
|
#include "hb.hh"
|
||||||
|
|
||||||
#include <cairo.h>
|
|
||||||
|
|
||||||
#ifdef HB_NO_OPEN
|
#ifdef HB_NO_OPEN
|
||||||
#define hb_blob_create_from_file(x) hb_blob_get_empty ()
|
#define hb_blob_create_from_file(x) hb_blob_get_empty ()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(HB_NO_COLOR) && defined(CAIRO_HAS_SVG_SURFACE)
|
#if !defined(HB_NO_COLOR)
|
||||||
|
|
||||||
#include "hb-ot.h"
|
#include "hb-ot.h"
|
||||||
|
|
||||||
#include "hb-ft.h"
|
|
||||||
|
|
||||||
#include <ft2build.h>
|
|
||||||
#include FT_FREETYPE_H
|
|
||||||
#include FT_GLYPH_H
|
|
||||||
|
|
||||||
#include <cairo-ft.h>
|
|
||||||
#include <cairo-svg.h>
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
@ -133,137 +122,152 @@ png_dump (hb_face_t *face, unsigned int face_index)
|
||||||
hb_font_destroy (font);
|
hb_font_destroy (font);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
struct user_data_t
|
||||||
layered_glyph_dump (hb_face_t *face, cairo_font_face_t *cairo_face, unsigned int face_index)
|
|
||||||
{
|
{
|
||||||
unsigned int upem = hb_face_get_upem (face);
|
FILE *f;
|
||||||
|
hb_position_t ascender;
|
||||||
|
};
|
||||||
|
|
||||||
unsigned glyph_count = hb_face_get_glyph_count (face);
|
static void
|
||||||
for (hb_codepoint_t gid = 0; gid < glyph_count; ++gid)
|
move_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data)
|
||||||
|
{
|
||||||
|
fprintf (user_data.f, "M%d,%d", to_x, user_data.ascender - to_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
line_to (hb_position_t to_x, hb_position_t to_y, user_data_t &user_data)
|
||||||
|
{
|
||||||
|
fprintf (user_data.f, "L%d,%d", to_x, user_data.ascender - to_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
conic_to (hb_position_t control_x, hb_position_t control_y,
|
||||||
|
hb_position_t to_x, hb_position_t to_y,
|
||||||
|
user_data_t &user_data)
|
||||||
|
{
|
||||||
|
fprintf (user_data.f, "Q%d,%d %d,%d", control_x, user_data.ascender - control_y,
|
||||||
|
to_x, user_data.ascender - to_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
cubic_to (hb_position_t control1_x, hb_position_t control1_y,
|
||||||
|
hb_position_t control2_x, hb_position_t control2_y,
|
||||||
|
hb_position_t to_x, hb_position_t to_y,
|
||||||
|
user_data_t &user_data)
|
||||||
|
{
|
||||||
|
fprintf (user_data.f, "C%d,%d %d,%d %d,%d", control1_x, user_data.ascender - control1_y,
|
||||||
|
control2_x, user_data.ascender - control2_y,
|
||||||
|
to_x, user_data.ascender - to_y);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
close_path (user_data_t &user_data)
|
||||||
|
{
|
||||||
|
fprintf (user_data.f, "Z");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
layered_glyph_dump (hb_font_t *font, hb_ot_glyph_decompose_funcs_t *funcs, unsigned int face_index)
|
||||||
|
{
|
||||||
|
hb_face_t *face = hb_font_get_face (font);
|
||||||
|
unsigned num_glyphs = hb_face_get_glyph_count (face);
|
||||||
|
for (hb_codepoint_t gid = 0; gid < num_glyphs; ++gid)
|
||||||
{
|
{
|
||||||
unsigned int num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, nullptr, nullptr);
|
unsigned int num_layers = hb_ot_color_glyph_get_layers (face, gid, 0, nullptr, nullptr);
|
||||||
if (!num_layers)
|
if (!num_layers) continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t));
|
hb_ot_color_layer_t *layers = (hb_ot_color_layer_t*) malloc (num_layers * sizeof (hb_ot_color_layer_t));
|
||||||
|
|
||||||
hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers);
|
hb_ot_color_glyph_get_layers (face, gid, 0, &num_layers, layers);
|
||||||
if (num_layers)
|
if (num_layers)
|
||||||
{
|
{
|
||||||
// Measure
|
hb_font_extents_t font_extents;
|
||||||
cairo_text_extents_t extents;
|
hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents);
|
||||||
|
hb_glyph_extents_t extents = {0};
|
||||||
|
if (!hb_font_get_glyph_extents (font, gid, &extents))
|
||||||
{
|
{
|
||||||
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
|
printf ("Skip gid: %d\n", gid);
|
||||||
cairo_t *cr = cairo_create (surface);
|
continue;
|
||||||
cairo_set_font_face (cr, cairo_face);
|
|
||||||
cairo_set_font_size (cr, upem);
|
|
||||||
|
|
||||||
cairo_glyph_t *glyphs = (cairo_glyph_t *) calloc (num_layers, sizeof (cairo_glyph_t));
|
|
||||||
for (unsigned int j = 0; j < num_layers; ++j)
|
|
||||||
glyphs[j].index = layers[j].glyph;
|
|
||||||
cairo_glyph_extents (cr, glyphs, num_layers, &extents);
|
|
||||||
free (glyphs);
|
|
||||||
cairo_surface_destroy (surface);
|
|
||||||
cairo_destroy (cr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a slight margin
|
|
||||||
extents.width += extents.width / 10;
|
|
||||||
extents.height += extents.height / 10;
|
|
||||||
extents.x_bearing -= extents.width / 20;
|
|
||||||
extents.y_bearing -= extents.height / 20;
|
|
||||||
|
|
||||||
// Render
|
|
||||||
unsigned int palette_count = hb_ot_color_palette_get_count (face);
|
unsigned int palette_count = hb_ot_color_palette_get_count (face);
|
||||||
for (unsigned int palette = 0; palette < palette_count; palette++)
|
for (unsigned int palette = 0; palette < palette_count; ++palette)
|
||||||
{
|
{
|
||||||
unsigned int num_colors = hb_ot_color_palette_get_colors (face, palette, 0, nullptr, nullptr);
|
unsigned int num_colors = hb_ot_color_palette_get_colors (face, palette, 0, nullptr, nullptr);
|
||||||
if (!num_colors)
|
if (!num_colors)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
char output_path[255];
|
||||||
|
sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index);
|
||||||
|
FILE *f = fopen (output_path, "wb");
|
||||||
|
fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\""
|
||||||
|
" viewBox=\"%d %d %d %d\">\n",
|
||||||
|
extents.x_bearing, 0,
|
||||||
|
extents.x_bearing + extents.width, -extents.height);
|
||||||
|
user_data_t user_data;
|
||||||
|
user_data.ascender = extents.y_bearing;
|
||||||
|
user_data.f = f;
|
||||||
|
|
||||||
hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t));
|
hb_color_t *colors = (hb_color_t*) calloc (num_colors, sizeof (hb_color_t));
|
||||||
hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors);
|
hb_ot_color_palette_get_colors (face, palette, 0, &num_colors, colors);
|
||||||
if (num_colors)
|
if (num_colors)
|
||||||
{
|
{
|
||||||
char output_path[255];
|
|
||||||
sprintf (output_path, "out/colr-%u-%u-%u.svg", gid, palette, face_index);
|
|
||||||
|
|
||||||
cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
|
|
||||||
cairo_t *cr = cairo_create (surface);
|
|
||||||
cairo_set_font_face (cr, cairo_face);
|
|
||||||
cairo_set_font_size (cr, upem);
|
|
||||||
|
|
||||||
for (unsigned int layer = 0; layer < num_layers; ++layer)
|
for (unsigned int layer = 0; layer < num_layers; ++layer)
|
||||||
{
|
{
|
||||||
hb_color_t color = 0x000000FF;
|
hb_color_t color = 0x000000FF;
|
||||||
if (layers[layer].color_index != 0xFFFF)
|
if (layers[layer].color_index != 0xFFFF)
|
||||||
color = colors[layers[layer].color_index];
|
color = colors[layers[layer].color_index];
|
||||||
cairo_set_source_rgba (cr,
|
fprintf (f, "<path fill=\"#%02X%02X%02X\" ",
|
||||||
hb_color_get_red (color) / 255.,
|
hb_color_get_red (color), hb_color_get_green (color), hb_color_get_green (color));
|
||||||
hb_color_get_green (color) / 255.,
|
if (hb_color_get_alpha (color) != 255)
|
||||||
hb_color_get_blue (color) / 255.,
|
fprintf (f, "fill-opacity=\"%.3f\"", (double) hb_color_get_alpha (color) / 255.);
|
||||||
hb_color_get_alpha (color) / 255.);
|
fprintf (f, "d=\"");
|
||||||
|
if (!hb_ot_glyph_decompose (font, layers[layer].glyph, funcs, &user_data))
|
||||||
cairo_glyph_t glyph;
|
printf ("Failed to decompose layer %d while %d\n", layers[layer].glyph, gid);
|
||||||
glyph.index = layers[layer].glyph;
|
fprintf (f, "\"/>\n");
|
||||||
glyph.x = -extents.x_bearing;
|
|
||||||
glyph.y = -extents.y_bearing;
|
|
||||||
cairo_show_glyphs (cr, &glyph, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cairo_surface_destroy (surface);
|
|
||||||
cairo_destroy (cr);
|
|
||||||
}
|
}
|
||||||
free (colors);
|
free (colors);
|
||||||
|
|
||||||
|
fprintf (f, "</svg>");
|
||||||
|
fclose (f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
free (layers);
|
free (layers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
dump_glyphs (cairo_font_face_t *cairo_face, unsigned int upem,
|
dump_glyphs (hb_font_t *font, hb_ot_glyph_decompose_funcs_t *funcs, unsigned int face_index)
|
||||||
unsigned int num_glyphs, unsigned int face_index)
|
|
||||||
{
|
{
|
||||||
for (unsigned int i = 0; i < num_glyphs; ++i)
|
unsigned num_glyphs = hb_face_get_glyph_count (hb_font_get_face (font));
|
||||||
|
for (unsigned int gid = 0; gid < num_glyphs; ++gid)
|
||||||
{
|
{
|
||||||
cairo_text_extents_t extents;
|
hb_font_extents_t font_extents;
|
||||||
cairo_glyph_t glyph = {0};
|
hb_font_get_extents_for_direction (font, HB_DIRECTION_LTR, &font_extents);
|
||||||
glyph.index = i;
|
hb_glyph_extents_t extents = {0};
|
||||||
|
if (!hb_font_get_glyph_extents (font, gid, &extents))
|
||||||
// Measure
|
|
||||||
{
|
{
|
||||||
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
|
printf ("Skip gid: %d\n", gid);
|
||||||
cairo_t *cr = cairo_create (surface);
|
continue;
|
||||||
cairo_set_font_face (cr, cairo_face);
|
|
||||||
cairo_set_font_size (cr, upem);
|
|
||||||
|
|
||||||
cairo_glyph_extents (cr, &glyph, 1, &extents);
|
|
||||||
cairo_surface_destroy (surface);
|
|
||||||
cairo_destroy (cr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a slight margin
|
char output_path[255];
|
||||||
extents.width += extents.width / 10;
|
sprintf (output_path, "out/%u-%u.svg", face_index, gid);
|
||||||
extents.height += extents.height / 10;
|
FILE *f = fopen (output_path, "wb");
|
||||||
extents.x_bearing -= extents.width / 20;
|
fprintf (f, "<svg xmlns=\"http://www.w3.org/2000/svg\""
|
||||||
extents.y_bearing -= extents.height / 20;
|
" viewBox=\"%d %d %d %d\"><path d=\"",
|
||||||
|
extents.x_bearing, 0,
|
||||||
// Render
|
extents.x_bearing + extents.width, font_extents.ascender - font_extents.descender);
|
||||||
{
|
user_data_t user_data;
|
||||||
char output_path[255];
|
user_data.ascender = font_extents.ascender;
|
||||||
sprintf (output_path, "out/%u-%u.svg", face_index, i);
|
user_data.f = f;
|
||||||
cairo_surface_t *surface = cairo_svg_surface_create (output_path, extents.width, extents.height);
|
if (!hb_ot_glyph_decompose (font, gid, funcs, &user_data))
|
||||||
cairo_t *cr = cairo_create (surface);
|
printf ("Failed to decompose gid: %d\n", gid);
|
||||||
cairo_set_font_face (cr, cairo_face);
|
fprintf (f, "\"/></svg>");
|
||||||
cairo_set_font_size (cr, upem);
|
fclose (f);
|
||||||
glyph.x = -extents.x_bearing;
|
|
||||||
glyph.y = -extents.y_bearing;
|
|
||||||
cairo_show_glyphs (cr, &glyph, 1);
|
|
||||||
cairo_surface_destroy (surface);
|
|
||||||
cairo_destroy (cr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -302,42 +306,37 @@ main (int argc, char **argv)
|
||||||
exit (1);
|
exit (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hb_ot_glyph_decompose_funcs_t *funcs = hb_ot_glyph_decompose_funcs_create ();
|
||||||
|
hb_ot_glyph_decompose_funcs_set_move_to_func (funcs, (hb_ot_glyph_decompose_move_to_func_t) move_to);
|
||||||
|
hb_ot_glyph_decompose_funcs_set_line_to_func (funcs, (hb_ot_glyph_decompose_line_to_func_t) line_to);
|
||||||
|
hb_ot_glyph_decompose_funcs_set_conic_to_func (funcs, (hb_ot_glyph_decompose_conic_to_func_t) conic_to);
|
||||||
|
hb_ot_glyph_decompose_funcs_set_cubic_to_func (funcs, (hb_ot_glyph_decompose_cubic_to_func_t) cubic_to);
|
||||||
|
hb_ot_glyph_decompose_funcs_set_close_path_func (funcs, (hb_ot_glyph_decompose_close_path_func_t) close_path);
|
||||||
|
|
||||||
for (unsigned int face_index = 0; face_index < hb_face_count (blob); face_index++)
|
for (unsigned int face_index = 0; face_index < hb_face_count (blob); face_index++)
|
||||||
{
|
{
|
||||||
hb_face_t *face = hb_face_create (blob, face_index);
|
hb_face_t *face = hb_face_create (blob, face_index);
|
||||||
hb_font_t *font = hb_font_create (face);
|
hb_font_t *font = hb_font_create (face);
|
||||||
|
|
||||||
if (hb_ot_color_has_png (face)) printf ("Dumping png (cbdt/sbix)...\n");
|
if (hb_ot_color_has_png (face))
|
||||||
|
printf ("Dumping png (CBDT/sbix)...\n");
|
||||||
png_dump (face, face_index);
|
png_dump (face, face_index);
|
||||||
|
|
||||||
if (hb_ot_color_has_svg (face)) printf ("Dumping svg...\n");
|
if (hb_ot_color_has_svg (face))
|
||||||
|
printf ("Dumping svg (SVG )...\n");
|
||||||
svg_dump (face, face_index);
|
svg_dump (face, face_index);
|
||||||
|
|
||||||
cairo_font_face_t *cairo_face;
|
|
||||||
{
|
|
||||||
FT_Library library;
|
|
||||||
FT_Init_FreeType (&library);
|
|
||||||
FT_Face ft_face;
|
|
||||||
FT_New_Face (library, argv[1], 0, &ft_face);
|
|
||||||
cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
|
|
||||||
}
|
|
||||||
if (hb_ot_color_has_layers (face) && hb_ot_color_has_palettes (face))
|
if (hb_ot_color_has_layers (face) && hb_ot_color_has_palettes (face))
|
||||||
printf ("Dumping layered color glyphs...\n");
|
printf ("Dumping layered color glyphs (COLR/CPAL)...\n");
|
||||||
layered_glyph_dump (face, cairo_face, face_index);
|
layered_glyph_dump (font, funcs, face_index);
|
||||||
|
|
||||||
unsigned int num_glyphs = hb_face_get_glyph_count (face);
|
dump_glyphs (font, funcs, face_index);
|
||||||
unsigned int upem = hb_face_get_upem (face);
|
|
||||||
|
|
||||||
// disabled when color font as cairo rendering of NotoColorEmoji is soooo slow
|
|
||||||
if (!hb_ot_color_has_layers (face) &&
|
|
||||||
!hb_ot_color_has_png (face) &&
|
|
||||||
!hb_ot_color_has_svg (face))
|
|
||||||
dump_glyphs (cairo_face, upem, num_glyphs, face_index);
|
|
||||||
|
|
||||||
hb_font_destroy (font);
|
hb_font_destroy (font);
|
||||||
hb_face_destroy (face);
|
hb_face_destroy (face);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hb_ot_glyph_decompose_funcs_destroy (funcs);
|
||||||
hb_blob_destroy (blob);
|
hb_blob_destroy (blob);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue