From 748e38ab87b917e6e86cbdc1471b245ea7d7054a Mon Sep 17 00:00:00 2001 From: Adrian Lees Date: Tue, 18 Jul 2006 01:51:19 +0000 Subject: Use desktop font svn path=/trunk/netsurf/; revision=2778 --- riscos/gui.c | 20 +++++--- riscos/sslcert.c | 8 ++- riscos/textarea.c | 14 ++++-- riscos/textarea.h | 4 +- riscos/treeview.c | 12 +++-- riscos/wimp.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ riscos/wimp.h | 9 ++++ 7 files changed, 195 insertions(+), 19 deletions(-) (limited to 'riscos') diff --git a/riscos/gui.c b/riscos/gui.c index 6cd8c7715..abedf7929 100644 --- a/riscos/gui.c +++ b/riscos/gui.c @@ -177,7 +177,7 @@ static struct { } prev_sigs; /** Accepted wimp user messages. */ -static wimp_MESSAGE_LIST(41) task_messages = { { +static wimp_MESSAGE_LIST(42) task_messages = { { message_HELP_REQUEST, message_DATA_SAVE, message_DATA_SAVE_ACK, @@ -189,11 +189,12 @@ static wimp_MESSAGE_LIST(41) task_messages = { { message_MENU_WARNING, message_MENUS_DELETED, message_WINDOW_INFO, - message_MODE_CHANGE, message_CLAIM_ENTITY, message_DATA_REQUEST, message_DRAGGING, message_DRAG_CLAIM, + message_MODE_CHANGE, + message_FONT_CHANGED, #ifdef WITH_URI message_URI_PROCESS, message_URI_RETURN_RESULT, @@ -435,6 +436,7 @@ void gui_init(int argc, char** argv) nsfont_init(); ro_gui_get_screen_properties(); + ro_gui_wimp_get_desktop_font(); /* Issue a *Desktop to poke AcornURI into life */ if (getenv("NetSurf$Start_URI_Handler")) @@ -1354,11 +1356,6 @@ void ro_gui_user_message(wimp_event_no event, wimp_message *message) ro_gui_menu_closed(true); break; - case message_MODE_CHANGE: - ro_gui_get_screen_properties(); - rufl_invalidate_cache(); - break; - case message_CLAIM_ENTITY: ro_gui_selection_claim_entity((wimp_full_message_claim_entity*)message); break; @@ -1367,6 +1364,15 @@ void ro_gui_user_message(wimp_event_no event, wimp_message *message) ro_gui_selection_data_request((wimp_full_message_data_request*)message); break; + case message_MODE_CHANGE: + ro_gui_get_screen_properties(); + rufl_invalidate_cache(); + break; + + case message_FONT_CHANGED: + ro_gui_wimp_get_desktop_font(); + break; + #ifdef WITH_URI case message_URI_PROCESS: if (event != wimp_USER_MESSAGE_ACKNOWLEDGE) diff --git a/riscos/sslcert.c b/riscos/sslcert.c index b1e4146bd..43acec8b7 100644 --- a/riscos/sslcert.c +++ b/riscos/sslcert.c @@ -329,7 +329,9 @@ void ro_gui_cert_open(struct tree *tree, struct node *node) if (session->issuer) textarea_destroy(session->issuer); session->issuer = textarea_create(w, ICON_CERT_ISSUER, - TEXTAREA_MULTILINE | TEXTAREA_READONLY, NULL, 0); + TEXTAREA_MULTILINE | TEXTAREA_READONLY, + ro_gui_desktop_font_family, ro_gui_desktop_font_size, + ro_gui_desktop_font_style); if (!session->issuer) { xwimp_delete_window(w); warn_user("NoMemory", 0); @@ -345,7 +347,9 @@ void ro_gui_cert_open(struct tree *tree, struct node *node) if (session->subject) textarea_destroy(session->subject); session->subject = textarea_create(w, ICON_CERT_SUBJECT, - TEXTAREA_MULTILINE | TEXTAREA_READONLY, NULL, 0); + TEXTAREA_MULTILINE | TEXTAREA_READONLY, + ro_gui_desktop_font_family, ro_gui_desktop_font_size, + ro_gui_desktop_font_style); if (!session->subject) { textarea_destroy(session->issuer); xwimp_delete_window(w); diff --git a/riscos/textarea.c b/riscos/textarea.c index 68e26a861..0247e0980 100644 --- a/riscos/textarea.c +++ b/riscos/textarea.c @@ -63,6 +63,7 @@ static struct text_area { char *font_family; /**< Font family of text */ unsigned int font_size; /**< Font size (16ths/pt) */ + rufl_style font_style; /**< Font style (rufl) */ int line_height; /**< Total height of a line, given font size */ int line_spacing; /**< Height of line spacing, given font size */ @@ -115,10 +116,12 @@ static void textarea_open(wimp_open *open); * \param flags Text area flags * \param font_family RUfl font family to use, or NULL for default * \param font_size Font size to use (pt * 16), or 0 for default + * \param font_style Font style to use, or 0 for default * \return Opaque handle for textarea or 0 on error */ uintptr_t textarea_create(wimp_w parent, wimp_i icon, unsigned int flags, - const char *font_family, unsigned int font_size) + const char *font_family, unsigned int font_size, + rufl_style font_style) { struct text_area *ret; wimp_window_state state; @@ -156,6 +159,7 @@ uintptr_t textarea_create(wimp_w parent, wimp_i icon, unsigned int flags, return 0; } ret->font_size = font_size ? font_size : 192 /* 12pt */; + ret->font_style = font_style ? font_style : rufl_WEIGHT_400; /** \todo Better line height calculation */ ret->line_height = (int)(((ret->font_size * 1.3) / 16) * 2.0) + 1; @@ -567,7 +571,7 @@ void textarea_set_caret(uintptr_t self, unsigned int caret) for (b_off = 0; index-- > 0; b_off = utf8_next(ta->text, ta->text_len, b_off)) ; /* do nothing */ - code = rufl_width(ta->font_family, rufl_WEIGHT_400, ta->font_size, + code = rufl_width(ta->font_family, ta->font_style, ta->font_size, ta->text + ta->lines[ta->caret_pos.line].b_start, b_off - ta->lines[ta->caret_pos.line].b_start, &x); if (code != rufl_OK) { @@ -639,7 +643,7 @@ void textarea_set_caret_xy(uintptr_t self, int x, int y) if (ta->line_count - 1 < (unsigned)line) line = ta->line_count - 1; - code = rufl_x_to_offset(ta->font_family, rufl_WEIGHT_400, + code = rufl_x_to_offset(ta->font_family, ta->font_style, ta->font_size, ta->text + ta->lines[line].b_start, ta->lines[line].b_length, @@ -749,7 +753,7 @@ void textarea_reflow(struct text_area *ta, unsigned int line) for (len = ta->text_len - 1, text = ta->text; len > 0; len -= b_off, text += b_off) { - code = rufl_split(ta->font_family, rufl_WEIGHT_400, + code = rufl_split(ta->font_family, ta->font_style, ta->font_size, text, len, ta->vis_width - MARGIN_LEFT - MARGIN_RIGHT, &b_off, &x); @@ -1153,7 +1157,7 @@ void textarea_redraw_internal(wimp_draw *redraw, bool update) return; } - code = rufl_paint(ta->font_family, rufl_WEIGHT_400, + code = rufl_paint(ta->font_family, ta->font_style, ta->font_size, ta->text + ta->lines[line].b_start, ta->lines[line].b_length, diff --git a/riscos/textarea.h b/riscos/textarea.h index a0d592d91..a40bfb9ad 100644 --- a/riscos/textarea.h +++ b/riscos/textarea.h @@ -11,13 +11,15 @@ #ifndef _NETSURF_RISCOS_TEXTAREA_H_ #define _NETSURF_RISCOS_TEXTAREA_H_ +#include "rufl.h" /* Text area flags */ #define TEXTAREA_MULTILINE 0x01 /**< Text area is multiline */ #define TEXTAREA_READONLY 0x02 /**< Text area is read only */ uintptr_t textarea_create(wimp_w parent, wimp_i icon, unsigned int flags, - const char *font_family, unsigned int font_size); + const char *font_family, unsigned int font_size, + rufl_style font_style); void textarea_destroy(uintptr_t self); bool textarea_set_text(uintptr_t self, const char *text); int textarea_get_text(uintptr_t self, char *buf, unsigned int len); diff --git a/riscos/treeview.c b/riscos/treeview.c index 69b323a20..4978f9cd7 100644 --- a/riscos/treeview.c +++ b/riscos/treeview.c @@ -49,6 +49,7 @@ static void ro_gui_tree_launch_selected_node(struct tree *tree, struct node *nod static bool ro_gui_tree_launch_node(struct tree *tree, struct node *node); static void tree_handle_node_changed_callback(void *p); + /* an array of sprite addresses for Tinct */ static char *ro_gui_tree_sprites[2]; @@ -305,7 +306,8 @@ void tree_draw_node_element(struct tree *tree, struct node_element *element) { error->errnum, error->errmess)); return; } - code = rufl_paint("Homerton", rufl_WEIGHT_400, 192, + code = rufl_paint(ro_gui_desktop_font_family, ro_gui_desktop_font_style, + ro_gui_desktop_font_size, element->text, strlen(element->text), x0 + 8, y0 + 10, rufl_BLEND_FONT); @@ -449,8 +451,8 @@ void tree_recalculate_node_element(struct node_element *element) { assert(element->sprite); case NODE_ELEMENT_TEXT: assert(element->text); - - code = rufl_width("Homerton", rufl_WEIGHT_400, 192, + code = rufl_width(ro_gui_desktop_font_family, ro_gui_desktop_font_style, + ro_gui_desktop_font_size, element->text, strlen(element->text), &element->box.width); if (code != rufl_OK) { @@ -1041,7 +1043,9 @@ void ro_gui_tree_start_edit(struct tree *tree, struct node_element *element, error->errnum, error->errmess)); tree->textarea_handle = textarea_create((wimp_w)tree->handle, - (wimp_i)tree->edit_handle, 0, "Homerton", 192); + (wimp_i)tree->edit_handle, 0, ro_gui_desktop_font_family, + ro_gui_desktop_font_size, + ro_gui_desktop_font_style); if (!tree->textarea_handle) { ro_gui_tree_stop_edit(tree); return; diff --git a/riscos/wimp.c b/riscos/wimp.c index b0b2fd020..1636a412c 100644 --- a/riscos/wimp.c +++ b/riscos/wimp.c @@ -10,6 +10,7 @@ */ #include +#include #include #include #include @@ -29,7 +30,15 @@ #include "netsurf/utils/utf8.h" #include "netsurf/utils/utils.h" + +/** desktop font, size and style being used */ +char ro_gui_desktop_font_family[48]; +int ro_gui_desktop_font_size = 12; +rufl_style ro_gui_desktop_font_style = rufl_WEIGHT_400; + + static void ro_gui_wimp_cache_furniture_sizes(wimp_w w); +static void ro_gui_wimp_desktop_font(char *family, size_t bufsize, int *psize, rufl_style *pstyle); static wimpextend_furniture_sizes furniture_sizes; static wimp_w furniture_window = NULL; @@ -927,3 +936,141 @@ bool ro_gui_wimp_check_window_furniture(wimp_w w, wimp_window_flags mask) { } return state.flags & mask; } + + +/** + * Looks up the current desktop font and converts that to a family name, + * font size and style flags suitable for passing directly to rufl + * + * \param family buffer to receive font family + * \param bufsize buffer size + * \param psize receives the font size in points + * \param pstyle receives the style settings to be passed to rufl + * \return pointer to family name + */ + +void ro_gui_wimp_desktop_font(char *family, size_t bufsize, int *psize, rufl_style *pstyle) { + rufl_style style = rufl_WEIGHT_400; + bool got_family = false; + char *buf = NULL; + os_error *error; + int ptx, pty; + char *p, *ep; + font_f fh; + int used; + + error = xwimpreadsysinfo_font(&fh, NULL); + if (error) { + LOG(("xwimpreadsysinfo_font: 0x%x: %s", + error->errnum, error->errmess)); + warn_user("WimpError", error->errmess); + goto failsafe; + } + + error = xfont_read_identifier(fh, NULL, &used); + if (error) { + LOG(("xfont_read_identifier: 0x%x: %s", + error->errnum, error->errmess)); + goto failsafe; + } + + buf = malloc(used+1); + if (!buf) { + warn_user("NoMemory", NULL); + goto failsafe; + } + + if (psize) { + error = xfont_read_defn(fh, buf, &ptx, &pty, NULL, NULL, NULL, NULL); + if (error) { + LOG(("xfont_read_defn: 0x%x: %s", + error->errnum, error->errmess)); + goto failsafe; + } + *psize = max(ptx, pty); + } + + error = xfont_read_identifier(fh, buf, &used); + if (error) { + LOG(("xfont_read_identifier: 0x%x: %s", + error->errnum, error->errmess)); + goto failsafe; + } + + ep = buf + used; + p = buf; + *ep = '\0'; + + while (p < ep) { + if (*p++ != '\\') continue; + + if (toupper(*p) == 'F') { + /* find the end of the family name */ + const char *match[] = { "Bold", "Italic", "Oblique" }; + const int match_len[] = { 4, 6, 7 }; + char *q = ++p; + size_t len; + while (*q > ' ' && *q != '\\' && *q != '.') q++; + len = q - p; + if (len >= bufsize) { + LOG(("font family name too long")); + goto failsafe; + } + memmove(family, p, len); + family[len] = '\0'; + got_family = true; + p = q; + while (*p > ' ' && *p != '\\') { + char *q; + int m = 0; + if (*p == '.') p++; + q = p; while (*q > ' ' && *q != '.' && *q != '\\') q++; + + while (m < NOF_ELEMENTS(match) && + (q - p != match_len[m] || + strncasecmp(p, match[m], match_len[m]))) + m++; + + switch (m) { + case 0: style = (style & ~rufl_WEIGHT_400) | + rufl_WEIGHT_700; + break; + case 1: /* no break */ + case 2: style |= rufl_SLANTED; break; + } + p = q; + } + } + else + while (*p > ' ' && *p != '\\') p++; + } + + free(buf); + + if (got_family) { + if (pstyle) *pstyle = style; + return; + } + +failsafe: + free(buf); + + memcpy(family, "Homerton", 9); + + if (psize) *psize = 12*16; + if (pstyle) *pstyle = rufl_WEIGHT_400; +} + + +/** + * Retrieve the current desktop font family, size and style from + * the WindowManager in a form suitable for passing to rufl + */ + +void ro_gui_wimp_get_desktop_font(void) +{ + ro_gui_wimp_desktop_font(ro_gui_desktop_font_family, + sizeof(ro_gui_desktop_font_family), + &ro_gui_desktop_font_size, + &ro_gui_desktop_font_style); +} diff --git a/riscos/wimp.h b/riscos/wimp.h index f169ce8f1..6cc104d5c 100644 --- a/riscos/wimp.h +++ b/riscos/wimp.h @@ -20,6 +20,13 @@ #include #include "oslib/os.h" #include "oslib/wimp.h" +#include "rufl.h" + +/** desktop font, size and style being used */ +extern char ro_gui_desktop_font_family[]; +extern int ro_gui_desktop_font_size; +extern rufl_style ro_gui_desktop_font_style; + int ro_get_hscroll_height(wimp_w w); int ro_get_vscroll_width(wimp_w w); @@ -61,4 +68,6 @@ void ro_gui_wimp_update_window_furniture(wimp_w w, wimp_window_flags bic_mask, wimp_window_flags xor_mask); bool ro_gui_wimp_check_window_furniture(wimp_w w, wimp_window_flags mask); +void ro_gui_wimp_get_desktop_font(void); + #endif -- cgit v1.2.3