Skip to content

Instantly share code, notes, and snippets.

@drott
Last active October 18, 2018 13:04
Show Gist options
  • Save drott/05ce5237a47bed21115a46d85d6c9d06 to your computer and use it in GitHub Desktop.
Save drott/05ce5237a47bed21115a46d85d6c9d06 to your computer and use it in GitHub Desktop.
Quick script to overlay CoreText vs HB's own shaper for tracking
#!/usr/bin/env python3
import subprocess
from PIL import Image, ImageOps
import io
font_ptem_sizes = [
6,
9,
10,
11,
12,
13,
14,
15,
16,
17,
20,
22,
28,
32,
36,
50,
64,
80,
100,
138,
]
shapers = ["ot", "coretext"]
shape_text = "www"
font_path = "/System/Library/Fonts/SFNSText.ttf"
features = "-kern"
images = []
for font_ptem_size in font_ptem_sizes:
for shaper in shapers:
proc = subprocess.Popen(
[
"./hb-view",
"--features=%s" % features,
"--font-ptem=%d" % font_ptem_size,
"--font-size=256",
"--shaper=%s" % shaper,
font_path,
shape_text,
],
shell=False,
stdout=subprocess.PIPE,
)
images.append(Image.open(io.BytesIO(proc.communicate()[0])))
max_x = 0
acc_y = 0
for image in images:
max_x = max(max_x, image.getbbox()[2])
acc_y += image.getbbox()[3]
composite_image = Image.new("RGBA", (max_x, acc_y))
insert_position = 0
for image_ot, image_coretext in zip(images[::2], images[1::2]):
merged_size = (
max(image_ot.width, image_coretext.width),
max(image_ot.height, image_coretext.height),
)
alpha_blended = Image.new("RGBA", merged_size)
alpha_blended.paste(
ImageOps.colorize(image_coretext, "Green", "White"),
(0, 0),
Image.new("L", image_coretext.size, 255),
)
alpha_blended.paste(
ImageOps.colorize(image_ot, "Red", "White"),
(0, 0),
Image.new("L", image_ot.size, 100),
)
composite_image.paste(alpha_blended, (0, insert_position))
insert_position += merged_size[1]
composite_image.save("coretext_green_ot_red.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment