From f90ed33501485663a091baefe41a87db93d2db91 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 24 Apr 2016 21:13:20 +0100 Subject: update cocoa frontend to use font layout table --- cocoa/NetsurfApp.m | 2 ++ cocoa/font.h | 4 +++- cocoa/font.m | 51 +++++++++++++++++++++++++++++---------------------- 3 files changed, 34 insertions(+), 23 deletions(-) (limited to 'cocoa') diff --git a/cocoa/NetsurfApp.m b/cocoa/NetsurfApp.m index 3d5ff0691..ec798be61 100644 --- a/cocoa/NetsurfApp.m +++ b/cocoa/NetsurfApp.m @@ -25,6 +25,7 @@ #import "cocoa/selection.h" #import "cocoa/fetch.h" #import "cocoa/bitmap.h" +#import "cocoa/font.h" #import "utils/filename.h" #import "utils/log.h" @@ -235,6 +236,7 @@ int main( int argc, char **argv ) .fetch = cocoa_fetch_table, .search = cocoa_search_table, .bitmap = cocoa_bitmap_table, + .layout = cocoa_layout_table, }; error = netsurf_register(&cocoa_table); diff --git a/cocoa/font.h b/cocoa/font.h index 2a31e2c08..cabd2b933 100644 --- a/cocoa/font.h +++ b/cocoa/font.h @@ -21,6 +21,8 @@ #import "desktop/plot_style.h" -void cocoa_draw_string( CGFloat x, CGFloat y, const char *bytes, size_t length, const plot_font_style_t *style ); +void cocoa_draw_string( CGFloat x, CGFloat y, const char *bytes, size_t length, const struct plot_font_style *style ); + +struct gui_layout_table *cocoa_layout_table; #endif diff --git a/cocoa/font.m b/cocoa/font.m index 338339ba8..fb0562be5 100644 --- a/cocoa/font.m +++ b/cocoa/font.m @@ -38,21 +38,24 @@ static NSDictionary *cocoa_font_attributes( const plot_font_style_t *style ); static NSTextStorage *cocoa_text_storage = nil; static NSTextContainer *cocoa_text_container = nil; -static bool nsfont_width(const plot_font_style_t *style, - const char *string, size_t length, - int *width) +static nserror cocoa_font_width(const plot_font_style_t *style, + const char *string, size_t length, + int *width) { - NSLayoutManager *layout = cocoa_prepare_layout_manager( string, length, style ); + NSLayoutManager *layout; + layout = cocoa_prepare_layout_manager( string, length, style ); *width = cocoa_layout_width( layout ); - return true; + return NSERROR_OK; } -static bool nsfont_position_in_string(const plot_font_style_t *style, - const char *string, size_t length, - int x, size_t *char_offset, int *actual_x) +static nserror cocoa_font_position(const plot_font_style_t *style, + const char *string, size_t length, + int x, size_t *char_offset, int *actual_x) { NSLayoutManager *layout = cocoa_prepare_layout_manager( string, length, style ); - if (layout == nil) return false; + if (layout == nil) { + return NSERROR_BAD_PARAMETER; + } NSUInteger glyphIndex = cocoa_glyph_for_location( layout, x ); NSUInteger chars = [layout characterIndexForGlyphAtIndex: glyphIndex]; @@ -61,17 +64,17 @@ static bool nsfont_position_in_string(const plot_font_style_t *style, else *char_offset = cocoa_bytes_for_characters( string, chars ); *actual_x = cocoa_pt_to_px( NSMaxX( [layout boundingRectForGlyphRange: NSMakeRange( glyphIndex - 1, 1 ) - inTextContainer: cocoa_text_container] ) ); + inTextContainer: cocoa_text_container] ) ); - return true; + return NSERROR_OK; } -static bool nsfont_split(const plot_font_style_t *style, - const char *string, size_t length, - int x, size_t *char_offset, int *actual_x) +static nserror cocoa_font_split(const plot_font_style_t *style, + const char *string, size_t length, + int x, size_t *char_offset, int *actual_x) { NSLayoutManager *layout = cocoa_prepare_layout_manager( string, length, style ); - if (layout == nil) return false; + if (layout == nil) return NSERROR_BAD_PARAMETER; NSUInteger glyphIndex = cocoa_glyph_for_location( layout, x ); NSUInteger chars = [layout characterIndexForGlyphAtIndex: glyphIndex]; @@ -79,7 +82,7 @@ static bool nsfont_split(const plot_font_style_t *style, if (chars >= [cocoa_text_storage length]) { *char_offset = length; *actual_x = cocoa_layout_width( layout ); - return true; + return NSERROR_OK; } @@ -87,21 +90,25 @@ static bool nsfont_split(const plot_font_style_t *style, if (chars == NSNotFound) { *char_offset = 0; *actual_x = 0; - return true; + return NSERROR_OK; } *char_offset = cocoa_bytes_for_characters( string, chars ); *actual_x = cocoa_layout_width_chars( layout, chars ); - return true; + return NSERROR_OK; } -const struct font_functions nsfont = { - nsfont_width, - nsfont_position_in_string, - nsfont_split + +static struct gui_layout_table layout_table = { + .width = cocoa_font_width, + .position = fb_font_position, + .split = fb_font_split, }; +struct gui_layout_table *cocoa_layout_table = &layout_table; + + #pragma mark - void cocoa_draw_string( CGFloat x, CGFloat y, const char *bytes, size_t length, const plot_font_style_t *style ) -- cgit v1.2.3