Compare commits

..

No commits in common. "0b33d03b73418fc0ecc05bac345f6baee8d01978" and "07408d01a9c09794ee1f43ab3dd79c8cfa8f8ebd" have entirely different histories.

2 changed files with 16 additions and 12 deletions

View File

@ -5,8 +5,7 @@ require 'nokogiri'
require 'set' require 'set'
# 1) Process all files passed in from the command line # 1) Process all files passed in from the command line
svgpath = ARGV[0] files = ARGV
files = ARGV[1..]
# 2) Extract used Feather icons # 2) Extract used Feather icons
used_icons = Set.new used_icons = Set.new
@ -28,7 +27,7 @@ end
puts "Found #{used_icons.size} unique icons: #{used_icons.to_a.join(', ')}" puts "Found #{used_icons.size} unique icons: #{used_icons.to_a.join(', ')}"
# 3) Load the full feather-sprite.svg as XML # 3) Load the full feather-sprite.svg as XML
sprite_doc = File.open(svgpath, "r:UTF-8") { |f| Nokogiri::XML(f) } sprite_doc = File.open("feather-sprite.svg", "r:UTF-8") { |f| Nokogiri::XML(f) }
# 4) Create a new SVG with only the required symbols # 4) Create a new SVG with only the required symbols
new_svg = Nokogiri::XML::Document.new new_svg = Nokogiri::XML::Document.new
@ -44,7 +43,7 @@ sprite_doc.css("symbol").each do |symbol_node|
end end
# 5) Save the subset sprite # 5) Save the subset sprite
File.open(svgpath, "w:UTF-8") do |f| File.open("custom-sprite.svg", "w:UTF-8") do |f|
f.write(new_svg.to_xml) f.write(new_svg.to_xml)
end end

View File

@ -1,9 +1,12 @@
import os import os
import sys
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from fontTools.subset import Subsetter, Options from fontTools.subset import Subsetter, Options
from fontTools.ttLib import TTFont 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 FONT_EXTENSIONS = (".ttf", ".woff", ".woff2", ".otf") # Font file types
def extract_text_from_html(file_path): def extract_text_from_html(file_path):
@ -12,13 +15,15 @@ def extract_text_from_html(file_path):
soup = BeautifulSoup(f.read(), "html.parser") soup = BeautifulSoup(f.read(), "html.parser")
return soup.get_text() return soup.get_text()
def get_used_characters(files): def get_used_characters(directory):
"""Collect unique characters from all .html files in the given directory.""" """Collect unique characters from all .html files in the given directory."""
char_set = set() char_set = set()
for file in files: for root, _, files in os.walk(directory):
full_path = os.path.join(root, file) for file in files:
text = extract_text_from_html(full_path) if file.endswith(".html"):
char_set.update(text) full_path = os.path.join(root, file)
text = extract_text_from_html(full_path)
char_set.update(text)
return "".join(sorted(char_set)) return "".join(sorted(char_set))
def find_font_files(directory): def find_font_files(directory):
@ -60,10 +65,10 @@ def subset_font_in_place(font_path, characters):
print(f"Subsetted font in place: {font_path}") print(f"Subsetted font in place: {font_path}")
if __name__ == "__main__": if __name__ == "__main__":
used_chars = get_used_characters(sys.argv[2:]) used_chars = get_used_characters(HTML_DIR)
print(f"Extracted {len(used_chars)} unique characters from HTML files.") print(f"Extracted {len(used_chars)} unique characters from HTML files.")
font_files = find_font_files(sys.argv[1]) font_files = find_font_files(FONT_DIR)
print(f"Found {len(font_files)} font files to subset.") print(f"Found {len(font_files)} font files to subset.")
for font_file in font_files: for font_file in font_files: