Skip to content

Public API for COLR/CPAL #849

@ebraminio

Description

@ebraminio

Now that we have COLR/CPAL tables parsing, it would be nice if we expose some of it as a public API for clients so they won't need to write their own font parsing logic. All what is needed here is signature of public APIs we like to have (which of course is not that easy...). Some useful links:

Using our current CPAL/COLR structs helpers (private API) it can be implemented like this:

  const OT::COLR &colr = _get_colr(font->face);
  const OT::CPAL &cpal = _get_cpal(font->face);

  FT_Face ftface = hb_ft_font_get_face (font);
  for (unsigned int i = 0; i < buffer->len; ++i)
  {
    hb_codepoint_t code_point = buffer->info[i].codepoint;

    FT_Load_Glyph (ftface, code_point, FT_LOAD_DEFAULT);
    FT_GlyphSlot slot = ftface->glyph;
    FT_Render_Glyph (slot, FT_RENDER_MODE_NORMAL);

    int width = slot->bitmap.width;
    int height = slot->bitmap.rows;
    int left = slot->bitmap_left;
    int top = slot->bitmap_top;

    unsigned char* image = (unsigned char*) calloc (width * height * 16, sizeof(char));

    unsigned int first_layer_index, num_layers;
    if (colr.get_base_glyph_record (code_point, first_layer_index, num_layers))
    {
      for (unsigned int j = 0; j < num_layers; ++j)
      {
        hb_codepoint_t glyph_id;
        unsigned int color_index;
        colr.get_layer_record(first_layer_index + j, glyph_id, color_index);

        uint32_t color = cpal.get_color_record_argb(color_index, 0);

        FT_Load_Glyph(ftface, glyph_id, FT_LOAD_DEFAULT);
        FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);

        drawBitmap(&slot->bitmap,
          slot->bitmap_left - left,
          top - slot->bitmap_top,
          image, width, height,
          color);
      }
    }
  }

(considering #859 is merged)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions