Skip to content

Commit

Permalink
Hide windows when they have zero height or width
Browse files Browse the repository at this point in the history
New in the 0.7.5 Glk spec is a note about zero-size windows. They should
disappear completely, including any borders that they have. This change
makes the size allocation code ignore zero-size windows, and in the case
of a pair window that is going to allocate zero size to one of its
children, it should instead give the entire space to its other child,
eliminating the border.

This is an interesting problem to test, but it can be done by testing
against a reference image. This mechanism might be useful for writing more
tests in the future, so we add a reftest runner (and image diffing code
copied from GTK's reftest suite.)
  • Loading branch information
ptomato committed Nov 5, 2023
1 parent cd4cc98 commit 5da5c83
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 1 deletion.
18 changes: 17 additions & 1 deletion libchimara/chimara-glk.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,11 @@ chimara_glk_get_preferred_height(GtkWidget *widget, int *minimal, int *natural)
static winid_t
allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
{
if (allocation->width == 0 || allocation->height == 0) {
/* Just don't show this window */
return win;
}

if(win->type == wintype_Pair)
{
g_mutex_lock(&win->lock);
Expand Down Expand Up @@ -693,7 +698,18 @@ allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
child1.width = child2.width = allocation->width;
break;
}


/* If either of the child windows get 0 size, hide that window and just
* give the full space to the other one */
if (child1.width == 0 || child1.height == 0) {
allocate_recurse(win->window_node->children->next->data, allocation, spacing);
return win;
}
if (child2.width == 0 || child2.height == 0) {
allocate_recurse(win->window_node->children->data, allocation, spacing);
return win;
}

/* Recurse */
winid_t arrange1 = allocate_recurse(win->window_node->children->data, &child1, spacing);
winid_t arrange2 = allocate_recurse(win->window_node->children->next->data, &child2, spacing);
Expand Down
21 changes: 21 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ glulxercise_runner = executable('glulxercise-runner',
'glulxercise-runner.c', 'keycode.c',
include_directories: top_include, link_with: libchimara,
dependencies: [gmodule, gtk])
reftest_runner = executable('reftest-runner',
'reftest-runner.c', 'reftest-compare.c',
include_directories: top_include, link_with: libchimara,
dependencies: [gmodule, gtk])

shared_module('first', 'first.c', name_prefix: '',
include_directories: top_include,
Expand Down Expand Up @@ -80,6 +84,23 @@ foreach t : unit_tests
protocol: 'tap', env: test_env)
endforeach

reftests = [
'zero-height-window',
'zero-width-window',
]

reftest_deps = []
foreach t : reftests
plugin = shared_module(t, 'reftest/@[email protected]'.format(t), name_prefix: '',
include_directories: [top_include, '../libchimara'],
link_args: plugin_link_args, link_depends: plugin_link_depends)
reftest_deps += plugin
endforeach

reftest_dir = join_paths(meson.current_source_dir(), 'reftest')
test('reftests', reftest_runner, args: [reftest_dir, meson.current_build_dir()],
depends: reftest_deps, suite: 'reftest', protocol: 'tap', env: test_env)

glulxercise_tests = [
'abbrevtest',
'abbrevtest-e',
Expand Down
148 changes: 148 additions & 0 deletions tests/reftest-compare.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright (C) 2011 Red Hat Inc.
*
* Author:
* Benjamin Otte <[email protected]>
*
* 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, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <stdint.h>

#include <cairo.h>
#include <gdk/gdk.h>

static void
get_surface_size(cairo_surface_t *surface, int *width, int *height)
{
cairo_t *cr = cairo_create(surface);

GdkRectangle area;
if (!gdk_cairo_get_clip_rectangle(cr, &area))
g_assert_not_reached ();

g_assert(area.x == 0 && area.y == 0);
g_assert(area.width > 0 && area.height > 0);

cairo_destroy(cr);

*width = area.width;
*height = area.height;
}

static cairo_surface_t *
coerce_surface_for_comparison(cairo_surface_t *surface, int width, int height)
{
cairo_surface_t *coerced = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
cairo_t *cr = cairo_create(coerced);

cairo_set_source_surface(cr, surface, 0, 0);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr);

cairo_destroy(cr);

g_assert(cairo_surface_status(coerced) == CAIRO_STATUS_SUCCESS);

return coerced;
}

/* Compares two CAIRO_FORMAT_ARGB32 buffers, returning NULL if the buffers are
* equal or a surface containing a diff between the two surfaces.
*
* This function should be rewritten to compare all formats supported by
* cairo_format_t instead of taking a mask as a parameter.
*
* This function is originally from cairo:test/buffer-diff.c.
* Copyright © 2004 Richard D. Worth
*/
static cairo_surface_t *
buffer_diff_core(const uint8_t *buf_a, int stride_a, const uint8_t *buf_b, int stride_b, int width, int height)
{
uint8_t *buf_diff = NULL;
int stride_diff = 0;
cairo_surface_t *diff = NULL;

for (int y = 0; y < height; y++) {
const uint32_t *row_a = (const uint32_t *) (buf_a + y * stride_a);
const uint32_t *row_b = (const uint32_t *) (buf_b + y * stride_b);
uint32_t *row = (uint32_t *) (buf_diff + y * stride_diff);

for (int x = 0; x < width; x++) {
/* check if the pixels are the same */
if (row_a[x] == row_b[x])
continue;

if (diff == NULL) {
diff = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
g_assert(cairo_surface_status(diff) == CAIRO_STATUS_SUCCESS);
buf_diff = cairo_image_surface_get_data(diff);
stride_diff = cairo_image_surface_get_stride(diff);
row = (uint32_t *) (buf_diff + y * stride_diff);
}

/* calculate a difference value for all 4 channels */
uint32_t diff_pixel = 0;
for (int channel = 0; channel < 4; channel++) {
int value_a = (row_a[x] >> (channel * 8)) & 0xff;
int value_b = (row_b[x] >> (channel * 8)) & 0xff;

unsigned diff = ABS(value_a - value_b);
diff *= 4; /* emphasize */
if (diff)
diff += 128; /* make sure it's visible */
if (diff > 255)
diff = 255;
diff_pixel |= diff << (channel * 8);
}

if ((diff_pixel & 0x00ffffff) == 0) {
/* alpha only difference, convert to luminance */
uint8_t alpha = diff_pixel >> 24;
diff_pixel = alpha * 0x010101;
}

row[x] = diff_pixel;
}
}

return diff;
}

cairo_surface_t *
reftest_compare_surfaces(cairo_surface_t *surface1, cairo_surface_t *surface2)
{
int w1, h1, w2, h2;
get_surface_size(surface1, &w1, &h1);
get_surface_size(surface2, &w2, &h2);
int w = MAX(w1, w2);
int h = MAX(h1, h2);

cairo_surface_t *test_s1 = coerce_surface_for_comparison(surface1, w, h);
cairo_surface_t *test_s2 = coerce_surface_for_comparison(surface2, w, h);

cairo_surface_t *diff = buffer_diff_core(cairo_image_surface_get_data(test_s1),
cairo_image_surface_get_stride(test_s1),
cairo_image_surface_get_data(test_s2),
cairo_image_surface_get_stride(test_s2),
w, h);

cairo_surface_destroy(test_s1);
cairo_surface_destroy(test_s2);

return diff;
}

Loading

0 comments on commit 5da5c83

Please sign in to comment.