Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : Groupe Ka — app iOS native7// File : Shared/SVGPath.swift8// Module : Utilities9// Repository : https://git.spboucher.ai/groupe-ka-ios10// Project Website : Not specified11// Created : 2026-12-0312// Last Modified : 2026-12-0313//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI4243/// Lecture d'un attribut `d` de SVG (M L H V C S Q A Z, absolus et relatifs) vers un `Path` SwiftUI.44/// Sert à dessiner les symboles des marques avec les tracés exacts publiés par le site, sans retouche.45enum SVGPath {46 static func path(_ d: String) -> Path {47 var lecteur = Lecteur(d)48 var p = Path()49 var courant = CGPoint.zero50 var depart = CGPoint.zero51 var controle: CGPoint?52 var commande: Character = "M"5354 while true {55 lecteur.passerBlancs()56 guard !lecteur.fini else { break }57 if let c = lecteur.commande() { commande = c } else if commande == "Z" || commande == "z" { break }58 let relatif = commande.isLowercase59 func pt(_ x: CGFloat, _ y: CGFloat) -> CGPoint {60 relatif ? CGPoint(x: courant.x + x, y: courant.y + y) : CGPoint(x: x, y: y)61 }62 switch commande {63 case "M", "m":64 guard let x = lecteur.nombre(), let y = lecteur.nombre() else { return p }65 courant = pt(x, y); depart = courant66 p.move(to: courant)67 commande = relatif ? "l" : "L" // les paires suivantes sont des lignes68 controle = nil69 case "L", "l":70 guard let x = lecteur.nombre(), let y = lecteur.nombre() else { return p }71 courant = pt(x, y); p.addLine(to: courant); controle = nil72 case "H", "h":73 guard let x = lecteur.nombre() else { return p }74 courant = CGPoint(x: relatif ? courant.x + x : x, y: courant.y); p.addLine(to: courant); controle = nil75 case "V", "v":76 guard let y = lecteur.nombre() else { return p }77 courant = CGPoint(x: courant.x, y: relatif ? courant.y + y : y); p.addLine(to: courant); controle = nil78 case "C", "c":79 guard let x1 = lecteur.nombre(), let y1 = lecteur.nombre(), let x2 = lecteur.nombre(), let y2 = lecteur.nombre(),80 let x = lecteur.nombre(), let y = lecteur.nombre() else { return p }81 let c1 = pt(x1, y1), c2 = pt(x2, y2), fin = pt(x, y)82 p.addCurve(to: fin, control1: c1, control2: c2)83 controle = c2; courant = fin84 case "S", "s":85 guard let x2 = lecteur.nombre(), let y2 = lecteur.nombre(), let x = lecteur.nombre(), let y = lecteur.nombre() else { return p }86 let c1 = controle.map { CGPoint(x: 2 * courant.x - $0.x, y: 2 * courant.y - $0.y) } ?? courant87 let c2 = pt(x2, y2), fin = pt(x, y)88 p.addCurve(to: fin, control1: c1, control2: c2)89 controle = c2; courant = fin90 case "Q", "q":91 guard let x1 = lecteur.nombre(), let y1 = lecteur.nombre(), let x = lecteur.nombre(), let y = lecteur.nombre() else { return p }92 let c = pt(x1, y1), fin = pt(x, y)93 p.addQuadCurve(to: fin, control: c)94 controle = nil; courant = fin95 case "A", "a":96 guard let rx = lecteur.nombre(), let ry = lecteur.nombre(), let rot = lecteur.nombre(),97 let grand = lecteur.drapeau(), let sens = lecteur.drapeau(),98 let x = lecteur.nombre(), let y = lecteur.nombre() else { return p }99 let fin = pt(x, y)100 arc(&p, de: courant, a: fin, rx: rx, ry: ry, rotation: rot, grand: grand, horaire: sens)101 courant = fin; controle = nil102 case "Z", "z":103 p.closeSubpath(); courant = depart; controle = nil104 default:105 return p106 }107 }108 return p109 }110111 /// Arc elliptique de SVG (paramètres d'extrémités) converti en courbes de Bézier (norme SVG, annexe F.6).112 private static func arc(_ p: inout Path, de p0: CGPoint, a p1: CGPoint, rx rx0: CGFloat, ry ry0: CGFloat,113 rotation: CGFloat, grand: Bool, horaire: Bool) {114 var rx = abs(rx0), ry = abs(ry0)115 if rx == 0 || ry == 0 || p0 == p1 { p.addLine(to: p1); return }116 let phi = rotation * .pi / 180, cosP = cos(phi), sinP = sin(phi)117 let dx = (p0.x - p1.x) / 2, dy = (p0.y - p1.y) / 2118 let x1 = cosP * dx + sinP * dy, y1 = -sinP * dx + cosP * dy119 let lambda = (x1 * x1) / (rx * rx) + (y1 * y1) / (ry * ry)120 if lambda > 1 { rx *= sqrt(lambda); ry *= sqrt(lambda) }121 let num = rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1122 let den = rx * rx * y1 * y1 + ry * ry * x1 * x1123 var k = den == 0 ? 0 : sqrt(max(0, num / den))124 if grand == horaire { k = -k }125 let cxp = k * rx * y1 / ry, cyp = -k * ry * x1 / rx126 let cx = cosP * cxp - sinP * cyp + (p0.x + p1.x) / 2127 let cy = sinP * cxp + cosP * cyp + (p0.y + p1.y) / 2128 func angle(_ ux: CGFloat, _ uy: CGFloat, _ vx: CGFloat, _ vy: CGFloat) -> CGFloat {129 atan2(ux * vy - uy * vx, ux * vx + uy * vy)130 }131 let t1 = angle(1, 0, (x1 - cxp) / rx, (y1 - cyp) / ry)132 var dt = angle((x1 - cxp) / rx, (y1 - cyp) / ry, (-x1 - cxp) / rx, (-y1 - cyp) / ry)133 if !horaire && dt > 0 { dt -= 2 * .pi } else if horaire && dt < 0 { dt += 2 * .pi }134 let n = max(1, Int(ceil(abs(dt) / (.pi / 2))))135 let pas = dt / CGFloat(n)136 let alpha = 4 / 3 * tan(pas / 4)137 var t = t1138 for _ in 0..<n {139 let c1 = cos(t), s1 = sin(t), c2 = cos(t + pas), s2 = sin(t + pas)140 func place(_ ex: CGFloat, _ ey: CGFloat) -> CGPoint {141 CGPoint(x: cx + cosP * rx * ex - sinP * ry * ey, y: cy + sinP * rx * ex + cosP * ry * ey)142 }143 p.addCurve(to: place(c2, s2), control1: place(c1 - alpha * s1, s1 + alpha * c1), control2: place(c2 + alpha * s2, s2 - alpha * c2))144 t += pas145 }146 }147148 private struct Lecteur {149 let s: [UInt8]150 var i = 0151 init(_ d: String) { s = Array(d.utf8) }152 var fini: Bool { i >= s.count }153 mutating func passerBlancs() {154 while i < s.count, s[i] == 0x20 || s[i] == 0x2C || s[i] == 0x0A || s[i] == 0x09 || s[i] == 0x0D { i += 1 }155 }156 mutating func commande() -> Character? {157 passerBlancs()158 guard i < s.count else { return nil }159 let c = s[i]160 guard (c >= 0x41 && c <= 0x5A) || (c >= 0x61 && c <= 0x7A), c != 0x65, c != 0x45 else { return nil }161 i += 1162 return Character(UnicodeScalar(c))163 }164 mutating func drapeau() -> Bool? {165 passerBlancs()166 guard i < s.count, s[i] == 0x30 || s[i] == 0x31 else { return nil }167 defer { i += 1 }168 return s[i] == 0x31169 }170 mutating func nombre() -> CGFloat? {171 passerBlancs()172 let debut = i173 if i < s.count, s[i] == 0x2D || s[i] == 0x2B { i += 1 }174 var point = false175 while i < s.count {176 let c = s[i]177 if c >= 0x30 && c <= 0x39 { i += 1 } else if c == 0x2E && !point { point = true; i += 1 } else { break }178 }179 if i < s.count, s[i] == 0x65 || s[i] == 0x45 {180 var j = i + 1181 if j < s.count, s[j] == 0x2D || s[j] == 0x2B { j += 1 }182 if j < s.count, s[j] >= 0x30 && s[j] <= 0x39 {183 while j < s.count, s[j] >= 0x30 && s[j] <= 0x39 { j += 1 }184 i = j185 }186 }187 guard i > debut, let v = Double(String(decoding: s[debut..<i], as: UTF8.self)) else { i = debut; return nil }188 return CGFloat(v)189 }190 }191}192