[cairo] font size change only in large steps

Alois Treindl alois at astro.ch
Sat Sep 29 08:57:35 UTC 2018

sorry, sent html message again which this list dos not accept.
I send a second time.
I added font size changes to the example, and print extents with metric 
on and off.
The problem which I am addressing appears: font size changes only in steps.

9.0 pt on  Size: 49x7    9.0 pt off  Size: 45.7764x7
9.1 pt on  Size: 49x7    9.1 pt off  Size: 45.7764x7
9.2 pt on  Size: 49x7    9.2 pt off  Size: 45.7764x7
9.3 pt on  Size: 49x7    9.3 pt off  Size: 45.7764x7
9.4 pt on  Size: 49x7    9.4 pt off  Size: 45.7764x7
9.5 pt on  Size: 51x7    9.5 pt off  Size: 50.5293x7
9.6 pt on  Size: 51x7    9.6 pt off  Size: 50.5293x7
9.7 pt on  Size: 51x7    9.7 pt off  Size: 50.5293x7
9.8 pt on  Size: 51x7    9.8 pt off  Size: 50.5293x7
9.9 pt on  Size: 51x7    9.9 pt off  Size: 50.5293x7
10.0 pt on  Size: 51x7    10.0 pt off  Size: 50.5293x7

I ran also the same example but with PDF surface, and get as output:
9.0 pt on  Size: 45.4951x6.8125    9.0 pt off  Size: 45.4951x6.8125
9.1 pt on  Size: 45.4951x6.8125    9.1 pt off  Size: 45.4951x6.8125
9.2 pt on  Size: 45.4951x6.8125    9.2 pt off  Size: 45.4951x6.8125
9.3 pt on  Size: 45.4951x6.8125    9.3 pt off  Size: 45.4951x6.8125
9.4 pt on  Size: 45.4951x6.8125    9.4 pt off  Size: 45.4951x6.8125
9.5 pt on  Size: 50.5605x7.5625    9.5 pt off  Size: 50.5605x7.5625
9.6 pt on  Size: 50.5605x7.5625    9.6 pt off  Size: 50.5605x7.5625
9.7 pt on  Size: 50.5605x7.5625    9.7 pt off  Size: 50.5605x7.5625
9.8 pt on  Size: 50.5605x7.5625    9.8 pt off  Size: 50.5605x7.5625
9.9 pt on  Size: 50.5605x7.5625    9.9 pt off  Size: 50.5605x7.5625
10.0 pt on  Size: 50.5605x7.5625    10.0 pt off  Size: 50.5605x7.5625

with PDF, metrics on or off makes no difference. But the font size 
changes are in steps.

On 29.09.18 08:12, Uli Schlachter wrote:
> Hi,
> On 28.09.2018 14:10, Alois Treindl wrote:
>> I noticed that cairo does not allow me to set a precise font size, like 9.4 pt
>> or 9.7 pt.
>> What I get is exactly the same font size, for a range of font sizes.
> Try disabling metrics hinting via CAIRO_HINT_METRICS_OFF. See the
> attached example program. Per the docs, this metrics hinting quantizises
> font metrics so that they are integer values in device space, i.e. does
> exactly what you are trying to get rid of.
> You might also want to do cairo_font_options_set_hint_style(opt,
> CAIRO_HINT_STYLE_NONE), depending on, well, if you want the font
> outlines to be hinted or not.
> Cheers,
> Uli

-------------- next part --------------
#include <cairo.h>
#include <stdio.h>

static void measure(cairo_t *cr, char *s)
	cairo_text_extents_t exts;
	cairo_text_extents(cr, "Some text", &exts);

	printf("%s Size: %gx%g", s, exts.width, exts.height);

int main()
  int i; 
  char t[80];
  double pt;
  cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1000, 1000);
  cairo_t *cr = cairo_create(s);
  cairo_font_options_t *opt, *opt0 = cairo_font_options_create();
  cairo_get_font_options(cr, opt0);
  opt = cairo_font_options_copy(opt0);
  cairo_font_options_set_hint_metrics(opt, CAIRO_HINT_METRICS_OFF);
  for (i = 90; i <= 100; i++) {
    pt = i * 0.1;
    cairo_set_font_size(cr, pt);
    cairo_set_font_options(cr, opt0);
    sprintf(t, "%.1lf pt on ", pt);
    measure(cr, t);
    cairo_set_font_options(cr, opt);
    sprintf(t, "\t%.1lf pt off ", pt);
    measure(cr, t);

	return 0;

More information about the cairo mailing list