From 2565a37a52a5c714e7c6bbd1808db0d6d37c6ea3 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Tue, 21 Jul 2009 10:59:53 +0000 Subject: Refactor text plotter and other font functions to remove dependency on CSS. svn path=/trunk/netsurf/; revision=8641 --- riscos/configure/con_fonts.c | 24 ++++++--- riscos/font.c | 125 ++++++++++++++++--------------------------- riscos/gui.h | 4 +- riscos/plotters.c | 13 ++--- riscos/print.c | 10 ++-- riscos/save_draw.c | 13 ++--- 6 files changed, 83 insertions(+), 106 deletions(-) (limited to 'riscos') diff --git a/riscos/configure/con_fonts.c b/riscos/configure/con_fonts.c index 4ed436871..b420b26e5 100644 --- a/riscos/configure/con_fonts.c +++ b/riscos/configure/con_fonts.c @@ -19,6 +19,7 @@ #include #include "css/css.h" #include "desktop/options.h" +#include "desktop/plot_style.h" #include "riscos/dialog.h" #include "riscos/gui.h" #include "riscos/menus.h" @@ -58,6 +59,14 @@ * probably be released at some point */ static wimp_menu *default_menu; +static const char *font_names[PLOT_FONT_FAMILY_COUNT] = { + "Sans-serif", + "Serif", + "Monospace", + "Cursive", + "Fantasy" +}; + static void ro_gui_options_fonts_default(wimp_pointer *pointer); static bool ro_gui_options_fonts_ok(wimp_w w); static bool ro_gui_options_fonts_init_menu(void); @@ -73,7 +82,7 @@ bool ro_gui_options_fonts_initialise(wimp_w w) ro_gui_set_icon_string(w, FONT_CURSIVE_FIELD, option_font_cursive, true); ro_gui_set_icon_string(w, FONT_FANTASY_FIELD, option_font_fantasy, true); ro_gui_set_icon_string(w, FONT_DEFAULT_FIELD, - css_font_family_name[option_font_default], true); + font_names[option_font_default], true); if (!ro_gui_options_fonts_init_menu()) return false; @@ -124,7 +133,7 @@ void ro_gui_options_fonts_default(wimp_pointer *pointer) ro_gui_set_icon_string(pointer->w, FONT_FANTASY_FIELD, nsfont_exists("Sassoon") ? "Sassoon" : fallback, true); ro_gui_set_icon_string(pointer->w, FONT_DEFAULT_FIELD, - css_font_family_name[1], true); + font_names[0], true); } bool ro_gui_options_fonts_ok(wimp_w w) @@ -149,16 +158,15 @@ bool ro_gui_options_fonts_ok(wimp_w w) option_font_fantasy = strdup(ro_gui_get_icon_string(w, FONT_FANTASY_FIELD)); for (i = 0; i != 5; i++) { - if (!strcmp(css_font_family_name[i+1], - ro_gui_get_icon_string(w, - FONT_DEFAULT_FIELD))) + if (!strcmp(font_names[i], ro_gui_get_icon_string(w, + FONT_DEFAULT_FIELD))) break; } if (i == 5) /* this should never happen, but still */ i = 0; - option_font_default = i + 1; + option_font_default = i; ro_gui_save_options(); return true; @@ -182,9 +190,9 @@ bool ro_gui_options_fonts_init_menu(void) ro_gui_menu_init_structure(default_menu, 5); for (i = 0; i < 5; i++) { default_menu->entries[i].data.indirected_text.text = - (char *) css_font_family_name[i+1]; + (char *) font_names[i]; default_menu->entries[i].data.indirected_text.size = - strlen(css_font_family_name[i+1]); + strlen(font_names[i]); } return true; } diff --git a/riscos/font.c b/riscos/font.c index 5051fa6f3..3aac2cd5e 100644 --- a/riscos/font.c +++ b/riscos/font.c @@ -41,13 +41,13 @@ static int nsfont_list_cmp(const void *keyval, const void *datum); static void nsfont_check_fonts(void); static void ro_gui_wimp_desktop_font(char *family, size_t bufsize, int *psize, rufl_style *pstyle); -static bool nsfont_width(const struct css_style *style, +static bool nsfont_width(const plot_font_style_t *fstyle, const char *string, size_t length, int *width); -static bool nsfont_position_in_string(const struct css_style *style, +static bool nsfont_position_in_string(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x); -static bool nsfont_split(const struct css_style *style, +static bool nsfont_split(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x); @@ -99,12 +99,12 @@ void nsfont_init(void) nsfont_check_option(&option_font_cursive, "Churchill", fallback); nsfont_check_option(&option_font_fantasy, "Sassoon", fallback); - if (option_font_default != CSS_FONT_FAMILY_SANS_SERIF && - option_font_default != CSS_FONT_FAMILY_SERIF && - option_font_default != CSS_FONT_FAMILY_MONOSPACE && - option_font_default != CSS_FONT_FAMILY_CURSIVE && - option_font_default != CSS_FONT_FAMILY_FANTASY) - option_font_default = CSS_FONT_FAMILY_SANS_SERIF; + if (option_font_default != PLOT_FONT_FAMILY_SANS_SERIF && + option_font_default != PLOT_FONT_FAMILY_SERIF && + option_font_default != PLOT_FONT_FAMILY_MONOSPACE && + option_font_default != PLOT_FONT_FAMILY_CURSIVE && + option_font_default != PLOT_FONT_FAMILY_FANTASY) + option_font_default = PLOT_FONT_FAMILY_SANS_SERIF; } @@ -218,15 +218,14 @@ void nsfont_check_fonts(void) /** * Measure the width of a string. * - * \param style css_style for this text, with style->font_size.size == - * CSS_FONT_SIZE_LENGTH + * \param fstyle plot style for this text * \param string UTF-8 string to measure * \param length length of string * \param width updated to width of string[0..length) * \return true on success, false on error and error reported */ -bool nsfont_width(const struct css_style *style, +bool nsfont_width(const plot_font_style_t *fstyle, const char *string, size_t length, int *width) { @@ -235,7 +234,7 @@ bool nsfont_width(const struct css_style *style, rufl_style font_style; rufl_code code; - nsfont_read_style(style, &font_family, &font_size, &font_style); + nsfont_read_style(fstyle, &font_family, &font_size, &font_style); code = rufl_width(font_family, font_style, font_size, string, length, @@ -259,8 +258,7 @@ bool nsfont_width(const struct css_style *style, /** * Find the position in a string where an x coordinate falls. * - * \param style css_style for this text, with style->font_size.size == - * CSS_FONT_SIZE_LENGTH + * \param fstyle style for this text * \param string UTF-8 string to measure * \param length length of string * \param x x coordinate to search for @@ -269,7 +267,7 @@ bool nsfont_width(const struct css_style *style, * \return true on success, false on error and error reported */ -bool nsfont_position_in_string(const struct css_style *style, +bool nsfont_position_in_string(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x) { @@ -278,7 +276,7 @@ bool nsfont_position_in_string(const struct css_style *style, rufl_style font_style; rufl_code code; - nsfont_read_style(style, &font_family, &font_size, &font_style); + nsfont_read_style(fstyle, &font_family, &font_size, &font_style); code = rufl_x_to_offset(font_family, font_style, font_size, string, length, @@ -303,8 +301,7 @@ bool nsfont_position_in_string(const struct css_style *style, /** * Find where to split a string to make it fit a width. * - * \param style css_style for this text, with style->font_size.size == - * CSS_FONT_SIZE_LENGTH + * \param fstyle style for this text * \param string UTF-8 string to measure * \param length length of string * \param x width available @@ -317,7 +314,7 @@ bool nsfont_position_in_string(const struct css_style *style, * char_offset == length] */ -bool nsfont_split(const struct css_style *style, +bool nsfont_split(const plot_font_style_t *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x) { @@ -326,7 +323,7 @@ bool nsfont_split(const struct css_style *style, rufl_style font_style; rufl_code code; - nsfont_read_style(style, &font_family, &font_size, &font_style); + nsfont_read_style(fstyle, &font_family, &font_size, &font_style); code = rufl_split(font_family, font_style, font_size, string, length, @@ -368,19 +365,16 @@ bool nsfont_split(const struct css_style *style, /** * Paint a string. * - * \param style css_style for this text, with style->font_size.size == - * CSS_FONT_SIZE_LENGTH + * \param fstyle plot style for this text * \param string UTF-8 string to measure * \param length length of string * \param x x coordinate * \param y y coordinate * \param scale scale to apply to font size - * \param bg background colour - * \param c colour for text * \return true on success, false on error and error reported */ -bool nsfont_paint(const struct css_style *style, const char *string, +bool nsfont_paint(const plot_font_style_t *fstyle, const char *string, size_t length, int x, int y, float scale) { const char *font_family; @@ -388,7 +382,7 @@ bool nsfont_paint(const struct css_style *style, const char *string, rufl_style font_style; rufl_code code; - nsfont_read_style(style, &font_family, &font_size, &font_style); + nsfont_read_style(fstyle, &font_family, &font_size, &font_style); code = rufl_paint(font_family, font_style, font_size * scale, string, length, x, y, @@ -407,40 +401,50 @@ bool nsfont_paint(const struct css_style *style, const char *string, /** - * Convert a css_style to a font family, size and rufl_style. + * Convert a font style to a font family, size and rufl_style. * - * \param style css_style for this text, with style->font_size.size == - * CSS_FONT_SIZE_LENGTH + * \param fstyle plot style for this text * \param font_family updated to font family * \param font_size updated to font size * \param font_style updated to font style */ -void nsfont_read_style(const struct css_style *style, +void nsfont_read_style(const plot_font_style_t *fstyle, const char **font_family, unsigned int *font_size, rufl_style *font_style) { - assert(style->font_size.size == CSS_FONT_SIZE_LENGTH); - *font_size = css_len2pt(&style->font_size.value.length, style) * 16.; + static const rufl_style weight_table[] = { + rufl_WEIGHT_100, + rufl_WEIGHT_200, + rufl_WEIGHT_300, + rufl_WEIGHT_400, + rufl_WEIGHT_500, + rufl_WEIGHT_600, + rufl_WEIGHT_700, + rufl_WEIGHT_800, + rufl_WEIGHT_900 + }; + + *font_size = fstyle->size * 16.; if (*font_size < option_font_min_size * 1.6) *font_size = option_font_min_size * 1.6; if (1600 < *font_size) *font_size = 1600; - switch (style->font_family) { - case CSS_FONT_FAMILY_SANS_SERIF: + switch (fstyle->family) { + case PLOT_FONT_FAMILY_SANS_SERIF: *font_family = option_font_sans; break; - case CSS_FONT_FAMILY_SERIF: + case PLOT_FONT_FAMILY_SERIF: *font_family = option_font_serif; break; - case CSS_FONT_FAMILY_MONOSPACE: + case PLOT_FONT_FAMILY_MONOSPACE: *font_family = option_font_mono; break; - case CSS_FONT_FAMILY_CURSIVE: + case PLOT_FONT_FAMILY_CURSIVE: *font_family = option_font_cursive; break; - case CSS_FONT_FAMILY_FANTASY: + case PLOT_FONT_FAMILY_FANTASY: *font_family = option_font_fantasy; break; default: @@ -448,50 +452,13 @@ void nsfont_read_style(const struct css_style *style, break; } - switch (style->font_style) { - case CSS_FONT_STYLE_ITALIC: - case CSS_FONT_STYLE_OBLIQUE: + if ((fstyle->flags & FONTF_ITALIC) || (fstyle->flags & FONTF_OBLIQUE)) { *font_style = rufl_SLANTED; - break; - default: + } else { *font_style = 0; - break; } - switch (style->font_weight) { - case CSS_FONT_WEIGHT_100: - *font_style |= rufl_WEIGHT_100; - break; - case CSS_FONT_WEIGHT_200: - *font_style |= rufl_WEIGHT_200; - break; - case CSS_FONT_WEIGHT_300: - *font_style |= rufl_WEIGHT_300; - break; - case CSS_FONT_WEIGHT_NORMAL: - case CSS_FONT_WEIGHT_400: - *font_style |= rufl_WEIGHT_400; - break; - case CSS_FONT_WEIGHT_500: - *font_style |= rufl_WEIGHT_500; - break; - case CSS_FONT_WEIGHT_600: - *font_style |= rufl_WEIGHT_600; - break; - case CSS_FONT_WEIGHT_BOLD: - case CSS_FONT_WEIGHT_700: - *font_style |= rufl_WEIGHT_700; - break; - case CSS_FONT_WEIGHT_800: - *font_style |= rufl_WEIGHT_800; - break; - case CSS_FONT_WEIGHT_900: - *font_style |= rufl_WEIGHT_900; - break; - default: - *font_style |= rufl_WEIGHT_400; - break; - } + *font_style |= weight_table[(fstyle->weight / 100) - 1]; } diff --git a/riscos/gui.h b/riscos/gui.h index e9874bddb..2eaf7cb27 100644 --- a/riscos/gui.h +++ b/riscos/gui.h @@ -210,9 +210,9 @@ void ro_gui_print_prepare(struct gui_window *g); void nsfont_init(void); bool nsfont_exists(const char *font_family); const char *nsfont_fallback_font(void); -bool nsfont_paint(const struct css_style *style, const char *string, +bool nsfont_paint(const plot_font_style_t *fstyle, const char *string, size_t length, int x, int y, float scale); -void nsfont_read_style(const struct css_style *style, +void nsfont_read_style(const plot_font_style_t *fstyle, const char **font_family, unsigned int *font_size, rufl_style *font_style); void ro_gui_wimp_get_desktop_font(void); diff --git a/riscos/plotters.c b/riscos/plotters.c index 38971d57c..ed44a2cd6 100644 --- a/riscos/plotters.c +++ b/riscos/plotters.c @@ -43,8 +43,8 @@ static bool ro_plot_path(const float *p, unsigned int n, colour fill, float widt colour c, const float transform[6]); static bool ro_plot_clip(int clip_x0, int clip_y0, int clip_x1, int clip_y1); -static bool ro_plot_text(int x, int y, const struct css_style *style, - const char *text, size_t length, colour bg, colour c); +static bool ro_plot_text(int x, int y, const char *text, size_t length, + const plot_font_style_t *fstyle); static bool ro_plot_disc(int x, int y, int radius, const plot_style_t *style); static bool ro_plot_arc(int x, int y, int radius, int angle1, int angle2, const plot_style_t *style); @@ -394,20 +394,21 @@ bool ro_plot_clip(int clip_x0, int clip_y0, } -bool ro_plot_text(int x, int y, const struct css_style *style, - const char *text, size_t length, colour bg, colour c) +bool ro_plot_text(int x, int y, const char *text, size_t length, + const plot_font_style_t *fstyle) { os_error *error; error = xcolourtrans_set_font_colours(font_CURRENT, - bg << 8, c << 8, 14, 0, 0, 0); + fstyle->background << 8, fstyle->foreground << 8, + 14, 0, 0, 0); if (error) { LOG(("xcolourtrans_set_font_colours: 0x%x: %s", error->errnum, error->errmess)); return false; } - return nsfont_paint(style, text, length, + return nsfont_paint(fstyle, text, length, ro_plot_origin_x + x * 2, ro_plot_origin_y - y * 2, ro_plot_scale); diff --git a/riscos/print.c b/riscos/print.c index 108fe2d7c..37af874b2 100644 --- a/riscos/print.c +++ b/riscos/print.c @@ -102,8 +102,8 @@ static bool print_fonts_plot_line(int x0, int y0, int x1, int y1, const plot_sty static bool print_fonts_plot_polygon(const int *p, unsigned int n, const plot_style_t *style); static bool print_fonts_plot_clip(int clip_x0, int clip_y0, int clip_x1, int clip_y1); -static bool print_fonts_plot_text(int x, int y, const struct css_style *style, - const char *text, size_t length, colour bg, colour c); +static bool print_fonts_plot_text(int x, int y, const char *text, size_t length, + const plot_font_style_t *fstyle); static bool print_fonts_plot_disc(int x, int y, int radius, const plot_style_t *style); static bool print_fonts_plot_arc(int x, int y, int radius, int angle1, int angle2, const plot_style_t *style); static bool print_fonts_plot_bitmap(int x, int y, int width, int height, @@ -852,15 +852,15 @@ bool print_fonts_plot_path(const float *p, unsigned int n, colour fill, float wi * Plotter for text plotting during font listing. */ -bool print_fonts_plot_text(int x, int y, const struct css_style *style, - const char *text, size_t length, colour bg, colour c) +bool print_fonts_plot_text(int x, int y, const char *text, size_t length, + const plot_font_style_t *fstyle) { const char *font_family; unsigned int font_size; rufl_style font_style; rufl_code code; - nsfont_read_style(style, &font_family, &font_size, &font_style); + nsfont_read_style(fstyle, &font_family, &font_size, &font_style); code = rufl_paint_callback(font_family, font_style, font_size, text, length, 0, 0, print_fonts_callback, 0); diff --git a/riscos/save_draw.c b/riscos/save_draw.c index 6dbd50ddb..1a75ee5f0 100644 --- a/riscos/save_draw.c +++ b/riscos/save_draw.c @@ -44,8 +44,8 @@ static bool ro_save_draw_path(const float *p, unsigned int n, colour fill, float width, colour c, const float transform[6]); static bool ro_save_draw_clip(int clip_x0, int clip_y0, int clip_x1, int clip_y1); -static bool ro_save_draw_text(int x, int y, const struct css_style *style, - const char *text, size_t length, colour bg, colour c); +static bool ro_save_draw_text(int x, int y, const char *text, size_t length, + const plot_font_style_t *fstyle); static bool ro_save_draw_disc(int x, int y, int radius, const plot_style_t *style); static bool ro_save_draw_arc(int x, int y, int radius, int angle1, int angle2, const plot_style_t *style); @@ -348,18 +348,19 @@ bool ro_save_draw_clip(int clip_x0, int clip_y0, } -bool ro_save_draw_text(int x, int y, const struct css_style *style, - const char *text, size_t length, colour bg, colour c) +bool ro_save_draw_text(int x, int y, const char *text, size_t length, + const plot_font_style_t *fstyle) { pencil_code code; const char *font_family; unsigned int font_size; rufl_style font_style; - nsfont_read_style(style, &font_family, &font_size, &font_style); + nsfont_read_style(fstyle, &font_family, &font_size, &font_style); code = pencil_text(ro_save_draw_diagram, x * 2, -y * 2, font_family, - font_style, font_size, text, length, c << 8); + font_style, font_size, text, length, + fstyle->foreground << 8); if (code != pencil_OK) return ro_save_draw_error(code); -- cgit v1.2.3