def SysFont(name, size, bold=0, italic=0): """pygame.font.SysFont(name, size, bold=0, italic=0) -> Font create a pygame Font from system font resources This will search the system fonts for the given font name. You can also enable bold or italic styles, and the appropriate system font will be selected if available. This will always return a valid Font object, and will fallback on the builtin pygame font if the given font is not found. Name can also be a comma separated list of names, in which case set of names will be searched in order. Pygame uses a small set of common font aliases, if the specific font you ask for is not available, a reasonable alternative may be used. """ import pygame.font if not Sysfonts: initsysfonts() fontname = None if name: allnames = name for name in allnames.split(','): origbold = bold origitalic = italic name = _simplename(name) styles = Sysfonts.get(name) if not styles: styles = Sysalias.get(name) if styles: while not fontname: fontname = styles.get((bold, italic)) if italic: italic = 0 elif bold: bold = 0 elif not fontname: fontname = styles.values()[0] if fontname: break font = pygame.font.Font(fontname, size) if name: if origbold and not bold: font.set_bold(1) if origitalic and not italic: font.set_italic(1) else: if bold: font.set_bold(1) elif italic: font.set_italic(1) return font
def font_constructor(fontpath, size, bold, italic): import pygame.font font = pygame.font.Font(fontpath, size) if bold: font.set_bold(1) if italic: font.set_italic(1) return font
def file_master(screen, file_names, place, marker, menu_items, msg, down, button_op): paint_screen(gl.BLACK) show_message(msg, down, 10, ("bold", "transparent")) font = pygame.font.Font(gl.FONT_NAME, 9) font.set_bold(1) (esc_rect, esc_font) = close_button(screen) font_height = font.size(file_names[0])[1] screen_height = screen.get_height() name_max = 16 max_file_width = 116 line = 65 # leave room at top of screen for other stuff col = 5 count = 0 back_rect = forward_rect = sort_rect = junk_rect() for name in file_names[place:]: count = count + 1 place = place + 1 marker = marker + 1 if count >= gl.MAX_SCREEN_FILES or place >= len(file_names): ren_name = os.path.basename(name) if len(ren_name) > name_max: ren_name = ren_name[:name_max] + '...' # truncate if ren_name[-4:] == '....': ren_name = ren_name[:-1] # 3 .'s are enough ren = font.render(ren_name, 1, gl.MSG_COLOR, gl.BLACK) if (place + 1) < len(file_names): forward_rect = imgv_button(screen, " Next ", 10, 18, "topright") if (((place + 1) - gl.MAX_SCREEN_FILES) > 1): back_rect = imgv_button(screen, " Previous ", 10, 18, "topleft") if not gl.SORT_HIT: sort_rect = imgv_button(screen, " Sort ", 13, 42, "midtop") ren_rect = ren.get_rect() ren_rect[0] = col ren_rect[1] = line menu_items.append((ren_rect, name)) screen.blit(ren, ren_rect) update(ren_rect) return (file_names, menu_items, 1, place, marker, forward_rect, back_rect, sort_rect) ren_name = os.path.basename(name) if len(ren_name) > name_max: ren_name = ren_name[:name_max] + '...' if ren_name[-4:] == '....': ren_name = ren_name[:-1] ren = font.render(ren_name, 1, gl.MSG_COLOR, gl.BLACK) ren_rect = ren.get_rect() ren_rect[0] = col ren_rect[1] = line menu_items.append((ren_rect, name)) screen.blit(ren, ren_rect) line = line + 12 if (line + font_height) >= (screen_height - 15): line = 65 col = col + max_file_width update(ren_rect) return (file_names, menu_items, 0, place, marker, forward_rect, back_rect, sort_rect)
def set_text(self, text): self.text = text font = self.font font.set_underline(self.underline) font.set_bold(self.bold) font.set_italic(self.italic) # required for font.render to function properly when None is # given for background if self.background == None: self.surface = font.render(text, self.antialias, self.color).convert_alpha() else: self.surface = font.render(text, self.antialias, self.color, self.background).convert()
def get_filter_info(screen, menu_items): filt_ops = ["1) Start with", "2) Do not start with", "3) End with", "4) Do not end with", "5) Contain", "6) Do not contain", "7) View filter", "8) Erase filter", "9) Done"] line = 95 # start position of menu from top of screen for op in filt_ops: font = pygame.font.Font(gl.FONT_NAME, 18) font.set_bold(1) ren = font.render(op, 1, gl.BLUE) ren_rect = ren.get_rect() ren_rect[0] = 20 ren_rect[1] = line menu_items.append((ren_rect, op)) screen.blit(ren, ren_rect) line = line + 30 update(ren_rect) return (menu_items, filt_ops)
def drawBackground(): global background, backgroundOffset global sponsorLogo, sponsorLogoOffset if not background: image = random.choice([ resources.get('bck1_1024x768.jpg'), resources.get('bck2_1024x768.jpg'), resources.get('bck3_1024x768.jpg'), resources.get('bck4_1024x768.jpg'), resources.get('bck5_1024x768.jpg'), ]) background = pygame.image.load(image).convert_alpha() backgroundOffset = ( (gdata.screen.get_width() - background.get_width()) / 2, (gdata.screen.get_height() - background.get_height()) / 2, ) if not sponsorLogo: sponsorLogo = pygame.image.load(resources.get("sponsor_logo.png")).convert_alpha() sponsorLogoOffset = ( (gdata.screen.get_width() - 5 - sponsorLogo.get_width()), (gdata.screen.get_height() - 5 - sponsorLogo.get_height()), ) font = pygame.font.Font(resources.get('fonts/DejaVuLGCSans.ttf'), 12) font.set_bold(1) color = 0x40, 0x70, 0x40 # gdata.screen.blit(background, backgroundOffset) gdata.screen.blit(sponsorLogo, sponsorLogoOffset) img = font.render(_("Server sponsored by:"), 1, (0xc0, 0xc0, 0xc0)) gdata.screen.blit(img, (sponsorLogoOffset[0], sponsorLogoOffset[1] - img.get_height() - 2)) # screen.fill((0x00, 0x00, 0x00)) # OSCI version img = font.render(_('Outer Space %s') % ige.version.versionString, 1, color) gdata.screen.blit(img, (5, gdata.screen.get_height() - 4 * img.get_height() - 5)) # Pygame version img = font.render(_('Pygame %s') % pygame.version.ver, 1, color) gdata.screen.blit(img, (5, gdata.screen.get_height() - 3 * img.get_height() - 5)) # Python version img = font.render(_('Python %s') % sys.version, 1, color) gdata.screen.blit(img, (5, gdata.screen.get_height() - 2 * img.get_height() - 5)) # Video driver w, h = pygame.display.get_surface().get_size() d = pygame.display.get_surface().get_bitsize() img = font.render(_('Video Driver: %s [%dx%dx%d]') % (pygame.display.get_driver(), w, h, d), 1, color) gdata.screen.blit(img, (5, gdata.screen.get_height() - 1 * img.get_height() - 5))
def edit_menu(screen, file, menu_items): font = pygame.font.Font(gl.FONT_NAME, 18) font.set_bold(1) men_ops = ["1) Delete image", "2) Set as wallpaper", "3) Open in external viewer", "4) Preferences"] line = 65 col = 20 for m in men_ops: ren_name = m ren = font.render(ren_name, 1, gl.BLUE) ren_rect = ren.get_rect() ren_rect[0] = col ren_rect[1] = line menu_items.append((ren_rect, m)) screen.blit(ren, ren_rect) line = line + 30 update(ren_rect) close_button(screen) return (menu_items, men_ops)
def drawBackground(): global background, backgroundOffset if not background: image = random.choice([ 'res/bck1_1024x768.jpg', 'res/bck2_1024x768.jpg', 'res/bck3_1024x768.jpg', 'res/bck4_1024x768.jpg', 'res/bck5_1024x768.jpg', ]) background = pygame.image.load(image).convert_alpha() backgroundOffset = ( (screen.get_width() - background.get_width()) / 2, (screen.get_height() - background.get_height()) / 2, ) font = pygame.font.Font('res/fonts/DejaVuLGCSans.ttf', 12) font.set_bold(1) color = 0x40, 0x70, 0x40 # screen.blit(background, backgroundOffset) # screen.fill((0x00, 0x00, 0x00)) # OSCI version img = font.render(_('OSCI %s') % osci.versionString, 1, color) screen.blit(img, (5, screen.get_height() - 4 * img.get_height() - 5)) # Pygame version img = font.render(_('Pygame %s') % pygame.version.ver, 1, color) screen.blit(img, (5, screen.get_height() - 3 * img.get_height() - 5)) # Python version img = font.render(_('Python %s') % sys.version, 1, color) screen.blit(img, (5, screen.get_height() - 2 * img.get_height() - 5)) # Video driver w, h = pygame.display.get_surface().get_size() d = pygame.display.get_surface().get_bitsize() img = font.render( _('Video Driver: %s [%dx%dx%d]') % (pygame.display.get_driver(), w, h, d), 1, color) screen.blit(img, (5, screen.get_height() - 1 * img.get_height() - 5))
def drawBackground(): global background, backgroundOffset if not background: image = random.choice([ 'res/bck1_1024x768.jpg', 'res/bck2_1024x768.jpg', 'res/bck3_1024x768.jpg', 'res/bck4_1024x768.jpg', 'res/bck5_1024x768.jpg', ]) background = pygame.image.load(image).convert_alpha() backgroundOffset = ( (screen.get_width() - background.get_width()) / 2, (screen.get_height() - background.get_height()) / 2, ) font = pygame.font.Font('res/fonts/DejaVuLGCSans.ttf', 12) font.set_bold(1) color = 0x40, 0x70, 0x40 # screen.blit(background, backgroundOffset) # screen.fill((0x00, 0x00, 0x00)) # OSCI version img = font.render(_('OSCI %s') % osci.versionString, 1, color) screen.blit(img, (5, screen.get_height() - 4 * img.get_height() - 5)) # Pygame version img = font.render(_('Pygame %s') % pygame.version.ver, 1, color) screen.blit(img, (5, screen.get_height() - 3 * img.get_height() - 5)) # Python version img = font.render(_('Python %s') % sys.version, 1, color) screen.blit(img, (5, screen.get_height() - 2 * img.get_height() - 5)) # Video driver w, h = pygame.display.get_surface().get_size() d = pygame.display.get_surface().get_bitsize() img = font.render(_('Video Driver: %s [%dx%dx%d]') % (pygame.display.get_driver(), w, h, d), 1, color) screen.blit(img, (5, screen.get_height() - 1 * img.get_height() - 5))
def font_options(font_ops, font): "lets you have bold and/or underline and/or italic fonts" if type(font_ops[0]) is StringType: if font_ops[0] == "bold": font.set_bold(1) if font_ops[0] == "underline": font.set_underline(1) if font_ops[0] == "italic": font.set_italic(1) if font_ops[0] == "transparent": gl.TEXT_TRANSPARENT = 1 else: if font_ops[0][0] == "bold": font.set_bold(1) if font_ops[0][0] == "underline": font.set_underline(1) if font_ops[0][0] == "italic": font.set_italic(1) if font_ops[0][0] == "transparent": gl.TEXT_TRANSPARENT = 1 if len(font_ops[0]) > 1: if font_ops[0][1] == "underline": font.set_underline(1) if font_ops[0][1] == "bold": font.set_bold(1) if font_ops[0][1] == "italic": font.set_italic(1) if font_ops[0][1] == "transparent": gl.TEXT_TRANSPARENT = 1 if len(font_ops[0]) > 2: if font_ops[0][2] == "underline": font.set_underline(1) if font_ops[0][2] == "bold": font.set_bold(1) if font_ops[0][2] == "italic": font.set_italic(1) if font_ops[0][2] == "transparent": gl.TEXT_TRANSPARENT = 1
def show_dirs(screen, num_imgs, file): wait_cursor() if platform == 'win32': try: os.chdir(gl.DRIVE + ":") except: pass # Probably an OSError from not having a cd in the drive slash, get_curdir = os.sep, os.getcwd() fg_color = gl.SILVER font_size = 10 font = pygame.font.Font(gl.FONT_NAME, font_size) font.set_bold(1) # very important line = 55 name_max = 16 # dir name max menu_items = [] if get_curdir[-1] == slash: curdir = get_curdir else: curdir = get_curdir + slash paint_screen(screen, gl.BLACK) screen_height = screen.get_height() if not gl.BEEN_THERE_DONE_THAT: show_message(screen, "You can type in a directory number or shortcut (L/T/A/D/C/V/S/Q) instead of clicking: _", "bottom", 11) else: show_message(screen, "Directory number or shortcut: _", "bottom", 11) curdir_msg = check_truncate(screen.get_width(), curdir) all_files = os.listdir('.') all_files.sort() n_dirs = len([d for d in all_files if os.path.isdir(d)]) all_files = [f for f in all_files if not os.path.isdir(f)] n_files = len(all_files) all_images = get_imgs(os.getcwd(), 0) n_images = len(all_images) get_movies = lambda x, y: [i.upper().endswith(y) for i in all_images].count(True) n_movies = get_movies(all_images, ".MPG") + get_movies(all_images, ".MPEG") dirs_text = "Directories" files_text = "Files" images_text = "Images" movies_text = "MPEGs" if n_dirs == 1: dirs_text = "Directory" # unplural if n_files == 1: files_text = "File" if n_images == 1: images_text = "Image" if n_movies == 1: movies_text = "MPEG" files_msg = "[%d %s. %d %s. %d %s. %d %s]" % (n_dirs, dirs_text, n_files, files_text, n_images - n_movies, images_text, n_movies, movies_text) curdir_msg_wpos = (screen.get_width() / 2 - font.size(curdir_msg)[0] / 2) - font.size(files_msg)[0] / 2 + 10 files_msg_wpos = screen.get_width() / 2 + font.size(curdir_msg)[0] / 2 - font.size(files_msg)[0] / 2 + 20 if not gl.REFRESH_IMG_COUNT and gl.CACHE_DIR_OK: dirs = gl.CACHE_DIRS gl.REFRESH_IMG_COUNT = 1 set_caption(curdir) show_message(screen, curdir_msg, (curdir_msg_wpos, 4), 10, ("bold")) show_message(screen, files_msg, (files_msg_wpos, 4), 10) else: show_message(screen, curdir_msg, (curdir_msg_wpos, 4), 10, ("bold")) show_message(screen, files_msg, (files_msg_wpos, 4), 10) set_caption(curdir) dirs = os.listdir(curdir) dirs.sort() dirs = strip_dirs(dirs) # ensure the root dir and last dir items go at top dirs.insert(0, "..") dirs.insert(0, slash) gl.CACHE_DIRS = dirs ren_load_rect = imgv_button(screen, " (L)oad ", 0, 18, "topleft") ren_load_subdirs_rect = imgv_button(screen, " Subdirs (T)oo ", 62, 18, "topleft") if platform == 'win32': ren_drive_rect = imgv_button(screen, " Change (D)rive ", 271, 18, "topleft") dirpl_rect = imgv_button(screen, " (A)dd To Playlist ", 160, 18, "topleft") untag_all_rect = imgv_button(screen, " (C)lear Tags ", 380, 18, "topleft") view_tagged_rect = imgv_button(screen, " (V)iew Tags ", 472, 18, "topleft") filter_rect = imgv_button(screen, " (S)earch ", 559, 18, "topleft") col = 10 show_message(screen, "Right-Click directories to tag multiple directories to load. Ctrl+Left-Click to untag.", (10, 40), 10) if gl.MULT_DIRS != []: show_message(screen, "[Dirs tagged: %s]" % len(gl.MULT_DIRS), (440, 40), 10, "bold") if gl.FILTER_COMMAND != {}: show_message(screen, "[Filter: on]", (560, 40), 10, "bold") # add numbers to directory names if gl.ADDED_DIR_NUMS == 0 and dirs[0] != '*': gl.ADDED_DIR_NUMS = 1 for i, d in enumerate(dirs): dirs[i] = '*' + str(i) + gl.DIRNUMSEP + d for d in dirs: d = d[1:] # strip out the '*' marker if d[3:] == slash: if gl.DIRNUM_COLORS: dmsg = d else: ren = font.render(d, 1, gl.MSG_COLOR) else: if len(d) > name_max: if gl.DIRNUM_COLORS: dmsg = truncate_name(d, name_max) else: ren = font.render(truncate_name(d, name_max), 1, gl.MSG_COLOR) else: if gl.DIRNUM_COLORS: dmsg = d + slash else: ren = font.render(d + slash, 1, gl.MSG_COLOR) # print directory names on screen, wrapping if necessary font_height = font.size(' '.join(d.split(' ')[1:]))[1] if (line + font_height) >= (screen_height - 10): line = 55 # reset to beginning of screen col = col + (name_max + 130) # go to next column if gl.DIRNUM_COLORS: before_color = gl.MSG_COLOR if gl.MSG_COLOR == gl.SILVER: gl.MSG_COLOR = (142, 142, 142) else: gl.MSG_COLOR = gl.SILVER if before_color == gl.WHITE: ren_rect = show_message(screen, dmsg, (col, line), font_size, ("bold"), (len(dmsg[:dmsg.index(gl.DIRNUMSEP) + 1]), gl.SILVER)) else: ren_rect = show_message(screen, dmsg, (col, line), font_size, ("bold"), (len(dmsg[:dmsg.index(gl.DIRNUMSEP) + 1]), before_color)) else: ren_rect = ren.get_rect() ren_rect[0] = col ren_rect[1] = line screen.blit(ren, ren_rect) update(ren_rect) line = line + 12 menu_items.append((ren_rect, d)) if gl.DIRNUM_COLORS: gl.MSG_COLOR = before_color normal_cursor() pygame.event.set_blocked(MOUSEMOTION) while 1: event = pygame.event.poll() pygame.time.wait(1) cursor = pygame.mouse.get_pos() (esc_rect, close_font) = close_button(screen) hover_fx(screen, curdir, menu_items, cursor) if platform == 'win32':# hover_cursor(cursor, [ren_load_rect, ren_load_subdirs_rect, ren_drive_rect, dirpl_rect, untag_all_rect, view_tagged_rect, filter_rect, esc_rect] + [x[0] for x in menu_items]) hover_button(ren_load_rect, cursor, screen, " (L)oad ", 0, 18, "topleft") hover_button(ren_load_subdirs_rect, cursor, screen, " Subdirs (T)oo ", 62, 18, "topleft") if platform == 'win32': hover_button(ren_drive_rect, cursor, screen, " Change (D)rive ", 271, 18, "topleft") hover_button(dirpl_rect, cursor, screen, " (A)dd To Playlist ", 160, 18, "topleft") hover_button(untag_all_rect, cursor, screen, " (C)lear Tags ", 380, 18, "topleft") hover_button(view_tagged_rect, cursor, screen, " (V)iew Tags ", 472, 18, "topleft") hover_button(filter_rect, cursor, screen, " (S)earch ", 559, 18, "topleft") if left_click(event): for item in menu_items: if item[0].collidepoint(cursor): if pygame.mouse.get_pressed()[0] and (pygame.key.get_pressed()[K_LCTRL] or\ pygame.key.get_pressed()[K_RCTRL]): try: # untag directory gl.MULT_DIRS.remove(os.getcwd() + slash + ' '.join(item[1].split(' ')[1:])) show_message(screen, " " * 30, (440, 40), 10, ("bold")) show_message(screen, "[Dirs tagged: %s]" % len(gl.MULT_DIRS), (440, 40), 10, "bold") except: pass else: # (normal mode) change to a single directory and load its images (num_imgs, file) = do_change_dir(screen, num_imgs, file, item[1]) return (num_imgs, file) if right_click(event): for item in menu_items: if item[0].collidepoint(cursor): # tag directory if os.getcwd()[-1] != slash: gl.MULT_DIRS.append(os.getcwd() + slash + ' '.join(item[1].split(' ')[1:])) else: gl.MULT_DIRS.append(os.getcwd() + ' '.join(item[1].split(' ')[1:])) show_message(screen, " " * 30, (440, 40), 10, ("bold")) show_message(screen, "[Dirs tagged: %s]" % len(gl.MULT_DIRS), (440, 40), 10, "bold") # allow number keys to be used to change directories dirnum = None if event.type == KEYDOWN and event.key in (K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_8, K_9, K_0, K_KP1, K_KP2, K_KP3, K_KP4, K_KP5, K_KP6, K_KP7, K_KP8, K_KP9, K_KP0): dirnum = get_dirnum(screen, event.key) if dirnum != 'backspaced': for item in menu_items: if item[1].startswith(str(dirnum)): (num_imgs, file) = do_change_dir(screen, num_imgs, file, item[1]) return (num_imgs, file) break if hit_key(event, K_RETURN) or hit_key(event, K_SPACE) or hit_key(event, K_l): (num_imgs, file) = do_load_dir() break if hit_key(event, K_t): # load subdirs on keypress 't' (num_imgs, file) = do_subdirs_too() break if hit_key(event, K_s): # search command_get_filter_info(screen) (num_imgs, file) = show_dirs(screen, num_imgs, file) break if hit_key(event, K_d): # change drives if platform == 'win32': gl.WAS_IN_CHANGE_DRIVES = 1 (num_imgs, file) = do_change_drive(screen, num_imgs, file) gl.WAS_IN_CHANGE_DRIVES = 0 break if hit_key(event, K_c): # clear tag list do_untag(screen) if hit_key(event, K_v): # view tagged dirs do_view_tagged(screen, num_imgs, file) break # break main loop to display properly if hit_key(event, K_a): # add curdir to playlist command_add_to_play_list(screen, curdir) return (num_imgs, file) if hit_key(event, K_ESCAPE): gl.ESCAPED = 1 gl.ADDED_DIR_NUMS = 0 break if left_click(event): if esc_rect.collidepoint(cursor): gl.ESCAPED = 1 gl.ADDED_DIR_NUMS = 0 break if ren_load_rect.collidepoint(cursor): # load current dir (num_imgs, file) = do_load_dir() break if ren_load_subdirs_rect.collidepoint(cursor): # load subdirs too (num_imgs, file) = do_subdirs_too() break if platform == 'win32': if ren_drive_rect.collidepoint(cursor): gl.WAS_IN_CHANGE_DRIVES = 1 (num_imgs, file) = do_change_drive(screen, num_imgs, file) gl.WAS_IN_CHANGE_DRIVES = 0 break if dirpl_rect.collidepoint(cursor): command_add_to_play_list(screen, curdir) return (num_imgs, file) if untag_all_rect.collidepoint(cursor): do_untag(screen) if view_tagged_rect.collidepoint(cursor): do_view_tagged(screen, num_imgs, file) break # break main loop to display properly if filter_rect.collidepoint(cursor): command_get_filter_info(screen) (num_imgs, file) = show_dirs(screen, num_imgs, file) break check_quit(event) gl.CACHE_DIR_OK = 1 return (num_imgs, file)
# pygame.font module # https://www.pygame.org/docs/ref/font.html # # Have an outline of text in Pygame # https://stackoverflow.com/questions/60987711/have-an-outline-of-text-in-pygame # # GitHub - PyGameExamplesAndAnswers - Text and font # https://github.com/Rabbid76/PyGameExamplesAndAnswers/blob/master/documentation/pygame/pygame_text_and_font.md import pygame import pygame.font pygame.init() font = pygame.font.SysFont(None, 40) font.set_bold(True) width, height = 400, 300 screen = pygame.display.set_mode((width, height)) clock = pygame.time.Clock() textRect = pygame.Rect(0, 0, 0, 0) def text_speech(font, text: str, color, x, y, bold: bool, outline: int): global textRect if outline > 0: outlineSurf = font.render(text, True, (255, 0, 0)) outlineSize = outlineSurf.get_size() textSurf = pygame.Surface( (outlineSize[0] + outline * 2, outlineSize[1] + 2 * outline)) textRect = textSurf.get_rect()