Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Measuring text width per given height and string #3

Open
5 tasks
HYUNSEONG-KIM opened this issue Sep 27, 2022 · 0 comments
Open
5 tasks

Measuring text width per given height and string #3

HYUNSEONG-KIM opened this issue Sep 27, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request sub sub problem or enhancement of the other main issue

Comments

@HYUNSEONG-KIM
Copy link
Contributor

HYUNSEONG-KIM commented Sep 27, 2022

Text width measuring

In writing texts in template pdf file, its precise dimension is required before applying to real pdf page.
Overlapping objects on result pdf could not be desired result for all situations.
Now its routine is only required by Note utils but further routines would require this feature.

Previous works

Using Pillow's text string method is popular on stackoverflow and the other sites.

  • Help me

I implmented prototype version and tested it but pts and point conversion has an issue.

from PIL import Image, ImageDraw, ImageFont
def get_text_dim(string, font_file: str, size):
    im_width = len(string) * size
    im_height = size
    bg_color = (0, 0, 0)

    # Generate string as image
    im = Image.new("RGB", (im_width, im_height), bg_color)
    draw = ImageDraw.Draw(im)
    font_PIL = ImageFont.truetype(font_file, size)
    draw.text((0, 0), string, font=font_PIL, fill=(255, 255, 255))
    bbox = im.getbbox()  # left, upper, right, and lower pixel coordinate

    width = bbox[2] + bbox[0]
    height = bbox[3] + int(bbox[1] / 2)
    return width, height

Others

Done

  • Basic font routines are implemented:
    • Load system fonts
    • points <-> pixel conversion for general setting.

Remains

  • Fonts routine wapper class implement compatible with fonttools library.
  • Measuring dpi for reportlab pdfgen routine.
  • Write width measuring based on reportlab routines.
@HYUNSEONG-KIM HYUNSEONG-KIM added enhancement New feature or request sub sub problem or enhancement of the other main issue labels Sep 27, 2022
@HYUNSEONG-KIM HYUNSEONG-KIM self-assigned this Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request sub sub problem or enhancement of the other main issue
Projects
None yet
Development

No branches or pull requests

1 participant