예제 #1
0
 def __init__(
     self,
     win,
     text,
     font='Arvo',
     pos=(0, 0),
     size=None,
     padding=None,
     anchor='center',
     units=None,
     color='white',
     fillColor='darkgrey',
     borderColor=None,
     borderWidth=0,
     colorSpace='rgb',
     opacity=None,
     letterHeight=None,
     bold=True,
     italic=False,
     name="",
     autoLog=None,
 ):
     # Initialise TextBox
     TextBox2.__init__(self,
                       win,
                       text,
                       font,
                       name=name,
                       pos=pos,
                       size=size,
                       padding=padding,
                       anchor=anchor,
                       units=units,
                       color=color,
                       fillColor=fillColor,
                       borderColor=borderColor,
                       borderWidth=borderWidth,
                       colorSpace=colorSpace,
                       opacity=opacity,
                       letterHeight=letterHeight,
                       bold=bold,
                       italic=italic,
                       alignment='center',
                       editable=False,
                       autoLog=None)
     self.listener = event.Mouse(win=win)
     self.buttonClock = core.Clock()
     self.wasClicked = False  # Attribute to save whether button was previously clicked
     # Arrays to store times of clicks on and off
     self.timesOn = []
     self.timesOff = []
예제 #2
0
 def test_editable(self):
     # Make textbox editable
     self.textbox.editable = True
     # Make a second textbox, which is editable from init
     textbox2 = TextBox2(self.win,
                         "",
                         "Noto Sans",
                         pos=(0.5, 0.5),
                         size=(1, 1),
                         units='height',
                         letterHeight=0.1,
                         colorSpace="rgb",
                         editable=True)
     # Check that both are editable children of the window
     editables = []
     for ref in self.win._editableChildren:
         editables.append(ref())  # Actualise weakrefs
     assert self.textbox in editables
     assert textbox2 in editables
     # Set current editable to textbox 1
     self.win.currentEditable = self.textbox
     # Make sure next editable is textbox 2
     self.win.nextEditable()
     assert self.win.currentEditable == textbox2
     # Make each textbox no longer editable
     self.textbox.editable = False
     textbox2.editable = False
     # Make sure they are no longer editable children of the window
     editables = []
     for ref in self.win._editableChildren:
         editables.append(ref())  # Actualise weakrefs
     assert self.textbox not in editables
     assert textbox2 not in editables
     # Cleanup
     del textbox2
예제 #3
0
 def setup(self):
     self.win = Window((128, 128),
                       pos=(50, 50),
                       monitor="testMonitor",
                       allowGUI=False,
                       autoLog=False)
     self.error = _BaseErrorHandler()
     self.textbox = TextBox2(
         self.win,
         "A PsychoPy zealot knows a smidge of wx, but JavaScript is the question.",
         "Noto Sans",
         alignment="top left",
         lineSpacing=1,
         pos=(0, 0),
         size=(1, 1),
         units='height',
         letterHeight=0.1,
         colorSpace="rgb")
     self.obj = self.textbox  # point to textbox for mixin tests
     # Pixel which is the border color
     self.borderPoint = (0, 0)
     self.borderUsed = True
     # Pixel which is the fill color
     self.fillPoint = (2, 2)
     self.fillUsed = True
     # Textbox foreground is too unreliable due to fonts for pixel analysis
     self.foreUsed = False
예제 #4
0
    def test_caret_position(self):

        # Identify key indices to test
        indices = (
            4,  # first line
            30,  # wrapping line
            64  # newline line
        )
        # One array for anchor and alignment as they use the same allowed vals
        anchalign = (
            'center',
            'top-center',
            'bottom-center',
            'center-left',
            'center-right',
            'top-left',
            'top-right',
            'bottom-left',
            'bottom-right',
        )
        # Try with all anchors
        for anchor in anchalign:
            # Try with all alignments
            for align in anchalign:
                # Try flipped horiz and unflipped
                for flipHoriz in (True, False):
                    # Try flipped vert and unflipped
                    for flipVert in (True, False):
                        # Create a textbox with multiple lines of differing length from both wrapping and a newline char
                        textbox = TextBox2(
                            self.win,
                            text=
                            "A PsychoPy zealot knows a smidge of wx, but JavaScript is the \nquestion.",
                            size=(128, 64),
                            pos=(0, 0),
                            units='pix',
                            anchor=anchor,
                            alignment=align,
                            flipHoriz=flipHoriz,
                            flipVert=flipVert)
                        # Check that caret position at key indices matches character positions
                        for i in indices:
                            # Set caret index
                            textbox.caret.index = i
                            # Draw textbox to refresh verts
                            textbox.draw()
                            textbox.caret.draw()
                            # Compare bottom vert of caret to bottom right vert of character (within 1 px)
                            caretVerts = textbox.caret.vertices
                            charVerts = textbox._vertices.pix[range(
                                (i - 1) * 4, (i - 1) * 4 + 4)]
                            assert all(
                                np.isclose(caretVerts[0], charVerts[2], 1)
                            ), (f"Textbox caret at index {i} didn't align with bottom right corner of "
                                f"matching char when:\n"
                                f"anchor={anchor}, alignment={align}, flipHoriz={flipHoriz}, flipVert={flipVert}.\n"
                                f"Caret vertices were:\n"
                                f"{caretVerts}\n"
                                f"Char verts were \n"
                                f"{charVerts}\n")
예제 #5
0
 def setup_class(self):
     self.win = Window([128, 128],
                       pos=[50, 50],
                       allowGUI=False,
                       autoLog=False)
     self.error = _BaseErrorHandler()
     self.textbox = TextBox2(self.win,
                             "",
                             "Noto Sans",
                             pos=(0, 0),
                             size=(1, 1),
                             units='height',
                             letterHeight=0.1,
                             colorSpace="rgb")
예제 #6
0
 def setup_class(self):
     self.win = Window([128, 128],
                       pos=[50, 50],
                       allowGUI=False,
                       autoLog=False)
     self.error = _BaseErrorHandler()
     self.textbox = TextBox2(self.win,
                             "",
                             "Noto Sans",
                             pos=(0, 0),
                             size=(1, 1),
                             units='height',
                             letterHeight=0.1,
                             colorSpace="rgb")
     self.obj = self.textbox  # point to textbox for mixin tests
     # Pixel which is the border color
     self.borderPoint = (0, 0)
     self.borderUsed = True
     # Pixel which is the fill color
     self.fillPoint = (2, 2)
     self.fillUsed = True
     # Textbox foreground is too unreliable due to fonts for pixel analysis
     self.foreUsed = False
예제 #7
0
 def test_alerts(self):
     noFontTextbox = TextBox2(self.win, "", font="Raleway Dots", bold=True)
     assert (self.error.alerts[0].code == 4325)
예제 #8
0
 def test_colors(self):
     textbox = TextBox2(self.win,
                        "",
                        "Consolas",
                        pos=(0, 0),
                        size=(1, 1),
                        letterHeight=0.1,
                        units='height',
                        colorSpace="rgb")
     textbox.fontMGR.addGoogleFont("Noto Sans")
     textbox.font = "Noto Sans"
     textbox.text = "A PsychoPy zealot knows a smidge of wx, but JavaScript is the question."
     # Some exemplar text to test basic colors
     exemplars = [
         # White on black in rgb
         {
             "color": (1, 1, 1),
             "fillColor": (-1, -1, -1),
             "borderColor": (-1, -1, -1),
             "space": "rgb",
             "screenshot": "textbox_colors_WOB.png"
         },
         # White on black in named
         {
             "color": "white",
             "fillColor": "black",
             "borderColor": "black",
             "space": "rgb",
             "screenshot": "textbox_colors_WOB.png"
         },
         # White on black in hex
         {
             "color": "#ffffff",
             "fillColor": "#000000",
             "borderColor": "#000000",
             "space": "hex",
             "screenshot": "textbox_colors_WOB.png"
         },
     ]
     # Some colors which are likely to cause problems if something isn't working
     tykes = [
         # Text only
         {
             "color": "white",
             "fillColor": None,
             "borderColor": None,
             "space": "rgb",
             "screenshot": "textbox_colors_tyke1.png"
         },
         # The following will only work when the Color class is implemented (currently opacity is all or nothing)
         # Fill only
         # {"color": None, "fillColor": "white", "borderColor": None, "space": "rgb",
         #  "screenshot": "textbox_colors_tyke2.png"},
         # Border only
         # {"color": None, "fillColor": None, "borderColor": "white", "space": "rgb",
         # "screenshot": "textbox_colors_tyke3.png"},
     ]
     # Test each case and compare against screenshot
     for case in exemplars + tykes:
         # Raise error if case spec does not contain all necessary keys
         if not all(
                 key in case for key in
             ["color", "fillColor", "borderColor", "space", "screenshot"]):
             raise KeyError(
                 f"Case spec for test_colors in class {self.__class__.__name__} ({__file__}) invalid, test cannot be run."
             )
         # Apply params from case spec
         textbox.colorSpace = case['space']
         textbox.color = case['color']
         textbox.fillColor = case['fillColor']
         textbox.borderColor = case['borderColor']
         self.win.flip()
         textbox.draw()
         if case['screenshot']:
             # Uncomment to save current configuration as desired
             # self.win.getMovieFrame(buffer='back').save(Path(utils.TESTS_DATA_PATH) / case['screenshot'])
             utils.compareScreenshot(
                 Path(utils.TESTS_DATA_PATH) / case['screenshot'], self.win)
예제 #9
0
 def test_glyph_rendering(self):
     textbox = TextBox2(self.win,
                        "",
                        "Arial",
                        pos=(0, 0),
                        size=(1, 1),
                        letterHeight=0.1,
                        units='height')
     # Add all Noto Sans fonts to cover widest possible base of handles characters
     for font in [
             "Noto Sans", "Noto Sans HK", "Noto Sans JP", "Noto Sans KR",
             "Noto Sans SC", "Noto Sans TC", "Niramit", "Indie Flower"
     ]:
         textbox.fontMGR.addGoogleFont(font)
     # Some exemplar text to test basic TextBox rendering
     exemplars = [
         # An English pangram
         {
             "text":
             "A PsychoPy zealot knows a smidge of wx, but JavaScript is the question.",
             "font": "Noto Sans",
             "screenshot": "textbox_exemplar_1.png"
         },
         # The same pangram in IPA
         {
             "text":
             "ə saɪkəʊpaɪ zɛlət nəʊz ə smidge ɒv wx, bʌt ˈʤɑːvəskrɪpt ɪz ðə ˈkwɛsʧən",
             "font": "Noto Sans",
             "screenshot": "textbox_exemplar_2.png"
         },
         # The same pangram in Hangul
         {
             "text": "아 프시초피 제알롣 크노W스 아 s믿게 오f wx, 붇 자v앗c립t 잇 테 q왯디온",
             "font": "Noto Sans KR",
             "screenshot": "textbox_exemplar_3.png"
         },
         # A noticeably non-standard font
         {
             "text":
             "A PsychoPy zealot knows a smidge of wx, but JavaScript is the question.",
             "font": "Indie Flower",
             "screenshot": "textbox_exemplar_4.png",
         }
     ]
     # Some text which is likely to cause problems if something isn't working
     tykes = [
         # Text which doesn't render properly on Mac (Issue #3203)
         {
             "text": "कोशिकायें",
             "font": "Noto Sans",
             "screenshot": "textbox_tyke_1.png"
         },
         # Thai text which old Text component couldn't handle due to Pyglet
         {
             "text": "ขาว แดง เขียว เหลือง ชมพู ม่วง เทา",
             "font": "Niramit",
             "screenshot": "textbox_tyke_2.png"
         }
     ]
     # Test each case and compare against screenshot
     for case in exemplars + tykes:
         textbox.reset()
         textbox.fontMGR.addGoogleFont(case['font'])
         textbox.font = case['font']
         textbox.text = case['text']
         self.win.flip()
         textbox.draw()
         if case['screenshot']:
             # Uncomment to save current configuration as desired
             #self.win.getMovieFrame(buffer='back').save(Path(utils.TESTS_DATA_PATH) / case['screenshot'])
             utils.compareScreenshot(
                 Path(utils.TESTS_DATA_PATH) / case['screenshot'], self.win)