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

understanding pygame font render position #169

Open
DeepInSearch opened this issue Jul 24, 2019 · 0 comments
Open

understanding pygame font render position #169

DeepInSearch opened this issue Jul 24, 2019 · 0 comments

Comments

@DeepInSearch
Copy link

I'm trying to understand the font rendering process in render_curved(self, font, word_text) function of text_utils.py. I focus on these lines, but I can't understand why ch_bounds is computed as this, in line 189.

`

    bbs = []
    # place middle char
    rect = font.get_rect(word_text[mid_idx])
    rect.centerx = surf.get_rect().centerx
    rect.centery = surf.get_rect().centery + rect.height
    rect.centery +=  curve[mid_idx]
    ch_bounds = font.render_to(surf, rect, word_text[mid_idx], rotation=rots[mid_idx])
    ch_bounds.x = rect.x + ch_bounds.x
    ch_bounds.y = rect.y - ch_bounds.y
    mid_ch_bb = np.array(ch_bounds)`

I have something don't understand:

  1. why the rect.centery need to add rect.height ?
  2. why ch_bounds.x and ch_bounds.y have different calculation manner with ch_bounds?

I imitated this rendering process as follows:
`

   import os, sys
   import pygame
   from pygame.locals import *
   from pygame import freetype

   pygame.init()
   font = freetype.Font("../font/Amiri-Bold.ttf", size=40)
   bg_surf = pygame.Surface((200,200),SRCALPHA, 32)

   display_char = "E"
   rect = font.get_rect(display_char)
   pygame.draw.rect(bg_surf, pygame.Color(128, 0, 0), rect,1)

   rect.centerx = bg_surf.get_rect().centerx
   rect.centery = bg_surf.get_rect().centery + rect.height
   pygame.draw.rect(bg_surf, pygame.Color(255, 0, 0), rect,1)

   ch_bounds = font.render_to(bg_surf, rect, display_char, rotation=0, fgcolor=(255,255,255))
   print(rect,ch_bounds)
   ch_bounds.x = rect.x + ch_bounds.x
   ch_bounds.y = rect.y - ch_bounds.y
   pygame.draw.rect(bg_surf, pygame.Color(0, 128, 0), ch_bounds,1)
   pygame.image.save(bg_surf, 'bg_surf.jpg')`

The result show like this.
bg_surf

So why we don't just use this ?
`

    bbs = []
    # place middle char
    rect = font.get_rect(word_text[mid_idx])
    rect.centerx = surf.get_rect().centerx
    rect.centery = surf.get_rect().centery 
    rect.centery +=  curve[mid_idx]
    ch_bounds = font.render_to(surf, rect, word_text[mid_idx], rotation=rots[mid_idx])
    ch_bounds.x = rect.x 
    ch_bounds.y = rect.y 
    mid_ch_bb = np.array(ch_bounds)`

How do you consider the position of each character?

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