From fb223af0beca612982669a86e338d570d35cea79 Mon Sep 17 00:00:00 2001 From: judah Date: Thu, 4 Apr 2024 15:47:37 +1300 Subject: [PATCH] Quick fix to use Sharp Icons --Assuming you have set up Pro-- Copy the contents of icon-families.json into the lib/fonts/icons.json This code is backwards compatible so that if you use icons.json it will pull out the relevant fields. icon-families.json doesn't have the styles tag so instead we can iterate over the svgs keys of "classic" and check if it has "sharp" This also means that the key for sharp is now "sharp name" so we have to do some manipulation to remove the empty space to map the Class names correctly and to un-comment out the relevant pubspec.yaml files. I hope this helps someone out to start using Sharp Icons in this. --- lib/src/icon_data.dart | 52 ++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 16 +++++++++++++ util/lib/main.dart | 24 ++++++++++++++----- 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/lib/src/icon_data.dart b/lib/src/icon_data.dart index b3db429..e653d47 100644 --- a/lib/src/icon_data.dart +++ b/lib/src/icon_data.dart @@ -83,3 +83,55 @@ class IconDataThin extends IconData { fontPackage: 'font_awesome_flutter', ); } + +/// [IconData] for a font awesome sharp thin icon from a code point. Only works if +/// thin icons (font awesome pro, v6+) have been installed. +/// +/// Code points can be obtained from fontawesome.com +class IconDataSharpThin extends IconData { + const IconDataSharpThin(int codePoint) + : super( + codePoint, + fontFamily: 'FontAwesomeSharpThin', + fontPackage: 'font_awesome_flutter', + ); +} + +/// [IconData] for a font awesome sharp light icon from a code point. Only works if +/// thin icons (font awesome pro, v6+) have been installed. +/// +/// Code points can be obtained from fontawesome.com +class IconDataSharpLight extends IconData { + const IconDataSharpLight(int codePoint) + : super( + codePoint, + fontFamily: 'FontAwesomeSharpLight', + fontPackage: 'font_awesome_flutter', + ); +} + +/// [IconData] for a font awesome sharp regular icon from a code point. Only works if +/// thin icons (font awesome pro, v6+) have been installed. +/// +/// Code points can be obtained from fontawesome.com +class IconDataSharpRegular extends IconData { + const IconDataSharpRegular(int codePoint) + : super( + codePoint, + fontFamily: 'FontAwesomeSharpRegular', + fontPackage: 'font_awesome_flutter', + ); +} + +/// [IconData] for a font awesome sharp solid icon from a code point. Only works if +/// thin icons (font awesome pro, v6+) have been installed. +/// +/// Code points can be obtained from fontawesome.com +class IconDataSharpSolid extends IconData { + const IconDataSharpSolid(int codePoint) + : super( + codePoint, + fontFamily: 'FontAwesomeSharpSolid', + fontPackage: 'font_awesome_flutter', + ); +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index f3c77d4..2537c5e 100755 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -44,3 +44,19 @@ flutter: # fonts: # - asset: lib/fonts/fa-thin-100.ttf # weight: 100 +# - family: FontAwesomeSharpThin +# fonts: +# - asset: lib/fonts/fa-sharp-thin-100.ttf +# weight: 100 +# - family: FontAwesomeSharpLight +# fonts: +# - asset: lib/fonts/fa-sharp-light-300.ttf +# weight: 300 +# - family: FontAwesomeSharpRegular +# fonts: +# - asset: lib/fonts/fa-sharp-regular-400.ttf +# weight: 400 +# - family: FontAwesomeSharpSolid +# fonts: +# - asset: lib/fonts/fa-sharp-solid-900.ttf +# weight: 900 diff --git a/util/lib/main.dart b/util/lib/main.dart index 3e07dec..dc616fb 100644 --- a/util/lib/main.dart +++ b/util/lib/main.dart @@ -194,7 +194,7 @@ void adjustPubspecFontIncludes(Set styles) { if (!line.trimLeft().startsWith('- family:')) continue; styleName = line.substring(25).toLowerCase(); // - family: FontAwesomeXXXXXX - if (styles.contains(styleName)) { + if (styles.any((element) => element.replaceAll(' ', '') == styleName)) { //Because of 'sharp thin' we need to remove spaces here pubspec[i] = uncommentYamlLine(pubspec[i]); pubspec[i + 1] = uncommentYamlLine(pubspec[i + 1]); pubspec[i + 2] = uncommentYamlLine(pubspec[i + 2]); @@ -445,7 +445,7 @@ String normalizeIconName(String iconName, String style, int styleCompetitors) { /// Utility function to generate the correct 'IconData' subclass for a [style] String styleToDataSource(String style) { - return 'IconData${style[0].toUpperCase()}${style.substring(1)}'; + return 'IconData${style.split(' ').map((word) => word.isNotEmpty ? word[0].toUpperCase() + word.substring(1) : '').toList().join('')}'; } /// Gets the default branch from github's metadata @@ -556,13 +556,25 @@ bool readAndPickMetadata(File iconsJson, List metadata, versions.add(v); } - List iconStyles = (icon['styles'] as List).cast(); - + List iconStyles = []; + if (icon.containsKey("styles")) { + iconStyles = (icon['styles'] as List).cast(); + } else if (icon.containsKey("svgs")) { + iconStyles.addAll((icon['svgs']['classic'] as Map).keys); + if (icon['svgs']?['sharp'] != null) { + iconStyles.addAll((icon['svgs']['sharp'] as Map).keys.map((key) => 'sharp $key')); //"sharp thin ..." + } + } //TODO: Remove line once duotone support discontinuation notice is removed if (iconStyles.contains('duotone')) hasDuotoneIcons = true; for (var excluded in excludedStyles) { - iconStyles.remove(excluded); + if (excluded == 'sharp') { + //Since it's 'sharp thin' then remove any containing sharp + iconStyles.removeWhere((element) => element.contains('sharp')); + } else { + iconStyles.remove(excluded); + } } if (iconStyles.isEmpty) continue; @@ -628,7 +640,7 @@ ArgParser setUpArgParser() { argParser.addMultiOption('exclude', abbr: 'e', defaultsTo: [], - allowed: ['brands', 'regular', 'solid', 'duotone', 'light', 'thin'], + allowed: ['brands', 'regular', 'solid', 'duotone', 'light', 'thin', 'sharp'], help: 'icon styles which are excluded by the generator'); argParser.addFlag('dynamic',