Skip to content

Commit

Permalink
Quick fix to use Sharp Icons
Browse files Browse the repository at this point in the history
--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 <thin, light, regular, solid> 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.
  • Loading branch information
JudahMcNicholl committed Apr 4, 2024
1 parent b03974f commit fb223af
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
52 changes: 52 additions & 0 deletions lib/src/icon_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
}
16 changes: 16 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 18 additions & 6 deletions util/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void adjustPubspecFontIncludes(Set<String> 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]);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -556,13 +556,25 @@ bool readAndPickMetadata(File iconsJson, List<IconMetadata> metadata,
versions.add(v);
}

List<String> iconStyles = (icon['styles'] as List).cast<String>();

List<String> iconStyles = [];
if (icon.containsKey("styles")) {
iconStyles = (icon['styles'] as List).cast<String>();
} else if (icon.containsKey("svgs")) {
iconStyles.addAll((icon['svgs']['classic'] as Map<String, dynamic>).keys);
if (icon['svgs']?['sharp'] != null) {
iconStyles.addAll((icon['svgs']['sharp'] as Map<String, dynamic>).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;
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit fb223af

Please sign in to comment.