1- """Shared drawing helpers of the documentation figures.
1+ """Shared drawing helpers of the FEAScript figures (documentation and website tutorials) .
22
3- Black line art on a transparent background (inverted in dark mode by src/css/custom.css), sans-serif 15 px labels,
4- boundary keys in grey 12 px, dashed dimension lines with arrows, and thin grey mesh edges.
3+ The same file is in FEAScript-documentation/scripts and FEAScript-website/scripts; keep the two copies identical.
4+
5+ Style: line art in the FEAScript colors, Roboto Mono labels, a sunken fill for domains, and boundaries colored by the
6+ type of their condition. Every SVG carries a small stylesheet that maps a few fixed colors to CSS variables:
7+
8+ #000 ink, #666 muted text, #fff background, #f6efd9 domain fill,
9+ #c8003f given value (Dirichlet), #d4aa00 convection or spring (Robin), #0086d4 given flux, force or load,
10+ #00a86b zero flux (symmetry, insulated, zero gradient, stress-free), #8f7200 gold text.
11+
12+ The variables (--fig-ink, --fig-muted, --fig-bg, --fig-area, --fig-value, --fig-robin, --fig-flux, --fig-zero,
13+ --fig-gold-ink) are set by the page for its light and dark themes, and the pages inline the SVG so that it follows the
14+ theme. Opened on its own, an SVG falls back to the light colors. The fixed colors double as names: draw with them,
15+ never with other colors, so that every figure stays in the scheme.
16+
17+ Series in plots: FEAScript results in flow blue (#0086d4), references and exact solutions in ink, dashed.
518"""
619
720import math
821
22+ INK , MUTED , BG , AREA = "#000" , "#666" , "#fff" , "#f6efd9"
23+ VALUE , ROBIN , FLUX , ZERO , GOLD_TEXT = "#c8003f" , "#d4aa00" , "#0086d4" , "#00a86b" , "#8f7200"
24+ SERIES = [FLUX , VALUE , ROBIN ] # Colors of the first, second and third series of a plot
25+
26+ STYLE = """ <style>
27+ .fig { font-family: "Roboto Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
28+ .fig[fill="#000"], .fig [fill="#000"] { fill: var(--fig-ink, #333333); }
29+ .fig [stroke="#000"] { stroke: var(--fig-ink, #333333); }
30+ .fig [fill="#666"] { fill: var(--fig-muted, #6b6960); }
31+ .fig [fill="#fff"] { fill: var(--fig-bg, #fdfaf2); }
32+ .fig [stroke="#fff"] { stroke: var(--fig-bg, #fdfaf2); }
33+ .fig [fill="#f6efd9"] { fill: var(--fig-area, #f6efd9); }
34+ .fig [stroke="#f6efd9"] { stroke: var(--fig-area, #f6efd9); }
35+ .fig [stroke="#c8003f"] { stroke: var(--fig-value, #c8003f); }
36+ .fig [fill="#c8003f"] { fill: var(--fig-value, #c8003f); }
37+ .fig [stroke="#d4aa00"] { stroke: var(--fig-robin, #d4aa00); }
38+ .fig [fill="#d4aa00"] { fill: var(--fig-robin, #d4aa00); }
39+ .fig [stroke="#0086d4"] { stroke: var(--fig-flux, #0086d4); }
40+ .fig [fill="#0086d4"] { fill: var(--fig-flux, #0086d4); }
41+ .fig [stroke="#00a86b"] { stroke: var(--fig-zero, #00a86b); }
42+ .fig [fill="#00a86b"] { fill: var(--fig-zero, #00a86b); }
43+ .fig [fill="#8f7200"] { fill: var(--fig-gold-ink, #8f7200); }
44+ .fig [font-size="11"] { font-size: 10px; }
45+ .fig [font-size="12"] { font-size: 11px; }
46+ .fig [font-size="13"] { font-size: 12px; }
47+ .fig [font-size="16"] { font-size: 14px; }
48+ .fig [font-size="18"] { font-size: 15px; }
49+ </style>"""
50+
951DEFS = """ <defs>
1052 <marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
1153 <path d="M0,0 L10,5 L0,10 z" fill="#000"/>
1254 </marker>
55+ <marker id="arrow-flux" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse">
56+ <path d="M0,0 L10,5 L0,10 z" fill="#0086d4"/>
57+ </marker>
1358 </defs>"""
1459
15- HALO = 'stroke="#fff" stroke-width="4" stroke-linejoin="round" paint-order="stroke"' # white outline behind labels
60+ HALO = 'stroke="#fff" stroke-width="4" stroke-linejoin="round" paint-order="stroke"' # Background outline behind labels
1661
1762MESH_STYLE = 'fill="none" stroke="#000" stroke-width="0.6" stroke-opacity="0.45" stroke-linejoin="round"'
1863
@@ -23,8 +68,9 @@ def fmt(value):
2368
2469def svg (width , height , body , comment ):
2570 return (
26- f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 { width } { height } " width="{ width } " height="{ height } " '
27- f'font-family="sans-serif" font-size="15" fill="#000">\n <!-- { comment } -->\n { DEFS } \n '
71+ f'<svg xmlns="http://www.w3.org/2000/svg" class="fig" viewBox="0 0 { width } { height } " width="{ width } " '
72+ f'height="{ height } " font-family="Roboto Mono, monospace" font-size="13" fill="#000">\n <!-- { comment } -->\n '
73+ f"{ STYLE } \n { DEFS } \n "
2874 + "\n " .join (" " + line for line in body )
2975 + "\n </svg>\n "
3076 )
@@ -55,6 +101,31 @@ def text(x, y, content, size=15, fill="#000", anchor="middle", rotate=None, weig
55101 return f"<text { attributes } >{ content } </text>"
56102
57103
104+ def bname (boundary ):
105+ """A boundary name as it is written in code, for the key line under a condition: "bottom"."""
106+ return f'"{ boundary } "'
107+
108+
109+ def kind (condition ):
110+ """The color of a boundary from the wording of its condition (see the module docstring)."""
111+ plain = condition .lower ()
112+ for tag in ("<tspan" , "</tspan>" ):
113+ plain = plain .replace (tag , " " )
114+ if "convection" in plain or "spring" in plain :
115+ return ROBIN
116+ if any (word in plain for word in ("symmetry" , "insulated" , "zero gradient" , "stress-free" , "∂" , "d<" )):
117+ return ZERO
118+ if "force" in plain or "load" in plain :
119+ return FLUX
120+ return VALUE
121+
122+
123+ def edge (points , color , width = 4 ):
124+ """A boundary drawn over the outline in the color of its condition."""
125+ path = " " .join (f"{ fmt (x )} ,{ fmt (y )} " for x , y in points )
126+ return f'<polyline points="{ path } " fill="none" stroke="{ color } " stroke-width="{ width } " stroke-linecap="round" stroke-linejoin="round"/>'
127+
128+
58129def label (x , y , condition , key , side ):
59130 """Boundary condition and its key, placed outside a domain edge (side: bottom, top, left or right)."""
60131 if side == "bottom" :
@@ -74,14 +145,15 @@ def dimension(x1, y1, x2, y2, content, offset_x=0, offset_y=0, rotate=None):
74145 return [line , text ((x1 + x2 ) / 2 + offset_x , (y1 + y2 ) / 2 + offset_y , content , rotate = rotate )]
75146
76147
77- def outline (points , width = 2 ):
148+ def outline (points , width = 2 , filled = False ):
78149 path = " " .join (f"{ fmt (x )} ,{ fmt (y )} " for x , y in points )
79- return f'<polygon points="{ path } " fill="none" stroke="#000" stroke-width="{ width } " stroke-linejoin="round"/>'
150+ fill = AREA if filled else "none"
151+ return f'<polygon points="{ path } " fill="{ fill } " stroke="#000" stroke-width="{ width } " stroke-linejoin="round"/>'
80152
81153
82- def dot (x , y , radius = 3 , filled = True ):
83- fill = "#000" if filled else "#fff"
84- return f'<circle cx="{ fmt (x )} " cy="{ fmt (y )} " r="{ radius } " fill="{ fill } " stroke="#000 " stroke-width="1"/>'
154+ def dot (x , y , radius = 3 , filled = True , color = "#000" ):
155+ fill = color if filled else "#fff"
156+ return f'<circle cx="{ fmt (x )} " cy="{ fmt (y )} " r="{ radius } " fill="{ fill } " stroke="{ color } " stroke-width="1.2 "/>'
85157
86158
87159def origin (frame ):
@@ -98,16 +170,20 @@ def sub(content):
98170
99171
100172def structured_mesh (frame , width , height , nx , ny ):
101- lines = []
173+ """Grid lines of an nx x ny mesh on a filled rectangle; draw the outline after it."""
174+ x0 , y0 = frame (0 , 0 )
175+ x1 , y1 = frame (width , height )
176+ lines = [f'<rect x="{ fmt (min (x0 , x1 ))} " y="{ fmt (min (y0 , y1 ))} " width="{ fmt (abs (x1 - x0 ))} " height="{ fmt (abs (y1 - y0 ))} " fill="{ AREA } "/>' ]
177+ grid = []
102178 for i in range (1 , nx ):
103- x1 , y1 = frame (width * i / nx , 0 )
104- x2 , y2 = frame (width * i / nx , height )
105- lines .append (f'<line x1="{ fmt (x1 )} " y1="{ fmt (y1 )} " x2="{ fmt (x2 )} " y2="{ fmt (y2 )} "/>' )
179+ xa , ya = frame (width * i / nx , 0 )
180+ xb , yb = frame (width * i / nx , height )
181+ grid .append (f'<line x1="{ fmt (xa )} " y1="{ fmt (ya )} " x2="{ fmt (xb )} " y2="{ fmt (yb )} "/>' )
106182 for j in range (1 , ny ):
107- x1 , y1 = frame (0 , height * j / ny )
108- x2 , y2 = frame (width , height * j / ny )
109- lines .append (f'<line x1="{ fmt (x1 )} " y1="{ fmt (y1 )} " x2="{ fmt (x2 )} " y2="{ fmt (y2 )} "/>' )
110- return [f"<g { MESH_STYLE } >" ] + [" " + line for line in lines ] + ["</g>" ]
183+ xa , ya = frame (0 , height * j / ny )
184+ xb , yb = frame (width , height * j / ny )
185+ grid .append (f'<line x1="{ fmt (xa )} " y1="{ fmt (ya )} " x2="{ fmt (xb )} " y2="{ fmt (yb )} "/>' )
186+ return lines + [f"<g { MESH_STYLE } >" ] + [" " + line for line in grid ] + ["</g>" ]
111187
112188
113189def read_gmsh (path ):
@@ -141,15 +217,26 @@ def read_gmsh(path):
141217
142218
143219def gmsh_mesh (frame , polygons ):
144- body = [f" <g { MESH_STYLE } >" ]
220+ body = [f' <g fill=" { AREA } " stroke="#000" stroke-width="0.6" stroke-opacity="0.45" stroke-linejoin="round">' ]
145221 for polygon in polygons :
146222 points = " " .join (f"{ fmt (px )} ,{ fmt (py )} " for px , py in (frame (x , y ) for x , y in polygon ))
147223 body .append (f' <polygon points="{ points } "/>' )
148224 return body + ["</g>" ]
149225
150226
151- def rectangle (frame , width , height ):
152- return outline ([frame (0 , 0 ), frame (width , 0 ), frame (width , height ), frame (0 , height )])
227+ def rectangle (frame , width , height , filled = True ):
228+ return outline ([frame (0 , 0 ), frame (width , 0 ), frame (width , height ), frame (0 , height )], filled = filled )
229+
230+
231+ def rectangle_edges (frame , width , height , colors ):
232+ """The four sides of a rectangle in the colors of their conditions: colors = (bottom, left, top, right)."""
233+ corners = {"bottom" : ((0 , 0 ), (width , 0 )), "left" : ((0 , 0 ), (0 , height )), "top" : ((0 , height ), (width , height )), "right" : ((width , 0 ), (width , height ))}
234+ return [edge ([frame (* a ), frame (* b )], color ) for (a , b ), color in zip (corners .values (), colors ) if color ]
235+
236+
237+ def series_style (k ):
238+ """Color and dash pattern of the k-th series of a plot."""
239+ return SERIES [k % len (SERIES )], ["" , ' stroke-dasharray="5 4"' , ' stroke-dasharray="1.5 3"' ][k % 3 ]
153240
154241
155242def log_plot (series , x_label , y_label , comment , width = 640 , height = 360 , padding = 0.25 ):
@@ -158,9 +245,8 @@ def log_plot(series, x_label, y_label, comment, width=640, height=360, padding=0
158245 Gridlines and ticks are at the decades, labelled as powers of ten; an axis that spans less than two decades
159246 gets ticks at 1, 2 and 5 times the decades instead, labelled as plain numbers.
160247
161- series: list of (name, points), at most three. The first is a solid line with open circles, the second a
162- dashed line with filled circles, as in the line plots of the example pages, and the third a dotted line with
163- open squares.
248+ series: list of (name, points), at most three, in flow blue, crimson and gold; the first is a solid line with
249+ open circles, the second a dashed line with filled circles and the third a dotted line with open squares.
164250 """
165251 left , right , top , bottom = 90 , width - 40 , 30 , height - 60
166252 xs = [math .log10 (x ) for _ , points in series for x , _ in points ]
@@ -191,7 +277,7 @@ def ticks(low, high):
191277 return [(v , f"{ v :g} " ) for v in values if low <= math .log10 (v ) <= high ]
192278
193279 x_ticks , y_ticks = ticks (x_min , x_max ), ticks (y_min , y_max )
194- body = ['<g stroke="#000" stroke-width="0.6" stroke-opacity="0.25 ">' ]
280+ body = ['<g stroke="#000" stroke-width="0.6" stroke-opacity="0.2 ">' ]
195281 body += [f' <line x1="{ fmt (px (v ))} " y1="{ top } " x2="{ fmt (px (v ))} " y2="{ bottom } "/>' for v , _ in x_ticks ]
196282 body += [f' <line x1="{ left } " y1="{ fmt (py (v ))} " x2="{ right } " y2="{ fmt (py (v ))} "/>' for v , _ in y_ticks ]
197283 body += ["</g>" ]
@@ -201,18 +287,20 @@ def ticks(low, high):
201287 body += [text (left - 8 , py (v ) + 4 , label , 13 , anchor = "end" ) for v , label in y_ticks ]
202288 body += [text ((left + right ) / 2 , height - 14 , x_label )]
203289 body += [text (24 , (top + bottom ) / 2 , y_label , rotate = - 90 )]
290+
204291 def marker (k , x , y ):
292+ color , _ = series_style (k )
205293 if k == 2 : # Open square
206- return f'<rect x="{ fmt (x - 3.5 )} " y="{ fmt (y - 3.5 )} " width="7" height="7" fill="#fff" stroke="#000 " stroke-width="1"/>'
207- return dot (x , y , 3.5 , filled = k == 1 )
294+ return f'<rect x="{ fmt (x - 3.5 )} " y="{ fmt (y - 3.5 )} " width="7" height="7" fill="#fff" stroke="{ color } " stroke-width="1.2 "/>'
295+ return dot (x , y , 3.5 , filled = k == 1 , color = color )
208296
209- for k , (name , points ) in enumerate (series ):
210- dash = [ "" , ' stroke-dasharray="5 4"' , ' stroke-dasharray="1.5 3"' ][ k ]
297+ for k , (name_ , points ) in enumerate (series ):
298+ color , dash = series_style ( k )
211299 path = " " .join (f"{ fmt (px (x ))} ,{ fmt (py (y ))} " for x , y in points )
212- body .append (f'<polyline points="{ path } " fill="none" stroke="#000 " stroke-width="1.5 "{ dash } />' )
300+ body .append (f'<polyline points="{ path } " fill="none" stroke="{ color } " stroke-width="2 "{ dash } />' )
213301 body += [marker (k , px (x ), py (y )) for x , y in points ]
214302 ly = top + 18 + 22 * k
215- body .append (f'<line x1="{ left + 18 } " y1="{ ly } " x2="{ left + 50 } " y2="{ ly } " stroke="#000 " stroke-width="1.5 "{ dash } />' )
303+ body .append (f'<line x1="{ left + 18 } " y1="{ ly } " x2="{ left + 50 } " y2="{ ly } " stroke="{ color } " stroke-width="2 "{ dash } />' )
216304 body .append (marker (k , left + 34 , ly ))
217- body .append (f'<text x="{ left + 60 } " y="{ ly + 4 } " font-size="13" { HALO } >{ name } </text>' )
305+ body .append (f'<text x="{ left + 60 } " y="{ ly + 4 } " font-size="13" { HALO } >{ name_ } </text>' )
218306 return svg (width , height , body , comment )
0 commit comments