[pango/simple-fontmap: 4/15] Add PangoHbFace
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pango/simple-fontmap: 4/15] Add PangoHbFace
- Date: Sun, 31 Oct 2021 23:51:10 +0000 (UTC)
commit 80a9d0a4bd9fadd465d3e20356f190c79d46e843
Author: Matthias Clasen <mclasen redhat com>
Date: Fri Oct 29 19:18:27 2021 -0400
Add PangoHbFace
PangoHbFace is a simple wrapper around hb_face_t.
This is an attempt to make a font backend that does
not depend on platform specifics and can be used in
tests.
pango/meson.build | 2 +
pango/pango-hbface-private.h | 32 ++++
pango/pango-hbface.c | 405 +++++++++++++++++++++++++++++++++++++++++++
pango/pango-hbface.h | 50 ++++++
4 files changed, 489 insertions(+)
---
diff --git a/pango/meson.build b/pango/meson.build
index 084de229..381ee155 100644
--- a/pango/meson.build
+++ b/pango/meson.build
@@ -27,6 +27,7 @@ pango_sources = [
'pango-utils.c',
'reorder-items.c',
'shape.c',
+ 'pango-hbface.c',
]
pango_headers = [
@@ -56,6 +57,7 @@ pango_headers = [
'pango-tabs.h',
'pango-types.h',
'pango-utils.h',
+ 'pango-hbface.h',
]
pango_installed_headers = pango_headers + [ 'pango-version-macros.h' ]
diff --git a/pango/pango-hbface-private.h b/pango/pango-hbface-private.h
new file mode 100644
index 00000000..ace0f8e2
--- /dev/null
+++ b/pango/pango-hbface-private.h
@@ -0,0 +1,32 @@
+#pragma once
+
+#include "pango-hbface.h"
+#include "pango-fontmap.h"
+#include <hb.h>
+
+struct _PangoHbFace
+{
+ PangoFontFace parent_instance;
+
+ hb_blob_t *blob;
+ unsigned int index;
+ hb_face_t *face;
+ PangoFontDescription *description;
+ char *name;
+ const char *family;
+ PangoMatrix matrix;
+ double x_scale, y_scale;
+ PangoLanguage **languages;
+
+ PangoFontMap *map;
+};
+
+void pango_hb_face_set_font_map (PangoHbFace *self,
+ PangoFontMap *map);
+
+gboolean pango_hb_face_is_monospace (PangoHbFace *self);
+gboolean pango_hb_face_is_variable (PangoHbFace *self);
+PangoLanguage **pango_hb_face_get_languages (PangoHbFace *self);
+void pango_hb_face_set_languages (PangoHbFace *self,
+ const PangoLanguage **languages,
+ gsize n_languages);
diff --git a/pango/pango-hbface.c b/pango/pango-hbface.c
new file mode 100644
index 00000000..a95235d3
--- /dev/null
+++ b/pango/pango-hbface.c
@@ -0,0 +1,405 @@
+/* Pango
+ *
+ * Copyright (C) 2021 Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "pango-hbface-private.h"
+
+#include <hb-ot.h>
+#include <hb-ft.h>
+
+/* {{{ PangoFontFace implementation */
+
+struct _PangoHbFaceClass
+{
+ PangoFontFaceClass parent_class;
+};
+
+G_DEFINE_TYPE (PangoHbFace, pango_hb_face, PANGO_TYPE_FONT_FACE)
+
+static void
+pango_hb_face_init (PangoHbFace *self)
+{
+}
+
+static void
+pango_hb_face_finalize (GObject *object)
+{
+ PangoHbFace *self = PANGO_HB_FACE (object);
+
+ hb_blob_destroy (self->blob);
+ hb_face_destroy (self->face);
+ pango_font_description_free (self->description);
+ if (self->map)
+ g_object_remove_weak_pointer (G_OBJECT (self->map), (gpointer *)&self->map);
+ g_free (self->name);
+ g_free (self->languages);
+
+ G_OBJECT_CLASS (pango_hb_face_parent_class)->finalize (object);
+}
+
+static const char *
+pango_hb_face_get_face_name (PangoFontFace *face)
+{
+ PangoHbFace *self = PANGO_HB_FACE (face);
+
+ return self->name;
+}
+
+static PangoFontDescription *
+pango_hb_face_describe (PangoFontFace *face)
+{
+ PangoHbFace *self = PANGO_HB_FACE (face);
+
+ return pango_font_description_copy (self->description);
+}
+
+static PangoFontFamily *
+pango_hb_face_get_family (PangoFontFace *face)
+{
+ PangoHbFace *self = PANGO_HB_FACE (face);
+
+ if (self->map)
+ return pango_font_map_get_family (self->map, self->family);
+
+ return NULL;
+}
+
+static void
+pango_hb_face_class_init (PangoHbFaceClass *class)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (class);
+ PangoFontFaceClass *face_class = PANGO_FONT_FACE_CLASS (class);
+
+ object_class->finalize = pango_hb_face_finalize;
+
+ face_class->get_face_name = pango_hb_face_get_face_name;
+ face_class->describe = pango_hb_face_describe;
+ face_class->list_sizes = NULL;
+ face_class->is_synthesized = NULL;
+ face_class->get_family = pango_hb_face_get_family;
+}
+
+/* }}} */
+/* {{{ Utilities */
+
+static char *
+get_name_from_hb_face (hb_face_t *face,
+ hb_ot_name_id_t name_id)
+{
+ char buf[256];
+ unsigned int len = sizeof (buf);
+ hb_language_t language;
+
+ language = hb_language_from_string ("en", -1);
+
+ hb_ot_name_get_utf8 (face, name_id, language, &len, buf);
+
+ return g_strdup (buf);
+}
+
+static PangoHbFace *
+pango_hb_face_new_from_blob (hb_blob_t *blob,
+ unsigned int index)
+{
+ PangoHbFace *self;
+ char *name;
+
+ self = g_object_new (PANGO_TYPE_HB_FACE, NULL);
+
+ self->blob = hb_blob_reference (blob);
+ self->index = index;
+ self->face = hb_face_create (self->blob, index);
+
+ name = get_name_from_hb_face (self->face, HB_OT_NAME_ID_FULL_NAME);
+ self->description = pango_font_description_from_string (name);
+ g_free (name);
+
+ self->family = pango_font_description_get_family (self->description);
+
+ self->name = get_name_from_hb_face (self->face, HB_OT_NAME_ID_TYPOGRAPHIC_SUBFAMILY);
+ if (!self->name || self->name[0] == '\0')
+ {
+ g_free (self->name);
+ self->name = get_name_from_hb_face (self->face, HB_OT_NAME_ID_FONT_SUBFAMILY);
+ }
+
+ self->matrix = (PangoMatrix) PANGO_MATRIX_INIT;
+ self->x_scale = self->y_scale = 1.;
+
+ return self;
+}
+
+/* }}} */
+/* {{{ Private API */
+
+/*< private >
+ * pango_hb_face_set_font_map:
+ * @self: a `PangoHbFace`
+ * @map: a `PangoFontMap`
+ *
+ * Sets the font map of a `PangoHbFace`.
+ *
+ * This should only be called by fontmap implementations.
+ */
+void
+pango_hb_face_set_font_map (PangoHbFace *self,
+ PangoFontMap *map)
+{
+ self->map = map;
+ g_object_add_weak_pointer (G_OBJECT (map), (gpointer *)&self->map);
+}
+
+typedef struct {
+ guint16 major;
+ guint16 minor;
+ gint32 italicAngle;
+ gint16 underlinePosition;
+ gint16 underlineThickness;
+ guint32 isFixedPitch;
+} PostTable;
+
+/*< private >
+ * pango_hb_face_is_monospace:
+ * @face: a `PangoHbFace`
+ *
+ * Returns whether @face should be considered monospace.
+ *
+ * Returns: `TRUE` if @face is monospace
+ */
+gboolean
+pango_hb_face_is_monospace (PangoHbFace *face)
+{
+ hb_blob_t *post_blob;
+ guint8 *raw_post;
+ unsigned int post_len;
+ gboolean res;
+
+ post_blob = hb_face_reference_table (face->face, HB_TAG ('p', 'o', 's', 't'));
+ raw_post = (guint8 *) hb_blob_get_data (post_blob, &post_len);
+ res = ((PostTable *)raw_post)->isFixedPitch != 0;
+
+ hb_blob_destroy (post_blob);
+
+ return res;
+}
+
+/*< private >
+ * pango_hb_face_is_variable:
+ * @face: a `PangoHbFace`
+ *
+ * Returns whether @face should be considered a variable font.
+ *
+ * Returns: `TRUE` if @face is variable
+ */
+gboolean
+pango_hb_face_is_variable (PangoHbFace *face)
+{
+ return hb_ot_var_get_axis_count (face->face) > 0;
+}
+
+/*< private >
+ * pango_hb_face_get_languages:
+ * @face: a `PangoHbFace`
+ *
+ * Returns the languages supported by @face.
+ *
+ * Returns: (transfer one) (nullable): a NULL-terminated
+ * array of `PangoLanguage *`
+ */
+PangoLanguage **
+pango_hb_face_get_languages (PangoHbFace *face)
+{
+ return face->languages;
+}
+
+/*< private >
+ * pango_hb_face_set_languages:
+ * @face: a `PangoHbFace`
+ * @languages: (array length=n_languages): the languages supported by @face
+ * @n_languages: the number of languages
+ *
+ * Sets the languages that are supported by @face.
+ *
+ * This should only be called by fontmap implementations.
+ */
+void
+pango_hb_face_set_languages (PangoHbFace *face,
+ const PangoLanguage **languages,
+ gsize n_languages)
+{
+ // It would be nicer if HB_OT_META_TAG_SUPPORTED_LANGUAGES was useful
+ g_free (face->languages);
+ face->languages = g_new (PangoLanguage *, n_languages + 1);
+ memcpy (face->languages, languages, n_languages * sizeof (PangoLanguage *));
+ face->languages[n_languages] = NULL;
+}
+
+/* }}} */
+/* {{{ Public API */
+
+/**
+ * pango_hb_face_new_from_bytes:
+ * @bytes: a `GBytes` containing font data
+ * @index: face index
+ *
+ * Creates a new `PangoHbFace` from font data in memory.
+ *
+ * Returns: a newly created `PangoHbFace`
+ */
+PangoHbFace *
+pango_hb_face_new_from_bytes (GBytes *bytes,
+ unsigned int index)
+{
+ hb_blob_t *blob;
+ PangoHbFace *face;
+
+ blob = hb_blob_create (g_bytes_get_data (bytes, NULL),
+ g_bytes_get_size (bytes),
+ HB_MEMORY_MODE_READONLY,
+ g_bytes_ref (bytes),
+ (hb_destroy_func_t) g_bytes_unref);
+
+ face = pango_hb_face_new_from_blob (blob, index);
+
+ hb_blob_destroy (blob);
+
+ return face;
+}
+
+/**
+ * pango_hb_face_new_from_file:
+ * @file: font filename
+ * @index: face index
+ *
+ * Creates a new `PangoHbFace` from a font file.
+ *
+ * Returns: a newly created `PangoHbFace`
+ */
+PangoHbFace *
+pango_hb_face_new_from_file (const char *file,
+ unsigned int index)
+{
+ hb_blob_t *blob;
+ PangoHbFace *face;
+
+ blob = hb_blob_create_from_file (file);
+
+ face = pango_hb_face_new_from_blob (blob, index);
+
+ hb_blob_destroy (blob);
+
+ return face;
+}
+
+/**
+ * pango_hb_face_new_transformed:
+ * @face: a `PangoHbFace`
+ * @name: the name for the transformed face
+ * @transform: the transform to apply
+ *
+ * Creates a new `PangoHbFace` that applies the
+ * given @transform to the glyphs in @face.
+ *
+ * Note that the @name should not include the family
+ * name. For example, when using this to create a
+ * synthetic Italic, the name should just be "Italic".
+ *
+ * Returns: (transfer full): a newly created `PangoHbFace`
+ */
+PangoHbFace *
+pango_hb_face_new_transformed (PangoHbFace *face,
+ const char *name,
+ const PangoMatrix *transform)
+{
+ PangoHbFace *self;
+ char *fullname;
+
+ g_return_val_if_fail (PANGO_IS_HB_FACE (face), NULL);
+ g_return_val_if_fail (name && *name, NULL);
+ g_return_val_if_fail (transform != NULL, NULL);
+
+ self = g_object_new (PANGO_TYPE_HB_FACE, NULL);
+
+ self->blob = hb_blob_reference (face->blob);
+ self->index = face->index;
+ self->face = hb_face_reference (face->face);
+
+ fullname = g_strconcat (face->family, " ", name, NULL);
+ self->description = pango_font_description_from_string (fullname);
+ g_free (fullname);
+
+ self->family = pango_font_description_get_family (self->description);
+ self->name = g_strdup (name);
+ self->matrix = *transform;
+ pango_matrix_get_font_scale_factors (&self->matrix, &self->x_scale, &self->y_scale);
+ pango_matrix_scale (&self->matrix, 1./self->x_scale, 1./self->y_scale);
+
+ return self;
+}
+
+/**
+ * pango_hb_face_new_alias:
+ * @face: a `PangoHbFace`
+ * @fullname: the full name of the new face
+ *
+ * Creates a new `PangoHbFace that acts as an alias
+ * for the given @face.
+ *
+ * Note that @fullname should include the family name.
+ * For example, when using this to create a monospace
+ * italic font, @fullname should be "Monospace Italic".
+ *
+ * Returns: (transfer full): a newly created `PangoHbFace`
+ */
+PangoHbFace *
+pango_hb_face_new_alias (PangoHbFace *face,
+ const char *fullname)
+{
+ PangoHbFace *self;
+
+ g_return_val_if_fail (PANGO_IS_HB_FACE (face), NULL);
+ g_return_val_if_fail (fullname && *fullname, NULL);
+
+ self = g_object_new (PANGO_TYPE_HB_FACE, NULL);
+
+ self->blob = hb_blob_reference (face->blob);
+ self->index = face->index;
+ self->face = hb_face_reference (face->face);
+
+ self->description = pango_font_description_from_string (fullname);
+
+ self->family = pango_font_description_get_family (self->description);
+
+ if (g_str_has_prefix (fullname, self->family) &&
+ fullname[strlen (self->family)] != '\0')
+ self->name = g_strstrip (g_strdup (fullname + strlen (self->family)));
+ else
+ self->name = g_strdup ("Regular");
+
+ self->matrix = (PangoMatrix) PANGO_MATRIX_INIT;
+ self->x_scale = self->y_scale = 1.;
+
+ return self;
+}
+
+/* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/pango/pango-hbface.h b/pango/pango-hbface.h
new file mode 100644
index 00000000..dc06ca30
--- /dev/null
+++ b/pango/pango-hbface.h
@@ -0,0 +1,50 @@
+/* Pango
+ *
+ * Copyright (C) 2021 Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#pragma once
+
+#include <pango/pango-font.h>
+
+#include <hb.h>
+
+G_BEGIN_DECLS
+
+#define PANGO_TYPE_HB_FACE (pango_hb_face_get_type ())
+PANGO_AVAILABLE_IN_1_50
+G_DECLARE_FINAL_TYPE (PangoHbFace, pango_hb_face, PANGO, HB_FACE, PangoFontFace)
+
+PANGO_AVAILABLE_IN_1_50
+PangoHbFace * pango_hb_face_new_from_file (const char *file,
+ unsigned int index);
+
+PANGO_AVAILABLE_IN_1_50
+PangoHbFace * pango_hb_face_new_from_bytes (GBytes *bytes,
+ unsigned int index);
+
+PANGO_AVAILABLE_IN_1_50
+PangoHbFace * pango_hb_face_new_transformed (PangoHbFace *face,
+ const char *name,
+ const PangoMatrix *transform);
+
+PANGO_AVAILABLE_IN_1_50
+PangoHbFace * pango_hb_face_new_alias (PangoHbFace *face,
+ const char *fullname);
+
+G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]