Re: PDF: Font size is rounded

Adrian Johnson <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <[email protected]>
On 02/04/16 00:23, Kouhei Sutou wrote:
> Hi,
> 
> In <[email protected]>
>   "Re: [cairo] PDF: Font size is rounded" on Fri, 1 Apr 2016 22:22:48 +1030,
>   Adrian Johnson <[email protected]> wrote:
> 
>>> The specified font size of scaled TrueType font is rounded
>>> when PDF surface is used. Is it the expected behavior?
>>
>> The PDF surface should turn off hinting metrics when generating PDF
>> output. Have you tried checking the metrics returned by
>> cairo_scaled_font_glyph_extents()?. Use cairo_set_font_options() to turn
>> off hinting metrics when doing this.
> 
> I didn't tried it. Does the following code implement your
> suggestion?
> 
>     cairo_select_font_face(cr,
>                            "DejaVu Sans Mono",
>                            CAIRO_FONT_SLANT_NORMAL,
>                            CAIRO_FONT_WEIGHT_NORMAL);
>     cairo_set_font_size(cr, font_size);
>     {
>       cairo_font_options_t *options;
>       options = cairo_font_options_create();
>       cairo_get_font_options(cr, options);
>       cairo_font_options_set_hint_metrics(options, CAIRO_HINT_METRICS_OFF);
>       cairo_set_font_options(cr, options);
>     }
>     {
>       cairo_scaled_font_t *font;
>       cairo_text_extents_t extents;
>       font = cairo_get_scaled_font(cr);
>       cairo_scaled_font_text_extents(font, "abcdefghijklm", &extents);
>       printf("-------------\n"
>              "font size: %10f\n"
>              "x_bearing: %10f\n"
>              "y_bearing: %10f\n"
>              "    width: %10f\n"
>              "   height: %10f\n"
>              "x_advance: %10f\n"
>              "y_advance: %10f\n",
>              font_size,
>              extents.x_bearing,
>              extents.y_bearing,
>              extents.width,
>              extents.height,
>              extents.x_advance,
>              extents.y_advance);
>     }
> 
> The test program with the above change outputs the
> following:
> 
> -------------
> font size:   8.000000
> x_bearing:   0.515625
> y_bearing:  -6.125000
>     width:  61.718750
>    height:   7.843750
> x_advance:  62.613281
> y_advance:   0.000000
> -------------
> font size:   8.125000
> x_bearing:   0.515625
> y_bearing:  -6.125000
>     width:  61.718750
>    height:   7.843750
> x_advance:  62.613281
> y_advance:   0.000000
> -------------
> font size:   8.250000
> x_bearing:   0.515625
> y_bearing:  -6.125000
>     width:  61.718750
>    height:   7.843750
> x_advance:  62.613281
> y_advance:   0.000000
> (snip)
> 
> All of 8.0 font size, 8.125 font size and 8.25 font size
> reports the same width and height.

I put some printfs in _cairo_ft_scaled_glyph_init (in cairo-ft-font.c).
It appears that FreeType is returning hinted metrics. I'm guessing that
to get unhinted metrics of TrueType fonts we need to set the scale to
the EM size like we do for win32 fonts.

I'm attaching a patch to do this. It fixes your test case. I don't know
if this patch is the correct way to fix the problem. I am not familiar
with the FreeType font backend.


> It seems that
> 
>   cairo_font_options_set_hint_metrics(options, CAIRO_HINT_METRICS_OFF);
> 
> doesn't effect the result...
> 
> Full program: http://pub.cozmixng.org/~kou/tmp/truetype-size.c
> 
> 
> Thanks,
> --
> kou
>

-- 
cairo mailing list
[email protected]
https://lists.cairographics.org/mailman/listinfo/cairo
0001-FT-scale-to-em-size-to-get-unhinted-metrics.patch (text/x-patch, 1.2 KB)
From 4f02bbf3f69175e563eb12b227e44e2e00568811 Mon Sep 17 00:00:00 2001
From: Adrian Johnson <[email protected]>
Date: Sat, 2 Apr 2016 14:02:23 +1030
Subject: [PATCH] FT: scale to em size to get unhinted metrics

---
 src/cairo-ft-font.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/cairo-ft-font.c b/src/cairo-ft-font.c
index a0c7beb..6c7311c 100644
--- a/src/cairo-ft-font.c
+++ b/src/cairo-ft-font.c
@@ -2229,8 +2229,16 @@ _cairo_ft_scaled_glyph_init (void			*abstract_font,
     if (!face)
 	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 
-    status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
-				                &scaled_font->base.scale);
+    if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF ||
+	face->units_per_EM == 0) {
+	status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
+						    &scaled_font->base.scale);
+    } else {
+	cairo_matrix_t em_size;
+	cairo_matrix_init_scale (&em_size, face->units_per_EM, face->units_per_EM);
+	status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
+						    &em_size);
+    }
     if (unlikely (status))
 	goto FAIL;
 
-- 
2.1.4
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.