Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016 Google, Inc. |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 3 | * Copyright © 2018 Ebrahim Byagowi |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 4 | * |
| 5 | * This is part of HarfBuzz, a text shaping library. |
| 6 | * |
| 7 | * Permission is hereby granted, without written agreement and without |
| 8 | * license or royalty fees, to use, copy, modify, and distribute this |
| 9 | * software and its documentation for any purpose, provided that the |
| 10 | * above copyright notice and the following two paragraphs appear in |
| 11 | * all copies of this software. |
| 12 | * |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 14 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 15 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 16 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 17 | * DAMAGE. |
| 18 | * |
| 19 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 20 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 22 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 23 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 24 | * |
| 25 | * Google Author(s): Sascha Brawer |
| 26 | */ |
| 27 | |
| 28 | #include "hb-open-type-private.hh" |
Ebrahim Byagowi | c446c23 | 2018-03-03 22:43:23 +0330 | [diff] [blame] | 29 | #include "hb-ot-color-colr-table.hh" |
| 30 | #include "hb-ot-color-cpal-table.hh" |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 31 | #include "hb-ot.h" |
| 32 | |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include "hb-ot-layout-private.hh" |
| 37 | #include "hb-shaper-private.hh" |
| 38 | |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 39 | #if 0 |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 40 | HB_MARK_AS_FLAG_T (hb_ot_color_palette_flags_t) |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 41 | //HB_SHAPER_DATA_ENSURE_DECLARE(ot, face) Hmm? |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 42 | |
| 43 | |
Ebrahim Byagowi | f671f7f | 2018-02-28 13:19:40 +0330 | [diff] [blame] | 44 | static inline const OT::COLR& |
| 45 | _get_colr (hb_face_t *face) |
| 46 | { |
| 47 | if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::COLR); |
| 48 | hb_ot_layout_t * layout = hb_ot_layout_from_face (face); |
| 49 | return *(layout->colr.get ()); |
| 50 | } |
| 51 | |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 52 | static inline const OT::CPAL& |
| 53 | _get_cpal (hb_face_t *face) |
| 54 | { |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 55 | if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::CPAL); |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 56 | hb_ot_layout_t * layout = hb_ot_layout_from_face (face); |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 57 | return *(layout->cpal.get ()); |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * hb_ot_color_get_palette_count: |
| 63 | * @face: a font face. |
| 64 | * |
| 65 | * Returns: the number of color palettes in @face, or zero if @face has |
| 66 | * no colors. |
| 67 | * |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 68 | * Since: REPLACEME |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 69 | */ |
| 70 | unsigned int |
| 71 | hb_ot_color_get_palette_count (hb_face_t *face) |
| 72 | { |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 73 | const OT::CPAL& cpal = _get_cpal (face); |
| 74 | return cpal.get_palette_count (); |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * hb_ot_color_get_palette_name_id: |
| 80 | * @face: a font face. |
| 81 | * @palette: the index of the color palette whose name is being requested. |
| 82 | * |
| 83 | * Retrieves the name id of a color palette. For example, a color font can |
| 84 | * have themed palettes like "Spring", "Summer", "Fall", and "Winter". |
| 85 | * |
| 86 | * Returns: an identifier within @face's `name` table. |
| 87 | * If the requested palette has no name, or if @face has no colors, |
| 88 | * or if @palette is not between 0 and hb_ot_color_get_palette_count(), |
Sascha Brawer | 5967eab | 2016-06-24 16:42:27 +0200 | [diff] [blame] | 89 | * the result is 0xFFFF. The implementation does not check whether |
| 90 | * the returned palette name id is actually in @face's `name` table. |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 91 | * |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 92 | * Since: REPLACEME |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 93 | */ |
| 94 | unsigned int |
| 95 | hb_ot_color_get_palette_name_id (hb_face_t *face, unsigned int palette) |
| 96 | { |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 97 | const OT::CPAL& cpal = _get_cpal (face); |
| 98 | return cpal.get_palette_name_id (palette); |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * hb_ot_color_get_palette_flags: |
| 104 | * @face: a font face |
| 105 | * @palette: the index of the color palette whose flags are being requested |
| 106 | * |
| 107 | * Returns: the flags for the requested color palette. If @face has no colors, |
| 108 | * or if @palette is not between 0 and hb_ot_color_get_palette_count(), |
| 109 | * the result is #HB_OT_COLOR_PALETTE_FLAG_DEFAULT. |
| 110 | * |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 111 | * Since: REPLACEME |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 112 | */ |
| 113 | hb_ot_color_palette_flags_t |
| 114 | hb_ot_color_get_palette_flags (hb_face_t *face, unsigned int palette) |
| 115 | { |
| 116 | const OT::CPAL& cpal = _get_cpal(face); |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 117 | return cpal.get_palette_flags (palette); |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * hb_ot_color_get_palette_colors: |
| 123 | * @face: a font face. |
| 124 | * @palette: the index of the color palette whose colors |
| 125 | * are being requested. |
| 126 | * @start_offset: the index of the first color being requested. |
| 127 | * @color_count: (inout) (optional): on input, how many colors |
| 128 | * can be maximally stored into the @colors array; |
| 129 | * on output, how many colors were actually stored. |
Sascha Brawer | 5967eab | 2016-06-24 16:42:27 +0200 | [diff] [blame] | 130 | * @colors: (array length=color_count) (optional): |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 131 | * an array of #hb_ot_color_t records. After calling |
| 132 | * this function, @colors will be filled with |
| 133 | * the palette colors. If @colors is NULL, the function |
| 134 | * will just return the number of total colors |
| 135 | * without storing any actual colors; this can be used |
| 136 | * for allocating a buffer of suitable size before calling |
| 137 | * hb_ot_color_get_palette_colors() a second time. |
| 138 | * |
| 139 | * Retrieves the colors in a color palette. |
| 140 | * |
| 141 | * Returns: the total number of colors in the palette. All palettes in |
| 142 | * a font have the same number of colors. If @face has no colors, or if |
| 143 | * @palette is not between 0 and hb_ot_color_get_palette_count(), |
| 144 | * the result is zero. |
| 145 | * |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 146 | * Since: REPLACEME |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 147 | */ |
| 148 | unsigned int |
| 149 | hb_ot_color_get_palette_colors (hb_face_t *face, |
| 150 | unsigned int palette, /* default=0 */ |
| 151 | unsigned int start_offset, |
| 152 | unsigned int *color_count /* IN/OUT */, |
| 153 | hb_ot_color_t *colors /* OUT */) |
| 154 | { |
| 155 | const OT::CPAL& cpal = _get_cpal(face); |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 156 | if (unlikely (palette >= cpal.numPalettes)) |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 157 | { |
| 158 | if (color_count) *color_count = 0; |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | const OT::ColorRecord* crec = &cpal.offsetFirstColorRecord (&cpal); |
Sascha Brawer | d34d3ac | 2016-04-25 18:20:57 +0200 | [diff] [blame] | 163 | crec += cpal.colorRecordIndices[palette]; |
| 164 | |
| 165 | unsigned int num_results = 0; |
| 166 | if (likely (color_count && colors)) |
| 167 | { |
| 168 | for (unsigned int i = start_offset; |
| 169 | i < cpal.numPaletteEntries && num_results < *color_count; ++i) |
| 170 | { |
| 171 | hb_ot_color_t* result = &colors[num_results]; |
| 172 | result->red = crec[i].red; |
| 173 | result->green = crec[i].green; |
| 174 | result->blue = crec[i].blue; |
| 175 | result->alpha = crec[i].alpha; |
| 176 | ++num_results; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (likely (color_count)) *color_count = num_results; |
| 181 | return cpal.numPaletteEntries; |
| 182 | } |
Ebrahim Byagowi | 5b9c234 | 2018-02-27 22:56:17 +0330 | [diff] [blame] | 183 | #endif |