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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/*
* CairoFontInfo.m
*
* Copyright (C) 2003 Free Software Foundation, Inc.
* August 31, 2003
* Written by Banlu Kemiyatorn <object at gmail dot com>
* Base on original code of Alex Malmberg
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
* This library 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
* Library General Public License for more details.
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
*/
#include "GNUstepBase/Unicode.h"
#include <AppKit/NSAffineTransform.h>
#include "cairo/CairoFontInfo.h"
#include "cairo/CairoFontEnumerator.h"
#include <math.h>
#include <cairo-ft.h>
@implementation CairoFontInfo
- (void) setCacheSize: (unsigned int)size
{
_cacheSize = size;
if (_cachedSizes)
{
free(_cachedSizes);
}
if (_cachedGlyphs)
{
free(_cachedGlyphs);
}
_cachedSizes = malloc(sizeof(NSSize) * size);
memset(_cachedSizes, 0, sizeof(NSSize) * size);
_cachedGlyphs = malloc(sizeof(unsigned int) * size);
memset(_cachedGlyphs, 0, sizeof(unsigned int) * size);
}
- (BOOL) setupAttributes
{
cairo_font_extents_t font_extents;
cairo_font_face_t *face;
cairo_matrix_t font_matrix;
cairo_matrix_t ctm;
cairo_font_options_t *options;
/* do not forget to check for font specific
* cache size from face info FIXME FIXME
*/
ASSIGN(_faceInfo, [CairoFontEnumerator fontWithName: fontName]);
if (!_faceInfo)
{
return NO;
}
_cachedSizes = NULL;
[self setCacheSize: [_faceInfo cacheSize]];
/* setting GSFontInfo:
* weight, traits, familyName,
* mostCompatibleStringEncoding, encodingScheme,
*/
weight = [_faceInfo weight];
traits = [_faceInfo traits];
familyName = [[_faceInfo familyName] copy];
mostCompatibleStringEncoding = NSUTF8StringEncoding;
encodingScheme = @"iso10646-1";
/* setting GSFontInfo:
* xHeight, pix_width, pix_height
*/
cairo_matrix_init(&font_matrix, matrix[0], matrix[1], matrix[2],
-matrix[3], matrix[4], matrix[5]);
cairo_matrix_init_identity(&ctm);
// FIXME: Should get default font options from somewhere
options = cairo_font_options_create();
face = [_faceInfo fontFace];
if (!face)
{
return NO;
}
_scaled = cairo_scaled_font_create(face, &font_matrix, &ctm, options);
cairo_font_options_destroy(options);
if (!_scaled)
{
return NO;
}
cairo_scaled_font_extents(_scaled, &font_extents);
// FIXME: Need some adjustment here
ascender = font_extents.ascent + 3;
descender = font_extents.descent;
xHeight = font_extents.height;
maximumAdvancement = NSMakeSize(font_extents.max_x_advance,
font_extents.max_y_advance);
fontBBox = NSMakeRect(0, descender,
maximumAdvancement.width, ascender + descender);
return YES;
}
- (id) initWithFontName: (NSString *)name
matrix: (const float *)fmatrix
screenFont: (BOOL)p_screenFont
{
//NSLog(@"initWithFontName %@",name);
[super init];
_screenFont = p_screenFont;
fontName = [name copy];
memcpy(matrix, fmatrix, sizeof(matrix));
if (_screenFont)
{
/* Round up; makes the text more legible. */
matrix[0] = ceil(matrix[0]);
if (matrix[3] < 0.0)
matrix[3] = floor(matrix[3]);
else
matrix[3] = ceil(matrix[3]);
}
if (![self setupAttributes])
{
RELEASE(self);
return nil;
}
return self;
}
- (void) dealloc
{
RELEASE(_faceInfo);
if (_scaled)
{
cairo_scaled_font_destroy(_scaled);
}
free(_cachedSizes);
free(_cachedGlyphs);
[super dealloc];
}
- (BOOL) glyphIsEncoded: (NSGlyph)glyph
{
/* subclass should override */
return YES;
}
static
BOOL _cairo_extents_for_NSGlyph(cairo_scaled_font_t *scaled_font, NSGlyph glyph,
cairo_text_extents_t *ctext)
{
unichar ustr[2];
char str[4];
unsigned char *b;
unsigned int size = 4;
int length = 1;
ustr[0] = glyph;
ustr[1] = 0;
b = (unsigned char *)str;
if (!GSFromUnicode(&b, &size, ustr, length,
NSUTF8StringEncoding, NULL, GSUniTerminate))
{
NSLog(@"Conversion failed for %@",
[NSString stringWithCharacters: ustr length: length]);
return NO;
}
cairo_scaled_font_text_extents(scaled_font, str, ctext);
return cairo_scaled_font_status(scaled_font) == CAIRO_STATUS_SUCCESS;
}
- (NSSize) advancementForGlyph: (NSGlyph)glyph
{
cairo_text_extents_t ctext;
int entry;
entry = glyph % _cacheSize;
if (_cachedGlyphs[entry] == glyph)
{
return _cachedSizes[entry];
}
if (_cairo_extents_for_NSGlyph(_scaled, glyph, &ctext))
{
_cachedGlyphs[entry] = glyph;
_cachedSizes[entry] = NSMakeSize(ctext.x_advance, ctext.y_advance);
return _cachedSizes[entry];
}
return NSZeroSize;
}
- (NSRect) boundingRectForGlyph: (NSGlyph)glyph
{
cairo_text_extents_t ctext;
if (_cairo_extents_for_NSGlyph(_scaled, glyph, &ctext))
{
return NSMakeRect(ctext.x_bearing, ctext.y_bearing,
ctext.width, ctext.height);
}
return NSZeroRect;
}
- (float) widthOfString: (NSString *)string
{
cairo_text_extents_t ctext;
cairo_scaled_font_text_extents(_scaled, [string UTF8String], &ctext);
if (cairo_scaled_font_status(_scaled) == CAIRO_STATUS_SUCCESS)
{
return ctext.width;
}
return 0.0;
}
-(NSGlyph) glyphWithName: (NSString *) glyphName
{
/* subclass should override */
/* terrible! FIXME */
NSGlyph g = [glyphName cString][0];
return g;
}
/* need cairo to export its cairo_path_t first */
- (void) appendBezierPathWithGlyphs: (NSGlyph *)glyphs
count: (int)count
toBezierPath: (NSBezierPath *)path
{
/* TODO LATER
cairo_t *ct;
int i;
NSPoint start = [path currentPoint];
cairo_glyph_t *cairo_glyphs;
cairo_glyphs = malloc(sizeof(cairo_glyph_t) * count);
ct = cairo_create();
cairo_destroy(ct);
*/
}
- (void) drawGlyphs: (const NSGlyph*)glyphs
length: (int)length
on: (cairo_t*)ct
{
cairo_matrix_t font_matrix;
unichar ustr[length+1];
char str[3*length+1];
unsigned char *b;
int i;
unsigned int size = 3*length+1;
for (i = 0; i < length; i++)
{
ustr[i] = glyphs[i];
}
ustr[length] = 0;
b = (unsigned char *)str;
if (!GSFromUnicode(&b, &size, ustr, length,
NSUTF8StringEncoding, NULL, GSUniTerminate))
{
NSLog(@"Conversion failed for %@",
[NSString stringWithCharacters: ustr length: length]);
return;
}
cairo_matrix_init(&font_matrix, matrix[0], matrix[1], matrix[2],
-matrix[3], matrix[4], matrix[5]);
cairo_set_font_matrix(ct, &font_matrix);
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while setting font matrix: %s",
cairo_status_to_string(cairo_status(ct)));
return;
}
cairo_set_font_face(ct, [_faceInfo fontFace]);
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error while setting font face: %s",
cairo_status_to_string(cairo_status(ct)));
return;
}
// FIXME: Need some adjustment here
cairo_rel_move_to(ct, 0.0, -5.0);
cairo_show_text(ct, str);
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
{
NSLog(@"Error drawing string: '%s' for string %s",
cairo_status_to_string(cairo_status(ct)), str);
}
}
@end
|