sup

A curses threads-with-tags style email client

sup-website.git

git clone https://supmua.dev/git/sup-website/

_plugins/favicon.rb (3104B) - raw

      1 require 'open3'
      2 require 'tmpdir'
      3 
      4 def run_command *args
      5   out, err, status = Open3.capture3 *args
      6   raise "#{args.first} failed: #{err}" unless status.success?
      7   out
      8 end
      9 
     10 class ResizedFavicon < Jekyll::StaticFile
     11   attr_reader :size
     12 
     13   def initialize site, name, source, size
     14     super site, site.source, "images", name
     15     @source = source
     16     @size = size
     17   end
     18 
     19   def path
     20     @source
     21   end
     22 
     23   def write dest
     24     dest_path = destination dest
     25     Jekyll.logger.info "Resizing:", dest_path
     26     FileUtils.mkdir_p File.dirname(dest_path)
     27     run_command "magick", @source, "-filter", "Lanczos", "-resize", "#{@size}x#{@size}", dest_path
     28     run_command "oxipng", "--quiet", "--opt", "2", "--strip", "all", dest_path
     29     true
     30   end
     31 end
     32 
     33 class FaviconGenerator < Jekyll::Generator
     34   # Needs to run after WebfontsGenerator
     35   priority :low
     36 
     37   def generate site
     38     Jekyll.logger.info "Favicon:", "Building"
     39 
     40     font_file = site.static_files.find do |f|
     41       f.is_a?(WebfontFile) && File.basename(f.path) == "Iosevka-Regular.woff2"
     42     end
     43     raise "Iosevka-Regular.woff2 not found in site.static_files" unless font_file
     44 
     45     @build_dir = Dir.mktmpdir "sup-favicon"
     46     at_exit { FileUtils.rm_r @build_dir }
     47 
     48     master_transparent = File.join @build_dir, "favicon-dark-256.png"
     49     Jekyll.logger.info "Rendering:", master_transparent
     50     render font_file.path, master_transparent, false
     51 
     52     master_with_tile = File.join @build_dir, "favicon-256.png"
     53     Jekyll.logger.info "Rendering:", master_with_tile
     54     render font_file.path, master_with_tile, true
     55 
     56     [16, 32, 48].each do |size|
     57       site.static_files << ResizedFavicon.new(site, "favicon-#{size}.png", master_with_tile, size)
     58       site.static_files << ResizedFavicon.new(site, "favicon-dark-#{size}.png", master_transparent, size)
     59     end
     60   end
     61 
     62   def render font_path, out_path, background
     63     # Render a normal fill layer and then two blur layers on top, mimicking the
     64     # phosphor glow effect from the stylesheet:
     65     # color #b5e853; text-shadow: 0 0 5px rgba(181,232,83,.5), 0 0 10px rgba(181,232,83,.5)
     66     text = ">@"
     67     fg_colour = "#b5e853"
     68     bg_colour = "#151515"
     69     args = ["magick", "-size", "256x256"]
     70     if background
     71       args += [
     72         "xc:none",
     73         "-fill", bg_colour,
     74         "-draw", "roundrectangle 8,8 247,247 40,40"
     75       ]
     76       pointsize = "160"
     77     else
     78       args += ["xc:none"]
     79       pointsize = "220"
     80     end
     81     text_layer = [
     82       "(",
     83         "-size", "256x256", "xc:none",
     84         "-font", font_path,
     85         "-pointsize", pointsize,
     86         "-fill", fg_colour,
     87         "-gravity", "center",
     88         "-annotate", "+0-4", text,
     89       ")",
     90     ]
     91     args += text_layer
     92     args += [
     93       "(", "-clone", "1", "-channel", "A", "-evaluate", "multiply", "0.65", "+channel", "-blur", "0x18", ")",
     94       "(", "-clone", "1", "-channel", "A", "-evaluate", "multiply", "0.75", "+channel", "-blur", "0x10", ")",
     95       "-delete", "1",
     96       "-background", "none",
     97       "-layers", "merge",
     98       "+repage",
     99     ]
    100     args += text_layer
    101     args += [ "-composite", out_path ]
    102     run_command *args
    103   end
    104 end