2011-01-12 23:21:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Sven Weidauer <sven.weidauer@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2011-01-31 22:12:00 +03:00
|
|
|
#import "cocoa/plotter.h"
|
|
|
|
#import "cocoa/font.h"
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2013-05-26 01:46:27 +04:00
|
|
|
#import "utils/nsoption.h"
|
2016-04-24 23:21:49 +03:00
|
|
|
#import "desktop/gui_layout.h"
|
2011-01-31 22:12:00 +03:00
|
|
|
#import "desktop/plotters.h"
|
2011-01-12 23:21:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
static NSLayoutManager *cocoa_prepare_layout_manager( const char *string, size_t length,
|
|
|
|
const plot_font_style_t *style );
|
2011-01-26 12:58:29 +03:00
|
|
|
|
|
|
|
static CGFloat cocoa_layout_width( NSLayoutManager *layout );
|
|
|
|
static CGFloat cocoa_layout_width_chars( NSLayoutManager *layout, size_t characters );
|
|
|
|
static NSUInteger cocoa_glyph_for_location( NSLayoutManager *layout, CGFloat x );
|
|
|
|
static size_t cocoa_bytes_for_characters( const char *string, size_t characters );
|
|
|
|
static NSDictionary *cocoa_font_attributes( const plot_font_style_t *style );
|
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
static NSTextStorage *cocoa_text_storage = nil;
|
|
|
|
static NSTextContainer *cocoa_text_container = nil;
|
|
|
|
|
2016-04-24 23:13:20 +03:00
|
|
|
static nserror cocoa_font_width(const plot_font_style_t *style,
|
|
|
|
const char *string, size_t length,
|
|
|
|
int *width)
|
2011-01-12 23:21:17 +03:00
|
|
|
{
|
2016-04-24 23:13:20 +03:00
|
|
|
NSLayoutManager *layout;
|
|
|
|
layout = cocoa_prepare_layout_manager( string, length, style );
|
2011-01-26 12:58:29 +03:00
|
|
|
*width = cocoa_layout_width( layout );
|
2016-04-24 23:13:20 +03:00
|
|
|
return NSERROR_OK;
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
2016-04-24 23:13:20 +03:00
|
|
|
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)
|
2011-01-12 23:21:17 +03:00
|
|
|
{
|
|
|
|
NSLayoutManager *layout = cocoa_prepare_layout_manager( string, length, style );
|
2016-04-24 23:13:20 +03:00
|
|
|
if (layout == nil) {
|
|
|
|
return NSERROR_BAD_PARAMETER;
|
|
|
|
}
|
2011-01-25 13:25:10 +03:00
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
NSUInteger glyphIndex = cocoa_glyph_for_location( layout, x );
|
|
|
|
NSUInteger chars = [layout characterIndexForGlyphAtIndex: glyphIndex];
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2011-01-27 10:55:06 +03:00
|
|
|
if (chars >= [cocoa_text_storage length]) *char_offset = length;
|
|
|
|
else *char_offset = cocoa_bytes_for_characters( string, chars );
|
|
|
|
|
2011-01-27 14:43:48 +03:00
|
|
|
*actual_x = cocoa_pt_to_px( NSMaxX( [layout boundingRectForGlyphRange: NSMakeRange( glyphIndex - 1, 1 )
|
2016-04-24 23:13:20 +03:00
|
|
|
inTextContainer: cocoa_text_container] ) );
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2016-04-24 23:13:20 +03:00
|
|
|
return NSERROR_OK;
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
2016-04-24 23:13:20 +03:00
|
|
|
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)
|
2011-01-12 23:21:17 +03:00
|
|
|
{
|
2011-01-26 12:58:29 +03:00
|
|
|
NSLayoutManager *layout = cocoa_prepare_layout_manager( string, length, style );
|
2016-04-24 23:13:20 +03:00
|
|
|
if (layout == nil) return NSERROR_BAD_PARAMETER;
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
NSUInteger glyphIndex = cocoa_glyph_for_location( layout, x );
|
|
|
|
NSUInteger chars = [layout characterIndexForGlyphAtIndex: glyphIndex];
|
|
|
|
|
2011-01-27 10:55:06 +03:00
|
|
|
if (chars >= [cocoa_text_storage length]) {
|
2011-01-26 12:58:29 +03:00
|
|
|
*char_offset = length;
|
|
|
|
*actual_x = cocoa_layout_width( layout );
|
2016-04-24 23:13:20 +03:00
|
|
|
return NSERROR_OK;
|
2011-01-26 12:58:29 +03:00
|
|
|
}
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2011-01-13 02:21:36 +03:00
|
|
|
|
2011-03-04 17:53:12 +03:00
|
|
|
chars = [[cocoa_text_storage string] rangeOfString: @" " options: NSBackwardsSearch range: NSMakeRange( 0, chars + 1 )].location;
|
2011-01-26 12:58:29 +03:00
|
|
|
if (chars == NSNotFound) {
|
|
|
|
*char_offset = 0;
|
|
|
|
*actual_x = 0;
|
2016-04-24 23:13:20 +03:00
|
|
|
return NSERROR_OK;
|
2011-01-26 12:58:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*char_offset = cocoa_bytes_for_characters( string, chars );
|
|
|
|
*actual_x = cocoa_layout_width_chars( layout, chars );
|
|
|
|
|
2016-04-24 23:13:20 +03:00
|
|
|
return NSERROR_OK;
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
2016-04-24 23:13:20 +03:00
|
|
|
|
|
|
|
static struct gui_layout_table layout_table = {
|
|
|
|
.width = cocoa_font_width,
|
2016-04-24 23:21:49 +03:00
|
|
|
.position = cocoa_font_position,
|
|
|
|
.split = cocoa_font_split,
|
2011-01-26 12:58:29 +03:00
|
|
|
};
|
|
|
|
|
2016-04-24 23:13:20 +03:00
|
|
|
struct gui_layout_table *cocoa_layout_table = &layout_table;
|
|
|
|
|
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
#pragma mark -
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2011-01-27 14:43:48 +03:00
|
|
|
void cocoa_draw_string( CGFloat x, CGFloat y, const char *bytes, size_t length, const plot_font_style_t *style )
|
2011-01-12 23:21:17 +03:00
|
|
|
{
|
2011-03-14 17:17:46 +03:00
|
|
|
NSLayoutManager *layout = cocoa_prepare_layout_manager( bytes, length, style );
|
2011-01-28 10:36:09 +03:00
|
|
|
if (layout == nil) return;
|
2011-01-18 00:47:08 +03:00
|
|
|
|
2011-01-28 10:36:09 +03:00
|
|
|
NSFont *font = [cocoa_text_storage attribute: NSFontAttributeName atIndex: 0 effectiveRange: NULL];
|
|
|
|
CGFloat baseline = [layout defaultLineHeightForFont: font] * 3.0 / 4.0;
|
|
|
|
|
|
|
|
NSRange glyphRange = [layout glyphRangeForTextContainer: cocoa_text_container];
|
|
|
|
[layout drawGlyphsForGlyphRange: glyphRange atPoint: NSMakePoint( x, y - baseline )];
|
2011-01-26 12:58:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
static inline CGFloat cocoa_layout_width( NSLayoutManager *layout )
|
|
|
|
{
|
|
|
|
if (layout == nil) return 0.0;
|
2011-01-18 00:47:08 +03:00
|
|
|
|
2011-01-27 14:43:48 +03:00
|
|
|
return cocoa_pt_to_px( NSWidth( [layout usedRectForTextContainer: cocoa_text_container] ) );
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
static inline CGFloat cocoa_layout_width_chars( NSLayoutManager *layout, size_t characters )
|
2011-01-12 23:21:17 +03:00
|
|
|
{
|
2011-01-26 12:58:29 +03:00
|
|
|
NSUInteger glyphIndex = [layout glyphIndexForCharacterAtIndex: characters];
|
2011-01-27 14:43:48 +03:00
|
|
|
return cocoa_pt_to_px( [layout locationForGlyphAtIndex: glyphIndex].x );
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
static inline NSUInteger cocoa_glyph_for_location( NSLayoutManager *layout, CGFloat x )
|
2011-01-12 23:21:17 +03:00
|
|
|
{
|
2011-01-26 12:58:29 +03:00
|
|
|
CGFloat fraction = 0.0;
|
2011-01-27 14:43:48 +03:00
|
|
|
NSUInteger glyphIndex = [layout glyphIndexForPoint: NSMakePoint( cocoa_px_to_pt( x ), 0 )
|
2011-01-26 12:58:29 +03:00
|
|
|
inTextContainer: cocoa_text_container
|
|
|
|
fractionOfDistanceThroughGlyph: &fraction];
|
2011-01-28 16:22:34 +03:00
|
|
|
if (fraction >= 1.0) ++glyphIndex;
|
2011-01-26 12:58:29 +03:00
|
|
|
return glyphIndex;
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
static inline size_t cocoa_bytes_for_characters( const char *string, size_t chars )
|
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
while (chars-- > 0) {
|
|
|
|
uint8_t ch = ((uint8_t *)string)[offset];
|
|
|
|
|
|
|
|
if (0xC2 <= ch && ch <= 0xDF) offset += 2;
|
|
|
|
else if (0xE0 <= ch && ch <= 0xEF) offset += 3;
|
|
|
|
else if (0xF0 <= ch && ch <= 0xF4) offset += 4;
|
|
|
|
else offset++;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
2011-01-12 23:21:17 +03:00
|
|
|
|
|
|
|
static NSLayoutManager *cocoa_prepare_layout_manager( const char *bytes, size_t length,
|
|
|
|
const plot_font_style_t *style )
|
|
|
|
{
|
|
|
|
if (NULL == bytes || 0 == length) return nil;
|
2011-01-28 16:22:34 +03:00
|
|
|
|
|
|
|
NSString *string = [[[NSString alloc] initWithBytes: bytes length:length encoding:NSUTF8StringEncoding] autorelease];
|
|
|
|
if (string == nil) return nil;
|
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
static NSLayoutManager *layout = nil;
|
|
|
|
if (nil == layout) {
|
|
|
|
cocoa_text_container = [[NSTextContainer alloc] initWithContainerSize: NSMakeSize( CGFLOAT_MAX, CGFLOAT_MAX )];
|
|
|
|
[cocoa_text_container setLineFragmentPadding: 0];
|
2011-01-26 12:58:29 +03:00
|
|
|
|
2011-01-12 23:21:17 +03:00
|
|
|
layout = [[NSLayoutManager alloc] init];
|
|
|
|
[layout addTextContainer: cocoa_text_container];
|
|
|
|
}
|
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
static NSString *oldString = 0;
|
2011-01-31 10:57:52 +03:00
|
|
|
static plot_font_style_t oldStyle = { 0, 0, 0, 0, 0, 0 };
|
2011-01-26 12:58:29 +03:00
|
|
|
|
|
|
|
const bool styleChanged = memcmp( style, &oldStyle, sizeof oldStyle ) != 0;
|
|
|
|
|
|
|
|
if ([oldString isEqualToString: string] && !styleChanged) {
|
|
|
|
return layout;
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
2011-01-26 12:58:29 +03:00
|
|
|
|
|
|
|
[oldString release];
|
|
|
|
oldString = [string copy];
|
|
|
|
oldStyle = *style;
|
|
|
|
|
|
|
|
static NSDictionary *attributes = nil;
|
|
|
|
if (styleChanged || attributes == nil) {
|
|
|
|
[attributes release];
|
|
|
|
attributes = [cocoa_font_attributes( style ) retain];
|
|
|
|
}
|
2011-01-28 16:22:34 +03:00
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
[cocoa_text_storage release];
|
|
|
|
cocoa_text_storage = [[NSTextStorage alloc] initWithString: string attributes: attributes];
|
|
|
|
[cocoa_text_storage addLayoutManager: layout];
|
|
|
|
|
|
|
|
[layout ensureLayoutForTextContainer: cocoa_text_container];
|
2011-01-12 23:21:17 +03:00
|
|
|
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
static NSString * const cocoa_font_families[PLOT_FONT_FAMILY_COUNT] = {
|
|
|
|
[PLOT_FONT_FAMILY_SERIF] = @"Times",
|
|
|
|
[PLOT_FONT_FAMILY_SANS_SERIF] = @"Helvetica",
|
|
|
|
[PLOT_FONT_FAMILY_MONOSPACE] = @"Courier",
|
|
|
|
[PLOT_FONT_FAMILY_CURSIVE] = @"Apple Chancery",
|
|
|
|
[PLOT_FONT_FAMILY_FANTASY] = @"Marker Felt"
|
|
|
|
};
|
2011-01-17 18:00:18 +03:00
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
static inline NSFont *cocoa_font_get_nsfont( const plot_font_style_t *style )
|
2011-01-12 23:21:17 +03:00
|
|
|
{
|
2011-01-26 12:58:29 +03:00
|
|
|
NSFont *font = [NSFont fontWithName: cocoa_font_families[style->family]
|
2011-03-04 17:53:16 +03:00
|
|
|
size: (CGFloat)style->size / FONT_SIZE_SCALE];
|
2011-01-12 23:21:17 +03:00
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
NSFontTraitMask traits = 0;
|
|
|
|
if (style->flags & FONTF_ITALIC || style->flags & FONTF_OBLIQUE) traits |= NSItalicFontMask;
|
|
|
|
if (style->flags & FONTF_SMALLCAPS) traits |= NSSmallCapsFontMask;
|
|
|
|
if (style->weight > 400) traits |= NSBoldFontMask;
|
|
|
|
|
|
|
|
if (0 != traits) {
|
|
|
|
NSFontManager *fm = [NSFontManager sharedFontManager];
|
|
|
|
font = [fm convertFont: font toHaveTrait: traits];
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
2011-01-26 12:58:29 +03:00
|
|
|
|
|
|
|
return font;
|
2011-01-12 23:21:17 +03:00
|
|
|
}
|
|
|
|
|
2011-01-26 12:58:29 +03:00
|
|
|
static inline NSDictionary *cocoa_font_attributes( const plot_font_style_t *style )
|
|
|
|
{
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
cocoa_font_get_nsfont( style ), NSFontAttributeName,
|
|
|
|
cocoa_convert_colour( style->foreground ), NSForegroundColorAttributeName,
|
|
|
|
nil];
|
2014-10-13 14:56:31 +04:00
|
|
|
}
|