diff options
| author | Chong Yidong | 2012-08-16 14:40:57 +0800 |
|---|---|---|
| committer | Chong Yidong | 2012-08-16 14:40:57 +0800 |
| commit | 179dad8ed18677af1251493751e3c9de4c80ee43 (patch) | |
| tree | 921010ec2d8d06e358b2dca7e93f270ae1ab973e /src | |
| parent | 032a42c88d421ee434e783e29bf2cde3d6a81e7b (diff) | |
| download | emacs-179dad8ed18677af1251493751e3c9de4c80ee43.tar.gz emacs-179dad8ed18677af1251493751e3c9de4c80ee43.zip | |
Fix average font width calculation on NS.
* src/nsfont.m (nsfont_open): Similar to the Xft backend, set
min_width to space_width and average_width to the average over
printable ASCII characters.
(ns_char_width): Code cleanup.
(ns_ascii_average_width): New utility function.
Diffstat (limited to 'src')
| -rw-r--r-- | src/nsfont.m | 64 | ||||
| -rw-r--r-- | src/nsterm.h | 5 |
2 files changed, 55 insertions, 14 deletions
diff --git a/src/nsfont.m b/src/nsfont.m index 6f9294e082a..fdb6e0c33c1 100644 --- a/src/nsfont.m +++ b/src/nsfont.m | |||
| @@ -236,27 +236,62 @@ ns_fallback_entity (void) | |||
| 236 | } | 236 | } |
| 237 | 237 | ||
| 238 | 238 | ||
| 239 | /* Utility: get width of a char c in screen font sfont */ | 239 | /* Utility: get width of a char c in screen font SFONT */ |
| 240 | static float | 240 | static float |
| 241 | ns_char_width (NSFont *sfont, int c) | 241 | ns_char_width (NSFont *sfont, int c) |
| 242 | { | 242 | { |
| 243 | float w; | 243 | float w = -1.0; |
| 244 | NSString *cstr = [NSString stringWithFormat: @"%c", c]; | 244 | NSString *cstr = [NSString stringWithFormat: @"%c", c]; |
| 245 | |||
| 245 | #ifdef NS_IMPL_COCOA | 246 | #ifdef NS_IMPL_COCOA |
| 246 | NSGlyph glyph = [sfont glyphWithName: cstr]; | 247 | NSGlyph glyph = [sfont glyphWithName: cstr]; |
| 247 | if (glyph) | 248 | if (glyph) |
| 248 | { | 249 | w = [sfont advancementForGlyph: glyph].width; |
| 249 | float w = [sfont advancementForGlyph: glyph].width; | ||
| 250 | if (w >= 1.5) | ||
| 251 | return w; | ||
| 252 | } | ||
| 253 | #endif | 250 | #endif |
| 251 | |||
| 252 | if (w < 0.0) | ||
| 254 | { | 253 | { |
| 255 | NSDictionary *attrsDictionary = | 254 | NSDictionary *attrsDictionary = |
| 256 | [NSDictionary dictionaryWithObject: sfont forKey: NSFontAttributeName]; | 255 | [NSDictionary dictionaryWithObject: sfont forKey: NSFontAttributeName]; |
| 257 | w = [cstr sizeWithAttributes: attrsDictionary].width; | 256 | w = [cstr sizeWithAttributes: attrsDictionary].width; |
| 258 | } | 257 | } |
| 259 | return max (w, 2.0); | 258 | |
| 259 | return max (w, 1.0); | ||
| 260 | } | ||
| 261 | |||
| 262 | /* Return average width over ASCII printable characters for SFONT. */ | ||
| 263 | |||
| 264 | static NSString *ascii_printable; | ||
| 265 | |||
| 266 | static int | ||
| 267 | ns_ascii_average_width (NSFont *sfont) | ||
| 268 | { | ||
| 269 | float w = -1.0; | ||
| 270 | |||
| 271 | if (!ascii_printable) | ||
| 272 | { | ||
| 273 | char chars[95]; | ||
| 274 | int ch; | ||
| 275 | for (ch = 0; ch < 95; ch++) | ||
| 276 | chars[ch] = ' ' + ch; | ||
| 277 | |||
| 278 | ascii_printable = [NSString initWithFormat: @"%s", chars]; | ||
| 279 | } | ||
| 280 | |||
| 281 | #ifdef NS_IMPL_COCOA | ||
| 282 | NSGlyph glyph = [sfont glyphWithName: ascii_printable]; | ||
| 283 | if (glyph) | ||
| 284 | w = [sfont advancementForGlyph: glyph].width; | ||
| 285 | #endif | ||
| 286 | |||
| 287 | if (w < 0.0) | ||
| 288 | { | ||
| 289 | NSDictionary *attrsDictionary = | ||
| 290 | [NSDictionary dictionaryWithObject: sfont forKey: NSFontAttributeName]; | ||
| 291 | w = [ascii_printable sizeWithAttributes: attrsDictionary].width; | ||
| 292 | } | ||
| 293 | |||
| 294 | return lrint (w / 95.0); | ||
| 260 | } | 295 | } |
| 261 | 296 | ||
| 262 | 297 | ||
| @@ -885,10 +920,11 @@ nsfont_open (FRAME_PTR f, Lisp_Object font_entity, int pixel_size) | |||
| 885 | /* set up metrics portion of font struct */ | 920 | /* set up metrics portion of font struct */ |
| 886 | font->ascent = lrint([sfont ascender]); | 921 | font->ascent = lrint([sfont ascender]); |
| 887 | font->descent = -lrint(floor(adjusted_descender)); | 922 | font->descent = -lrint(floor(adjusted_descender)); |
| 888 | font->min_width = ns_char_width(sfont, '|'); | ||
| 889 | font->space_width = lrint (ns_char_width (sfont, ' ')); | 923 | font->space_width = lrint (ns_char_width (sfont, ' ')); |
| 890 | font->average_width = lrint (font_info->width); | ||
| 891 | font->max_width = lrint (font_info->max_bounds.width); | 924 | font->max_width = lrint (font_info->max_bounds.width); |
| 925 | font->min_width = font->space_width; /* Approximate. */ | ||
| 926 | font->average_width = ns_ascii_average_width (sfont); | ||
| 927 | |||
| 892 | font->height = lrint (font_info->height); | 928 | font->height = lrint (font_info->height); |
| 893 | font->underline_position = lrint (font_info->underpos); | 929 | font->underline_position = lrint (font_info->underpos); |
| 894 | font->underline_thickness = lrint (font_info->underwidth); | 930 | font->underline_thickness = lrint (font_info->underwidth); |
| @@ -1492,4 +1528,6 @@ syms_of_nsfont (void) | |||
| 1492 | DEFSYM (Qmedium, "medium"); | 1528 | DEFSYM (Qmedium, "medium"); |
| 1493 | DEFVAR_LISP ("ns-reg-to-script", Vns_reg_to_script, | 1529 | DEFVAR_LISP ("ns-reg-to-script", Vns_reg_to_script, |
| 1494 | doc: /* Internal use: maps font registry to Unicode script. */); | 1530 | doc: /* Internal use: maps font registry to Unicode script. */); |
| 1531 | |||
| 1532 | ascii_printable = NULL; | ||
| 1495 | } | 1533 | } |
diff --git a/src/nsterm.h b/src/nsterm.h index 94984b3d35e..54f4a4ec89e 100644 --- a/src/nsterm.h +++ b/src/nsterm.h | |||
| @@ -450,7 +450,10 @@ struct nsfont_info | |||
| 450 | struct font font; | 450 | struct font font; |
| 451 | 451 | ||
| 452 | char *name; /* PostScript name, uniquely identifies on NS systems */ | 452 | char *name; /* PostScript name, uniquely identifies on NS systems */ |
| 453 | float width; /* this and following metrics stored as float rather than int */ | 453 | |
| 454 | /* The following metrics are stored as float rather than int. */ | ||
| 455 | |||
| 456 | float width; /* Maximum advance for the font. */ | ||
| 454 | float height; | 457 | float height; |
| 455 | float underpos; | 458 | float underpos; |
| 456 | float underwidth; | 459 | float underwidth; |