-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgerber_tool.py
More file actions
1488 lines (1252 loc) · 49.5 KB
/
Copy pathgerber_tool.py
File metadata and controls
1488 lines (1252 loc) · 49.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
gerber_tool.py — Outil de conversion Gerber <-> SVG
Modes :
--svg Gerber -> SVG + JSON (pour edition dans Inkscape)
--gerber SVG -> Gerber (reconversion apres edition)
Usage :
python3 gerber_tool.py --svg board.GTO [output.svg] [--outline board.GKO]
python3 gerber_tool.py --gerber board.GTO.svg [output.GTO]
"""
import sys
import os
import re
import json
import math
import xml.etree.ElementTree as ET
from pathlib import Path
INCH_TO_MM = 25.4
MM_TO_INCH = 1.0 / INCH_TO_MM
SVG_NS = '{http://www.w3.org/2000/svg}'
# === GERBER PARSING ===
def parse_format_spec(line):
"""Parse %FSLAX25Y25*% → dict"""
m = re.match(r'%FS([LA])([AI])X(\d)(\d)Y(\d)(\d)\*%', line)
if not m:
return None
return {
'zero_omit': m.group(1),
'coord_mode': m.group(2),
'x_int': int(m.group(3)),
'x_dec': int(m.group(4)),
'y_int': int(m.group(5)),
'y_dec': int(m.group(6)),
}
def parse_aperture_def(line):
"""Parse %ADD10C,0.00600*% → (id, type, params, params_raw)"""
m = re.match(r'%ADD(\d+)([A-Za-z][A-Za-z0-9]*),?(.*?)\*%', line)
if not m:
return None
ap_id = int(m.group(1))
ap_type = m.group(2)
params_str = m.group(3)
params = [float(p) for p in params_str.split('X')] if params_str else []
params_raw = params_str.split('X') if params_str else []
return ap_id, ap_type, params, params_raw
def parse_aperture_macro(lines):
"""Parse multi-line aperture macros."""
macros = {}
i = 0
while i < len(lines):
line = lines[i]
if line.startswith('%AM'):
name = line[3:].rstrip('*')
body_lines = []
i += 1
while i < len(lines) and not lines[i].startswith('%'):
body_lines.append(lines[i].rstrip('*'))
i += 1
macros[name] = body_lines
i += 1
return macros
def parse_coord(s, n_dec):
"""Convertit une chaîne de coordonnée Gerber en float (inches)."""
if s is None:
return None
return int(s) / (10 ** n_dec)
def parse_gerber(filepath):
"""Parse un fichier Gerber complet. Retourne (info, operations)."""
with open(filepath, 'r') as f:
raw = f.read()
lines = raw.replace('\r\n', '\n').replace('\r', '\n').split('\n')
lines = [l.strip() for l in lines if l.strip()]
info = {
'source_file': os.path.basename(filepath),
'units': 'inch',
'format': None,
'apertures': {},
'macros': {},
'polarity': 'D',
}
operations = []
# Parse macros (multi-line)
info['macros'] = parse_aperture_macro(lines)
current_aperture = None
current_x = 0.0
current_y = 0.0
fmt = None
for line in lines:
if line == '%MOIN*%':
info['units'] = 'inch'
continue
if line == '%MOMM*%':
info['units'] = 'mm'
continue
if line.startswith('%FS'):
fmt = parse_format_spec(line)
info['format'] = fmt
continue
if line.startswith('%ADD'):
result = parse_aperture_def(line)
if result:
ap_id, ap_type, params, params_raw = result
info['apertures'][str(ap_id)] = {
'type': ap_type,
'params': params,
'params_raw': params_raw,
}
continue
m = re.match(r'%LP([DC])\*%', line)
if m:
info['polarity'] = m.group(1)
continue
if line.startswith('%'):
continue
if re.match(r'^G\d+\*$', line):
continue
if re.match(r'^M\d+\*$', line):
continue
m = re.match(r'^D(\d+)\*$', line)
if m:
d = int(m.group(1))
if d >= 10:
current_aperture = d
continue
if fmt is None:
continue
m = re.match(r'^(?:G\d+)?(?:X([+-]?\d+))?(?:Y([+-]?\d+))?D(\d+)\*$', line)
if m:
x_raw, y_raw, d_code = m.group(1), m.group(2), int(m.group(3))
new_x = parse_coord(x_raw, fmt['x_dec']) if x_raw else current_x
new_y = parse_coord(y_raw, fmt['y_dec']) if y_raw else current_y
if d_code == 2:
current_x = new_x
current_y = new_y
elif d_code == 1:
operations.append({
'type': 'line',
'aperture': current_aperture,
'x1': current_x, 'y1': current_y,
'x2': new_x, 'y2': new_y,
})
current_x = new_x
current_y = new_y
elif d_code == 3:
operations.append({
'type': 'flash',
'aperture': current_aperture,
'x': new_x, 'y': new_y,
})
current_x = new_x
current_y = new_y
return info, operations
# === GERBER -> SVG ===
def to_mm(val, units):
return val * INCH_TO_MM if units == 'inch' else val
def aperture_diameter_mm(ap_def, macros, units):
"""Retourne le diamètre effectif d'une aperture en mm."""
ap_type = ap_def['type']
params = ap_def['params']
if ap_type == 'C':
return to_mm(params[0], units) if params else 0
if ap_type == 'R':
w = to_mm(params[0], units) if len(params) > 0 else 0
h = to_mm(params[1], units) if len(params) > 1 else w
return max(w, h)
if ap_type in macros:
for body_line in macros[ap_type]:
parts = body_line.split(',')
if parts[0].strip() == '5':
diam_expr = parts[5].strip()
if 'X' in diam_expr and '$' in diam_expr:
factor = float(diam_expr.split('X')[0])
diam = factor * params[0] if params else 0
else:
diam = float(diam_expr)
return to_mm(diam, units)
return to_mm(params[0], units) if params else 0
def make_flash_svg(ap_def, macros, x_mm, y_mm, units, ap_id):
"""Génère l'élément SVG pour un flash."""
ap_type = ap_def['type']
params = ap_def['params']
if ap_type == 'C':
r = to_mm(params[0], units) / 2 if params else 0
if r == 0:
return f'<circle cx="{x_mm:.4f}" cy="{y_mm:.4f}" r="0.05" class="ap{ap_id} flash zero-size"/>'
return f'<circle cx="{x_mm:.4f}" cy="{y_mm:.4f}" r="{r:.4f}" class="ap{ap_id} flash"/>'
if ap_type == 'R':
w = to_mm(params[0], units) if len(params) > 0 else 0
h = to_mm(params[1], units) if len(params) > 1 else w
rx = x_mm - w / 2
ry = y_mm - h / 2
return f'<rect x="{rx:.4f}" y="{ry:.4f}" width="{w:.4f}" height="{h:.4f}" class="ap{ap_id} flash"/>'
if ap_type in macros:
for body_line in macros[ap_type]:
parts = body_line.split(',')
if parts[0].strip() == '5':
n_vert = int(parts[2].strip())
diam_expr = parts[5].strip()
rotation = float(parts[6].strip()) if len(parts) > 6 else 0
if 'X' in diam_expr and '$' in diam_expr:
factor = float(diam_expr.split('X')[0])
diam = factor * params[0]
else:
diam = float(diam_expr)
r = to_mm(diam, units) / 2
points = []
for i in range(n_vert):
angle = math.radians(rotation + i * 360 / n_vert)
px = x_mm + r * math.cos(angle)
py = y_mm + r * math.sin(angle)
points.append(f'{px:.4f},{py:.4f}')
return f'<polygon points="{" ".join(points)}" class="ap{ap_id} flash"/>'
return f'<circle cx="{x_mm:.4f}" cy="{y_mm:.4f}" r="0.1" class="ap{ap_id} flash"/>'
def build_polylines(trace_ops, units):
"""Regroupe les segments consécutifs en polylines."""
paths = []
current_path = None
for op in trace_ops:
x1_mm = to_mm(op['x1'], units)
y1_mm = -to_mm(op['y1'], units)
x2_mm = to_mm(op['x2'], units)
y2_mm = -to_mm(op['y2'], units)
if (current_path
and abs(current_path[-1][0] - x1_mm) < 0.0001
and abs(current_path[-1][1] - y1_mm) < 0.0001):
current_path.append((x2_mm, y2_mm))
else:
if current_path:
paths.append(current_path)
current_path = [(x1_mm, y1_mm), (x2_mm, y2_mm)]
if current_path:
paths.append(current_path)
return paths
def generate_outline_layer(outline_ops, outline_info):
"""Génère le calque SVG verrouillé pour le contour PCB (GKO)."""
units = outline_info['units']
svg = []
svg.append(f' <!-- Contour PCB (référence verrouillée, ignoré à la reconversion) -->')
svg.append(f' <g id="outline-ref"'
f' inkscape:groupmode="layer"'
f' inkscape:label="[REF] Contour PCB"'
f' sodipodi:insensitive="true"'
f' style="opacity:0.6">')
# Regrouper toutes les opérations de ligne (toutes apertures confondues)
all_traces = [op for op in outline_ops if op['type'] == 'line']
paths = build_polylines(all_traces, units)
for i, path in enumerate(paths):
d_parts = [f'M{path[0][0]:.4f},{path[0][1]:.4f}']
for pt in path[1:]:
d_parts.append(f'L{pt[0]:.4f},{pt[1]:.4f}')
d_str = ' '.join(d_parts)
svg.append(f' <path d="{d_str}"'
f' stroke="#f0c030" stroke-width="0.15"'
f' fill="none" stroke-dasharray="1,0.5"'
f' id="outline-path-{i}"/>')
svg.append(f' </g>')
return svg
def generate_svg(info, operations, outline_ops=None, outline_info=None):
"""Génère le contenu SVG complet."""
units = info['units']
macros = info['macros']
apertures = info['apertures']
# Calculer les bornes (inclure l'outline si présent)
all_x = []
all_y = []
for op in operations:
if op['type'] == 'line':
all_x.extend([op['x1'], op['x2']])
all_y.extend([op['y1'], op['y2']])
elif op['type'] == 'flash':
all_x.append(op['x'])
all_y.append(op['y'])
if outline_ops:
for op in outline_ops:
if op['type'] == 'line':
all_x.extend([op['x1'], op['x2']])
all_y.extend([op['y1'], op['y2']])
if not all_x:
return '<svg xmlns="http://www.w3.org/2000/svg"></svg>'
margin_inch = 0.05
min_x = min(all_x) - margin_inch
max_x = max(all_x) + margin_inch
min_y = min(all_y) - margin_inch
max_y = max(all_y) + margin_inch
vb_x = to_mm(min_x, units)
vb_w = to_mm(max_x - min_x, units)
vb_y = -to_mm(max_y, units)
vb_h = to_mm(max_y - min_y, units)
svg = []
svg.append(f'<?xml version="1.0" encoding="UTF-8"?>')
svg.append(f'<svg xmlns="http://www.w3.org/2000/svg"')
svg.append(f' xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"')
svg.append(f' xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.0.dtd"')
svg.append(f' width="{vb_w:.4f}mm" height="{vb_h:.4f}mm"')
svg.append(f' viewBox="{vb_x:.4f} {vb_y:.4f} {vb_w:.4f} {vb_h:.4f}">')
svg.append(f'')
# Style
svg.append(f' <defs>')
svg.append(f' <style>')
svg.append(f' .trace {{ fill: none; stroke: #b02020; stroke-linecap: round; stroke-linejoin: round; }}')
svg.append(f' .flash {{ fill: #b02020; stroke: none; }}')
svg.append(f' .zero-size {{ fill: #ff00ff; opacity: 0.3; }}')
svg.append(f' </style>')
svg.append(f' </defs>')
svg.append(f'')
# Background
svg.append(f' <rect x="{vb_x:.4f}" y="{vb_y:.4f}" width="{vb_w:.4f}" height="{vb_h:.4f}"')
svg.append(f' fill="#1a1a2e" id="background"/>')
svg.append(f'')
# Outline layer (verrouillé, en dessous de tout)
if outline_ops and outline_info:
svg.extend(generate_outline_layer(outline_ops, outline_info))
svg.append(f'')
# Couches par aperture
ops_by_aperture = {}
for op in operations:
ap = op['aperture']
if ap not in ops_by_aperture:
ops_by_aperture[ap] = []
ops_by_aperture[ap].append(op)
for ap_id in sorted(ops_by_aperture.keys()):
ops = ops_by_aperture[ap_id]
ap_def = apertures.get(str(ap_id))
if not ap_def:
continue
diam = aperture_diameter_mm(ap_def, macros, units)
ap_desc = f'{ap_def["type"]}({",".join(f"{p}" for p in ap_def["params"])})'
svg.append(f' <!-- Aperture D{ap_id}: {ap_desc} -->')
svg.append(f' <g id="aperture-D{ap_id}"'
f' inkscape:groupmode="layer"'
f' inkscape:label="D{ap_id} {ap_desc}">')
# Traces
trace_ops = [op for op in ops if op['type'] == 'line']
if trace_ops:
svg.append(f' <g id="traces-D{ap_id}" class="traces">')
paths = build_polylines(trace_ops, units)
for i, path in enumerate(paths):
d_parts = [f'M{path[0][0]:.4f},{path[0][1]:.4f}']
for pt in path[1:]:
d_parts.append(f'L{pt[0]:.4f},{pt[1]:.4f}')
svg.append(f' <path d="{" ".join(d_parts)}"'
f' stroke-width="{diam:.4f}"'
f' class="trace ap{ap_id}"'
f' id="trace-D{ap_id}-{i}"/>')
svg.append(f' </g>')
# Flashes
flash_ops = [op for op in ops if op['type'] == 'flash']
if flash_ops:
svg.append(f' <g id="flashes-D{ap_id}" class="flashes">')
for i, op in enumerate(flash_ops):
x_mm = to_mm(op['x'], units)
y_mm = -to_mm(op['y'], units)
elem = make_flash_svg(ap_def, macros, x_mm, y_mm, units, ap_id)
elem = elem.replace('/>', f' id="flash-D{ap_id}-{i}"/>', 1)
if '"/' not in elem:
elem = elem.replace('class=', f'id="flash-D{ap_id}-{i}" class=', 1)
svg.append(f' {elem}')
svg.append(f' </g>')
svg.append(f' </g>')
svg.append(f'')
# Trouver la plus fine aperture circulaire non-zero pour le calque EDIT
edit_ap_id = None
edit_ap_diam = float('inf')
for ap_id_str, ap_def in apertures.items():
if ap_def['type'] == 'C' and ap_def['params']:
d = ap_def['params'][0]
if 0 < d < edit_ap_diam:
edit_ap_diam = d
edit_ap_id = int(ap_id_str)
# Calque EDIT (vide, en haut de la pile)
if edit_ap_id is not None:
diam_mm = to_mm(edit_ap_diam, units)
svg.append(f' <!-- ============================================ -->')
svg.append(f' <!-- CALQUE EDIT : dessinez vos modifications ici -->')
svg.append(f' <!-- Aperture D{edit_ap_id} (trait {diam_mm:.3f}mm) -->')
svg.append(f' <!-- Utilisez fill="#b02020" pour les formes -->')
svg.append(f' <!-- ============================================ -->')
svg.append(f' <g id="edit-layer"'
f' inkscape:groupmode="layer"'
f' inkscape:label="EDIT (D{edit_ap_id} — {diam_mm:.3f}mm)">')
svg.append(f' </g>')
svg.append(f'')
svg.append(f'</svg>')
return '\n'.join(svg)
# === SVG PARSING (paths, transforms, fill/stroke) ===
def lerp(a, b, t):
return a + (b - a) * t
def cubic_bezier(p0, p1, p2, p3, n_steps=8):
"""Linéarise une courbe de Bézier cubique en n_steps segments."""
pts = []
for i in range(1, n_steps + 1):
t = i / n_steps
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]
pts.append((x, y))
return pts
def quad_bezier(p0, p1, p2, n_steps=8):
"""Linéarise une courbe de Bézier quadratique."""
pts = []
for i in range(1, n_steps + 1):
t = i / n_steps
x = (1-t)**2*p0[0] + 2*(1-t)*t*p1[0] + t**2*p2[0]
y = (1-t)**2*p0[1] + 2*(1-t)*t*p1[1] + t**2*p2[1]
pts.append((x, y))
return pts
def arc_to_points(cx, cy, rx, ry, phi, theta1, dtheta, n_steps=16):
"""Convertit un arc SVG paramétrisé en points."""
pts = []
for i in range(1, n_steps + 1):
t = theta1 + dtheta * i / n_steps
cos_phi = math.cos(phi)
sin_phi = math.sin(phi)
x = cos_phi * rx * math.cos(t) - sin_phi * ry * math.sin(t) + cx
y = sin_phi * rx * math.cos(t) + cos_phi * ry * math.sin(t) + cy
pts.append((x, y))
return pts
def svg_arc_to_center(x1, y1, rx, ry, phi, fa, fs, x2, y2):
"""Convertit les paramètres d'arc SVG endpoint → center parameterization."""
if rx == 0 or ry == 0:
return [(x2, y2)]
rx, ry = abs(rx), abs(ry)
phi_rad = math.radians(phi)
cos_phi = math.cos(phi_rad)
sin_phi = math.sin(phi_rad)
dx2 = (x1 - x2) / 2
dy2 = (y1 - y2) / 2
x1p = cos_phi * dx2 + sin_phi * dy2
y1p = -sin_phi * dx2 + cos_phi * dy2
# Correction des rayons si trop petits
lam = (x1p**2) / (rx**2) + (y1p**2) / (ry**2)
if lam > 1:
rx *= math.sqrt(lam)
ry *= math.sqrt(lam)
num = max(0, rx**2 * ry**2 - rx**2 * y1p**2 - ry**2 * x1p**2)
den = rx**2 * y1p**2 + ry**2 * x1p**2
if den == 0:
return [(x2, y2)]
sq = math.sqrt(num / den)
if fa == fs:
sq = -sq
cxp = sq * rx * y1p / ry
cyp = -sq * ry * x1p / rx
cx = cos_phi * cxp - sin_phi * cyp + (x1 + x2) / 2
cy = sin_phi * cxp + cos_phi * cyp + (y1 + y2) / 2
def angle(ux, uy, vx, vy):
n = math.sqrt(ux*ux + uy*uy) * math.sqrt(vx*vx + vy*vy)
if n == 0:
return 0
c = (ux*vx + uy*vy) / n
c = max(-1, min(1, c))
a = math.acos(c)
if ux*vy - uy*vx < 0:
a = -a
return a
theta1 = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry)
dtheta = angle((x1p - cxp) / rx, (y1p - cyp) / ry,
(-x1p - cxp) / rx, (-y1p - cyp) / ry)
if fs == 0 and dtheta > 0:
dtheta -= 2 * math.pi
elif fs == 1 and dtheta < 0:
dtheta += 2 * math.pi
return arc_to_points(cx, cy, rx, ry, phi_rad, theta1, dtheta)
def tokenize_svg_path(d):
"""Tokenise un path SVG en commandes et nombres."""
return re.findall(r'[MmLlHhVvCcSsQqTtAaZz]|[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?', d)
def parse_svg_path_d(d):
"""Parse complet d'un path SVG. Retourne une liste de polylines."""
tokens = tokenize_svg_path(d)
polylines = []
current = []
cx, cy = 0, 0 # position courante
sx, sy = 0, 0 # start of subpath (pour Z)
last_cp = None # dernier point de contrôle (pour S/T)
last_cmd = ''
i = 0
def next_float():
nonlocal i
if i < len(tokens):
val = float(tokens[i])
i += 1
return val
return 0
def next_flag():
nonlocal i
if i < len(tokens):
# Les flags peuvent être collés (ex: "0,0,1" ou "001")
val = int(float(tokens[i]))
i += 1
return val
return 0
while i < len(tokens):
tok = tokens[i]
if tok.isalpha() and tok not in '.-+':
cmd = tok
i += 1
else:
# Commande implicite (répétition)
cmd = last_cmd
if cmd == 'M':
cmd = 'L'
elif cmd == 'm':
cmd = 'l'
if cmd in 'Zz':
if current and len(current) > 1:
current.append((sx, sy))
cx, cy = sx, sy
last_cmd = cmd
continue
if cmd == 'M':
if current and len(current) > 0:
polylines.append(current)
cx, cy = next_float(), next_float()
sx, sy = cx, cy
current = [(cx, cy)]
last_cmd = 'M'
elif cmd == 'm':
if current and len(current) > 0:
polylines.append(current)
cx += next_float()
cy += next_float()
sx, sy = cx, cy
current = [(cx, cy)]
last_cmd = 'm'
elif cmd == 'L':
cx, cy = next_float(), next_float()
current.append((cx, cy))
last_cmd = 'L'
elif cmd == 'l':
cx += next_float()
cy += next_float()
current.append((cx, cy))
last_cmd = 'l'
elif cmd == 'H':
cx = next_float()
current.append((cx, cy))
last_cmd = 'H'
elif cmd == 'h':
cx += next_float()
current.append((cx, cy))
last_cmd = 'h'
elif cmd == 'V':
cy = next_float()
current.append((cx, cy))
last_cmd = 'V'
elif cmd == 'v':
cy += next_float()
current.append((cx, cy))
last_cmd = 'v'
elif cmd == 'C':
x1, y1 = next_float(), next_float()
x2, y2 = next_float(), next_float()
x, y = next_float(), next_float()
pts = cubic_bezier((cx, cy), (x1, y1), (x2, y2), (x, y))
current.extend(pts)
last_cp = (x2, y2)
cx, cy = x, y
last_cmd = 'C'
elif cmd == 'c':
x1, y1 = cx + next_float(), cy + next_float()
x2, y2 = cx + next_float(), cy + next_float()
x, y = cx + next_float(), cy + next_float()
pts = cubic_bezier((cx, cy), (x1, y1), (x2, y2), (x, y))
current.extend(pts)
last_cp = (x2, y2)
cx, cy = x, y
last_cmd = 'c'
elif cmd == 'S':
if last_cmd in 'CcSs' and last_cp:
x1 = 2 * cx - last_cp[0]
y1 = 2 * cy - last_cp[1]
else:
x1, y1 = cx, cy
x2, y2 = next_float(), next_float()
x, y = next_float(), next_float()
pts = cubic_bezier((cx, cy), (x1, y1), (x2, y2), (x, y))
current.extend(pts)
last_cp = (x2, y2)
cx, cy = x, y
last_cmd = 'S'
elif cmd == 's':
if last_cmd in 'CcSs' and last_cp:
x1 = 2 * cx - last_cp[0]
y1 = 2 * cy - last_cp[1]
else:
x1, y1 = cx, cy
x2, y2 = cx + next_float(), cy + next_float()
x, y = cx + next_float(), cy + next_float()
pts = cubic_bezier((cx, cy), (x1, y1), (x2, y2), (x, y))
current.extend(pts)
last_cp = (x2, y2)
cx, cy = x, y
last_cmd = 's'
elif cmd == 'Q':
x1, y1 = next_float(), next_float()
x, y = next_float(), next_float()
pts = quad_bezier((cx, cy), (x1, y1), (x, y))
current.extend(pts)
last_cp = (x1, y1)
cx, cy = x, y
last_cmd = 'Q'
elif cmd == 'q':
x1, y1 = cx + next_float(), cy + next_float()
x, y = cx + next_float(), cy + next_float()
pts = quad_bezier((cx, cy), (x1, y1), (x, y))
current.extend(pts)
last_cp = (x1, y1)
cx, cy = x, y
last_cmd = 'q'
elif cmd == 'T':
if last_cmd in 'QqTt' and last_cp:
x1 = 2 * cx - last_cp[0]
y1 = 2 * cy - last_cp[1]
else:
x1, y1 = cx, cy
x, y = next_float(), next_float()
pts = quad_bezier((cx, cy), (x1, y1), (x, y))
current.extend(pts)
last_cp = (x1, y1)
cx, cy = x, y
last_cmd = 'T'
elif cmd == 't':
if last_cmd in 'QqTt' and last_cp:
x1 = 2 * cx - last_cp[0]
y1 = 2 * cy - last_cp[1]
else:
x1, y1 = cx, cy
x, y = cx + next_float(), cy + next_float()
pts = quad_bezier((cx, cy), (x1, y1), (x, y))
current.extend(pts)
last_cp = (x1, y1)
cx, cy = x, y
last_cmd = 't'
elif cmd == 'A':
rx = next_float()
ry = next_float()
phi = next_float()
fa = next_flag()
fs = next_flag()
x, y = next_float(), next_float()
pts = svg_arc_to_center(cx, cy, rx, ry, phi, fa, fs, x, y)
current.extend(pts)
cx, cy = x, y
last_cmd = 'A'
elif cmd == 'a':
rx = next_float()
ry = next_float()
phi = next_float()
fa = next_flag()
fs = next_flag()
dx, dy = next_float(), next_float()
x, y = cx + dx, cy + dy
pts = svg_arc_to_center(cx, cy, rx, ry, phi, fa, fs, x, y)
current.extend(pts)
cx, cy = x, y
last_cmd = 'a'
else:
i += 1 # skip unknown
if current:
polylines.append(current)
return polylines
# ---------------------------------------------------------------------------
# Transform parsing
# ---------------------------------------------------------------------------
def parse_transform(transform_str):
"""Parse un attribut transform SVG et retourne une matrice 3x3 [a,b,c,d,e,f]."""
if not transform_str:
return None
result = [1, 0, 0, 1, 0, 0] # identity
for m in re.finditer(r'(\w+)\s*\(([^)]+)\)', transform_str):
func = m.group(1)
vals = [float(v) for v in re.findall(r'[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?', m.group(2))]
if func == 'matrix' and len(vals) >= 6:
result = vals[:6]
elif func == 'translate':
tx = vals[0] if len(vals) > 0 else 0
ty = vals[1] if len(vals) > 1 else 0
result = multiply_matrices(result, [1, 0, 0, 1, tx, ty])
elif func == 'scale':
sx = vals[0] if len(vals) > 0 else 1
sy = vals[1] if len(vals) > 1 else sx
result = multiply_matrices(result, [sx, 0, 0, sy, 0, 0])
elif func == 'rotate':
a = math.radians(vals[0])
cos_a, sin_a = math.cos(a), math.sin(a)
result = multiply_matrices(result, [cos_a, sin_a, -sin_a, cos_a, 0, 0])
return result
def multiply_matrices(m1, m2):
"""Multiplie deux matrices de transformation SVG [a,b,c,d,e,f]."""
a1, b1, c1, d1, e1, f1 = m1
a2, b2, c2, d2, e2, f2 = m2
return [
a1*a2 + c1*b2,
b1*a2 + d1*b2,
a1*c2 + c1*d2,
b1*c2 + d1*d2,
a1*e2 + c1*f2 + e1,
b1*e2 + d1*f2 + f1,
]
def apply_transform(matrix, x, y):
"""Applique une matrice de transformation à un point."""
if matrix is None:
return x, y
a, b, c, d, e, f = matrix
return a*x + c*y + e, b*x + d*y + f
# ---------------------------------------------------------------------------
# SVG Element Extraction
# ---------------------------------------------------------------------------
def detect_fill_stroke(elem):
"""Détecte si un élément SVG a un fill et/ou un stroke.
Retourne (has_fill, has_stroke)."""
style = elem.get('style', '')
fill_attr = elem.get('fill')
stroke_attr = elem.get('stroke')
# Parser le style inline
fill_val = None
stroke_val = None
for part in style.split(';'):
part = part.strip()
if part.startswith('fill:'):
fill_val = part.split(':',1)[1].strip()
elif part.startswith('stroke:'):
stroke_val = part.split(':',1)[1].strip()
# Attributs directs (priorité plus basse que style)
if fill_val is None:
fill_val = fill_attr
if stroke_val is None:
stroke_val = stroke_attr
# Défauts SVG : fill=black, stroke=none
has_fill = fill_val is not None and fill_val.lower() != 'none'
if fill_val is None:
has_fill = True # défaut SVG = rempli
has_stroke = stroke_val is not None and stroke_val.lower() != 'none'
return has_fill, has_stroke
def extract_polylines_from_element(elem, parent_transform=None):
"""Extrait les polylines géométriques d'un élément SVG."""
polylines = []
elem_transform = parse_transform(elem.get('transform'))
if parent_transform and elem_transform:
matrix = multiply_matrices(parent_transform, elem_transform)
elif elem_transform:
matrix = elem_transform
else:
matrix = parent_transform
tag = elem.tag.replace(SVG_NS, '')
if tag == 'path':
d = elem.get('d', '')
if d:
for pl in parse_svg_path_d(d):
if matrix:
pl = [apply_transform(matrix, x, y) for x, y in pl]
if len(pl) >= 2:
polylines.append(pl)
elif tag == 'polygon':
pts_str = elem.get('points', '').strip()
if pts_str:
coords = re.findall(r'[+-]?(?:\d+\.?\d*|\.\d+)', pts_str)
pts = []
for j in range(0, len(coords) - 1, 2):
x, y = float(coords[j]), float(coords[j+1])
if matrix:
x, y = apply_transform(matrix, x, y)
pts.append((x, y))
if len(pts) >= 2:
pts.append(pts[0])
polylines.append(pts)
elif tag == 'rect':
if elem.get('id') == 'background':
return polylines
x = float(elem.get('x', 0))
y = float(elem.get('y', 0))
w = float(elem.get('width', 0))
h = float(elem.get('height', 0))
pts = [(x, y), (x+w, y), (x+w, y+h), (x, y+h), (x, y)]
if matrix:
pts = [apply_transform(matrix, px, py) for px, py in pts]
polylines.append(pts)
elif tag == 'circle':
cx_v = float(elem.get('cx', 0))
cy_v = float(elem.get('cy', 0))
r = float(elem.get('r', 0))
if r > 0:
pts = []
for j in range(33):
angle = 2 * math.pi * j / 32
px = cx_v + r * math.cos(angle)
py = cy_v + r * math.sin(angle)
if matrix:
px, py = apply_transform(matrix, px, py)
pts.append((px, py))
polylines.append(pts)
return polylines
def extract_paths_from_element(elem, parent_transform=None):
"""Compat: retourne les polylines sans distinction fill/stroke."""
return extract_polylines_from_element(elem, parent_transform)
def extract_with_fill_info(elem, parent_transform=None):
"""Extrait les polylines avec info fill/stroke.
Retourne (fills, holes, strokes) — trois listes de polylines.
fills = contours extérieurs (dark), holes = évidements (clear)."""
polylines = extract_polylines_from_element(elem, parent_transform)
has_fill, has_stroke = detect_fill_stroke(elem)
fills = []
holes = []
strokes = []
if has_fill and len(polylines) > 1:
# Plusieurs sous-chemins : séparer extérieur / trous via l'aire signée
areas = [(signed_area(pl), pl) for pl in polylines]
# Le sous-chemin avec la plus grande aire absolue = extérieur
# Les autres avec un signe opposé = trous
max_area_item = max(areas, key=lambda x: abs(x[0]))
outer_sign = 1 if max_area_item[0] >= 0 else -1
for area, pl in areas:
if area == 0:
fills.append(pl) # dégénéré, traité comme fill
elif (area > 0) == (outer_sign > 0):
fills.append(pl) # même signe que l'extérieur
else:
holes.append(pl) # signe opposé = trou
elif has_fill:
fills = polylines
if has_stroke:
strokes = polylines
if not has_fill and not has_stroke:
fills = polylines
return fills, holes, strokes
def signed_area(polyline):
"""Aire signée d'un polygone (shoelace formula).
Positif = sens horaire en coordonnées SVG (Y vers le bas)."""
area = 0
n = len(polyline)
for i in range(n):
x1, y1 = polyline[i]
x2, y2 = polyline[(i + 1) % n]
area += (x2 - x1) * (y2 + y1)
return area / 2