diff --git a/tools/chafa/chafa.c b/tools/chafa/chafa.c index 6813f60..931e4e7 100644 --- a/tools/chafa/chafa.c +++ b/tools/chafa/chafa.c @@ -1238,11 +1238,11 @@ parse_grid_arg (G_GNUC_UNUSED const gchar *option_name, const gchar *value, G_GN } else if (width < 0) { - width = height; + width = -1; } else if (height < 0) { - height = width; + height = -1; } options.grid_width = width; @@ -3276,7 +3276,8 @@ run_grid (GList *filenames) tty_options_init (); - canvas_config = build_config (options.width, options.height, FALSE); + /* The prototype canvas' size isn't used for anything; set it to a legal value */ + canvas_config = build_config (1, 1, FALSE); grid_layout = grid_layout_new (); grid_layout_set_view_size (grid_layout, options.width, options.height); diff --git a/tools/chafa/grid-layout.c b/tools/chafa/grid-layout.c index 750de8c..40c4b95 100644 --- a/tools/chafa/grid-layout.c +++ b/tools/chafa/grid-layout.c @@ -41,17 +41,48 @@ update_geometry (GridLayout *grid) gint view_width, view_height; gint n_cols, n_rows; gint item_width, item_height; + gint cell_width_px, cell_height_px; + gint font_ratio; + + if (!grid->canvas_config) + return; + + chafa_canvas_config_get_cell_geometry (grid->canvas_config, &cell_width_px, &cell_height_px); + if (cell_width_px < 1 || cell_height_px < 1) + { + cell_width_px = 10; + cell_height_px = 20; + } view_width = MAX (grid->view_width, 1); view_height = MAX (grid->view_height, 1); - n_cols = MAX (grid->n_cols, 1); - n_rows = MAX (grid->n_rows, 1); + n_cols = grid->n_cols; + n_rows = grid->n_rows; - item_width = MAX (view_width / n_cols - 1, 1); - item_height = MAX (view_height / n_rows - 1, 1); + if (n_cols < 1 && n_rows < 1) + n_cols = n_rows = 1; - if (grid->canvas_config) - chafa_canvas_config_set_geometry (grid->canvas_config, item_width, item_height); + /* If one dimension is not provided, make square tiles */ + + if (n_cols < 1) + { + item_height = view_height / n_rows - 1; + item_width = (item_height * cell_height_px) / cell_width_px; + } + else if (n_rows < 1) + { + item_width = view_width / n_cols - 1; + item_height = (item_width * cell_width_px) / cell_height_px; + } + else + { + item_width = view_width / n_cols - 1; + item_height = view_height / n_rows - 1; + } + + item_width = MAX (item_width, 1); + item_height = MAX (item_height, 1); + chafa_canvas_config_set_geometry (grid->canvas_config, item_width, item_height); } static ChafaCanvas *