def setUp(self): sprite_map_set_limits(100000, 100) self.sprites = OrderedDict() def send_to_gpu(x, y, z, data): self.sprites[(x, y, z)] = data set_send_sprite_to_gpu(send_to_gpu) set_logical_dpi(96.0, 96.0) get_logical_dpi((96.0, 96.0)) self.cell_width, self.cell_height = set_font_family() prerender() self.assertEqual([k[0] for k in self.sprites], [0, 1, 2, 3, 4])
def setup_for_testing(family='monospace', size=11.0, dpi=96.0): from kitty.utils import get_logical_dpi opts = defaults._replace(font_family=family) sprites = {} def send_to_gpu(x, y, z, data): sprites[(x, y, z)] = data sprite_map_set_limits(100000, 100) set_send_sprite_to_gpu(send_to_gpu) set_logical_dpi(dpi, dpi) get_logical_dpi((dpi, dpi)) cell_width, cell_height = set_font_family(opts, override_font_size=size) prerender() return sprites, cell_width, cell_height
def set_font_family(opts, override_font_size=None): global current_font_family, current_font_family_name, cff_size, cell_width, cell_height, CharTexture, baseline global underline_position, underline_thickness size_in_pts = override_font_size or opts.font_size current_font_family = get_font_files(opts) current_font_family_name = opts.font_family dpi = get_logical_dpi() cff_size = ceil_int(64 * size_in_pts) cff_size = { 'width': cff_size, 'height': cff_size, 'hres': int(dpi[0]), 'vres': int(dpi[1]) } install_symbol_map(opts.symbol_map) for fobj in chain(current_font_family.values(), symbol_map.values()): set_char_size(fobj.face, **cff_size) face = current_font_family['regular'].face cell_width = face.calc_cell_width() cell_height = font_units_to_pixels(face.height, face.units_per_EM, size_in_pts, dpi[1]) cell_height = adjust_line_height(cell_height, opts.adjust_line_height) baseline = font_units_to_pixels(face.ascender, face.units_per_EM, size_in_pts, dpi[1]) underline_position = min( baseline - font_units_to_pixels( face.underline_position, face.units_per_EM, size_in_pts, dpi[1]), cell_height - 1) underline_thickness = font_units_to_pixels(face.underline_thickness, face.units_per_EM, size_in_pts, dpi[1]) CharTexture = ctypes.c_ubyte * (cell_width * cell_height) font_for_text.cache_clear() alt_face_cache.clear() return cell_width, cell_height
def set_font_family(opts, override_font_size=None, ignore_dpi_failure=False): global cell_width, cell_height, baseline, CellTexture, WideCellTexture, underline_thickness, underline_position try: dpi = get_logical_dpi() except Exception: if not ignore_dpi_failure: raise dpi = (72, 72) # Happens when running via develop() in an ssh session dpi = sum(dpi) / 2.0 font_size = override_font_size or opts.font_size all_fonts = create_font_map(coretext_all_fonts()) for (bold, italic), attr in attr_map.items(): main_font[(bold, italic)] = get_face(all_fonts, getattr(opts, attr), opts.font_family, font_size, dpi, bold, italic) install_symbol_map(all_fonts, opts.symbol_map, font_size, dpi) mf = main_font[(False, False)] cell_width, cell_height = mf.cell_size() cell_height = adjust_line_height(cell_height, opts.adjust_line_height) CellTexture = ctypes.c_ubyte * (cell_width * cell_height) WideCellTexture = ctypes.c_ubyte * (2 * cell_width * cell_height) baseline = int(round(mf.ascent)) underline_position = int(round(baseline - mf.underline_position)) underline_thickness = ceil_int(mf.underline_thickness) return cell_width, cell_height
def set_font_family(family, size_in_pts): global current_font_family, current_font_family_name, cff_size, cell_width, cell_height, CharTexture, baseline global underline_position, underline_thickness if current_font_family_name != family or cff_size != size_in_pts: find_font_for_character.cache_clear() current_font_family = get_font_files(family) current_font_family_name = family dpi = get_logical_dpi() cff_size = ceil_int(64 * size_in_pts) cff_size = { 'width': cff_size, 'height': cff_size, 'hres': int(dpi[0]), 'vres': int(dpi[1]) } for fobj in current_font_family.values(): set_char_size(fobj.face, **cff_size) face = current_font_family['regular'].face cell_width = calc_cell_width(current_font_family['regular'], face) cell_height = font_units_to_pixels(face.height, face.units_per_EM, size_in_pts, dpi[1]) baseline = font_units_to_pixels(face.ascender, face.units_per_EM, size_in_pts, dpi[1]) underline_position = min( baseline - font_units_to_pixels(face.underline_position, face.units_per_EM, size_in_pts, dpi[1]), cell_height - 1) underline_thickness = font_units_to_pixels(face.underline_thickness, face.units_per_EM, size_in_pts, dpi[1]) CharTexture = ctypes.c_ubyte * (cell_width * cell_height) font_for_char.cache_clear() alt_face_cache.clear() return cell_width, cell_height
def set_font_family(opts, override_font_size=None, ignore_dpi_failure=False): global cell_width, cell_height, baseline, CellTexture, WideCellTexture, underline_thickness, underline_position try: dpi = get_logical_dpi() except Exception: if not ignore_dpi_failure: raise dpi = (72, 72) # Happens when running via develop() in an ssh session dpi = sum(dpi) / 2.0 attr_map = { (False, False): 'font_family', (True, False): 'bold_font', (False, True): 'italic_font', (True, True): 'bold_italic_font' } def get_family(bold, italic): ans = getattr(opts, attr_map[(bold, italic)]) if ans.lower() == 'monospace': ans = 'Menlo' if ans == 'auto' and (bold or italic): ans = get_family(False, False) return ans font_size = override_font_size or opts.font_size for bold in (False, True): for italic in (False, True): main_font[(bold, italic)] = Face(get_family(bold, italic), bold, italic, True, font_size, dpi) install_symbol_map(opts.symbol_map, font_size, dpi) mf = main_font[(False, False)] cell_width, cell_height = mf.cell_size() CellTexture = ctypes.c_ubyte * (cell_width * cell_height) WideCellTexture = ctypes.c_ubyte * (2 * cell_width * cell_height) baseline = int(round(mf.ascent)) underline_position = int(round(baseline - mf.underline_position)) underline_thickness = ceil_int(mf.underline_thickness) return cell_width, cell_height
def thickness(level=1, horizontal=True): dpi = get_logical_dpi()[0 if horizontal else 1] pts = (0.001, 1, 1.5, 2)[level] return int(math.ceil(pts * dpi / 72.0))