blob: 63ba7f197a0a3f2c51bcd39b88b86302b7bff170 [file] [edit]
/*
* Copyright © 2022 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.
*/
#ifndef HB_LIMITS_HH
#define HB_LIMITS_HH
#include "hb.hh"
#ifndef HB_BUFFER_MAX_LEN_FACTOR
#define HB_BUFFER_MAX_LEN_FACTOR 512
#endif
#ifndef HB_BUFFER_MAX_LEN_MIN
#define HB_BUFFER_MAX_LEN_MIN 65536
#endif
#ifndef HB_BUFFER_MAX_LEN_DEFAULT
#define HB_BUFFER_MAX_LEN_DEFAULT 0x3FFFFFFF /* Shaping more than a billion chars? Let us know! */
#endif
#ifndef HB_BUFFER_MAX_OPS_FACTOR
/* Duployan fonts spend up to ~4.3K operations per input character on
* words and sentences. Allow over ten times that. */
#define HB_BUFFER_MAX_OPS_FACTOR 65536
#endif
#ifndef HB_BUFFER_MAX_OPS_MIN
/* Short Duployan inputs spend up to ~37K operations before the linear
* allowance takes over. */
#define HB_BUFFER_MAX_OPS_MIN 524288
#endif
#ifndef HB_BUFFER_MAX_OPS_DEFAULT
#define HB_BUFFER_MAX_OPS_DEFAULT 0x3FFFFFFF /* Shaping more than a billion operations? Let us know! */
#endif
#ifndef HB_MAX_NESTING_LEVEL
#define HB_MAX_NESTING_LEVEL 64
#endif
#ifndef HB_MAX_CONTEXT_LENGTH
#define HB_MAX_CONTEXT_LENGTH 64
#endif
#ifndef HB_MAX_SYLLABLE_LENGTH
#define HB_MAX_SYLLABLE_LENGTH 64
#endif
#ifndef HB_CLOSURE_MAX_STAGES
/*
* The maximum number of times a lookup can be applied during shaping.
* Used to limit the number of iterations of the closure algorithm.
* This must be larger than the number of times add_gsub_pause() is
* called in a collect_features call of any shaper.
*/
#define HB_CLOSURE_MAX_STAGES 16
#endif
#ifndef HB_MAX_SCRIPTS
#define HB_MAX_SCRIPTS 500
#endif
#ifndef HB_MAX_LANGSYS
#define HB_MAX_LANGSYS 2000
#endif
#ifndef HB_MAX_LANGSYS_FEATURE_COUNT
#define HB_MAX_LANGSYS_FEATURE_COUNT 50000
#endif
#ifndef HB_MAX_FEATURE_INDICES
#define HB_MAX_FEATURE_INDICES 8000
#endif
#ifndef HB_MAX_LOOKUP_VISIT_COUNT
#define HB_MAX_LOOKUP_VISIT_COUNT 35000
#endif
/* Distinct lookup recursion paths the dependency graph builder interns
* over a whole GSUB; building the graph fails when it needs more.
* Lookups that invoke each other in every order make a factorial number
* of them. The most among the test fonts is 744 (NotoNastaliqUrdu-Bold). */
#ifndef HB_MAX_DEPEND_RECURSE_PATHS
#define HB_MAX_DEPEND_RECURSE_PATHS 35000
#endif
/* Subtables of one kern, kerx, mort, or morx table; the table is rejected
* when it declares more. The table's accelerator keeps glyph sets for
* each, which can take far more memory than the subtable has bytes. */
#ifndef HB_AAT_MAX_SUBTABLES
#define HB_AAT_MAX_SUBTABLES 1024
#endif
#ifndef HB_MAX_GRAPH_EDGE_COUNT
#define HB_MAX_GRAPH_EDGE_COUNT 16384
#endif
/* Maximum number of edges stored in one glyph dependency graph. GSUB
* edges are keyed by (source, target, feature tag), so a lookup reached
* by many feature tags multiplies its substitutions by the tag count and
* a small table can describe a graph of hundreds of millions of edges. */
#ifndef HB_DEPEND_MAX_EDGES
#define HB_DEPEND_MAX_EDGES (1u << 25)
#endif
#ifndef HB_VAR_COMPOSITE_MAX_AXES
#define HB_VAR_COMPOSITE_MAX_AXES 4096
#endif
/* Region axes copied into tuples when one ItemVariationStore is converted
* for instancing, summed over its VarData subtables. Every region a
* VarData references gets its own copy of that region's axes, and the
* font pays a few bytes per reference however many axes the region has. */
#ifndef HB_VARSTORE_MAX_TUPLE_AXES
#define HB_VARSTORE_MAX_TUPLE_AXES (1u << 20)
#endif
/* Deltas that instancing an avar version 2 table may add to its variation
* store as offset-compensation tuples. Each tuple holds a delta for every
* row of the VarData it is added to, but the breakpoint it encodes costs
* four bytes of the table and a VarData without regions stores its rows in
* no bytes at all, so the table size does not bound that memory. The
* budget is this many deltas per byte of the table, but no less than the
* minimum; exceeding it fails the table. */
#ifndef HB_AVAR2_MAX_DELTAS_FACTOR
#define HB_AVAR2_MAX_DELTAS_FACTOR 128
#endif
#ifndef HB_AVAR2_MAX_DELTAS_MIN
#define HB_AVAR2_MAX_DELTAS_MIN 65536
#endif
#ifndef HB_GLYF_MAX_POINTS
#define HB_GLYF_MAX_POINTS 200000
#endif
#ifndef HB_CFF_MAX_OPS
#define HB_CFF_MAX_OPS 200000
#endif
#ifndef HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH
#define HB_MAX_COMPOSITE_OPERATIONS_PER_GLYPH 64
#endif
/* Maximum serialized subset table size, as a multiple of the source table
* length, with a floor for compact tables that expand under renumbering.
* The serializer's INT_MAX allocation ceiling still applies. */
#ifndef HB_SUBSET_MAX_TABLE_SIZE_FACTOR
#define HB_SUBSET_MAX_TABLE_SIZE_FACTOR 256
#endif
#ifndef HB_SUBSET_MAX_TABLE_SIZE_MIN
#define HB_SUBSET_MAX_TABLE_SIZE_MIN ((uint64_t) 1 << 20)
#endif
/* Source bytes of all the glyphs the subsetter retains, as a multiple of
* the glyf table length; exceeding it fails the plan. A monotonic loca
* never needs more than one; a loca that aliases the same bytes to many
* glyph IDs makes trimming and hint dropping cost that many table scans. */
#ifndef HB_SUBSET_GLYF_MAX_BYTES_FACTOR
#define HB_SUBSET_GLYF_MAX_BYTES_FACTOR 16
#endif
#ifndef HB_GPU_DRAW_MAX_CURVES
#define HB_GPU_DRAW_MAX_CURVES 65536
#endif
/* Bytes the CBDT subsetter may copy out of the source CBDT table, as a
* multiple of that table's length. Image blocks of different glyphs or
* strikes may overlap or repeat, so bounds-checking each block against the
* table does not bound their sum. */
#ifndef HB_SUBSET_CBDT_MAX_COPY_FACTOR
#define HB_SUBSET_CBDT_MAX_COPY_FACTOR 64
#endif
/* Strikes of a CBLC table considered when choosing one for a glyph query;
* later ones are ignored. numSizes is a 32-bit field and every query walks
* the strikes, while ppem is 8-bit and real fonts have a handful. */
#ifndef HB_CBLC_MAX_STRIKES
#define HB_CBLC_MAX_STRIKES 256
#endif
/* Total bytes of sub-blobs (gradient color stops, clip outlines) in one
* hb_gpu_paint_t walk; exceeding it makes the paint unsupported. */
#ifndef HB_GPU_PAINT_MAX_SUB_BYTES
#define HB_GPU_PAINT_MAX_SUB_BYTES ((unsigned) 64 << 20)
#endif
/* Tiles emitted by one hb_paint_sweep_gradient_tiles() call. Also
* sets the angular resolution (2π over this) below which a repeating
* color line is filled with its average color instead of tiled;
* a repeat/reflect color line whose stops span a tiny angle could
* otherwise emit millions of patches while covering 0..2π. */
#ifndef HB_PAINT_MAX_SWEEP_TILES
#define HB_PAINT_MAX_SWEEP_TILES 4096
#endif
#ifndef HB_SVG_MAX_DOCUMENT_SIZE
#define HB_SVG_MAX_DOCUMENT_SIZE ((size_t) 16 << 20)
#endif
/* Maximum size of one serialized vector document (SVG or PDF) produced by
* the vector backends. Bounds output that is not outline-derived -- most
* of a path, but especially sweep-gradient meshes and embedded bitmaps --
* which the outline work budget cannot see. Unsigned (not size_t like the
* SVG-table limit above) to match hb_vector_buf_t's unsigned length arithmetic. */
#ifndef HB_VECTOR_MAX_DOCUMENT_SIZE
#define HB_VECTOR_MAX_DOCUMENT_SIZE ((unsigned) 40 << 20)
#endif
#ifndef HB_RASTER_MAX_BUFFER_SIZE
#define HB_RASTER_MAX_BUFFER_SIZE ((size_t) 1 << 30)
#endif
/* Maximum surface dimension (pixels per side) when extents are derived
* from font data (glyph extents, accumulated outline bounds, or the header
* of an embedded PNG image about to be decoded). Bounds
* attacker-controlled allocations; extents set explicitly through
* hb_raster_{draw,paint}_set_extents() are not limited. */
#ifndef HB_RASTER_MAX_AUTO_DIMENSION
#define HB_RASTER_MAX_AUTO_DIMENSION 4096
#endif
/*
* Cumulative work budgets.
*
* The limits above bound work within one subsystem: points per glyf
* glyph, ops per CFF charstring, nodes in a COLR or VARC graph,
* pixels per raster surface. When one subsystem drives another --
* COLR driving a paint backend, a paint backend or VARC loading
* glyf/CFF outlines -- those limits multiply. Each driving session
* therefore carries one cumulative budget, initialized once per
* top-level entry and only ever decremented, shared by everything
* the session consumes. Once a budget is exhausted, further work
* is skipped best-effort.
*/
/* Fixed relative weights. Keep the value in the name so charge sites are
* easy to audit; tune the common session cap, not these values. */
#define HB_BUDGET_1 1u
#define HB_BUDGET_2 2u
#define HB_BUDGET_4 4u
#define HB_BUDGET_8 8u
#define HB_BUDGET_16 16u
#define HB_BUDGET_32 32u
#define HB_BUDGET_64 64u
#define HB_BUDGET_128 128u
#define HB_BUDGET_256 256u
#define HB_BUDGET_512 512u
#define HB_BUDGET_1024 1024u
/* The common finite default for one top-level glyph rendering session.
* Nested outline and paint work shares the same live counter. */
#ifndef HB_BUDGET_GLYPH
#define HB_BUDGET_GLYPH ((int64_t) 1 << 25)
#endif
/* Precharge COST * MULT. Callers bound COST and MULT structurally, and live
* budgets are concrete non-negative values when a session starts. */
static HB_ALWAYS_INLINE bool
hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
{
budget -= (int64_t) cost * mult;
return budget >= 0;
}
/* One glyf instancing session in the subsetter. It loads the outline of
* every retained glyph, and composite components are loaded again for each
* glyph that uses them, so the per-glyph budget (HB_BUDGET_GLYPH) would
* multiply by the glyph count. This is one cumulative budget for the whole
* table instead; exhausting it fails the table. Sized well above what real
* variable fonts spend -- instancing all 1,605 glyphs of Handjet at the
* midpoint of its axes uses ~625M units -- while keeping the
* adversarial worst case bounded. */
#ifndef HB_BUDGET_SUBSET_GLYF
#define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 33)
#endif
/* Coverage and ClassDef glyph walks while subsetting one layout table.
* A glyph range costs a few bytes and any number of offsets can share it,
* so neither the table size nor the sanitizer's budget bounds that work. */
#ifndef HB_BUDGET_SUBSET_LAYOUT
#define HB_BUDGET_SUBSET_LAYOUT ((int64_t) 1 << 28)
#endif
/* The flat floor for the subsetter's work on the charstrings of a CFF or
* CFF2 table, in operators and operands interpreted, shared by all passes
* of one subset over them. Glyphs can share subroutines, so neither
* HB_CFF_MAX_OPS per glyph nor the table size bounds that work. Running
* out fails the charstring being interpreted, as exceeding HB_CFF_MAX_OPS
* does. */
#ifndef HB_BUDGET_SUBSET_CFF
#define HB_BUDGET_SUBSET_CFF ((int64_t) 1 << 25)
#endif
/* Very large CFF and CFF2 tables still get several operations per byte;
* subroutinized fonts interpret a few operations per table byte. */
#ifndef HB_BUDGET_SUBSET_CFF_PER_BYTE
#define HB_BUDGET_SUBSET_CFF_PER_BYTE 96
#endif
/* Partially instancing one CFF2 table in the subsetter, in cells of the
* old-region to new-region matrices built for its VarDatas. A VarData
* with k regions costs k * k cells for 2 * k bytes of font, and many
* offsets can share one VarData. Exceeding the budget fails the table. */
#ifndef HB_BUDGET_SUBSET_CFF2_CELLS
#define HB_BUDGET_SUBSET_CFF2_CELLS ((int64_t) 1 << 24)
#endif
/* The flat pixel-work floor for one raster draw or paint session. Pixel and
* outline work use separate live counters, so keep this independent of
* the common outline budget above.
*
* Small surfaces live off this. What a layer of a color paint costs does
* not shrink as fast as the area of the surface does, so the smaller the
* surface, the more times its area a glyph needs: Bitcount Ink with every
* axis at its maximum needs 680 times at 64 pixels per em, 3 million in
* all, and 18 million at 256. */
#ifndef HB_BUDGET_RASTER_PIXELS
#define HB_BUDGET_RASTER_PIXELS ((int64_t) 1 << 26)
#endif
/* Larger paint surfaces get pixel work in proportion to their area. A
* layer is charged for its mask, its clip and its fill, over the box it
* can show in. The most that takes at 2048 pixels per em is 122 times
* the area of the surface, for Bitcount Ink with every axis at its
* maximum; allow four times that. Its default instance needs 18 times,
* and the heaviest glyph of Noto Color Emoji 110. */
#ifndef HB_BUDGET_RASTER_PAINT_PASSES
#define HB_BUDGET_RASTER_PAINT_PASSES 512
#endif
/* Sweeping an outline costs a small fraction of the area of its mask, and
* far less than the floor at any size, so a very large mask gets just a
* few times its own. */
#ifndef HB_BUDGET_RASTER_DRAW_PASSES
#define HB_BUDGET_RASTER_DRAW_PASSES 4
#endif
/* One raster draw session, in accumulated non-horizontal edges. */
#ifndef HB_RASTER_MAX_DRAW_EDGES
#define HB_RASTER_MAX_DRAW_EDGES ((int64_t) 1 << 20)
#endif
/* Compiling one AAT feature map: morx/mort chains and chain feature
* records visited for each range of the text with its own set of
* features, plus extra for each range a chain keeps rather than merges
* into the previous one, since those are what the map stores. The
* ranges come from the caller's features and the chains from the font,
* so neither input bounds their product. Once exhausted, the last
* range compiled extends to the end of the text. */
#ifndef HB_BUDGET_AAT_MAP
#define HB_BUDGET_AAT_MAP ((int64_t) 1 << 24)
#endif
/* One dependency graph build. A few bytes of Coverage or ClassDef range
* can name any number of glyphs, so the glyph sets built from them are
* charged per page: for the work of expanding them, and for the memory
* they hold once the graph keeps a copy. Exceeding the budget fails the
* build. */
#ifndef HB_BUDGET_DEPEND
#define HB_BUDGET_DEPEND ((int64_t) 1 << 26)
#endif
/* One GSUB glyph-closure session, in records (glyphs, offsets, ranges,
* rules) walked. HB_MAX_LOOKUP_VISIT_COUNT bounds how many times lookups
* are entered per round, not what one visit walks: through shared offsets
* a subtable can walk many times the size of its table, and every glyph
* the closure gains makes all lookups visitable again. */
#ifndef HB_BUDGET_LAYOUT_CLOSURE
#define HB_BUDGET_LAYOUT_CLOSURE ((int64_t) 1 << 28)
#endif
/* One instancing session: all the tuple variations of one table.
* Limiting an axis splits every tuple that uses it into up to six, so
* tuples multiply with each limited axis. The copies are charged to a
* budget that starts at MIN and earns FACTOR times each source tuple,
* in tuple_delta_t::get_cost () units of roughly eight bytes. */
#ifndef HB_BUDGET_INSTANCER_FACTOR
#define HB_BUDGET_INSTANCER_FACTOR 128
#endif
#ifndef HB_BUDGET_INSTANCER_MIN
#define HB_BUDGET_INSTANCER_MIN ((int64_t) 1 << 22)
#endif
/* Instancing one whole gvar or cvar table in the subsetter, in points.
* Every tuple is expanded to one entry per point of its glyph (per cvt
* value), however few of them it refers to; a tuple takes a dozen bytes
* and a run of repeated glyf points a fraction of a byte each, so the
* size of the font does not bound the product. The budget is this many
* points per byte of the table, but no less than the minimum; exceeding
* it fails the table. */
#ifndef HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR
#define HB_BUDGET_SUBSET_TUPLE_POINTS_FACTOR 64
#endif
#ifndef HB_BUDGET_SUBSET_TUPLE_POINTS_MIN
#define HB_BUDGET_SUBSET_TUPLE_POINTS_MIN ((int64_t) 1 << 20)
#endif
/* Decoded glyf points a subset plan keeps for partially instancing gvar:
* one per byte of the source glyf table, but no fewer than this. Repeated
* flags and loca entries that share glyph data let a font declare far more
* points than it has bytes. Exceeding the budget fails the plan. */
#ifndef HB_BUDGET_SUBSET_CONTOUR_POINTS
#define HB_BUDGET_SUBSET_CONTOUR_POINTS ((int64_t) 1 << 23)
#endif
/* One avar2 partial-instancing plan, in region tents visited to bound the
* final coordinates each axis can reach. A delta-set row can list a region
* any number of times and axes can share a row, so the table size does not
* bound that work. An axis whose exact bounds do not fit what is left gets
* the looser interval bounds; once exhausted, the remaining axes get no
* range and are not detected as self-contained. Either way the instance
* stays correct, only less optimized: those ranges only cull variation
* data that cannot be reached, and a pinned axis not found self-contained
* stays in fvar as a hidden pin. */
#ifndef HB_BUDGET_SUBSET_AVAR2_RANGES
#define HB_BUDGET_SUBSET_AVAR2_RANGES ((int64_t) 1 << 26)
#endif
/* The instancer rebuilds an ItemVariationStore from a dense delta matrix,
* one int per (row, region) pair. In the font a row only pays for the
* regions of its own VarData, so a small font can ask for a huge matrix.
* These are flat ceilings on that one allocation: a proportional bound
* would reject legitimate optimizer output, where several VarData use
* disjoint region subsets so the dense matrix is much wider than any one
* VarData. Exceeding either limit fails instancing of that store. */
#ifndef HB_SUBSET_MAX_VARSTORE_ROWS
#define HB_SUBSET_MAX_VARSTORE_ROWS (1u << 21)
#endif
#ifndef HB_SUBSET_MAX_VARSTORE_DELTAS
#define HB_SUBSET_MAX_VARSTORE_DELTAS ((uint64_t) 1 << 26)
#endif
#ifndef HB_REPACKER_MAX_ITERATIONS
#define HB_REPACKER_MAX_ITERATIONS 500
#endif
#ifndef HB_REPACKER_MAX_VERTICES
#define HB_REPACKER_MAX_VERTICES 800000
#endif
#ifndef HB_REPACKER_MAX_SPACES
#define HB_REPACKER_MAX_SPACES 8000
#endif
#endif /* HB_LIMITS_HH */