diff libervia/backend/tools/common/template.py @ 4322:00837fa13e5a default tip @

tools (common/template), cli (call/gui): use font-awesome instead of fontello: following change in Libervia Media, code has been updated to use font-awesome now instead of fontello.
author Goffi <goffi@goffi.org>
date Sat, 26 Oct 2024 22:42:17 +0200
parents 0d7bb4df2343
children
line wrap: on
line diff
--- a/libervia/backend/tools/common/template.py	Mon Sep 30 14:15:47 2024 +0200
+++ b/libervia/backend/tools/common/template.py	Sat Oct 26 22:42:17 2024 +0200
@@ -516,7 +516,7 @@
         self.env.filters["media_type_sub"] = self.media_type_sub
         # custom tests
         self.env.tests["in_the_past"] = self._in_the_past
-        self.icons_path = os.path.join(host.media_dir, "fonts/fontello/svg")
+        self.fa_svgs_path = Path(host.media_dir, "fonts/fontawesome/svgs")
 
         # policies
         self.env.policies["ext.i18n.trimmed"] = True
@@ -998,7 +998,6 @@
 
     def _icon_defs(self, *names):
         """Define svg icons which will be used in the template.
-
         Their name is used as id
         """
         svg_elt = etree.Element(
@@ -1010,13 +1009,47 @@
         )
         defs_elt = etree.SubElement(svg_elt, "defs")
         for name in names:
-            path = os.path.join(self.icons_path, name + ".svg")
-            icon_svg_elt = etree.parse(path).getroot()
-            # we use icon name as id, so we can retrieve them easily
-            icon_svg_elt.set("id", name)
+            if "/" in name:
+                raise ValueError("SVG name must not have a `/` in it.")
+            if name.startswith("regular "):
+                name = name[8:]
+                path = self.fa_svgs_path / f"regular/{name}.svg"
+            else:
+                if name.startswith("solid "):
+                    # "solid" can be explicitely specified, and it's the default value.
+                    name = name[6:]
+                path = self.fa_svgs_path / f"solid/{name}.svg"
+
+            # Parse the source SVG
+            try:
+                icon_svg_elt = etree.parse(path).getroot()
+            except OSError:
+                log.exception(f"Can't load icon at {path}.")
+                return safe("")
+
             if not icon_svg_elt.tag == "{http://www.w3.org/2000/svg}svg":
                 raise exceptions.DataError("invalid SVG element")
-            defs_elt.append(icon_svg_elt)
+
+            # Extract the viewBox and path data
+            viewbox = icon_svg_elt.get("viewBox")
+            path_element = icon_svg_elt.find(".//{http://www.w3.org/2000/svg}path")
+
+            # Create a new symbol element
+            symbol = etree.SubElement(
+                defs_elt,
+                "symbol",
+                id=name,
+                viewBox=viewbox,
+            )
+
+            # Add the path to the symbol
+            if path_element is not None:
+                path = etree.SubElement(
+                    symbol,
+                    "path",
+                    d=path_element.get("d"),
+                )
+
         return safe(etree.tostring(svg_elt, encoding="unicode"))
 
     def _icon_use(self, name: str, cls: str = "", **kwargs: str) -> safe:
@@ -1024,7 +1057,7 @@
         extra_attrs = " ".join(f'{k}="{html.escape(str(v))}"' for k, v in kwargs.items())
         return safe(
             '<svg class="svg-icon{cls}"{extra_attrs} xmlns="http://www.w3.org/2000/svg" '
-            'viewBox="0 0 100 100">\n'
+            'width="1em" height="1em" fill="currentColor" >\n'
             '    <use href="#{name}"/>'
             "</svg>\n".format(
                 name=name,