From 974a4a21e16a2da85a66aae9b80eeca15ca26dd6 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sat, 23 Apr 2016 23:32:21 +0100 Subject: split out the layout glyph sizing and splitting API This refactors the core "font" sizing API to be handled through gui function tables similar to every other core/frontend calling API. --- desktop/browser_history.c | 15 +++++--- desktop/font.h | 97 ----------------------------------------------- desktop/gui_factory.c | 40 +++++++++++++++++++ desktop/gui_layout.h | 91 ++++++++++++++++++++++++++++++++++++++++++++ desktop/gui_table.h | 51 ++++++++++++++++++------- desktop/plot_style.h | 2 +- desktop/print.c | 2 +- desktop/print.h | 9 +++-- desktop/textarea.c | 16 ++++---- desktop/treeview.c | 22 +++++------ 10 files changed, 205 insertions(+), 140 deletions(-) delete mode 100644 desktop/font.h create mode 100644 desktop/gui_layout.h (limited to 'desktop') diff --git a/desktop/browser_history.c b/desktop/browser_history.c index 1ee800019..f2b0fb3ee 100644 --- a/desktop/browser_history.c +++ b/desktop/browser_history.c @@ -37,11 +37,11 @@ #include "css/css.h" #include "image/bitmap.h" +#include "desktop/gui_layout.h" #include "desktop/gui_internal.h" #include "desktop/browser_history.h" #include "desktop/browser_private.h" #include "desktop/plotters.h" -#include "desktop/font.h" #define WIDTH 100 #define HEIGHT 86 @@ -273,6 +273,7 @@ browser_window_history__redraw_entry(struct history *history, .stroke_width = entry == history->current ? 3 : 1, }; plot_font_style_t fstyle = *plot_style_font; + nserror res; if (clip) { struct rect rect; @@ -292,17 +293,21 @@ browser_window_history__redraw_entry(struct history *history, WIDTH, HEIGHT, entry->bitmap, 0xffffff, 0); } + if (!plot->rectangle(entry->x - 1 + xoffset, entry->y - 1 + yoffset, entry->x + xoffset + WIDTH, entry->y + yoffset + HEIGHT, - &pstyle_history_rect)) + &pstyle_history_rect)) { return false; + } - if (!nsfont.font_position_in_string(plot_style_font, entry->page.title, - strlen(entry->page.title), WIDTH, - &char_offset, &actual_x)) + res = guit->layout->position(plot_style_font, entry->page.title, + strlen(entry->page.title), WIDTH, + &char_offset, &actual_x); + if (res != NSERROR_OK) { return false; + } fstyle.background = HISTORY_COLOUR_BACKGROUND; fstyle.foreground = c; diff --git a/desktop/font.h b/desktop/font.h deleted file mode 100644 index c883f8a28..000000000 --- a/desktop/font.h +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2003 Phil Mellor - * Copyright 2005 James Bursa - * Copyright 2004 John Tytgat - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -/** \file - * Font handling interface. - * - * These functions provide font related services. They all work on - * UTF-8 strings with lengths given. - * - * Note that an interface to painting is not defined here. Painting is - * redirected through platform-dependent plotters anyway, so there is - * no gain in abstracting it here. - */ - -#ifndef _NETSURF_DESKTOP_FONT_H_ -#define _NETSURF_DESKTOP_FONT_H_ - -#include -#include - -#include "desktop/plot_style.h" - -struct font_functions -{ - /** - * Measure the width of a string. - * - * \param[in] fstyle plot style for this text - * \param[in] string UTF-8 string to measure - * \param[in] length length of string, in bytes - * \param[out] width updated to width of string[0..length) - * \return true on success and width updated else false. - */ - bool (*font_width)(const plot_font_style_t *fstyle, - const char *string, size_t length, - int *width); - /** - * Find the position in a string where an x coordinate falls. - * - * \param[in] fstyle style for this text - * \param[in] string UTF-8 string to measure - * \param[in] length length of string, in bytes - * \param[in] x coordinate to search for - * \param[out] char_offset updated to offset in string of actual_x, [0..length] - * \param[out] actual_x updated to x coordinate of character closest to x - * \return true on success, false on error and error reported - */ - bool (*font_position_in_string)(const plot_font_style_t *fstyle, - const char *string, size_t length, - int x, size_t *char_offset, int *actual_x); - /** - * Find where to split a string to make it fit a width. - * - * \param fstyle style for this text - * \param string UTF-8 string to measure - * \param length length of string, in bytes - * \param x width available - * \param char_offset updated to offset in string of actual_x, [1..length] - * \param actual_x updated to x coordinate of character closest to x - * \return true on success, false on error and error reported - * - * On exit, char_offset indicates first character after split point. - * - * Note: char_offset of 0 should never be returned. - * - * Returns: - * char_offset giving split point closest to x, where actual_x <= x - * else - * char_offset giving split point closest to x, where actual_x > x - * - * Returning char_offset == length means no split possible - */ - bool (*font_split)(const plot_font_style_t *fstyle, - const char *string, size_t length, - int x, size_t *char_offset, int *actual_x); -}; - -extern const struct font_functions nsfont; - -#endif diff --git a/desktop/gui_factory.c b/desktop/gui_factory.c index 0b168f405..c15603e1c 100644 --- a/desktop/gui_factory.c +++ b/desktop/gui_factory.c @@ -37,6 +37,7 @@ #include "desktop/gui_search.h" #include "desktop/gui_clipboard.h" #include "desktop/gui_utf8.h" +#include "desktop/gui_layout.h" #include "desktop/netsurf.h" /** @@ -644,6 +645,35 @@ static nserror verify_bitmap_register(struct gui_bitmap_table *gbt) return NSERROR_OK; } +/** + * verify layout table is valid + * + * \param glt The layout table to verify. + * \return NSERROR_OK if the table is valid else NSERROR_BAD_PARAMETER. + */ +static nserror verify_layout_register(struct gui_layout_table *glt) +{ + /* check table is present */ + if (glt == NULL) { + return NSERROR_BAD_PARAMETER; + } + + /* check the mandantory fields are set */ + if (glt->width == NULL) { + return NSERROR_BAD_PARAMETER; + } + + if (glt->position == NULL) { + return NSERROR_BAD_PARAMETER; + } + + if (glt->split == NULL) { + return NSERROR_BAD_PARAMETER; + } + + return NSERROR_OK; +} + static void gui_default_quit(void) { } @@ -725,6 +755,8 @@ nserror netsurf_register(struct netsurf_table *gt) return NSERROR_BAD_PARAMETER; } + /* mandantory tables */ + /* miscellaneous table */ err = verify_misc_register(gt->misc); if (err != NSERROR_OK) { @@ -749,6 +781,14 @@ nserror netsurf_register(struct netsurf_table *gt) return err; } + /* layout table */ + err = verify_layout_register(gt->layout); + if (err != NSERROR_OK) { + return err; + } + + /* optional tables */ + /* file table */ if (gt->file == NULL) { gt->file = default_file_table; diff --git a/desktop/gui_layout.h b/desktop/gui_layout.h new file mode 100644 index 000000000..1696aee91 --- /dev/null +++ b/desktop/gui_layout.h @@ -0,0 +1,91 @@ +/* + * Copyright 2016 Vincent Sanders + * + * This file is part of NetSurf, http://www.netsurf-browser.org/ + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file + * + * Interface to platform-specific layout operation table. + * + * This table is part of the layout used to measure glyphs before + * rendering, previously referred to as font functions. + * + * \note This is an old interface within the browser, it has been + * broken out purely to make the API obvious not as an indication this + * is the correct approach. + */ + +#ifndef _NETSURF_DESKTOP_GUI_LAYOUT_H_ +#define _NETSURF_DESKTOP_GUI_LAYOUT_H_ + +struct plot_font_style; + +struct gui_layout_table +{ + /** + * Measure the width of a string. + * + * \param[in] fstyle plot style for this text + * \param[in] string UTF-8 string to measure + * \param[in] length length of string, in bytes + * \param[out] width updated to width of string[0..length) + * \return NSERROR_OK and width updated or appropriate error + * code on faliure + */ + nserror (*width)(const struct plot_font_style *fstyle, const char *string, size_t length, int *width); + + + /** + * Find the position in a string where an x coordinate falls. + * + * \param[in] fstyle style for this text + * \param[in] string UTF-8 string to measure + * \param[in] length length of string, in bytes + * \param[in] x coordinate to search for + * \param[out] char_offset updated to offset in string of actual_x, [0..length] + * \param[out] actual_x updated to x coordinate of character closest to x + * \return NSERROR_OK and char_offset and actual_x updated or appropriate error code on faliure + */ + nserror (*position)(const struct plot_font_style *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x); + + + /** + * Find where to split a string to make it fit a width. + * + * \param[in] fstyle style for this text + * \param[in] string UTF-8 string to measure + * \param[in] length length of string, in bytes + * \param[in] x width available + * \param[out] char_offset updated to offset in string of actual_x, [1..length] + * \param[out] actual_x updated to x coordinate of character closest to x + * \return NSERROR_OK or appropriate error code on faliure + * + * On exit, char_offset indicates first character after split point. + * + * \note char_offset of 0 must never be returned. + * + * Returns: + * char_offset giving split point closest to x, where actual_x <= x + * else + * char_offset giving split point closest to x, where actual_x > x + * + * Returning char_offset == length means no split possible + */ + nserror (*split)(const struct plot_font_style *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x); +}; + +#endif diff --git a/desktop/gui_table.h b/desktop/gui_table.h index 6952228e2..0ac9f0417 100644 --- a/desktop/gui_table.h +++ b/desktop/gui_table.h @@ -38,6 +38,7 @@ struct gui_search_table; struct gui_search_web_table; struct gui_llcache_table; struct gui_bitmap_table; +struct gui_layout_table; /** * NetSurf operation function table @@ -49,8 +50,9 @@ struct netsurf_table { /** * Browser table. * - * Provides miscellaneous browser functionality. The table - * is mandantory and must be provided. + * Provides miscellaneous browser functionality. + * + * The table is mandantory and must be provided. */ struct gui_misc_table *misc; @@ -58,6 +60,8 @@ struct netsurf_table { * Window table. * * Provides all operations which affect a frontends display window. + * + * The table is mandantory and must be provided. */ struct gui_window_table *window; @@ -75,15 +79,18 @@ struct netsurf_table { /** * Fetcher table + * + * The table is mandantory and must be provided. */ struct gui_fetch_table *fetch; /** * File table * - * Provides file and filename operations to the core. The - * table is optional and may be NULL in which case the default - * posix compliant operations will be used. + * Provides file and filename operations to the core. + * + * The table is optional and may be NULL in which case the + * default posix compliant operations will be used. */ struct gui_file_table *file; @@ -91,8 +98,10 @@ struct netsurf_table { * UTF8 table. * * Provides for conversion between the gui local character - * encoding and utf8. The table optional and may be NULL which - * implies the local encoding is utf8. + * encoding and utf8. + * + * The table optional and may be NULL which implies the local + * encoding is utf8. */ struct gui_utf8_table *utf8; @@ -106,9 +115,10 @@ struct netsurf_table { /** * Web search table. * - * Used by the web search provider system. The table is - * optional and may be NULL which uses the default empty - * implementation. + * Used by the web search provider system. + * + * The table is optional and may be NULL which uses the + * default empty implementation. */ struct gui_search_web_table *search_web; @@ -116,8 +126,10 @@ struct netsurf_table { * Low level cache table. * * Used by the low level cache to push objects to persistant - * storage. The table is optional and may be NULL which - * uses the default implementation. + * storage. + * + * The table is optional and may be NULL which uses the + * default implementation. */ struct gui_llcache_table *llcache; @@ -125,10 +137,21 @@ struct netsurf_table { * Bitmap table. * * Used by the image convertors as a generic interface to - * native platform-specific image formats. The table - * is mandantory and must be provided. + * native platform-specific image formats. + * + * The table is mandantory and must be provided. */ struct gui_bitmap_table *bitmap; + + /** + * Layout table + * + * Used by the layout process to measure glyphs in a frontend + * specific manner. + * + * The table is mandantory and must be provided. + */ + struct gui_layout_table *layout; }; #endif diff --git a/desktop/plot_style.h b/desktop/plot_style.h index 45813d40d..af54e79cb 100644 --- a/desktop/plot_style.h +++ b/desktop/plot_style.h @@ -150,7 +150,7 @@ typedef unsigned long plot_font_flags_t; /** * Font style for plotting */ -typedef struct { +typedef struct plot_font_style { plot_font_generic_family_t family; /**< Generic family to plot with */ int size; /**< Font size, in points * FONT_SIZE_SCALE */ int weight; /**< Font weight: value in range [100,900] as per CSS */ diff --git a/desktop/print.c b/desktop/print.c index 64d796946..55caf5105 100644 --- a/desktop/print.c +++ b/desktop/print.c @@ -246,7 +246,7 @@ bool print_cleanup(hlcache_handle *content, const struct printer *printer, * configuration or lack of memory. */ struct print_settings *print_make_settings(print_configuration configuration, - const char *filename, const struct font_functions *font_func) + const char *filename, const struct gui_layout_table *font_func) { struct print_settings *settings; css_fixed length = 0; diff --git a/desktop/print.h b/desktop/print.h index 63c2935f1..5c51981d1 100644 --- a/desktop/print.h +++ b/desktop/print.h @@ -38,6 +38,7 @@ struct hlcache_handle; struct printer; +struct gui_layout_table; enum { MARGINLEFT = 0, MARGINRIGHT = 1, MARGINTOP = 2, MARGINBOTTOM = 3}; @@ -60,7 +61,7 @@ struct print_settings{ const char *output; /*the functions used to measure fonts*/ - const struct font_functions *font_func; + const struct gui_layout_table *font_func; }; @@ -73,8 +74,10 @@ bool print_draw_next_page(const struct printer *printer, bool print_cleanup(struct hlcache_handle *, const struct printer *, struct print_settings *settings); -struct print_settings *print_make_settings(print_configuration configuration, - const char *url, const struct font_functions *font_func); +/** + * Setup print settings for print render operation. + */ +struct print_settings *print_make_settings(print_configuration configuration, const char *url, const struct gui_layout_table *font_func); /*is the content currently redrawn for printing?*/ extern bool html_redraw_printing; diff --git a/desktop/textarea.c b/desktop/textarea.c index 9970bd6ab..4a7e008bc 100644 --- a/desktop/textarea.c +++ b/desktop/textarea.c @@ -37,8 +37,8 @@ #include "desktop/textinput.h" #include "desktop/plotters.h" #include "desktop/scrollbar.h" -#include "desktop/font.h" #include "desktop/gui_clipboard.h" +#include "desktop/gui_layout.h" #include "desktop/gui_internal.h" #define CARET_COLOR 0x0000FF @@ -404,7 +404,7 @@ static bool textarea_set_caret_internal(struct textarea *ta, int caret_b) /* find byte offset of caret position */ b_off = index; - nsfont.font_width(&ta->fstyle, + guit->layout->width(&ta->fstyle, ta->show->data + ta->lines[ta->caret_pos.line].b_start, b_off - ta->lines[ta->caret_pos.line].b_start, @@ -873,12 +873,12 @@ static bool textarea_reflow_singleline(struct textarea *ta, size_t b_off, } /* Measure new width */ - nsfont.font_width(&ta->fstyle, ta->show->data, + guit->layout->width(&ta->fstyle, ta->show->data, ta->show->len - 1, &x); /* Get width of retained text */ if (b_off != ta->lines[0].b_length) { - nsfont.font_width(&ta->fstyle, ta->show->data, + guit->layout->width(&ta->fstyle, ta->show->data, b_off, &retained_width); } else { retained_width = ta->lines[0].width; @@ -1019,7 +1019,7 @@ static bool textarea_reflow_multiline(struct textarea *ta, } /* Wrap current line in paragraph */ - nsfont.font_split(&ta->fstyle, text, para_end - text, + guit->layout->split(&ta->fstyle, text, para_end - text, avail_width, &b_off, &x); /* b_off now marks space, or end of paragraph */ @@ -1201,7 +1201,7 @@ static bool textarea_reflow_multiline(struct textarea *ta, ta->lines[start].b_start; text = ta->text.data + ta->lines[start].b_start; - nsfont.font_width(&ta->fstyle, text, + guit->layout->width(&ta->fstyle, text, retain_end, &retained_width); r->x0 = max(r->x0, @@ -1252,7 +1252,7 @@ static size_t textarea_get_b_off_xy(struct textarea *ta, int x, int y, line = 0; /* Get byte position */ - nsfont.font_position_in_string(&ta->fstyle, + guit->layout->position(&ta->fstyle, ta->show->data + ta->lines[line].b_start, ta->lines[line].b_length, x, &bpos, &x); @@ -2303,7 +2303,7 @@ void textarea_redraw(struct textarea *ta, int x, int y, colour bg, float scale, /* find clip left/right for this part of line */ left = right; if (b_len_part != b_len) { - nsfont.font_width(&fstyle, line_text, b_end, + guit->layout->width(&fstyle, line_text, b_end, &right); } else { right = ta->lines[line].width; diff --git a/desktop/treeview.c b/desktop/treeview.c index 210c6f97e..97cf30d23 100644 --- a/desktop/treeview.c +++ b/desktop/treeview.c @@ -32,8 +32,8 @@ #include "desktop/plotters.h" #include "desktop/textarea.h" #include "desktop/treeview.h" -#include "desktop/font.h" #include "desktop/gui_clipboard.h" +#include "desktop/gui_layout.h" #include "desktop/gui_internal.h" /** @todo get rid of REDRAW_MAX -- need to be able to know window size */ @@ -540,10 +540,10 @@ static inline void treeview_insert_node(treeview_node *a, /* Parent is expanded, so inserted node will be visible and * affect layout */ if (a->text.width == 0) { - nsfont.font_width(&plot_style_odd.text, - a->text.data, - a->text.len, - &(a->text.width)); + guit->layout->width(&plot_style_odd.text, + a->text.data, + a->text.len, + &(a->text.width)); } do { @@ -644,7 +644,7 @@ nserror treeview_update_node_folder(treeview *tree, if (folder->parent->flags & TV_NFLAGS_EXPANDED) { /* Text will be seen, get its width */ - nsfont.font_width(&plot_style_odd.text, + guit->layout->width(&plot_style_odd.text, folder->text.data, folder->text.len, &(folder->text.width)); @@ -694,7 +694,7 @@ nserror treeview_update_node_entry(treeview *tree, if (entry->parent->flags & TV_NFLAGS_EXPANDED) { /* Text will be seen, get its width */ - nsfont.font_width(&plot_style_odd.text, + guit->layout->width(&plot_style_odd.text, entry->text.data, entry->text.len, &(entry->text.width)); @@ -714,7 +714,7 @@ nserror treeview_update_node_entry(treeview *tree, if (entry->flags & TV_NFLAGS_EXPANDED) { /* Text will be seen, get its width */ - nsfont.font_width(&plot_style_odd.text, + guit->layout->width(&plot_style_odd.text, e->fields[i - 1].value.data, e->fields[i - 1].value.len, &(e->fields[i - 1].value.width)); @@ -1311,7 +1311,7 @@ nserror treeview_create(treeview **tree, f->value.data = lwc_string_data(fields[i].field); f->value.len = lwc_string_length(fields[i].field); - nsfont.font_width(&plot_style_odd.text, f->value.data, + guit->layout->width(&plot_style_odd.text, f->value.data, f->value.len, &(f->value.width)); if (f->flags & TREE_FLAG_SHOW_NAME) @@ -1411,7 +1411,7 @@ static nserror treeview_node_expand_internal(treeview *tree, do { assert((child->flags & TV_NFLAGS_EXPANDED) == false); if (child->text.width == 0) { - nsfont.font_width(&plot_style_odd.text, + guit->layout->width(&plot_style_odd.text, child->text.data, child->text.len, &(child->text.width)); @@ -1432,7 +1432,7 @@ static nserror treeview_node_expand_internal(treeview *tree, for (i = 0; i < tree->n_fields - 1; i++) { if (e->fields[i].value.width == 0) { - nsfont.font_width(&plot_style_odd.text, + guit->layout->width(&plot_style_odd.text, e->fields[i].value.data, e->fields[i].value.len, &(e->fields[i].value.width)); -- cgit v1.2.3