From d4a807596c343e9a6ed7dc0c137e0e2ef9efc20f Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Fri, 21 May 2021 18:51:38 -0400 Subject: [PATCH] matrix: default.nix to build icons --- matrix/build.rb | 75 ++++++++++++++++++++++++++++++++++++++++++++++ matrix/default.nix | 70 +++++++++++++++++++++++++++++++++++++++++++ matrix/pkgs.nix | 9 ++++++ 3 files changed, 154 insertions(+) create mode 100644 matrix/build.rb create mode 100644 matrix/default.nix create mode 100644 matrix/pkgs.nix diff --git a/matrix/build.rb b/matrix/build.rb new file mode 100644 index 0000000..d575017 --- /dev/null +++ b/matrix/build.rb @@ -0,0 +1,75 @@ +#!/usr/bin/env ruby + +require "nokogiri" +require "fileutils" + +$document = ARGV.first +$out = ENV["out"] + +include FileUtils + +def save(doc, out) + File.open("icons.src.svg", "w") do |f| + doc.write_xml_to(f, :encoding => "UTF-8") + end + + system( + "inkscape", + "icons.src.svg", + "--export-area-page", + "--export-filename=#{out}", + ) + + rm("icons.src.svg") +end + +puts("Matrix Icons Builder...") +puts("=======================\n") + +mkdir_p($out) +$doc = Nokogiri::XML::Document.parse(File.read($document)) + +$doc.element_children.each do |svg| + # Checks we indeed loaded an SVG document... + raise unless svg.name == "svg" + + # First hide everything. + svg.element_children.select {|el| el.name == "g"}.each do |el| + el["style"] = "display:none" + end + + # Then prepare exports for this category + svg.element_children.select {|el| el.name == "g"}.each do |el| + type = el["inkscape:label"] + + # Skip an unneeded category + next if type == "DO NOT PRINT" + + # Show the current layer + el["style"] = "" + + # But first hide everything but the background + el.element_children.each do |child| + if child["inkscape:label"].match(/^\$BACKGROUND/) + child["style"] = "" + else + child["style"] = "display:none" + end + end + + el.element_children.each do |child| + next if child["inkscape:label"].match(/^\$BACKGROUND/) + # Show the layer + child["style"] = "" + + name = child["inkscape:label"] + save($doc, File.join($out, "#{type}-#{name}.png")) + + # Hide the layer + child["style"] = "display:none" + end + + # Hide the current layer again + el["style"] = "display:none" + end +end diff --git a/matrix/default.nix b/matrix/default.nix new file mode 100644 index 0000000..19dfd37 --- /dev/null +++ b/matrix/default.nix @@ -0,0 +1,70 @@ +# This uses a pinned Nixpkgs so the software +# used for the export (inkscape) and fonts used +# for the export stay proper. +{ pkgs ? import ./pkgs.nix {} }: + +let + FONTCONFIG_FILE = pkgs.callPackage ( + { symlinkJoin + , writeText + , encode-sans + }: + let + mkFontsDir = fonts: symlinkJoin { + name = "fonts-dir"; + paths = fonts; + }; + + fontsDir = mkFontsDir [ + encode-sans + ]; + in + + writeText "fonts.conf" '' + + + + ${fontsDir} + + + + + true + + + true + + + hintslight + + + true + + + + + + '' + ) {}; +in + +# Yes, you can callPackage something else than a path! +pkgs.callPackage ( + + { runCommandNoCC, inkscape, encode-sans, FONTCONFIG_FILE, ruby }: + runCommandNoCC "nixos-artwork-matrix-icons" { + nativeBuildInputs = [ + inkscape + ruby + ]; + inherit FONTCONFIG_FILE; + } '' + ruby ${./build.rb} ${./icons.src.svg} + '' + +) { + inherit FONTCONFIG_FILE; + ruby = pkgs.ruby.withPackages(p: [ + p.nokogiri + ]); +} diff --git a/matrix/pkgs.nix b/matrix/pkgs.nix new file mode 100644 index 0000000..6bd3d24 --- /dev/null +++ b/matrix/pkgs.nix @@ -0,0 +1,9 @@ +let + rev = "d42cd445dde587e9a993cd9434cb43da07c4c5de"; + sha256 = "0dzrn97srxyw5a3g7hf8chwccxns5z3aij23hc0fch7ygc8w0gq0"; + tarball = builtins.fetchTarball { + url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz"; + inherit sha256; + }; +in +builtins.trace "Using default Nixpkgs revision '${rev}'..." (import tarball)