2018-08-06 19:04:53 +02:00
|
|
|
/*
|
2018-11-12 17:47:07 +01:00
|
|
|
* Copyright © 2018 Adobe Inc.
|
2018-08-06 19:04:53 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Adobe Author(s): Michiharu Ariza
|
|
|
|
*/
|
|
|
|
|
2019-06-18 22:07:44 +02:00
|
|
|
#include "hb.hh"
|
|
|
|
|
|
|
|
#ifndef HB_NO_SUBSET_CFF
|
|
|
|
|
2018-08-29 22:26:17 +02:00
|
|
|
#include "hb-ot-cff-common.hh"
|
2018-08-06 19:04:53 +02:00
|
|
|
#include "hb-ot-cff2-table.hh"
|
2018-08-29 22:26:17 +02:00
|
|
|
#include "hb-subset-cff-common.hh"
|
2018-08-06 19:04:53 +02:00
|
|
|
|
2018-09-20 02:00:10 +02:00
|
|
|
/* Disable FDSelect format 0 for compatibility with fonttools which doesn't seem choose it.
|
|
|
|
* Rarely any/much smaller than format 3 anyway. */
|
|
|
|
#define CFF_SERIALIZE_FDSELECT_0 0
|
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
using namespace CFF;
|
|
|
|
|
2021-07-26 02:12:21 +02:00
|
|
|
|
|
|
|
/* Determine an optimal FDSelect format according to a provided plan.
|
2018-08-06 19:04:53 +02:00
|
|
|
*
|
|
|
|
* Return value: FDSelect format, size, and ranges for the most compact subset FDSelect
|
|
|
|
* along with a font index remapping table
|
2021-07-26 02:12:21 +02:00
|
|
|
*/
|
2018-08-06 19:04:53 +02:00
|
|
|
|
|
|
|
bool
|
2019-01-31 23:16:37 +01:00
|
|
|
hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan,
|
2019-06-13 12:34:51 +02:00
|
|
|
unsigned int fdCount,
|
|
|
|
const FDSelect &src, /* IN */
|
|
|
|
unsigned int &subset_fd_count /* OUT */,
|
|
|
|
unsigned int &subset_fdselect_size /* OUT */,
|
|
|
|
unsigned int &subset_fdselect_format /* OUT */,
|
|
|
|
hb_vector_t<code_pair_t> &fdselect_ranges /* OUT */,
|
2019-06-18 07:12:40 +02:00
|
|
|
hb_inc_bimap_t &fdmap /* OUT */)
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
|
|
|
subset_fd_count = 0;
|
2018-09-13 01:08:54 +02:00
|
|
|
subset_fdselect_size = 0;
|
|
|
|
subset_fdselect_format = 0;
|
2019-06-13 12:34:51 +02:00
|
|
|
unsigned int num_ranges = 0;
|
2018-08-06 19:04:53 +02:00
|
|
|
|
2019-01-31 23:16:37 +01:00
|
|
|
unsigned int subset_num_glyphs = plan->num_output_glyphs ();
|
2018-08-06 19:04:53 +02:00
|
|
|
if (subset_num_glyphs == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
{
|
|
|
|
/* use hb_set to determine the subset of font dicts */
|
2022-11-25 22:57:40 +01:00
|
|
|
hb_set_t set;
|
2019-06-13 12:34:51 +02:00
|
|
|
hb_codepoint_t prev_fd = CFF_UNDEF_CODE;
|
2018-08-06 19:04:53 +02:00
|
|
|
for (hb_codepoint_t i = 0; i < subset_num_glyphs; i++)
|
|
|
|
{
|
2019-06-13 12:34:51 +02:00
|
|
|
hb_codepoint_t glyph;
|
|
|
|
hb_codepoint_t fd;
|
2019-01-31 23:16:37 +01:00
|
|
|
if (!plan->old_gid_for_new_gid (i, &glyph))
|
|
|
|
{
|
2019-02-08 00:32:32 +01:00
|
|
|
/* fonttools retains FDSelect & font dicts for missing glyphs. do the same */
|
|
|
|
glyph = i;
|
2019-01-31 23:16:37 +01:00
|
|
|
}
|
2019-02-08 00:32:32 +01:00
|
|
|
fd = src.get_fd (glyph);
|
2022-11-25 22:57:40 +01:00
|
|
|
set.add (fd);
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
if (fd != prev_fd)
|
|
|
|
{
|
2018-12-01 05:04:59 +01:00
|
|
|
num_ranges++;
|
|
|
|
prev_fd = fd;
|
2018-12-22 16:47:04 +01:00
|
|
|
code_pair_t pair = { fd, i };
|
2018-12-01 05:04:59 +01:00
|
|
|
fdselect_ranges.push (pair);
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 22:57:40 +01:00
|
|
|
subset_fd_count = set.get_population ();
|
2018-08-06 19:44:12 +02:00
|
|
|
if (subset_fd_count == fdCount)
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
|
|
|
/* all font dicts belong to the subset. no need to subset FDSelect & FDArray */
|
2018-11-16 00:39:43 +01:00
|
|
|
fdmap.identity (fdCount);
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
2018-11-16 00:39:43 +01:00
|
|
|
else
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2018-11-16 00:39:43 +01:00
|
|
|
/* create a fdmap */
|
2019-06-18 07:12:40 +02:00
|
|
|
fdmap.reset ();
|
2018-11-16 00:39:43 +01:00
|
|
|
|
2019-06-13 12:34:51 +02:00
|
|
|
hb_codepoint_t fd = CFF_UNDEF_CODE;
|
2022-11-25 22:57:40 +01:00
|
|
|
while (set.next (&fd))
|
2018-12-01 05:04:59 +01:00
|
|
|
fdmap.add (fd);
|
2019-06-18 07:12:40 +02:00
|
|
|
if (unlikely (fdmap.get_population () != subset_fd_count))
|
2019-12-31 13:23:02 +01:00
|
|
|
return false;
|
2018-09-11 01:00:20 +02:00
|
|
|
}
|
|
|
|
|
2018-09-13 01:08:54 +02:00
|
|
|
/* update each font dict index stored as "code" in fdselect_ranges */
|
2018-12-22 00:46:51 +01:00
|
|
|
for (unsigned int i = 0; i < fdselect_ranges.length; i++)
|
2019-06-20 23:33:09 +02:00
|
|
|
fdselect_ranges[i].code = fdmap[fdselect_ranges[i].code];
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* determine which FDSelect format is most compact */
|
|
|
|
if (subset_fd_count > 0xFF)
|
|
|
|
{
|
2018-12-11 21:20:20 +01:00
|
|
|
if (unlikely (src.format != 4))
|
|
|
|
return false;
|
2018-09-13 01:08:54 +02:00
|
|
|
subset_fdselect_format = 4;
|
2018-11-03 00:40:20 +01:00
|
|
|
subset_fdselect_size = FDSelect::min_size + FDSelect4::min_size + FDSelect4_Range::static_size * num_ranges + HBUINT32::static_size;
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-20 02:00:10 +02:00
|
|
|
#if CFF_SERIALIZE_FDSELECT_0
|
2018-11-03 00:40:20 +01:00
|
|
|
unsigned int format0_size = FDSelect::min_size + FDSelect0::min_size + HBUINT8::static_size * subset_num_glyphs;
|
2018-09-20 02:00:10 +02:00
|
|
|
#endif
|
2018-11-03 00:40:20 +01:00
|
|
|
unsigned int format3_size = FDSelect::min_size + FDSelect3::min_size + FDSelect3_Range::static_size * num_ranges + HBUINT16::static_size;
|
2018-08-06 19:04:53 +02:00
|
|
|
|
2018-09-20 02:00:10 +02:00
|
|
|
#if CFF_SERIALIZE_FDSELECT_0
|
2018-08-06 19:04:53 +02:00
|
|
|
if (format0_size <= format3_size)
|
|
|
|
{
|
2018-09-13 01:08:54 +02:00
|
|
|
// subset_fdselect_format = 0;
|
|
|
|
subset_fdselect_size = format0_size;
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
|
|
|
else
|
2018-09-20 02:00:10 +02:00
|
|
|
#endif
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
2018-09-13 01:08:54 +02:00
|
|
|
subset_fdselect_format = 3;
|
|
|
|
subset_fdselect_size = format3_size;
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FDSELECT3_4>
|
|
|
|
static inline bool
|
|
|
|
serialize_fdselect_3_4 (hb_serialize_context_t *c,
|
2019-06-13 12:34:51 +02:00
|
|
|
const unsigned int num_glyphs,
|
|
|
|
const FDSelect &src,
|
|
|
|
unsigned int size,
|
|
|
|
const hb_vector_t<code_pair_t> &fdselect_ranges)
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
FDSELECT3_4 *p = c->allocate_size<FDSELECT3_4> (size);
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!p)) return_trace (false);
|
2019-03-30 04:17:46 +01:00
|
|
|
p->nRanges () = fdselect_ranges.length;
|
2018-12-22 00:46:51 +01:00
|
|
|
for (unsigned int i = 0; i < fdselect_ranges.length; i++)
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
2019-03-30 04:17:46 +01:00
|
|
|
p->ranges[i].first = fdselect_ranges[i].glyph;
|
|
|
|
p->ranges[i].fd = fdselect_ranges[i].code;
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
2019-06-13 12:34:51 +02:00
|
|
|
p->sentinel () = num_glyphs;
|
2018-08-06 19:04:53 +02:00
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2021-07-26 02:12:21 +02:00
|
|
|
/* Serialize a subset FDSelect format planned above. */
|
2018-08-06 19:04:53 +02:00
|
|
|
bool
|
|
|
|
hb_serialize_cff_fdselect (hb_serialize_context_t *c,
|
2019-06-13 12:34:51 +02:00
|
|
|
const unsigned int num_glyphs,
|
|
|
|
const FDSelect &src,
|
|
|
|
unsigned int fd_count,
|
|
|
|
unsigned int fdselect_format,
|
|
|
|
unsigned int size,
|
|
|
|
const hb_vector_t<code_pair_t> &fdselect_ranges)
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
2019-06-13 12:34:51 +02:00
|
|
|
FDSelect *p = c->allocate_min<FDSelect> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!p)) return_trace (false);
|
2019-03-30 04:17:46 +01:00
|
|
|
p->format = fdselect_format;
|
2018-08-06 19:04:53 +02:00
|
|
|
size -= FDSelect::min_size;
|
|
|
|
|
|
|
|
switch (fdselect_format)
|
|
|
|
{
|
2018-09-20 02:00:10 +02:00
|
|
|
#if CFF_SERIALIZE_FDSELECT_0
|
2019-06-13 12:34:51 +02:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
FDSelect0 *p = c->allocate_size<FDSelect0> (size);
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!p)) return_trace (false);
|
2019-06-13 12:34:51 +02:00
|
|
|
unsigned int range_index = 0;
|
|
|
|
unsigned int fd = fdselect_ranges[range_index++].code;
|
|
|
|
for (unsigned int i = 0; i < num_glyphs; i++)
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
2019-06-13 12:34:51 +02:00
|
|
|
if ((range_index < fdselect_ranges.len) &&
|
|
|
|
(i >= fdselect_ranges[range_index].glyph))
|
2018-09-13 01:08:54 +02:00
|
|
|
{
|
2019-06-13 12:34:51 +02:00
|
|
|
fd = fdselect_ranges[range_index++].code;
|
2018-09-13 01:08:54 +02:00
|
|
|
}
|
2019-06-13 12:34:51 +02:00
|
|
|
p->fds[i] = fd;
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
2019-06-13 12:34:51 +02:00
|
|
|
return_trace (true);
|
|
|
|
}
|
2018-09-20 02:00:10 +02:00
|
|
|
#endif /* CFF_SERIALIZE_FDSELECT_0 */
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2019-06-13 12:34:51 +02:00
|
|
|
case 3:
|
|
|
|
return serialize_fdselect_3_4<FDSelect3> (c, num_glyphs, src,
|
|
|
|
size, fdselect_ranges);
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2019-06-13 12:34:51 +02:00
|
|
|
case 4:
|
|
|
|
return serialize_fdselect_3_4<FDSelect4> (c, num_glyphs, src,
|
|
|
|
size, fdselect_ranges);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return_trace (false);
|
|
|
|
}
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
2019-06-18 22:07:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
#endif
|