Skip to content

Instantly share code, notes, and snippets.

View typoman's full-sized avatar
:electron:
Never stop learning!

Bahman Eslami typoman

:electron:
Never stop learning!
View GitHub Profile
@typoman
typoman / cubic-curve-type.py
Created October 23, 2024 15:40
determine cubic bezier curve type using the canonical form in python
import math
from typing import Tuple
EPSILON = .1
def forward_transform(points: Tuple[Tuple[float, float], ...], scale: float = 1) -> Tuple[float, float]:
p1, p2, p3, p4 = points
dy = p2[1] - p1[1]
if abs(dy) < EPSILON:
@typoman
typoman / cubic-bezier-inflection-point.py
Last active October 28, 2024 12:27
find inflection points on a cubic bezier curve using python
import logging
import sys
import time
logger = logging.getLogger('my_timer')
handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)
def timeit(method):
"""
A decorator that makes it possible to time functions.
@typoman
typoman / bezier-path-find-closest-point.py
Last active October 28, 2024 19:11
Finds closest point on a bezier path using Newton–Raphson method and visualizes it in drawbot app
import math
def bezier_curve(p0, p1, p2, p3, t):
x = (1-t)**3 * p0[0] + 3*(1-t)**2 * t * p1[0] + 3*(1-t) * t**2 * p2[0] + t**3 * p3[0]
y = (1-t)**3 * p0[1] + 3*(1-t)**2 * t * p1[1] + 3*(1-t) * t**2 * p2[1] + t**3 * p3[1]
return (x, y)
def bezier_curve_derivative(p0, p1, p2, p3, t):
x = 3*(1-t)**2 * (p1[0] - p0[0]) + 6*(1-t) * t * (p2[0] - p1[0]) + 3*t**2 * (p3[0] - p2[0])
y = 3*(1-t)**2 * (p1[1] - p0[1]) + 6*(1-t) * t * (p2[1] - p1[1]) + 3*t**2 * (p3[1] - p2[1])
@typoman
typoman / drawbot-random-circles-on-a-line-grid.py
Created October 14, 2024 12:38
drawbot random circles on a line grid
import math
import cmath
import random
def distance(p1: tuple, p2: tuple):
"""
Calculate the Euclidean distance between two points.
Args:
p1 (tuple): The first point as a tuple of (x, y) coordinates.
@typoman
typoman / segment symbols pen.py
Last active September 18, 2024 10:54
A pen class for converting glyph outlines into symbolic representations. The resulting string can be used for quick visual comparison or analysis of glyph shapes without rendering the full outline.
from typing import Tuple, Optional
from fontTools.pens.basePen import BasePen
from fontTools.misc.arrayTools import calcBounds
import math
def round_angle_to_step(angle: float, degree: int) -> int:
"""
Rounds an angle to the nearest degree increment.
Args:
@typoman
typoman / extract-letter-shape-contours-from-image.py
Created July 8, 2024 14:37
This python script is written as a part of research for contextual spacing tools to ease extracting letterform shapes from manuscripts
import cv2
import numpy as np
import os
from pathlib import Path
import argparse
"""
Usage:
Install prerequisite python packages:
@typoman
typoman / intersectPen.py
Created February 7, 2024 16:58
Get intersection points of list of lines with a glyph in a font
from fontTools.pens.basePen import BasePen
from fontTools.misc.bezierTools import calcCubicParameters, solveCubic, _alignment_transformation, cubicPointAtT, _line_t_of_pt, linePointAtT
def _curve_line_intersections_t(curve, line):
aligned_curve = _alignment_transformation(line).transformPoints(curve)
a, b, c, d = calcCubicParameters(*aligned_curve)
intersections = solveCubic(a[1], b[1], c[1], d[1])
return sorted(i for i in intersections if 0.0 <= i <= 1)
def curveLineIntersections(curve, line):

OpenType font name table notes

Provided are the name table record ID and instructions on how to fill it out. Arial is used as an example to show how the names records can be filled. You can see the example records here:

https://learn.microsoft.com/en-us/typography/opentype/spec/namesmp

There are also more exmaples added to the bottom of this file.

Why this document?

@typoman
typoman / robofont-key-binding-doom-emacs-config.el
Last active April 14, 2023 17:07
Run the script from doom emacs inside RoboFont using the key sequence "leader r f"
(defun run-robofont ()
"Run current python file inside roboFont."
(interactive)
(when (eq major-mode 'python-mode)
(let ((file-path (buffer-file-name)))
(when file-path
(shell-command (concat "roboFont '" file-path "';open '/Applications/RoboFont.app'"))))))
(map! :map python-mode-map
:leader
@typoman
typoman / addSnippet.py
Last active September 21, 2023 16:28
UI form for adding snippets to espanso
import os
import sys
"""
Add {cb} anywhere inside the replacement to make it replaced with the contents
of the clipboard.
"""
transMap = str.maketrans({
"\n": '\\n',
"\\": '\\\\',
"\"": '\\"',