/******************************************************************************/ /* */ /* svg2md2draw.cls - SVG to a group of md2slides (a .md2draw file) */ /* =============================================================== */ /* */ /* This program is part of the Rexx Parser package */ /* [See https://rexx.epbcn.com/rexx-parser/] */ /* */ /* Copyright (c) 2024-2026 Josep Maria Blasco */ /* */ /* License: Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0) */ /* */ /* Date Version Details */ /* -------- ------- --------------------------------------------------------- */ /* 20261014 0.7 First version */ /* 20261015 Gradients that fill a box: all of them, as CSS */ /* */ /******************************************************************************/ /* */ /* Rony G. Flatscher, 14/10/2026: "SVG to a group". A box-and-arrow diagram */ /* in SVG (draw.io, Inkscape, md2slides' own "Export SVG") as a group of a */ /* drawing of md2slides, which d's editor can then move, join and restyle: */ /* */ /* a rectangle (rounded too) a box: > box */ /* an ellipse or a circle a round box: > box round */ /* a text the text of the box it lies in, or a text */ /* block of its own: > box bare */ /* a line, a polyline, a path an arrow: > arrow #a -> #b, from the box an */ /* that is not closed end lies on, or from a place of the group */ /* (#group:x,y); heads from its markers, or */ /* from a small closed shape at an end (as */ /* draw.io draws them); no head: -- */ /* */ /* Colours, stroke widths and dashes go in style= (none with skin: the skin */ /* decides). A gradient that fills a box, linear or radial, goes in style= */ /* as CSS (background: linear-gradient(...), radial-gradient(...), or their */ /* repeating-* for spreadMethod repeat and reflect), with all its stops, its */ /* units and its gradientTransform, so that it falls on the box as in the */ /* SVG. The size of the text is in em of the drawing's text, for the */ /* drawing at the width the header of the file says. Everything else is left */ /* out and said (~notes): other polygons, curves (a curved line is drawn */ /* straight, end to end), gradients on lines, borders and texts (their first */ /* colour), the focal point of a radial gradient, turned or slanted shapes, */ /* pictures, and all that svg2jdor leaves out. */ /* */ /* The SVG is read by svg2jdor.cls (.SVG.JDOR): this class is a subclass of */ /* it that takes its shapes and texts instead of drawing them. */ /* */ /* Usage: */ /* */ /* conv = .SVG.MD2DRAW~new */ /* lines = conv~md2draw(svg, "chart", "chart.svg") -- an Array */ /* Do note Over conv~notes; Say note; End */ /* */ /******************************************************************************/ ::Options Digits 16 ::Requires "svg2jdor.cls" ::Class SVG.MD2DRAW Public SubClass SVG.JDOR /******************************************************************************/ /* md2draw: the SVG (an Array of lines or a string) as the lines of a */ /* .md2draw file with one group, named name. skin: no colours. file: the */ /* file's name, for the header's example. */ /******************************************************************************/ ::Method md2draw Expose items skin Use Strict Arg source, name, title = "", skin = 0, file = "" items = .Array~new If file == "" Then file = name".md2draw" discard = self~convert(source, title) size = self~imageSize W = size[1]; H = size[2] out = .Array~new If W <= 0 | H <= 0 Then Return out boxes = .Array~new; texts = .Array~new; lines = .Array~new; heads = .Array~new Do it Over items Select Case it~kind When "box" Then boxes~append(it) When "text" Then texts~append(it) When "line" Then lines~append(it) When "head" Then heads~append(it) End End -- Heads: a small closed shape at the end of a line is its head. Do hd Over heads best = .Nil; bestD = hd~size * 1.5 Do ln Over lines Do e = 1 To 2 d = Dist(hd~cx, hd~cy, ln~x[e], ln~y[e]) If d <= bestD Then Do; best = ln; bestE = e; bestD = d; End End End If best == .Nil Then Do self~note("a polygon", "only rectangles, ellipses and circles make boxes") Iterate End best~head[bestE] = 1 If best~color == "" Then best~color = hd~color -- the line ends at the head's tip: its point farthest from the other end o = 3 - bestE; far = -1 Do pt Over hd~pts d = Dist(pt[1], pt[2], best~x[o], best~y[o]) If d > far Then Do; far = d; best~x[bestE] = pt[1]; best~y[bestE] = pt[2]; End End End -- Texts: in the smallest box their middle is in, or blocks of their own. free = .Array~new Do tx Over texts cx = tx~x + tx~w / 2; cy = tx~y + tx~h / 2 host = .Nil Do bx Over boxes If cx < bx~x | cx > bx~x + bx~w | cy < bx~y | cy > bx~y + bx~h Then Iterate If host == .Nil Then host = bx Else If bx~w * bx~h < host~w * host~h Then host = bx End If host == .Nil Then free~append(tx) Else host~texts~append(tx) End -- A label: a text on a line, about its middle, with nothing round it (or -- only the plain background that hides the line under it) is the line's. Do ln Over lines ln~label = "" a = .Array~of(ln~x[1], ln~y[1]); b = .Array~of(ln~x[2], ln~y[2]) L = Dist(a[1], a[2], b[1], b[2]) If L == 0 Then Iterate cands = .Array~new Do x Over free; cands~append(x); End Do x Over boxes; cands~append(x); End Do cand Over cands If cand~kind == "box" Then Do If cand~stroke \== "" | cand~texts~items \== 1 Then Iterate tx = cand~texts[1] End Else tx = cand If cand~isLabel == 1 Then Iterate cx = cand~x + cand~w / 2; cy = cand~y + cand~h / 2 along = ((cx - a[1]) * (b[1] - a[1]) + (cy - a[2]) * (b[2] - a[2])) / (L * L) If along < 0.2 | along > 0.8 Then Iterate If SegDist(.Array~of(cx, cy), a, b) > Max(cand~h, tx~fpx) Then Iterate ln~label = tx~plain~makeString("L", " ") ln~labelPx = tx~fpx cand~isLabel = 1 Leave End End kept = .Array~new Do tx Over free If tx~isLabel \== 1 Then kept~append(tx) End free = kept kept = .Array~new Do bx Over boxes If bx~isLabel \== 1 Then kept~append(bx) End boxes = kept -- The text sizes, in em of the drawing's 16px, for the drawing at the -- width the header says (at most the stage's 1280px). shown = Min(W, 1280) emOf = shown / W / 16 -- Names: the box's own id when it can be one, else b1, b2... taken = .Set~new taken~put(Lower(name)) n = 0 Do bx Over boxes bx~name = "" If bx~id \== "", IsName(bx~id), \taken~hasIndex(Lower(bx~id)) Then bx~name = bx~id Else Loop n += 1 If \taken~hasIndex("b"n) Then Do; bx~name = "b"n; Leave; End End taken~put(Lower(bx~name)) End n = 0 Do tx Over free Loop n += 1 If \taken~hasIndex("t"n) Then Do; tx~name = "t"n; Leave; End End taken~put(Lower(tx~name)) End -- The header: how the file is used. ratio = Fmt2(W / H) pct = Min(100, Format(W / 12.8, , 0)) If title \== "" Then out~append("") out~append("") out~append(". group id=#"name "at=0,0,100,100") -- The blocks, in the order of the SVG (which is the order they stack in). blocks = .Array~new Do it Over items If it~kind == "box", boxes~hasItem(it) Then blocks~append(it) Else If it~kind == "text", free~hasItem(it) Then blocks~append(it) End Do bl Over blocks out~append(". at="Pct(bl~x, W)","Pct(bl~y, H)","Pct(bl~w, W)","Pct(bl~h, H) "id=#"bl~name) style = .Array~new If bl~kind == "box" Then Do words = "box" If bl~round Then words ||= " round" If bl~dash Then words ||= " dashed" If bl~stroke == "" Then style~append("border: none") Else If \skin Then Do style~append("border-color:" bl~stroke) style~append("border-width:" Fmt2(bl~sw * shown / W)"px") End If bl~fill == "" Then style~append("background: none") Else If \skin Then style~append("background:" bl~fill) If \bl~round, bl~rx > 0 Then style~append("border-radius:" Fmt2(bl~rx / bl~w * 100)"% /" Fmt2(bl~ry / bl~h * 100)"%") body = bl~texts End Else Do words = "box bare" body = .Array~of(bl) style~append("white-space: nowrap") -- its lines are the SVG's End -- the text's colour and size: of the first text in it If body~items > 0 Then Do t1 = body[1] If \skin, t1~color \== "" Then style~append("color:" t1~color) em = t1~fpx * emOf style~append("font-size:" Fmt2(em)"em") End If style~items > 0 Then words ||= ' style="'style~makeString("L", "; ")'"' out~append("." words) para = .Array~new Do tx Over body Do L Over tx~lines para~append(L) End End If para~items == 0 Then para~append(" ") Do i = 1 To para~items If i < para~items Then out~append(para[i]"\") Else out~append(para[i]) End out~append("") End -- The arrows, over the blocks. An end joins the block it is in (the -- smallest), or else the nearest one a little way off (an arrow stops -- short of what it points at); or else it is a place of the group. tol = Max(3, Sqrt(W * W + H * H) / 40) Do ln Over lines ends = .Array~new Do e = 1 To 2 host = .Nil; hostD = tol Do bl Over blocks d = RectDist(ln~x[e], ln~y[e], bl) If d > hostD Then Iterate If host == .Nil | d < hostD Then Do; host = bl; hostD = d; End Else If d == 0, bl~w * bl~h < host~w * host~h Then host = bl End If host == .Nil Then ends~append("#"name":"Pct(ln~x[e], W)","Pct(ln~y[e], H)) Else ends~append("#"host~name) End If ends[1] == ends[2], ends[1]~pos(":") == 0 Then Do self~note("a line inside one box", "it joins nothing") Iterate End Select When ln~head[1] & ln~head[2] Then sign = "<->" When ln~head[2] Then sign = "->" When ln~head[1] Then sign = "<-" Otherwise sign = "--" End opts = "" If \skin, ln~color \== "" Then opts ||= " color="ln~color If \skin Then opts ||= " width="Fmt2(Max(1, ln~sw * shown / W)) If ln~elbow Then opts ||= " route=elbow" If ln~dash Then opts ||= " line=dashed" If ln~label \== "" Then opts ||= ' label="'ln~label~changeStr('"', "'")'"' out~append(". arrow" ends[1] sign ends[2] || opts) End out~append(". end group") Return out /******************************************************************************/ /* What svg2jdor would draw, taken instead. */ /******************************************************************************/ ::Method shapeElement Private Expose items Use Arg node, props, m If WordPos(props["visibility"], "hidden collapse") > 0 Then Return geo = self~geometry(node, props) If geo == .Nil Then Return fill = "" If geo~kind \== "line" Then Do fill = self~cssPaint(props["fill"], props, props["fill-opacity"], 1) -- a gradient that starts clear is still a fill If fill == "", self~gradientDef(props["fill"], props, props["fill-opacity"]) \== .Nil Then fill = "rgba(0,0,0,0)" End stroke = self~cssPaint(props["stroke"], props, props["stroke-opacity"]) sw = SVGLen(props["stroke-width"], 100, props["font-px"]) If sw <= 0 Then stroke = "" If fill == "" & stroke == "" Then Return dash = Default(props["stroke-dasharray"], "none") \== "none" turned = Abs(m[2]) > 1e-9 | Abs(m[3]) > 1e-9 scale = Sqrt(Abs(m[1] * m[4] - m[2] * m[3])) If WordPos(geo~kind, "rect round oval") > 0 Then Do If turned Then Do self~note("a turned or slanted rectangle or ellipse", "a box of md2slides is upright") Return End a = geo~a p = Apply(m, a[1], a[2]); q = Apply(m, a[1] + a[3], a[2] + a[4]) it = .Directory~new it~kind = "box"; it~id = Default(node~attrs["id"], "") it~x = Min(p[1], q[1]); it~y = Min(p[2], q[2]) it~w = Abs(q[1] - p[1]); it~h = Abs(q[2] - p[2]) it~round = geo~kind == "oval" it~rx = 0; it~ry = 0 If geo~kind == "round" Then Do it~rx = a[5] / 2 * Abs(m[1]); it~ry = a[6] / 2 * Abs(m[4]) End fill = self~boxFill(props, fill, .Array~of(a[1], a[2], a[3], a[4]), m, it) it~fill = fill; it~stroke = stroke; it~sw = sw * scale; it~dash = dash it~texts = .Array~new items~append(it) Return End -- A line, a polyline or a path: each piece of it (M ... ) on its own. pieces = .Array~new cur = .Nil Do s Over geo~segs Select Case s[1] When "M" Then Do cur = .Directory~new; cur~pts = .Array~of(Apply(m, s[2], s[3])) cur~closed = 0; cur~curved = 0 pieces~append(cur) End When "Z" Then If cur \== .Nil Then cur~closed = 1 Otherwise If cur \== .Nil Then Do n = s~items prev = cur~pts[cur~pts~items] endP = Apply(m, s[n - 1], s[n]) -- a curve whose control points lie on its chord is a straight line If s[1] \== "L" Then Do k = 2 To n - 3 By 2 cp = Apply(m, s[k], s[k + 1]) If SegDist(cp, prev, endP) > Max(1, Dist(prev[1], prev[2], endP[1], endP[2]) / 50) Then cur~curved = 1 End cur~pts~append(endP) End End End diag = Sqrt(self~imageSize[1] ** 2 + self~imageSize[2] ** 2) Do pc Over pieces If pc~pts~items < 2 Then Iterate xs = .Array~new; ys = .Array~new Do pt Over pc~pts; xs~append(pt[1]); ys~append(pt[2]); End x0 = MinOf(xs); x1 = MaxOf(xs); y0 = MinOf(ys); y1 = MaxOf(ys) If pc~closed | (pc~pts~items > 2 & fill \== "" & stroke == "") Then Do -- a rectangle drawn as a path If \pc~curved, IsRect(pc~pts) Then Do it = .Directory~new it~kind = "box"; it~id = Default(node~attrs["id"], "") it~x = x0; it~y = y0; it~w = x1 - x0; it~h = y1 - y0 it~round = 0; it~rx = 0; it~ry = 0 -- the box in the element's own units, for a gradient on it ub = .Nil inv = Inverse(m) If inv \== .Nil Then Do p = Apply(inv, x0, y0); q = Apply(inv, x1, y1) ub = .Array~of(Min(p[1], q[1]), Min(p[2], q[2]), Abs(q[1] - p[1]), Abs(q[2] - p[2])) End If ub \== .Nil Then fill = self~boxFill(props, fill, ub, m, it) it~fill = fill; it~stroke = stroke; it~sw = sw * scale; it~dash = dash it~texts = .Array~new items~append(it) Iterate End -- a small one: perhaps the head of a line size = Max(x1 - x0, y1 - y0) If size <= diag / 25 Then Do it = .Directory~new it~kind = "head"; it~cx = (x0 + x1) / 2; it~cy = (y0 + y1) / 2; it~size = size it~pts = pc~pts it~color = fill If it~color == "" Then it~color = stroke If self~gradientDef(props["fill"], props, props["fill-opacity"]) \== .Nil Then self~note("a gradient on a line, a border or a text", "it is drawn with its first colour") items~append(it) Iterate End self~note("a polygon or a closed curve", "only rectangles, ellipses and circles make boxes") Iterate End If stroke == "" Then Iterate elbow = \pc~curved & IsElbow(pc~pts) If \elbow, pc~curved | pc~pts~items > 2 Then self~note("a line that bends or curves", "drawn straight, from end to end") it = .Directory~new it~kind = "line" first = pc~pts[1]; last = pc~pts[pc~pts~items] it~x = .Array~of(first[1], last[1]); it~y = .Array~of(first[2], last[2]) it~head = .Array~of(0, 0) If Marked(props["marker-start"]) | Marked(props["marker"]) Then it~head[1] = 1 If Marked(props["marker-end"]) | Marked(props["marker"]) Then it~head[2] = 1 it~color = stroke; it~sw = sw * scale; it~dash = dash; it~elbow = elbow items~append(it) End ::Method textElement Private Expose items Use Arg node, props, m If WordPos(props["visibility"], "hidden collapse") > 0 Then Return If Abs(m[2]) > 1e-9 | Abs(m[3]) > 1e-9 Then Do self~note("a turned or slanted text", "the text of md2slides is upright") Return End size = self~imageSize fpx = props["font-px"] x = SVGFirstLen(node~attrs["x"], size[1], fpx) + SVGFirstLen(node~attrs["dx"], size[1], fpx) y = SVGFirstLen(node~attrs["y"], size[2], fpx) + SVGFirstLen(node~attrs["dy"], size[2], fpx) runs = .Array~new run = .Directory~new run~x = x; run~y = y; run~props = props; run~text = "" runs~append(run) self~textRuns(node, props, runs) -- Each run where it goes, in the SVG's px; then the lines (by y). lines = .Array~new -- Directory: y, left, right, text endX = x Do r Over runs t = SVGCollapse(r~text) If r == runs[1] Then t = t~strip("L") If r == runs[runs~items] Then t = t~strip("T") If t~strip == "" Then Iterate p = r~props bold = Default(p["font-weight"], "normal") == "bold" | Number(p["font-weight"], 400) >= 600 w = SVGTextWidth(t, p["font-px"], Default(p["font-family"], "serif"), bold) rx = r~x If rx == .Nil Then rx = endX Else Select Case Default(p["text-anchor"], "start") When "middle" Then rx = rx - w / 2 When "end" Then rx = rx - w Otherwise Nop End endX = rx + w md = MdEscape(t) If bold Then md = "**"md~strip"**" If Default(p["font-style"], "normal") == "italic" Then md = "*"md~strip"*" hit = .Nil Do L Over lines If Abs(L~y - r~y) < p["font-px"] * 0.3 Then hit = L End If hit == .Nil Then Do hit = .Directory~new; hit~y = r~y; hit~left = rx; hit~right = rx + w; hit~text = md hit~plain = t~strip lines~append(hit) End Else Do gap = "" If rx > hit~right + p["font-px"] * 0.15 Then gap = " " hit~text = hit~text || gap || md hit~plain = hit~plain || gap || t~strip hit~left = Min(hit~left, rx); hit~right = Max(hit~right, rx + w) End End If lines~items == 0 Then Return -- top to bottom Do i = 2 To lines~items Do j = i To 2 By -1 If lines[j]~y >= lines[j - 1]~y Then Leave tmp = lines[j]; lines[j] = lines[j - 1]; lines[j - 1] = tmp End End left = 1e99; right = -1e99 Do L Over lines left = Min(left, L~left); right = Max(right, L~right) End top = lines[1]~y - fpx * 0.9 bottom = lines[lines~items]~y + fpx * 0.3 p = Apply(m, left, top); q = Apply(m, right, bottom) it = .Directory~new it~kind = "text" it~x = Min(p[1], q[1]); it~y = Min(p[2], q[2]) it~w = Abs(q[1] - p[1]); it~h = Abs(q[2] - p[2]) it~fpx = fpx * Abs(m[4]) it~color = self~cssPaint(props["fill"], props, props["fill-opacity"]) it~lines = .Array~new; it~plain = .Array~new Do L Over lines it~lines~append(L~text) it~plain~append(L~plain) End items~append(it) -- What svg2jdor says, as it is for md2slides: markers are the arrows' heads -- here, and what JDOR has nothing like is what a drawing has nothing like. ::Method note Private Use Arg name, why If name == "" Then Return why = why~changeStr("it has no JDOR equivalent", "a drawing of md2slides has nothing like it") why = why~changeStr("they have no JDOR equivalent", "a drawing of md2slides has nothing like them") why = why~changeStr("JDOR fills text only", "md2slides fills text only") self~note:super(name, why) -- A paint as CSS: "#rrggbb", "rgba(...)", or "" for none. A gradient is its -- first colour here (and said, unless quiet): the fill of a box gets all of -- it from boxFill. ::Method cssPaint Private Use Arg v, props, opacity, quiet = 0 g = self~gradientDef(v, props, opacity) If g \== .Nil Then Do If \quiet Then self~note("a gradient on a line, a border or a text", "it is drawn with its first colour") c = g~stops[1] End Else Do c = self~paintOf(v, props, Number(opacity, 1)) If c == .Nil Then Return "" If c~kind \== "color" Then c = c~c1 End If c~a == 0 Then Return "" Return ColorCSS(c) -- The fill of a box: a gradient as CSS (linear-gradient, radial-gradient or -- their repeating-*), drawn on the box as the SVG draws it on the shape; else -- the colour that was found (fill). ub: the shape's box in its own units (x y -- w h), which a gradient in objectBoundingBox units is of; m: the matrix from -- those units to the page's; it: the box on the page (~x ~y ~w ~h). ::Method boxFill Private Use Arg props, fill, ub, m, it g = self~gradientDef(props["fill"], props, props["fill-opacity"]) If g == .Nil Then Return fill If it~w <= 0 | it~h <= 0 Then Return fill -- gradient space -> page: m . (the box, when objectBoundingBox) . gradientTransform B = .Array~of(1, 0, 0, 1, 0, 0) If g~bb Then Do If ub[3] <= 0 | ub[4] <= 0 Then Return fill B = .Array~of(ub[3], 0, 0, ub[4], ub[1], ub[2]) End T = Mul(m, Mul(B, g~gt)) stops = g~stops last = stops[stops~items] -- the stops of one period: offsets 0 to 1 for repeat, 0 to 2 for reflect If g~spread \== "pad" Then Do per = .Array~new If stops[1]~offset > 0 Then Do c = stops[1]~copy; c~offset = 0; per~append(c) End Do s Over stops; per~append(s); End If last~offset < 1 Then Do c = last~copy; c~offset = 1; per~append(c) End If g~spread == "reflect" Then Do n = per~items Do i = n - 1 To 1 By -1 c = per[i]~copy; c~offset = 2 - per[i]~offset; per~append(c) End End stops = per End stops = AsSVGBlends(stops) rep = "" If g~spread \== "pad" Then rep = "repeating-" If g~kind == "linear" Then Do -- t, the offset, as a function of the page: t = a*x + b*y + c Dx = g~x2 - g~x1; Dy = g~y2 - g~y1 D2 = Dx * Dx + Dy * Dy inv = Inverse(T) If D2 < 1e-18 | inv == .Nil Then Return ColorCSS(last) a = (inv[1] * Dx + inv[2] * Dy) / D2 b = (inv[3] * Dx + inv[4] * Dy) / D2 c = ((inv[5] - g~x1) * Dx + (inv[6] - g~y1) * Dy) / D2 gl = Sqrt(a * a + b * b) If gl < 1e-18 Then Return ColorCSS(last) -- CSS: 0deg points up, 90deg right; the line runs through the middle, -- as long as the box is across in that direction rad = Atan2(a, -b) deg = rad * 180 / 3.141592653589793 If deg < 0 Then deg += 360 sn = a / gl; cs = -b / gl L = Abs(it~w * sn) + Abs(it~h * cs) Sx = it~x + it~w / 2 - sn * L / 2 Sy = it~y + it~h / 2 + cs * L / 2 tS = a * Sx + b * Sy + c span = L * gl list = .Array~new Do s Over stops list~append(ColorCSS(s) Fmt2((s~offset - tS) / span * 100)"%") End Return rep"linear-gradient("Fmt2(deg)"deg," list~makeString("L", ", ")")" End -- radial: the circle (cx cy r) on the page is an ellipse If g~r <= 0 Then Return ColorCSS(last) If Abs(T[2]) > 1e-9 | Abs(T[3]) > 1e-9 Then self~note("a turned or slanted radial gradient", "it is drawn upright") ctr = Apply(T, g~cx, g~cy) Rx = g~r * Sqrt(T[1] ** 2 + T[2] ** 2) Ry = g~r * Sqrt(T[3] ** 2 + T[4] ** 2) If Rx <= 0 | Ry <= 0 Then Return ColorCSS(last) If Abs(g~fx - g~cx) > 1e-9 | Abs(g~fy - g~cy) > 1e-9 Then self~note("the focal point of a radial gradient (fx, fy)", "CSS draws it from its centre") fr = Max(0, Min(g~fr, g~r)) list = .Array~new Do s Over stops list~append(ColorCSS(s) Fmt2((fr + s~offset * (g~r - fr)) / g~r * 100)"%") End shape = "ellipse" Fmt2(Rx / it~w * 100)"%" Fmt2(Ry / it~h * 100)"%" where = "at" Fmt2((ctr[1] - it~x) / it~w * 100)"%" Fmt2((ctr[2] - it~y) / it~h * 100)"%" Return rep"radial-gradient("shape where"," list~makeString("L", ", ")")" /******************************************************************************/ /* Helpers */ /******************************************************************************/ ::Routine Apply Use Arg m, x, y Return .Array~of(m[1] * x + m[3] * y + m[5], m[2] * x + m[4] * y + m[6]) ::Routine Mul Use Arg p, q Return .Array~of(p[1] * q[1] + p[3] * q[2], p[2] * q[1] + p[4] * q[2], - p[1] * q[3] + p[3] * q[4], p[2] * q[3] + p[4] * q[4], - p[1] * q[5] + p[3] * q[6] + p[5], p[2] * q[5] + p[4] * q[6] + p[6]) ::Routine Inverse Use Arg m det = m[1] * m[4] - m[2] * m[3] If Abs(det) < 1e-12 Then Return .Nil a = m[4] / det; b = -m[2] / det; c = -m[3] / det; d = m[1] / det Return .Array~of(a, b, c, d, -(a * m[5] + c * m[6]), -(b * m[5] + d * m[6])) -- A colour (r g b a, 0-255) as CSS: #rrggbb when it is opaque. ::Routine ColorCSS Use Arg c hex = "#"c~r~d2x(2)~lower || c~g~d2x(2)~lower || c~b~d2x(2)~lower If c~a >= 255 Then Return hex Return "rgba("c~r","c~g","c~b","Fmt2(c~a / 255)")" -- SVG blends two stops colour by colour and opacity by opacity; CSS blends -- them as the colour shows (premultiplied). The same when the opacity or the -- colour stays; else, between them, stops that blend as the SVG does. ::Routine AsSVGBlends Use Arg stops out = .Array~new Do i = 1 To stops~items s = stops[i] If i > 1 Then Do p = stops[i - 1] same = p~r == s~r & p~g == s~g & p~b == s~b If p~a \== s~a, \same, s~offset > p~offset Then Do k = 1 To 3 f = k / 4 c = s~copy c~r = Format(p~r + (s~r - p~r) * f, , 0); c~g = Format(p~g + (s~g - p~g) * f, , 0) c~b = Format(p~b + (s~b - p~b) * f, , 0); c~a = Format(p~a + (s~a - p~a) * f, , 0) c~offset = p~offset + (s~offset - p~offset) * f out~append(c) End End out~append(s) End Return out ::Routine Atan2 Use Arg y, x pi = 3.141592653589793 If x > 0 Then Return RxCalcArcTan(y / x, 16, "R") If x < 0, y >= 0 Then Return RxCalcArcTan(y / x, 16, "R") + pi If x < 0 Then Return RxCalcArcTan(y / x, 16, "R") - pi If y > 0 Then Return pi / 2 If y < 0 Then Return -pi / 2 Return 0 ::Routine RxCalcArcTan External "LIBRARY rxmath RxCalcArcTan" ::Routine Default Use Arg v, d If v == .Nil Then Return d Return v ::Routine Number Use Arg v, d If v == .Nil Then Return d v = v~strip If DataType(v, "N") Then Return v + 0 If v~right(1) == "%", DataType(v~left(v~length - 1), "N") Then Return v~left(v~length - 1) / 100 Return d ::Routine Marked Use Arg v If v == .Nil Then Return 0 Return v~strip \== "none" & v~strip \== "" ::Routine Dist Use Arg x1, y1, x2, y2 Return Sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) -- Three points, two straight pieces, one across and one up or down (either -- way): the elbow md2slides draws with route=elbow. ::Routine IsElbow Use Arg pts If pts~items \== 3 Then Return 0 a = pts[1]; b = pts[2]; c = pts[3] h1 = Abs(a[2] - b[2]) < 1e-6; v1 = Abs(a[1] - b[1]) < 1e-6 h2 = Abs(b[2] - c[2]) < 1e-6; v2 = Abs(b[1] - c[1]) < 1e-6 Return (h1 & v2) | (v1 & h2) -- How far a point is from a block (0 inside it). ::Routine RectDist Use Arg x, y, b dx = Max(b~x - x, 0, x - (b~x + b~w)) dy = Max(b~y - y, 0, y - (b~y + b~h)) Return Sqrt(dx * dx + dy * dy) -- How far a point is from the line through a and b. ::Routine SegDist Use Arg p, a, b L = Dist(a[1], a[2], b[1], b[2]) If L == 0 Then Return Dist(p[1], p[2], a[1], a[2]) Return Abs((b[1] - a[1]) * (a[2] - p[2]) - (a[1] - p[1]) * (b[2] - a[2])) / L ::Routine MinOf Use Arg a r = a[1] Do v Over a; r = Min(r, v); End Return r ::Routine MaxOf Use Arg a r = a[1] Do v Over a; r = Max(r, v); End Return r -- Four corners (a fifth that closes it may follow), upright: a rectangle. ::Routine IsRect Use Arg pts n = pts~items If n == 5 Then Do If Abs(pts[5][1] - pts[1][1]) < 1e-6, Abs(pts[5][2] - pts[1][2]) < 1e-6 Then n = 4 End If n \== 4 Then Return 0 Do i = 1 To 4 a = pts[i]; b = pts[i // 4 + 1] If Abs(a[1] - b[1]) > 1e-6 & Abs(a[2] - b[2]) > 1e-6 Then Return 0 End Return 1 -- A per cent, with one decimal at most. ::Routine Pct Use Arg v, full Return Fmt1(v / full * 100) ::Routine Fmt1 Use Arg v v = Format(v, , 1) If v~right(2) == ".0" Then v = v~left(v~length - 2) If v == "-0" Then v = "0" Return v ::Routine Fmt2 Use Arg v v = Format(v, , 2) Loop While v~pos(".") > 0, v~right(1) == "0" v = v~left(v~length - 1) End If v~right(1) == "." Then v = v~left(v~length - 1) If v == "-0" Then v = "0" Return v -- A name md2slides takes for a block: a letter, then letters, digits, - or _. ::Routine IsName Public Use Arg v If v == "" Then Return 0 If \DataType(v~left(1), "M") Then Return 0 Return v~verify("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_") == 0 -- Text as Markdown that says only that text: what would be markup is escaped, -- and a line cannot start a list, a heading or a line of md2slides (>). ::Routine MdEscape Public Use Arg t out = "" Do i = 1 To t~length c = t~subchar(i) If "\`*_[]<>#|"~pos(c) > 0 Then out ||= "\"c Else out ||= c End If out~left(1) == "-" | out~left(1) == "+" Then out = "\"out If out~left(1) == "." Then out = "\"out -- (v315) not a line of md2slides Parse Var out num "." rest If num \== "", DataType(num, "W"), out~pos(".") == num~length + 1 Then out = num"\."rest Return out ::Routine Sqrt Use Arg x If x <= 0 Then Return 0 r = x Do 60 r2 = (r + x / r) / 2 If Abs(r2 - r) < 1e-12 * r2 Then Leave r = r2 End Return r