Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[open] Easiest way to change font-size in inline scrip items

edited September 2012 in OpenSesame

I'm looking for a way to increase the font size in an inline script item in combination with the canvas.text() function.
I found the function canvas.set_font(style,size), but this expects you to provide a font as the first argument, while I only want to change the size. Also, it says it expects 3 arguments, while in the OpenSesame documentation only 2 are described

What would be the best way to change the font size in an inline script, without having to change anything else?

Comments

  • edited September 2012

    Hi Daniel!

    The standard font is (as you probably know) 'mono'. So you could just use the following:

    my_canvas.set_font('mono', size) # change size to any integer you'd like

    Otherwise, but this is a solution that's pretty much the same amount of trouble as the first option, add an inline_script to the main experiment sequence containing the following (Prepare phase):

    global fontsize, fonttype
    
    fonttype = 'mono' # or any preferred other type, like 'sans' or 'serif'
    
    def fontsize(my_canvas,size):
        from openexp.canvas import canvas
        global fonttype
        my_canvas.set_font(fonttype,size)
        return my_canvas
    

    ALTERNATIVE:

    self.experiment.set("fonttype", 'mono')
    

    Next, in inline scripts, do the following:

    from openexp.canvas import canvas
    
    global fontsize
    
    # create canvas
    my_canvas = canvas(self.experiment)
    
    # to change fontsize
    fontsize(my_canvas, size) # change size to any integer you'd like
    

    ALTERNATIVE:

    from openexp.canvas import canvas
    
    # create canvas
    my_canvas = canvas(self.experiment)
    
    # to change fontsize
    my_canvas.set_font(self.get("fonttype"), size)
    

    Is the first option really to much of a hassle, you recon? I do think you have a point, so I would vote for a feature request (say, splitting up canvas.set_font(type,size) to canvas.set_fonttype(type) and canvas.set_fontsize(size))

    Best,

    Edwin

    EDIT: Might prove to be difficult, since the fonttype is set using pygame.font.Font (see http://www.pygame.org/docs/ref/font.html#pygame.font.Font) in the legacy backend.

    from openexp/_canvas/legacy.py (lines 271 to 283):

        def set_font(self, style, size):
        
            """
            Sets the font for subsequent drawing operations.
            
            Arguments:
            style -- A font located in the resources folder (without the .ttf
                     extension)
            size -- A font size in pixels       
            """
            
            self.font = pygame.font.Font(self.experiment.resource( \
                "%s.ttf" % style), size)
    

    You could just change this to (or add) the following in your OpenSesame source files:

        def set_fontsize(self, size):
        
            """
            Sets the font size  for subsequent drawing operations.
            
            Arguments:
            size -- A font size in pixels       
            """
            
            style = 'mono' # or preferred style
            
            self.font = pygame.font.Font(self.experiment.resource( \
                "%s.ttf" % style), size)
    

    Then you could use

    canvas.set_fontsize(size)
  • edited 4:55PM

    Hi Daniel and Edwin,

    Yes, I'm afraid only more-or-less hackish solutions are possible at the moment. Perhaps it would be a good idea to change the arguments of canvas.set_font() to keywords, so that you can change only one property, while leaving the rest unchanged. The API has already changed a bit, to allow for (bold, italic, and underlined)[http://osdoc.cogsci.nl/usage/text-formatting] fonts as well.

    Perhaps something like:

    def set_font(self, style=None, size=None, italic=None, bold=None, underline=None):
        # Function body

    This is backwards-compatible with the previous non-keyword API, but also allows you to do things like:

    canvas.set_font(style='sans')
    canvas.set_font(italic=True)

    Regarding the mysterious 3rd argument: Python also counts the self parameter for methods, which is a bit confusing.

    Cheers!
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • edited September 2012

    Hi Sebastiaan and Edwin,

    thanks for your replies. I didn't know the default font was mono and tried 'Arial', which it couldn't find as a ttf in the resource folder. I tried 'Mono' as well I think, but specified it with a capital M in the beginning, which OpenSesame didn't take.

    About the 'self' argument: whoops, I could have known that, although OS gave the error message "only 1 of 3 arguments is specified" when i tried canvas.set_font(size=12). Since 'self' is passed through automatically, shouldn't it have said "only 2 of 3 arguments are specified" in this case?

    Nevertheless, canvas.set_font('mono',50) did the trick for me and works.

  • edited 4:55PM
    About the 'self' argument: whoops, I could have known that, although OS gave the error message "only 1 of 3 arguments is specified" when i tried canvas.set_font(size=12). Since 'self' is passed through automatically, shouldn't it have said "only 2 of 3 arguments are specified" in this case?

    Right, that's because you're passing size as a keyword, rather than a fixed argument. If you pass the size as a non-keyword, Python indeed reports 2 arguments:

    from openexp.canvas import canvas
    c = canvas(exp)
    c.set_font(12)

    Error:

    raceback (most recent call last):
      File "/home/sebastiaan/git/opensesame/libopensesame/inline_script.py", line 121, in run
        exec(self.crun)
      File "", line 4, in 
    TypeError: set_font() takes at least 3 arguments (2 given)

    Cheers!

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

Sign In or Register to comment.