Files
harfbuzz/src/hb-gpu.h
T
Behdad Esfahbod 3fa9ba2473 [api] Add _clear() companions to _reset() across render contexts
Introduce a public _clear() entry point on each render-context type
that discards accumulated output state while preserving user
configuration.  _reset() is redefined as _clear() plus resetting user
configuration to defaults — same observable effect as before, but
now there is one code path for the output-state teardown.

New public API:
  hb_vector_draw_clear()
  hb_vector_paint_clear()
  hb_raster_draw_clear()
  hb_raster_paint_clear()
  hb_gpu_draw_clear()

Contract:
  _clear(): preserves transform, scale factors, precision, flat,
            foreground, palette, custom palette colors.
  _reset(): equivalent to _clear() + resetting all user config.

The render/encode entry points now auto-clear on every exit path, so
the context is ready for the next glyph out of the box:

  hb_vector_draw_render (SVG + PDF) — ends with _clear().
  hb_vector_paint_render (SVG + PDF) — ends with _clear().
  hb_raster_draw_render — wraps body in HB_SCOPE_GUARD (_clear()).
  hb_raster_paint_render — wraps body in HB_SCOPE_GUARD (_clear()).

hb_gpu_draw_encode is intentionally NOT auto-clearing: its
hb_gpu_draw_get_extents() reads computed extents from the draw after
encode, so callers need that state preserved until they explicitly
clear.

Side fix: hb_vector_paint_clear() (and therefore _reset() and the
render paths) now correctly free the PDF backend's heap-allocated
hb_pdf_resources_t that had been leaking through clear_render_state.
hb_vector_paint_destroy() also free it now.  Expose
hb_vector_paint_pdf_free_resources() as HB_INTERNAL from
hb-vector-paint-pdf.cc for that.

All 240 tests still pass.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 21:27:00 -06:00

133 lines
3.2 KiB
C

/*
* Copyright (C) 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_GPU_H
#define HB_GPU_H
#include "hb.h"
HB_BEGIN_DECLS
/**
* hb_gpu_shader_lang_t:
* @HB_GPU_SHADER_LANG_GLSL: GLSL (OpenGL 3.3 / OpenGL ES 3.0 / WebGL 2.0)
* @HB_GPU_SHADER_LANG_WGSL: WGSL (WebGPU)
* @HB_GPU_SHADER_LANG_MSL: MSL (Metal)
* @HB_GPU_SHADER_LANG_HLSL: HLSL (Direct3D)
*
* Shader language variant.
*
* Since: 14.0.0
*/
typedef enum {
HB_GPU_SHADER_LANG_GLSL,
HB_GPU_SHADER_LANG_WGSL,
HB_GPU_SHADER_LANG_MSL,
HB_GPU_SHADER_LANG_HLSL,
} hb_gpu_shader_lang_t;
HB_EXTERN const char *
hb_gpu_shader_fragment_source (hb_gpu_shader_lang_t lang);
HB_EXTERN const char *
hb_gpu_shader_vertex_source (hb_gpu_shader_lang_t lang);
/**
* hb_gpu_draw_t:
*
* An opaque GPU shape encoder. Accumulates outlines via draw
* callbacks, then encodes them into a compact blob for GPU
* rendering.
*
* Since: 14.0.0
*/
typedef struct hb_gpu_draw_t hb_gpu_draw_t;
HB_EXTERN hb_gpu_draw_t *
hb_gpu_draw_create_or_fail (void);
HB_EXTERN hb_gpu_draw_t *
hb_gpu_draw_reference (hb_gpu_draw_t *draw);
HB_EXTERN void
hb_gpu_draw_destroy (hb_gpu_draw_t *draw);
HB_EXTERN hb_bool_t
hb_gpu_draw_set_user_data (hb_gpu_draw_t *draw,
hb_user_data_key_t *key,
void *data,
hb_destroy_func_t destroy,
hb_bool_t replace);
HB_EXTERN void *
hb_gpu_draw_get_user_data (hb_gpu_draw_t *draw,
hb_user_data_key_t *key);
/* Scale */
HB_EXTERN void
hb_gpu_draw_set_scale (hb_gpu_draw_t *draw,
int x_scale,
int y_scale);
/* Draw */
HB_EXTERN hb_draw_funcs_t *
hb_gpu_draw_get_funcs (void);
HB_EXTERN void
hb_gpu_draw_glyph (hb_gpu_draw_t *draw,
hb_font_t *font,
hb_codepoint_t codepoint);
/* Encode */
HB_EXTERN hb_blob_t *
hb_gpu_draw_encode (hb_gpu_draw_t *draw);
HB_EXTERN void
hb_gpu_draw_get_extents (hb_gpu_draw_t *draw,
hb_glyph_extents_t *extents);
HB_EXTERN void
hb_gpu_draw_clear (hb_gpu_draw_t *draw);
HB_EXTERN void
hb_gpu_draw_reset (hb_gpu_draw_t *draw);
HB_EXTERN void
hb_gpu_draw_recycle_blob (hb_gpu_draw_t *draw,
hb_blob_t *blob);
HB_END_DECLS
#endif /* HB_GPU_H */