Commit 8661ebaa authored by Dmitry Antipov's avatar Dmitry Antipov
Browse files

One more minor cleanup of font subsystem.

* font.h (struct font_driver): Convert text_extents to
return void because returned value is never actually used.
* macfont.c (macfont_text_extents):
* w32font.c (w32font_text_extents):
* xftfont.c (xftfont_text_extents): Adjust to return void
and assume that 'metrics' argument is always non-NULL.
* ftfont.c (ftfont_text_extents):
* xfont.c (xfont_text_extents): Likewise.  Avoid redundant memset.
parent 90c5c877
2014-08-25 Dmitry Antipov <dmantipov@yandex.ru>
One more minor cleanup of font subsystem.
* font.h (struct font_driver): Convert text_extents to
return void because returned value is never actually used.
* macfont.c (macfont_text_extents):
* w32font.c (w32font_text_extents):
* xftfont.c (xftfont_text_extents): Adjust to return void
and assume that 'metrics' argument is always non-NULL.
* ftfont.c (ftfont_text_extents):
* xfont.c (xfont_text_extents): Likewise. Avoid redundant memset.
2014-08-25 Paul Eggert <eggert@cs.ucla.edu> 2014-08-25 Paul Eggert <eggert@cs.ucla.edu>
   
Minor cleanups of str_collate fix (Bug#18051). Minor cleanups of str_collate fix (Bug#18051).
......
...@@ -570,9 +570,9 @@ struct font_driver ...@@ -570,9 +570,9 @@ struct font_driver
/* Compute the total metrics of the NGLYPHS glyphs specified by /* Compute the total metrics of the NGLYPHS glyphs specified by
the font FONT and the sequence of glyph codes CODE, and store the the font FONT and the sequence of glyph codes CODE, and store the
result in METRICS. */ result in METRICS. */
int (*text_extents) (struct font *font, void (*text_extents) (struct font *font,
unsigned *code, int nglyphs, unsigned *code, int nglyphs,
struct font_metrics *metrics); struct font_metrics *metrics);
#ifdef HAVE_WINDOW_SYSTEM #ifdef HAVE_WINDOW_SYSTEM
......
...@@ -499,8 +499,8 @@ static Lisp_Object ftfont_open (struct frame *, Lisp_Object, int); ...@@ -499,8 +499,8 @@ static Lisp_Object ftfont_open (struct frame *, Lisp_Object, int);
static void ftfont_close (struct font *); static void ftfont_close (struct font *);
static int ftfont_has_char (Lisp_Object, int); static int ftfont_has_char (Lisp_Object, int);
static unsigned ftfont_encode_char (struct font *, int); static unsigned ftfont_encode_char (struct font *, int);
static int ftfont_text_extents (struct font *, unsigned *, int, static void ftfont_text_extents (struct font *, unsigned *, int,
struct font_metrics *); struct font_metrics *);
static int ftfont_get_bitmap (struct font *, unsigned, static int ftfont_get_bitmap (struct font *, unsigned,
struct font_bitmap *, int); struct font_bitmap *, int);
static int ftfont_anchor_point (struct font *, unsigned, int, static int ftfont_anchor_point (struct font *, unsigned, int,
...@@ -1371,19 +1371,18 @@ ftfont_encode_char (struct font *font, int c) ...@@ -1371,19 +1371,18 @@ ftfont_encode_char (struct font *font, int c)
return (code > 0 ? code : FONT_INVALID_CODE); return (code > 0 ? code : FONT_INVALID_CODE);
} }
static int static void
ftfont_text_extents (struct font *font, unsigned int *code, int nglyphs, struct font_metrics *metrics) ftfont_text_extents (struct font *font, unsigned int *code,
int nglyphs, struct font_metrics *metrics)
{ {
struct ftfont_info *ftfont_info = (struct ftfont_info *) font; struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
FT_Face ft_face = ftfont_info->ft_size->face; FT_Face ft_face = ftfont_info->ft_size->face;
int width = 0; int i, width = 0;
int i;
bool first; bool first;
if (ftfont_info->ft_size != ft_face->size) if (ftfont_info->ft_size != ft_face->size)
FT_Activate_Size (ftfont_info->ft_size); FT_Activate_Size (ftfont_info->ft_size);
if (metrics)
memset (metrics, 0, sizeof (struct font_metrics));
for (i = 0, first = 1; i < nglyphs; i++) for (i = 0, first = 1; i < nglyphs; i++)
{ {
if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0) if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
...@@ -1392,39 +1391,28 @@ ftfont_text_extents (struct font *font, unsigned int *code, int nglyphs, struct ...@@ -1392,39 +1391,28 @@ ftfont_text_extents (struct font *font, unsigned int *code, int nglyphs, struct
if (first) if (first)
{ {
if (metrics) metrics->lbearing = m->horiBearingX >> 6;
{ metrics->rbearing = (m->horiBearingX + m->width) >> 6;
metrics->lbearing = m->horiBearingX >> 6; metrics->ascent = m->horiBearingY >> 6;
metrics->rbearing = (m->horiBearingX + m->width) >> 6; metrics->descent = (m->height - m->horiBearingY) >> 6;
metrics->ascent = m->horiBearingY >> 6;
metrics->descent = (m->height - m->horiBearingY) >> 6;
}
first = 0; first = 0;
} }
if (metrics) if (metrics->lbearing > width + (m->horiBearingX >> 6))
{ metrics->lbearing = width + (m->horiBearingX >> 6);
if (metrics->lbearing > width + (m->horiBearingX >> 6)) if (metrics->rbearing
metrics->lbearing = width + (m->horiBearingX >> 6); < width + ((m->horiBearingX + m->width) >> 6))
if (metrics->rbearing metrics->rbearing
< width + ((m->horiBearingX + m->width) >> 6)) = width + ((m->horiBearingX + m->width) >> 6);
metrics->rbearing if (metrics->ascent < (m->horiBearingY >> 6))
= width + ((m->horiBearingX + m->width) >> 6); metrics->ascent = m->horiBearingY >> 6;
if (metrics->ascent < (m->horiBearingY >> 6)) if (metrics->descent > ((m->height - m->horiBearingY) >> 6))
metrics->ascent = m->horiBearingY >> 6; metrics->descent = (m->height - m->horiBearingY) >> 6;
if (metrics->descent > ((m->height - m->horiBearingY) >> 6))
metrics->descent = (m->height - m->horiBearingY) >> 6;
}
width += m->horiAdvance >> 6; width += m->horiAdvance >> 6;
} }
else else
{ width += font->space_width;
width += font->space_width;
}
} }
if (metrics) metrics->width = width;
metrics->width = width;
return width;
} }
static int static int
......
...@@ -1553,8 +1553,8 @@ static CGGlyph macfont_get_glyph_for_cid (struct font *font, ...@@ -1553,8 +1553,8 @@ static CGGlyph macfont_get_glyph_for_cid (struct font *font,
static void macfont_close (struct font *); static void macfont_close (struct font *);
static int macfont_has_char (Lisp_Object, int); static int macfont_has_char (Lisp_Object, int);
static unsigned macfont_encode_char (struct font *, int); static unsigned macfont_encode_char (struct font *, int);
static int macfont_text_extents (struct font *, unsigned int *, int, static void macfont_text_extents (struct font *, unsigned int *, int,
struct font_metrics *); struct font_metrics *);
static int macfont_draw (struct glyph_string *, int, int, int, int, bool); static int macfont_draw (struct glyph_string *, int, int, int, int, bool);
static Lisp_Object macfont_shape (Lisp_Object); static Lisp_Object macfont_shape (Lisp_Object);
static int macfont_variation_glyphs (struct font *, int c, static int macfont_variation_glyphs (struct font *, int c,
...@@ -2653,9 +2653,9 @@ So we use CTFontDescriptorCreateMatchingFontDescriptor (no ...@@ -2653,9 +2653,9 @@ So we use CTFontDescriptorCreateMatchingFontDescriptor (no
return glyph != kCGFontIndexInvalid ? glyph : FONT_INVALID_CODE; return glyph != kCGFontIndexInvalid ? glyph : FONT_INVALID_CODE;
} }
static int static void
macfont_text_extents (struct font *font, unsigned int *code, int nglyphs, macfont_text_extents (struct font *font, unsigned int *code,
struct font_metrics *metrics) int nglyphs, struct font_metrics *metrics)
{ {
int width, i; int width, i;
...@@ -2664,28 +2664,21 @@ So we use CTFontDescriptorCreateMatchingFontDescriptor (no ...@@ -2664,28 +2664,21 @@ So we use CTFontDescriptorCreateMatchingFontDescriptor (no
for (i = 1; i < nglyphs; i++) for (i = 1; i < nglyphs; i++)
{ {
struct font_metrics m; struct font_metrics m;
int w = macfont_glyph_extents (font, code[i], metrics ? &m : NULL, int w = macfont_glyph_extents (font, code[i], &m, NULL, 0);
NULL, 0);
if (width + m.lbearing < metrics->lbearing)
if (metrics) metrics->lbearing = width + m.lbearing;
{ if (width + m.rbearing > metrics->rbearing)
if (width + m.lbearing < metrics->lbearing) metrics->rbearing = width + m.rbearing;
metrics->lbearing = width + m.lbearing; if (m.ascent > metrics->ascent)
if (width + m.rbearing > metrics->rbearing) metrics->ascent = m.ascent;
metrics->rbearing = width + m.rbearing; if (m.descent > metrics->descent)
if (m.ascent > metrics->ascent) metrics->descent = m.descent;
metrics->ascent = m.ascent;
if (m.descent > metrics->descent)
metrics->descent = m.descent;
}
width += w; width += w;
} }
unblock_input (); unblock_input ();
if (metrics) metrics->width = width;
metrics->width = width;
return width;
} }
static int static int
......
...@@ -627,8 +627,8 @@ static Lisp_Object nsfont_open (struct frame *f, Lisp_Object font_entity, ...@@ -627,8 +627,8 @@ static Lisp_Object nsfont_open (struct frame *f, Lisp_Object font_entity,
static void nsfont_close (struct font *font); static void nsfont_close (struct font *font);
static int nsfont_has_char (Lisp_Object entity, int c); static int nsfont_has_char (Lisp_Object entity, int c);
static unsigned int nsfont_encode_char (struct font *font, int c); static unsigned int nsfont_encode_char (struct font *font, int c);
static int nsfont_text_extents (struct font *font, unsigned int *code, static void nsfont_text_extents (struct font *font, unsigned int *code,
int nglyphs, struct font_metrics *metrics); int nglyphs, struct font_metrics *metrics);
static int nsfont_draw (struct glyph_string *s, int from, int to, int x, int y, static int nsfont_draw (struct glyph_string *s, int from, int to, int x, int y,
bool with_background); bool with_background);
...@@ -988,9 +988,9 @@ when setting family in ns_spec_to_descriptor(). */ ...@@ -988,9 +988,9 @@ when setting family in ns_spec_to_descriptor(). */
/* Perform the size computation of glyphs of FONT and fill in members /* Perform the size computation of glyphs of FONT and fill in members
of METRICS. The glyphs are specified by their glyph codes in of METRICS. The glyphs are specified by their glyph codes in
CODE (length NGLYPHS). */ CODE (length NGLYPHS). */
static int static void
nsfont_text_extents (struct font *font, unsigned int *code, int nglyphs, nsfont_text_extents (struct font *font, unsigned int *code,
struct font_metrics *metrics) int nglyphs, struct font_metrics *metrics)
{ {
struct nsfont_info *font_info = (struct nsfont_info *)font; struct nsfont_info *font_info = (struct nsfont_info *)font;
struct font_metrics *pcm; struct font_metrics *pcm;
...@@ -1000,7 +1000,7 @@ when setting family in ns_spec_to_descriptor(). */ ...@@ -1000,7 +1000,7 @@ when setting family in ns_spec_to_descriptor(). */
memset (metrics, 0, sizeof (struct font_metrics)); memset (metrics, 0, sizeof (struct font_metrics));
for (i =0; i<nglyphs; i++) for (i = 0; i < nglyphs; i++)
{ {
/* get metrics for this glyph, filling cache if need be */ /* get metrics for this glyph, filling cache if need be */
/* TODO: get metrics for whole string from an NSLayoutManager /* TODO: get metrics for whole string from an NSLayoutManager
...@@ -1024,8 +1024,6 @@ when setting family in ns_spec_to_descriptor(). */ ...@@ -1024,8 +1024,6 @@ when setting family in ns_spec_to_descriptor(). */
} }
metrics->width = totalWidth; metrics->width = totalWidth;
return totalWidth; /* not specified in doc, but xfont.c does it */
} }
......
...@@ -473,7 +473,7 @@ w32font_encode_char (struct font *font, int c) ...@@ -473,7 +473,7 @@ w32font_encode_char (struct font *font, int c)
of METRICS. The glyphs are specified by their glyph codes in of METRICS. The glyphs are specified by their glyph codes in
CODE (length NGLYPHS). Apparently metrics can be NULL, in this CODE (length NGLYPHS). Apparently metrics can be NULL, in this
case just return the overall width. */ case just return the overall width. */
int void
w32font_text_extents (struct font *font, unsigned *code, w32font_text_extents (struct font *font, unsigned *code,
int nglyphs, struct font_metrics *metrics) int nglyphs, struct font_metrics *metrics)
{ {
...@@ -487,84 +487,80 @@ w32font_text_extents (struct font *font, unsigned *code, ...@@ -487,84 +487,80 @@ w32font_text_extents (struct font *font, unsigned *code,
struct w32font_info *w32_font = (struct w32font_info *) font; struct w32font_info *w32_font = (struct w32font_info *) font;
if (metrics) memset (metrics, 0, sizeof (struct font_metrics));
{ metrics->ascent = font->ascent;
memset (metrics, 0, sizeof (struct font_metrics)); metrics->descent = font->descent;
metrics->ascent = font->ascent;
metrics->descent = font->descent;
for (i = 0; i < nglyphs; i++)
{
struct w32_metric_cache *char_metric;
int block = *(code + i) / CACHE_BLOCKSIZE;
int pos_in_block = *(code + i) % CACHE_BLOCKSIZE;
if (block >= w32_font->n_cache_blocks) for (i = 0; i < nglyphs; i++)
{ {
if (!w32_font->cached_metrics) struct w32_metric_cache *char_metric;
w32_font->cached_metrics int block = *(code + i) / CACHE_BLOCKSIZE;
= xmalloc ((block + 1) int pos_in_block = *(code + i) % CACHE_BLOCKSIZE;
* sizeof (struct w32_metric_cache *));
else
w32_font->cached_metrics
= xrealloc (w32_font->cached_metrics,
(block + 1)
* sizeof (struct w32_metric_cache *));
memset (w32_font->cached_metrics + w32_font->n_cache_blocks, 0,
((block + 1 - w32_font->n_cache_blocks)
* sizeof (struct w32_metric_cache *)));
w32_font->n_cache_blocks = block + 1;
}
if (!w32_font->cached_metrics[block]) if (block >= w32_font->n_cache_blocks)
{ {
w32_font->cached_metrics[block] if (!w32_font->cached_metrics)
= xzalloc (CACHE_BLOCKSIZE * sizeof (struct w32_metric_cache)); w32_font->cached_metrics
} = xmalloc ((block + 1)
* sizeof (struct w32_metric_cache *));
else
w32_font->cached_metrics
= xrealloc (w32_font->cached_metrics,
(block + 1)
* sizeof (struct w32_metric_cache *));
memset (w32_font->cached_metrics + w32_font->n_cache_blocks, 0,
((block + 1 - w32_font->n_cache_blocks)
* sizeof (struct w32_metric_cache *)));
w32_font->n_cache_blocks = block + 1;
}
char_metric = w32_font->cached_metrics[block] + pos_in_block; if (!w32_font->cached_metrics[block])
{
w32_font->cached_metrics[block]
= xzalloc (CACHE_BLOCKSIZE * sizeof (struct w32_metric_cache));
}
if (char_metric->status == W32METRIC_NO_ATTEMPT) char_metric = w32_font->cached_metrics[block] + pos_in_block;
{
if (dc == NULL)
{
/* TODO: Frames can come and go, and their fonts
outlive them. So we can't cache the frame in the
font structure. Use selected_frame until the API
is updated to pass in a frame. */
f = XFRAME (selected_frame);
dc = get_frame_dc (f);
old_font = SelectObject (dc, w32_font->hfont);
}
compute_metrics (dc, w32_font, *(code + i), char_metric);
}
if (char_metric->status == W32METRIC_SUCCESS) if (char_metric->status == W32METRIC_NO_ATTEMPT)
{
if (dc == NULL)
{ {
metrics->lbearing = min (metrics->lbearing, /* TODO: Frames can come and go, and their fonts
metrics->width + char_metric->lbearing); outlive them. So we can't cache the frame in the
metrics->rbearing = max (metrics->rbearing, font structure. Use selected_frame until the API
metrics->width + char_metric->rbearing); is updated to pass in a frame. */
metrics->width += char_metric->width; f = XFRAME (selected_frame);
dc = get_frame_dc (f);
old_font = SelectObject (dc, w32_font->hfont);
} }
else compute_metrics (dc, w32_font, *(code + i), char_metric);
/* If we couldn't get metrics for a char,
use alternative method. */
break;
} }
/* If we got through everything, return. */
if (i == nglyphs)
{
if (dc != NULL)
{
/* Restore state and release DC. */
SelectObject (dc, old_font);
release_frame_dc (f, dc);
}
return metrics->width; if (char_metric->status == W32METRIC_SUCCESS)
} {
metrics->lbearing = min (metrics->lbearing,
metrics->width + char_metric->lbearing);
metrics->rbearing = max (metrics->rbearing,
metrics->width + char_metric->rbearing);
metrics->width += char_metric->width;
}
else
/* If we couldn't get metrics for a char,
use alternative method. */
break;
}
/* If we got through everything, return. */
if (i == nglyphs)
{
if (dc != NULL)
{
/* Restore state and release DC. */
SelectObject (dc, old_font);
release_frame_dc (f, dc);
}
return;
} }
/* For non-truetype fonts, GetGlyphOutlineW is not supported, so /* For non-truetype fonts, GetGlyphOutlineW is not supported, so
...@@ -620,18 +616,13 @@ w32font_text_extents (struct font *font, unsigned *code, ...@@ -620,18 +616,13 @@ w32font_text_extents (struct font *font, unsigned *code,
} }
/* Give our best estimate of the metrics, based on what we know. */ /* Give our best estimate of the metrics, based on what we know. */
if (metrics) metrics->width = total_width - w32_font->metrics.tmOverhang;
{ metrics->lbearing = 0;
metrics->width = total_width - w32_font->metrics.tmOverhang; metrics->rbearing = total_width;
metrics->lbearing = 0;
metrics->rbearing = total_width;
}
/* Restore state and release DC. */ /* Restore state and release DC. */
SelectObject (dc, old_font); SelectObject (dc, old_font);
release_frame_dc (f, dc); release_frame_dc (f, dc);
return total_width;
} }
/* w32 implementation of draw for font backend. /* w32 implementation of draw for font backend.
......
...@@ -74,8 +74,8 @@ int w32font_open_internal (struct frame *f, Lisp_Object font_entity, ...@@ -74,8 +74,8 @@ int w32font_open_internal (struct frame *f, Lisp_Object font_entity,
int pixel_size, Lisp_Object font_object); int pixel_size, Lisp_Object font_object);
void w32font_close (struct font *font); void w32font_close (struct font *font);
int w32font_has_char (Lisp_Object entity, int c); int w32font_has_char (Lisp_Object entity, int c);
int w32font_text_extents (struct font *font, unsigned *code, int nglyphs, void w32font_text_extents (struct font *font, unsigned *code, int nglyphs,
struct font_metrics *metrics); struct font_metrics *metrics);
int w32font_draw (struct glyph_string *s, int from, int to, int w32font_draw (struct glyph_string *s, int from, int to,
int x, int y, bool with_background); int x, int y, bool with_background);
......
...@@ -591,8 +591,8 @@ uniscribe_encode_char (struct font *font, int c) ...@@ -591,8 +591,8 @@ uniscribe_encode_char (struct font *font, int c)
Lisp_Object uniscribe_get_cache (Lisp_Object frame); Lisp_Object uniscribe_get_cache (Lisp_Object frame);
void uniscribe_free_entity (Lisp_Object font_entity); void uniscribe_free_entity (Lisp_Object font_entity);
int uniscribe_has_char (Lisp_Object entity, int c); int uniscribe_has_char (Lisp_Object entity, int c);
int uniscribe_text_extents (struct font *font, unsigned *code, void uniscribe_text_extents (struct font *font, unsigned *code,
int nglyphs, struct font_metrics *metrics); int nglyphs, struct font_metrics *metrics);
int uniscribe_draw (struct glyph_string *s, int from, int to, int uniscribe_draw (struct glyph_string *s, int from, int to,
int x, int y, int with_background); int x, int y, int with_background);
......
...@@ -124,8 +124,8 @@ static void xfont_close (struct font *); ...@@ -124,8 +124,8 @@ static void xfont_close (struct font *);
static void xfont_prepare_face (struct frame *, struct face *); static void xfont_prepare_face (struct frame *, struct face *);
static int xfont_has_char (Lisp_Object, int); static int xfont_has_char (Lisp_Object, int);
static unsigned xfont_encode_char (struct font *, int); static unsigned xfont_encode_char (struct font *, int);
static int xfont_text_extents (struct font *, unsigned *, int, static void xfont_text_extents (struct font *, unsigned *, int,
struct font_metrics *); struct font_metrics *);
static int xfont_draw (struct glyph_string *, int, int, int, int, bool); static int xfont_draw (struct glyph_string *, int, int, int, int, bool);
static int xfont_check (struct frame *, struct font *); static int xfont_check (struct frame *, struct font *);
...@@ -975,15 +975,14 @@ xfont_encode_char (struct font *font, int c) ...@@ -975,15 +975,14 @@ xfont_encode_char (struct font *font, int c)
return (xfont_get_pcm (xfont, &char2b) ? code : FONT_INVALID_CODE); return (xfont_get_pcm (xfont, &char2b) ? code : FONT_INVALID_CODE);
} }
static int static void
xfont_text_extents (struct font *font, unsigned int *code, int nglyphs, struct font_metrics *metrics) xfont_text_extents (struct font *font, unsigned int *code,
int nglyphs, struct font_metrics *metrics)
{ {
XFontStruct *xfont = ((struct xfont_info *) font)->xfont; XFontStruct *xfont = ((struct xfont_info *) font)->xfont;
int width = 0; int i, width = 0;
int i, first; bool first;
if (metrics)
memset (metrics, 0, sizeof (struct font_metrics));
for (i = 0, first = 1; i < nglyphs; i++) for (i = 0, first = 1; i < nglyphs; i++)
{ {
XChar2b char2b; XChar2b char2b;
...@@ -997,34 +996,27 @@ xfont_text_extents (struct font *font, unsigned int *code, int nglyphs, struct f ...@@ -997,34 +996,27 @@ xfont_text_extents (struct font *font, unsigned int *code, int nglyphs, struct f
continue; continue;
if (first) if (first)
{ {
if (metrics) metrics->lbearing = pcm->lbearing;
{ metrics->rbearing = pcm->rbearing;
metrics->lbearing = pcm->lbearing; metrics->ascent = pcm->ascent;
metrics->rbearing = pcm->rbearing; metrics->descent = pcm->descent;
metrics->ascent = pcm->ascent;
metrics->descent = pcm->descent;
}
first = 0; first = 0;
} }
else else
{ {
if (metrics) if (metrics->lbearing > width + pcm->lbearing)
{ metrics->lbearing = width + pcm->lbearing;
if (metrics->lbearing > width + pcm->lbearing) if (metrics->rbearing < width + pcm->rbearing)
metrics->lbearing = width + pcm->lbearing; metrics->rbearing = width + pcm->rbearing;
if (metrics->rbearing < width + pcm->rbearing) if (metrics->ascent < pcm->ascent)
metrics->rbearing = width + pcm->rbearing; metrics->ascent = pcm->ascent;
if (metrics->ascent < pcm->ascent) if (metrics->descent < pcm->descent)
metrics->ascent = pcm->ascent; metrics->descent = pcm->descent;
if (metrics->descent < pcm->descent)
metrics->descent = pcm->descent;
}
} }
width += pcm->width; width += pcm->width;
} }
if (metrics)
metrics->width = width; metrics->width = width;
return width;
} }
static int static int
......