4
* Copyright (C) 2003 Free Software Foundation, Inc.
6
* Written by Banlu Kemiyatorn <object at gmail dot com>
7
* Base on original code of Alex Malmberg
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Library General Public
10
* License as published by the Free Software Foundation; either
11
* version 2 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Library General Public License for more details.
18
* You should have received a copy of the GNU Library General Public
19
* License along with this library; if not, write to the Free
20
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
* Boston, MA 02111 USA.
4
Copyright (C) 2003 Free Software Foundation, Inc.
7
Written by Banlu Kemiyatorn <object at gmail dot com>
8
Base on original code of Alex Malmberg
10
This file is part of GNUstep.
12
This library is free software; you can redistribute it and/or
13
modify it under the terms of the GNU Lesser General Public
14
License as published by the Free Software Foundation; either
15
version 2 of the License, or (at your option) any later version.
17
This library is distributed in the hope that it will be useful,
18
but WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
Lesser General Public License for more details.
22
You should have received a copy of the GNU Lesser General Public
23
License along with this library; see the file COPYING.LIB.
24
If not, see <http://www.gnu.org/licenses/> or write to the
25
Free Software Foundation, 51 Franklin Street, Fifth Floor,
26
Boston, MA 02110-1301, USA.
24
29
#include "GNUstepBase/Unicode.h"
25
30
#include <AppKit/NSAffineTransform.h>
31
#include <AppKit/NSBezierPath.h>
26
32
#include "cairo/CairoFontInfo.h"
27
33
#include "cairo/CairoFontEnumerator.h"
45
_cachedSizes = malloc(sizeof(NSSize) * size);
46
memset(_cachedSizes, 0, sizeof(NSSize) * size);
47
_cachedGlyphs = malloc(sizeof(unsigned int) * size);
48
memset(_cachedGlyphs, 0, sizeof(unsigned int) * size);
45
objc_free(_cachedSizes);
49
objc_free(_cachedGlyphs);
51
_cachedSizes = objc_malloc(sizeof(NSSize) * size);
54
memset(_cachedSizes, 0, sizeof(NSSize) * size);
56
_cachedGlyphs = objc_malloc(sizeof(unsigned int) * size);
59
memset(_cachedGlyphs, 0, sizeof(unsigned int) * size);
51
63
- (BOOL) setupAttributes
83
92
* xHeight, pix_width, pix_height
85
94
cairo_matrix_init(&font_matrix, matrix[0], matrix[1], matrix[2],
86
matrix[3], matrix[4], matrix[5]);
95
matrix[3], matrix[4], matrix[5]);
87
96
cairo_matrix_init_identity(&ctm);
88
98
// FIXME: Should get default font options from somewhere
89
99
options = cairo_font_options_create();
90
100
face = [_faceInfo fontFace];
105
116
xHeight = ascender * 0.6;
106
117
lineHeight = font_extents.height;
107
118
maximumAdvancement = NSMakeSize(font_extents.max_x_advance,
108
font_extents.max_y_advance);
119
font_extents.max_y_advance);
109
120
fontBBox = NSMakeRect(0, descender,
110
maximumAdvancement.width, ascender - descender);
121
maximumAdvancement.width, ascender - descender);
115
126
- (id) initWithFontName: (NSString *)name
116
matrix: (const float *)fmatrix
117
screenFont: (BOOL)p_screenFont
127
matrix: (const float *)fmatrix
128
screenFont: (BOOL)p_screenFont
119
//NSLog(@"initWithFontName %@",name);
122
134
_screenFont = p_screenFont;
123
135
fontName = [name copy];
162
176
- (BOOL) glyphIsEncoded: (NSGlyph)glyph
164
/* subclass should override */
178
/* FIXME: There is no proper way to determine with the toy font API,
179
whether a glyph is supported or not. We will just ignore ligatures
180
and report all other glyph as existing.
181
return !NSEqualSizes([self advancementForGlyph: glyph], NSZeroSize);
183
if ((glyph >= 0xFB00) && (glyph <= 0xFB05))
169
190
BOOL _cairo_extents_for_NSGlyph(cairo_scaled_font_t *scaled_font, NSGlyph glyph,
170
cairo_text_extents_t *ctext)
191
cairo_text_extents_t *ctext)
194
215
- (NSSize) advancementForGlyph: (NSGlyph)glyph
196
217
cairo_text_extents_t ctext;
199
entry = glyph % _cacheSize;
201
if (_cachedGlyphs[entry] == glyph)
203
return _cachedSizes[entry];
221
int entry = glyph % _cacheSize;
223
if (_cachedGlyphs[entry] == glyph)
225
return _cachedSizes[entry];
228
if (_cairo_extents_for_NSGlyph(_scaled, glyph, &ctext))
230
_cachedGlyphs[entry] = glyph;
231
_cachedSizes[entry] = NSMakeSize(ctext.x_advance, ctext.y_advance);
233
return _cachedSizes[entry];
206
if (_cairo_extents_for_NSGlyph(_scaled, glyph, &ctext))
208
_cachedGlyphs[entry] = glyph;
209
_cachedSizes[entry] = NSMakeSize(ctext.x_advance, ctext.y_advance);
211
return _cachedSizes[entry];
238
if (_cairo_extents_for_NSGlyph(_scaled, glyph, &ctext))
240
return NSMakeSize(ctext.x_advance, ctext.y_advance);
214
244
return NSZeroSize;
252
/* need cairo to export its cairo_path_t first */
253
287
- (void) appendBezierPathWithGlyphs: (NSGlyph *)glyphs
255
toBezierPath: (NSBezierPath *)path
289
toBezierPath: (NSBezierPath *)path
260
NSPoint start = [path currentPoint];
261
cairo_glyph_t *cairo_glyphs;
263
cairo_glyphs = malloc(sizeof(cairo_glyph_t) * count);
291
cairo_format_t format = CAIRO_FORMAT_ARGB32;
292
cairo_surface_t *isurface;
296
unsigned char *cdata;
298
unichar ustr[length+1];
299
char str[3*length+1];
301
unsigned int size = 3*length+1;
302
cairo_status_t status;
303
cairo_matrix_t font_matrix;
305
for (i = 0; i < length; i++)
311
b = (unsigned char *)str;
312
if (!GSFromUnicode(&b, &size, ustr, length,
313
NSUTF8StringEncoding, NULL, GSUniTerminate))
315
NSLog(@"Conversion failed for %@",
316
[NSString stringWithCharacters: ustr length: length]);
320
cdata = objc_malloc(sizeof(char) * 4 * ix * iy);
323
NSLog(@"Could not allocate drawing space for glyphs");
327
isurface = cairo_image_surface_create_for_data(cdata, format, ix, iy, 4*ix);
328
status = cairo_surface_status(isurface);
329
if (status != CAIRO_STATUS_SUCCESS)
331
NSLog(@"Error while creating surface: %s",
332
cairo_status_to_string(status));
333
cairo_surface_destroy(isurface);
338
ct = cairo_create(isurface);
339
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
341
NSLog(@"Error while creating context: %s",
342
cairo_status_to_string(cairo_status(ct)));
344
cairo_surface_destroy(isurface);
350
cairo_matrix_init(&font_matrix, matrix[0], matrix[1], matrix[2],
351
-matrix[3], matrix[4], matrix[5]);
352
cairo_set_font_matrix(ct, &font_matrix);
353
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
355
NSLog(@"Error while setting font matrix: %s",
356
cairo_status_to_string(cairo_status(ct)));
358
cairo_surface_destroy(isurface);
363
cairo_set_font_face(ct, [_faceInfo fontFace]);
364
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
366
NSLog(@"Error while setting font face: %s",
367
cairo_status_to_string(cairo_status(ct)));
369
cairo_surface_destroy(isurface);
374
if ([path elementCount] > 0)
378
p = [path currentPoint];
379
cairo_move_to(ct, floorf(p.x), floorf(p.y));
382
cairo_text_path(ct, str);
383
if (cairo_status(ct) == CAIRO_STATUS_SUCCESS)
386
cairo_path_data_t *data;
388
cpath = cairo_copy_path(ct);
390
for (i = 0; i < cpath->num_data; i += cpath->data[i].header.length)
392
data = &cpath->data[i];
393
switch (data->header.type)
395
case CAIRO_PATH_MOVE_TO:
396
[path moveToPoint: NSMakePoint(data[1].point.x, data[1].point.y)];
398
case CAIRO_PATH_LINE_TO:
399
[path lineToPoint: NSMakePoint(data[1].point.x, data[1].point.y)];
401
case CAIRO_PATH_CURVE_TO:
402
[path curveToPoint: NSMakePoint(data[3].point.x, data[3].point.y)
403
controlPoint1: NSMakePoint(data[1].point.x, data[1].point.y)
404
controlPoint2: NSMakePoint(data[2].point.x, data[2].point.y)];
406
case CAIRO_PATH_CLOSE_PATH:
411
cairo_path_destroy(cpath);
414
cairo_surface_destroy(isurface);
271
418
- (void) drawGlyphs: (const NSGlyph*)glyphs
275
422
cairo_matrix_t font_matrix;
276
423
unichar ustr[length+1];
288
435
b = (unsigned char *)str;
289
436
if (!GSFromUnicode(&b, &size, ustr, length,
290
NSUTF8StringEncoding, NULL, GSUniTerminate))
437
NSUTF8StringEncoding, NULL, GSUniTerminate))
292
439
NSLog(@"Conversion failed for %@",
293
[NSString stringWithCharacters: ustr length: length]);
440
[NSString stringWithCharacters: ustr length: length]);
297
444
cairo_matrix_init(&font_matrix, matrix[0], matrix[1], matrix[2],
298
matrix[3], matrix[4], matrix[5]);
445
matrix[3], matrix[4], matrix[5]);
299
446
cairo_set_font_matrix(ct, &font_matrix);
300
447
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
302
NSLog(@"Error while setting font matrix: %s",
303
cairo_status_to_string(cairo_status(ct)));
449
NSLog(@"Error while setting font matrix: %s",
450
cairo_status_to_string(cairo_status(ct)));
306
454
cairo_set_font_face(ct, [_faceInfo fontFace]);
307
455
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
309
NSLog(@"Error while setting font face: %s",
310
cairo_status_to_string(cairo_status(ct)));
457
NSLog(@"Error while setting font face: %s",
458
cairo_status_to_string(cairo_status(ct)));
314
462
cairo_show_text(ct, str);
315
463
if (cairo_status(ct) != CAIRO_STATUS_SUCCESS)
317
NSLog(@"Error drawing string: '%s' for string %s",
318
cairo_status_to_string(cairo_status(ct)), str);
465
NSLog(@"Error drawing string: '%s' for string %s",
466
cairo_status_to_string(cairo_status(ct)), str);