2022-06-26 02:03:15 +02:00
|
|
|
#ifndef OT_GLYF_COMPOSITEGLYPH_HH
|
|
|
|
#define OT_GLYF_COMPOSITEGLYPH_HH
|
|
|
|
|
|
|
|
|
|
|
|
#include "../../hb-open-type.hh"
|
|
|
|
|
|
|
|
|
|
|
|
namespace OT {
|
|
|
|
namespace glyf_impl {
|
|
|
|
|
|
|
|
|
2022-06-26 03:03:30 +02:00
|
|
|
struct CompositeGlyphRecord
|
2022-06-26 02:03:15 +02:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
enum composite_glyph_flag_t
|
|
|
|
{
|
|
|
|
ARG_1_AND_2_ARE_WORDS = 0x0001,
|
|
|
|
ARGS_ARE_XY_VALUES = 0x0002,
|
|
|
|
ROUND_XY_TO_GRID = 0x0004,
|
|
|
|
WE_HAVE_A_SCALE = 0x0008,
|
|
|
|
MORE_COMPONENTS = 0x0020,
|
|
|
|
WE_HAVE_AN_X_AND_Y_SCALE = 0x0040,
|
|
|
|
WE_HAVE_A_TWO_BY_TWO = 0x0080,
|
|
|
|
WE_HAVE_INSTRUCTIONS = 0x0100,
|
|
|
|
USE_MY_METRICS = 0x0200,
|
|
|
|
OVERLAP_COMPOUND = 0x0400,
|
|
|
|
SCALED_COMPONENT_OFFSET = 0x0800,
|
2022-07-12 05:52:25 +02:00
|
|
|
UNSCALED_COMPONENT_OFFSET = 0x1000,
|
2022-07-28 17:53:30 +02:00
|
|
|
#ifndef HB_NO_BEYOND_64K
|
2022-07-12 05:52:25 +02:00
|
|
|
GID_IS_24BIT = 0x2000
|
2022-07-28 17:53:30 +02:00
|
|
|
#endif
|
2022-06-26 02:03:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
unsigned int get_size () const
|
|
|
|
{
|
|
|
|
unsigned int size = min_size;
|
2022-07-12 05:52:25 +02:00
|
|
|
/* glyphIndex is 24bit instead of 16bit */
|
2022-07-28 17:53:30 +02:00
|
|
|
#ifndef HB_NO_BEYOND_64K
|
2022-07-12 05:52:25 +02:00
|
|
|
if (flags & GID_IS_24BIT) size += HBGlyphID24::static_size - HBGlyphID16::static_size;
|
2022-07-28 17:53:30 +02:00
|
|
|
#endif
|
2022-06-26 02:03:15 +02:00
|
|
|
/* arg1 and 2 are int16 */
|
|
|
|
if (flags & ARG_1_AND_2_ARE_WORDS) size += 4;
|
|
|
|
/* arg1 and 2 are int8 */
|
|
|
|
else size += 2;
|
|
|
|
|
|
|
|
/* One x 16 bit (scale) */
|
|
|
|
if (flags & WE_HAVE_A_SCALE) size += 2;
|
|
|
|
/* Two x 16 bit (xscale, yscale) */
|
|
|
|
else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) size += 4;
|
|
|
|
/* Four x 16 bit (xscale, scale01, scale10, yscale) */
|
|
|
|
else if (flags & WE_HAVE_A_TWO_BY_TWO) size += 8;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void drop_instructions_flag () { flags = (uint16_t) flags & ~WE_HAVE_INSTRUCTIONS; }
|
|
|
|
void set_overlaps_flag ()
|
|
|
|
{
|
|
|
|
flags = (uint16_t) flags | OVERLAP_COMPOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_instructions () const { return flags & WE_HAVE_INSTRUCTIONS; }
|
|
|
|
|
|
|
|
bool has_more () const { return flags & MORE_COMPONENTS; }
|
|
|
|
bool is_use_my_metrics () const { return flags & USE_MY_METRICS; }
|
|
|
|
bool is_anchored () const { return !(flags & ARGS_ARE_XY_VALUES); }
|
|
|
|
void get_anchor_points (unsigned int &point1, unsigned int &point2) const
|
|
|
|
{
|
2022-07-12 05:52:25 +02:00
|
|
|
const auto *p = &StructAfter<const HBUINT8> (flags);
|
2022-07-28 17:53:30 +02:00
|
|
|
#ifndef HB_NO_BEYOND_64K
|
2022-07-12 05:52:25 +02:00
|
|
|
if (flags & GID_IS_24BIT)
|
|
|
|
p += HBGlyphID24::static_size;
|
|
|
|
else
|
2022-07-28 17:53:30 +02:00
|
|
|
#endif
|
2022-07-12 05:52:25 +02:00
|
|
|
p += HBGlyphID16::static_size;
|
2022-06-26 02:03:15 +02:00
|
|
|
if (flags & ARG_1_AND_2_ARE_WORDS)
|
|
|
|
{
|
|
|
|
point1 = ((const HBUINT16 *) p)[0];
|
|
|
|
point2 = ((const HBUINT16 *) p)[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
point1 = p[0];
|
|
|
|
point2 = p[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void transform_points (contour_point_vector_t &points) const
|
|
|
|
{
|
|
|
|
float matrix[4];
|
|
|
|
contour_point_t trans;
|
|
|
|
if (get_transformation (matrix, trans))
|
|
|
|
{
|
|
|
|
if (scaled_offsets ())
|
|
|
|
{
|
|
|
|
points.translate (trans);
|
|
|
|
points.transform (matrix);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
points.transform (matrix);
|
|
|
|
points.translate (trans);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 18:37:09 +02:00
|
|
|
void apply_delta_to_offsets (const contour_point_t &p_delta)
|
|
|
|
{
|
|
|
|
HBINT8 *p = &StructAfter<HBINT8> (flags);
|
|
|
|
#ifndef HB_NO_BEYOND_64K
|
|
|
|
if (flags & GID_IS_24BIT)
|
|
|
|
p += HBGlyphID24::static_size;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
p += HBGlyphID16::static_size;
|
|
|
|
|
|
|
|
if (flags & ARG_1_AND_2_ARE_WORDS)
|
|
|
|
{
|
|
|
|
HBINT16 *px = reinterpret_cast<HBINT16 *> (p);
|
|
|
|
px[0] += roundf (p_delta.x);
|
|
|
|
px[1] += roundf (p_delta.y);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p[0] += roundf (p_delta.x);
|
|
|
|
p[1] += roundf (p_delta.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-26 02:03:15 +02:00
|
|
|
protected:
|
|
|
|
bool scaled_offsets () const
|
|
|
|
{ return (flags & (SCALED_COMPONENT_OFFSET | UNSCALED_COMPONENT_OFFSET)) == SCALED_COMPONENT_OFFSET; }
|
|
|
|
|
|
|
|
bool get_transformation (float (&matrix)[4], contour_point_t &trans) const
|
|
|
|
{
|
|
|
|
matrix[0] = matrix[3] = 1.f;
|
|
|
|
matrix[1] = matrix[2] = 0.f;
|
|
|
|
|
2022-07-12 05:52:25 +02:00
|
|
|
const auto *p = &StructAfter<const HBINT8> (flags);
|
2022-07-28 17:53:30 +02:00
|
|
|
#ifndef HB_NO_BEYOND_64K
|
2022-07-12 05:52:25 +02:00
|
|
|
if (flags & GID_IS_24BIT)
|
|
|
|
p += HBGlyphID24::static_size;
|
|
|
|
else
|
2022-07-28 17:53:30 +02:00
|
|
|
#endif
|
2022-07-12 05:52:25 +02:00
|
|
|
p += HBGlyphID16::static_size;
|
2022-06-26 02:03:15 +02:00
|
|
|
int tx, ty;
|
|
|
|
if (flags & ARG_1_AND_2_ARE_WORDS)
|
|
|
|
{
|
|
|
|
tx = *(const HBINT16 *) p;
|
|
|
|
p += HBINT16::static_size;
|
|
|
|
ty = *(const HBINT16 *) p;
|
|
|
|
p += HBINT16::static_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tx = *p++;
|
|
|
|
ty = *p++;
|
|
|
|
}
|
|
|
|
if (is_anchored ()) tx = ty = 0;
|
|
|
|
|
|
|
|
trans.init ((float) tx, (float) ty);
|
|
|
|
|
|
|
|
{
|
|
|
|
const F2DOT14 *points = (const F2DOT14 *) p;
|
|
|
|
if (flags & WE_HAVE_A_SCALE)
|
|
|
|
{
|
|
|
|
matrix[0] = matrix[3] = points[0].to_float ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (flags & WE_HAVE_AN_X_AND_Y_SCALE)
|
|
|
|
{
|
|
|
|
matrix[0] = points[0].to_float ();
|
|
|
|
matrix[3] = points[1].to_float ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (flags & WE_HAVE_A_TWO_BY_TWO)
|
|
|
|
{
|
|
|
|
matrix[0] = points[0].to_float ();
|
|
|
|
matrix[1] = points[1].to_float ();
|
|
|
|
matrix[2] = points[2].to_float ();
|
|
|
|
matrix[3] = points[3].to_float ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tx || ty;
|
|
|
|
}
|
|
|
|
|
2022-06-26 03:06:27 +02:00
|
|
|
public:
|
2022-07-12 05:52:25 +02:00
|
|
|
hb_codepoint_t get_gid () const
|
|
|
|
{
|
2022-07-28 17:53:30 +02:00
|
|
|
#ifndef HB_NO_BEYOND_64K
|
2022-07-12 05:52:25 +02:00
|
|
|
if (flags & GID_IS_24BIT)
|
|
|
|
return StructAfter<const HBGlyphID24> (flags);
|
|
|
|
else
|
2022-07-28 17:53:30 +02:00
|
|
|
#endif
|
2022-07-12 05:52:25 +02:00
|
|
|
return StructAfter<const HBGlyphID16> (flags);
|
|
|
|
}
|
|
|
|
void set_gid (hb_codepoint_t gid)
|
|
|
|
{
|
2022-07-28 17:53:30 +02:00
|
|
|
#ifndef HB_NO_BEYOND_64K
|
2022-07-12 05:52:25 +02:00
|
|
|
if (flags & GID_IS_24BIT)
|
|
|
|
StructAfter<HBGlyphID24> (flags) = gid;
|
|
|
|
else
|
2022-07-28 17:53:30 +02:00
|
|
|
#endif
|
2022-07-12 05:52:25 +02:00
|
|
|
/* TODO assert? */
|
|
|
|
StructAfter<HBGlyphID16> (flags) = gid;
|
|
|
|
}
|
2022-07-11 23:08:22 +02:00
|
|
|
|
|
|
|
protected:
|
2022-06-26 02:03:15 +02:00
|
|
|
HBUINT16 flags;
|
2022-07-12 05:52:25 +02:00
|
|
|
HBUINT24 pad;
|
2022-06-26 02:03:15 +02:00
|
|
|
public:
|
|
|
|
DEFINE_SIZE_MIN (4);
|
|
|
|
};
|
|
|
|
|
2022-06-26 03:03:30 +02:00
|
|
|
struct composite_iter_t : hb_iter_with_fallback_t<composite_iter_t, const CompositeGlyphRecord &>
|
2022-06-26 02:03:15 +02:00
|
|
|
{
|
2022-06-26 03:03:30 +02:00
|
|
|
typedef const CompositeGlyphRecord *__item_t__;
|
2022-06-26 02:03:15 +02:00
|
|
|
composite_iter_t (hb_bytes_t glyph_, __item_t__ current_) :
|
|
|
|
glyph (glyph_), current (nullptr), current_size (0)
|
|
|
|
{
|
2022-06-26 03:01:43 +02:00
|
|
|
set_current (current_);
|
2022-06-26 02:03:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
composite_iter_t () : glyph (hb_bytes_t ()), current (nullptr), current_size (0) {}
|
|
|
|
|
2022-06-26 03:01:43 +02:00
|
|
|
item_t __item__ () const { return *current; }
|
2022-06-26 02:03:15 +02:00
|
|
|
bool __more__ () const { return current; }
|
|
|
|
void __next__ ()
|
|
|
|
{
|
|
|
|
if (!current->has_more ()) { current = nullptr; return; }
|
|
|
|
|
2022-06-26 03:03:30 +02:00
|
|
|
set_current (&StructAtOffset<CompositeGlyphRecord> (current, current_size));
|
2022-06-26 02:03:15 +02:00
|
|
|
}
|
|
|
|
composite_iter_t __end__ () const { return composite_iter_t (); }
|
|
|
|
bool operator != (const composite_iter_t& o) const
|
|
|
|
{ return current != o.current; }
|
|
|
|
|
|
|
|
|
2022-06-26 03:01:43 +02:00
|
|
|
void set_current (__item_t__ current_)
|
2022-06-26 02:03:15 +02:00
|
|
|
{
|
2022-06-26 03:03:30 +02:00
|
|
|
if (!glyph.check_range (current_, CompositeGlyphRecord::min_size))
|
2022-06-26 02:03:15 +02:00
|
|
|
{
|
|
|
|
current = nullptr;
|
|
|
|
current_size = 0;
|
|
|
|
return;
|
|
|
|
}
|
2022-06-26 03:01:43 +02:00
|
|
|
unsigned size = current_->get_size ();
|
|
|
|
if (!glyph.check_range (current_, size))
|
2022-06-26 02:03:15 +02:00
|
|
|
{
|
|
|
|
current = nullptr;
|
|
|
|
current_size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-26 03:01:43 +02:00
|
|
|
current = current_;
|
2022-06-26 02:03:15 +02:00
|
|
|
current_size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
hb_bytes_t glyph;
|
|
|
|
__item_t__ current;
|
|
|
|
unsigned current_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CompositeGlyph
|
|
|
|
{
|
|
|
|
const GlyphHeader &header;
|
|
|
|
hb_bytes_t bytes;
|
|
|
|
CompositeGlyph (const GlyphHeader &header_, hb_bytes_t bytes_) :
|
|
|
|
header (header_), bytes (bytes_) {}
|
|
|
|
|
2022-06-26 02:31:31 +02:00
|
|
|
composite_iter_t iter () const
|
2022-06-26 03:03:30 +02:00
|
|
|
{ return composite_iter_t (bytes, &StructAfter<CompositeGlyphRecord, GlyphHeader> (header)); }
|
2022-06-26 02:03:15 +02:00
|
|
|
|
|
|
|
unsigned int instructions_length (hb_bytes_t bytes) const
|
|
|
|
{
|
|
|
|
unsigned int start = bytes.length;
|
|
|
|
unsigned int end = bytes.length;
|
2022-06-26 03:03:30 +02:00
|
|
|
const CompositeGlyphRecord *last = nullptr;
|
2022-06-26 02:31:31 +02:00
|
|
|
for (auto &item : iter ())
|
2022-06-26 02:03:15 +02:00
|
|
|
last = &item;
|
|
|
|
if (unlikely (!last)) return 0;
|
|
|
|
|
|
|
|
if (last->has_instructions ())
|
|
|
|
start = (char *) last - &bytes + last->get_size ();
|
|
|
|
if (unlikely (start > end)) return 0;
|
|
|
|
return end - start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trimming for composites not implemented.
|
|
|
|
* If removing hints it falls out of that. */
|
|
|
|
const hb_bytes_t trim_padding () const { return bytes; }
|
|
|
|
|
|
|
|
void drop_hints ()
|
|
|
|
{
|
2022-06-26 02:31:31 +02:00
|
|
|
for (const auto &_ : iter ())
|
2022-06-26 03:03:30 +02:00
|
|
|
const_cast<CompositeGlyphRecord &> (_).drop_instructions_flag ();
|
2022-06-26 02:03:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Chop instructions off the end */
|
|
|
|
void drop_hints_bytes (hb_bytes_t &dest_start) const
|
|
|
|
{ dest_start = bytes.sub_array (0, bytes.length - instructions_length (bytes)); }
|
|
|
|
|
|
|
|
void set_overlaps_flag ()
|
|
|
|
{
|
2022-06-26 03:03:30 +02:00
|
|
|
CompositeGlyphRecord& glyph_chain = const_cast<CompositeGlyphRecord &> (
|
|
|
|
StructAfter<CompositeGlyphRecord, GlyphHeader> (header));
|
|
|
|
if (!bytes.check_range(&glyph_chain, CompositeGlyphRecord::min_size))
|
2022-06-26 02:03:15 +02:00
|
|
|
return;
|
|
|
|
glyph_chain.set_overlaps_flag ();
|
|
|
|
}
|
2022-07-22 18:37:09 +02:00
|
|
|
|
|
|
|
bool compile_bytes_with_deltas (const hb_bytes_t &source_bytes,
|
|
|
|
const contour_point_vector_t &deltas,
|
|
|
|
hb_bytes_t &dest_bytes /* OUT */)
|
|
|
|
{
|
|
|
|
int len = source_bytes.length - GlyphHeader::static_size;
|
|
|
|
if (len <= 0 || header.numberOfContours != -1)
|
|
|
|
{
|
|
|
|
dest_bytes = hb_bytes_t ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *p = (char *) hb_calloc (len, sizeof (char));
|
|
|
|
if (unlikely (!p)) return false;
|
|
|
|
|
|
|
|
memcpy (p, source_bytes.arrayZ + GlyphHeader::static_size, len);
|
|
|
|
dest_bytes = hb_bytes_t (p, len);
|
|
|
|
|
|
|
|
auto it = composite_iter_t (dest_bytes, (CompositeGlyphRecord *)p);
|
|
|
|
|
|
|
|
unsigned i = 0;
|
|
|
|
for (auto &component : it)
|
|
|
|
{
|
|
|
|
if (!component.is_anchored ())
|
|
|
|
{
|
|
|
|
/* last 4 points in deltas are phantom points and should not be included*/
|
|
|
|
if (i >= deltas.length - 4) return false;
|
|
|
|
const_cast<CompositeGlyphRecord &> (component).apply_delta_to_offsets (deltas[i]);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2022-06-26 02:03:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} /* namespace glyf_impl */
|
|
|
|
} /* namespace OT */
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* OT_GLYF_COMPOSITEGLYPH_HH */
|