From 78ba818f54e593c444a9a775434f80d736c771a5 Mon Sep 17 00:00:00 2001 From: Fred Kiefer Date: Tue, 27 Feb 2007 16:35:31 +0000 Subject: [PATCH] Clean up of font attributes and better protection against _ct being null in cairo backend. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/back/trunk@24710 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 16 +++ Headers/cairo/CairoFontInfo.h | 8 +- Source/art/ftfont.m | 10 +- Source/cairo/CairoFontInfo.m | 18 ++- Source/cairo/CairoGState.m | 244 +++++++++++++++++++++++++--------- Source/winlib/WIN32FontInfo.m | 6 +- Source/xlib/GSXftFontInfo.m | 3 +- Source/xlib/XGFont.m | 3 +- 8 files changed, 219 insertions(+), 89 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6db1a2c..3f22ee4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2007-02-27 Fred Kiefer + + * Source/winlib/Win32FontInfo.m (-setupAttributes:): + Correct setting of maximumAdvancement and add xHeight. + * Source/art/ftfont.m (-initWithFontName:matrix:screenFont:): + Correct setting of maximumAdvancement and xHeight. + * Source/xlib/GSXftFontInfo.m (-setupAttributes:): + Correct setting of maximumAdvancement. + * Source/xlib/XGFont.m(-setupAttributes:): Correct setting of + maximumAdvancement. + * Headers/cairo/CairoFontInfo.h: Add new ivar lineHeight. + * Source/cairo/CairoFontInfo.m (-setupAttributes:): + Correct setting of descender and add lineHeight and xHeight. + * Source/cairo/CairoFontInfo.m (-drawGlyphs:length:on:): Remove adjustment. + * Source/cairo/CairoGState.m: Better protection against _ct being null. + 2007-02-27 Nicola Pero * Source/GNUmakefile.postamble (config.h): Use MKDIRS, not diff --git a/Headers/cairo/CairoFontInfo.h b/Headers/cairo/CairoFontInfo.h index b2ceba3..4e6ded4 100644 --- a/Headers/cairo/CairoFontInfo.h +++ b/Headers/cairo/CairoFontInfo.h @@ -31,15 +31,9 @@ { @public cairo_scaled_font_t *_scaled; - CairoFaceInfo *_faceInfo; - BOOL _screenFont; - - /* will be used in GSNFont subclass - NSMapTable *_ligatureMap; - NSMapTable *_kerningMap; - */ + float lineHeight; unsigned int _cacheSize; unsigned int *_cachedGlyphs; diff --git a/Source/art/ftfont.m b/Source/art/ftfont.m index 3a11d12..5515c0d 100644 --- a/Source/art/ftfont.m +++ b/Source/art/ftfont.m @@ -764,15 +764,13 @@ static FT_Error ft_get_face(FTC_FaceID fid, FT_Library lib, FT_Pointer data, FT_ return self; } -// xHeight = ft_size->metrics.height / 64.0; -/* TODO: these are _really_ messed up when fonts are flipped */ - /* TODO: need to look acrefully at these and make sure they are correct */ + /* TODO: these are _really_ messed up when fonts are flipped */ + /* TODO: need to look carefully at these and make sure they are correct */ ascender = fabs(((int)ft_size->metrics.ascender) / 64.0); descender = fabs(((int)ft_size->metrics.descender) / 64.0); lineHeight = (int)ft_size->metrics.height / 64.0; - xHeight = ascender * 0.5; /* TODO */ - maximumAdvancement - = NSMakeSize((ft_size->metrics.max_advance / 64.0), ascender + descender); + xHeight = (int)ft_size->metrics.y_ppem / 64.0; + maximumAdvancement = NSMakeSize((ft_size->metrics.max_advance / 64.0), 0.0); fontBBox = NSMakeRect(0, descender, maximumAdvancement.width, ascender + descender); diff --git a/Source/cairo/CairoFontInfo.m b/Source/cairo/CairoFontInfo.m index 2936e5a..b6930b4 100644 --- a/Source/cairo/CairoFontInfo.m +++ b/Source/cairo/CairoFontInfo.m @@ -99,14 +99,15 @@ 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; + ascender = font_extents.ascent; + // The FreeType documentation claims this value is already negative, but it isn't. + descender = -font_extents.descent; + xHeight = ascender * 0.6; + lineHeight = font_extents.height; maximumAdvancement = NSMakeSize(font_extents.max_x_advance, font_extents.max_y_advance); fontBBox = NSMakeRect(0, descender, - maximumAdvancement.width, ascender + descender); + maximumAdvancement.width, ascender - descender); return YES; } @@ -153,6 +154,11 @@ [super dealloc]; } +- (float) defaultLineHeightForFont +{ + return lineHeight; +} + - (BOOL) glyphIsEncoded: (NSGlyph)glyph { /* subclass should override */ @@ -305,8 +311,6 @@ BOOL _cairo_extents_for_NSGlyph(cairo_scaled_font_t *scaled_font, NSGlyph glyph, 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) { diff --git a/Source/cairo/CairoGState.m b/Source/cairo/CairoGState.m index 96052a5..3d63153 100644 --- a/Source/cairo/CairoGState.m +++ b/Source/cairo/CairoGState.m @@ -294,15 +294,21 @@ - (void) DPScurrentflat: (float *)flatness { - *flatness = cairo_get_tolerance(_ct); + if (_ct) + { + *flatness = cairo_get_tolerance(_ct); + } } - (void) DPScurrentlinecap: (int *)linecap { cairo_line_cap_t lc; - lc = cairo_get_line_cap(_ct); - *linecap = lc; + if (_ct) + { + lc = cairo_get_line_cap(_ct); + *linecap = lc; + } /* switch (lc) { @@ -326,8 +332,11 @@ { cairo_line_join_t lj; - lj = cairo_get_line_join(_ct); - *linejoin = lj; + if (_ct) + { + lj = cairo_get_line_join(_ct); + *linejoin = lj; + } /* switch (lj) { @@ -349,19 +358,33 @@ - (void) DPScurrentlinewidth: (float *)width { - *width = cairo_get_line_width(_ct); + if (_ct) + { + *width = cairo_get_line_width(_ct); + } } - (void) DPScurrentmiterlimit: (float *)limit { - *limit = cairo_get_miter_limit(_ct); + if (_ct) + { + *limit = cairo_get_miter_limit(_ct); + } } - (NSPoint) currentPoint { double dx, dy; - cairo_get_current_point(_ct, &dx, &dy); + if (_ct) + { + cairo_get_current_point(_ct, &dx, &dy); + } + else + { + dx = 0.0; + dy = 0.0; + } return NSMakePoint(dx, dy); } @@ -375,41 +398,59 @@ double *dpat; int i; - i = size; - dpat = malloc(sizeof(double) * size); - while (i) + if (_ct) { - i--; - dpat[i] = pat[i]; + i = size; + dpat = malloc(sizeof(double) * size); + while (i) + { + i--; + dpat[i] = pat[i]; + } + // FIXME: There may be a difference in concept as some dashes look wrong + cairo_set_dash(_ct, dpat, size, doffset); + free(dpat); } - // FIXME: There may be a difference in concept as some dashes look wrong - cairo_set_dash(_ct, dpat, size, doffset); - free(dpat); } - (void) DPSsetflat: (float)flatness { - cairo_set_tolerance(_ct, flatness); + if (_ct) + { + cairo_set_tolerance(_ct, flatness); + } } - (void) DPSsetlinecap: (int)linecap { - cairo_set_line_cap(_ct, (cairo_line_cap_t)linecap); + if (_ct) + { + cairo_set_line_cap(_ct, (cairo_line_cap_t)linecap); + } } - (void) DPSsetlinejoin: (int)linejoin { - cairo_set_line_join(_ct, (cairo_line_join_t)linejoin); + if (_ct) + { + cairo_set_line_join(_ct, (cairo_line_join_t)linejoin); + } } - (void) DPSsetlinewidth: (float)width { - cairo_set_line_width(_ct, width); + if (_ct) + { + cairo_set_line_width(_ct, width); + } } - (void) DPSsetmiterlimit: (float)limit { - cairo_set_miter_limit(_ct, limit); + if (_ct) + { + cairo_set_miter_limit(_ct, limit); + } } - (void) DPSsetstrokeadjust: (int)b @@ -545,12 +586,18 @@ - (void) DPSarc: (float)x : (float)y : (float)r : (float)angle1 : (float)angle2 { - cairo_arc(_ct, x, y, r, angle1 * M_PI / 180, angle2 * M_PI / 180); + if (_ct) + { + cairo_arc(_ct, x, y, r, angle1 * M_PI / 180, angle2 * M_PI / 180); + } } - (void) DPSarcn: (float)x : (float)y : (float)r : (float)angle1 : (float)angle2 { - cairo_arc_negative(_ct, x, y, r, angle1 * M_PI / 180, angle2 * M_PI / 180); + if (_ct) + { + cairo_arc_negative(_ct, x, y, r, angle1 * M_PI / 180, angle2 * M_PI / 180); + } } - (void)DPSarct: (float)x1 : (float)y1 : (float)x2 : (float)y2 : (float)r @@ -562,52 +609,76 @@ - (void) DPSclip { - cairo_clip(_ct); + if (_ct) + { + cairo_clip(_ct); + } } - (void) DPSclosepath { - cairo_close_path(_ct); + if (_ct) + { + cairo_close_path(_ct); + } } - (void) DPScurveto: (float)x1 : (float)y1 : (float)x2 : (float)y2 : (float)x3 : (float)y3 { - cairo_curve_to(_ct, x1, y1, x2, y2, x3, y3); + if (_ct) + { + cairo_curve_to(_ct, x1, y1, x2, y2, x3, y3); + } } - (void) DPSeoclip { - cairo_set_fill_rule(_ct, CAIRO_FILL_RULE_EVEN_ODD); - cairo_clip(_ct); - cairo_set_fill_rule(_ct, CAIRO_FILL_RULE_WINDING); + if (_ct) + { + cairo_set_fill_rule(_ct, CAIRO_FILL_RULE_EVEN_ODD); + cairo_clip(_ct); + cairo_set_fill_rule(_ct, CAIRO_FILL_RULE_WINDING); + } } - (void) DPSeofill { - cairo_set_fill_rule(_ct, CAIRO_FILL_RULE_EVEN_ODD); - cairo_fill(_ct); - cairo_set_fill_rule(_ct, CAIRO_FILL_RULE_WINDING); + if (_ct) + { + cairo_set_fill_rule(_ct, CAIRO_FILL_RULE_EVEN_ODD); + cairo_fill(_ct); + cairo_set_fill_rule(_ct, CAIRO_FILL_RULE_WINDING); + } } - (void) DPSfill { - cairo_fill(_ct); + if (_ct) + { + cairo_fill(_ct); + } } - (void) DPSflattenpath { cairo_path_t *cpath; - cpath = cairo_copy_path_flat(_ct); - cairo_new_path(_ct); - cairo_append_path(_ct, cpath); - cairo_path_destroy(cpath); + if (_ct) + { + cpath = cairo_copy_path_flat(_ct); + cairo_new_path(_ct); + cairo_append_path(_ct, cpath); + cairo_path_destroy(cpath); + } } - (void) DPSinitclip { - cairo_reset_clip(_ct); + if (_ct) + { + cairo_reset_clip(_ct); + } } - (void) DPSlineto: (float)x : (float)y @@ -641,6 +712,11 @@ cairo_path_data_t *data; NSBezierPath *bpath =[NSBezierPath bezierPath]; + if (!_ct) + { + return bpath; + } + cpath = cairo_copy_path(_ct); for (i=0; i < cpath->num_data; i += cpath->data[i].header.length) @@ -673,33 +749,45 @@ - (void) DPSrcurveto: (float)x1 : (float)y1 : (float)x2 : (float)y2 : (float)x3 : (float)y3 { - cairo_rel_curve_to(_ct, x1, y1, x2, y2, x3, y3); + if (_ct) + { + cairo_rel_curve_to(_ct, x1, y1, x2, y2, x3, y3); + } } - (void) DPSrectclip: (float)x : (float)y : (float)w : (float)h { - cairo_new_path(_ct); - cairo_rectangle(_ct, x, y, w, h); - cairo_clip(_ct); - cairo_new_path(_ct); + if (_ct) + { + cairo_new_path(_ct); + cairo_rectangle(_ct, x, y, w, h); + cairo_clip(_ct); + cairo_new_path(_ct); + } } - (void) DPSrectfill: (float)x : (float)y : (float)w : (float)h { - cairo_save(_ct); - cairo_new_path(_ct); - cairo_rectangle(_ct, x, y, w, h); - cairo_fill(_ct); - cairo_restore(_ct); + if (_ct) + { + cairo_save(_ct); + cairo_new_path(_ct); + cairo_rectangle(_ct, x, y, w, h); + cairo_fill(_ct); + cairo_restore(_ct); + } } - (void) DPSrectstroke: (float)x : (float)y : (float)w : (float)h { - cairo_save(_ct); - cairo_new_path(_ct); - cairo_rectangle(_ct, x, y, w, h); - cairo_stroke(_ct); - cairo_restore(_ct); + if (_ct) + { + cairo_save(_ct); + cairo_new_path(_ct); + cairo_rectangle(_ct, x, y, w, h); + cairo_stroke(_ct); + cairo_restore(_ct); + } } - (void) DPSreversepath @@ -712,17 +800,26 @@ - (void) DPSrlineto: (float)x : (float)y { - cairo_rel_line_to(_ct, x, y); + if (_ct) + { + cairo_rel_line_to(_ct, x, y); + } } - (void) DPSrmoveto: (float)x : (float)y { - cairo_rel_move_to(_ct, x, y); + if (_ct) + { + cairo_rel_move_to(_ct, x, y); + } } - (void) DPSstroke { - cairo_stroke(_ct); + if (_ct) + { + cairo_stroke(_ct); + } } - (void) GSSendBezierPath: (NSBezierPath *)bpath @@ -736,6 +833,11 @@ SEL elmsel = @selector(elementAtIndex: associatedPoints:); IMP elmidx = [bpath methodForSelector: elmsel]; + if (!_ct) + { + return; + } + cairo_new_path(_ct); n = [bpath elementCount]; @@ -786,6 +888,11 @@ NSMutableData *data; unsigned char *cdata; + if (!_ct) + { + return nil; + } + x = NSWidth(r); y = NSHeight(r); cairo_user_to_device_distance(_ct, &x, &y); @@ -934,6 +1041,11 @@ _set_op(cairo_t *ct, NSCompositingOperation op) NSLog(@"%@ DPSimage %dx%d (%p)", self, pixelsWide, pixelsHigh, cairo_get_target(_ct)); */ + if (!_ct) + { + return; + } + if (isPlanar || !([colorSpaceName isEqualToString: NSDeviceRGBColorSpace] || [colorSpaceName isEqualToString: NSCalibratedRGBColorSpace])) { @@ -1088,12 +1200,15 @@ _set_op(cairo_t *ct, NSCompositingOperation op) - (void) compositerect: (NSRect)aRect op: (NSCompositingOperation)op { - cairo_save(_ct); - _set_op(_ct, op); - cairo_rectangle(_ct, NSMinX(aRect), NSMinY(aRect), NSWidth(aRect), - NSHeight(aRect)); - cairo_fill(_ct); - cairo_restore(_ct); + if (_ct) + { + cairo_save(_ct); + _set_op(_ct, op); + cairo_rectangle(_ct, NSMinX(aRect), NSMinY(aRect), NSWidth(aRect), + NSHeight(aRect)); + cairo_fill(_ct); + cairo_restore(_ct); + } } - (void) compositeGState: (CairoGState *)source @@ -1109,6 +1224,11 @@ _set_op(cairo_t *ct, NSCompositingOperation op) NSSize size; double x, y; + if (!_ct || !source->_ct) + { + return; + } + size = [source->_surface size]; dh = size.height; diff --git a/Source/winlib/WIN32FontInfo.m b/Source/winlib/WIN32FontInfo.m index b124445..de12e00 100644 --- a/Source/winlib/WIN32FontInfo.m +++ b/Source/winlib/WIN32FontInfo.m @@ -331,15 +331,15 @@ NSLog(@"No glyph for U%d", c); ascender = metric.tmAscent; //NSLog(@"Resulted in height %d and ascent %d", metric.tmHeight, metric.tmAscent); descender = -metric.tmDescent; + /* TODO */ + xHeight = ascender * 0.5; + maximumAdvancement = NSMakeSize((float)metric.tmMaxCharWidth, 0.0); fontBBox = NSMakeRect((float)(0), (float)(0 - metric.tmAscent), (float)metric.tmMaxCharWidth, (float)metric.tmHeight); - maximumAdvancement = NSMakeSize((float)metric.tmMaxCharWidth, - (float)metric.tmHeight); - weight = win32_font_weight(metric.tmWeight); traits = 0; diff --git a/Source/xlib/GSXftFontInfo.m b/Source/xlib/GSXftFontInfo.m index 6116fcb..c96a18b 100644 --- a/Source/xlib/GSXftFontInfo.m +++ b/Source/xlib/GSXftFontInfo.m @@ -706,8 +706,7 @@ static NSArray *faFromFc(FcPattern *pat) (float)(0 - font_info->ascent), (float)(font_info->max_advance_width), (float)(font_info->ascent + font_info->descent)); - maximumAdvancement = NSMakeSize(font_info->max_advance_width, - (font_info->ascent + font_info->descent)); + maximumAdvancement = NSMakeSize(font_info->max_advance_width, 0.0); minimumAdvancement = NSMakeSize(0,0); // printf("h=%g a=%g d=%g max=(%g %g) (%g %g)+(%g %g)\n", // xHeight, ascender, descender, diff --git a/Source/xlib/XGFont.m b/Source/xlib/XGFont.m index 984dfed..29603fc 100644 --- a/Source/xlib/XGFont.m +++ b/Source/xlib/XGFont.m @@ -307,8 +307,7 @@ static BOOL XGInitAtoms(Display *dpy) (float)(0 - font_info->max_bounds.ascent), (float)(font_info->max_bounds.rbearing - font_info->min_bounds.lbearing), (float)(font_info->max_bounds.ascent + font_info->max_bounds.descent)); - maximumAdvancement = NSMakeSize(font_info->max_bounds.width, - (font_info->max_bounds.ascent +font_info->max_bounds.descent)); + maximumAdvancement = NSMakeSize(font_info->max_bounds.width, 0.0); minimumAdvancement = NSMakeSize(0,0); weight = XGWeightOfFont(xdpy, font_info); traits = XGTraitsOfFont(xdpy, font_info);