mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-26 09:21:06 +01:00
[gpu] Port hb-gpu-demo to options.hh infrastructure
Replace the manual getopt-based demo main with the standard main_font_text_t / shape_consumer_t composition, getting all harfbuzz options for free: --variations, --features, --script, --direction, --face-index, --font-funcs, etc. New gpu_output_t (in util/gpu-output.hh) implements the output interface, feeding shaped glyphs to the demo's GL renderer. Add demo_buffer_add_glyph() and demo_buffer_current_line() to demo-buffer for glyph-at-a-time insertion (vs the old batch add_text which did its own shaping). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3ab11b19b6
commit
25e69d9b96
11 files changed
+386
-370
No files matched your search
+10
-1
@@ -31,7 +31,7 @@
|
||||
|
||||
struct face_options_t
|
||||
{
|
||||
~face_options_t ()
|
||||
virtual ~face_options_t ()
|
||||
{
|
||||
g_free (font_file);
|
||||
g_free (face_loader);
|
||||
@@ -48,6 +48,8 @@ struct face_options_t
|
||||
|
||||
void post_parse (GError **error);
|
||||
|
||||
virtual hb_face_t *default_face () { return nullptr; }
|
||||
|
||||
static struct cache_t
|
||||
{
|
||||
~cache_t ()
|
||||
@@ -78,6 +80,13 @@ face_options_t::post_parse (GError **error)
|
||||
{
|
||||
if (!font_file)
|
||||
{
|
||||
hb_face_t *default_face_ = default_face ();
|
||||
if (default_face_)
|
||||
{
|
||||
set_face (default_face_);
|
||||
hb_face_destroy (default_face_);
|
||||
return;
|
||||
}
|
||||
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
|
||||
"No font file set");
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* 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 GPU_OUTPUT_HH
|
||||
#define GPU_OUTPUT_HH
|
||||
|
||||
#include "gpu/demo-common.h"
|
||||
#include "gpu/demo-buffer.h"
|
||||
#include "gpu/demo-font.h"
|
||||
#include "gpu/demo-glstate.h"
|
||||
#include "gpu/demo-view.h"
|
||||
|
||||
#include "options.hh"
|
||||
#include "font-options.hh"
|
||||
|
||||
#define WINDOW_W 700
|
||||
#define WINDOW_H 700
|
||||
|
||||
struct gpu_output_t
|
||||
{
|
||||
static constexpr bool repeat_shape = false;
|
||||
|
||||
void add_options (option_parser_t *parser HB_UNUSED) {}
|
||||
|
||||
template <typename app_t>
|
||||
void init (hb_buffer_t *buffer_ HB_UNUSED, const app_t *app)
|
||||
{
|
||||
font = app->font;
|
||||
face = hb_font_get_face (font);
|
||||
|
||||
int x_scale, y_scale;
|
||||
hb_font_get_scale (font, &x_scale, &y_scale);
|
||||
font_size = (double) y_scale;
|
||||
|
||||
/* Setup GL */
|
||||
glfwSetErrorCallback ([] (int error, const char *desc) {
|
||||
fprintf (stderr, "GLFW error %d: %s\n", error, desc);
|
||||
});
|
||||
if (!glfwInit ())
|
||||
fail (false, "Failed to initialize GLFW");
|
||||
|
||||
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
glfwWindowHint (GLFW_SRGB_CAPABLE, GLFW_TRUE);
|
||||
|
||||
window = glfwCreateWindow (WINDOW_W, WINDOW_H, "HarfBuzz GPU Demo", NULL, NULL);
|
||||
if (!window) {
|
||||
glfwTerminate ();
|
||||
fail (false, "Failed to create GLFW window");
|
||||
}
|
||||
glfwMakeContextCurrent (window);
|
||||
|
||||
glewExperimental = GL_TRUE;
|
||||
glewInit ();
|
||||
while (glGetError () != GL_NO_ERROR)
|
||||
;
|
||||
if (!glewIsSupported ("GL_VERSION_3_3"))
|
||||
fail (false, "OpenGL 3.3 not supported");
|
||||
|
||||
{
|
||||
int fb_width, fb_height;
|
||||
glfwGetFramebufferSize (window, &fb_width, &fb_height);
|
||||
glViewport (0, 0, fb_width, fb_height);
|
||||
}
|
||||
|
||||
st = demo_glstate_create ();
|
||||
vu = demo_view_create (st, window);
|
||||
|
||||
glfwSetWindowUserPointer (window, this);
|
||||
glfwSetFramebufferSizeCallback (window, [] (GLFWwindow *w, int width, int height) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_reshape_func (self->vu, width, height);
|
||||
});
|
||||
glfwSetKeyCallback (window, [] (GLFWwindow *w, int key, int scancode, int action, int mods) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_key_func (self->vu, key, scancode, action, mods);
|
||||
});
|
||||
glfwSetCharCallback (window, [] (GLFWwindow *w, unsigned int cp) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_char_func (self->vu, cp);
|
||||
});
|
||||
glfwSetMouseButtonCallback (window, [] (GLFWwindow *w, int button, int action, int mods) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_mouse_func (self->vu, button, action, mods);
|
||||
});
|
||||
glfwSetScrollCallback (window, [] (GLFWwindow *w, double xoff, double yoff) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_scroll_func (self->vu, xoff, yoff);
|
||||
});
|
||||
glfwSetCursorPosCallback (window, [] (GLFWwindow *w, double x, double y) {
|
||||
auto *self = (gpu_output_t *) glfwGetWindowUserPointer (w);
|
||||
demo_view_motion_func (self->vu, x, y);
|
||||
});
|
||||
|
||||
demo_font_ = demo_font_create (font, demo_glstate_get_atlas (st));
|
||||
buf = demo_buffer_create ();
|
||||
|
||||
demo_point_t top_left = {0, 0};
|
||||
demo_buffer_move_to (buf, &top_left);
|
||||
demo_buffer_current_line (buf, font_size);
|
||||
}
|
||||
|
||||
void new_line ()
|
||||
{
|
||||
demo_buffer_current_line (buf, font_size);
|
||||
}
|
||||
|
||||
void consume_text (hb_buffer_t *buffer HB_UNUSED,
|
||||
const char *text HB_UNUSED,
|
||||
unsigned text_len HB_UNUSED,
|
||||
hb_bool_t utf8_clusters HB_UNUSED) {}
|
||||
|
||||
void consume_glyphs (hb_buffer_t *buffer,
|
||||
const char *text HB_UNUSED,
|
||||
unsigned text_len HB_UNUSED,
|
||||
hb_bool_t utf8_clusters HB_UNUSED)
|
||||
{
|
||||
unsigned count = hb_buffer_get_length (buffer);
|
||||
hb_glyph_info_t *infos = hb_buffer_get_glyph_infos (buffer, nullptr);
|
||||
hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, nullptr);
|
||||
|
||||
for (unsigned i = 0; i < count; i++)
|
||||
demo_buffer_add_glyph (buf, demo_font_,
|
||||
font_size,
|
||||
infos[i].codepoint,
|
||||
pos[i].x_offset,
|
||||
pos[i].y_offset,
|
||||
pos[i].x_advance);
|
||||
}
|
||||
|
||||
void error (const char *message)
|
||||
{
|
||||
fprintf (stderr, "error: %s\n", message);
|
||||
}
|
||||
|
||||
template <typename app_t>
|
||||
void finish (hb_buffer_t *buffer_ HB_UNUSED, const app_t *app HB_UNUSED)
|
||||
{
|
||||
demo_font_print_stats (demo_font_);
|
||||
demo_view_print_help (vu);
|
||||
demo_view_setup (vu);
|
||||
|
||||
demo_view_display (vu, buf);
|
||||
glfwPollEvents ();
|
||||
demo_view_display (vu, buf);
|
||||
|
||||
while (!glfwWindowShouldClose (window))
|
||||
{
|
||||
glfwPollEvents ();
|
||||
if (demo_view_should_redraw (vu))
|
||||
demo_view_display (vu, buf);
|
||||
else
|
||||
glfwWaitEvents ();
|
||||
}
|
||||
|
||||
demo_buffer_destroy (buf);
|
||||
demo_font_destroy (demo_font_);
|
||||
demo_view_destroy (vu);
|
||||
demo_glstate_destroy (st);
|
||||
|
||||
glfwDestroyWindow (window);
|
||||
glfwTerminate ();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
double font_size = 1;
|
||||
hb_font_t *font = nullptr;
|
||||
hb_face_t *face = nullptr;
|
||||
|
||||
GLFWwindow *window = nullptr;
|
||||
demo_glstate_t *st = nullptr;
|
||||
demo_view_t *vu = nullptr;
|
||||
demo_font_t *demo_font_ = nullptr;
|
||||
demo_buffer_t *buf = nullptr;
|
||||
};
|
||||
|
||||
#endif /* GPU_OUTPUT_HH */
|
||||
@@ -132,6 +132,47 @@ demo_buffer_add_text (demo_buffer_t *buffer,
|
||||
buffer->dirty = true;
|
||||
}
|
||||
|
||||
void
|
||||
demo_buffer_current_line (demo_buffer_t *buffer,
|
||||
double font_size)
|
||||
{
|
||||
buffer->cursor.x = 0;
|
||||
buffer->cursor.y += font_size;
|
||||
}
|
||||
|
||||
void
|
||||
demo_buffer_add_glyph (demo_buffer_t *buffer,
|
||||
demo_font_t *font,
|
||||
double font_size,
|
||||
unsigned int glyph_index,
|
||||
double x_offset,
|
||||
double y_offset,
|
||||
double x_advance)
|
||||
{
|
||||
glyph_info_t gi;
|
||||
demo_font_lookup_glyph (font, glyph_index, &gi);
|
||||
|
||||
double scale = font_size / gi.upem;
|
||||
|
||||
demo_extents_t ink_extents;
|
||||
demo_point_t position = buffer->cursor;
|
||||
position.x += scale * x_offset;
|
||||
position.y -= scale * y_offset;
|
||||
demo_shader_add_glyph_vertices (position, font_size, &gi, buffer->vertices, &ink_extents);
|
||||
demo_extents_extend (&buffer->ink_extents, &ink_extents);
|
||||
|
||||
demo_point_t corner;
|
||||
corner.x = buffer->cursor.x;
|
||||
corner.y = buffer->cursor.y - font_size;
|
||||
demo_extents_add (&buffer->logical_extents, &corner);
|
||||
corner.x = buffer->cursor.x + scale * gi.advance;
|
||||
corner.y = buffer->cursor.y;
|
||||
demo_extents_add (&buffer->logical_extents, &corner);
|
||||
|
||||
buffer->cursor.x += scale * x_advance;
|
||||
buffer->dirty = true;
|
||||
}
|
||||
|
||||
void
|
||||
demo_buffer_draw (demo_buffer_t *buffer)
|
||||
{
|
||||
|
||||
@@ -38,6 +38,19 @@ demo_buffer_add_text (demo_buffer_t *buffer,
|
||||
demo_font_t *font,
|
||||
double font_size);
|
||||
|
||||
void
|
||||
demo_buffer_add_glyph (demo_buffer_t *buffer,
|
||||
demo_font_t *font,
|
||||
double font_size,
|
||||
unsigned int glyph_index,
|
||||
double x_offset,
|
||||
double y_offset,
|
||||
double x_advance);
|
||||
|
||||
void
|
||||
demo_buffer_current_line (demo_buffer_t *buffer,
|
||||
double font_size);
|
||||
|
||||
void
|
||||
demo_buffer_draw (demo_buffer_t *buffer);
|
||||
|
||||
|
||||
@@ -20,13 +20,13 @@ struct demo_font_t {
|
||||
};
|
||||
|
||||
demo_font_t *
|
||||
demo_font_create (hb_face_t *face,
|
||||
demo_font_create (hb_font_t *hb_font,
|
||||
demo_atlas_t *atlas)
|
||||
{
|
||||
demo_font_t *font = (demo_font_t *) calloc (1, sizeof (demo_font_t));
|
||||
|
||||
font->face = hb_face_reference (face);
|
||||
font->font = hb_font_create (face);
|
||||
font->face = hb_face_reference (hb_font_get_face (hb_font));
|
||||
font->font = hb_font_reference (hb_font);
|
||||
font->glyph_cache = new glyph_cache_t ();
|
||||
font->atlas = demo_atlas_reference (atlas);
|
||||
font->g = hb_gpu_draw_create_or_fail ();
|
||||
@@ -86,7 +86,9 @@ _demo_font_upload_glyph (demo_font_t *font,
|
||||
glyph_info->extents.max_y = hb_ext.y_bearing;
|
||||
glyph_info->extents.min_y = hb_ext.y_bearing + hb_ext.height;
|
||||
glyph_info->advance = hb_font_get_glyph_h_advance (font->font, glyph_index);
|
||||
glyph_info->upem = hb_face_get_upem (font->face);
|
||||
int x_scale, y_scale;
|
||||
hb_font_get_scale (font->font, &x_scale, &y_scale);
|
||||
glyph_info->upem = y_scale;
|
||||
glyph_info->is_empty = (len == 0);
|
||||
|
||||
if (!glyph_info->is_empty)
|
||||
|
||||
@@ -20,7 +20,7 @@ typedef struct {
|
||||
typedef struct demo_font_t demo_font_t;
|
||||
|
||||
demo_font_t *
|
||||
demo_font_create (hb_face_t *face,
|
||||
demo_font_create (hb_font_t *font,
|
||||
demo_atlas_t *atlas);
|
||||
|
||||
void
|
||||
|
||||
@@ -1,358 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 Google, Inc. All Rights Reserved.
|
||||
* Copyright 2026 Behdad Esfahbod. All Rights Reserved.
|
||||
*
|
||||
* Google Author(s): Behdad Esfahbod, Maysum Panju, Wojciech Baranowski
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <libgen.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#include "demo-buffer.h"
|
||||
#include "demo-font.h"
|
||||
#include "demo-view.h"
|
||||
|
||||
static demo_glstate_t *st;
|
||||
static demo_view_t *vu;
|
||||
static demo_buffer_t *buffer;
|
||||
|
||||
#define WINDOW_W 700
|
||||
#define WINDOW_H 700
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
static int opterr = 1;
|
||||
static int optind = 1;
|
||||
static int optopt;
|
||||
static char *optarg;
|
||||
|
||||
static int getopt(int argc, char *argv[], char *opts)
|
||||
{
|
||||
static int sp = 1;
|
||||
int c;
|
||||
char *cp;
|
||||
|
||||
if (sp == 1) {
|
||||
if (optind >= argc ||
|
||||
argv[optind][0] != '-' || argv[optind][1] == '\0')
|
||||
return EOF;
|
||||
else if (!strcmp(argv[optind], "--")) {
|
||||
optind++;
|
||||
return EOF;
|
||||
}
|
||||
}
|
||||
optopt = c = argv[optind][sp];
|
||||
if (c == ':' || !(cp = strchr(opts, c))) {
|
||||
fprintf(stderr, ": illegal option -- %c\n", c);
|
||||
if (argv[optind][++sp] == '\0') {
|
||||
optind++;
|
||||
sp = 1;
|
||||
}
|
||||
return '?';
|
||||
}
|
||||
if (*++cp == ':') {
|
||||
if (argv[optind][sp+1] != '\0')
|
||||
optarg = &argv[optind++][sp+1];
|
||||
else if(++optind >= argc) {
|
||||
fprintf(stderr, ": option requires an argument -- %c\n", c);
|
||||
sp = 1;
|
||||
return '?';
|
||||
} else
|
||||
optarg = argv[optind++];
|
||||
sp = 1;
|
||||
} else {
|
||||
if (argv[optind][++sp] == '\0') {
|
||||
sp = 1;
|
||||
optind++;
|
||||
}
|
||||
optarg = NULL;
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static void
|
||||
framebuffer_size_func (GLFWwindow *window, int width, int height)
|
||||
{
|
||||
demo_view_reshape_func (vu, width, height);
|
||||
}
|
||||
|
||||
static void
|
||||
key_func (GLFWwindow *window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
demo_view_key_func (vu, key, scancode, action, mods);
|
||||
}
|
||||
|
||||
static void
|
||||
char_func (GLFWwindow *window, unsigned int codepoint)
|
||||
{
|
||||
demo_view_char_func (vu, codepoint);
|
||||
}
|
||||
|
||||
static void
|
||||
mouse_func (GLFWwindow *window, int button, int action, int mods)
|
||||
{
|
||||
demo_view_mouse_func (vu, button, action, mods);
|
||||
}
|
||||
|
||||
static void
|
||||
scroll_func (GLFWwindow *window, double xoffset, double yoffset)
|
||||
{
|
||||
demo_view_scroll_func (vu, xoffset, yoffset);
|
||||
}
|
||||
|
||||
static void
|
||||
cursor_func (GLFWwindow *window, double x, double y)
|
||||
{
|
||||
demo_view_motion_func (vu, x, y);
|
||||
}
|
||||
|
||||
static void
|
||||
show_usage(const char *name)
|
||||
{
|
||||
printf("Usage:\n"
|
||||
" %s [fontfile [text]]\n"
|
||||
"or:\n"
|
||||
" %s [-h] [-f fontfile] [-t text]\n"
|
||||
"\n"
|
||||
" -h show this help message and exit;\n"
|
||||
" -t text the text string to be rendered;\n"
|
||||
" -f fontfile the font file\n"
|
||||
"\n", name, name);
|
||||
|
||||
demo_view_print_help (NULL);
|
||||
}
|
||||
|
||||
static bool
|
||||
contains_case_insensitive (const char *haystack, const char *needle)
|
||||
{
|
||||
if (!haystack || !needle)
|
||||
return false;
|
||||
|
||||
size_t needle_len = strlen (needle);
|
||||
if (!needle_len)
|
||||
return true;
|
||||
|
||||
for (const char *h = haystack; *h; h++) {
|
||||
size_t i = 0;
|
||||
while (i < needle_len &&
|
||||
h[i] &&
|
||||
tolower ((unsigned char) h[i]) == tolower ((unsigned char) needle[i]))
|
||||
i++;
|
||||
if (i == needle_len)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
running_on_gnome_desktop (void)
|
||||
{
|
||||
const char *desktops[] = {
|
||||
getenv ("XDG_CURRENT_DESKTOP"),
|
||||
getenv ("XDG_SESSION_DESKTOP"),
|
||||
getenv ("DESKTOP_SESSION"),
|
||||
};
|
||||
|
||||
for (unsigned int i = 0; i < ARRAY_LEN (desktops); i++) {
|
||||
if (contains_case_insensitive (desktops[i], "gnome"))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool
|
||||
using_mesa_gl (void)
|
||||
{
|
||||
const char *strings[] = {
|
||||
(const char *) glGetString (GL_VENDOR),
|
||||
(const char *) glGetString (GL_RENDERER),
|
||||
(const char *) glGetString (GL_VERSION),
|
||||
};
|
||||
|
||||
for (unsigned int i = 0; i < ARRAY_LEN (strings); i++) {
|
||||
if (contains_case_insensitive (strings[i], "mesa"))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void
|
||||
warn_about_vsync_override (void)
|
||||
{
|
||||
const char *vblank_mode = getenv ("vblank_mode");
|
||||
if (vblank_mode) {
|
||||
LOGW ("Using vblank_mode=%s from the environment.\n", vblank_mode);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!running_on_gnome_desktop () || !using_mesa_gl ())
|
||||
return;
|
||||
|
||||
LOGW ("GNOME/Mesa detected with vblank_mode unset.\n");
|
||||
LOGW ("If toggling vsync with `v` has no effect, restart with `vblank_mode=0`.\n");
|
||||
}
|
||||
|
||||
static void
|
||||
glfw_error_callback (int error, const char *description)
|
||||
{
|
||||
LOGW ("GLFW error %d: %s\n", error, description);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char** argv)
|
||||
{
|
||||
# include "default-text.h"
|
||||
const char *text = NULL;
|
||||
const char *font_path = NULL;
|
||||
char arg;
|
||||
while ((arg = getopt(argc, argv, (char *)"t:f:h")) != -1) {
|
||||
switch (arg) {
|
||||
case 't':
|
||||
text = optarg;
|
||||
break;
|
||||
case 'f':
|
||||
font_path = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
show_usage(argv[0]);
|
||||
return 0;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (!font_path)
|
||||
{
|
||||
if (optind < argc)
|
||||
font_path = argv[optind++];
|
||||
}
|
||||
if (!text)
|
||||
{
|
||||
if (optind < argc)
|
||||
text = argv[optind++];
|
||||
else
|
||||
text = default_text;
|
||||
}
|
||||
if (!text)
|
||||
{
|
||||
show_usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Setup GLFW */
|
||||
glfwSetErrorCallback (glfw_error_callback);
|
||||
if (!glfwInit ())
|
||||
die ("Failed to initialize GLFW");
|
||||
|
||||
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||
#endif
|
||||
glfwWindowHint (GLFW_SRGB_CAPABLE, GLFW_TRUE);
|
||||
|
||||
GLFWwindow *window = glfwCreateWindow (WINDOW_W, WINDOW_H, "HarfBuzz GPU Demo", NULL, NULL);
|
||||
if (!window) {
|
||||
glfwTerminate ();
|
||||
die ("Failed to create GLFW window");
|
||||
}
|
||||
glfwMakeContextCurrent (window);
|
||||
|
||||
glfwSetFramebufferSizeCallback (window, framebuffer_size_func);
|
||||
glfwSetKeyCallback (window, key_func);
|
||||
glfwSetCharCallback (window, char_func);
|
||||
glfwSetMouseButtonCallback (window, mouse_func);
|
||||
glfwSetScrollCallback (window, scroll_func);
|
||||
glfwSetCursorPosCallback (window, cursor_func);
|
||||
|
||||
/* Setup GLEW */
|
||||
glewExperimental = GL_TRUE;
|
||||
glewInit ();
|
||||
while (glGetError () != GL_NO_ERROR)
|
||||
;
|
||||
if (!glewIsSupported ("GL_VERSION_3_3"))
|
||||
die ("OpenGL 3.3 not supported");
|
||||
|
||||
{
|
||||
int fb_width, fb_height;
|
||||
glfwGetFramebufferSize (window, &fb_width, &fb_height);
|
||||
glViewport (0, 0, fb_width, fb_height);
|
||||
}
|
||||
|
||||
st = demo_glstate_create ();
|
||||
vu = demo_view_create (st, window);
|
||||
demo_view_print_help (vu);
|
||||
warn_about_vsync_override ();
|
||||
|
||||
hb_blob_t *blob = NULL;
|
||||
if (font_path)
|
||||
{
|
||||
blob = hb_blob_create_from_file_or_fail (font_path);
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef _WIN32
|
||||
blob = hb_blob_create_from_file_or_fail ("C:\\Windows\\Fonts\\calibri.ttf");
|
||||
#else
|
||||
#include "default-font.h"
|
||||
blob = hb_blob_create ((const char *) default_font, sizeof (default_font),
|
||||
HB_MEMORY_MODE_READONLY, NULL, NULL);
|
||||
#endif
|
||||
}
|
||||
if (!blob)
|
||||
die ("Failed to open font file");
|
||||
|
||||
hb_face_t *hb_face = hb_face_create (blob, 0);
|
||||
demo_font_t *font = demo_font_create (hb_face, demo_glstate_get_atlas (st));
|
||||
|
||||
buffer = demo_buffer_create ();
|
||||
demo_point_t top_left = {0, 0};
|
||||
demo_buffer_move_to (buffer, &top_left);
|
||||
demo_buffer_add_text (buffer, text, font, 1);
|
||||
|
||||
demo_font_print_stats (font);
|
||||
|
||||
demo_view_setup (vu);
|
||||
|
||||
demo_view_display (vu, buffer);
|
||||
glfwPollEvents ();
|
||||
demo_view_display (vu, buffer);
|
||||
|
||||
/* Main loop */
|
||||
while (!glfwWindowShouldClose (window))
|
||||
{
|
||||
glfwPollEvents ();
|
||||
|
||||
if (demo_view_should_redraw (vu))
|
||||
demo_view_display (vu, buffer);
|
||||
else
|
||||
glfwWaitEvents ();
|
||||
}
|
||||
|
||||
demo_buffer_destroy (buffer);
|
||||
demo_font_destroy (font);
|
||||
|
||||
hb_face_destroy (hb_face);
|
||||
hb_blob_destroy (blob);
|
||||
|
||||
demo_view_destroy (vu);
|
||||
demo_glstate_destroy (st);
|
||||
|
||||
glfwDestroyWindow (window);
|
||||
glfwTerminate ();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -12,7 +12,6 @@ gpu_demo_sources = files(
|
||||
'demo-shader.cc',
|
||||
'demo-view.h',
|
||||
'demo-view.cc',
|
||||
'hb-gpu-demo.cc',
|
||||
'matrix4x4.h',
|
||||
'matrix4x4.c',
|
||||
'trackball.h',
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include "font-options.hh"
|
||||
#include "main-font-text.hh"
|
||||
#include "shape-consumer.hh"
|
||||
#include "text-options.hh"
|
||||
#include "gpu-output.hh"
|
||||
|
||||
const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM;
|
||||
const unsigned SUBPIXEL_BITS = 0;
|
||||
|
||||
#include "gpu/default-text.h"
|
||||
|
||||
struct gpu_text_options_t : shape_text_options_t
|
||||
{
|
||||
const char *default_text () override
|
||||
{
|
||||
return ::default_text;
|
||||
}
|
||||
};
|
||||
|
||||
struct gpu_font_options_t : font_options_t
|
||||
{
|
||||
hb_face_t *default_face () override
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return nullptr; /* Will fail; user must provide font on Windows. */
|
||||
#else
|
||||
#include "gpu/default-font.h"
|
||||
hb_blob_t *blob = hb_blob_create ((const char *) default_font,
|
||||
sizeof (default_font),
|
||||
HB_MEMORY_MODE_READONLY,
|
||||
nullptr, nullptr);
|
||||
hb_face_t *face_ = hb_face_create (blob, 0);
|
||||
hb_blob_destroy (blob);
|
||||
return face_;
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
using base_t = main_font_text_t<shape_consumer_t<gpu_output_t>,
|
||||
gpu_font_options_t,
|
||||
gpu_text_options_t>;
|
||||
|
||||
struct gpu_main_t : base_t
|
||||
{
|
||||
int operator () (int argc, char **argv)
|
||||
{
|
||||
add_options ();
|
||||
add_exit_code (RETURN_VALUE_OPERATION_FAILED, "Operation failed.");
|
||||
|
||||
parse (&argc, &argv);
|
||||
|
||||
this->init (this);
|
||||
|
||||
while (this->consume_line (*this))
|
||||
;
|
||||
|
||||
this->finish (this);
|
||||
|
||||
if (this->failed && return_value == RETURN_VALUE_SUCCESS)
|
||||
return_value = RETURN_VALUE_OPERATION_FAILED;
|
||||
|
||||
return return_value;
|
||||
}
|
||||
};
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
gpu_main_t driver;
|
||||
return driver (argc, argv);
|
||||
}
|
||||
+3
-3
@@ -217,12 +217,12 @@ endif
|
||||
if not get_option('gpu').disabled()
|
||||
subdir('gpu')
|
||||
|
||||
if have_gpu_demo_deps
|
||||
if have_gpu_demo_deps and conf.get('HAVE_GLIB', 0) == 1
|
||||
hb_gpu_util = executable('hb-gpu-demo',
|
||||
gpu_demo_sources + gpu_demo_generated,
|
||||
['hb-gpu-demo.cc'] + gpu_demo_sources + gpu_demo_generated,
|
||||
cpp_args: cpp_args,
|
||||
include_directories: [incconfig, incsrc],
|
||||
dependencies: [gl_dep, glew_dep, glfw_dep],
|
||||
dependencies: [util_deps, gl_dep, glew_dep, glfw_dep],
|
||||
link_with: [libharfbuzz, libharfbuzz_gpu],
|
||||
install: false,
|
||||
)
|
||||
|
||||
+10
-2
@@ -34,7 +34,7 @@ struct text_options_t
|
||||
text_options_t ()
|
||||
: gs (g_string_new (nullptr))
|
||||
{}
|
||||
~text_options_t ()
|
||||
virtual ~text_options_t ()
|
||||
{
|
||||
g_free (text);
|
||||
g_free (text_file);
|
||||
@@ -46,10 +46,18 @@ struct text_options_t
|
||||
|
||||
void add_options (option_parser_t *parser);
|
||||
|
||||
virtual const char *default_text () { return nullptr; }
|
||||
|
||||
void post_parse (GError **error G_GNUC_UNUSED)
|
||||
{
|
||||
if (!text && !text_file)
|
||||
text_file = g_strdup ("-");
|
||||
{
|
||||
const char *dt = default_text ();
|
||||
if (dt)
|
||||
text = g_strdup (dt);
|
||||
else
|
||||
text_file = g_strdup ("-");
|
||||
}
|
||||
|
||||
if (text && text_file)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user