def _create_surface(self): """Create the surface of the stimulus.""" rect = pygame.Rect((0, 0), self.size) if os.path.isfile(self._text_font): _font = pygame.font.Font(unicode2str(self._text_font, fse=True), self._text_size) else: _font = pygame.font.Font(self._text_font, self._text_size) _font.set_bold(self.text_bold) _font.set_italic(self.text_italic) _font.set_underline(self.text_underline) if type(self.text) is not unicode: # Pygame wants latin-1 encoding here for character strings _text = str2unicode(self.text).encode('latin-1') else: _text = self.text surface = self.render_textrect(self.format_block(_text), _font, rect, self.text_colour, self.background_colour, self.text_justification) return surface
def _create_surface(self): """Create the surface of the stimulus.""" if os.path.isfile(self._text_font): _font = pygame.font.Font(unicode2str(self._text_font, fse=True), self._text_size) else: _font = pygame.font.Font(self._text_font, self._text_size) _font.set_bold(self.text_bold) _font.set_italic(self.text_italic) _font.set_underline(self.text_underline) if type(self.text) is not unicode: # Pygame wants latin-1 encoding here for character strings _text = str2unicode(self.text).encode('latin-1') else: _text = self.text if self.background_colour: surface = _font.render(_text, True, self.text_colour, self.background_colour) else: surface = _font.render(_text, True, self.text_colour) surface.convert_alpha() if self._max_width > 0 and surface.get_size()[0] > self._max_width: # trim too long text lines self._text = self._text[:-2] + "~" surface = self._create_surface() # recursion return surface
def _create_surface(self): """Create the surface of the stimulus.""" if os.path.isfile(self._text_font): _font = pygame.font.Font(unicode2str(self._text_font, fse=True), self._text_size) else: _font = pygame.font.Font(self._text_font, self._text_size) _font.set_bold(self.text_bold) _font.set_italic(self.text_italic) _font.set_underline(self.text_underline) if type(self.text) is not unicode: # Pygame wants latin-1 encoding here for character strings _text = str2unicode(self.text).encode('latin-1') else: _text = self.text if self.background_colour: surface = _font.render(_text, True, self.text_colour, self.background_colour) else: surface = _font.render(_text, True, self.text_colour) surface = surface.convert_alpha() if self._max_width > 0 and surface.get_size()[0] > self._max_width: # trim too long text lines self._text = self._text[:-2] + "~" surface = self._create_surface() # recursion return surface
def _create_surface(self): """Create the surface of the stimulus.""" if os.path.isfile(self._text_font): _font = pygame.font.Font(unicode2str(self._text_font, fse=True), self._text_size) else: _font = pygame.font.Font(self._text_font, self._text_size) _font.set_bold(self.text_bold) _font.set_italic(self.text_italic) _font.set_underline(self.text_underline) if type(self.text) is not unicode: # Pygame wants latin-1 encoding here for character strings _text = str2unicode(self.text).encode('latin-1') else: _text = self.text if self.background_colour: text = _font.render(_text, True, self.text_colour, self.background_colour) else: text = _font.render(_text, True, self.text_colour) text.convert_alpha() surface = text return surface
def load(self, filename, encoding=None): """Load a stimulation protocol from a csv file. Parameters ---------- filename : str The filename to read the protocol from encoding : str, optional The encoding to be used when reading from the file """ self._conditions = [] self._unit = None if encoding is None: with open(filename, 'r') as f: first_line = f.readline() encoding = re.findall("coding[:=]\s*([-\w.]+)", first_line) if encoding == []: second_line = f.readline() encoding = re.findall("coding[:=]\s*([-\w.]+)", second_line) if encoding == []: encoding = [None] else: encoding = [encoding] with codecs.open(filename, 'rb', encoding[0], errors='replace') as f: for line in f: line = str2unicode(line) if line.startswith("#"): if line.startswith("#unit="): self._unit = line[6:].strip('\n') elif line.startswith("condition,begin,end,weight"): pass else: data = line.split(",") if self._find_condition_by_name(data[0]) is None: self.add_condition(data[0]) self.add_event(data[0], int(data[1]), int(data[2]), int(data[3]))
def __init__(self, filename, encoding=None): """Create an input file. All lines in the specified text file will be read into a list of strings. Parameters ---------- filename : str, optional name of the input file encoding : str, optional the encoding used to read the content of the file """ self._filename = filename self._current_line = 1 self._lines = [] if not(os.path.isfile(self._filename)): raise IOError("The input file '{0}' does not exist.".format( unicode2str(self._filename))) if encoding is None: with open(filename, 'r') as fl: first_line = fl.readline() encoding = re.findall("coding[:=]\s*([-\w.]+)", first_line) if encoding == []: second_line = fl.readline() encoding = re.findall("coding[:=]\s*([-\w.]+)", second_line) if encoding == []: encoding = [None] else: encoding = [encoding] with codecs.open(self._filename, 'rb', encoding[0], errors='replace') as f: for line in f: self._lines.append(str2unicode(line.rstrip('\r\n')))