示例#1
0
文件: sftext.py 项目: LukeMS/sftext
    def parse_text(self):
        self.parsed = []
        scr_w = self.screen_rect.width

        self.default_style = Style.default_style
        self.default_style['font_obj'] = self.fonts.load(
            self.default_style['font'], self.default_style['size'])
        self.default_style['w'], self.default_style['h'] = (
            self.default_style['font_obj'].size(' '))

        y = 0
        for line in self.text.splitlines():
            x = 0
            for style in line.split("{style}"):

                text, styled_txt = Style.split(style)

                self.set_font(styled_txt)
                font = styled_txt['font_obj']

                w, h = styled_txt['w'], styled_txt['h'] = font.size(' ')
                # determine the amount of space needed to render text

                wraps = self.wrap_text(text, scr_w, x, styled_txt)

                for wrap in wraps:
                    rect = pygame.Rect((0, 0), font.size(wrap['text']))

                    if (x + wrap['w1'] + w * 3) > scr_w:
                        x = 0
                        y += wrap['h']

                    if len(wraps) == 1 and wrap['align'] == 'center':
                        rect.midtop = (self.screen_rect.centerx,
                                       self.screen_rect.bottom + y)
                    else:
                        rect.topleft = (x + w * 3, self.screen_rect.bottom + y)
                    wrap['rect'] = rect
                    wrap['x'] = x
                    wrap['y'] = y
                    if False:
                        print("\n{}: {},".format('x', wrap['x']), end='')
                        print("{}: {},".format('y', wrap['y']), end='')
                        print("{}: {},".format('w', wrap['w']), end='')
                        print("{}: {}".format('h', wrap['h']))
                        print(wrap['text'])
                    self.parsed.append(wrap)

                    x += wrap['w1']
            y += wrap['h']
        # exit()
        print('done parsing')

        self.start_y = 0 - self.screen_rect.h + self.default_style['h']

        self.y = int(self.start_y)

        self.end_y = (-sum(p['h'] for p in self.parsed if p['x'] == 0) -
                      self.default_style['h'] * 2)
示例#2
0
}

mystring = ("Hello World!")

# you can start with an already formatted string
print()
print("This is our test text:")
print('"{}"'.format(mystring))

# you can format plain text with a given style or the default one
print("\nThis is our text with style:")
newstring = Style.stylize(mystring, old_style)
print('"{}"'.format(newstring))

# Style.split returns a string without its style and a separate style dict
text, newstyle = Style.split(newstring)
print("\nThe style format is a dictionary like this one:")
pprint(newstyle)
print("\nThis is formatted text with its style removed.")
print("It shoul be equal to the text we started with.")
print('"{}"'.format(text))
assert(text == mystring)
assert(newstyle == old_style)

newstyle['color'] = (255, 255, 255)
newstyle['size'] = 18
# you can set a default style from a dict
Style.set_default(newstyle)

# or you can set a default style from a formatted string.
# both will do the same.
示例#3
0
文件: sftext.py 项目: LukeMS/sftext
    def parse_text(self):
        self.parsed = []
        scr_w = self.screen_rect.width

        self.default_style = Style.default_style
        self.default_style['font_obj'] = self.fonts.load(
            self.default_style['font'], self.default_style['size'])
        self.default_style['w'], self.default_style['h'] = (
            self.default_style['font_obj'].size(' '))

        y = 0
        for line in self.text.splitlines():
            x = 0
            for style in line.split("{style}"):

                text, styled_txt = Style.split(style)

                self.set_font(styled_txt)
                font = styled_txt['font_obj']

                w, h = styled_txt['w'], styled_txt['h'] = font.size(' ')
                # determine the amount of space needed to render text

                wraps = self.wrap_text(text, scr_w, x, styled_txt)

                for wrap in wraps:
                    rect = pygame.Rect((0, 0), font.size(wrap['text']))

                    if (x + wrap['w1'] + w * 3) > scr_w:
                        x = 0
                        y += wrap['h']

                    if len(wraps) == 1 and wrap['align'] == 'center':
                        rect.midtop = (
                            self.screen_rect.centerx,
                            self.screen_rect.bottom + y)
                    else:
                        rect.topleft = (
                            x + w * 3,
                            self.screen_rect.bottom + y)
                    wrap['rect'] = rect
                    wrap['x'] = x
                    wrap['y'] = y
                    if False:
                        print("\n{}: {},".format('x', wrap['x']), end='')
                        print("{}: {},".format('y', wrap['y']), end='')
                        print("{}: {},".format('w', wrap['w']), end='')
                        print("{}: {}".format('h', wrap['h']))
                        print(wrap['text'])
                    self.parsed.append(wrap)

                    x += wrap['w1']
            y += wrap['h']
        # exit()
        print('done parsing')

        self.start_y = 0 - self.screen_rect.h + self.default_style['h']

        self.y = int(self.start_y)

        self.end_y = (
            -sum(p['h'] for p in self.parsed if p['x'] == 0)
            - self.default_style['h'] * 2)