[util] Separate shape_text_options_t from text_options_t

This commit is contained in:
Behdad Esfahbod 2021-08-11 19:28:16 -06:00
parent 6ba7ddafed
commit 97a9e4e41e
7 changed files with 98 additions and 30 deletions

View File

@ -61,16 +61,19 @@ struct shape_closure_consumer_t
failed = false; failed = false;
buffer = hb_buffer_create (); buffer = hb_buffer_create ();
} }
void consume_line (const char *text, template <typename text_options_t>
unsigned int text_len, bool consume_line (text_options_t &text_opts)
const char *text_before,
const char *text_after)
{ {
unsigned int text_len;
const char *text;
if (!(text = text_opts.get_line (&text_len)))
return false;
hb_set_clear (glyphs); hb_set_clear (glyphs);
shaper.shape_closure (text, text_len, font, buffer, glyphs); shaper.shape_closure (text, text_len, font, buffer, glyphs);
if (hb_set_is_empty (glyphs)) if (hb_set_is_empty (glyphs))
return; return true;
/* Print it out! */ /* Print it out! */
bool first = true; bool first = true;
@ -88,6 +91,8 @@ struct shape_closure_consumer_t
} else } else
printf ("%u", i); printf ("%u", i);
} }
return true;
} }
void finish (const font_options_t *font_opts) void finish (const font_options_t *font_opts)
{ {

View File

@ -161,6 +161,6 @@ struct output_buffer_t : output_options_t
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
using main_t = main_font_text_t<shape_consumer_t<output_buffer_t>, font_options_t, text_options_t>; using main_t = main_font_text_t<shape_consumer_t<output_buffer_t>, font_options_t, shape_text_options_t>;
return batch_main<main_t> (argc, argv); return batch_main<main_t> (argc, argv);
} }

View File

@ -52,17 +52,19 @@ struct subset_consumer_t : subset_options_t, output_options_t
face = hb_face_reference (face_opts->face); face = hb_face_reference (face_opts->face);
} }
void consume_line (const char *text, bool consume_line (text_options_t &text_opts)
unsigned int text_len,
const char *text_before,
const char *text_after)
{ {
unsigned int text_len;
const char *text;
if (!(text = text_opts.get_line (&text_len)))
return false;
// TODO does this only get called with at least 1 codepoint? // TODO does this only get called with at least 1 codepoint?
hb_set_t *codepoints = hb_subset_input_unicode_set (input); hb_set_t *codepoints = hb_subset_input_unicode_set (input);
if (0 == strcmp (text, "*")) if (0 == strcmp (text, "*"))
{ {
hb_face_collect_unicodes (face, codepoints); hb_face_collect_unicodes (face, codepoints);
return; return true;
} }
gchar *c = (gchar *)text; gchar *c = (gchar *)text;
@ -71,6 +73,8 @@ struct subset_consumer_t : subset_options_t, output_options_t
hb_codepoint_t hb_cp = cp; hb_codepoint_t hb_cp = cp;
hb_set_add (codepoints, hb_cp); hb_set_add (codepoints, hb_cp);
} while ((c = g_utf8_find_next_char(c, text + text_len))); } while ((c = g_utf8_find_next_char(c, text + text_len)));
return true;
} }
hb_bool_t hb_bool_t

View File

@ -37,5 +37,5 @@ const unsigned SUBPIXEL_BITS = 6;
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
return main_font_text_t<shape_consumer_t<view_cairo_t>, font_options_t, text_options_t> () (argc, argv); return main_font_text_t<shape_consumer_t<view_cairo_t>, font_options_t, shape_text_options_t> () (argc, argv);
} }

View File

@ -41,10 +41,8 @@ struct main_font_text_t : option_parser_t, font_options_t, text_options_t, consu
this->init (this); this->init (this);
unsigned int line_len; while (this->consume_line (*this))
const char *line; ;
while ((line = this->get_line (&line_len)))
this->consume_line (line, line_len, this->text_before, this->text_after);
this->finish (this); this->finish (this);

View File

@ -29,6 +29,7 @@
#include "font-options.hh" #include "font-options.hh"
#include "shape-options.hh" #include "shape-options.hh"
#include "text-options.hh"
template <typename output_t> template <typename output_t>
@ -48,18 +49,20 @@ struct shape_consumer_t : shape_options_t
output.init (buffer, font_opts); output.init (buffer, font_opts);
} }
void consume_line (const char *text, bool consume_line (shape_text_options_t &text_opts)
unsigned int text_len,
const char *text_before,
const char *text_after)
{ {
unsigned int text_len;
const char *text;
if (!(text = text_opts.get_line (&text_len)))
return false;
output.new_line (); output.new_line ();
for (unsigned int n = num_iterations; n; n--) for (unsigned int n = num_iterations; n; n--)
{ {
const char *error = nullptr; const char *error = nullptr;
populate_buffer (buffer, text, text_len, text_before, text_after); populate_buffer (buffer, text, text_len, text_opts.text_before, text_opts.text_after);
if (n == 1) if (n == 1)
output.consume_text (buffer, text, text_len, utf8_clusters); output.consume_text (buffer, text, text_len, utf8_clusters);
if (!shape (font, buffer, &error)) if (!shape (font, buffer, &error))
@ -69,11 +72,12 @@ struct shape_consumer_t : shape_options_t
if (hb_buffer_get_content_type (buffer) == HB_BUFFER_CONTENT_TYPE_GLYPHS) if (hb_buffer_get_content_type (buffer) == HB_BUFFER_CONTENT_TYPE_GLYPHS)
break; break;
else else
return; return true;
} }
} }
output.consume_glyphs (buffer, text, text_len, utf8_clusters); output.consume_glyphs (buffer, text, text_len, utf8_clusters);
return true;
} }
void finish (const font_options_t *font_opts) void finish (const font_options_t *font_opts)
{ {

View File

@ -1,3 +1,33 @@
/*
* Copyright © 2011 Google, Inc.
*
* 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.
*
* Google Author(s): Behdad Esfahbod
*/
#ifndef TEXT_OPTIONS_HH
#define TEXT_OPTIONS_HH
#include "options.hh"
struct text_options_t struct text_options_t
{ {
@ -6,8 +36,6 @@ struct text_options_t
{} {}
~text_options_t () ~text_options_t ()
{ {
g_free (text_before);
g_free (text_after);
g_free (text); g_free (text);
g_free (text_file); g_free (text_file);
if (gs) if (gs)
@ -47,9 +75,6 @@ struct text_options_t
const char *get_line (unsigned int *len); const char *get_line (unsigned int *len);
char *text_before = nullptr;
char *text_after = nullptr;
int text_len = -1; int text_len = -1;
char *text = nullptr; char *text = nullptr;
char *text_file = nullptr; char *text_file = nullptr;
@ -59,6 +84,20 @@ struct text_options_t
GString *gs = nullptr; GString *gs = nullptr;
}; };
struct shape_text_options_t : text_options_t
{
~shape_text_options_t ()
{
g_free (text_before);
g_free (text_after);
}
void add_options (option_parser_t *parser);
char *text_before = nullptr;
char *text_after = nullptr;
};
static gboolean static gboolean
parse_text (const char *name G_GNUC_UNUSED, parse_text (const char *name G_GNUC_UNUSED,
@ -182,9 +221,7 @@ text_options_t::add_options (option_parser_t *parser)
{ {
{"text", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_text, "Set input text", "string"}, {"text", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_text, "Set input text", "string"},
{"text-file", 0, 0, G_OPTION_ARG_STRING, &this->text_file, "Set input text file-name", "filename"}, {"text-file", 0, 0, G_OPTION_ARG_STRING, &this->text_file, "Set input text file-name", "filename"},
{"unicodes", 'u', 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_unicodes, "Set input Unicode codepoints\n\n If no text is provided, standard input is used for input.\n", "list of hex numbers"}, {"unicodes", 'u', 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_unicodes, "Set input Unicode codepoints\n\n If no text is provided, standard input is used for input.", "list of hex numbers"},
{"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"},
{"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"},
{nullptr} {nullptr}
}; };
parser->add_group (entries, parser->add_group (entries,
@ -193,3 +230,23 @@ text_options_t::add_options (option_parser_t *parser)
"Options for the input text", "Options for the input text",
this); this);
} }
void
shape_text_options_t::add_options (option_parser_t *parser)
{
text_options_t::add_options (parser);
GOptionEntry entries[] =
{
{"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"},
{"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"},
{nullptr}
};
parser->add_group (entries,
"text-context",
"Textual context options:",
"Options for the input context text",
this);
}
#endif