2017-11-11 05:41:41 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2017-12-28 09:56:05 +00:00
|
|
|
static char *write_to_filename = NULL;
|
|
|
|
static gboolean compare_node;
|
|
|
|
|
|
|
|
static GOptionEntry options[] = {
|
|
|
|
{ "write", 'o', 0, G_OPTION_ARG_STRING, &write_to_filename, "Write PNG file", NULL },
|
|
|
|
{ "compare", 'c', 0, G_OPTION_ARG_NONE, &compare_node, "Compare render to render_texture", NULL },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-11-11 05:41:41 +00:00
|
|
|
|
|
|
|
typedef struct _GtkNodeView GtkNodeView;
|
|
|
|
typedef struct _GtkNodeViewClass GtkNodeViewClass;
|
|
|
|
|
|
|
|
#define GTK_TYPE_NODE_VIEW (gtk_node_view_get_type ())
|
|
|
|
#define GTK_NODE_VIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST(obj, GTK_TYPE_NODE_VIEW, GtkNodeView))
|
|
|
|
#define GTK_NODE_VIEW_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST(cls, GTK_TYPE_NODE_VIEW, GtkNodeViewClass))
|
|
|
|
struct _GtkNodeView
|
|
|
|
{
|
|
|
|
GtkWidget parent_instance;
|
|
|
|
|
|
|
|
GskRenderNode *node;
|
2021-01-29 08:05:03 +00:00
|
|
|
GFileMonitor *file_monitor;
|
2017-11-11 05:41:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkNodeViewClass
|
|
|
|
{
|
|
|
|
GtkWidgetClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
GType gtk_node_view_get_type (void) G_GNUC_CONST;
|
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(GtkNodeView, gtk_node_view, GTK_TYPE_WIDGET)
|
|
|
|
|
2021-01-29 08:05:03 +00:00
|
|
|
static void
|
|
|
|
deserialize_error_func (const GskParseLocation *start,
|
|
|
|
const GskParseLocation *end,
|
|
|
|
const GError *error,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GString *string = g_string_new ("<data>");
|
|
|
|
|
|
|
|
g_string_append_printf (string, ":%zu:%zu",
|
|
|
|
start->lines + 1, start->line_chars + 1);
|
|
|
|
if (start->lines != end->lines || start->line_chars != end->line_chars)
|
|
|
|
{
|
|
|
|
g_string_append (string, "-");
|
|
|
|
if (start->lines != end->lines)
|
|
|
|
g_string_append_printf (string, "%zu:", end->lines + 1);
|
|
|
|
g_string_append_printf (string, "%zu", end->line_chars + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_warning ("Error at %s: %s", string->str, error->message);
|
|
|
|
|
|
|
|
g_string_free (string, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_file_contents (GtkNodeView *self,
|
|
|
|
GFile *file)
|
|
|
|
{
|
|
|
|
GBytes *bytes;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
bytes = g_file_load_bytes (file, NULL, NULL, NULL);
|
|
|
|
if (bytes == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!g_utf8_validate (g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), NULL))
|
|
|
|
{
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->node = gsk_render_node_deserialize (bytes, deserialize_error_func, &error);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_critical ("Invalid node file: %s", error->message);
|
|
|
|
g_clear_error (&error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_queue_draw (GTK_WIDGET (self));
|
|
|
|
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
file_changed_cb (GFileMonitor *monitor,
|
|
|
|
GFile *file,
|
|
|
|
GFile *other_file,
|
|
|
|
GFileMonitorEvent event_type,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GtkNodeView *self = user_data;
|
|
|
|
|
|
|
|
if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
|
|
|
|
load_file_contents (self, file);
|
|
|
|
}
|
|
|
|
|
2017-12-28 09:56:05 +00:00
|
|
|
static void
|
|
|
|
gtk_node_view_measure (GtkWidget *widget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline)
|
|
|
|
{
|
|
|
|
GtkNodeView *self = GTK_NODE_VIEW (widget);
|
|
|
|
graphene_rect_t bounds;
|
|
|
|
|
|
|
|
|
|
|
|
if (self->node == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gsk_render_node_get_bounds (self->node, &bounds);
|
|
|
|
|
|
|
|
if (orientation == GTK_ORIENTATION_HORIZONTAL)
|
|
|
|
{
|
|
|
|
*minimum = *natural = bounds.origin.x + bounds.size.width;
|
|
|
|
}
|
|
|
|
else /* VERTICAL */
|
|
|
|
{
|
|
|
|
*minimum = *natural = bounds.origin.y + bounds.size.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 05:41:41 +00:00
|
|
|
static void
|
|
|
|
gtk_node_view_snapshot (GtkWidget *widget,
|
|
|
|
GtkSnapshot *snapshot)
|
|
|
|
{
|
|
|
|
GtkNodeView *self = GTK_NODE_VIEW (widget);
|
|
|
|
|
|
|
|
if (self->node != NULL)
|
2020-05-16 07:24:29 +00:00
|
|
|
gtk_snapshot_append_node (snapshot, self->node);
|
2017-11-11 05:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_node_view_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkNodeView *self = GTK_NODE_VIEW (object);
|
|
|
|
|
|
|
|
if (self->node)
|
|
|
|
gsk_render_node_unref (self->node);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_node_view_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_node_view_init (GtkNodeView *self)
|
|
|
|
{
|
2020-05-16 07:24:29 +00:00
|
|
|
gtk_widget_set_overflow (GTK_WIDGET (self), GTK_OVERFLOW_HIDDEN);
|
2017-11-11 05:41:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_node_view_class_init (GtkNodeViewClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gtk_node_view_finalize;
|
|
|
|
|
2017-12-28 09:56:05 +00:00
|
|
|
widget_class->measure = gtk_node_view_measure;
|
2017-11-11 05:41:41 +00:00
|
|
|
widget_class->snapshot = gtk_node_view_snapshot;
|
|
|
|
}
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
static void
|
|
|
|
quit_cb (GtkWidget *widget,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gboolean *done = data;
|
|
|
|
|
|
|
|
*done = TRUE;
|
|
|
|
|
|
|
|
g_main_context_wakeup (NULL);
|
|
|
|
}
|
|
|
|
|
2017-11-11 05:41:41 +00:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *nodeview;
|
|
|
|
graphene_rect_t node_bounds;
|
2017-12-28 09:56:05 +00:00
|
|
|
GOptionContext *option_context;
|
|
|
|
GError *error = NULL;
|
2020-02-10 03:24:47 +00:00
|
|
|
gboolean done = FALSE;
|
2021-01-29 08:05:03 +00:00
|
|
|
GFile *file;
|
2017-11-11 05:41:41 +00:00
|
|
|
|
2017-12-28 09:56:05 +00:00
|
|
|
option_context = g_option_context_new ("NODE-FILE [-o OUTPUT] [--compare]");
|
|
|
|
g_option_context_add_main_entries (option_context, options, NULL);
|
|
|
|
|
|
|
|
if (argc < 2)
|
2017-11-11 05:41:41 +00:00
|
|
|
{
|
2017-12-28 09:56:05 +00:00
|
|
|
printf ("Usage: showrendernode NODEFILE [-o OUTPUT] [--compare]\n");
|
2017-11-11 05:41:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-28 09:56:05 +00:00
|
|
|
if (!g_option_context_parse (option_context, &argc, &argv, &error))
|
|
|
|
{
|
|
|
|
g_printerr ("Option parsing failed: %s\n", error->message);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-10-10 17:02:43 +00:00
|
|
|
g_option_context_free (option_context);
|
|
|
|
option_context = NULL;
|
|
|
|
|
2017-12-28 09:56:05 +00:00
|
|
|
g_message ("Compare: %d, write to filename: %s", compare_node, write_to_filename);
|
|
|
|
|
2017-11-11 05:41:41 +00:00
|
|
|
gtk_init ();
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2017-11-11 05:41:41 +00:00
|
|
|
nodeview = g_object_new (GTK_TYPE_NODE_VIEW, NULL);
|
|
|
|
|
|
|
|
gtk_window_set_decorated (GTK_WINDOW (window), FALSE);
|
|
|
|
|
2021-01-29 08:05:03 +00:00
|
|
|
file = g_file_new_for_path (argv[1]);
|
|
|
|
load_file_contents (GTK_NODE_VIEW (nodeview), file);
|
|
|
|
GTK_NODE_VIEW (nodeview)->file_monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
|
|
|
|
g_object_unref (file);
|
|
|
|
|
2017-11-11 05:41:41 +00:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
g_warning ("%s", error->message);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-01-29 08:05:03 +00:00
|
|
|
g_signal_connect (GTK_NODE_VIEW (nodeview)->file_monitor,
|
|
|
|
"changed", G_CALLBACK (file_changed_cb), nodeview);
|
2017-11-11 05:41:41 +00:00
|
|
|
|
2017-12-28 09:56:05 +00:00
|
|
|
if (write_to_filename != NULL)
|
|
|
|
{
|
2020-07-30 21:06:59 +00:00
|
|
|
GdkSurface *surface = gdk_surface_new_toplevel (gdk_display_get_default());
|
2020-03-06 17:05:43 +00:00
|
|
|
GskRenderer *renderer = gsk_renderer_new_for_surface (surface);
|
2017-12-28 09:56:05 +00:00
|
|
|
GdkTexture *texture = gsk_renderer_render_texture (renderer, GTK_NODE_VIEW (nodeview)->node, NULL);
|
|
|
|
|
|
|
|
g_message ("Writing .node file to .png using %s", G_OBJECT_TYPE_NAME (renderer));
|
|
|
|
|
|
|
|
g_assert (texture != NULL);
|
|
|
|
|
2018-04-12 11:59:55 +00:00
|
|
|
gdk_texture_save_to_png (texture, write_to_filename);
|
2017-12-28 09:56:05 +00:00
|
|
|
|
|
|
|
gsk_renderer_unrealize (renderer);
|
|
|
|
|
|
|
|
g_object_unref (texture);
|
|
|
|
g_object_unref (renderer);
|
2020-03-06 17:05:43 +00:00
|
|
|
g_object_unref (surface);
|
2017-12-28 09:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (compare_node)
|
|
|
|
{
|
|
|
|
GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
|
2020-07-30 21:06:59 +00:00
|
|
|
GdkSurface *gdk_surface = gdk_surface_new_toplevel (gdk_display_get_default());
|
2018-03-20 11:05:26 +00:00
|
|
|
GskRenderer *renderer = gsk_renderer_new_for_surface (gdk_surface);
|
2017-12-28 09:56:05 +00:00
|
|
|
GdkTexture *texture = gsk_renderer_render_texture (renderer, GTK_NODE_VIEW (nodeview)->node, NULL);
|
2018-03-16 03:29:42 +00:00
|
|
|
GtkWidget *image = gtk_image_new_from_paintable (GDK_PAINTABLE (texture));
|
2017-12-28 09:56:05 +00:00
|
|
|
|
2018-12-02 15:14:31 +00:00
|
|
|
gtk_widget_set_size_request (image,
|
|
|
|
gdk_texture_get_width (texture),
|
|
|
|
gdk_texture_get_height (texture));
|
|
|
|
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), nodeview);
|
|
|
|
gtk_box_append (GTK_BOX (box), image);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), box);
|
2017-12-28 09:56:05 +00:00
|
|
|
|
|
|
|
gsk_renderer_unrealize (renderer);
|
|
|
|
g_object_unref (texture);
|
|
|
|
g_object_unref (renderer);
|
2018-03-20 10:40:08 +00:00
|
|
|
g_object_unref (gdk_surface);
|
2017-12-28 09:56:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), nodeview);
|
2017-12-28 09:56:05 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 05:41:41 +00:00
|
|
|
gsk_render_node_get_bounds (GTK_NODE_VIEW (nodeview)->node, &node_bounds);
|
2020-12-02 09:08:46 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window),
|
|
|
|
MAX (600, node_bounds.size.width),
|
|
|
|
MAX (500, node_bounds.size.height));
|
2017-11-11 05:41:41 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
g_signal_connect (window, "destroy", G_CALLBACK (quit_cb), &done);
|
2022-11-29 12:06:33 +00:00
|
|
|
gtk_window_present (GTK_WINDOW (window));
|
2020-02-10 03:24:47 +00:00
|
|
|
|
|
|
|
while (!done)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2017-11-11 05:41:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|