-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store text polygons in oas file, load when creating labels
- Loading branch information
1 parent
38a14fe
commit f9a2694
Showing
5 changed files
with
169 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# This code is part of KQCircuits | ||
# Copyright (C) 2024 IQM Finland Oy | ||
# | ||
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public | ||
# License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later | ||
# version. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along with this program. If not, see | ||
# https://www.gnu.org/licenses/gpl-3.0.html. | ||
# | ||
# The software distribution should follow IQM trademark policy for open-source software | ||
# (meetiqm.com/iqm-open-source-trademark-policy). IQM welcomes contributions to the code. | ||
# Please see our contribution agreements for individuals (meetiqm.com/iqm-individual-contributor-license-agreement) | ||
# and organizations (meetiqm.com/iqm-organization-contributor-license-agreement). | ||
|
||
from functools import lru_cache | ||
from pathlib import Path | ||
from kqcircuits.pya_resolver import pya | ||
|
||
|
||
# Expected properties of the text geometry as loaded from the ``font_polygons.oas`` file | ||
|
||
# File path to the ``font_polygons.oas`` file | ||
OAS_PATH = str(Path(__file__).parent.joinpath("font_polygons.oas")) | ||
# Letters in the oas file | ||
OAS_LETTERS = [chr(ord("A") + i) for i in range(0, 32)] + [chr(ord("0") + i) for i in range(0, 10)] | ||
# Spacing between two letters | ||
OAS_TEXT_SPACING = 300.0 | ||
# "mag" parameter set for TEXT pcell in oas file | ||
OAS_TEXT_MAGNIFICATION = 500.0 | ||
# dbu in the oas file | ||
OAS_DBU = 0.001 | ||
|
||
|
||
def get_text_polygon(label: str, size: float = OAS_TEXT_MAGNIFICATION) -> pya.Region: | ||
"""Returns the given label string as a region. | ||
Utilizes speed ups compared to generating text geometry with KLayout's TEXT PCells. | ||
Only supports characters layed out in the ``font_polygons.oas`` file. | ||
""" | ||
|
||
font_polygons = load_font_polygons() | ||
unsported_characters = set(x.upper() for x in label) - set(font_polygons.keys()) | ||
if unsported_characters: | ||
raise ValueError(f"Unsupported characters for get_text_polygon: {unsported_characters}") | ||
|
||
norm_size = size / OAS_TEXT_MAGNIFICATION | ||
spacing = norm_size * OAS_TEXT_SPACING / OAS_DBU | ||
label_region = pya.Region() | ||
if label is not None: | ||
for i, letter in enumerate(str(label)): | ||
label_region += ( | ||
font_polygons[letter.upper()].scaled_and_snapped(0, norm_size, 1, 0, norm_size, 1).moved(i * spacing, 0) | ||
) | ||
return label_region | ||
|
||
|
||
@lru_cache(maxsize=None) | ||
def load_font_polygons() -> dict[str, pya.Region]: | ||
"""Loads from static OAS file a region for each letter used in labels. | ||
Cached for reuse. | ||
""" | ||
layout = pya.Layout() | ||
layout.read(OAS_PATH) | ||
|
||
font_dict = {letter: pya.Region() for letter in OAS_LETTERS} | ||
for shape in pya.Region(layout.top_cells()[-1].begin_shapes_rec(layout.layer(129, 1))).each(): | ||
index = round(shape.bbox().to_dtype(OAS_DBU).p1.x) // OAS_TEXT_MAGNIFICATION | ||
font_dict[OAS_LETTERS[int(index)]] += pya.Region(shape.moved(-OAS_TEXT_MAGNIFICATION * index / OAS_DBU, 0)) | ||
|
||
return font_dict |
60 changes: 60 additions & 0 deletions
60
klayout_package/python/scripts/macros/generate/font_polygon_oas.lym
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<klayout-macro> | ||
<description>Lay out all characters</description> | ||
<version/> | ||
<category>pymacros</category> | ||
<prolog/> | ||
<epilog/> | ||
<doc/> | ||
<autorun>false</autorun> | ||
<autorun-early>false</autorun-early> | ||
<priority>0</priority> | ||
<shortcut/> | ||
<show-in-menu>false</show-in-menu> | ||
<group-name/> | ||
<interpreter>python</interpreter> | ||
<dsl-interpreter-name/> | ||
<text># This code is part of KQCircuits | ||
# Copyright (C) 2024 IQM Finland Oy | ||
# | ||
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public | ||
# License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later | ||
# version. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along with this program. If not, see | ||
# https://www.gnu.org/licenses/gpl-3.0.html. | ||
# | ||
# The software distribution should follow IQM trademark policy for open-source software | ||
# (meetiqm.com/iqm-open-source-trademark-policy). IQM welcomes contributions to the code. | ||
# Please see our contribution agreements for individuals (meetiqm.com/iqm-individual-contributor-license-agreement) | ||
# and organizations (meetiqm.com/iqm-organization-contributor-license-agreement). | ||
|
||
from kqcircuits.pya_resolver import pya | ||
from kqcircuits.defaults import default_layers | ||
from kqcircuits.klayout_view import KLayoutView | ||
|
||
keyboard_chars = [chr(ord("A") + i) for i in range(0, 32)] + [chr(ord("0") + i) for i in range(0, 10)] | ||
|
||
polygons = {} | ||
view = KLayoutView() | ||
layout = view.layout | ||
for i, label in enumerate(keyboard_chars): | ||
|
||
char_cell = layout.create_cell( | ||
"TEXT", | ||
"Basic", | ||
{ | ||
"layer": default_layers["1t1_base_metal_gap"], | ||
"text": label, | ||
"mag": 500, | ||
}, | ||
) | ||
layout.top_cells()[0].insert(pya.DCellInstArray(char_cell.cell_index(), pya.DTrans(0, False, i*500, 0))) | ||
|
||
print("Label character shapes generated. Please save the generated layout as kqcircuits/util/font_polygons.oas") | ||
print("Uncheck 'Store PCell and library context information' in 'Save Layout options' window") | ||
</text> | ||
</klayout-macro> |