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

how could I draw vertical text in cairo ? #246

Open
duwangthefirst opened this issue Dec 30, 2021 · 2 comments
Open

how could I draw vertical text in cairo ? #246

duwangthefirst opened this issue Dec 30, 2021 · 2 comments

Comments

@duwangthefirst
Copy link

I've implemented horizontal text rendering, the code is as below: (but how could I draw text vertically(not rotating for 90 or 270 degree))

截屏2021-12-30 上午10 55 17

import cairo
from math import pi


def draw_horizontal_text(context, text, x0, y0, font_size, font_color=(0, 0, 0, 1), font_family="Source Han Mono SC"):
    context.move_to(x0, y0)
    context.select_font_face(font_family, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
    context.set_font_size(font_size)
    x_bearing, y_bearing, width, height, x_advance, y_advance = context.text_extents(text)
    context.move_to(x0-x_bearing, y0-(height+y_bearing))
    context.set_source_rgba(*font_color)
    context.show_text(text)


def demo():
    width = 2000
    height = 2000
    surface = cairo.PDFSurface("result.pdf", width, height)
    context = cairo.Context(surface)
    context.scale(width, height)
    draw_horizontal_text(context, "你吃饭了没啊aAg", 0.1, 0.9, 0.1)


if __name__ == '__main__':
    demo()
@naveen521kk
Copy link
Contributor

I don't think it's possible to do with plain pycairo, instead, you should use something like Pango for doing it. Also, I don't think there exists any binding for Pango with Python that can be used with pycairo. I think something like cairocffi + pangocairocffi can be used, I could see an example here.

hth

@ldo
Copy link

ldo commented Mar 22, 2022

This is really a job for a text-shaping library like HarfBuzz.

Relevant example use of my Python binding for HarfBuzz here, output from that example here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants