def render_text(text, size=None, position=(0, 0), font_prop=None, tolerance=0.1): tol = 0.1 * tolerance path = TextPath(position, text, size=size, prop=font_prop) polys = [] xmax = position[0] for points, code in path.iter_segments(): if code == path.MOVETO: c = gdstk.Curve(points, tolerance=tolerance) elif code == path.LINETO: c.segment(points.reshape(points.size // 2, 2)) elif code == path.CURVE3: c.quadratic(points.reshape(points.size // 2, 2)) elif code == path.CURVE4: c.cubic(points.reshape(points.size // 2, 2)) elif code == path.CLOSEPOLY: poly = c.points() if poly.size > 0: if poly[:, 0].min() < xmax: i = len(polys) - 1 while i >= 0: if gdstk.inside(poly[:1], [polys[i]], precision=tol)[0]: p = polys.pop(i) b = gdstk.boolean([p], [poly], "xor", tol) poly = b[0].points break elif gdstk.inside(polys[i][:1], [poly], precision=tol)[0]: p = polys.pop(i) b = gdstk.boolean([p], [poly], "xor", tol) poly = b[0].points i -= 1 xmax = max(xmax, poly[:, 0].max()) polys.append(poly) return polys
def _render_text(text, size=None, position=(0, 0), font_prop=None, tolerance=0.1): """This function is copied from https://gdspy.readthedocs.io/en/stable/gettingstarted.html#using-system-fonts""" path = TextPath(position, text, size=size, prop=font_prop) polys = [] xmax = position[0] for points, code in path.iter_segments(): if code == path.MOVETO: c = gdspy.Curve(*points, tolerance=tolerance) elif code == path.LINETO: c.L(*points) elif code == path.CURVE3: c.Q(*points) elif code == path.CURVE4: c.C(*points) elif code == path.CLOSEPOLY: poly = c.get_points() if poly.size > 0: if poly[:, 0].min() < xmax: i = len(polys) - 1 while i >= 0: if gdspy.inside(poly[:1], [polys[i]], precision=0.1 * tolerance)[0]: p = polys.pop(i) poly = gdspy.boolean( [p], [poly], "xor", precision=0.1 * tolerance, max_points=0, ).polygons[0] break elif gdspy.inside(polys[i][:1], [poly], precision=0.1 * tolerance)[0]: p = polys.pop(i) poly = gdspy.boolean( [p], [poly], "xor", precision=0.1 * tolerance, max_points=0, ).polygons[0] i -= 1 xmax = max(xmax, poly[:, 0].max()) polys.append(poly) return polys
def render_text(text, size=None, position=(0, 0), font_prop=None, tolerance=0.1): path = TextPath(position, text, size=size, prop=font_prop) polys = [] xmax = position[0] for points, code in path.iter_segments(): if code == path.MOVETO: c = gdspy.Curve(*points, tolerance=tolerance) elif code == path.LINETO: c.L(*points) elif code == path.CURVE3: c.Q(*points) elif code == path.CURVE4: c.C(*points) elif code == path.CLOSEPOLY: poly = c.get_points() if poly.size > 0: if poly[:, 0].min() < xmax: i = len(polys) - 1 while i >= 0: if gdspy.inside(poly[:1], [polys[i]], precision=0.1 * tolerance)[0]: p = polys.pop(i) poly = gdspy.boolean( [p], [poly], "xor", precision=0.1 * tolerance, max_points=0, ).polygons[0] break elif gdspy.inside(polys[i][:1], [poly], precision=0.1 * tolerance)[0]: p = polys.pop(i) poly = gdspy.boolean( [p], [poly], "xor", precision=0.1 * tolerance, max_points=0, ).polygons[0] i -= 1 xmax = max(xmax, poly[:, 0].max()) polys.append(poly) return polys
def render_text(text, size=None, position=(0, 0), font_prop=None, tolerance=0.1): precision = 0.1 * tolerance path = TextPath(position, text, size=size, prop=font_prop) polys = [] xmax = position[0] for points, code in path.iter_segments(): if code == path.MOVETO: c = gdstk.Curve(points, tolerance=tolerance) elif code == path.LINETO: c.segment(points.reshape(points.size // 2, 2)) elif code == path.CURVE3: c.quadratic(points.reshape(points.size // 2, 2)) elif code == path.CURVE4: c.cubic(points.reshape(points.size // 2, 2)) elif code == path.CLOSEPOLY: pts = c.points() if pts.size > 0: poly = gdstk.Polygon(pts) if pts[:, 0].min() < xmax: i = len(polys) - 1 while i >= 0: if polys[i].contain_any(*poly.points): p = polys.pop(i) poly = gdstk.boolean(p, poly, "xor", precision)[0] break elif poly.contain_any(*polys[i].points): p = polys.pop(i) poly = gdstk.boolean(p, poly, "xor", precision)[0] i -= 1 xmax = max(xmax, poly.points[:, 0].max()) polys.append(poly) return polys