Skip to content

Draw font text line gap seems incorrect #6469

@TakWolf

Description

@TakWolf

Python: 3.9
Pillow: 9.2.0

I use this font: ark-pixel-font-12px-otf v2022.07.05
https://github.com/TakWolf/ark-pixel-font/releases

The font has zero line_gap:
font_size = 12
upm = 1200
line_height = 1200 (ascent - descent)
line_gap = 0

(xml gen use https://github.com/fonttools/fonttools ttx tools)

  <hhea>
    <tableVersion value="0x00010000"/>
    <ascent value="1000"/>
    <descent value="-200"/>
    <lineGap value="0"/>      <----- This
    <advanceWidthMax value="1200"/>
    <minLeftSideBearing value="0"/>
    <minRightSideBearing value="0"/>
    <xMaxExtent value="1200"/>
    <caretSlopeRise value="1"/>
    <caretSlopeRun value="0"/>
    <caretOffset value="0"/>
    <reserved0 value="0"/>
    <reserved1 value="0"/>
    <reserved2 value="0"/>
    <reserved3 value="0"/>
    <metricDataFormat value="0"/>
    <numberOfHMetrics value="9437"/>
  </hhea>

and

  <OS_2>
    ............
    <sTypoAscender value="1000"/>
    <sTypoDescender value="-200"/>
    <sTypoLineGap value="0"/>     <----- and this
    <usWinAscent value="1000"/>
    <usWinDescent value="200"/>
    <ulCodePageRange1 value="00000000 00000000 00000000 00000000"/>
    <ulCodePageRange2 value="00000000 00000000 00000000 00000000"/>
    <sxHeight value="600"/>
    <sCapHeight value="800"/>
    <usDefaultChar value="0"/>
    <usBreakChar value="32"/>
    <usMaxContext value="0"/>
  </OS_2>

In pillow:

from PIL import ImageFont, Image, ImageDraw

if __name__ == '__main__':
    font = ImageFont.truetype('build/outputs/ark-pixel-12px-latin.otf', 12)
    image = Image.new('RGBA', (400, 400), (255, 255, 255))
    draw = ImageDraw.Draw(image)
    text = '方舟像\n素字体'

    text_size = draw.textsize(text, font=font, spacing=0)
    print(text_size)

    text_bbox = draw.textbbox((0, 0), text, font=font, spacing=0)
    print(text_bbox)

    draw.text((0, 0), text, fill=(0, 0, 0), font=font, spacing=0)
    image.save('build/outputs/test.png')

image

Actually there is -2 line_gap

result:

(36, 20)    -> expect (36, 24)
(0, 1, 36, 22) -> expect (_, _, 36, 24)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions