/* * Copyright © 2009 Red Hat, Inc. * Copyright © 2009 Keith Stribley * Copyright © 2015 Google, Inc. * * 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. * * Red Hat Author(s): Behdad Esfahbod * Google Author(s): Behdad Esfahbod */ #include "hb.hh" #ifdef HAVE_FREETYPE #include "hb-ft.h" #include "hb-cache.hh" #include "hb-draw.hh" #include "hb-font.hh" #include "hb-machinery.hh" #include "hb-ot-os2-table.hh" #include "hb-ot-shaper-arabic-pua.hh" #include "hb-paint.hh" #include FT_ADVANCES_H #include FT_MULTIPLE_MASTERS_H #include FT_OUTLINE_H #include FT_TRUETYPE_TABLES_H #include FT_SYNTHESIS_H #if (FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH) >= 21300 #include FT_COLOR_H #endif /** * SECTION:hb-ft * @title: hb-ft * @short_description: FreeType integration * @include: hb-ft.h * * Functions for using HarfBuzz with the FreeType library. * * HarfBuzz supports using FreeType to provide face and * font data. * * <note>Note that FreeType is not thread-safe, therefore these * functions are not thread-safe either.</note> **/ /* TODO: * * In general, this file does a fine job of what it's supposed to do. * There are, however, things that need more work: * * - FreeType works in 26.6 mode. Clients can decide to use that mode, and everything * would work fine. However, we also abuse this API for performing in font-space, * but don't pass the correct flags to FreeType. We just abuse the no-hinting mode * for that, such that no rounding etc happens. As such, we don't set ppem, and * pass NO_HINTING as load_flags. Would be much better to use NO_SCALE, and scale * ourselves. * * - We don't handle / allow for emboldening / obliqueing. * * - In the future, we should add constructors to create fonts in font space? */ hb_ft_advance_cache_t; struct hb_ft_font_t { … }; static hb_ft_font_t * _hb_ft_font_create (FT_Face ft_face, bool symbol, bool unref) { … } static void _hb_ft_face_destroy (void *data) { … } static void _hb_ft_font_destroy (void *data) { … } /* hb_font changed, update FT_Face. */ static void _hb_ft_hb_font_changed (hb_font_t *font, FT_Face ft_face) { … } /* Check if hb_font changed, update FT_Face. */ static inline bool _hb_ft_hb_font_check_changed (hb_font_t *font, const hb_ft_font_t *ft_font) { … } /** * hb_ft_font_set_load_flags: * @font: #hb_font_t to work upon * @load_flags: The FreeType load flags to set * * Sets the FT_Load_Glyph load flags for the specified #hb_font_t. * * For more information, see * <https://freetype.org/freetype2/docs/reference/ft2-glyph_retrieval.html#ft_load_xxx> * * This function works with #hb_font_t objects created by * hb_ft_font_create() or hb_ft_font_create_referenced(). * * Since: 1.0.5 **/ void hb_ft_font_set_load_flags (hb_font_t *font, int load_flags) { … } /** * hb_ft_font_get_load_flags: * @font: #hb_font_t to work upon * * Fetches the FT_Load_Glyph load flags of the specified #hb_font_t. * * For more information, see * <https://freetype.org/freetype2/docs/reference/ft2-glyph_retrieval.html#ft_load_xxx> * * This function works with #hb_font_t objects created by * hb_ft_font_create() or hb_ft_font_create_referenced(). * * Return value: FT_Load_Glyph flags found, or 0 * * Since: 1.0.5 **/ int hb_ft_font_get_load_flags (hb_font_t *font) { … } /** * hb_ft_font_get_face: (skip) * @font: #hb_font_t to work upon * * Fetches the FT_Face associated with the specified #hb_font_t * font object. * * This function works with #hb_font_t objects created by * hb_ft_font_create() or hb_ft_font_create_referenced(). * * Return value: (nullable): the FT_Face found or `NULL` * * Since: 0.9.2 **/ FT_Face hb_ft_font_get_face (hb_font_t *font) { … } /** * hb_ft_font_lock_face: (skip) * @font: #hb_font_t to work upon * * Gets the FT_Face associated with @font. * * This face will be kept around and access to the FT_Face object * from other HarfBuzz API wil be blocked until you call hb_ft_font_unlock_face(). * * This function works with #hb_font_t objects created by * hb_ft_font_create() or hb_ft_font_create_referenced(). * * Return value: (nullable) (transfer none): the FT_Face associated with @font or `NULL` * Since: 2.6.5 **/ FT_Face hb_ft_font_lock_face (hb_font_t *font) { … } /** * hb_ft_font_unlock_face: (skip) * @font: #hb_font_t to work upon * * Releases an FT_Face previously obtained with hb_ft_font_lock_face(). * * Since: 2.6.5 **/ void hb_ft_font_unlock_face (hb_font_t *font) { … } static hb_bool_t hb_ft_get_nominal_glyph (hb_font_t *font, void *font_data, hb_codepoint_t unicode, hb_codepoint_t *glyph, void *user_data HB_UNUSED) { … } static unsigned int hb_ft_get_nominal_glyphs (hb_font_t *font HB_UNUSED, void *font_data, unsigned int count, const hb_codepoint_t *first_unicode, unsigned int unicode_stride, hb_codepoint_t *first_glyph, unsigned int glyph_stride, void *user_data HB_UNUSED) { … } static hb_bool_t hb_ft_get_variation_glyph (hb_font_t *font HB_UNUSED, void *font_data, hb_codepoint_t unicode, hb_codepoint_t variation_selector, hb_codepoint_t *glyph, void *user_data HB_UNUSED) { … } static void hb_ft_get_glyph_h_advances (hb_font_t* font, void* font_data, unsigned count, const hb_codepoint_t *first_glyph, unsigned glyph_stride, hb_position_t *first_advance, unsigned advance_stride, void *user_data HB_UNUSED) { … } #ifndef HB_NO_VERTICAL static hb_position_t hb_ft_get_glyph_v_advance (hb_font_t *font, void *font_data, hb_codepoint_t glyph, void *user_data HB_UNUSED) { … } #endif #ifndef HB_NO_VERTICAL static hb_bool_t hb_ft_get_glyph_v_origin (hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_position_t *x, hb_position_t *y, void *user_data HB_UNUSED) { … } #endif #ifndef HB_NO_OT_SHAPE_FALLBACK static hb_position_t hb_ft_get_glyph_h_kerning (hb_font_t *font, void *font_data, hb_codepoint_t left_glyph, hb_codepoint_t right_glyph, void *user_data HB_UNUSED) { … } #endif static hb_bool_t hb_ft_get_glyph_extents (hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_glyph_extents_t *extents, void *user_data HB_UNUSED) { … } static hb_bool_t hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED, void *font_data, hb_codepoint_t glyph, unsigned int point_index, hb_position_t *x, hb_position_t *y, void *user_data HB_UNUSED) { … } static hb_bool_t hb_ft_get_glyph_name (hb_font_t *font HB_UNUSED, void *font_data, hb_codepoint_t glyph, char *name, unsigned int size, void *user_data HB_UNUSED) { … } static hb_bool_t hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED, void *font_data, const char *name, int len, /* -1 means nul-terminated */ hb_codepoint_t *glyph, void *user_data HB_UNUSED) { … } static hb_bool_t hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED, void *font_data, hb_font_extents_t *metrics, void *user_data HB_UNUSED) { … } #ifndef HB_NO_DRAW static int _hb_ft_move_to (const FT_Vector *to, void *arg) { … } static int _hb_ft_line_to (const FT_Vector *to, void *arg) { … } static int _hb_ft_conic_to (const FT_Vector *control, const FT_Vector *to, void *arg) { … } static int _hb_ft_cubic_to (const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *arg) { … } static void hb_ft_draw_glyph (hb_font_t *font, void *font_data, hb_codepoint_t glyph, hb_draw_funcs_t *draw_funcs, void *draw_data, void *user_data HB_UNUSED) { … } #endif #ifndef HB_NO_PAINT #if (FREETYPE_MAJOR*10000 + FREETYPE_MINOR*100 + FREETYPE_PATCH) >= 21300 #include "hb-ft-colr.hh" static void hb_ft_paint_glyph (hb_font_t *font, void *font_data, hb_codepoint_t gid, hb_paint_funcs_t *paint_funcs, void *paint_data, unsigned int palette_index, hb_color_t foreground, void *user_data) { … } #endif #endif static inline void free_static_ft_funcs (); static struct hb_ft_font_funcs_lazy_loader_t : hb_font_funcs_lazy_loader_t<hb_ft_font_funcs_lazy_loader_t> { … } static_ft_funcs; static inline void free_static_ft_funcs () { … } static hb_font_funcs_t * _hb_ft_get_font_funcs () { … } static void _hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref) { … } static hb_blob_t * _hb_ft_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) { … } static unsigned _hb_ft_get_table_tags (const hb_face_t *face HB_UNUSED, unsigned int start_offset, unsigned int *table_count, hb_tag_t *table_tags, void *user_data) { … } /** * hb_ft_face_create: * @ft_face: (destroy destroy) (scope notified): FT_Face to work upon * @destroy: (nullable): A callback to call when the face object is not needed anymore * * Creates an #hb_face_t face object from the specified FT_Face. * * Note that this is using the FT_Face object just to get at the underlying * font data, and fonts created from the returned #hb_face_t will use the native * HarfBuzz font implementation, unless you call hb_ft_font_set_funcs() on them. * * This variant of the function does not provide any life-cycle management. * * Most client programs should use hb_ft_face_create_referenced() * (or, perhaps, hb_ft_face_create_cached()) instead. * * If you know you have valid reasons not to use hb_ft_face_create_referenced(), * then it is the client program's responsibility to destroy @ft_face * after the #hb_face_t face object has been destroyed. * * Return value: (transfer full): the new #hb_face_t face object * * Since: 0.9.2 **/ hb_face_t * hb_ft_face_create (FT_Face ft_face, hb_destroy_func_t destroy) { … } /** * hb_ft_face_create_referenced: * @ft_face: FT_Face to work upon * * Creates an #hb_face_t face object from the specified FT_Face. * * Note that this is using the FT_Face object just to get at the underlying * font data, and fonts created from the returned #hb_face_t will use the native * HarfBuzz font implementation, unless you call hb_ft_font_set_funcs() on them. * * This is the preferred variant of the hb_ft_face_create* * function family, because it calls FT_Reference_Face() on @ft_face, * ensuring that @ft_face remains alive as long as the resulting * #hb_face_t face object remains alive. Also calls FT_Done_Face() * when the #hb_face_t face object is destroyed. * * Use this version unless you know you have good reasons not to. * * Return value: (transfer full): the new #hb_face_t face object * * Since: 0.9.38 **/ hb_face_t * hb_ft_face_create_referenced (FT_Face ft_face) { … } static void hb_ft_face_finalize (void *arg) { … } /** * hb_ft_face_create_cached: * @ft_face: FT_Face to work upon * * Creates an #hb_face_t face object from the specified FT_Face. * * Note that this is using the FT_Face object just to get at the underlying * font data, and fonts created from the returned #hb_face_t will use the native * HarfBuzz font implementation, unless you call hb_ft_font_set_funcs() on them. * * This variant of the function caches the newly created #hb_face_t * face object, using the @generic pointer of @ft_face. Subsequent function * calls that are passed the same @ft_face parameter will have the same * #hb_face_t returned to them, and that #hb_face_t will be correctly * reference counted. * * However, client programs are still responsible for destroying * @ft_face after the last #hb_face_t face object has been destroyed. * * Return value: (transfer full): the new #hb_face_t face object * * Since: 0.9.2 **/ hb_face_t * hb_ft_face_create_cached (FT_Face ft_face) { … } /** * hb_ft_font_create: * @ft_face: (destroy destroy) (scope notified): FT_Face to work upon * @destroy: (nullable): A callback to call when the font object is not needed anymore * * Creates an #hb_font_t font object from the specified FT_Face. * * <note>Note: You must set the face size on @ft_face before calling * hb_ft_font_create() on it. HarfBuzz assumes size is always set and will * access `size` member of FT_Face unconditionally.</note> * * This variant of the function does not provide any life-cycle management. * * Most client programs should use hb_ft_font_create_referenced() * instead. * * If you know you have valid reasons not to use hb_ft_font_create_referenced(), * then it is the client program's responsibility to destroy @ft_face * after the #hb_font_t font object has been destroyed. * * HarfBuzz will use the @destroy callback on the #hb_font_t font object * if it is supplied when you use this function. However, even if @destroy * is provided, it is the client program's responsibility to destroy @ft_face, * and it is the client program's responsibility to ensure that @ft_face is * destroyed only after the #hb_font_t font object has been destroyed. * * Return value: (transfer full): the new #hb_font_t font object * * Since: 0.9.2 **/ hb_font_t * hb_ft_font_create (FT_Face ft_face, hb_destroy_func_t destroy) { … } /** * hb_ft_font_changed: * @font: #hb_font_t to work upon * * Refreshes the state of @font when the underlying FT_Face has changed. * This function should be called after changing the size or * variation-axis settings on the FT_Face. * * Since: 1.0.5 **/ void hb_ft_font_changed (hb_font_t *font) { … } /** * hb_ft_hb_font_changed: * @font: #hb_font_t to work upon * * Refreshes the state of the underlying FT_Face of @font when the hb_font_t * @font has changed. * This function should be called after changing the size or * variation-axis settings on the @font. * This call is fast if nothing has changed on @font. * * Return value: true if changed, false otherwise * * Since: 4.4.0 **/ hb_bool_t hb_ft_hb_font_changed (hb_font_t *font) { … } /** * hb_ft_font_create_referenced: * @ft_face: FT_Face to work upon * * Creates an #hb_font_t font object from the specified FT_Face. * * <note>Note: You must set the face size on @ft_face before calling * hb_ft_font_create_referenced() on it. HarfBuzz assumes size is always set * and will access `size` member of FT_Face unconditionally.</note> * * This is the preferred variant of the hb_ft_font_create* * function family, because it calls FT_Reference_Face() on @ft_face, * ensuring that @ft_face remains alive as long as the resulting * #hb_font_t font object remains alive. * * Use this version unless you know you have good reasons not to. * * Return value: (transfer full): the new #hb_font_t font object * * Since: 0.9.38 **/ hb_font_t * hb_ft_font_create_referenced (FT_Face ft_face) { … } static inline void free_static_ft_library (); static struct hb_ft_library_lazy_loader_t : hb_lazy_loader_t<hb_remove_pointer<FT_Library>, hb_ft_library_lazy_loader_t> { … } static_ft_library; static inline void free_static_ft_library () { … } static FT_Library get_ft_library () { … } static void _release_blob (void *arg) { … } /** * hb_ft_font_set_funcs: * @font: #hb_font_t to work upon * * Configures the font-functions structure of the specified * #hb_font_t font object to use FreeType font functions. * * In particular, you can use this function to configure an * existing #hb_face_t face object for use with FreeType font * functions even if that #hb_face_t face object was initially * created with hb_face_create(), and therefore was not * initially configured to use FreeType font functions. * * An #hb_font_t object created with hb_ft_font_create() * is preconfigured for FreeType font functions and does not * require this function to be used. * * Note that if you modify the underlying #hb_font_t after * calling this function, you need to call hb_ft_hb_font_changed() * to update the underlying FT_Face. * * <note>Note: Internally, this function creates an FT_Face. * </note> * * Since: 1.0.5 **/ void hb_ft_font_set_funcs (hb_font_t *font) { … } #endif