Skip to content

Clipping when rendering Khmer text using Pillow and libraqm #3235

Closed

Description

@bluman88

What did you do?

I used Pillow (5.3.0) to draw Khmer text onto an image using libraqm (0.5.0) for complex layout support (layout_engine=ImageFont.LAYOUT_RAQM).

What did you expect to happen?

I expected the word "លោកុប្បត្តិ" to be rendered like this (output from MS Word):
khmer-word

What actually happened?

The left offset of the text was too large and resulted in clipping on the right-hand side of the bounding box (shown in red):
khmer-pillow

Depending on how many glyphs of the word I chose to render, the offset became larger.

What versions of Pillow and Python are you using?

I am using the following versions on Ubuntu 18.04:

Code to reproduce clipping effect:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

img = Image.new("RGB", (512, 512), "white")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("Battambang-Regular.ttf", 60, layout_engine=ImageFont.LAYOUT_RAQM)
text = "លោកុប្បត្តិ"
draw.text((0, 0), text, (0, 0, 0), font=font)
img.save('out.png')

Activity

  1. hugovk commented on Sep 18, 2018

    @hugovk
    Member

    From the raqm issue:

    Raqm does not do the rendering, just the layout. This clipping is likely a Pillow bug and should be reported and fixed there.

    HOST-Oman/libraqm#87 (comment)

  2. bluman88 commented on Sep 18, 2018

    @bluman88
    Author

    Thanks for linking the raqm issue, Hugo. This is still a problem. Does anyone know what is causing the issue?

  3. bluman88 commented on Jan 3, 2019

    @bluman88
    Author

    Confirmed that this is still a problem with Pillow 5.5.0 and libraqm 0.5.0.

    Does anyone know where to start looking?

  4. radarhere commented on Mar 31, 2019

    @radarhere
    Member

    From #597, I find that the widths are different if Unicode numbers are used instead of characters. Neither is correct though.

    from PIL import Image, ImageDraw, ImageFont
    img = Image.new('L', (300, 130))
    d = ImageDraw.Draw(img)
    font = ImageFont.truetype("KhmerOS.ttf", 30)
    d.text((50, 30), u'\u179f\u17d2\u1793\u17b6', fill=(255), font=font)
    d.text((150, 30), u'ស្នាក់', fill=(255), font=font)
    img.save('test.tif')
  5. radarhere commented on Apr 11, 2019

    @radarhere
    Member

    I've created PR #3787 to resolve this.

  6. bluman88 commented on Apr 11, 2019

    @bluman88
    Author

    Perfect. Thank you. PR #3787 fixes the issue I have been having. Here is the original text with correct layout using the new code:

    sample-out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions