def get_font_height_to_size_ratio(font_name): """ Find the ratio between a font size and the corresponding textbox height in pixels. The returned ratio is height/size :type font_name: str """ freetype.init() font = Font(find_font(font_name), size=100) size = font.size("hfbpqgXQ,") return size[1] / 100.0
def __init__(self, text, size, position=None, text_font=None, text_size=None, text_bold=None, text_italic=None, text_underline=None, text_justification=None, text_colour=None, background_colour=None): """Create a text box. Notes ----- text_font can be both, a name or path to a font file! When text_font is a name, Expyriment will try to find a font that best matches the given name. If no matching font can be found, or if the given font file cannot be found, the Pygame system default will be used. In any case the value of the attribute text_font will always resemble the font that is actually in use! Parameters ---------- text : str text to wrap size : (int, int) size of the text box position : (int, int), optional position of the stimulus text_font : str, optional text font to use as a name or as a path to a font file text_size : int, optional size of the text text_bold : bool, optional font should be bold text_italic : bool, optional font should be italic text_underline : bool, optional font should get an underline text_justification : int, optional text justification, 0 (left), 1 (center), 2 (right) text_colour : (int, int, int), optional colour of the text background_colour : (int, int, int), optional background colour """ pygame.font.init() if position is None: position = defaults.textbox_position Visual.__init__(self, position) self._text = text self._size = size if text_size is None: text_size = defaults.textbox_text_size if text_size is not None: self._text_size = text_size else: self._text_size = expyriment._active_exp.text_size if text_font is None: text_font = defaults.textbox_text_font if text_font is not None: self._text_font = find_font(text_font) else: self._text_font = find_font(expyriment._active_exp.text_font) try: _font = pygame.font.Font(unicode2str(self._text_font, fse=True), 10) _font = None except: raise IOError("Font '{0}' not found!".format(text_font)) if text_bold is not None: self._text_bold = text_bold else: self._text_bold = defaults.textbox_text_bold if text_italic is not None: self._text_italic = text_italic else: self._text_italic = defaults.textbox_text_italic if text_underline is not None: self._text_underline = text_underline else: self._text_underline = defaults.textbox_text_underline if text_justification is not None: self._text_justification = text_justification else: self._text_justification = defaults.textbox_text_justification if text_colour is not None: self._text_colour = text_colour else: if defaults.textbox_text_colour is not None: self._text_colour = defaults.textbox_text_colour else: self._text_colour = expyriment._active_exp.foreground_colour if background_colour is not None: self._background_colour = background_colour else: self._background_colour = \ defaults.textbox_background_colour
def __init__(self, heading, text, position=None, heading_font=None, heading_size=None, heading_bold=None, heading_italic=None, heading_underline=None, heading_colour=None, text_font=None, text_size=None, text_bold=None, text_italic=None, text_underline=None, text_colour=None, text_justification=None, background_colour=None, size=None): """Create a text screen. Parameters ---------- heading : str heading of the text screen text : str text of the text screen position : (int, int), optional position of the stimulus heading_font : str, optional heading font to use heading_size : int, optional heading font size heading_bold : bool, optional heading should be bold heading_italic : bool, optional heading should be italic heading_underline : bool, optional heading should get an underline heading_colour : (int,int,int), optional heding colour text_font : str, optional text font to use text_size : int, optional text font size text_bold : bool, optional text should be bold text_italic : bool, optional text should be italic text_underline : bool, optional text should get an underline text_colour : (int,int,int), optional text colour text_justification : int, optional 0 (Left), 1(center), 2(right) (int) (optional) background_colour : (int, int, int), optional background_colour size : (int, int), optional size of the text screen """ if position is None: position = defaults.textscreen_position Visual.__init__(self, position, log_comment="text_screen") self._heading = heading self._text = text if heading_font is None: heading_font = defaults.textscreen_heading_font if heading_font is not None: self._heading_font = find_font(heading_font) else: self._heading_font = find_font(expyriment._active_exp.text_font) try: _font = pygame.font.Font( unicode2str(self._heading_font, fse=True), 10) _font = None except: raise IOError("Font '{0}' not found!".format(heading_font)) if heading_size is None: heading_size = defaults.textscreen_heading_size if heading_size: self._heading_size = heading_size else: self._heading_size = int(expyriment._active_exp.text_size * 1.2) if heading_bold is not None: self._heading_bold = heading_bold else: self._heading_bold = defaults.textscreen_heading_bold if heading_italic is not None: self._heading_italic = heading_italic else: self._heading_italic = \ defaults.textscreen_heading_italic if heading_underline is not None: self._heading_underline = heading_underline else: self._heading_underline = \ defaults.textscreen_heading_underline if heading_colour is None: heading_colour = defaults.textscreen_heading_colour if heading_colour is not None: self._heading_colour = heading_colour else: self._heading_colour = expyriment._active_exp.foreground_colour if text_font is None: text_font = defaults.textscreen_text_font if text_font is not None: self._text_font = find_font(text_font) else: self._text_font = find_font(expyriment._active_exp.text_font) try: _font = pygame.font.Font(unicode2str(self._text_font, fse=True), 10) _font = None except: raise IOError("Font '{0}' not found!".format(text_font)) if text_size is None: self._text_size = defaults.textscreen_text_size if text_size is not None: self._text_size = text_size else: self._text_size = expyriment._active_exp.text_size if text_bold is not None: self._text_bold = text_bold else: self._text_bold = defaults.textscreen_text_bold if text_italic is not None: self._text_italic = text_italic else: self._text_italic = defaults.textscreen_text_italic if text_underline is not None: self._text_underline = text_underline else: self._text_underline = defaults.textscreen_text_underline if text_colour is None: text_colour = defaults.textscreen_text_colour if text_colour is not None: self._text_colour = text_colour else: self._text_colour = expyriment._active_exp.foreground_colour if text_justification is not None: self._text_justification = text_justification else: self._text_justification = \ defaults.textscreen_text_justification if size is not None: self._size = size else: size = defaults.textscreen_size if size is None: try: self._size = ( expyriment._active_exp.screen.surface.get_size()[0] - expyriment._active_exp.screen.surface.get_size()[0] / 5, expyriment._active_exp.screen.surface.get_size()[1] - expyriment._active_exp.screen.surface.get_size()[1] / 5) except: raise RuntimeError("Cannot get size of screen!") if background_colour is not None: self._background_colour = background_colour else: self._background_colour = \ defaults.textscreen_background_colour
def __init__(self, message="", position=None, ascii_filter=None, length=None, message_text_size=None, message_colour=None, message_font=None, message_bold=None, message_italic=None, user_text_size=None, user_text_bold=None, user_text_font=None, user_text_colour=None, background_colour=None, frame_colour=None, gap=None, screen=None, background_stimulus=None): """Create a text input box. Notes ----- This stimulus is not optimized for timing accurate presentation! Parameters ---------- message : str, optional message to show position : (int, int), optional position of the TextInput canvas length : int, optional the length of the text input frame in number of charaters ascii_filter : list, optional list of ASCII codes to filter for message_text_size : int, optional text size of the message message_colour : (int, int, int), optional text colour of the message message_font : str, optional text font of the message message_bold : bool, optional True if message text should be bold message_italic : bool, optional True if message text should be italic user_text_size : int, optional text size of the user input user_text_font : str, optional text font of the user input user_text_colour : (int, int ,int), optional text colour of the user input user_text_bold : bool, optional True if user text should be bold background_colour : (int, int, int), optional frame_colour : (int, int, int) colour of the frame gap : int, optional gap between message and user input screen : io.Screen, optional screen to present on background_stimulus : visual Expyriment stimulus, optional The background stimulus is a second stimulus that will be presented together with the TextInput. For both stimuli overlap TextInput will appear on top of the background_stimulus """ if not expyriment._active_exp.is_initialized: raise RuntimeError( "Cannot create TextInput before expyriment.initialize()!") Input.__init__(self) self._message = message if position is not None: self._position = position else: self._position = defaults.textinput_position if ascii_filter is not None: self._ascii_filter = ascii_filter else: self._ascii_filter = defaults.textinput_ascii_filter if length is not None: self._length = length else: self._length = defaults.textinput_length if message_text_size is None: message_text_size = defaults.textinput_message_text_size if message_text_size is not None: self._message_text_size = message_text_size else: self._message_text_size = expyriment._active_exp.text_size if message_colour is None: message_colour = defaults.textinput_message_colour if message_colour is not None: self._message_colour = message_colour else: self._message_colour = expyriment._active_exp.foreground_colour if message_font is None: message_font = defaults.textinput_message_font if message_font is not None: self._message_font = find_font(message_font) else: self._message_font = find_font(expyriment._active_exp.text_font) try: _font = pygame.font.Font(unicode2str(self._message_font, fse=True), 10) except: raise IOError("Font '{0}' not found!".format(message_font)) if message_bold is not None: self._message_bold = message_bold else: self._message_bold = defaults.textinput_message_bold if message_italic is not None: self._message_italic = message_italic else: self._message_italic = defaults.textinput_message_italic if user_text_size is None: user_text_size = defaults.textinput_user_text_size if user_text_size is not None: self._user_text_size = user_text_size else: self._user_text_size = expyriment._active_exp.text_size if user_text_bold is not None: self._user_text_bold = user_text_bold else: self._user_text_bold = defaults.textinput_user_text_bold if user_text_font is None: user_text_font = defaults.textinput_user_text_font if user_text_font is not None: self._user_text_font = find_font(user_text_font) else: self._user_text_font = find_font(expyriment._active_exp.text_font) try: _font = pygame.font.Font( unicode2str(self._user_text_font, fse=True), 10) except: raise IOError("Font '{0}' not found!".format(user_text_font)) if user_text_colour is None: user_text_colour = defaults.textinput_user_text_colour if user_text_colour is not None: self._user_text_colour = user_text_colour else: self._user_text_colour = expyriment._active_exp.foreground_colour if background_colour is None: background_colour = \ defaults.textinput_background_colour if background_colour is not None: self._background_colour = background_colour else: self._background_colour = \ expyriment._active_exp.background_colour if frame_colour is None: frame_colour = defaults.textinput_frame_colour if frame_colour is not None: self._frame_colour = frame_colour else: self._frame_colour = expyriment._active_exp.foreground_colour if gap is not None: self._gap = gap else: self._gap = defaults.textinput_gap if screen is not None: self._screen = screen else: self._screen = expyriment._active_exp.screen if background_stimulus is not None: # FIXME child of child of visual does not work as background stimulus, e.g. BlankScreen if background_stimulus.__class__.__base__ in \ [expyriment.stimuli._visual.Visual, expyriment.stimuli.Shape]: self._background_stimulus = background_stimulus else: raise TypeError("{0} ".format(type(background_stimulus)) + "is not a valid background stimulus. " + "Use an expyriment visual stimulus.") else: self._background_stimulus = None self._user = [] self._user_text_surface_size = None self._max_size = None self._message_surface_size = None self._canvas = None self._canvas_size = None
def __init__(self, text, position=None, text_font=None, text_size=None, text_bold=None, text_italic=None, text_underline=None, text_colour=None, background_colour=None, max_width=None): """Create a text line stimulus. NOTE: text_font can be both, a name or path to a font file! When text_font is a name, Expyriment will try to find a font that best matches the given name. If no matching font can be found, or if the given font file cannot be found, the Pygame system default will be used. In any case the value of the attribute text_font will always resemble the font that is actually in use! Parameters ---------- text : str text to show (str) position : (int, int), optional position of the stimulus text_font : str, optional font to use as name or as path to a font file text_size : int, optional text size text_bold : bool, optional font should be bold text_italic : bool, optional font should be italic text_underline : bool, optional font should get an underline text_colour : (int, int, int), optional text colour background_colour : (int, int, int), optional background colour max_width: int, optional maximum surface width of the text line stimulus if this parameter is defined, text lines that exceed this surface width will be trimmed (indicated by a '~' as last letter) """ pygame.font.init() if position is None: position = defaults.textline_position Visual.__init__(self, position, log_comment=text) self._text = text self._max_width = max_width if text_size is None: text_size = defaults.textline_text_size if text_size is not None: self._text_size = text_size else: self._text_size = expyriment._active_exp.text_size if text_font is None: text_font = defaults.textline_text_font if text_font is not None: self._text_font = find_font(text_font) else: self._text_font = find_font(expyriment._active_exp.text_font) try: _font = pygame.font.Font(unicode2str(self._text_font, fse=True), 10) _font = None except: raise IOError("Font '{0}' not found!".format(text_font)) if text_bold is not None: self._text_bold = text_bold else: self._text_bold = defaults.textline_text_bold if text_italic is not None: self._text_italic = text_italic else: self._text_italic = defaults.textline_text_italic if text_underline is not None: self._text_underline = text_underline else: self._text_underline = defaults.textline_text_underline if text_colour is None: text_colour = defaults.textline_text_colour if text_colour is not None: self._text_colour = text_colour else: self._text_colour = expyriment._active_exp.foreground_colour if background_colour is not None: self._background_colour = background_colour else: self._background_colour = \ defaults.textline_background_colour
def __init__( self, message="", position=None, ascii_filter=None, length=None, message_text_size=None, message_colour=None, message_font=None, message_bold=None, message_italic=None, user_text_size=None, user_text_bold=None, user_text_font=None, user_text_colour=None, background_colour=None, frame_colour=None, gap=None, screen=None, background_stimulus=None, ): """Create a text input box. Notes ----- This stimulus is not optimized for timing accurate presentation! Parameters ---------- message : str, optional message to show position : (int, int), optional position of the TextInput canvas length : int, optional the length of the text input frame in number of charaters ascii_filter : list, optional list of ASCII codes to filter for message_text_size : int, optional text size of the message message_colour : (int, int, int), optional text colour of the message message_font : str, optional text font of the message message_bold : bool, optional True if message text should be bold message_italic : bool, optional True if message text should be italic user_text_size : int, optional text size of the user input user_text_font : str, optional text font of the user input user_text_colour : (int, int ,int), optional text colour of the user input user_text_bold : bool, optional True if user text should be bold background_colour : (int, int, int), optional frame_colour : (int, int, int) colour of the frame gap : int, optional gap between message and user input screen : io.Screen, optional screen to present on background_stimulus : visual Expyriment stimulus, optional The background stimulus is a second stimulus that will be presented together with the TextInput. For both stimuli overlap TextInput will appear on top of the background_stimulus """ if not expyriment._active_exp.is_initialized: raise RuntimeError("Cannot create TextInput before expyriment.initialize()!") Input.__init__(self) self._message = message if position is not None: self._position = position else: self._position = defaults.textinput_position if ascii_filter is not None: self._ascii_filter = ascii_filter else: self._ascii_filter = defaults.textinput_ascii_filter if length is not None: self._length = length else: self._length = defaults.textinput_length if message_text_size is None: message_text_size = defaults.textinput_message_text_size if message_text_size is not None: self._message_text_size = message_text_size else: self._message_text_size = expyriment._active_exp.text_size if message_colour is None: message_colour = defaults.textinput_message_colour if message_colour is not None: self._message_colour = message_colour else: self._message_colour = expyriment._active_exp.foreground_colour if message_font is None: message_font = defaults.textinput_message_font if message_font is not None: self._message_font = find_font(message_font) else: self._message_font = find_font(expyriment._active_exp.text_font) try: _font = pygame.font.Font(unicode2str(self._message_font, fse=True), 10) except: raise IOError("Font '{0}' not found!".format(message_font)) if message_bold is not None: self._message_bold = message_bold else: self._message_bold = defaults.textinput_message_bold if message_italic is not None: self._message_italic = message_italic else: self._message_italic = defaults.textinput_message_italic if user_text_size is None: user_text_size = defaults.textinput_user_text_size if user_text_size is not None: self._user_text_size = user_text_size else: self._user_text_size = expyriment._active_exp.text_size if user_text_bold is not None: self._user_text_bold = user_text_bold else: self._user_text_bold = defaults.textinput_user_text_bold if user_text_font is None: user_text_font = defaults.textinput_user_text_font if user_text_font is not None: self._user_text_font = find_font(user_text_font) else: self._user_text_font = find_font(expyriment._active_exp.text_font) try: _font = pygame.font.Font(unicode2str(self._user_text_font, fse=True), 10) except: raise IOError("Font '{0}' not found!".format(user_text_font)) if user_text_colour is None: user_text_colour = defaults.textinput_user_text_colour if user_text_colour is not None: self._user_text_colour = user_text_colour else: self._user_text_colour = expyriment._active_exp.foreground_colour if background_colour is None: background_colour = defaults.textinput_background_colour if background_colour is not None: self._background_colour = background_colour else: self._background_colour = expyriment._active_exp.background_colour if frame_colour is None: frame_colour = defaults.textinput_frame_colour if frame_colour is not None: self._frame_colour = frame_colour else: self._frame_colour = expyriment._active_exp.foreground_colour if gap is not None: self._gap = gap else: self._gap = defaults.textinput_gap if screen is not None: self._screen = screen else: self._screen = expyriment._active_exp.screen if background_stimulus is not None: # FIXME child of child of visual does not work as background stimulus, e.g. BlankScreen if background_stimulus.__class__.__base__ in [expyriment.stimuli._visual.Visual, expyriment.stimuli.Shape]: self._background_stimulus = background_stimulus else: raise TypeError( "{0} ".format(type(background_stimulus)) + "is not a valid background stimulus. " + "Use an expyriment visual stimulus." ) else: self._background_stimulus = None self._user = [] self._user_text_surface_size = None self._max_size = None self._message_surface_size = None self._canvas = None self._canvas_size = None
def __init__(self, text, size, position=None, text_font=None, text_size=None, text_bold=None, text_italic=None, text_underline=None, text_justification=None, text_colour=None, background_colour=None, do_not_trim_words=None): """Create a text box. Notes ----- text_font can be both, a name or path to a font file! When text_font is a name, Expyriment will try to find a font that best matches the given name. If no matching font can be found, or if the given font file cannot be found, the Pygame system default will be used. In any case the value of the attribute text_font will always resemble the font that is actually in use! Parameters ---------- text : str text to wrap size : (int, int) size of the text box position : (int, int), optional position of the stimulus text_font : str, optional text font to use as a name or as a path to a font file text_size : int, optional size of the text text_bold : bool, optional font should be bold text_italic : bool, optional font should be italic text_underline : bool, optional font should get an underline text_justification : int, optional text justification, 0 (left), 1 (center), 2 (right) text_colour : (int, int, int), optional colour of the text background_colour : (int, int, int), optional background colour do_not_trim_words: bool, optional if True, words that exceed the width of the text box will be not be trimmed and an exception is raise instead. default: False """ pygame.font.init() if position is None: position = defaults.textbox_position Visual.__init__(self, position) self._text = text self._size = size if text_size is None: text_size = defaults.textbox_text_size if text_size is not None: self._text_size = text_size else: self._text_size = expyriment._active_exp.text_size if text_font is None: text_font = defaults.textbox_text_font if text_font is not None: self._text_font = find_font(text_font) else: self._text_font = find_font(expyriment._active_exp.text_font) try: _font = pygame.font.Font(unicode2str(self._text_font, fse=True), 10) _font = None except: raise IOError("Font '{0}' not found!".format(text_font)) if text_bold is not None: self._text_bold = text_bold else: self._text_bold = defaults.textbox_text_bold if text_italic is not None: self._text_italic = text_italic else: self._text_italic = defaults.textbox_text_italic if text_underline is not None: self._text_underline = text_underline else: self._text_underline = defaults.textbox_text_underline if text_justification is not None: self._text_justification = text_justification else: self._text_justification = defaults.textbox_text_justification if text_colour is not None: self._text_colour = text_colour else: if defaults.textbox_text_colour is not None: self._text_colour = defaults.textbox_text_colour else: self._text_colour = expyriment._active_exp.foreground_colour if background_colour is not None: self._background_colour = background_colour else: self._background_colour = \ defaults.textbox_background_colour if do_not_trim_words is not None: self._do_not_trim_words = do_not_trim_words else: self._do_not_trim_words = defaults.textbox_do_not_trim_words