summaryrefslogtreecommitdiff
path: root/cocoa/font.m
blob: 82e8ca5946aa1351d8e4323ec3a1ac7796ea531c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * 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>

#include <inttypes.h>

#include <assert.h>

#include "css/css.h"
#include "render/font.h"
#include "desktop/options.h"

#import "font.h"
#import "plotter.h"

static NSLayoutManager *cocoa_prepare_layout_manager( const char *string, size_t length, 
													 const plot_font_style_t *style );
static NSTextStorage *cocoa_text_storage = nil;
static NSTextContainer *cocoa_text_container = nil;
static CGFloat cocoa_font_scale_factor = 1.0;

static bool nsfont_width(const plot_font_style_t *style,
						 const char *string, size_t length,
						 int *width)
{
	NSCParameterAssert( NULL != width );
	
	NSLayoutManager *layout = cocoa_prepare_layout_manager( string, length, style );
	*width = NULL != layout ? NSWidth( [layout usedRectForTextContainer: cocoa_text_container] ) : 0;
	return true;
}

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)
{
	NSLayoutManager *layout = cocoa_prepare_layout_manager( string, length, style );
	CGFloat fraction = 0.0;
	NSUInteger glyphIndex = [layout glyphIndexForPoint: NSMakePoint( x, 0 ) 
									   inTextContainer: cocoa_text_container 
						fractionOfDistanceThroughGlyph: &fraction];
	if (fraction > 0) ++glyphIndex;
	NSUInteger chars = [layout characterIndexForGlyphAtIndex: glyphIndex];
	
	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++;
	}
	
	*char_offset = offset;
	*actual_x = [layout locationForGlyphAtIndex: glyphIndex].x;
	
	return true;
}

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)
{
	nsfont_position_in_string(style, string, length, x, char_offset, 
							  actual_x);
	if (*char_offset == length) return true;

	while ((string[*char_offset] != ' ') && (*char_offset > 0))
		(*char_offset)--;
	
	nsfont_position_in_string(style, string, *char_offset, x, char_offset,
							  actual_x);

	return true;
}


static NSString *cocoa_font_family_name( plot_font_generic_family_t family )
{
	switch (family) {
		case PLOT_FONT_FAMILY_SERIF: return @"Times";
		case PLOT_FONT_FAMILY_SANS_SERIF: return @"Helvetica";
		case PLOT_FONT_FAMILY_MONOSPACE: return @"Courier";
		case PLOT_FONT_FAMILY_CURSIVE: return @"Apple Chancery";
		case PLOT_FONT_FAMILY_FANTASY: return @"Marker Felt";
		default: return nil;
	}
}

static NSFont *cocoa_font_get_nsfont( const plot_font_style_t *style )
{
	NSFont *font = [NSFont fontWithName: cocoa_font_family_name( style->family ) 
						   size: cocoa_font_scale_factor * (CGFloat)style->size / FONT_SIZE_SCALE];
	
	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];
	}
	
	return font;
}

static 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];
}

static NSString *cocoa_string_from_utf8_characters( const char *string, size_t characters )
{
	if (NULL == string || 0 == characters) return nil;
	
	return [[[NSString alloc] initWithBytes: string length:characters encoding:NSUTF8StringEncoding] autorelease];
}


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;
	
	static NSLayoutManager *layout = nil;

	if (nil == layout) {
		cocoa_text_container = [[NSTextContainer alloc] initWithContainerSize: NSMakeSize( CGFLOAT_MAX, CGFLOAT_MAX )];
		[cocoa_text_container setLineFragmentPadding: 0];
		 
		layout = [[NSLayoutManager alloc] init];
		[layout addTextContainer: cocoa_text_container];
		
		cocoa_text_storage = [[NSTextStorage alloc] init];
		[cocoa_text_storage addLayoutManager: layout];
	}
	
	
	NSString *string = cocoa_string_from_utf8_characters( bytes, length );
	if (nil == string) return nil;
	
	NSDictionary *attributes = cocoa_font_attributes( style );
	NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString: string attributes: attributes];
	if (![attributedString isEqualToAttributedString: cocoa_text_storage]) {
		[cocoa_text_storage setAttributedString: attributedString];
		[layout ensureLayoutForTextContainer: cocoa_text_container];
	}
	[attributedString release];
	
	
	return layout;
}

void cocoa_set_font_scale_factor( float newFactor )
{
	cocoa_font_scale_factor = newFactor;
}

void cocoa_draw_string( int x, int y, const char *bytes, size_t length, const plot_font_style_t *style )
{
	NSLayoutManager *layout = cocoa_prepare_layout_manager( bytes, length, style );
	
	if ([cocoa_text_storage length] > 0) {
		NSFont *font = [cocoa_text_storage attribute: NSFontAttributeName atIndex: 0 effectiveRange: NULL];
		CGFloat baseline = [layout defaultBaselineOffsetForFont: font];
		
		NSRange glyphRange = [layout glyphRangeForTextContainer: cocoa_text_container];
		[layout drawGlyphsForGlyphRange: glyphRange atPoint: NSMakePoint( x, y - baseline )];
	}
}

const struct font_functions nsfont = {
	nsfont_width,
	nsfont_position_in_string,
	nsfont_split
};