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.
This commit is contained in:
parent
032a42c88d
commit
179dad8ed1
2 changed files with 55 additions and 14 deletions
64
src/nsfont.m
64
src/nsfont.m
|
@ -236,27 +236,62 @@ static void ns_glyph_metrics (struct nsfont_info *font_info,
|
|||
}
|
||||
|
||||
|
||||
/* Utility: get width of a char c in screen font sfont */
|
||||
/* Utility: get width of a char c in screen font SFONT */
|
||||
static float
|
||||
ns_char_width (NSFont *sfont, int c)
|
||||
{
|
||||
float w;
|
||||
NSString *cstr = [NSString stringWithFormat: @"%c", c];
|
||||
float w = -1.0;
|
||||
NSString *cstr = [NSString stringWithFormat: @"%c", c];
|
||||
|
||||
#ifdef NS_IMPL_COCOA
|
||||
NSGlyph glyph = [sfont glyphWithName: cstr];
|
||||
if (glyph)
|
||||
{
|
||||
float w = [sfont advancementForGlyph: glyph].width;
|
||||
if (w >= 1.5)
|
||||
return w;
|
||||
}
|
||||
NSGlyph glyph = [sfont glyphWithName: cstr];
|
||||
if (glyph)
|
||||
w = [sfont advancementForGlyph: glyph].width;
|
||||
#endif
|
||||
|
||||
if (w < 0.0)
|
||||
{
|
||||
NSDictionary *attrsDictionary =
|
||||
[NSDictionary dictionaryWithObject: sfont forKey: NSFontAttributeName];
|
||||
w = [cstr sizeWithAttributes: attrsDictionary].width;
|
||||
}
|
||||
return max (w, 2.0);
|
||||
|
||||
return max (w, 1.0);
|
||||
}
|
||||
|
||||
/* Return average width over ASCII printable characters for SFONT. */
|
||||
|
||||
static NSString *ascii_printable;
|
||||
|
||||
static int
|
||||
ns_ascii_average_width (NSFont *sfont)
|
||||
{
|
||||
float w = -1.0;
|
||||
|
||||
if (!ascii_printable)
|
||||
{
|
||||
char chars[95];
|
||||
int ch;
|
||||
for (ch = 0; ch < 95; ch++)
|
||||
chars[ch] = ' ' + ch;
|
||||
|
||||
ascii_printable = [NSString initWithFormat: @"%s", chars];
|
||||
}
|
||||
|
||||
#ifdef NS_IMPL_COCOA
|
||||
NSGlyph glyph = [sfont glyphWithName: ascii_printable];
|
||||
if (glyph)
|
||||
w = [sfont advancementForGlyph: glyph].width;
|
||||
#endif
|
||||
|
||||
if (w < 0.0)
|
||||
{
|
||||
NSDictionary *attrsDictionary =
|
||||
[NSDictionary dictionaryWithObject: sfont forKey: NSFontAttributeName];
|
||||
w = [ascii_printable sizeWithAttributes: attrsDictionary].width;
|
||||
}
|
||||
|
||||
return lrint (w / 95.0);
|
||||
}
|
||||
|
||||
|
||||
|
@ -885,10 +920,11 @@ when setting family in ns_spec_to_descriptor(). */
|
|||
/* set up metrics portion of font struct */
|
||||
font->ascent = lrint([sfont ascender]);
|
||||
font->descent = -lrint(floor(adjusted_descender));
|
||||
font->min_width = ns_char_width(sfont, '|');
|
||||
font->space_width = lrint (ns_char_width (sfont, ' '));
|
||||
font->average_width = lrint (font_info->width);
|
||||
font->max_width = lrint (font_info->max_bounds.width);
|
||||
font->min_width = font->space_width; /* Approximate. */
|
||||
font->average_width = ns_ascii_average_width (sfont);
|
||||
|
||||
font->height = lrint (font_info->height);
|
||||
font->underline_position = lrint (font_info->underpos);
|
||||
font->underline_thickness = lrint (font_info->underwidth);
|
||||
|
@ -1492,4 +1528,6 @@ - (void)setIntAttribute: (NSInteger)attributeTag value: (NSInteger)val
|
|||
DEFSYM (Qmedium, "medium");
|
||||
DEFVAR_LISP ("ns-reg-to-script", Vns_reg_to_script,
|
||||
doc: /* Internal use: maps font registry to Unicode script. */);
|
||||
|
||||
ascii_printable = NULL;
|
||||
}
|
||||
|
|
|
@ -450,7 +450,10 @@ struct nsfont_info
|
|||
struct font font;
|
||||
|
||||
char *name; /* PostScript name, uniquely identifies on NS systems */
|
||||
float width; /* this and following metrics stored as float rather than int */
|
||||
|
||||
/* The following metrics are stored as float rather than int. */
|
||||
|
||||
float width; /* Maximum advance for the font. */
|
||||
float height;
|
||||
float underpos;
|
||||
float underwidth;
|
||||
|
|
Loading…
Add table
Reference in a new issue