freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Git][freetype/freetype][wl/colr] Improve handling of color palettes.


From: Werner Lemberg (@wl)
Subject: [Git][freetype/freetype][wl/colr] Improve handling of color palettes.
Date: Mon, 07 Mar 2022 16:39:29 +0000

Werner Lemberg pushed to branch wl/colr at FreeType / FreeType

Commits:

3 changed files:

Changes:

  • include/freetype/ftcolor.h
    ... ... @@ -45,6 +45,12 @@ FT_BEGIN_HEADER
    45 45
        * @description:
    
    46 46
        *   The functions described here allow access and manipulation of color
    
    47 47
        *   palette entries in OpenType's 'CPAL' tables.
    
    48
    +   *
    
    49
    +   *   FreeType maintains a 'working palette' (together with a corresponding
    
    50
    +   *   'working palette index'), which can be set either to a palette stored
    
    51
    +   *   in the font (function @FT_Palette_Select) or to a user-defined palette
    
    52
    +   *   (function @FT_Palette_Set).  For user-defined palettes, the working
    
    53
    +   *   palette index is negative, and positive otherwise.
    
    48 54
        */
    
    49 55
     
    
    50 56
     
    
    ... ... @@ -218,18 +224,13 @@ FT_BEGIN_HEADER
    218 224
        *   FT_Palette_Select
    
    219 225
        *
    
    220 226
        * @description:
    
    221
    -   *   This function has two purposes.
    
    222
    -   *
    
    223
    -   *   (1) It activates a palette for rendering color glyphs, and
    
    224
    -   *
    
    225
    -   *   (2) it retrieves all (unmodified) color entries of this palette.  This
    
    226
    -   *       function returns a read-write array, which means that a calling
    
    227
    -   *       application can modify the palette entries on demand.
    
    227
    +   *   This function copies a palette entry in the font's 'CPAL' table into a
    
    228
    +   *   'working palette', which is a read-write array managed by FreeType.
    
    229
    +   *   It also sets the 'working palette index' to the index value given as
    
    230
    +   *   an argument.
    
    228 231
        *
    
    229
    -   * A corollary of (2) is that calling the function, then modifying some
    
    230
    -   * values, then calling the function again with the same arguments resets
    
    231
    -   * all color entries to the original 'CPAL' values; all user modifications
    
    232
    -   * are lost.
    
    232
    +   *   The current working palette and palette index can be retrieved with
    
    233
    +   *   @FT_Palette_Get.
    
    233 234
        *
    
    234 235
        * @input:
    
    235 236
        *   face ::
    
    ... ... @@ -240,10 +241,10 @@ FT_BEGIN_HEADER
    240 241
        *
    
    241 242
        * @output:
    
    242 243
        *   apalette ::
    
    243
    -   *     An array of color entries for a palette with index `palette_index`,
    
    244
    -   *     having `num_palette_entries` elements (as found in the
    
    245
    -   *     `FT_Palette_Data` structure).  If `apalette` is set to `NULL`, no
    
    246
    -   *     array gets returned (and no color entries can be modified).
    
    244
    +   *     A pointer to the 'working palette', which is an array of color
    
    245
    +   *     entries for a palette with index `palette_index`, having
    
    246
    +   *     `num_palette_entries` elements (as found in the `FT_Palette_Data`
    
    247
    +   *     structure).  If `apalette` is set to `NULL`, no array gets returned.
    
    247 248
        *
    
    248 249
        *     In case the font doesn't support color palettes, `NULL` is returned.
    
    249 250
        *
    
    ... ... @@ -266,6 +267,92 @@ FT_BEGIN_HEADER
    266 267
                          FT_Color*  *apalette );
    
    267 268
     
    
    268 269
     
    
    270
    +  /**************************************************************************
    
    271
    +   *
    
    272
    +   * @function:
    
    273
    +   *   FT_Palette_Set
    
    274
    +   *
    
    275
    +   * @description:
    
    276
    +   *   Set FreeType's 'working palette' and 'working palette index' to the
    
    277
    +   *   given arguments.  Use this function if you want to provide a
    
    278
    +   *   user-defined palette, not being part of the font's 'CPAL' table.
    
    279
    +   *
    
    280
    +   *   The current working palette and palette index can be retrieved with
    
    281
    +   *   @FT_Palette_Get.
    
    282
    +   *
    
    283
    +   * @input:
    
    284
    +   *   face ::
    
    285
    +   *     The source face handle.
    
    286
    +   *
    
    287
    +   *   palette_index ::
    
    288
    +   *     A palette index, which must be an arbitrary, negative integer (since
    
    289
    +   *     positive values are reserved for indices from the 'CPAL' table).
    
    290
    +   *     FreeType sets the 'working palette index' to this value.
    
    291
    +   *
    
    292
    +   *   palette ::
    
    293
    +   *     A pointer to an array of color entries, having `num_palette_entries`
    
    294
    +   *     elements (as found in the `FT_Palette_Data` structure).  FreeType
    
    295
    +   *     copies this array into its 'working palette'.
    
    296
    +   *
    
    297
    +   * @return:
    
    298
    +   *   FreeType error code.  0~means success.
    
    299
    +   *
    
    300
    +   * @note:
    
    301
    +   *   This function always returns an error if the config macro
    
    302
    +   *   `TT_CONFIG_OPTION_COLOR_LAYERS` is not defined in `ftoption.h`.
    
    303
    +   *
    
    304
    +   * @since:
    
    305
    +   *   2.12
    
    306
    +   */
    
    307
    +  FT_EXPORT( FT_Error )
    
    308
    +  FT_Palette_Set( FT_Face    face,
    
    309
    +                  FT_Int     index,
    
    310
    +                  FT_Color*  palette );
    
    311
    +
    
    312
    +
    
    313
    +  /**************************************************************************
    
    314
    +   *
    
    315
    +   * @function:
    
    316
    +   *   FT_Palette_Get
    
    317
    +   *
    
    318
    +   * @description:
    
    319
    +   *   Get FreeType's 'working palette' and 'working palette index'.
    
    320
    +   *
    
    321
    +   *   The working palette and palette index can be set with @FT_Palette_Set.
    
    322
    +   *
    
    323
    +   * @input:
    
    324
    +   *   face ::
    
    325
    +   *     The source face handle.
    
    326
    +   *
    
    327
    +   * @output:
    
    328
    +   *   anindex ::
    
    329
    +   *     The 'working palette index'.  If the value is zero or positive, the
    
    330
    +   *     working palette represents an entry from the font's 'CPAL' table
    
    331
    +   *     (with the given index).  Otherwise it is a user-defined palette.  If
    
    332
    +   *     `anindex` is set to `NULL`, no value gets returned.
    
    333
    +   *
    
    334
    +   *   apalette ::
    
    335
    +   *     A pointer to the 'working palette', which is an array of color
    
    336
    +   *     entries for a palette having `num_palette_entries` elements (as
    
    337
    +   *     found in the `FT_Palette_Data` structure).  If `apalette` is set to
    
    338
    +   *     `NULL`, no array gets returned.
    
    339
    +   *
    
    340
    +   * @return:
    
    341
    +   *   FreeType error code.  0~means success.
    
    342
    +   *
    
    343
    +   * @note:
    
    344
    +   *   This function always returns an error if the config macro
    
    345
    +   *   `TT_CONFIG_OPTION_COLOR_LAYERS` is not defined in `ftoption.h`.
    
    346
    +   *
    
    347
    +   * @since:
    
    348
    +   *   2.12
    
    349
    +   */
    
    350
    +  FT_EXPORT( FT_Error )
    
    351
    +  FT_Palette_Get( FT_Face     face,
    
    352
    +                  FT_Int     *anindex,
    
    353
    +                  FT_Color*  *apalette );
    
    354
    +
    
    355
    +
    
    269 356
       /**************************************************************************
    
    270 357
        *
    
    271 358
        * @function:
    
    ... ... @@ -286,11 +373,9 @@ FT_BEGIN_HEADER
    286 373
        *   FreeType error code.  0~means success.
    
    287 374
        *
    
    288 375
        * @note:
    
    289
    -   *   If this function isn't called, the text foreground color is set to
    
    290
    -   *   white opaque (BGRA value 0xFFFFFFFF) if
    
    291
    -   *   @FT_PALETTE_FOR_DARK_BACKGROUND is present for the current palette,
    
    292
    -   *   and black opaque (BGRA value 0x000000FF) otherwise, including the case
    
    293
    -   *   that no palette types are available in the 'CPAL' table.
    
    376
    +   *   See function @FT_Palette_Get_Foreground_Color for details on what
    
    377
    +   *   foreground color is used if `FT_Palette_Set_Foreground_Color` is not
    
    378
    +   *   called.
    
    294 379
        *
    
    295 380
        *   This function always returns an error if the config macro
    
    296 381
        *   `TT_CONFIG_OPTION_COLOR_LAYERS` is not defined in `ftoption.h`.
    
    ... ... @@ -303,6 +388,53 @@ FT_BEGIN_HEADER
    303 388
                                        FT_Color  foreground_color );
    
    304 389
     
    
    305 390
     
    
    391
    +  /**************************************************************************
    
    392
    +   *
    
    393
    +   * @function:
    
    394
    +   *   FT_Palette_Get_Foreground_Color
    
    395
    +   *
    
    396
    +   * @description:
    
    397
    +   *   Get the 'text foreground color' as set by a previous call to
    
    398
    +   *   @FT_Palette_Set_Foreground_Color.
    
    399
    +   *
    
    400
    +   * @input:
    
    401
    +   *   face ::
    
    402
    +   *     The source face handle.
    
    403
    +   *
    
    404
    +   * @output:
    
    405
    +   *   aforeground_color ::
    
    406
    +   *     The text foreground color.
    
    407
    +   *
    
    408
    +   * @return:
    
    409
    +   *   FreeType error code.  0~means success.
    
    410
    +   *
    
    411
    +   * @note:
    
    412
    +   *   The returned color is as follows.
    
    413
    +   *
    
    414
    +   *   * If a foreground color was set with @FT_Palette_Get_Foreground_Color,
    
    415
    +   *     return this value.
    
    416
    +   *
    
    417
    +   *   * Otherwise, return black opaque (BGRA value 0x000000FF) if the
    
    418
    +   *     'working palette' is a user-defined palette.
    
    419
    +   *
    
    420
    +   *   * Otherwise, return white opaque (BGRA value 0xFFFFFFFF) if
    
    421
    +   *     @FT_PALETTE_FOR_DARK_BACKGROUND is present for the current palette.
    
    422
    +   *
    
    423
    +   *   * Otherwise, return black opaque (BGRA value 0x000000FF).  This
    
    424
    +   *     includes the case that no palette types are available in the 'CPAL'
    
    425
    +   *     table.
    
    426
    +   *
    
    427
    +   *   This function always returns an error if the config macro
    
    428
    +   *   `TT_CONFIG_OPTION_COLOR_LAYERS` is not defined in `ftoption.h`.
    
    429
    +   *
    
    430
    +   * @since:
    
    431
    +   *   2.12
    
    432
    +   */
    
    433
    +  FT_EXPORT( FT_Error )
    
    434
    +  FT_Palette_Get_Foreground_Color( FT_Face    face,
    
    435
    +                                   FT_Color*  aforeground_color );
    
    436
    +
    
    437
    +
    
    306 438
       /**************************************************************************
    
    307 439
        *
    
    308 440
        * @section:
    

  • include/freetype/internal/tttypes.h
    ... ... @@ -1537,7 +1537,7 @@ FT_BEGIN_HEADER
    1537 1537
     
    
    1538 1538
         /* glyph colors */
    
    1539 1539
         FT_Palette_Data       palette_data;         /* since 2.10 */
    
    1540
    -    FT_UShort             palette_index;
    
    1540
    +    FT_Int                palette_index;
    
    1541 1541
         FT_Color*             palette;
    
    1542 1542
         FT_Bool               have_foreground_color;
    
    1543 1543
         FT_Color              foreground_color;
    

  • src/base/ftcolor.c
    ... ... @@ -36,7 +36,7 @@
    36 36
       {
    
    37 37
         if ( !face )
    
    38 38
           return FT_THROW( Invalid_Face_Handle );
    
    39
    -    if ( !apalette_data)
    
    39
    +    if ( !apalette_data )
    
    40 40
           return FT_THROW( Invalid_Argument );
    
    41 41
     
    
    42 42
         if ( FT_IS_SFNT( face ) )
    
    ... ... @@ -88,6 +88,57 @@
    88 88
       }
    
    89 89
     
    
    90 90
     
    
    91
    +  /* documentation is in ftcolor.h */
    
    92
    +
    
    93
    +  FT_EXPORT_DEF( FT_Error )
    
    94
    +  FT_Palette_Set( FT_Face    face,
    
    95
    +                  FT_Int     index,
    
    96
    +                  FT_Color*  palette )
    
    97
    +  {
    
    98
    +    TT_Face    ttface;
    
    99
    +    FT_UShort  i;
    
    100
    +
    
    101
    +
    
    102
    +    if ( !face || !FT_IS_SFNT( face ) )
    
    103
    +      return FT_THROW( Invalid_Face_Handle );
    
    104
    +
    
    105
    +    if ( !palette || index >= 0 )
    
    106
    +      return FT_THROW( Invalid_Argument );
    
    107
    +
    
    108
    +    ttface = (TT_Face)face;
    
    109
    +
    
    110
    +    for ( i = 0; i < ttface->palette_data.num_palette_entries; i++ )
    
    111
    +      ttface->palette[i] = palette[i];
    
    112
    +    ttface->palette_index = index;
    
    113
    +
    
    114
    +    return FT_Err_Ok;
    
    115
    +  }
    
    116
    +
    
    117
    +
    
    118
    +  /* documentation is in ftcolor.h */
    
    119
    +
    
    120
    +  FT_EXPORT_DEF( FT_Error )
    
    121
    +  FT_Palette_Get( FT_Face     face,
    
    122
    +                  FT_Int     *anindex,
    
    123
    +                  FT_Color*  *apalette )
    
    124
    +  {
    
    125
    +    TT_Face  ttface;
    
    126
    +
    
    127
    +
    
    128
    +    if ( !face || !FT_IS_SFNT( face ) )
    
    129
    +      return FT_THROW( Invalid_Face_Handle );
    
    130
    +
    
    131
    +    ttface = (TT_Face)face;
    
    132
    +
    
    133
    +    if ( anindex )
    
    134
    +      *anindex  = ttface->palette_index;
    
    135
    +    if ( apalette )
    
    136
    +      *apalette = ttface->palette;
    
    137
    +
    
    138
    +    return FT_Err_Ok;
    
    139
    +  }
    
    140
    +
    
    141
    +
    
    91 142
       /* documentation is in ftcolor.h */
    
    92 143
     
    
    93 144
       FT_EXPORT_DEF( FT_Error )
    
    ... ... @@ -111,6 +162,44 @@
    111 162
         return FT_Err_Ok;
    
    112 163
       }
    
    113 164
     
    
    165
    +
    
    166
    +  /* documentation is in ftcolor.h */
    
    167
    +
    
    168
    +  FT_EXPORT_DEF( FT_Error )
    
    169
    +  FT_Palette_Get_Foreground_Color( FT_Face    face,
    
    170
    +                                   FT_Color*  aforeground_color )
    
    171
    +  {
    
    172
    +    TT_Face  ttface;
    
    173
    +
    
    174
    +    FT_Color  white = { 0xFF, 0xFF, 0xFF, 0xFF };
    
    175
    +    FT_Color  black = { 0x00, 0x00, 0x00, 0xFF };
    
    176
    +
    
    177
    +
    
    178
    +    if ( !face || !FT_IS_SFNT( face ) )
    
    179
    +      return FT_THROW( Invalid_Face_Handle );
    
    180
    +
    
    181
    +    if ( !aforeground_color )
    
    182
    +      return FT_THROW( Invalid_Argument );
    
    183
    +
    
    184
    +    ttface = (TT_Face)face;
    
    185
    +
    
    186
    +    if ( ttface->have_foreground_color )
    
    187
    +      *aforeground_color = ttface->foreground_color;
    
    188
    +    else if ( ttface->palette_index < 0 )
    
    189
    +      *aforeground_color = black;
    
    190
    +    else
    
    191
    +    {
    
    192
    +      if ( ttface->palette_data.palette_flags                            &&
    
    193
    +           ( ttface->palette_data.palette_flags[ttface->palette_index] &
    
    194
    +               FT_PALETTE_FOR_DARK_BACKGROUND                          ) )
    
    195
    +        *aforeground_color = white;
    
    196
    +      else
    
    197
    +        *aforeground_color = black;
    
    198
    +    }
    
    199
    +
    
    200
    +    return FT_Err_Ok;
    
    201
    +  }
    
    202
    +
    
    114 203
     #else /* !TT_CONFIG_OPTION_COLOR_LAYERS */
    
    115 204
     
    
    116 205
       FT_EXPORT_DEF( FT_Error )
    
    ... ... @@ -139,6 +228,34 @@
    139 228
       }
    
    140 229
     
    
    141 230
     
    
    231
    +  FT_EXPORT_DEF( FT_Error )
    
    232
    +  FT_Palette_Set( FT_Face    face,
    
    233
    +                  FT_Int     index,
    
    234
    +                  FT_Color*  palette )
    
    235
    +  {
    
    236
    +    FT_UNUSED( face );
    
    237
    +    FT_UNUSED( index );
    
    238
    +    FT_UNUSED( palette );
    
    239
    +
    
    240
    +
    
    241
    +    return FT_THROW( Unimplemented_Feature );
    
    242
    +  }
    
    243
    +
    
    244
    +
    
    245
    +  FT_EXPORT_DEF( FT_Error )
    
    246
    +  FT_Palette_Get( FT_Face     face,
    
    247
    +                  FT_Int     *anindex,
    
    248
    +                  FT_Color*  *apalette )
    
    249
    +  {
    
    250
    +    FT_UNUSED( face );
    
    251
    +    FT_UNUSED( anindex );
    
    252
    +    FT_UNUSED( apalette );
    
    253
    +
    
    254
    +
    
    255
    +    return FT_THROW( Unimplemented_Feature );
    
    256
    +  }
    
    257
    +
    
    258
    +
    
    142 259
       FT_EXPORT_DEF( FT_Error )
    
    143 260
       FT_Palette_Set_Foreground_Color( FT_Face   face,
    
    144 261
                                        FT_Color  foreground_color )
    
    ... ... @@ -150,6 +267,18 @@
    150 267
         return FT_THROW( Unimplemented_Feature );
    
    151 268
       }
    
    152 269
     
    
    270
    +
    
    271
    +  FT_EXPORT_DEF( FT_Error )
    
    272
    +  FT_Palette_Get_Foreground_Color( FT_Face    face,
    
    273
    +                                   FT_Color*  aforeground_color )
    
    274
    +  {
    
    275
    +    FT_UNUSED( face );
    
    276
    +    FT_UNUSED( aforeground_color );
    
    277
    +
    
    278
    +
    
    279
    +    return FT_THROW( Unimplemented_Feature );
    
    280
    +  }
    
    281
    +
    
    153 282
     #endif /* !TT_CONFIG_OPTION_COLOR_LAYERS */
    
    154 283
     
    
    155 284
     
    


  • reply via email to

    [Prev in Thread] Current Thread [Next in Thread]