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

Getting correct text bounds #168

Open
prashant-saxena opened this issue Dec 19, 2021 · 1 comment
Open

Getting correct text bounds #168

prashant-saxena opened this issue Dec 19, 2021 · 1 comment

Comments

@prashant-saxena
Copy link

Describe the bug
I'm trying to render a text at certain point and a rectangle around it. This example is doing exactly what I need. In python implementation font.measureText returns only advance of text. As per the doc only "TextBlob" class is having "bounds" method and to calculate it first you need to create an instance of it:

textblob = skia.TextBlob(txt, self.font)
bounds = textblob.bounds()

I think it's an unnecessary step to calculate bounds. We already have our font instance available to calculate bounds. The doc also suggests about paint.getTextBounds(...) method but it's not implemented yet.

To Reproduce
Steps to reproduce the behavior:
As shown in the above example at link:

# Init
typeface = skia.Typeface('Arial')
font = skia.Font(typeface, 16.0, 1.0, 0.0)
textblob = skia.TextBlob('Hello World', font)
bounds = textblob.bounds()
bounds = bounds.makeOffset(100,100)

# Draw
canvas.drawString('Hello World', 100, 100, font, p)
canvas.drawRect(bounds, strokePaint)

bug3
Neither rect is having correct size nor it's drawn properly around the text.
Expected behavior
Get correct text bounds.

Desktop (please complete the following information):

  • OS: Windows 10
  • Python: 3.9.5
  • skia-python version: 87.3

Additional context
A simple example that shows getting the correct text bounds.

Regards.

@prashant-saxena
Copy link
Author

prashant-saxena commented Dec 20, 2021

Ok,
I have created this little function for text alignment and hope other users would find it useful.

# Horizontal align
ALIGN_LEFT 		= 1 << 0 # Default, align text horizontally to left.
ALIGN_CENTER 	= 1 << 1 # Align text horizontally to center.
ALIGN_RIGHT 	= 1 << 2 # Align text horizontally to right.
# Vertical align
ALIGN_TOP 		= 1 << 3 # Align text vertically to top.
ALIGN_MIDDLE	= 1 << 4 # Align text vertically to middle.
ALIGN_BOTTOM	= 1 << 5 # Align text vertically to bottom.
ALIGN_BASELINE	= 1 << 6 # Default, align text vertically to baseline.


def draw_text(canvas, txt, x, y, font, paint, flags):
    # Get bounds of txt
    rect = skia.Rect.MakeXYWH(0, 0, 0, 0)
    font.measureText(txt, bounds=rect)
     
    px = 0.0
    py = 0.0
    if ALIGN_LEFT & flags:
        px = rect.x()
    elif ALIGN_CENTER & flags:
        px = rect.width()/2.0
    elif ALIGN_RIGHT & flags:
        px = rect.width()
        
    if ALIGN_TOP & flags:
        py = rect.y()
    elif ALIGN_MIDDLE & flags:
        py = -rect.height()/2.0
    elif ALIGN_BOTTOM & flags:
        py = 0.0
    elif ALIGN_BASELINE & flags:
        pass
    
    canvas.drawString('Hello World', x-px, y-py, font, paint)

#Example
draw_text(canvas, "Hello World", 100, 100, font, paint, ALIGN_CENTER|ALIGN_MIDDLE)

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

1 participant