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?

Buy Me A Coffee

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 10:57PM

    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

    Buy Me A Coffee

  • 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.

    Buy Me A Coffee

  • edited 10:57PM
    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!

    Buy Me A Coffee

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games