Compare commits
3 Commits
7130c6bd11
...
816a473913
Author | SHA1 | Date | |
---|---|---|---|
816a473913 | |||
ce8f8fb872 | |||
2f60004241 |
50
chatgpt-subset-feather-icon.rb
Normal file
50
chatgpt-subset-feather-icon.rb
Normal file
@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'nokogiri'
|
||||
require 'set'
|
||||
|
||||
# 1) Find all HTML files, excluding those in "venv/"
|
||||
files = Dir.glob("**/*.html").reject { |f| f.start_with?("venv") }
|
||||
|
||||
# 2) Extract used Feather icons
|
||||
used_icons = Set.new
|
||||
|
||||
files.each do |file|
|
||||
# Parse each HTML file
|
||||
doc = File.open(file, "r:UTF-8") { |f| Nokogiri::HTML(f) }
|
||||
|
||||
# Look for <use xlink:href="/feather-sprite.svg#iconName">
|
||||
doc.css("use").each do |use_tag|
|
||||
href = use_tag["xlink:href"] || use_tag["href"]
|
||||
if href && href.start_with?("/feather-sprite.svg#")
|
||||
icon_name = href.split("#").last
|
||||
used_icons << icon_name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
puts "Found #{used_icons.size} unique icons: #{used_icons.to_a.join(', ')}"
|
||||
|
||||
# 3) Load the full feather-sprite.svg as XML
|
||||
sprite_doc = File.open("feather-sprite.svg", "r:UTF-8") { |f| Nokogiri::XML(f) }
|
||||
|
||||
# 4) Create a new SVG with only the required symbols
|
||||
new_svg = Nokogiri::XML::Document.new
|
||||
svg_tag = Nokogiri::XML::Node.new("svg", new_svg)
|
||||
svg_tag["xmlns"] = "http://www.w3.org/2000/svg"
|
||||
new_svg.add_child(svg_tag)
|
||||
|
||||
sprite_doc.css("symbol").each do |symbol_node|
|
||||
if used_icons.include?(symbol_node["id"])
|
||||
# Duplicate the symbol node (so it can be inserted in the new document)
|
||||
svg_tag.add_child(symbol_node.dup)
|
||||
end
|
||||
end
|
||||
|
||||
# 5) Save the subset sprite
|
||||
File.open("custom-sprite.svg", "w:UTF-8") do |f|
|
||||
f.write(new_svg.to_xml)
|
||||
end
|
||||
|
||||
puts "Generated custom-sprite.svg with only the required icons."
|
75
chatgpt-subset-one-go.py
Normal file
75
chatgpt-subset-one-go.py
Normal file
@ -0,0 +1,75 @@
|
||||
import os
|
||||
from bs4 import BeautifulSoup
|
||||
from fontTools.subset import Subsetter, Options
|
||||
from fontTools.ttLib import TTFont
|
||||
|
||||
# Directories
|
||||
HTML_DIR = "." # Directory with .html files
|
||||
FONT_DIR = "." # Directory containing fonts to be modified
|
||||
|
||||
FONT_EXTENSIONS = (".ttf", ".woff", ".woff2", ".otf") # Font file types
|
||||
|
||||
def extract_text_from_html(file_path):
|
||||
"""Extract text content from a single HTML file."""
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
soup = BeautifulSoup(f.read(), "html.parser")
|
||||
return soup.get_text()
|
||||
|
||||
def get_used_characters(directory):
|
||||
"""Collect unique characters from all .html files in the given directory."""
|
||||
char_set = set()
|
||||
for root, _, files in os.walk(directory):
|
||||
for file in files:
|
||||
if file.endswith(".html"):
|
||||
full_path = os.path.join(root, file)
|
||||
text = extract_text_from_html(full_path)
|
||||
char_set.update(text)
|
||||
return "".join(sorted(char_set))
|
||||
|
||||
def find_font_files(directory):
|
||||
"""Find all font files in the given directory, recursively."""
|
||||
font_files = []
|
||||
for root, _, files in os.walk(directory):
|
||||
for file in files:
|
||||
if file.endswith(FONT_EXTENSIONS):
|
||||
font_files.append(os.path.join(root, file))
|
||||
return font_files
|
||||
|
||||
def subset_font_in_place(font_path, characters):
|
||||
"""Subsets the given font file to include only the specified characters."""
|
||||
# Convert characters to their integer code points
|
||||
unicode_set = {ord(c) for c in characters}
|
||||
|
||||
font = TTFont(font_path)
|
||||
options = Options()
|
||||
options.drop_tables += ["DSIG"]
|
||||
options.drop_tables += ["LTSH", "VDMX", "hdmx", "gasp"]
|
||||
options.unicodes = unicode_set
|
||||
options.variations = False
|
||||
options.drop_variations = True
|
||||
options.layout_features = ["*"] # keep all OT features
|
||||
options.hinting = False
|
||||
|
||||
# Preserve original format if it was WOFF/WOFF2
|
||||
if font_path.endswith(".woff2"):
|
||||
options.flavor = "woff2"
|
||||
elif font_path.endswith(".woff"):
|
||||
options.flavor = "woff"
|
||||
|
||||
subsetter = Subsetter(options)
|
||||
subsetter.populate(unicodes=unicode_set)
|
||||
subsetter.subset(font)
|
||||
|
||||
# Overwrite the original font file
|
||||
font.save(font_path)
|
||||
print(f"Subsetted font in place: {font_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
used_chars = get_used_characters(HTML_DIR)
|
||||
print(f"Extracted {len(used_chars)} unique characters from HTML files.")
|
||||
|
||||
font_files = find_font_files(FONT_DIR)
|
||||
print(f"Found {len(font_files)} font files to subset.")
|
||||
|
||||
for font_file in font_files:
|
||||
subset_font_in_place(font_file, used_chars)
|
@ -1 +1 @@
|
||||
Subproject commit 98a9d782730fd714ae7a3300d8cf6791ed0bc341
|
||||
Subproject commit 2b7645a5728ed49d23a4a0b27085069b4d9c354f
|
Loading…
Reference in New Issue
Block a user