Skip to content

Commit

Permalink
Split scale option to x and y scale. Add force_narrow option that for…
Browse files Browse the repository at this point in the history
…ce to resize glyph narrow width.
  • Loading branch information
krymtkts committed Jul 4, 2021
1 parent ec746e1 commit 7653e06
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
51 changes: 28 additions & 23 deletions fontmerger/fontmerger.py
Expand Up @@ -19,7 +19,10 @@ def __init__(self, **kwargs):
self.codepoints = []
self.remap_start_point = None
self.scale = 1.0
self.scale_x = 1.0
self.scale_y = 1.0
self.adjust_position = False
self.force_narrow = False

for key, value in kwargs.items():
setattr(self, key, value)
Expand Down Expand Up @@ -232,7 +235,6 @@ def merge_one(self, ctx):

def merge_one_range(self, font, ctx):
unicode_range = ctx.unicode_range
glyph_scale = ctx.scale
ext_start = 0
ext_end = 0
if len(unicode_range) >= 1:
Expand All @@ -243,45 +245,46 @@ def merge_one_range(self, font, ctx):
remap_start_point = ctx.remap_start_point
if remap_start_point is not None:
base_start = int(remap_start_point, 16)
base_font_height = get_height(self.base_font)
font_height = get_height(font)
scale = self.transform_scale(font, ctx)

# scale transform
scale_ratio_x = round(float(self.base_font.em) / font.em, 3)
scale_ratio_y = round(float(base_font_height) / font_height, 3)
scale = psMat.scale(scale_ratio_x * glyph_scale,
scale_ratio_y * glyph_scale)
if ext_start == 0:
font.selection.all()
else:
font.selection.select(('ranges', 'unicode'), ext_start, ext_end)

self.do_merge_one(font, base_start, ext_start,scale,glyph_scale,ctx.adjust_position)
self.do_merge_one(font, base_start, ext_start, scale, ctx)

def merge_one_codepoint(self, font, ctx):
codepoints = ctx.codepoints
glyph_scale = ctx.scale
if not codepoints:
return
ext_start = int(codepoints[0], 16)
base_start = ext_start
remap_start_point = ctx.remap_start_point
if remap_start_point is not None:
base_start = int(remap_start_point, 16)
base_font_height = get_height(self.base_font)
font_height = get_height(font)
scale = self.transform_scale(font, ctx)

# scale transform
scale_ratio_x = round(float(self.base_font.em) / font.em, 3)
scale_ratio_y = round(float(base_font_height) / font_height, 3)
scale = psMat.scale(scale_ratio_x * glyph_scale,
scale_ratio_y * glyph_scale)
for cp in codepoints:
font.selection.select(('more', 'unicode'), cp)

self.do_merge_one(font, base_start, ext_start,scale,glyph_scale,ctx.adjust_position)
self.do_merge_one(font, base_start, ext_start, scale, ctx)

def transform_scale(self, font, ctx):
base_font_height = get_height(self.base_font)
font_height = get_height(font)
# scale transform
if ctx.force_narrow:
scale_ratio_x = round(float(self.base_font.em) / font.em / 2, 3)
else:
scale_ratio_x = round(float(self.base_font.em) / font.em, 3)
scale_ratio_y = round(float(base_font_height) / font_height, 3)
scale_x = ctx.scale_x if ctx.scale_x != 1.0 else ctx.scale
scale_y = ctx.scale_y if ctx.scale_y != 1.0 else ctx.scale
return psMat.scale(scale_ratio_x * scale_x,
scale_ratio_y * scale_y)

def do_merge_one(self, font, base_start, ext_start,scale,glyph_scale,adjust_position):
def do_merge_one(self, font, base_start, ext_start, scale, ctx):
# copy and transform glyphs
for glyph in list(font.selection.byGlyphs):
index = base_start + (glyph.encoding - ext_start)
Expand All @@ -297,14 +300,16 @@ def do_merge_one(self, font, base_start, ext_start,scale,glyph_scale,adjust_posi
move_y = 0
if self.hints.ascent < info.ymax:
move_y = self.hints.ascent - info.ymax
if glyph_scale < 1.0 and adjust_position:
move_x += info.width * (1.0 - glyph_scale) / 2.0
move_y -= info.height * (1.0 - glyph_scale) / 2.0
if ctx.adjust_position:
if ctx.scale != 1.0 or ctx.scale_x != 1.0:
move_x += info.width * (1.0 - (ctx.scale_x if ctx.scale_x != 0 else ctx.scale)) / 2.0
if ctx.scale != 1.0 or ctx.scale_y != 1.0:
move_y -= info.height * (1.0 - (ctx.scale_y if ctx.scale_y != 0 else ctx.scale)) / 2.0
info = get_glyph_size_info(_glyph)
if info.width + move_x < self.base_font.em:
_glyph.left_side_bearing += move_x
hint = self.get_hint(info.width)
if hint.glyph_width < info.width and adjust_position:
if hint.glyph_width < info.width and ctx.adjust_position:
delta = info.width - hint.glyph_width
move_x += 1.0 - (delta / hint.glyph_width)
_glyph.transform(psMat.translate(move_x, move_y))
Expand Down
9 changes: 9 additions & 0 deletions fonts.json
Expand Up @@ -28,6 +28,15 @@
"description": "https://github.com/powerline/powerline",
"filename": "./fonts/powerline/PowerlineSymbols.otf",
"unicode_range": ["E0A0", "E0B3"]
}, {
"id": "powerline-narrow",
"name": "Powerline Extra Symbols",
"description": "Extra glyphs for the powerline separators. https://github.com/ryanoasis/powerline-extra-symbols",
"filename": "./fonts/powerline-extra/PowerlineExtraSymbols.otf",
"unicode_range": ["E0B4", "E0D1"],
"scale_y": 1.05,
"force_narrow": true,
"adjust_position": true
}, {
"id": "pomicons",
"name": "Pomicons",
Expand Down

0 comments on commit 7653e06

Please sign in to comment.