[util] Use inheritence to embed inherent options of objects

This commit is contained in:
Behdad Esfahbod 2021-08-06 22:53:44 -06:00
parent 06d661803c
commit 4e97678ebb
3 changed files with 27 additions and 30 deletions

View File

@ -38,18 +38,18 @@ const unsigned SUBPIXEL_BITS = 0;
* Command line interface to the harfbuzz font subsetter.
*/
struct subset_consumer_t
struct subset_consumer_t : subset_options_t
{
void add_options (option_parser_t *parser)
{
subset_options_t::add_options (parser);
output_options.add_options (parser);
subset_options.add_options (parser);
}
void init (const face_options_t *face_opts)
{
face = hb_face_reference (face_opts->get_face ());
input = hb_subset_input_reference (subset_options.get_input ());
input = hb_subset_input_reference (get_input ());
}
void consume_line (const char *text,
@ -102,7 +102,7 @@ struct subset_consumer_t
void finish (const face_options_t *face_opts)
{
hb_face_t *new_face = nullptr;
for (unsigned i = 0; i < subset_options.num_iterations; i++)
for (unsigned i = 0; i < num_iterations; i++)
{
hb_face_destroy (new_face);
new_face = hb_subset_or_fail (face, input);
@ -126,7 +126,6 @@ struct subset_consumer_t
private:
output_options_t output_options;
subset_options_t subset_options;
hb_face_t *face = nullptr;
hb_subset_input_t *input = nullptr;

View File

@ -32,11 +32,11 @@
template <typename output_t>
struct shape_consumer_t
struct shape_consumer_t : shape_options_t
{
void add_options (option_parser_t *parser)
{
shaper.add_options (parser);
shape_options_t::add_options (parser);
output.add_options (parser);
}
@ -55,14 +55,14 @@ struct shape_consumer_t
{
output.new_line ();
for (unsigned int n = shaper.num_iterations; n; n--)
for (unsigned int n = num_iterations; n; n--)
{
const char *error = nullptr;
shaper.populate_buffer (buffer, text, text_len, text_before, text_after);
populate_buffer (buffer, text, text_len, text_before, text_after);
if (n == 1)
output.consume_text (buffer, text, text_len, shaper.utf8_clusters);
if (!shaper.shape (font, buffer, &error))
output.consume_text (buffer, text, text_len, utf8_clusters);
if (!shape (font, buffer, &error))
{
failed = true;
output.error (error);
@ -73,7 +73,7 @@ struct shape_consumer_t
}
}
output.consume_glyphs (buffer, text, text_len, shaper.utf8_clusters);
output.consume_glyphs (buffer, text, text_len, utf8_clusters);
}
void finish (const font_options_t *font_opts)
{
@ -88,7 +88,6 @@ struct shape_consumer_t
bool failed = false;
protected:
shape_options_t shaper;
output_t output;
hb_font_t *font = nullptr;

View File

@ -32,7 +32,7 @@
#include "helper-cairo.hh"
struct view_cairo_t
struct view_cairo_t : view_options_t
{
~view_cairo_t ()
{
@ -41,8 +41,8 @@ struct view_cairo_t
void add_options (option_parser_t *parser)
{
view_options_t::add_options (parser);
output_options.add_options (parser, helper_cairo_supported_formats);
view_options.add_options (parser);
}
void init (hb_buffer_t *buffer, const font_options_t *font_opts)
@ -87,7 +87,6 @@ struct view_cairo_t
void render (const font_options_t *font_opts);
output_options_t output_options;
view_options_t view_options;
hb_direction_t direction = HB_DIRECTION_INVALID; // Remove this, make segment_properties accessible
GArray *lines = nullptr;
@ -106,24 +105,24 @@ view_cairo_t::render (const font_options_t *font_opts)
hb_font_t *font = font_opts->get_font();
view_options_t::font_extents_t extents = view_options.font_extents;
if (!view_options.have_font_extents)
if (!have_font_extents)
{
hb_font_extents_t hb_extents;
hb_font_get_extents_for_direction (font, direction, &hb_extents);
extents.ascent = scalbn ((double) hb_extents.ascender, scale_bits);
extents.descent = -scalbn ((double) hb_extents.descender, scale_bits);
extents.line_gap = scalbn ((double) hb_extents.line_gap, scale_bits);
font_extents.ascent = scalbn ((double) hb_extents.ascender, scale_bits);
font_extents.descent = -scalbn ((double) hb_extents.descender, scale_bits);
font_extents.line_gap = scalbn ((double) hb_extents.line_gap, scale_bits);
have_font_extents = true;
}
double ascent = y_sign * extents.ascent;
double descent = y_sign * extents.descent;
double line_gap = y_sign * extents.line_gap + view_options.line_space;
double ascent = y_sign * font_extents.ascent;
double descent = y_sign * font_extents.descent;
double line_gap = y_sign * font_extents.line_gap + line_space;
double leading = ascent + descent + line_gap;
/* Calculate surface size. */
double w = 0, h = 0;
(vertical ? w : h) = (int) lines->len * leading - (extents.line_gap + view_options.line_space);
(vertical ? w : h) = (int) lines->len * leading - (font_extents.line_gap + line_space);
(vertical ? h : w) = 0;
for (unsigned int i = 0; i < lines->len; i++) {
helper_cairo_line_t &line = g_array_index (lines, helper_cairo_line_t, i);
@ -143,13 +142,13 @@ view_cairo_t::render (const font_options_t *font_opts)
content = CAIRO_CONTENT_COLOR;
/* Create surface. */
cairo_t *cr = helper_cairo_create_context (w + view_options.margin.l + view_options.margin.r,
h + view_options.margin.t + view_options.margin.b,
&view_options, &output_options, content);
cairo_t *cr = helper_cairo_create_context (w + margin.l + margin.r,
h + margin.t + margin.b,
static_cast<view_options_t *> (this), &output_options, content);
cairo_set_scaled_font (cr, scaled_font);
/* Setup coordinate system. */
cairo_translate (cr, view_options.margin.l, view_options.margin.t);
cairo_translate (cr, margin.l, margin.t);
if (vertical)
cairo_translate (cr,
w - ascent, /* We currently always stack lines right to left */
@ -169,7 +168,7 @@ view_cairo_t::render (const font_options_t *font_opts)
cairo_translate (cr, -vert * leading, +horiz * leading);
if (view_options.annotate) {
if (annotate) {
cairo_save (cr);
/* Draw actual glyph origins */