skia-python icon indicating copy to clipboard operation
skia-python copied to clipboard

canvas.drawTextBlob() not working sometimes with TTF fonts

Open RugnarLodbrok opened this issue 3 years ago • 0 comments

Describe the bug canvas.drawTextBlob() sometimes doesn't draw anything when using ttf fonts

To Reproduce Steps to reproduce the behavior:

import os
import zipfile

import numpy as np
import requests
import skia
from PIL import Image


def get_font_file():
    directory = 'fonts'
    font_file = 'SourceSerifPro-Regular.ttf'
    path = os.path.join(directory, font_file)
    if not os.path.exists(path):
        url = 'https://fonts.google.com/download?family=Source%20Serif%20Pro'
        r = requests.get(url)
        zip_file_name = 'fonts.zip'
        with open(zip_file_name, 'wb') as f:
            f.write(r.content)

        with zipfile.ZipFile(zip_file_name, 'r') as zip_ref:
            zip_ref.extractall(directory)
    return path


def get_font(is_custom=False):
    if not is_custom:
        return skia.Font(None, 36)
    else:
        file = get_font_file()
        with open(file, 'rb') as f:
            typeface = skia.Typeface.MakeFromData(f.read())
        return skia.Font(typeface, size=36, scaleX=1.0, skewX=0.0)


def main(is_custom=False, decrement_size=False):
    paint = skia.Paint(
        AntiAlias=True,
        Color=skia.Color(255, 0, 0, 255),
        Style=skia.Paint.kFill_Style,
    )

    font = get_font(is_custom)
    blob = skia.TextBlob('Simple text 123', font=font)

    w, h = 539, 113
    if decrement_size:
        h -= 1
    xy = 10, 50
    frame = np.ones((h, w, 4), dtype=np.uint8) * 255
    with skia.Surface(frame) as canvas:
        canvas.drawTextBlob(blob, *xy, paint)
    Image.fromarray(frame).save('skia!.png')



if __name__ == '__main__':
    # main(False, False)  # works
    # main(False, True)  # works
    # main(True, False)  # works
    main(True, True)  # doesnt't work!

Expected behavior Text should be rendered

Desktop (please complete the following information):

  • OS: Docker ubuntu:18.04
  • Python: 3.9.6
  • skia-python version: 87.2

RugnarLodbrok avatar Aug 11 '21 10:08 RugnarLodbrok