mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-21 06:51:01 +01:00
Merge pull request #5761 from harfbuzz/raster-svg
[raster] Rasterize simple SVG fonts
This commit is contained in:
11 files changed
+3804
-661
No files matched your search
@@ -1040,6 +1040,8 @@ hb_raster_paint_set_extents
|
||||
hb_raster_paint_get_extents
|
||||
hb_raster_paint_set_glyph_extents
|
||||
hb_raster_paint_set_foreground
|
||||
hb_raster_paint_clear_custom_palette_colors
|
||||
hb_raster_paint_set_custom_palette_color
|
||||
hb_raster_paint_get_funcs
|
||||
hb_raster_paint_glyph
|
||||
hb_raster_paint_render
|
||||
|
||||
+588
-4
@@ -27,7 +27,10 @@
|
||||
|
||||
#include "../../../hb-open-type.hh"
|
||||
#include "../../../hb-blob.hh"
|
||||
#include "../../../hb-map.hh"
|
||||
#include "../../../hb-paint.hh"
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* SVG -- SVG (Scalable Vector Graphics)
|
||||
@@ -84,13 +87,50 @@ struct SVG
|
||||
{
|
||||
static constexpr hb_tag_t tableTag = HB_OT_TAG_SVG;
|
||||
|
||||
struct svg_id_span_t
|
||||
{
|
||||
const char *p;
|
||||
unsigned len;
|
||||
|
||||
bool operator == (const svg_id_span_t &o) const
|
||||
{
|
||||
return len == o.len && !memcmp (p, o.p, len);
|
||||
}
|
||||
|
||||
uint32_t hash () const
|
||||
{
|
||||
uint32_t h = hb_hash (len);
|
||||
for (unsigned i = 0; i < len; i++)
|
||||
h = h * 33u + (unsigned char) p[i];
|
||||
return h;
|
||||
}
|
||||
};
|
||||
|
||||
struct svg_defs_entry_t
|
||||
{
|
||||
svg_id_span_t id;
|
||||
unsigned start;
|
||||
unsigned end;
|
||||
};
|
||||
|
||||
struct svg_doc_cache_t
|
||||
{
|
||||
hb_blob_t *blob = nullptr;
|
||||
const char *svg = nullptr;
|
||||
unsigned len = 0;
|
||||
hb_vector_t<svg_defs_entry_t> defs_entries;
|
||||
hb_codepoint_t start_glyph = HB_CODEPOINT_INVALID;
|
||||
hb_codepoint_t end_glyph = HB_CODEPOINT_INVALID;
|
||||
hb_vector_t<hb_pair_t<uint32_t, uint32_t>> glyph_spans;
|
||||
hb_hashmap_t<svg_id_span_t, hb_pair_t<uint32_t, uint32_t>> id_spans;
|
||||
};
|
||||
|
||||
bool has_data () const { return svgDocEntries; }
|
||||
|
||||
struct accelerator_t
|
||||
{
|
||||
accelerator_t (hb_face_t *face)
|
||||
{ table = hb_sanitize_context_t ().reference_table<SVG> (face); }
|
||||
~accelerator_t () { table.destroy (); }
|
||||
accelerator_t (hb_face_t *face);
|
||||
~accelerator_t ();
|
||||
|
||||
hb_blob_t *reference_blob_for_glyph (hb_codepoint_t glyph_id) const
|
||||
{
|
||||
@@ -111,6 +151,39 @@ struct SVG
|
||||
|
||||
bool has_data () const { return table->has_data (); }
|
||||
|
||||
const svg_doc_cache_t *
|
||||
get_or_create_doc_cache (hb_blob_t *image,
|
||||
const char *svg,
|
||||
unsigned len,
|
||||
unsigned doc_index,
|
||||
hb_codepoint_t start_glyph,
|
||||
hb_codepoint_t end_glyph) const;
|
||||
|
||||
const char *
|
||||
doc_cache_get_svg (const svg_doc_cache_t *doc,
|
||||
unsigned *len) const;
|
||||
|
||||
const hb_vector_t<svg_defs_entry_t> *
|
||||
doc_cache_get_defs_entries (const svg_doc_cache_t *doc) const;
|
||||
|
||||
bool
|
||||
doc_cache_get_glyph_span (const svg_doc_cache_t *doc,
|
||||
hb_codepoint_t glyph,
|
||||
unsigned *start,
|
||||
unsigned *end) const;
|
||||
|
||||
bool
|
||||
doc_cache_find_id_span (const svg_doc_cache_t *doc,
|
||||
svg_id_span_t id,
|
||||
unsigned *start,
|
||||
unsigned *end) const;
|
||||
|
||||
bool
|
||||
doc_cache_find_id_cstr (const svg_doc_cache_t *doc,
|
||||
const char *id,
|
||||
unsigned *start,
|
||||
unsigned *end) const;
|
||||
|
||||
bool paint_glyph (hb_font_t *font HB_UNUSED, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data) const
|
||||
{
|
||||
if (!has_data ())
|
||||
@@ -134,9 +207,20 @@ struct SVG
|
||||
}
|
||||
|
||||
private:
|
||||
svg_doc_cache_t *
|
||||
make_doc_cache (hb_blob_t *image,
|
||||
const char *svg,
|
||||
unsigned len,
|
||||
hb_codepoint_t start_glyph,
|
||||
hb_codepoint_t end_glyph) const;
|
||||
|
||||
static void destroy_doc_cache (svg_doc_cache_t *doc);
|
||||
|
||||
hb_blob_ptr_t<SVG> table;
|
||||
mutable hb_vector_t<hb_atomic_t<svg_doc_cache_t *>> doc_caches;
|
||||
public:
|
||||
DEFINE_SIZE_STATIC (sizeof (hb_blob_ptr_t<SVG>));
|
||||
DEFINE_SIZE_STATIC (sizeof (hb_blob_ptr_t<SVG>) +
|
||||
sizeof (hb_vector_t<hb_atomic_t<svg_doc_cache_t *>>));
|
||||
};
|
||||
|
||||
const SVGDocumentIndexEntry &get_glyph_entry (hb_codepoint_t glyph_id) const
|
||||
@@ -191,6 +275,506 @@ struct SVG
|
||||
DEFINE_SIZE_STATIC (10);
|
||||
};
|
||||
|
||||
namespace _hb_svg_cache_impl {
|
||||
|
||||
struct glyph_entry_t
|
||||
{
|
||||
hb_codepoint_t glyph;
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
};
|
||||
|
||||
struct id_entry_t
|
||||
{
|
||||
SVG::svg_id_span_t id;
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
};
|
||||
|
||||
struct open_elem_t
|
||||
{
|
||||
unsigned start;
|
||||
SVG::svg_id_span_t id;
|
||||
bool in_defs_content;
|
||||
bool is_defs;
|
||||
};
|
||||
|
||||
static const unsigned MAX_DEPTH = 128;
|
||||
|
||||
static inline int
|
||||
find_substr (const char *s,
|
||||
unsigned n,
|
||||
unsigned from,
|
||||
const char *needle,
|
||||
unsigned needle_len)
|
||||
{
|
||||
if (!needle_len || from >= n || needle_len > n)
|
||||
return -1;
|
||||
for (unsigned i = from; i + needle_len <= n; i++)
|
||||
if (s[i] == needle[0] && !memcmp (s + i, needle, needle_len))
|
||||
return (int) i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
parse_id_in_start_tag (const char *svg,
|
||||
unsigned tag_start,
|
||||
unsigned tag_end,
|
||||
SVG::svg_id_span_t *id)
|
||||
{
|
||||
unsigned p = tag_start;
|
||||
while (p + 4 <= tag_end)
|
||||
{
|
||||
if (!memcmp (svg + p, "id=\"", 4))
|
||||
{
|
||||
unsigned b = p + 4;
|
||||
unsigned e = b;
|
||||
while (e < tag_end && svg[e] != '"') e++;
|
||||
if (e <= tag_end && e > b)
|
||||
{
|
||||
*id = {svg + b, e - b};
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (!memcmp (svg + p, "id='", 4))
|
||||
{
|
||||
unsigned b = p + 4;
|
||||
unsigned e = b;
|
||||
while (e < tag_end && svg[e] != '\'') e++;
|
||||
if (e <= tag_end && e > b)
|
||||
{
|
||||
*id = {svg + b, e - b};
|
||||
return true;
|
||||
}
|
||||
}
|
||||
p++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
parse_glyph_id_span (const SVG::svg_id_span_t &id,
|
||||
hb_codepoint_t *glyph)
|
||||
{
|
||||
if (id.len <= 5 || memcmp (id.p, "glyph", 5))
|
||||
return false;
|
||||
|
||||
hb_codepoint_t gid = 0;
|
||||
for (unsigned i = 5; i < id.len; i++)
|
||||
{
|
||||
unsigned char c = (unsigned char) id.p[i];
|
||||
if (c < '0' || c > '9')
|
||||
return false;
|
||||
hb_codepoint_t digit = (hb_codepoint_t) (c - '0');
|
||||
if (unlikely (gid > HB_CODEPOINT_INVALID / 10 ||
|
||||
(gid == HB_CODEPOINT_INVALID / 10 &&
|
||||
digit > HB_CODEPOINT_INVALID % 10)))
|
||||
return false;
|
||||
gid = (hb_codepoint_t) (gid * 10 + digit);
|
||||
}
|
||||
|
||||
*glyph = gid;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
parse_cache_entries_linear (const char *svg,
|
||||
unsigned len,
|
||||
hb_vector_t<SVG::svg_defs_entry_t> *defs_entries,
|
||||
hb_vector_t<glyph_entry_t> *glyph_spans,
|
||||
hb_vector_t<id_entry_t> *id_entries)
|
||||
{
|
||||
open_elem_t stack[MAX_DEPTH];
|
||||
unsigned depth = 0;
|
||||
defs_entries->alloc (256);
|
||||
id_entries->alloc (256);
|
||||
|
||||
unsigned defs_depth = 0;
|
||||
unsigned i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
if (svg[i] != '<')
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i + 4 <= len && !memcmp (svg + i, "<!--", 4))
|
||||
{
|
||||
int cend = find_substr (svg, len, i + 4, "-->", 3);
|
||||
if (cend < 0) return false;
|
||||
i = (unsigned) cend + 3;
|
||||
continue;
|
||||
}
|
||||
if (i + 9 <= len && !memcmp (svg + i, "<![CDATA[", 9))
|
||||
{
|
||||
int cend = find_substr (svg, len, i + 9, "]]>", 3);
|
||||
if (cend < 0) return false;
|
||||
i = (unsigned) cend + 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
bool closing = (i + 1 < len && svg[i + 1] == '/');
|
||||
bool special = (i + 1 < len && (svg[i + 1] == '!' || svg[i + 1] == '?'));
|
||||
|
||||
unsigned gt = i + 1;
|
||||
char quote = 0;
|
||||
while (gt < len)
|
||||
{
|
||||
char c = svg[gt];
|
||||
if (quote)
|
||||
{
|
||||
if (c == quote) quote = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (c == '"' || c == '\'')
|
||||
quote = c;
|
||||
else if (c == '>')
|
||||
break;
|
||||
}
|
||||
gt++;
|
||||
}
|
||||
if (gt >= len)
|
||||
return false;
|
||||
|
||||
if (special)
|
||||
{
|
||||
i = gt + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned p = i + (closing ? 2 : 1);
|
||||
while (p < gt && isspace ((unsigned char) svg[p])) p++;
|
||||
const char *name = svg + p;
|
||||
unsigned name_len = 0;
|
||||
while (p + name_len < gt)
|
||||
{
|
||||
unsigned char c = (unsigned char) name[name_len];
|
||||
if (!(isalnum (c) || c == '_' || c == '-' || c == ':'))
|
||||
break;
|
||||
name_len++;
|
||||
}
|
||||
bool is_defs = (name_len == 4 && !memcmp (name, "defs", 4));
|
||||
|
||||
if (closing)
|
||||
{
|
||||
if (!depth)
|
||||
{
|
||||
i = gt + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
open_elem_t e = stack[--depth];
|
||||
unsigned end = gt + 1;
|
||||
|
||||
if (e.id.len)
|
||||
{
|
||||
auto *id_slot = id_entries->push ();
|
||||
if (unlikely (!id_slot))
|
||||
return false;
|
||||
*id_slot = {e.id, (uint32_t) e.start, (uint32_t) end};
|
||||
|
||||
if (e.in_defs_content)
|
||||
{
|
||||
auto *slot = defs_entries->push ();
|
||||
if (unlikely (!slot))
|
||||
return false;
|
||||
slot->id = e.id;
|
||||
slot->start = e.start;
|
||||
slot->end = end;
|
||||
}
|
||||
|
||||
hb_codepoint_t gid;
|
||||
if (parse_glyph_id_span (e.id, &gid))
|
||||
{
|
||||
auto *span = glyph_spans->push ();
|
||||
if (unlikely (!span))
|
||||
return false;
|
||||
span->glyph = gid;
|
||||
span->start = (uint32_t) e.start;
|
||||
span->end = (uint32_t) end;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.is_defs && defs_depth)
|
||||
defs_depth--;
|
||||
|
||||
i = end;
|
||||
continue;
|
||||
}
|
||||
|
||||
SVG::svg_id_span_t id = {nullptr, 0};
|
||||
parse_id_in_start_tag (svg, i, gt, &id);
|
||||
|
||||
unsigned r = gt;
|
||||
while (r > i && isspace ((unsigned char) svg[r - 1])) r--;
|
||||
bool self_closing = (r > i && svg[r - 1] == '/');
|
||||
|
||||
open_elem_t e = {i, id, defs_depth > 0, is_defs};
|
||||
|
||||
if (self_closing)
|
||||
{
|
||||
unsigned end = gt + 1;
|
||||
if (e.id.len)
|
||||
{
|
||||
auto *id_slot = id_entries->push ();
|
||||
if (unlikely (!id_slot))
|
||||
return false;
|
||||
*id_slot = {e.id, (uint32_t) e.start, (uint32_t) end};
|
||||
|
||||
if (e.in_defs_content)
|
||||
{
|
||||
auto *slot = defs_entries->push ();
|
||||
if (unlikely (!slot))
|
||||
return false;
|
||||
slot->id = e.id;
|
||||
slot->start = e.start;
|
||||
slot->end = end;
|
||||
}
|
||||
|
||||
hb_codepoint_t gid;
|
||||
if (parse_glyph_id_span (e.id, &gid))
|
||||
{
|
||||
auto *span = glyph_spans->push ();
|
||||
if (unlikely (!span))
|
||||
return false;
|
||||
span->glyph = gid;
|
||||
span->start = (uint32_t) e.start;
|
||||
span->end = (uint32_t) end;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (unlikely (depth >= MAX_DEPTH))
|
||||
return false;
|
||||
stack[depth++] = e;
|
||||
if (is_defs)
|
||||
defs_depth++;
|
||||
}
|
||||
|
||||
i = gt + 1;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} /* namespace _hb_svg_cache_impl */
|
||||
|
||||
inline
|
||||
SVG::accelerator_t::accelerator_t (hb_face_t *face)
|
||||
{
|
||||
table = hb_sanitize_context_t ().reference_table<SVG> (face);
|
||||
doc_caches.init ();
|
||||
unsigned doc_count = table->get_document_count ();
|
||||
if (doc_count && unlikely (!doc_caches.resize (doc_count)))
|
||||
doc_caches.resize (0);
|
||||
for (unsigned i = 0; i < doc_caches.length; i++)
|
||||
doc_caches.arrayZ[i].set_relaxed (nullptr);
|
||||
}
|
||||
|
||||
inline
|
||||
SVG::accelerator_t::~accelerator_t ()
|
||||
{
|
||||
for (unsigned i = 0; i < doc_caches.length; i++)
|
||||
destroy_doc_cache (doc_caches.arrayZ[i].get_relaxed ());
|
||||
doc_caches.fini ();
|
||||
table.destroy ();
|
||||
}
|
||||
|
||||
inline void
|
||||
SVG::accelerator_t::destroy_doc_cache (svg_doc_cache_t *doc)
|
||||
{
|
||||
if (!doc)
|
||||
return;
|
||||
doc->glyph_spans.fini ();
|
||||
doc->defs_entries.fini ();
|
||||
doc->id_spans.fini ();
|
||||
hb_blob_destroy (doc->blob);
|
||||
hb_free (doc);
|
||||
}
|
||||
|
||||
inline SVG::svg_doc_cache_t *
|
||||
SVG::accelerator_t::make_doc_cache (hb_blob_t *image,
|
||||
const char *svg,
|
||||
unsigned len,
|
||||
hb_codepoint_t start_glyph,
|
||||
hb_codepoint_t end_glyph) const
|
||||
{
|
||||
static const uint32_t INVALID_SPAN = 0xFFFFFFFFu;
|
||||
|
||||
auto *doc = (svg_doc_cache_t *) hb_malloc (sizeof (svg_doc_cache_t));
|
||||
if (!doc)
|
||||
return nullptr;
|
||||
|
||||
doc->blob = nullptr;
|
||||
doc->svg = nullptr;
|
||||
doc->len = 0;
|
||||
doc->defs_entries.init ();
|
||||
doc->start_glyph = HB_CODEPOINT_INVALID;
|
||||
doc->end_glyph = HB_CODEPOINT_INVALID;
|
||||
doc->glyph_spans.init ();
|
||||
doc->id_spans.init ();
|
||||
|
||||
doc->blob = hb_blob_reference (image);
|
||||
doc->svg = svg;
|
||||
doc->len = len;
|
||||
doc->start_glyph = start_glyph;
|
||||
doc->end_glyph = end_glyph;
|
||||
|
||||
if (unlikely (start_glyph == HB_CODEPOINT_INVALID || end_glyph < start_glyph))
|
||||
{
|
||||
destroy_doc_cache (doc);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
unsigned glyph_count = end_glyph - start_glyph + 1;
|
||||
if (!doc->glyph_spans.resize ((int) glyph_count))
|
||||
{
|
||||
destroy_doc_cache (doc);
|
||||
return nullptr;
|
||||
}
|
||||
for (unsigned i = 0; i < glyph_count; i++)
|
||||
doc->glyph_spans.arrayZ[i] = hb_pair_t<uint32_t, uint32_t> (INVALID_SPAN, INVALID_SPAN);
|
||||
|
||||
hb_vector_t<_hb_svg_cache_impl::glyph_entry_t> glyph_spans;
|
||||
glyph_spans.init ();
|
||||
hb_vector_t<_hb_svg_cache_impl::id_entry_t> id_entries;
|
||||
id_entries.init ();
|
||||
if (!_hb_svg_cache_impl::parse_cache_entries_linear (svg, len,
|
||||
&doc->defs_entries,
|
||||
&glyph_spans,
|
||||
&id_entries))
|
||||
{
|
||||
id_entries.fini ();
|
||||
glyph_spans.fini ();
|
||||
destroy_doc_cache (doc);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < glyph_spans.length; i++)
|
||||
{
|
||||
const auto &span = glyph_spans.arrayZ[i];
|
||||
if (unlikely (span.glyph < start_glyph || span.glyph > end_glyph))
|
||||
continue;
|
||||
doc->glyph_spans.arrayZ[span.glyph - start_glyph] = hb_pair_t<uint32_t, uint32_t> (span.start, span.end);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < id_entries.length; i++)
|
||||
{
|
||||
const auto &e = id_entries.arrayZ[i];
|
||||
hb_pair_t<uint32_t, uint32_t> *out = nullptr;
|
||||
if (doc->id_spans.has (e.id, &out))
|
||||
continue;
|
||||
if (unlikely (!doc->id_spans.set (e.id, hb_pair_t<uint32_t, uint32_t> (e.start, e.end))))
|
||||
{
|
||||
id_entries.fini ();
|
||||
glyph_spans.fini ();
|
||||
destroy_doc_cache (doc);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
id_entries.fini ();
|
||||
glyph_spans.fini ();
|
||||
return doc;
|
||||
}
|
||||
|
||||
inline const SVG::svg_doc_cache_t *
|
||||
SVG::accelerator_t::get_or_create_doc_cache (hb_blob_t *image,
|
||||
const char *svg,
|
||||
unsigned len,
|
||||
unsigned doc_index,
|
||||
hb_codepoint_t start_glyph,
|
||||
hb_codepoint_t end_glyph) const
|
||||
{
|
||||
if (doc_index >= doc_caches.length)
|
||||
return nullptr;
|
||||
|
||||
auto &slot = doc_caches.arrayZ[doc_index];
|
||||
auto *doc = slot.get_acquire ();
|
||||
if (doc)
|
||||
return doc;
|
||||
|
||||
auto *fresh = make_doc_cache (image, svg, len, start_glyph, end_glyph);
|
||||
if (!fresh)
|
||||
return nullptr;
|
||||
|
||||
auto *expected = (svg_doc_cache_t *) nullptr;
|
||||
if (slot.cmpexch (expected, fresh))
|
||||
return fresh;
|
||||
|
||||
destroy_doc_cache (fresh);
|
||||
return expected;
|
||||
}
|
||||
|
||||
inline const char *
|
||||
SVG::accelerator_t::doc_cache_get_svg (const svg_doc_cache_t *doc,
|
||||
unsigned *len) const
|
||||
{
|
||||
if (!doc)
|
||||
{
|
||||
if (len) *len = 0;
|
||||
return nullptr;
|
||||
}
|
||||
if (len) *len = doc->len;
|
||||
return doc->svg;
|
||||
}
|
||||
|
||||
inline const hb_vector_t<SVG::svg_defs_entry_t> *
|
||||
SVG::accelerator_t::doc_cache_get_defs_entries (const svg_doc_cache_t *doc) const
|
||||
{
|
||||
return doc ? &doc->defs_entries : nullptr;
|
||||
}
|
||||
|
||||
inline bool
|
||||
SVG::accelerator_t::doc_cache_get_glyph_span (const svg_doc_cache_t *doc,
|
||||
hb_codepoint_t glyph,
|
||||
unsigned *start,
|
||||
unsigned *end) const
|
||||
{
|
||||
static const uint32_t INVALID_SPAN = 0xFFFFFFFFu;
|
||||
if (!doc || doc->start_glyph == HB_CODEPOINT_INVALID ||
|
||||
glyph < doc->start_glyph || glyph > doc->end_glyph)
|
||||
return false;
|
||||
|
||||
const auto &span = doc->glyph_spans.arrayZ[glyph - doc->start_glyph];
|
||||
if (span.first == INVALID_SPAN)
|
||||
return false;
|
||||
|
||||
if (start) *start = span.first;
|
||||
if (end) *end = span.second;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool
|
||||
SVG::accelerator_t::doc_cache_find_id_span (const svg_doc_cache_t *doc,
|
||||
svg_id_span_t id,
|
||||
unsigned *start,
|
||||
unsigned *end) const
|
||||
{
|
||||
if (!doc || !id.p || !id.len)
|
||||
return false;
|
||||
hb_pair_t<uint32_t, uint32_t> *span = nullptr;
|
||||
if (!doc->id_spans.has (id, &span))
|
||||
return false;
|
||||
if (start) *start = span->first;
|
||||
if (end) *end = span->second;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool
|
||||
SVG::accelerator_t::doc_cache_find_id_cstr (const svg_doc_cache_t *doc,
|
||||
const char *id,
|
||||
unsigned *start,
|
||||
unsigned *end) const
|
||||
{
|
||||
if (!id) return false;
|
||||
svg_id_span_t key = {id, (unsigned) strlen (id)};
|
||||
return doc_cache_find_id_span (doc, key, start, end);
|
||||
}
|
||||
|
||||
struct SVG_accelerator_t : SVG::accelerator_t {
|
||||
SVG_accelerator_t (hb_face_t *face) : SVG::accelerator_t (face) {}
|
||||
};
|
||||
|
||||
+214
-168
@@ -26,174 +26,12 @@
|
||||
|
||||
#include "hb.hh"
|
||||
|
||||
#include "hb-raster-image.hh"
|
||||
#include "hb-geometry.hh"
|
||||
#include "hb-raster-paint.hh"
|
||||
#include "hb-raster-svg.hh"
|
||||
#include "hb-machinery.hh"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
/* hb_raster_clip_t — alpha mask for clipping */
|
||||
struct hb_raster_clip_t
|
||||
{
|
||||
hb_vector_t<uint8_t> alpha; /* A8 mask, same extents as root surface */
|
||||
unsigned width = 0;
|
||||
unsigned height = 0;
|
||||
unsigned stride = 0;
|
||||
|
||||
/* Fast path: simple rectangle (no alpha buffer needed) */
|
||||
bool is_rect = true;
|
||||
int rect_x0 = 0, rect_y0 = 0;
|
||||
int rect_x1 = 0, rect_y1 = 0;
|
||||
|
||||
/* Bounding box of non-zero alpha region (valid for both rect and mask) */
|
||||
unsigned min_x = 0, min_y = 0;
|
||||
unsigned max_x = 0, max_y = 0;
|
||||
|
||||
void init_full (unsigned w, unsigned h)
|
||||
{
|
||||
width = w;
|
||||
height = h;
|
||||
stride = (w + 3u) & ~3u;
|
||||
is_rect = true;
|
||||
rect_x0 = 0;
|
||||
rect_y0 = 0;
|
||||
rect_x1 = (int) w;
|
||||
rect_y1 = (int) h;
|
||||
min_x = 0;
|
||||
min_y = 0;
|
||||
max_x = w;
|
||||
max_y = h;
|
||||
}
|
||||
|
||||
void update_bounds_from_rect ()
|
||||
{
|
||||
min_x = (unsigned) hb_max (rect_x0, 0);
|
||||
min_y = (unsigned) hb_max (rect_y0, 0);
|
||||
max_x = (unsigned) hb_max (hb_min (rect_x1, (int) width), 0);
|
||||
max_y = (unsigned) hb_max (hb_min (rect_y1, (int) height), 0);
|
||||
}
|
||||
|
||||
uint8_t get_alpha (unsigned x, unsigned y) const
|
||||
{
|
||||
if (is_rect)
|
||||
return ((int) x >= rect_x0 && (int) x < rect_x1 &&
|
||||
(int) y >= rect_y0 && (int) y < rect_y1) ? 255 : 0;
|
||||
if (x >= width || y >= height) return 0;
|
||||
return alpha[y * stride + x];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* hb_raster_paint_t — color glyph paint context */
|
||||
struct hb_raster_paint_t
|
||||
{
|
||||
hb_object_header_t header;
|
||||
|
||||
/* Configuration */
|
||||
hb_transform_t<> base_transform = {1, 0, 0, 1, 0, 0};
|
||||
float x_scale_factor = 1.f;
|
||||
float y_scale_factor = 1.f;
|
||||
hb_raster_extents_t fixed_extents = {};
|
||||
bool has_extents = false;
|
||||
hb_color_t foreground = HB_COLOR (0, 0, 0, 255);
|
||||
|
||||
/* Stacks */
|
||||
hb_vector_t<hb_transform_t<>> transform_stack;
|
||||
hb_vector_t<hb_raster_clip_t> clip_stack;
|
||||
hb_vector_t<hb_raster_clip_t> clip_cache;
|
||||
hb_vector_t<hb_raster_image_t *> surface_stack;
|
||||
|
||||
/* Cached surface pool (freelist for reuse across push/pop group) */
|
||||
hb_vector_t<hb_raster_image_t *> surface_cache;
|
||||
hb_vector_t<hb_color_stop_t> scratch_color_stops;
|
||||
|
||||
/* Internal rasterizer for clip-to-glyph */
|
||||
hb_raster_draw_t *clip_rdr = nullptr;
|
||||
|
||||
/* Helpers */
|
||||
|
||||
hb_raster_image_t *acquire_surface ()
|
||||
{
|
||||
hb_raster_image_t *img;
|
||||
if (surface_cache.length)
|
||||
img = surface_cache.pop ();
|
||||
else
|
||||
{
|
||||
img = hb_raster_image_create_or_fail ();
|
||||
if (unlikely (!img)) return nullptr;
|
||||
}
|
||||
|
||||
if (unlikely (!img->configure (HB_RASTER_FORMAT_BGRA32, fixed_extents)))
|
||||
{
|
||||
hb_raster_image_destroy (img);
|
||||
return nullptr;
|
||||
}
|
||||
img->clear ();
|
||||
return img;
|
||||
}
|
||||
|
||||
void release_surface (hb_raster_image_t *img)
|
||||
{
|
||||
surface_cache.push (img);
|
||||
}
|
||||
|
||||
hb_raster_clip_t acquire_clip (unsigned w, unsigned h)
|
||||
{
|
||||
hb_raster_clip_t clip;
|
||||
if (clip_cache.length)
|
||||
clip = clip_cache.pop ();
|
||||
clip.width = w;
|
||||
clip.height = h;
|
||||
clip.stride = (w + 3u) & ~3u;
|
||||
clip.is_rect = false;
|
||||
return clip;
|
||||
}
|
||||
|
||||
void release_clip (hb_raster_clip_t &&clip)
|
||||
{
|
||||
if (clip.alpha.arrayZ)
|
||||
clip_cache.push (std::move (clip));
|
||||
}
|
||||
|
||||
void release_all_clips ()
|
||||
{
|
||||
while (clip_stack.length)
|
||||
release_clip (clip_stack.pop ());
|
||||
}
|
||||
|
||||
hb_raster_image_t *current_surface ()
|
||||
{
|
||||
return surface_stack.length ? surface_stack.tail () : nullptr;
|
||||
}
|
||||
|
||||
hb_raster_clip_t ¤t_clip ()
|
||||
{
|
||||
return clip_stack.tail ();
|
||||
}
|
||||
|
||||
hb_transform_t<> ¤t_transform ()
|
||||
{
|
||||
return transform_stack.tail ();
|
||||
}
|
||||
|
||||
void apply_scale_factor (hb_transform_t<> &t) const
|
||||
{
|
||||
t.xx /= x_scale_factor;
|
||||
t.xy /= x_scale_factor;
|
||||
t.x0 /= x_scale_factor;
|
||||
t.yx /= y_scale_factor;
|
||||
t.yy /= y_scale_factor;
|
||||
t.y0 /= y_scale_factor;
|
||||
}
|
||||
|
||||
hb_transform_t<> current_effective_transform ()
|
||||
{
|
||||
hb_transform_t<> t = current_transform ();
|
||||
apply_scale_factor (t);
|
||||
return t;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Pixel helpers (paint-specific)
|
||||
@@ -427,6 +265,152 @@ hb_raster_paint_push_clip_glyph (hb_paint_funcs_t *pfuncs HB_UNUSED,
|
||||
c->clip_stack.push (std::move (new_clip));
|
||||
}
|
||||
|
||||
/* Push clip from arbitrary path emitter (used by SVG rasterizer).
|
||||
* Identical to push_clip_glyph but calls user func instead of hb_font_draw_glyph. */
|
||||
void
|
||||
hb_raster_paint_push_clip_path (hb_raster_paint_t *c,
|
||||
hb_raster_svg_path_func_t func,
|
||||
void *user_data)
|
||||
{
|
||||
ensure_initialized (c);
|
||||
|
||||
hb_raster_image_t *surf = c->current_surface ();
|
||||
if (unlikely (!surf)) return;
|
||||
|
||||
unsigned w = surf->extents.width;
|
||||
unsigned h = surf->extents.height;
|
||||
|
||||
hb_raster_clip_t new_clip = c->acquire_clip (w, h);
|
||||
|
||||
hb_raster_draw_t *rdr = c->clip_rdr;
|
||||
hb_transform_t<> t = c->current_effective_transform ();
|
||||
hb_raster_draw_set_transform (rdr, t.xx, t.yx, t.xy, t.yy, t.x0, t.y0);
|
||||
|
||||
func (hb_raster_draw_get_funcs (), rdr, user_data);
|
||||
hb_raster_image_t *mask_img = hb_raster_draw_render (rdr);
|
||||
|
||||
if (unlikely (!mask_img))
|
||||
{
|
||||
new_clip.init_full (w, h);
|
||||
new_clip.is_rect = true;
|
||||
new_clip.rect_x0 = new_clip.rect_y0 = 0;
|
||||
new_clip.rect_x1 = new_clip.rect_y1 = 0;
|
||||
new_clip.min_x = new_clip.min_y = new_clip.max_x = new_clip.max_y = 0;
|
||||
c->clip_stack.push (std::move (new_clip));
|
||||
return;
|
||||
}
|
||||
|
||||
if (unlikely (!new_clip.alpha.resize (new_clip.stride * h)))
|
||||
{
|
||||
hb_raster_draw_recycle_image (rdr, mask_img);
|
||||
new_clip.init_full (w, h);
|
||||
new_clip.is_rect = true;
|
||||
new_clip.rect_x0 = new_clip.rect_y0 = 0;
|
||||
new_clip.rect_x1 = new_clip.rect_y1 = 0;
|
||||
new_clip.min_x = new_clip.min_y = new_clip.max_x = new_clip.max_y = 0;
|
||||
c->clip_stack.push (std::move (new_clip));
|
||||
return;
|
||||
}
|
||||
|
||||
const uint8_t *mask_buf = hb_raster_image_get_buffer (mask_img);
|
||||
hb_raster_extents_t mask_ext;
|
||||
hb_raster_image_get_extents (mask_img, &mask_ext);
|
||||
const hb_raster_clip_t &old_clip = c->current_clip ();
|
||||
|
||||
int mask_x0 = mask_ext.x_origin - surf->extents.x_origin;
|
||||
int mask_y0 = mask_ext.y_origin - surf->extents.y_origin;
|
||||
int mask_x1 = mask_x0 + (int) mask_ext.width;
|
||||
int mask_y1 = mask_y0 + (int) mask_ext.height;
|
||||
|
||||
int ix0_i = hb_max ((int) old_clip.min_x, hb_max (mask_x0, 0));
|
||||
int iy0_i = hb_max ((int) old_clip.min_y, hb_max (mask_y0, 0));
|
||||
int ix1_i = hb_min ((int) old_clip.max_x, hb_min (mask_x1, (int) w));
|
||||
int iy1_i = hb_min ((int) old_clip.max_y, hb_min (mask_y1, (int) h));
|
||||
|
||||
if (ix0_i >= ix1_i || iy0_i >= iy1_i)
|
||||
{
|
||||
hb_raster_draw_recycle_image (rdr, mask_img);
|
||||
new_clip.init_full (w, h);
|
||||
new_clip.is_rect = true;
|
||||
new_clip.rect_x0 = new_clip.rect_y0 = 0;
|
||||
new_clip.rect_x1 = new_clip.rect_y1 = 0;
|
||||
new_clip.min_x = new_clip.min_y = new_clip.max_x = new_clip.max_y = 0;
|
||||
c->clip_stack.push (std::move (new_clip));
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned ix0 = (unsigned) ix0_i;
|
||||
unsigned iy0 = (unsigned) iy0_i;
|
||||
unsigned ix1 = (unsigned) ix1_i;
|
||||
unsigned iy1 = (unsigned) iy1_i;
|
||||
|
||||
new_clip.min_x = w; new_clip.min_y = h;
|
||||
new_clip.max_x = 0; new_clip.max_y = 0;
|
||||
|
||||
if (old_clip.is_rect)
|
||||
{
|
||||
for (unsigned y = iy0; y < iy1; y++)
|
||||
{
|
||||
const uint8_t *mask_row = mask_buf + (unsigned) ((int) y - mask_y0) * mask_ext.stride;
|
||||
uint8_t *out_row = new_clip.alpha.arrayZ + y * new_clip.stride;
|
||||
unsigned row_min = ix1;
|
||||
unsigned row_max = ix0;
|
||||
unsigned mx = (unsigned) ((int) ix0 - mask_x0);
|
||||
for (unsigned x = ix0; x < ix1; x++)
|
||||
{
|
||||
uint8_t a = mask_row[mx++];
|
||||
out_row[x] = a;
|
||||
if (a && row_min == ix1)
|
||||
{
|
||||
row_min = x;
|
||||
row_max = x + 1;
|
||||
}
|
||||
else if (a)
|
||||
row_max = x + 1;
|
||||
}
|
||||
if (row_min < row_max)
|
||||
{
|
||||
new_clip.min_x = hb_min (new_clip.min_x, row_min);
|
||||
new_clip.min_y = hb_min (new_clip.min_y, y);
|
||||
new_clip.max_x = hb_max (new_clip.max_x, row_max);
|
||||
new_clip.max_y = hb_max (new_clip.max_y, y + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (unsigned y = iy0; y < iy1; y++)
|
||||
{
|
||||
const uint8_t *old_row = old_clip.alpha.arrayZ + y * old_clip.stride;
|
||||
const uint8_t *mask_row = mask_buf + (unsigned) ((int) y - mask_y0) * mask_ext.stride;
|
||||
uint8_t *out_row = new_clip.alpha.arrayZ + y * new_clip.stride;
|
||||
unsigned row_min = ix1;
|
||||
unsigned row_max = ix0;
|
||||
for (unsigned x = ix0; x < ix1; x++)
|
||||
{
|
||||
unsigned mx = (unsigned) ((int) x - mask_x0);
|
||||
uint8_t a = hb_raster_div255 (mask_row[mx] * old_row[x]);
|
||||
out_row[x] = a;
|
||||
if (a)
|
||||
{
|
||||
row_min = hb_min (row_min, x);
|
||||
row_max = x + 1;
|
||||
}
|
||||
}
|
||||
if (row_min < row_max)
|
||||
{
|
||||
new_clip.min_x = hb_min (new_clip.min_x, row_min);
|
||||
new_clip.min_y = hb_min (new_clip.min_y, y);
|
||||
new_clip.max_x = hb_max (new_clip.max_x, row_max);
|
||||
new_clip.max_y = hb_max (new_clip.max_y, y + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hb_raster_draw_recycle_image (rdr, mask_img);
|
||||
c->clip_stack.push (std::move (new_clip));
|
||||
}
|
||||
|
||||
static void
|
||||
hb_raster_paint_push_clip_rectangle (hb_paint_funcs_t *pfuncs HB_UNUSED,
|
||||
void *paint_data,
|
||||
@@ -723,7 +707,12 @@ hb_raster_paint_image (hb_paint_funcs_t *pfuncs HB_UNUSED,
|
||||
|
||||
ensure_initialized (c);
|
||||
|
||||
/* Only handle raw BGRA32 for now */
|
||||
/* Handle SVG format */
|
||||
if (format == HB_PAINT_IMAGE_FORMAT_SVG)
|
||||
return hb_raster_svg_render (c, blob, c->svg_glyph, c->svg_font,
|
||||
c->svg_palette, c->foreground);
|
||||
|
||||
/* Only handle raw BGRA32 otherwise */
|
||||
if (format != HB_TAG ('B','G','R','A'))
|
||||
return false;
|
||||
|
||||
@@ -1570,11 +1559,17 @@ done:
|
||||
|
||||
static hb_bool_t
|
||||
hb_raster_paint_custom_palette_color (hb_paint_funcs_t *funcs HB_UNUSED,
|
||||
void *paint_data HB_UNUSED,
|
||||
unsigned int color_index HB_UNUSED,
|
||||
hb_color_t *color HB_UNUSED,
|
||||
void *paint_data,
|
||||
unsigned int color_index,
|
||||
hb_color_t *color,
|
||||
void *user_data HB_UNUSED)
|
||||
{
|
||||
hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data;
|
||||
if (likely (c->custom_palette && hb_map_has (c->custom_palette, color_index)))
|
||||
{
|
||||
*color = hb_map_get (c->custom_palette, color_index);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1682,6 +1677,7 @@ void
|
||||
hb_raster_paint_destroy (hb_raster_paint_t *paint)
|
||||
{
|
||||
if (!hb_object_destroy (paint)) return;
|
||||
hb_map_destroy (paint->custom_palette);
|
||||
hb_raster_draw_destroy (paint->clip_rdr);
|
||||
for (auto *s : paint->surface_stack)
|
||||
hb_raster_image_destroy (s);
|
||||
@@ -1967,6 +1963,49 @@ hb_raster_paint_set_foreground (hb_raster_paint_t *paint,
|
||||
paint->foreground = foreground;
|
||||
}
|
||||
|
||||
/**
|
||||
* hb_raster_paint_clear_custom_palette_colors:
|
||||
* @paint: a paint context
|
||||
*
|
||||
* Clears all custom palette color overrides.
|
||||
*
|
||||
* XSince: REPLACEME
|
||||
**/
|
||||
void
|
||||
hb_raster_paint_clear_custom_palette_colors (hb_raster_paint_t *paint)
|
||||
{
|
||||
if (paint->custom_palette)
|
||||
hb_map_clear (paint->custom_palette);
|
||||
}
|
||||
|
||||
/**
|
||||
* hb_raster_paint_set_custom_palette_color:
|
||||
* @paint: a paint context
|
||||
* @color_index: palette color index to override
|
||||
* @color: replacement color for @color_index
|
||||
*
|
||||
* Sets a custom palette color override. During paint operations,
|
||||
* when the font requests @color_index from CPAL, @color is used.
|
||||
*
|
||||
* Return value: `true` if the override was set; `false` on allocation failure.
|
||||
*
|
||||
* XSince: REPLACEME
|
||||
**/
|
||||
hb_bool_t
|
||||
hb_raster_paint_set_custom_palette_color (hb_raster_paint_t *paint,
|
||||
unsigned int color_index,
|
||||
hb_color_t color)
|
||||
{
|
||||
if (unlikely (!paint->custom_palette))
|
||||
{
|
||||
paint->custom_palette = hb_map_create ();
|
||||
if (unlikely (!paint->custom_palette))
|
||||
return false;
|
||||
}
|
||||
hb_map_set (paint->custom_palette, color_index, color);
|
||||
return hb_map_allocation_successful (paint->custom_palette);
|
||||
}
|
||||
|
||||
/**
|
||||
* hb_raster_paint_get_funcs:
|
||||
*
|
||||
@@ -2033,9 +2072,15 @@ hb_raster_paint_glyph (hb_raster_paint_t *paint,
|
||||
}
|
||||
|
||||
hb_raster_paint_set_transform (paint, xx, yx, xy, yy, tx, ty);
|
||||
paint->svg_glyph = glyph;
|
||||
paint->svg_font = font;
|
||||
paint->svg_palette = palette;
|
||||
hb_bool_t ret = hb_font_paint_glyph_or_fail (font, glyph,
|
||||
hb_raster_paint_get_funcs (), paint,
|
||||
palette, foreground);
|
||||
paint->svg_glyph = 0;
|
||||
paint->svg_font = nullptr;
|
||||
paint->svg_palette = 0;
|
||||
hb_raster_paint_set_transform (paint, xx, yx, xy, yy, dx, dy);
|
||||
return ret;
|
||||
}
|
||||
@@ -2122,6 +2167,7 @@ hb_raster_paint_reset (hb_raster_paint_t *paint)
|
||||
paint->fixed_extents = {};
|
||||
paint->has_extents = false;
|
||||
paint->foreground = HB_COLOR (0, 0, 0, 255);
|
||||
hb_raster_paint_clear_custom_palette_colors (paint);
|
||||
paint->transform_stack.clear ();
|
||||
paint->release_all_clips ();
|
||||
for (auto *s : paint->surface_stack)
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* Copyright © 2026 Behdad Esfahbod
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Author(s): Behdad Esfahbod
|
||||
*/
|
||||
|
||||
#ifndef HB_RASTER_PAINT_HH
|
||||
#define HB_RASTER_PAINT_HH
|
||||
|
||||
#include "hb.hh"
|
||||
|
||||
#include "hb-raster-image.hh"
|
||||
#include "hb-geometry.hh"
|
||||
|
||||
/* hb_raster_clip_t — alpha mask for clipping */
|
||||
struct hb_raster_clip_t
|
||||
{
|
||||
hb_vector_t<uint8_t> alpha; /* A8 mask, same extents as root surface */
|
||||
unsigned width = 0;
|
||||
unsigned height = 0;
|
||||
unsigned stride = 0;
|
||||
|
||||
/* Fast path: simple rectangle (no alpha buffer needed) */
|
||||
bool is_rect = true;
|
||||
int rect_x0 = 0, rect_y0 = 0;
|
||||
int rect_x1 = 0, rect_y1 = 0;
|
||||
|
||||
/* Bounding box of non-zero alpha region (valid for both rect and mask) */
|
||||
unsigned min_x = 0, min_y = 0;
|
||||
unsigned max_x = 0, max_y = 0;
|
||||
|
||||
void init_full (unsigned w, unsigned h)
|
||||
{
|
||||
width = w;
|
||||
height = h;
|
||||
stride = (w + 3u) & ~3u;
|
||||
is_rect = true;
|
||||
rect_x0 = 0;
|
||||
rect_y0 = 0;
|
||||
rect_x1 = (int) w;
|
||||
rect_y1 = (int) h;
|
||||
min_x = 0;
|
||||
min_y = 0;
|
||||
max_x = w;
|
||||
max_y = h;
|
||||
}
|
||||
|
||||
void update_bounds_from_rect ()
|
||||
{
|
||||
min_x = (unsigned) hb_max (rect_x0, 0);
|
||||
min_y = (unsigned) hb_max (rect_y0, 0);
|
||||
max_x = (unsigned) hb_max (hb_min (rect_x1, (int) width), 0);
|
||||
max_y = (unsigned) hb_max (hb_min (rect_y1, (int) height), 0);
|
||||
}
|
||||
|
||||
uint8_t get_alpha (unsigned x, unsigned y) const
|
||||
{
|
||||
if (is_rect)
|
||||
return ((int) x >= rect_x0 && (int) x < rect_x1 &&
|
||||
(int) y >= rect_y0 && (int) y < rect_y1) ? 255 : 0;
|
||||
if (x >= width || y >= height) return 0;
|
||||
return alpha[y * stride + x];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* hb_raster_paint_t — color glyph paint context */
|
||||
struct hb_raster_paint_t
|
||||
{
|
||||
hb_object_header_t header;
|
||||
|
||||
/* Configuration */
|
||||
hb_transform_t<> base_transform = {1, 0, 0, 1, 0, 0};
|
||||
float x_scale_factor = 1.f;
|
||||
float y_scale_factor = 1.f;
|
||||
hb_raster_extents_t fixed_extents = {};
|
||||
bool has_extents = false;
|
||||
hb_color_t foreground = HB_COLOR (0, 0, 0, 255);
|
||||
hb_map_t *custom_palette = nullptr;
|
||||
|
||||
/* SVG rendering state */
|
||||
hb_codepoint_t svg_glyph = 0;
|
||||
hb_font_t *svg_font = nullptr;
|
||||
unsigned svg_palette = 0;
|
||||
|
||||
/* Stacks */
|
||||
hb_vector_t<hb_transform_t<>> transform_stack;
|
||||
hb_vector_t<hb_raster_clip_t> clip_stack;
|
||||
hb_vector_t<hb_raster_clip_t> clip_cache;
|
||||
hb_vector_t<hb_raster_image_t *> surface_stack;
|
||||
|
||||
/* Cached surface pool (freelist for reuse across push/pop group) */
|
||||
hb_vector_t<hb_raster_image_t *> surface_cache;
|
||||
hb_vector_t<hb_color_stop_t> scratch_color_stops;
|
||||
|
||||
/* Internal rasterizer for clip-to-glyph */
|
||||
hb_raster_draw_t *clip_rdr = nullptr;
|
||||
|
||||
/* Helpers */
|
||||
|
||||
hb_raster_image_t *acquire_surface ()
|
||||
{
|
||||
hb_raster_image_t *img;
|
||||
if (surface_cache.length)
|
||||
img = surface_cache.pop ();
|
||||
else
|
||||
{
|
||||
img = hb_raster_image_create_or_fail ();
|
||||
if (unlikely (!img)) return nullptr;
|
||||
}
|
||||
|
||||
if (unlikely (!img->configure (HB_RASTER_FORMAT_BGRA32, fixed_extents)))
|
||||
{
|
||||
hb_raster_image_destroy (img);
|
||||
return nullptr;
|
||||
}
|
||||
img->clear ();
|
||||
return img;
|
||||
}
|
||||
|
||||
void release_surface (hb_raster_image_t *img)
|
||||
{
|
||||
surface_cache.push (img);
|
||||
}
|
||||
|
||||
hb_raster_clip_t acquire_clip (unsigned w, unsigned h)
|
||||
{
|
||||
hb_raster_clip_t clip;
|
||||
if (clip_cache.length)
|
||||
clip = clip_cache.pop ();
|
||||
clip.width = w;
|
||||
clip.height = h;
|
||||
clip.stride = (w + 3u) & ~3u;
|
||||
clip.is_rect = false;
|
||||
return clip;
|
||||
}
|
||||
|
||||
void release_clip (hb_raster_clip_t &&clip)
|
||||
{
|
||||
if (clip.alpha.arrayZ)
|
||||
clip_cache.push (std::move (clip));
|
||||
}
|
||||
|
||||
void release_all_clips ()
|
||||
{
|
||||
while (clip_stack.length)
|
||||
release_clip (clip_stack.pop ());
|
||||
}
|
||||
|
||||
hb_raster_image_t *current_surface ()
|
||||
{
|
||||
return surface_stack.length ? surface_stack.tail () : nullptr;
|
||||
}
|
||||
|
||||
hb_raster_clip_t ¤t_clip ()
|
||||
{
|
||||
return clip_stack.tail ();
|
||||
}
|
||||
|
||||
hb_transform_t<> ¤t_transform ()
|
||||
{
|
||||
return transform_stack.tail ();
|
||||
}
|
||||
|
||||
void apply_scale_factor (hb_transform_t<> &t) const
|
||||
{
|
||||
t.xx /= x_scale_factor;
|
||||
t.xy /= x_scale_factor;
|
||||
t.x0 /= x_scale_factor;
|
||||
t.yx /= y_scale_factor;
|
||||
t.yy /= y_scale_factor;
|
||||
t.y0 /= y_scale_factor;
|
||||
}
|
||||
|
||||
hb_transform_t<> current_effective_transform ()
|
||||
{
|
||||
hb_transform_t<> t = current_transform ();
|
||||
apply_scale_factor (t);
|
||||
return t;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif /* HB_RASTER_PAINT_HH */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright © 2026 Behdad Esfahbod
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Author(s): Behdad Esfahbod
|
||||
*/
|
||||
|
||||
#ifndef HB_RASTER_SVG_HH
|
||||
#define HB_RASTER_SVG_HH
|
||||
|
||||
#include "hb.hh"
|
||||
|
||||
struct hb_raster_paint_t;
|
||||
|
||||
/* Callback type: emits SVG path draw commands to the given draw funcs */
|
||||
typedef void (*hb_raster_svg_path_func_t) (hb_draw_funcs_t *dfuncs,
|
||||
void *draw_data,
|
||||
void *user_data);
|
||||
|
||||
/* Push clip from arbitrary path (reuses push_clip_glyph logic) */
|
||||
HB_INTERNAL void
|
||||
hb_raster_paint_push_clip_path (hb_raster_paint_t *c,
|
||||
hb_raster_svg_path_func_t func,
|
||||
void *user_data);
|
||||
|
||||
/* Render SVG document for a specific glyph */
|
||||
HB_INTERNAL hb_bool_t
|
||||
hb_raster_svg_render (hb_raster_paint_t *paint,
|
||||
hb_blob_t *blob,
|
||||
hb_codepoint_t glyph,
|
||||
hb_font_t *font,
|
||||
unsigned palette,
|
||||
hb_color_t foreground);
|
||||
|
||||
|
||||
#endif /* HB_RASTER_SVG_HH */
|
||||
@@ -278,6 +278,14 @@ HB_EXTERN void
|
||||
hb_raster_paint_set_foreground (hb_raster_paint_t *paint,
|
||||
hb_color_t foreground);
|
||||
|
||||
HB_EXTERN void
|
||||
hb_raster_paint_clear_custom_palette_colors (hb_raster_paint_t *paint);
|
||||
|
||||
HB_EXTERN hb_bool_t
|
||||
hb_raster_paint_set_custom_palette_color (hb_raster_paint_t *paint,
|
||||
unsigned int color_index,
|
||||
hb_color_t color);
|
||||
|
||||
HB_EXTERN hb_paint_funcs_t *
|
||||
hb_raster_paint_get_funcs (void);
|
||||
|
||||
|
||||
+14
-486
@@ -26,7 +26,9 @@
|
||||
|
||||
#include "hb.hh"
|
||||
|
||||
#include "hb-face.hh"
|
||||
#include "hb-vector-svg-subset.hh"
|
||||
#include "OT/Color/svg/svg.hh"
|
||||
#include "hb-vector-svg-utils.hh"
|
||||
#include "hb-map.hh"
|
||||
#include "hb-ot-color.h"
|
||||
@@ -35,74 +37,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
struct hb_svg_id_span_t
|
||||
{
|
||||
const char *p;
|
||||
unsigned len;
|
||||
|
||||
bool operator == (const hb_svg_id_span_t &o) const
|
||||
{
|
||||
return len == o.len && !memcmp (p, o.p, len);
|
||||
}
|
||||
|
||||
uint32_t hash () const
|
||||
{
|
||||
uint32_t h = hb_hash (len);
|
||||
for (unsigned i = 0; i < len; i++)
|
||||
h = h * 33u + (unsigned char) p[i];
|
||||
return h;
|
||||
}
|
||||
};
|
||||
|
||||
struct hb_svg_defs_entry_t
|
||||
{
|
||||
hb_svg_id_span_t id;
|
||||
unsigned start;
|
||||
unsigned end;
|
||||
};
|
||||
|
||||
struct hb_svg_glyph_entry_t
|
||||
{
|
||||
hb_codepoint_t glyph;
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
};
|
||||
|
||||
struct hb_svg_doc_cache_t
|
||||
{
|
||||
hb_blob_t *blob = nullptr;
|
||||
const char *svg = nullptr;
|
||||
unsigned len = 0;
|
||||
hb_vector_t<hb_svg_defs_entry_t> defs_entries;
|
||||
hb_codepoint_t start_glyph = HB_CODEPOINT_INVALID;
|
||||
hb_codepoint_t end_glyph = HB_CODEPOINT_INVALID;
|
||||
hb_vector_t<hb_pair_t<uint32_t, uint32_t>> glyph_spans;
|
||||
};
|
||||
|
||||
using hb_svg_doc_t = hb_atomic_t<hb_svg_doc_cache_t *>;
|
||||
|
||||
struct hb_svg_face_cache_t
|
||||
{
|
||||
hb_vector_t<hb_svg_doc_t> slots;
|
||||
};
|
||||
|
||||
static hb_user_data_key_t hb_svg_face_cache_user_data_key;
|
||||
|
||||
static int
|
||||
hb_svg_find_substr (const char *s,
|
||||
unsigned n,
|
||||
unsigned from,
|
||||
const char *needle,
|
||||
unsigned needle_len)
|
||||
{
|
||||
if (!needle_len || from >= n || needle_len > n)
|
||||
return -1;
|
||||
for (unsigned i = from; i + needle_len <= n; i++)
|
||||
if (s[i] == needle[0] && !memcmp (s + i, needle, needle_len))
|
||||
return (int) i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool
|
||||
hb_svg_append_with_prefix (hb_vector_t<char> *out,
|
||||
const char *s,
|
||||
@@ -229,13 +163,13 @@ hb_svg_append_with_prefix (hb_vector_t<char> *out,
|
||||
}
|
||||
|
||||
static bool
|
||||
hb_svg_add_unique_id (hb_vector_t<hb_svg_id_span_t> *v,
|
||||
hb_hashmap_t<hb_svg_id_span_t, hb_bool_t> *seen_ids,
|
||||
hb_svg_add_unique_id (hb_vector_t<OT::SVG::svg_id_span_t> *v,
|
||||
hb_hashmap_t<OT::SVG::svg_id_span_t, hb_bool_t> *seen_ids,
|
||||
const char *p,
|
||||
unsigned n)
|
||||
{
|
||||
if (!n) return false;
|
||||
hb_svg_id_span_t key = {p, n};
|
||||
OT::SVG::svg_id_span_t key = {p, n};
|
||||
if (seen_ids->has (key))
|
||||
return false;
|
||||
if (unlikely (!seen_ids->set (key, true)))
|
||||
@@ -249,8 +183,8 @@ hb_svg_add_unique_id (hb_vector_t<hb_svg_id_span_t> *v,
|
||||
static bool
|
||||
hb_svg_collect_refs (const char *s,
|
||||
unsigned n,
|
||||
hb_vector_t<hb_svg_id_span_t> *ids,
|
||||
hb_hashmap_t<hb_svg_id_span_t, hb_bool_t> *seen_ids)
|
||||
hb_vector_t<OT::SVG::svg_id_span_t> *ids,
|
||||
hb_hashmap_t<OT::SVG::svg_id_span_t, hb_bool_t> *seen_ids)
|
||||
{
|
||||
unsigned i = 0;
|
||||
while (i < n)
|
||||
@@ -316,403 +250,6 @@ hb_svg_collect_refs (const char *s,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
hb_svg_parse_id_in_start_tag (const char *svg,
|
||||
unsigned tag_start,
|
||||
unsigned tag_end,
|
||||
hb_svg_id_span_t *id)
|
||||
{
|
||||
unsigned p = tag_start;
|
||||
while (p + 4 <= tag_end)
|
||||
{
|
||||
if (!memcmp (svg + p, "id=\"", 4))
|
||||
{
|
||||
unsigned b = p + 4;
|
||||
unsigned e = b;
|
||||
while (e < tag_end && svg[e] != '"') e++;
|
||||
if (e <= tag_end && e > b)
|
||||
{
|
||||
*id = {svg + b, e - b};
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (!memcmp (svg + p, "id='", 4))
|
||||
{
|
||||
unsigned b = p + 4;
|
||||
unsigned e = b;
|
||||
while (e < tag_end && svg[e] != '\'') e++;
|
||||
if (e <= tag_end && e > b)
|
||||
{
|
||||
*id = {svg + b, e - b};
|
||||
return true;
|
||||
}
|
||||
}
|
||||
p++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
struct hb_svg_open_elem_t
|
||||
{
|
||||
unsigned start;
|
||||
hb_svg_id_span_t id;
|
||||
bool in_defs_content;
|
||||
bool is_defs;
|
||||
};
|
||||
|
||||
static const unsigned HB_SVG_PARSE_MAX_DEPTH = 128;
|
||||
|
||||
static bool
|
||||
hb_svg_parse_glyph_id_span (const hb_svg_id_span_t &id,
|
||||
hb_codepoint_t *glyph)
|
||||
{
|
||||
if (id.len <= 5 || memcmp (id.p, "glyph", 5))
|
||||
return false;
|
||||
|
||||
hb_codepoint_t gid = 0;
|
||||
for (unsigned i = 5; i < id.len; i++)
|
||||
{
|
||||
unsigned char c = (unsigned char) id.p[i];
|
||||
if (c < '0' || c > '9')
|
||||
return false;
|
||||
hb_codepoint_t digit = (hb_codepoint_t) (c - '0');
|
||||
if (unlikely (gid > HB_CODEPOINT_INVALID / 10 ||
|
||||
(gid == HB_CODEPOINT_INVALID / 10 &&
|
||||
digit > HB_CODEPOINT_INVALID % 10)))
|
||||
return false;
|
||||
gid = (hb_codepoint_t) (gid * 10 + digit);
|
||||
}
|
||||
|
||||
*glyph = gid;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
hb_svg_parse_cache_entries_linear (const char *svg,
|
||||
unsigned len,
|
||||
hb_vector_t<hb_svg_defs_entry_t> *defs_entries,
|
||||
hb_vector_t<hb_svg_glyph_entry_t> *glyph_spans)
|
||||
{
|
||||
hb_svg_open_elem_t stack[HB_SVG_PARSE_MAX_DEPTH];
|
||||
unsigned depth = 0;
|
||||
defs_entries->alloc (256);
|
||||
|
||||
unsigned defs_depth = 0;
|
||||
unsigned i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
if (svg[i] != '<')
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i + 4 <= len && !memcmp (svg + i, "<!--", 4))
|
||||
{
|
||||
int cend = hb_svg_find_substr (svg, len, i + 4, "-->", 3);
|
||||
if (cend < 0) return false;
|
||||
i = (unsigned) cend + 3;
|
||||
continue;
|
||||
}
|
||||
if (i + 9 <= len && !memcmp (svg + i, "<![CDATA[", 9))
|
||||
{
|
||||
int cend = hb_svg_find_substr (svg, len, i + 9, "]]>", 3);
|
||||
if (cend < 0) return false;
|
||||
i = (unsigned) cend + 3;
|
||||
continue;
|
||||
}
|
||||
|
||||
bool closing = (i + 1 < len && svg[i + 1] == '/');
|
||||
bool special = (i + 1 < len && (svg[i + 1] == '!' || svg[i + 1] == '?'));
|
||||
|
||||
unsigned gt = i + 1;
|
||||
char quote = 0;
|
||||
while (gt < len)
|
||||
{
|
||||
char c = svg[gt];
|
||||
if (quote)
|
||||
{
|
||||
if (c == quote) quote = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (c == '"' || c == '\'')
|
||||
quote = c;
|
||||
else if (c == '>')
|
||||
break;
|
||||
}
|
||||
gt++;
|
||||
}
|
||||
if (gt >= len)
|
||||
return false;
|
||||
|
||||
if (special)
|
||||
{
|
||||
i = gt + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
unsigned p = i + (closing ? 2 : 1);
|
||||
while (p < gt && isspace ((unsigned char) svg[p])) p++;
|
||||
const char *name = svg + p;
|
||||
unsigned name_len = 0;
|
||||
while (p + name_len < gt)
|
||||
{
|
||||
unsigned char c = (unsigned char) name[name_len];
|
||||
if (!(isalnum (c) || c == '_' || c == '-' || c == ':'))
|
||||
break;
|
||||
name_len++;
|
||||
}
|
||||
bool is_defs = (name_len == 4 && !memcmp (name, "defs", 4));
|
||||
|
||||
if (closing)
|
||||
{
|
||||
if (!depth)
|
||||
{
|
||||
i = gt + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
hb_svg_open_elem_t e = stack[--depth];
|
||||
unsigned end = gt + 1;
|
||||
|
||||
if (e.id.len)
|
||||
{
|
||||
if (e.in_defs_content)
|
||||
{
|
||||
auto *slot = defs_entries->push ();
|
||||
if (unlikely (!slot))
|
||||
return false;
|
||||
slot->id = e.id;
|
||||
slot->start = e.start;
|
||||
slot->end = end;
|
||||
}
|
||||
|
||||
hb_codepoint_t gid;
|
||||
if (hb_svg_parse_glyph_id_span (e.id, &gid))
|
||||
{
|
||||
auto *span = glyph_spans->push ();
|
||||
if (unlikely (!span))
|
||||
return false;
|
||||
span->glyph = gid;
|
||||
span->start = (uint32_t) e.start;
|
||||
span->end = (uint32_t) end;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.is_defs && defs_depth)
|
||||
defs_depth--;
|
||||
|
||||
i = end;
|
||||
continue;
|
||||
}
|
||||
|
||||
hb_svg_id_span_t id = {nullptr, 0};
|
||||
hb_svg_parse_id_in_start_tag (svg, i, gt, &id);
|
||||
|
||||
unsigned r = gt;
|
||||
while (r > i && isspace ((unsigned char) svg[r - 1])) r--;
|
||||
bool self_closing = (r > i && svg[r - 1] == '/');
|
||||
|
||||
hb_svg_open_elem_t e = {i, id, defs_depth > 0, is_defs};
|
||||
|
||||
if (self_closing)
|
||||
{
|
||||
unsigned end = gt + 1;
|
||||
if (e.id.len)
|
||||
{
|
||||
if (e.in_defs_content)
|
||||
{
|
||||
auto *slot = defs_entries->push ();
|
||||
if (unlikely (!slot))
|
||||
return false;
|
||||
slot->id = e.id;
|
||||
slot->start = e.start;
|
||||
slot->end = end;
|
||||
}
|
||||
|
||||
hb_codepoint_t gid;
|
||||
if (hb_svg_parse_glyph_id_span (e.id, &gid))
|
||||
{
|
||||
auto *span = glyph_spans->push ();
|
||||
if (unlikely (!span))
|
||||
return false;
|
||||
span->glyph = gid;
|
||||
span->start = (uint32_t) e.start;
|
||||
span->end = (uint32_t) end;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (unlikely (depth >= HB_SVG_PARSE_MAX_DEPTH))
|
||||
return false;
|
||||
stack[depth++] = e;
|
||||
if (is_defs)
|
||||
defs_depth++;
|
||||
}
|
||||
|
||||
i = gt + 1;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_doc_cache_destroy (hb_svg_doc_cache_t *doc)
|
||||
{
|
||||
if (!doc)
|
||||
return;
|
||||
doc->glyph_spans.fini ();
|
||||
doc->defs_entries.fini ();
|
||||
hb_blob_destroy (doc->blob);
|
||||
hb_free (doc);
|
||||
}
|
||||
|
||||
static void
|
||||
hb_svg_face_cache_destroy (void *data)
|
||||
{
|
||||
auto *face_cache = (hb_svg_face_cache_t *) data;
|
||||
if (!face_cache)
|
||||
return;
|
||||
for (unsigned i = 0; i < face_cache->slots.length; i++)
|
||||
hb_svg_doc_cache_destroy (face_cache->slots.arrayZ[i].get_relaxed ());
|
||||
face_cache->slots.fini ();
|
||||
hb_free (face_cache);
|
||||
}
|
||||
|
||||
static hb_svg_doc_cache_t *
|
||||
hb_svg_make_doc_cache (hb_blob_t *image,
|
||||
const char *svg,
|
||||
unsigned len,
|
||||
hb_codepoint_t start_glyph,
|
||||
hb_codepoint_t end_glyph)
|
||||
{
|
||||
static const uint32_t INVALID_SPAN = 0xFFFFFFFFu;
|
||||
|
||||
auto *doc = (hb_svg_doc_cache_t *) hb_malloc (sizeof (hb_svg_doc_cache_t));
|
||||
if (!doc)
|
||||
return nullptr;
|
||||
doc->blob = nullptr;
|
||||
doc->svg = nullptr;
|
||||
doc->len = 0;
|
||||
doc->defs_entries.init ();
|
||||
doc->start_glyph = HB_CODEPOINT_INVALID;
|
||||
doc->end_glyph = HB_CODEPOINT_INVALID;
|
||||
doc->glyph_spans.init ();
|
||||
|
||||
doc->blob = hb_blob_reference (image);
|
||||
doc->svg = svg;
|
||||
doc->len = len;
|
||||
doc->start_glyph = start_glyph;
|
||||
doc->end_glyph = end_glyph;
|
||||
|
||||
if (unlikely (start_glyph == HB_CODEPOINT_INVALID || end_glyph < start_glyph))
|
||||
{
|
||||
hb_svg_doc_cache_destroy (doc);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
unsigned glyph_count = end_glyph - start_glyph + 1;
|
||||
if (!doc->glyph_spans.resize ((int) glyph_count))
|
||||
{
|
||||
hb_svg_doc_cache_destroy (doc);
|
||||
return nullptr;
|
||||
}
|
||||
for (unsigned i = 0; i < glyph_count; i++)
|
||||
doc->glyph_spans.arrayZ[i] = hb_pair_t<uint32_t, uint32_t> (INVALID_SPAN, INVALID_SPAN);
|
||||
|
||||
hb_vector_t<hb_svg_glyph_entry_t> glyph_spans;
|
||||
glyph_spans.init ();
|
||||
if (!hb_svg_parse_cache_entries_linear (svg, len,
|
||||
&doc->defs_entries,
|
||||
&glyph_spans))
|
||||
{
|
||||
glyph_spans.fini ();
|
||||
hb_svg_doc_cache_destroy (doc);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < glyph_spans.length; i++)
|
||||
{
|
||||
const auto &span = glyph_spans.arrayZ[i];
|
||||
if (unlikely (span.glyph < start_glyph || span.glyph > end_glyph))
|
||||
continue;
|
||||
doc->glyph_spans.arrayZ[span.glyph - start_glyph] = hb_pair_t<uint32_t, uint32_t> (span.start, span.end);
|
||||
}
|
||||
|
||||
glyph_spans.fini ();
|
||||
return doc;
|
||||
}
|
||||
|
||||
static hb_svg_face_cache_t *
|
||||
hb_svg_get_or_make_face_cache (hb_face_t *face)
|
||||
{
|
||||
if (!face)
|
||||
return nullptr;
|
||||
|
||||
auto *face_cache = (hb_svg_face_cache_t *) hb_face_get_user_data (face, &hb_svg_face_cache_user_data_key);
|
||||
if (face_cache)
|
||||
return face_cache;
|
||||
|
||||
face_cache = (hb_svg_face_cache_t *) hb_calloc (1, sizeof (hb_svg_face_cache_t));
|
||||
if (!face_cache)
|
||||
return nullptr;
|
||||
|
||||
unsigned doc_count = hb_ot_color_get_svg_document_count (face);
|
||||
if (doc_count && !face_cache->slots.resize (doc_count))
|
||||
{
|
||||
face_cache->slots.fini ();
|
||||
hb_free (face_cache);
|
||||
return nullptr;
|
||||
}
|
||||
for (unsigned i = 0; i < doc_count; i++)
|
||||
face_cache->slots.arrayZ[i].set_relaxed (nullptr);
|
||||
|
||||
if (!hb_face_set_user_data (face,
|
||||
&hb_svg_face_cache_user_data_key,
|
||||
face_cache,
|
||||
hb_svg_face_cache_destroy,
|
||||
false))
|
||||
{
|
||||
face_cache->slots.fini ();
|
||||
hb_free (face_cache);
|
||||
face_cache = (hb_svg_face_cache_t *) hb_face_get_user_data (face, &hb_svg_face_cache_user_data_key);
|
||||
}
|
||||
|
||||
return face_cache;
|
||||
}
|
||||
|
||||
static hb_svg_doc_cache_t *
|
||||
hb_svg_get_or_make_doc_cache (hb_face_t *face,
|
||||
hb_blob_t *image,
|
||||
const char *svg,
|
||||
unsigned len,
|
||||
unsigned doc_index,
|
||||
hb_codepoint_t start_glyph,
|
||||
hb_codepoint_t end_glyph)
|
||||
{
|
||||
auto *face_cache = hb_svg_get_or_make_face_cache (face);
|
||||
if (!face_cache || doc_index >= face_cache->slots.length)
|
||||
return nullptr;
|
||||
|
||||
auto &slot = face_cache->slots.arrayZ[doc_index];
|
||||
auto *doc = slot.get_acquire ();
|
||||
if (doc)
|
||||
return doc;
|
||||
|
||||
auto *fresh = hb_svg_make_doc_cache (image, svg, len, start_glyph, end_glyph);
|
||||
if (!fresh)
|
||||
return nullptr;
|
||||
|
||||
auto *expected = (hb_svg_doc_cache_t *) nullptr;
|
||||
if (slot.cmpexch (expected, fresh))
|
||||
return fresh;
|
||||
|
||||
hb_svg_doc_cache_destroy (fresh);
|
||||
return expected;
|
||||
}
|
||||
|
||||
bool
|
||||
hb_svg_subset_glyph_image (hb_face_t *face,
|
||||
hb_blob_t *image,
|
||||
@@ -738,30 +275,21 @@ hb_svg_subset_glyph_image (hb_face_t *face,
|
||||
if (!hb_ot_color_get_svg_document_glyph_range (face, doc_index, &start_glyph, &end_glyph))
|
||||
return false;
|
||||
|
||||
auto *doc_cache = hb_svg_get_or_make_doc_cache (face, image, svg, len,
|
||||
doc_index, start_glyph, end_glyph);
|
||||
auto *doc_cache = face->table.SVG->get_or_create_doc_cache (image, svg, len,
|
||||
doc_index, start_glyph, end_glyph);
|
||||
if (!doc_cache)
|
||||
return false;
|
||||
svg = doc_cache->svg;
|
||||
len = doc_cache->len;
|
||||
svg = face->table.SVG->doc_cache_get_svg (doc_cache, &len);
|
||||
|
||||
unsigned glyph_start = 0, glyph_end = 0;
|
||||
static const uint32_t INVALID_SPAN = 0xFFFFFFFFu;
|
||||
if (doc_cache->start_glyph == HB_CODEPOINT_INVALID ||
|
||||
glyph < doc_cache->start_glyph ||
|
||||
glyph > doc_cache->end_glyph)
|
||||
if (!face->table.SVG->doc_cache_get_glyph_span (doc_cache, glyph, &glyph_start, &glyph_end))
|
||||
return false;
|
||||
const auto &span = doc_cache->glyph_spans.arrayZ[glyph - doc_cache->start_glyph];
|
||||
if (span.first == INVALID_SPAN)
|
||||
return false;
|
||||
glyph_start = span.first;
|
||||
glyph_end = span.second;
|
||||
|
||||
auto *defs_entries = &doc_cache->defs_entries;
|
||||
auto *defs_entries = face->table.SVG->doc_cache_get_defs_entries (doc_cache);
|
||||
|
||||
hb_vector_t<hb_svg_id_span_t> needed_ids;
|
||||
hb_vector_t<OT::SVG::svg_id_span_t> needed_ids;
|
||||
needed_ids.alloc (16);
|
||||
hb_hashmap_t<hb_svg_id_span_t, hb_bool_t> needed_ids_set;
|
||||
hb_hashmap_t<OT::SVG::svg_id_span_t, hb_bool_t> needed_ids_set;
|
||||
if (!hb_svg_collect_refs (svg + glyph_start, glyph_end - glyph_start,
|
||||
&needed_ids, &needed_ids_set))
|
||||
return false;
|
||||
|
||||
@@ -706,6 +706,9 @@ hb_raster_sources = [
|
||||
'hb-raster-utils.hh',
|
||||
'hb-raster-draw.cc',
|
||||
'hb-raster-paint.cc',
|
||||
'hb-raster-paint.hh',
|
||||
'hb-raster-svg.cc',
|
||||
'hb-raster-svg.hh',
|
||||
'hb-static.cc',
|
||||
]
|
||||
|
||||
|
||||
@@ -141,7 +141,8 @@ main (int argc, char **argv)
|
||||
hb_font_set_scale (font, font_size, font_size);
|
||||
|
||||
bool has_color = hb_ot_color_has_paint (face) ||
|
||||
hb_ot_color_has_layers (face);
|
||||
hb_ot_color_has_layers (face) ||
|
||||
hb_ot_color_has_svg (face);
|
||||
|
||||
hb_raster_draw_t *rdr = hb_raster_draw_create_or_fail ();
|
||||
hb_raster_paint_t *pnt = has_color ? hb_raster_paint_create_or_fail () : nullptr;
|
||||
|
||||
+33
-2
@@ -66,6 +66,7 @@ struct raster_output_t : output_options_t<true>
|
||||
hb_font_destroy (font);
|
||||
g_free (fore);
|
||||
g_free (back);
|
||||
g_free (custom_palette);
|
||||
}
|
||||
|
||||
void add_options (option_parser_t *parser)
|
||||
@@ -79,6 +80,8 @@ struct raster_output_t : output_options_t<true>
|
||||
{
|
||||
{"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: #FFFFFF)", "rrggbb/rrggbbaa"},
|
||||
{"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: #000000)", "rrggbb/rrggbbaa"},
|
||||
{"font-palette", 0, 0, G_OPTION_ARG_INT, &this->palette, "Set font palette (default: 0)", "index"},
|
||||
{"custom-palette", 0, 0, G_OPTION_ARG_STRING, &this->custom_palette, "Custom palette", "comma-separated colors"},
|
||||
{"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: 0)", "one to four numbers"},
|
||||
{nullptr}
|
||||
};
|
||||
@@ -100,7 +103,8 @@ struct raster_output_t : output_options_t<true>
|
||||
|
||||
hb_face_t *face = hb_font_get_face (font);
|
||||
has_color = hb_ot_color_has_paint (face) ||
|
||||
hb_ot_color_has_layers (face);
|
||||
hb_ot_color_has_layers (face) ||
|
||||
hb_ot_color_has_svg (face);
|
||||
|
||||
/* Parse foreground / background colors */
|
||||
{
|
||||
@@ -126,6 +130,31 @@ struct raster_output_t : output_options_t<true>
|
||||
{
|
||||
pnt = hb_raster_paint_create_or_fail ();
|
||||
hb_raster_paint_set_foreground (pnt, fg_color);
|
||||
hb_raster_paint_clear_custom_palette_colors (pnt);
|
||||
if (custom_palette)
|
||||
{
|
||||
char **entries = g_strsplit (custom_palette, ",", -1);
|
||||
unsigned idx = 0;
|
||||
for (unsigned i = 0; entries[i]; i++)
|
||||
{
|
||||
char *entry = g_strstrip (entries[i]);
|
||||
const char *p = strchr (entry, '=');
|
||||
if (!p)
|
||||
p = entry;
|
||||
else
|
||||
{
|
||||
sscanf (entry, "%u", &idx);
|
||||
p++;
|
||||
}
|
||||
|
||||
unsigned r = 0, g = 0, b = 0, a = 0;
|
||||
if (parse_color (p, r, g, b, a))
|
||||
hb_raster_paint_set_custom_palette_color (pnt, idx,
|
||||
HB_COLOR ((uint8_t) b, (uint8_t) g, (uint8_t) r, (uint8_t) a));
|
||||
idx++;
|
||||
}
|
||||
g_strfreev (entries);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,7 +324,7 @@ struct raster_output_t : output_options_t<true>
|
||||
hb_raster_paint_set_transform (pnt, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f);
|
||||
hb_raster_paint_set_extents (pnt, &ext);
|
||||
|
||||
hb_raster_paint_glyph (pnt, font, g.gid, pen_x, pen_y, 0, fg_color);
|
||||
hb_raster_paint_glyph (pnt, font, g.gid, pen_x, pen_y, palette, fg_color);
|
||||
|
||||
hb_raster_image_t *img = hb_raster_paint_render (pnt);
|
||||
if (img)
|
||||
@@ -460,6 +489,8 @@ struct raster_output_t : output_options_t<true>
|
||||
|
||||
char *fore = nullptr;
|
||||
char *back = nullptr;
|
||||
char *custom_palette = nullptr;
|
||||
int palette = 0;
|
||||
margin_t margin = {0., 0., 0., 0.};
|
||||
hb_color_t fg_color = HB_COLOR (0, 0, 0, 255);
|
||||
uint8_t bg_r = 255, bg_g = 255, bg_b = 255, bg_a = 255;
|
||||
|
||||
Reference in new issue
Block a user