harfbuzz/src/hb-ot-color.cc

336 lines
8.7 KiB
C++
Raw Normal View History

2016-04-25 18:20:57 +02:00
/*
* Copyright © 2016 Google, Inc.
* Copyright © 2018 Ebrahim Byagowi
2016-04-25 18:20:57 +02:00
*
* 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.
*
2018-10-22 22:02:04 +02:00
* Google Author(s): Sascha Brawer, Behdad Esfahbod
2016-04-25 18:20:57 +02:00
*/
#include "hb-open-type.hh"
#include "hb-ot-color-cbdt-table.hh"
#include "hb-ot-color-colr-table.hh"
#include "hb-ot-color-cpal-table.hh"
#include "hb-ot-color-sbix-table.hh"
#include "hb-ot-color-svg-table.hh"
2018-05-01 17:16:46 +02:00
#include "hb-ot-face.hh"
2016-04-25 18:20:57 +02:00
#include "hb-ot.h"
#include <stdlib.h>
#include <string.h>
#include "hb-ot-layout.hh"
2016-04-25 18:20:57 +02:00
2018-10-27 13:58:32 +02:00
/**
* SECTION:hb-ot-color
* @title: hb-ot-color
* @short_description: OpenType Color Fonts
* @include: hb-ot.h
*
* Functions for fetching color-font information from OpenType font faces.
**/
2018-02-28 10:49:40 +01:00
static inline const OT::COLR&
_get_colr (hb_face_t *face)
{
2018-05-08 11:23:36 +02:00
if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::COLR);
2018-11-05 18:59:32 +01:00
return *(hb_ot_face_data (face)->COLR);
2018-02-28 10:49:40 +01:00
}
static inline const OT::CBDT_accelerator_t&
_get_cbdt (hb_face_t *face)
{
if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::CBDT_accelerator_t);
2018-11-05 18:59:32 +01:00
return *(hb_ot_face_data (face)->CBDT);
}
2016-04-25 18:20:57 +02:00
static inline const OT::CPAL&
_get_cpal (hb_face_t *face)
{
2018-05-08 11:23:36 +02:00
if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::CPAL);
2018-11-05 18:59:32 +01:00
return *(hb_ot_face_data (face)->CPAL);
2016-04-25 18:20:57 +02:00
}
2018-10-26 22:25:11 +02:00
static inline const OT::sbix_accelerator_t&
_get_sbix (hb_face_t *face)
{
2018-10-26 22:25:11 +02:00
if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::sbix_accelerator_t);
2018-11-05 18:59:32 +01:00
return *(hb_ot_face_data (face)->sbix);
}
static inline const OT::SVG_accelerator_t&
_get_svg (hb_face_t *face)
{
if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return Null(OT::SVG_accelerator_t);
2018-11-05 18:59:32 +01:00
return *(hb_ot_face_data (face)->SVG);
}
2018-10-22 23:43:12 +02:00
/*
* CPAL
2016-04-25 18:20:57 +02:00
*/
2016-04-25 18:20:57 +02:00
/**
* hb_ot_color_has_palettes:
2016-04-25 18:20:57 +02:00
* @face: a font face.
*
* Returns: whether CPAL table is available.
2016-04-25 18:20:57 +02:00
*
2018-10-27 11:35:06 +02:00
* Since: 2.1.0
2016-04-25 18:20:57 +02:00
*/
hb_bool_t
hb_ot_color_has_palettes (hb_face_t *face)
2016-04-25 18:20:57 +02:00
{
return _get_cpal (face).has_data ();
2016-04-25 18:20:57 +02:00
}
/**
2018-10-22 22:02:04 +02:00
* hb_ot_color_palette_get_count:
* @face: a font face.
2016-04-25 18:20:57 +02:00
*
2018-10-21 13:29:40 +02:00
* Returns: the number of color palettes in @face, or zero if @face has
* no colors.
2016-04-25 18:20:57 +02:00
*
2018-10-27 11:35:06 +02:00
* Since: 2.1.0
2016-04-25 18:20:57 +02:00
*/
unsigned int
2018-10-22 22:02:04 +02:00
hb_ot_color_palette_get_count (hb_face_t *face)
2016-04-25 18:20:57 +02:00
{
return _get_cpal (face).get_palette_count ();
2016-04-25 18:20:57 +02:00
}
/**
2018-10-22 22:02:04 +02:00
* hb_ot_color_palette_get_name_id:
2018-10-21 10:16:51 +02:00
* @face: a font face.
2018-10-27 07:09:17 +02:00
* @palette_index: the index of the color palette whose name is being requested.
*
* Retrieves the name id of a color palette. For example, a color font can
* have themed palettes like "Spring", "Summer", "Fall", and "Winter".
*
* Returns: an identifier within @face's `name` table.
* If the requested palette has no name the result is #HB_OT_NAME_ID_INVALID.
*
2018-10-27 11:35:06 +02:00
* Since: 2.1.0
*/
hb_ot_name_id_t
2018-10-22 22:02:04 +02:00
hb_ot_color_palette_get_name_id (hb_face_t *face,
unsigned int palette_index)
{
2018-10-22 22:02:04 +02:00
return _get_cpal (face).get_palette_name_id (palette_index);
}
/**
2018-10-22 22:02:04 +02:00
* hb_ot_color_palette_color_get_name_id:
2018-10-27 12:45:38 +02:00
* @face: a font face.
* @color_index: palette entry index.
*
2018-10-21 10:16:51 +02:00
* Returns: Name ID associated with a palette entry, e.g. eye color
*
2018-10-27 11:35:06 +02:00
* Since: 2.1.0
*/
hb_ot_name_id_t
2018-10-22 22:02:04 +02:00
hb_ot_color_palette_color_get_name_id (hb_face_t *face,
unsigned int color_index)
{
2018-10-22 23:51:40 +02:00
return _get_cpal (face).get_color_name_id (color_index);
}
2016-04-25 18:20:57 +02:00
/**
2018-10-22 22:02:04 +02:00
* hb_ot_color_palette_get_flags:
2018-10-27 12:45:38 +02:00
* @face: a font face
2018-10-22 22:02:04 +02:00
* @palette_index: the index of the color palette whose flags are being requested
*
* Returns: the flags for the requested color palette.
*
2018-10-27 11:35:06 +02:00
* Since: 2.1.0
2018-10-22 22:02:04 +02:00
*/
hb_ot_color_palette_flags_t
hb_ot_color_palette_get_flags (hb_face_t *face,
unsigned int palette_index)
{
2018-10-23 01:35:03 +02:00
return _get_cpal(face).get_palette_flags (palette_index);
2018-10-22 22:02:04 +02:00
}
/**
* hb_ot_color_palette_get_colors:
2016-04-25 18:20:57 +02:00
* @face: a font face.
2018-10-22 22:02:04 +02:00
* @palette_index:the index of the color palette whose colors
2016-04-25 18:20:57 +02:00
* are being requested.
* @start_offset: the index of the first color being requested.
* @color_count: (inout) (optional): on input, how many colors
* can be maximally stored into the @colors array;
* on output, how many colors were actually stored.
2018-05-01 17:16:46 +02:00
* @colors: (array length=color_count) (out) (optional):
* an array of #hb_color_t records. After calling
2016-04-25 18:20:57 +02:00
* this function, @colors will be filled with
* the palette colors. If @colors is NULL, the function
* will just return the number of total colors
* without storing any actual colors; this can be used
* for allocating a buffer of suitable size before calling
2018-10-22 22:02:04 +02:00
* hb_ot_color_palette_get_colors() a second time.
2016-04-25 18:20:57 +02:00
*
* Retrieves the colors in a color palette.
*
2018-10-22 22:02:04 +02:00
* Returns: the total number of colors in the palette.
2016-04-25 18:20:57 +02:00
*
2018-10-27 11:35:06 +02:00
* Since: 2.1.0
2016-04-25 18:20:57 +02:00
*/
unsigned int
hb_ot_color_palette_get_colors (hb_face_t *face,
unsigned int palette_index,
unsigned int start_offset,
unsigned int *colors_count /* IN/OUT. May be NULL. */,
hb_color_t *colors /* OUT. May be NULL. */)
2016-04-25 18:20:57 +02:00
{
return _get_cpal (face).get_palette_colors (palette_index, start_offset, colors_count, colors);
2018-05-01 17:16:46 +02:00
}
/*
* COLR
*/
/**
* hb_ot_color_has_layers:
* @face: a font face.
*
* Returns: whether COLR table is available.
*
2018-10-27 11:35:06 +02:00
* Since: 2.1.0
*/
hb_bool_t
hb_ot_color_has_layers (hb_face_t *face)
{
return _get_colr (face).has_data ();
}
/**
* hb_ot_color_glyph_get_layers:
2018-10-27 12:45:38 +02:00
* @face: a font face.
* @glyph: a layered color glyph id.
* @start_offset: starting offset of layers.
* @count: (inout) (optional): gets number of layers available to be written on buffer
* and returns number of written layers.
* @layers: (array length=count) (out) (optional): layers buffer to buffer.
*
2018-10-27 12:45:38 +02:00
* Returns: Total number of layers a layered color glyph have.
*
2018-10-27 11:35:06 +02:00
* Since: 2.1.0
*/
2018-05-01 17:16:46 +02:00
unsigned int
hb_ot_color_glyph_get_layers (hb_face_t *face,
hb_codepoint_t glyph,
unsigned int start_offset,
unsigned int *count, /* IN/OUT. May be NULL. */
hb_ot_color_layer_t *layers /* OUT. May be NULL. */)
2018-05-01 17:16:46 +02:00
{
2018-10-23 01:35:03 +02:00
return _get_colr (face).get_glyph_layers (glyph, start_offset, count, layers);
2016-04-25 18:20:57 +02:00
}
/*
* SVG
*/
/**
* hb_ot_color_has_svg:
* @face: a font face.
*
2018-10-30 23:52:26 +01:00
* Check whether @face has SVG glyph images.
*
2018-10-30 23:52:26 +01:00
* Returns true if available, false otherwise.
*
* Since: 2.1.0
*/
hb_bool_t
hb_ot_color_has_svg (hb_face_t *face)
{
return _get_svg (face).has_data ();
}
/**
2018-10-28 19:08:56 +01:00
* hb_ot_color_glyph_reference_svg:
2018-10-27 12:45:38 +02:00
* @face: a font face.
* @glyph: a svg glyph index.
*
2018-10-30 23:52:26 +01:00
* Get SVG document for a glyph.
*
2018-10-28 17:39:40 +01:00
* Returns: (transfer full): respective svg blob of the glyph, if available.
*
2018-10-30 23:52:26 +01:00
* Since: 2.1.0
*/
hb_blob_t *
2018-10-28 19:08:56 +01:00
hb_ot_color_glyph_reference_svg (hb_face_t *face, hb_codepoint_t glyph)
{
return _get_svg (face).reference_blob_for_glyph (glyph);
}
2018-10-26 22:25:11 +02:00
/*
2018-10-27 12:02:14 +02:00
* PNG: CBDT or sbix
2018-10-26 22:25:11 +02:00
*/
/**
* hb_ot_color_has_png:
* @face: a font face.
*
2018-10-30 23:52:26 +01:00
* Check whether @face has PNG glyph images (either CBDT or sbix tables).
2018-10-26 22:25:11 +02:00
*
2018-10-30 23:52:26 +01:00
* Returns true if available, false otherwise.
*
* Since: 2.1.0
2018-10-26 22:25:11 +02:00
*/
hb_bool_t
hb_ot_color_has_png (hb_face_t *face)
{
return _get_cbdt (face).has_data () || _get_sbix (face).has_data ();
}
/**
2018-10-28 19:08:56 +01:00
* hb_ot_color_glyph_reference_png:
2018-10-27 12:45:38 +02:00
* @font: a font object, not face. upem should be set on
* that font object if one wants to get optimal png blob, otherwise
* return the biggest one
* @glyph: a glyph index.
2018-10-26 22:25:11 +02:00
*
2018-10-30 23:52:26 +01:00
* Get PNG image for a glyph.
2018-10-26 22:25:11 +02:00
*
2018-10-30 23:52:26 +01:00
* Returns: (transfer full): respective PNG blob of the glyph, if available.
*
* Since: 2.1.0
2018-10-26 22:25:11 +02:00
*/
hb_blob_t *
2018-10-28 19:08:56 +01:00
hb_ot_color_glyph_reference_png (hb_font_t *font, hb_codepoint_t glyph)
2018-10-26 22:25:11 +02:00
{
hb_blob_t *blob = hb_blob_get_empty ();
2018-10-26 22:25:11 +02:00
if (_get_sbix (font->face).has_data ())
blob = _get_sbix (font->face).reference_png (font, glyph, nullptr, nullptr, nullptr);
2018-10-26 22:25:11 +02:00
2018-10-29 00:32:20 +01:00
if (!blob->length && _get_cbdt (font->face).has_data ())
2018-10-29 07:01:57 +01:00
blob = _get_cbdt (font->face).reference_png (font, glyph);
return blob;
2018-10-26 22:25:11 +02:00
}