Hans – Public Notes

Smaller nerd font icons when patching a font

    def get_scale_factors(self, sym_dim, stretch):
        """Get scale in x and y as tuple"""
        # It is possible to have empty glyphs, so we need to skip those.
        if not sym_dim["width"] or not sym_dim["height"]:
            return (1.0, 1.0)

        target_width = self.font_dim["width"] * self.get_target_width(stretch)
        scale_ratio_x = target_width / sym_dim["width"]

        # font_dim['height'] represents total line height, keep our symbols sized based upon font's em
        # Use the font_dim['height'] only for explicit 'y' scaling (not 'pa')
        target_height = self.font_dim["height"] if "^" in stretch else self.font_dim["iconheight"]
        target_height *= 1.0 - self.font_dim["ypadding"]
        scale_ratio_y = target_height / sym_dim["height"]

        if "pa" in stretch:
            # We want to preserve x/y aspect ratio, so find biggest scale factor that allows symbol to fit
            scale_ratio_x = min(scale_ratio_x, scale_ratio_y)
            if not self.args.single and not "!" in stretch:
                # non monospaced fonts just scale down on 'pa', not up
                scale_ratio_x = min(scale_ratio_x, 1.0)
            scale_ratio_y = scale_ratio_x
        else:
            # Keep the not-stretched direction
            if not "x" in stretch:
                scale_ratio_x = 1.0
            if not "y" in stretch:
                scale_ratio_y = 1.0

        return (scale_ratio_x, scale_ratio_y)