Convert SVG text to paths via fontTools; drop embedded font
This commit is contained in:
+1
-1
@@ -15,7 +15,7 @@ spec:
|
|||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: m1730-generator
|
- name: m1730-generator
|
||||||
image: git.baumann.gr/adebaumann/m1730-generator:0.5
|
image: git.baumann.gr/adebaumann/m1730-generator:0.6
|
||||||
imagePullPolicy: Always
|
imagePullPolicy: Always
|
||||||
ports:
|
ports:
|
||||||
- containerPort: 5000
|
- containerPort: 5000
|
||||||
|
|||||||
+59
-29
@@ -1,37 +1,35 @@
|
|||||||
import base64
|
import base64
|
||||||
import pathlib
|
import pathlib
|
||||||
from flask import Flask, render_template, request, Response
|
from flask import Flask, render_template, request, Response
|
||||||
from html import escape
|
from fontTools.ttLib import TTFont
|
||||||
|
from fontTools.pens.svgPathPen import SVGPathPen
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
VERSION = "0.6"
|
||||||
|
|
||||||
_LOGO_PATH = pathlib.Path(__file__).parent / 'static' / 'logo.png'
|
_LOGO_PATH = pathlib.Path(__file__).parent / 'static' / 'logo.png'
|
||||||
_LOGO_B64 = base64.b64encode(_LOGO_PATH.read_bytes()).decode()
|
_LOGO_B64 = base64.b64encode(_LOGO_PATH.read_bytes()).decode()
|
||||||
|
|
||||||
_FONT_PATH = pathlib.Path('/usr/local/share/fonts/г/ГОСТ_тип_А.ttf')
|
_FONT_PATH = pathlib.Path('/usr/local/share/fonts/г/ГОСТ_тип_А.ttf')
|
||||||
_FONT_B64 = base64.b64encode(_FONT_PATH.read_bytes()).decode()
|
_FONT_OBJ = TTFont(str(_FONT_PATH))
|
||||||
_FONT_FACE = (
|
_GLYPH_SET = _FONT_OBJ.getGlyphSet()
|
||||||
'<defs><style>'
|
_CMAP = _FONT_OBJ.getBestCmap()
|
||||||
'@font-face {'
|
_HMTX = _FONT_OBJ['hmtx'].metrics
|
||||||
'font-family:"ГОСТ тип А";'
|
_UPM = _FONT_OBJ['head'].unitsPerEm
|
||||||
f'src:url("data:font/truetype;base64,{_FONT_B64}") format("truetype");'
|
|
||||||
'}'
|
|
||||||
'</style></defs>'
|
|
||||||
)
|
|
||||||
|
|
||||||
# SVG absolute coordinate constants (1 user unit = 1 mm)
|
# SVG absolute coordinate constants (1 user unit = 1 mm)
|
||||||
ZERO_X = 60.844130 # x of the zero/left tick
|
ZERO_X = 60.844130
|
||||||
MAX_X = 180.251230 # x of the full-scale/right tick
|
MAX_X = 180.251230
|
||||||
BAR_Y = 107.54670 # y at the top of the scale bar (tick baseline)
|
BAR_Y = 107.54670
|
||||||
SCALE_LEN = MAX_X - ZERO_X # 119.4071 mm
|
SCALE_LEN = MAX_X - ZERO_X # 119.4071 mm
|
||||||
|
|
||||||
BIG_H = 5.0895 # major tick height
|
BIG_H = 5.0895
|
||||||
SMALL_H = 2.6683 # minor tick height
|
SMALL_H = 2.6683
|
||||||
|
|
||||||
|
_UNIT_AREA_WIDTH = 18.445
|
||||||
_UNIT_AREA_WIDTH = 18.445 # canvas mm available for the unit label
|
|
||||||
_UNIT_SCALE_X = 0.96486402
|
_UNIT_SCALE_X = 0.96486402
|
||||||
_UNIT_CHAR_EM = 0.65 # approximate advance-width ratio for GOST typ A
|
_UNIT_CHAR_EM = 0.65
|
||||||
|
|
||||||
|
|
||||||
def _unit_font_size(unit: str) -> float:
|
def _unit_font_size(unit: str) -> float:
|
||||||
@@ -45,6 +43,38 @@ def _fmt(v: float) -> str:
|
|||||||
return f"{v:.4g}"
|
return f"{v:.4g}"
|
||||||
|
|
||||||
|
|
||||||
|
def _text_to_paths(text, x, y, font_size, anchor='start', fill='#1a1a1a', wrap_transform=None):
|
||||||
|
"""Convert a text string to SVG <path> elements using glyph outlines."""
|
||||||
|
s = font_size / _UPM
|
||||||
|
|
||||||
|
# Compute total advance width for centering
|
||||||
|
if anchor == 'middle':
|
||||||
|
total_w = sum(
|
||||||
|
_HMTX.get(_CMAP.get(ord(ch)), (0, 0))[0] * s
|
||||||
|
for ch in text if _CMAP.get(ord(ch))
|
||||||
|
)
|
||||||
|
x -= total_w / 2
|
||||||
|
|
||||||
|
parts = []
|
||||||
|
cx = x
|
||||||
|
for ch in text:
|
||||||
|
gn = _CMAP.get(ord(ch))
|
||||||
|
if gn is None:
|
||||||
|
continue
|
||||||
|
pen = SVGPathPen(_GLYPH_SET)
|
||||||
|
_GLYPH_SET[gn].draw(pen)
|
||||||
|
d = pen.getCommands()
|
||||||
|
if d:
|
||||||
|
t = f'translate({cx:.5f},{y:.5f}) scale({s:.8f},{-s:.8f})'
|
||||||
|
parts.append(f'<path style="fill:{fill}" transform="{t}" d="{d}"/>')
|
||||||
|
cx += _HMTX.get(gn, (0, 0))[0] * s
|
||||||
|
|
||||||
|
inner = '\n'.join(parts)
|
||||||
|
if wrap_transform:
|
||||||
|
return f'<g transform="{wrap_transform}">{inner}</g>'
|
||||||
|
return inner
|
||||||
|
|
||||||
|
|
||||||
def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, label_count):
|
def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, label_count):
|
||||||
big_interval = SCALE_LEN / big_ticks
|
big_interval = SCALE_LEN / big_ticks
|
||||||
big_xs = [ZERO_X + i * big_interval for i in range(big_ticks + 1)]
|
big_xs = [ZERO_X + i * big_interval for i in range(big_ticks + 1)]
|
||||||
@@ -57,7 +87,6 @@ def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, la
|
|||||||
'<svg width="157.18373mm" height="26.683672mm"'
|
'<svg width="157.18373mm" height="26.683672mm"'
|
||||||
' viewBox="0 0 157.18373 26.683672" version="1.1"'
|
' viewBox="0 0 157.18373 26.683672" version="1.1"'
|
||||||
' xmlns="http://www.w3.org/2000/svg">',
|
' xmlns="http://www.w3.org/2000/svg">',
|
||||||
_FONT_FACE,
|
|
||||||
' <g transform="translate(-37.352226,-90.454928)">',
|
' <g transform="translate(-37.352226,-90.454928)">',
|
||||||
|
|
||||||
# Background rect with scale-bar cutout
|
# Background rect with scale-bar cutout
|
||||||
@@ -66,14 +95,14 @@ def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, la
|
|||||||
' d="M 37.444226,90.546928 V 117.0466 H 194.44395 V 90.546928 Z'
|
' d="M 37.444226,90.546928 V 117.0466 H 194.44395 V 90.546928 Z'
|
||||||
' M 58.443978,107.54692 H 183.64409 v 6.19963 H 58.443978 Z" />',
|
' M 58.443978,107.54692 H 183.64409 v 6.19963 H 58.443978 Z" />',
|
||||||
|
|
||||||
# Unit label (large letter in left panel)
|
# Unit label — paths in local coordinate space, wrapped in the same scale transform
|
||||||
f' <text style="font-size:{_unit_font_size(unit):.4f}px;font-family:\'ГОСТ тип А\',monospace;fill:#1a1a1a"'
|
' ' + _text_to_paths(
|
||||||
f' x="41.455711" y="109.03796"'
|
unit, 41.455711, 109.03796, _unit_font_size(unit),
|
||||||
f' transform="scale(0.96486402,1.0364155)">{escape(unit)}</text>',
|
wrap_transform=f'scale({_UNIT_SCALE_X},1.0364155)'
|
||||||
|
),
|
||||||
|
|
||||||
# Range label inside the strip on the right
|
# Range label inside the strip on the right
|
||||||
f' <text style="font-size:3.60539px;font-family:\'ГОСТ тип А\',monospace;fill:#1a1a1a"'
|
' ' + _text_to_paths(range_label, 184.79707, 113.6755, 3.60539),
|
||||||
f' x="184.79707" y="113.6755">{escape(range_label)}</text>',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
# Major ticks
|
# Major ticks
|
||||||
@@ -94,13 +123,14 @@ def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, la
|
|||||||
f' style="stroke:#1a1a1a;stroke-width:0.302;stroke-linecap:butt" />'
|
f' style="stroke:#1a1a1a;stroke-width:0.302;stroke-linecap:butt" />'
|
||||||
)
|
)
|
||||||
|
|
||||||
# Numeric labels (centered on their tick, above the tick area)
|
# Numeric labels centered on their tick
|
||||||
for idx in label_indices:
|
for idx in label_indices:
|
||||||
x = big_xs[idx]
|
x = big_xs[idx]
|
||||||
val = min_val + (max_val - min_val) * idx / big_ticks
|
val = min_val + (max_val - min_val) * idx / big_ticks
|
||||||
parts.append(
|
parts.append(
|
||||||
f' <text style="font-size:8.89194px;font-family:\'ГОСТ тип А\',monospace;fill:#1a1a1a"'
|
' ' + _text_to_paths(
|
||||||
f' text-anchor="middle" x="{x:.5f}" y="101.49598">{escape(_fmt(val))}</text>'
|
_fmt(val), x, 101.49598, 8.89194, anchor='middle'
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
parts += [' </g>', '</svg>']
|
parts += [' </g>', '</svg>']
|
||||||
@@ -109,7 +139,7 @@ def generate_svg(unit, min_val, max_val, range_label, big_ticks, small_ticks, la
|
|||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
return render_template('index.html', logo_b64=_LOGO_B64)
|
return render_template('index.html', logo_b64=_LOGO_B64, version=VERSION)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/generate', methods=['POST'])
|
@app.route('/generate', methods=['POST'])
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
flask
|
flask
|
||||||
gunicorn
|
gunicorn
|
||||||
|
fonttools
|
||||||
|
|||||||
Reference in New Issue
Block a user