Pango, Cairo, Scale, and Ink Rectangles



The width/height ratio of a single glyph changes with both the scale of the cairo context or the dpi of the pango context. I don't understand this behaviour. Here are some example numbers mapping dpi -> ink device width, ink device height (unrounded) for the glyph "8"

096: 09.0, 12.0
192: 17.0, 23.0
048: 05.0, 05.0

It seems unreasonable that with a DPI of 48, the glyph "8" would be drawn as wide as it is tall. What follows is example code.

Using Pango resolution (DPI):
    cairo_context.identity_matrix()
    pango_context = PangoCairo.create_context(cairo_context)
    PangoCairo.context_set_resolution(pango_context, dpi)
    pango_layout = Pango.Layout(pango_context)
    pango_layout.set_text("8",1)
    extents = pango_layout.get_extents()[0]
    ex = extents.x / Pango.SCALE
    ey = extents.y / Pango.SCALE
    ew = extents.w / Pango.SCALE
    eh = extents.h / Pango.SCALE
    print(dpi, ex, ey, ew, eh)

Use Cairo transformation matrix:
    cairo_context.identity_matrix()
    cairo_context.scale(scale, scale)
    pango_layout = PangoCairo.create_layout(cairo_context)
    pango_layout.set_text("8", 1)
    extents = pango_layout.get_extents()[0]
    ex = extents.x / Pango.SCALE
    ey = extents.y / Pango.SCALE
    ew = extents.w / Pango.SCALE
    eh = extents.h / Pango.SCALE
    m = cairo_context.get_matrix()
    print(m, ex, ey, ew, eh)

For the record, I need to draw a single letter maximized and centred within a given cell such that none of the input glyphs at constant scale factor overlap the cell boundaries. This seems impossible if the glyph width/height ratios vary with scale/dpi.

Sincerely,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]