2011-08-11 09:54:31 +00:00
|
|
|
/*
|
|
|
|
* 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 OPTIONS_HH
|
|
|
|
#define OPTIONS_HH
|
|
|
|
|
2018-08-26 05:36:36 +00:00
|
|
|
#include "hb.hh"
|
2019-05-17 22:30:01 +00:00
|
|
|
#include "hb-subset.h"
|
2011-09-19 20:41:17 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2017-08-12 02:51:06 +00:00
|
|
|
#include <assert.h>
|
2011-09-19 20:41:17 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2012-05-13 00:02:58 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h> /* for isatty() */
|
|
|
|
#endif
|
2013-02-12 20:35:32 +00:00
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__)
|
2013-01-31 23:18:05 +00:00
|
|
|
#include <io.h> /* for setmode() under Windows */
|
2011-09-19 20:41:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <hb.h>
|
2012-05-16 03:53:18 +00:00
|
|
|
#include <hb-ot.h>
|
2011-09-19 20:41:17 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gprintf.h>
|
|
|
|
|
2014-03-19 22:38:02 +00:00
|
|
|
void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
|
2011-09-19 20:41:17 +00:00
|
|
|
|
2011-09-13 17:30:39 +00:00
|
|
|
struct option_group_t
|
|
|
|
{
|
2018-12-17 18:01:01 +00:00
|
|
|
virtual ~option_group_t () {}
|
2018-09-30 09:46:20 +00:00
|
|
|
|
2011-09-13 17:30:39 +00:00
|
|
|
virtual void add_options (struct option_parser_t *parser) = 0;
|
|
|
|
|
2018-09-30 09:54:47 +00:00
|
|
|
virtual void pre_parse (GError **error G_GNUC_UNUSED) {}
|
|
|
|
virtual void post_parse (GError **error G_GNUC_UNUSED) {}
|
2011-09-13 17:30:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct option_parser_t
|
|
|
|
{
|
2018-09-30 10:06:26 +00:00
|
|
|
option_parser_t (const char *usage)
|
|
|
|
{
|
2011-09-13 17:30:39 +00:00
|
|
|
memset (this, 0, sizeof (*this));
|
|
|
|
usage_str = usage;
|
|
|
|
context = g_option_context_new (usage);
|
2014-07-04 22:09:29 +00:00
|
|
|
to_free = g_ptr_array_new ();
|
2011-09-13 17:30:39 +00:00
|
|
|
|
|
|
|
add_main_options ();
|
|
|
|
}
|
2019-01-26 13:04:51 +00:00
|
|
|
|
|
|
|
static void _g_free_g_func (void *p, void * G_GNUC_UNUSED) { g_free (p); }
|
|
|
|
|
2018-12-17 18:01:01 +00:00
|
|
|
~option_parser_t ()
|
2018-09-30 10:06:26 +00:00
|
|
|
{
|
2011-09-13 17:30:39 +00:00
|
|
|
g_option_context_free (context);
|
2019-01-26 13:04:51 +00:00
|
|
|
g_ptr_array_foreach (to_free, _g_free_g_func, nullptr);
|
2014-07-04 22:09:29 +00:00
|
|
|
g_ptr_array_free (to_free, TRUE);
|
2011-09-13 17:30:39 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 18:01:01 +00:00
|
|
|
void add_main_options ();
|
2011-09-13 17:30:39 +00:00
|
|
|
|
|
|
|
void add_group (GOptionEntry *entries,
|
|
|
|
const gchar *name,
|
|
|
|
const gchar *description,
|
|
|
|
const gchar *help_description,
|
|
|
|
option_group_t *option_group);
|
|
|
|
|
2014-07-04 22:09:29 +00:00
|
|
|
void free_later (char *p) {
|
|
|
|
g_ptr_array_add (to_free, p);
|
|
|
|
}
|
|
|
|
|
2011-09-13 17:30:39 +00:00
|
|
|
void parse (int *argc, char ***argv);
|
|
|
|
|
2018-12-17 18:01:01 +00:00
|
|
|
G_GNUC_NORETURN void usage () {
|
2011-09-13 17:30:39 +00:00
|
|
|
g_printerr ("Usage: %s [OPTION...] %s\n", g_get_prgname (), usage_str);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2014-07-04 22:09:29 +00:00
|
|
|
private:
|
2011-09-13 17:30:39 +00:00
|
|
|
const char *usage_str;
|
|
|
|
GOptionContext *context;
|
2014-07-04 22:09:29 +00:00
|
|
|
GPtrArray *to_free;
|
2011-09-13 17:30:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-05-13 10:51:02 +00:00
|
|
|
#define DEFAULT_MARGIN 16
|
2011-09-08 20:00:04 +00:00
|
|
|
#define DEFAULT_FORE "#000000"
|
|
|
|
#define DEFAULT_BACK "#FFFFFF"
|
2015-01-20 20:30:45 +00:00
|
|
|
#define FONT_SIZE_UPEM 0x7FFFFFFF
|
|
|
|
#define FONT_SIZE_NONE 0
|
2011-09-08 20:00:04 +00:00
|
|
|
|
2011-09-13 17:30:39 +00:00
|
|
|
struct view_options_t : option_group_t
|
2011-08-11 09:54:31 +00:00
|
|
|
{
|
2018-09-30 10:06:26 +00:00
|
|
|
view_options_t (option_parser_t *parser)
|
|
|
|
{
|
2011-09-13 17:30:39 +00:00
|
|
|
annotate = false;
|
2017-10-15 10:11:08 +00:00
|
|
|
fore = nullptr;
|
|
|
|
back = nullptr;
|
2011-09-13 17:30:39 +00:00
|
|
|
line_space = 0;
|
2020-05-15 01:58:53 +00:00
|
|
|
have_font_extents = false;
|
|
|
|
font_extents.ascent = font_extents.descent = font_extents.line_gap = 0;
|
2011-09-08 20:00:04 +00:00
|
|
|
margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
|
|
|
add_options (parser);
|
2011-08-11 09:54:31 +00:00
|
|
|
}
|
2019-06-03 15:31:50 +00:00
|
|
|
~view_options_t () override
|
2015-11-03 19:34:47 +00:00
|
|
|
{
|
|
|
|
g_free (fore);
|
|
|
|
g_free (back);
|
|
|
|
}
|
2011-08-11 09:54:31 +00:00
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
void add_options (option_parser_t *parser) override;
|
2011-09-08 21:08:32 +00:00
|
|
|
|
2011-09-20 18:43:55 +00:00
|
|
|
hb_bool_t annotate;
|
2015-11-03 19:34:47 +00:00
|
|
|
char *fore;
|
|
|
|
char *back;
|
2011-08-11 09:54:31 +00:00
|
|
|
double line_space;
|
2020-05-15 01:58:53 +00:00
|
|
|
bool have_font_extents;
|
|
|
|
struct font_extents_t {
|
|
|
|
double ascent, descent, line_gap;
|
|
|
|
} font_extents;
|
2011-08-11 09:54:31 +00:00
|
|
|
struct margin_t {
|
|
|
|
double t, r, b, l;
|
|
|
|
} margin;
|
2011-09-13 17:30:39 +00:00
|
|
|
};
|
2011-08-11 09:54:31 +00:00
|
|
|
|
2011-09-08 20:00:04 +00:00
|
|
|
|
2011-09-13 17:30:39 +00:00
|
|
|
struct shape_options_t : option_group_t
|
2011-08-11 09:54:31 +00:00
|
|
|
{
|
2012-06-02 16:21:19 +00:00
|
|
|
shape_options_t (option_parser_t *parser)
|
|
|
|
{
|
2017-10-15 10:11:08 +00:00
|
|
|
direction = language = script = nullptr;
|
2018-01-10 02:35:20 +00:00
|
|
|
bot = eot = preserve_default_ignorables = remove_default_ignorables = false;
|
2017-10-15 10:11:08 +00:00
|
|
|
features = nullptr;
|
2011-09-13 17:30:39 +00:00
|
|
|
num_features = 0;
|
2017-10-15 10:11:08 +00:00
|
|
|
shapers = nullptr;
|
2012-04-16 22:08:20 +00:00
|
|
|
utf8_clusters = false;
|
2018-10-08 00:46:11 +00:00
|
|
|
invisible_glyph = 0;
|
2015-07-22 15:51:12 +00:00
|
|
|
cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
|
2012-07-17 21:09:29 +00:00
|
|
|
normalize_glyphs = false;
|
2017-08-11 22:12:25 +00:00
|
|
|
verify = false;
|
2013-04-11 20:31:01 +00:00
|
|
|
num_iterations = 1;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
|
|
|
add_options (parser);
|
2011-08-11 09:54:31 +00:00
|
|
|
}
|
2019-06-03 15:31:50 +00:00
|
|
|
~shape_options_t () override
|
2012-06-02 16:21:19 +00:00
|
|
|
{
|
2015-11-03 19:34:47 +00:00
|
|
|
g_free (direction);
|
|
|
|
g_free (language);
|
|
|
|
g_free (script);
|
2011-09-08 20:42:37 +00:00
|
|
|
free (features);
|
2012-08-07 02:42:47 +00:00
|
|
|
g_strfreev (shapers);
|
2011-09-08 20:42:37 +00:00
|
|
|
}
|
2011-08-11 09:54:31 +00:00
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
void add_options (option_parser_t *parser) override;
|
2011-09-08 21:08:32 +00:00
|
|
|
|
2012-06-02 16:21:19 +00:00
|
|
|
void setup_buffer (hb_buffer_t *buffer)
|
|
|
|
{
|
2011-09-08 20:50:24 +00:00
|
|
|
hb_buffer_set_direction (buffer, hb_direction_from_string (direction, -1));
|
|
|
|
hb_buffer_set_script (buffer, hb_script_from_string (script, -1));
|
|
|
|
hb_buffer_set_language (buffer, hb_language_from_string (language, -1));
|
2018-01-10 02:35:20 +00:00
|
|
|
hb_buffer_set_flags (buffer, (hb_buffer_flags_t)
|
|
|
|
(HB_BUFFER_FLAG_DEFAULT |
|
|
|
|
(bot ? HB_BUFFER_FLAG_BOT : 0) |
|
|
|
|
(eot ? HB_BUFFER_FLAG_EOT : 0) |
|
|
|
|
(preserve_default_ignorables ? HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES : 0) |
|
|
|
|
(remove_default_ignorables ? HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES : 0) |
|
|
|
|
0));
|
2018-10-08 00:46:11 +00:00
|
|
|
hb_buffer_set_invisible_glyph (buffer, invisible_glyph);
|
2015-07-22 15:51:12 +00:00
|
|
|
hb_buffer_set_cluster_level (buffer, cluster_level);
|
2013-02-15 12:51:47 +00:00
|
|
|
hb_buffer_guess_segment_properties (buffer);
|
2011-09-08 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 02:51:06 +00:00
|
|
|
static void copy_buffer_properties (hb_buffer_t *dst, hb_buffer_t *src)
|
|
|
|
{
|
|
|
|
hb_segment_properties_t props;
|
|
|
|
hb_buffer_get_segment_properties (src, &props);
|
|
|
|
hb_buffer_set_segment_properties (dst, &props);
|
|
|
|
hb_buffer_set_flags (dst, hb_buffer_get_flags (src));
|
|
|
|
hb_buffer_set_cluster_level (dst, hb_buffer_get_cluster_level (src));
|
|
|
|
}
|
|
|
|
|
2012-11-13 23:12:24 +00:00
|
|
|
void populate_buffer (hb_buffer_t *buffer, const char *text, int text_len,
|
|
|
|
const char *text_before, const char *text_after)
|
2012-06-02 16:21:19 +00:00
|
|
|
{
|
2013-01-07 22:46:37 +00:00
|
|
|
hb_buffer_clear_contents (buffer);
|
2012-11-13 23:12:24 +00:00
|
|
|
if (text_before) {
|
|
|
|
unsigned int len = strlen (text_before);
|
|
|
|
hb_buffer_add_utf8 (buffer, text_before, len, len, 0);
|
|
|
|
}
|
2011-09-13 17:30:39 +00:00
|
|
|
hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
|
2012-11-13 23:12:24 +00:00
|
|
|
if (text_after) {
|
|
|
|
hb_buffer_add_utf8 (buffer, text_after, -1, 0, 0);
|
|
|
|
}
|
2012-01-22 00:07:22 +00:00
|
|
|
|
2012-04-16 22:08:20 +00:00
|
|
|
if (!utf8_clusters) {
|
|
|
|
/* Reset cluster values to refer to Unicode character index
|
|
|
|
* instead of UTF-8 index. */
|
|
|
|
unsigned int num_glyphs = hb_buffer_get_length (buffer);
|
2017-10-15 10:11:08 +00:00
|
|
|
hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, nullptr);
|
2012-04-16 22:08:20 +00:00
|
|
|
for (unsigned int i = 0; i < num_glyphs; i++)
|
|
|
|
{
|
|
|
|
info->cluster = i;
|
|
|
|
info++;
|
|
|
|
}
|
2012-01-22 00:07:22 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 20:49:02 +00:00
|
|
|
setup_buffer (buffer);
|
2012-06-02 16:21:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-15 10:11:08 +00:00
|
|
|
hb_bool_t shape (hb_font_t *font, hb_buffer_t *buffer, const char **error=nullptr)
|
2012-06-02 16:21:19 +00:00
|
|
|
{
|
2017-10-15 10:11:08 +00:00
|
|
|
hb_buffer_t *text_buffer = nullptr;
|
2017-08-12 02:51:06 +00:00
|
|
|
if (verify)
|
|
|
|
{
|
|
|
|
text_buffer = hb_buffer_create ();
|
|
|
|
hb_buffer_append (text_buffer, buffer, 0, -1);
|
|
|
|
}
|
|
|
|
|
2017-08-11 22:12:25 +00:00
|
|
|
if (!hb_shape_full (font, buffer, features, num_features, shapers))
|
|
|
|
{
|
|
|
|
if (error)
|
2019-08-24 13:27:14 +00:00
|
|
|
*error = "all shapers failed.";
|
2018-02-08 22:27:57 +00:00
|
|
|
goto fail;
|
2017-08-11 22:12:25 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 21:09:29 +00:00
|
|
|
if (normalize_glyphs)
|
|
|
|
hb_buffer_normalize_glyphs (buffer);
|
2017-08-11 22:12:25 +00:00
|
|
|
|
2017-08-12 02:51:06 +00:00
|
|
|
if (verify && !verify_buffer (buffer, text_buffer, font, error))
|
2018-02-08 22:27:57 +00:00
|
|
|
goto fail;
|
2017-08-11 22:12:25 +00:00
|
|
|
|
2017-08-12 02:51:06 +00:00
|
|
|
if (text_buffer)
|
|
|
|
hb_buffer_destroy (text_buffer);
|
|
|
|
|
2017-08-11 22:12:25 +00:00
|
|
|
return true;
|
2018-02-08 22:27:57 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (text_buffer)
|
|
|
|
hb_buffer_destroy (text_buffer);
|
|
|
|
|
|
|
|
return false;
|
2017-08-11 22:12:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 02:51:06 +00:00
|
|
|
bool verify_buffer (hb_buffer_t *buffer,
|
|
|
|
hb_buffer_t *text_buffer,
|
|
|
|
hb_font_t *font,
|
2017-10-15 10:11:08 +00:00
|
|
|
const char **error=nullptr)
|
2017-08-12 02:51:06 +00:00
|
|
|
{
|
|
|
|
if (!verify_buffer_monotone (buffer, error))
|
|
|
|
return false;
|
|
|
|
if (!verify_buffer_safe_to_break (buffer, text_buffer, font, error))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-15 10:11:08 +00:00
|
|
|
bool verify_buffer_monotone (hb_buffer_t *buffer, const char **error=nullptr)
|
2017-08-11 22:12:25 +00:00
|
|
|
{
|
|
|
|
/* Check that clusters are monotone. */
|
|
|
|
if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES ||
|
|
|
|
cluster_level == HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
|
|
|
|
{
|
|
|
|
bool is_forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
|
|
|
|
|
|
|
|
unsigned int num_glyphs;
|
|
|
|
hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
|
|
|
|
|
|
|
|
for (unsigned int i = 1; i < num_glyphs; i++)
|
|
|
|
if (info[i-1].cluster != info[i].cluster &&
|
|
|
|
(info[i-1].cluster < info[i].cluster) != is_forward)
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
*error = "clusters are not monotone.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-09-08 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 02:51:06 +00:00
|
|
|
bool verify_buffer_safe_to_break (hb_buffer_t *buffer,
|
|
|
|
hb_buffer_t *text_buffer,
|
|
|
|
hb_font_t *font,
|
2017-10-15 10:11:08 +00:00
|
|
|
const char **error=nullptr)
|
2017-08-12 02:51:06 +00:00
|
|
|
{
|
|
|
|
if (cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES &&
|
|
|
|
cluster_level != HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS)
|
|
|
|
{
|
|
|
|
/* Cannot perform this check without monotone clusters.
|
|
|
|
* Then again, unsafe-to-break flag is much harder to use without
|
|
|
|
* monotone clusters. */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that breaking up shaping at safe-to-break is indeed safe. */
|
|
|
|
|
|
|
|
hb_buffer_t *fragment = hb_buffer_create ();
|
|
|
|
hb_buffer_t *reconstruction = hb_buffer_create ();
|
|
|
|
copy_buffer_properties (reconstruction, buffer);
|
|
|
|
|
|
|
|
unsigned int num_glyphs;
|
|
|
|
hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, &num_glyphs);
|
|
|
|
|
|
|
|
unsigned int num_chars;
|
|
|
|
hb_glyph_info_t *text = hb_buffer_get_glyph_infos (text_buffer, &num_chars);
|
|
|
|
|
|
|
|
/* Chop text and shape fragments. */
|
|
|
|
bool forward = HB_DIRECTION_IS_FORWARD (hb_buffer_get_direction (buffer));
|
|
|
|
unsigned int start = 0;
|
|
|
|
unsigned int text_start = forward ? 0 : num_chars;
|
|
|
|
unsigned int text_end = text_start;
|
|
|
|
for (unsigned int end = 1; end < num_glyphs + 1; end++)
|
|
|
|
{
|
|
|
|
if (end < num_glyphs &&
|
|
|
|
(info[end].cluster == info[end-1].cluster ||
|
|
|
|
info[end-(forward?0:1)].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Shape segment corresponding to glyphs start..end. */
|
|
|
|
if (end == num_glyphs)
|
|
|
|
{
|
2019-08-24 13:27:14 +00:00
|
|
|
if (forward)
|
2017-08-12 02:51:06 +00:00
|
|
|
text_end = num_chars;
|
|
|
|
else
|
|
|
|
text_start = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (forward)
|
2017-08-31 00:25:10 +00:00
|
|
|
{
|
|
|
|
unsigned int cluster = info[end].cluster;
|
|
|
|
while (text_end < num_chars && text[text_end].cluster < cluster)
|
2017-08-12 02:51:06 +00:00
|
|
|
text_end++;
|
2017-08-31 00:25:10 +00:00
|
|
|
}
|
2017-08-12 02:51:06 +00:00
|
|
|
else
|
2017-08-31 00:25:10 +00:00
|
|
|
{
|
|
|
|
unsigned int cluster = info[end - 1].cluster;
|
|
|
|
while (text_start && text[text_start - 1].cluster >= cluster)
|
2017-08-12 02:51:06 +00:00
|
|
|
text_start--;
|
2017-08-31 00:25:10 +00:00
|
|
|
}
|
2017-08-12 02:51:06 +00:00
|
|
|
}
|
|
|
|
assert (text_start < text_end);
|
|
|
|
|
2017-08-16 00:12:28 +00:00
|
|
|
if (0)
|
|
|
|
printf("start %d end %d text start %d end %d\n", start, end, text_start, text_end);
|
|
|
|
|
2017-08-12 02:51:06 +00:00
|
|
|
hb_buffer_clear_contents (fragment);
|
|
|
|
copy_buffer_properties (fragment, buffer);
|
2017-08-16 00:12:28 +00:00
|
|
|
|
|
|
|
/* TODO: Add pre/post context text. */
|
|
|
|
hb_buffer_flags_t flags = hb_buffer_get_flags (fragment);
|
|
|
|
if (0 < text_start)
|
2019-08-24 13:27:14 +00:00
|
|
|
flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_BOT);
|
2017-08-16 00:12:28 +00:00
|
|
|
if (text_end < num_chars)
|
2019-08-24 13:27:14 +00:00
|
|
|
flags = (hb_buffer_flags_t) (flags & ~HB_BUFFER_FLAG_EOT);
|
2017-08-16 00:12:28 +00:00
|
|
|
hb_buffer_set_flags (fragment, flags);
|
|
|
|
|
2017-08-12 02:51:06 +00:00
|
|
|
hb_buffer_append (fragment, text_buffer, text_start, text_end);
|
|
|
|
if (!hb_shape_full (font, fragment, features, num_features, shapers))
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
*error = "all shapers failed while shaping fragment.";
|
2017-08-14 21:49:21 +00:00
|
|
|
hb_buffer_destroy (reconstruction);
|
|
|
|
hb_buffer_destroy (fragment);
|
2017-08-12 02:51:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hb_buffer_append (reconstruction, fragment, 0, -1);
|
|
|
|
|
|
|
|
start = end;
|
|
|
|
if (forward)
|
|
|
|
text_start = text_end;
|
|
|
|
else
|
|
|
|
text_end = text_start;
|
|
|
|
}
|
|
|
|
|
2017-08-16 00:12:28 +00:00
|
|
|
bool ret = true;
|
2017-09-05 03:14:13 +00:00
|
|
|
hb_buffer_diff_flags_t diff = hb_buffer_diff (reconstruction, buffer, (hb_codepoint_t) -1, 0);
|
2017-08-16 00:12:28 +00:00
|
|
|
if (diff)
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
*error = "Safe-to-break test failed.";
|
|
|
|
ret = false;
|
2017-08-12 02:51:06 +00:00
|
|
|
|
2017-08-31 00:07:29 +00:00
|
|
|
/* Return the reconstructed result instead so it can be inspected. */
|
|
|
|
hb_buffer_set_length (buffer, 0);
|
|
|
|
hb_buffer_append (buffer, reconstruction, 0, -1);
|
2017-08-16 00:12:28 +00:00
|
|
|
}
|
2017-08-12 02:51:06 +00:00
|
|
|
|
|
|
|
hb_buffer_destroy (reconstruction);
|
|
|
|
hb_buffer_destroy (fragment);
|
|
|
|
|
2017-08-16 00:12:28 +00:00
|
|
|
return ret;
|
2017-08-12 02:51:06 +00:00
|
|
|
}
|
|
|
|
|
2012-05-16 03:53:18 +00:00
|
|
|
void shape_closure (const char *text, int text_len,
|
|
|
|
hb_font_t *font, hb_buffer_t *buffer,
|
2012-06-02 16:21:19 +00:00
|
|
|
hb_set_t *glyphs)
|
|
|
|
{
|
2012-05-16 03:53:18 +00:00
|
|
|
hb_buffer_reset (buffer);
|
|
|
|
hb_buffer_add_utf8 (buffer, text, text_len, 0, text_len);
|
|
|
|
setup_buffer (buffer);
|
|
|
|
hb_ot_shape_glyphs_closure (font, buffer, features, num_features, glyphs);
|
|
|
|
}
|
|
|
|
|
2012-11-13 23:33:27 +00:00
|
|
|
/* Buffer properties */
|
2015-11-03 19:34:47 +00:00
|
|
|
char *direction;
|
|
|
|
char *language;
|
|
|
|
char *script;
|
2012-11-13 23:33:27 +00:00
|
|
|
|
|
|
|
/* Buffer flags */
|
|
|
|
hb_bool_t bot;
|
|
|
|
hb_bool_t eot;
|
|
|
|
hb_bool_t preserve_default_ignorables;
|
2018-01-10 02:35:20 +00:00
|
|
|
hb_bool_t remove_default_ignorables;
|
2012-11-13 23:33:27 +00:00
|
|
|
|
2011-08-11 09:54:31 +00:00
|
|
|
hb_feature_t *features;
|
|
|
|
unsigned int num_features;
|
|
|
|
char **shapers;
|
2012-04-16 22:08:20 +00:00
|
|
|
hb_bool_t utf8_clusters;
|
2018-10-08 00:46:11 +00:00
|
|
|
hb_codepoint_t invisible_glyph;
|
2015-07-22 15:51:12 +00:00
|
|
|
hb_buffer_cluster_level_t cluster_level;
|
2012-07-17 21:09:29 +00:00
|
|
|
hb_bool_t normalize_glyphs;
|
2017-08-11 22:12:25 +00:00
|
|
|
hb_bool_t verify;
|
2013-04-11 20:31:01 +00:00
|
|
|
unsigned int num_iterations;
|
2011-09-13 17:30:39 +00:00
|
|
|
};
|
2011-08-11 09:54:31 +00:00
|
|
|
|
2011-09-08 20:00:04 +00:00
|
|
|
|
2011-09-13 17:30:39 +00:00
|
|
|
struct font_options_t : option_group_t
|
2011-08-11 09:54:31 +00:00
|
|
|
{
|
2015-01-20 20:30:45 +00:00
|
|
|
font_options_t (option_parser_t *parser,
|
|
|
|
int default_font_size_,
|
2017-01-22 01:51:41 +00:00
|
|
|
unsigned int subpixel_bits_)
|
|
|
|
{
|
2017-10-15 10:11:08 +00:00
|
|
|
variations = nullptr;
|
2017-01-22 01:51:41 +00:00
|
|
|
num_variations = 0;
|
2015-01-20 20:30:45 +00:00
|
|
|
default_font_size = default_font_size_;
|
2018-02-22 23:24:12 +00:00
|
|
|
x_ppem = 0;
|
|
|
|
y_ppem = 0;
|
2018-02-25 09:00:33 +00:00
|
|
|
ptem = 0.;
|
2015-01-20 20:30:45 +00:00
|
|
|
subpixel_bits = subpixel_bits_;
|
2017-10-15 10:11:08 +00:00
|
|
|
font_file = nullptr;
|
2011-09-13 17:30:39 +00:00
|
|
|
face_index = 0;
|
2015-01-20 20:30:45 +00:00
|
|
|
font_size_x = font_size_y = default_font_size;
|
2017-10-15 10:11:08 +00:00
|
|
|
font_funcs = nullptr;
|
2018-09-11 12:21:15 +00:00
|
|
|
ft_load_flags = 2;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
2018-06-17 12:19:34 +00:00
|
|
|
blob = nullptr;
|
2017-10-15 10:11:08 +00:00
|
|
|
font = nullptr;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
|
|
|
add_options (parser);
|
|
|
|
}
|
2019-06-03 15:31:50 +00:00
|
|
|
~font_options_t () override
|
2018-09-30 10:06:26 +00:00
|
|
|
{
|
2015-11-03 19:34:47 +00:00
|
|
|
g_free (font_file);
|
2017-01-22 01:51:41 +00:00
|
|
|
free (variations);
|
2015-11-03 19:34:47 +00:00
|
|
|
g_free (font_funcs);
|
2011-09-13 17:30:39 +00:00
|
|
|
hb_font_destroy (font);
|
2011-08-11 09:54:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
void add_options (option_parser_t *parser) override;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
2018-12-17 18:01:01 +00:00
|
|
|
hb_font_t *get_font () const;
|
2011-09-08 21:08:32 +00:00
|
|
|
|
2015-11-03 19:34:47 +00:00
|
|
|
char *font_file;
|
2018-06-17 12:19:34 +00:00
|
|
|
mutable hb_blob_t *blob;
|
2011-08-11 09:54:31 +00:00
|
|
|
int face_index;
|
2017-01-22 01:51:41 +00:00
|
|
|
hb_variation_t *variations;
|
|
|
|
unsigned int num_variations;
|
2015-01-20 20:30:45 +00:00
|
|
|
int default_font_size;
|
2018-02-22 23:24:12 +00:00
|
|
|
int x_ppem;
|
|
|
|
int y_ppem;
|
|
|
|
double ptem;
|
2015-01-20 20:30:45 +00:00
|
|
|
unsigned int subpixel_bits;
|
|
|
|
mutable double font_size_x;
|
|
|
|
mutable double font_size_y;
|
2015-11-03 19:34:47 +00:00
|
|
|
char *font_funcs;
|
2018-09-11 12:21:15 +00:00
|
|
|
int ft_load_flags;
|
2011-08-11 09:54:31 +00:00
|
|
|
|
2018-06-18 15:29:33 +00:00
|
|
|
private:
|
2011-09-13 17:30:39 +00:00
|
|
|
mutable hb_font_t *font;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct text_options_t : option_group_t
|
|
|
|
{
|
2018-09-30 10:06:26 +00:00
|
|
|
text_options_t (option_parser_t *parser)
|
|
|
|
{
|
2017-10-15 10:11:08 +00:00
|
|
|
text_before = nullptr;
|
|
|
|
text_after = nullptr;
|
2012-11-13 23:12:24 +00:00
|
|
|
|
2018-11-06 15:49:19 +00:00
|
|
|
text_len = -1;
|
2017-10-15 10:11:08 +00:00
|
|
|
text = nullptr;
|
|
|
|
text_file = nullptr;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
2017-10-15 10:11:08 +00:00
|
|
|
fp = nullptr;
|
|
|
|
gs = nullptr;
|
|
|
|
line = nullptr;
|
2019-12-15 14:50:01 +00:00
|
|
|
line_len = UINT_MAX;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
|
|
|
add_options (parser);
|
|
|
|
}
|
2019-06-03 15:31:50 +00:00
|
|
|
~text_options_t () override
|
2018-09-30 10:06:26 +00:00
|
|
|
{
|
2015-11-03 19:34:47 +00:00
|
|
|
g_free (text_before);
|
|
|
|
g_free (text_after);
|
|
|
|
g_free (text);
|
|
|
|
g_free (text_file);
|
2011-09-16 06:08:36 +00:00
|
|
|
if (gs)
|
2012-06-06 00:35:40 +00:00
|
|
|
g_string_free (gs, true);
|
2018-10-30 08:24:23 +00:00
|
|
|
if (fp && fp != stdin)
|
2011-09-16 06:08:36 +00:00
|
|
|
fclose (fp);
|
2011-09-13 17:30:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
void add_options (option_parser_t *parser) override;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
void post_parse (GError **error G_GNUC_UNUSED) override {
|
2011-09-13 17:30:39 +00:00
|
|
|
if (text && text_file)
|
|
|
|
g_set_error (error,
|
|
|
|
G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
|
2012-05-12 13:54:27 +00:00
|
|
|
"Only one of text and text-file can be set");
|
2018-10-30 07:59:09 +00:00
|
|
|
}
|
2011-09-13 17:30:39 +00:00
|
|
|
|
|
|
|
const char *get_line (unsigned int *len);
|
|
|
|
|
2015-11-03 19:34:47 +00:00
|
|
|
char *text_before;
|
|
|
|
char *text_after;
|
2012-11-13 23:12:24 +00:00
|
|
|
|
2018-11-06 15:49:19 +00:00
|
|
|
int text_len;
|
2015-11-03 19:34:47 +00:00
|
|
|
char *text;
|
|
|
|
char *text_file;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
|
|
|
private:
|
2011-09-16 06:08:36 +00:00
|
|
|
FILE *fp;
|
|
|
|
GString *gs;
|
2015-11-03 19:34:47 +00:00
|
|
|
char *line;
|
|
|
|
unsigned int line_len;
|
2011-09-13 17:30:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct output_options_t : option_group_t
|
|
|
|
{
|
2012-12-21 21:46:53 +00:00
|
|
|
output_options_t (option_parser_t *parser,
|
2018-09-30 10:06:26 +00:00
|
|
|
const char **supported_formats_ = nullptr)
|
|
|
|
{
|
2017-10-15 10:11:08 +00:00
|
|
|
output_file = nullptr;
|
|
|
|
output_format = nullptr;
|
2012-12-21 21:46:53 +00:00
|
|
|
supported_formats = supported_formats_;
|
2012-12-21 21:01:52 +00:00
|
|
|
explicit_output_format = false;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
2017-10-15 10:11:08 +00:00
|
|
|
fp = nullptr;
|
2011-09-15 21:52:00 +00:00
|
|
|
|
2011-09-13 17:30:39 +00:00
|
|
|
add_options (parser);
|
|
|
|
}
|
2019-06-03 15:31:50 +00:00
|
|
|
~output_options_t () override
|
2018-09-30 10:06:26 +00:00
|
|
|
{
|
2015-11-03 19:34:47 +00:00
|
|
|
g_free (output_file);
|
|
|
|
g_free (output_format);
|
2018-10-30 08:24:23 +00:00
|
|
|
if (fp && fp != stdout)
|
2011-09-15 21:52:00 +00:00
|
|
|
fclose (fp);
|
|
|
|
}
|
2011-09-13 17:30:39 +00:00
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
void add_options (option_parser_t *parser) override;
|
2011-09-13 17:30:39 +00:00
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
void post_parse (GError **error G_GNUC_UNUSED) override
|
2011-09-13 17:30:39 +00:00
|
|
|
{
|
2012-12-21 21:01:52 +00:00
|
|
|
if (output_format)
|
|
|
|
explicit_output_format = true;
|
|
|
|
|
2011-09-13 17:30:39 +00:00
|
|
|
if (output_file && !output_format) {
|
|
|
|
output_format = strrchr (output_file, '.');
|
|
|
|
if (output_format)
|
2015-11-10 19:37:01 +00:00
|
|
|
{
|
2011-09-13 17:30:39 +00:00
|
|
|
output_format++; /* skip the dot */
|
2018-10-19 20:21:39 +00:00
|
|
|
output_format = g_strdup (output_format);
|
2015-11-10 19:37:01 +00:00
|
|
|
}
|
2011-09-13 17:30:39 +00:00
|
|
|
}
|
|
|
|
|
2011-09-15 21:52:00 +00:00
|
|
|
if (output_file && 0 == strcmp (output_file, "-"))
|
2017-10-15 10:11:08 +00:00
|
|
|
output_file = nullptr; /* STDOUT */
|
2011-09-15 21:52:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 18:01:01 +00:00
|
|
|
FILE *get_file_handle ();
|
2011-08-11 09:54:31 +00:00
|
|
|
|
2015-11-03 19:34:47 +00:00
|
|
|
char *output_file;
|
|
|
|
char *output_format;
|
2014-03-19 22:38:02 +00:00
|
|
|
const char **supported_formats;
|
2012-12-21 21:01:52 +00:00
|
|
|
bool explicit_output_format;
|
2011-09-15 21:52:00 +00:00
|
|
|
|
|
|
|
mutable FILE *fp;
|
2011-09-13 17:30:39 +00:00
|
|
|
};
|
2011-08-11 09:54:31 +00:00
|
|
|
|
2011-09-19 20:41:17 +00:00
|
|
|
struct format_options_t : option_group_t
|
|
|
|
{
|
|
|
|
format_options_t (option_parser_t *parser) {
|
|
|
|
show_glyph_names = true;
|
|
|
|
show_positions = true;
|
2018-01-10 01:20:14 +00:00
|
|
|
show_advances = true;
|
2011-09-19 20:41:17 +00:00
|
|
|
show_clusters = true;
|
2012-01-19 17:32:20 +00:00
|
|
|
show_text = false;
|
|
|
|
show_unicode = false;
|
2012-01-19 17:46:18 +00:00
|
|
|
show_line_num = false;
|
2015-08-24 12:49:55 +00:00
|
|
|
show_extents = false;
|
2016-05-02 12:47:45 +00:00
|
|
|
show_flags = false;
|
2017-07-20 00:20:55 +00:00
|
|
|
trace = false;
|
2011-09-19 20:41:17 +00:00
|
|
|
|
|
|
|
add_options (parser);
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
void add_options (option_parser_t *parser) override;
|
2011-09-19 20:41:17 +00:00
|
|
|
|
2020-09-17 15:49:59 +00:00
|
|
|
void serialize (hb_buffer_t *buffer,
|
2012-01-19 17:46:18 +00:00
|
|
|
hb_font_t *font,
|
2012-11-15 20:14:09 +00:00
|
|
|
hb_buffer_serialize_format_t format,
|
|
|
|
hb_buffer_serialize_flags_t flags,
|
2012-01-19 17:46:18 +00:00
|
|
|
GString *gs);
|
|
|
|
void serialize_line_no (unsigned int line_no,
|
|
|
|
GString *gs);
|
2012-06-02 16:13:08 +00:00
|
|
|
void serialize_buffer_of_text (hb_buffer_t *buffer,
|
|
|
|
unsigned int line_no,
|
|
|
|
const char *text,
|
|
|
|
unsigned int text_len,
|
|
|
|
hb_font_t *font,
|
|
|
|
GString *gs);
|
|
|
|
void serialize_message (unsigned int line_no,
|
2017-08-16 00:30:18 +00:00
|
|
|
const char *type,
|
2012-06-02 16:13:08 +00:00
|
|
|
const char *msg,
|
|
|
|
GString *gs);
|
|
|
|
void serialize_buffer_of_glyphs (hb_buffer_t *buffer,
|
|
|
|
unsigned int line_no,
|
|
|
|
const char *text,
|
|
|
|
unsigned int text_len,
|
|
|
|
hb_font_t *font,
|
2012-11-15 20:14:09 +00:00
|
|
|
hb_buffer_serialize_format_t output_format,
|
|
|
|
hb_buffer_serialize_flags_t format_flags,
|
2012-06-02 16:13:08 +00:00
|
|
|
GString *gs);
|
2012-01-19 17:46:18 +00:00
|
|
|
|
2011-09-19 20:41:17 +00:00
|
|
|
|
2011-09-20 18:43:55 +00:00
|
|
|
hb_bool_t show_glyph_names;
|
|
|
|
hb_bool_t show_positions;
|
2018-01-10 01:20:14 +00:00
|
|
|
hb_bool_t show_advances;
|
2011-09-20 18:43:55 +00:00
|
|
|
hb_bool_t show_clusters;
|
2012-01-19 17:32:20 +00:00
|
|
|
hb_bool_t show_text;
|
|
|
|
hb_bool_t show_unicode;
|
2012-01-19 17:46:18 +00:00
|
|
|
hb_bool_t show_line_num;
|
2015-08-24 12:49:55 +00:00
|
|
|
hb_bool_t show_extents;
|
2016-05-02 12:47:45 +00:00
|
|
|
hb_bool_t show_flags;
|
2017-07-20 00:20:55 +00:00
|
|
|
hb_bool_t trace;
|
2011-09-19 20:41:17 +00:00
|
|
|
};
|
|
|
|
|
2018-02-21 18:34:00 +00:00
|
|
|
struct subset_options_t : option_group_t
|
|
|
|
{
|
|
|
|
subset_options_t (option_parser_t *parser)
|
|
|
|
{
|
2019-05-17 22:30:01 +00:00
|
|
|
input = hb_subset_input_create_or_fail ();
|
2018-02-21 18:34:00 +00:00
|
|
|
add_options (parser);
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
~subset_options_t () override
|
2019-04-05 17:05:55 +00:00
|
|
|
{
|
2019-05-17 22:30:01 +00:00
|
|
|
hb_subset_input_destroy (input);
|
2019-04-05 17:05:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 15:31:50 +00:00
|
|
|
void add_options (option_parser_t *parser) override;
|
2018-02-21 18:34:00 +00:00
|
|
|
|
2019-05-17 22:30:01 +00:00
|
|
|
hb_subset_input_t *input;
|
2018-02-21 18:34:00 +00:00
|
|
|
};
|
|
|
|
|
2015-11-03 10:49:34 +00:00
|
|
|
/* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
|
|
|
|
#if defined (_MSC_VER) && (_MSC_VER < 1800)
|
|
|
|
|
|
|
|
#ifndef FLT_RADIX
|
|
|
|
#define FLT_RADIX 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
__inline long double scalbn (long double x, int exp)
|
|
|
|
{
|
|
|
|
return x * (pow ((long double) FLT_RADIX, exp));
|
|
|
|
}
|
|
|
|
|
|
|
|
__inline float scalbnf (float x, int exp)
|
|
|
|
{
|
|
|
|
return x * (pow ((float) FLT_RADIX, exp));
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-11 09:54:31 +00:00
|
|
|
|
|
|
|
#endif
|