Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sort by name and generate icon list option #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ Name for a generated class.
Name of a package that provides a font. Used to provide a font through package dependency.
- `--[no-]format`
Format dart generated code.
- `-s` or `--sort`
Sort by icon names for the generated code.

- `-l` or `--list`
Generate a list of icons.

Font options:
- `-f` or `--font-name=<name>`
Expand Down Expand Up @@ -102,6 +107,8 @@ icon_font:
class_name: "MyIcons"
package: my_font_package
format: true
sort: true
list: true

font_name: "My Icons"
normalize: true
Expand Down
2 changes: 2 additions & 0 deletions bin/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void _run(CliArguments parsedArgs) {
ignoreShapes: parsedArgs.ignoreShapes,
normalize: parsedArgs.normalize,
fontName: parsedArgs.fontName,
sort: parsedArgs.sort,
);

writeToFile(parsedArgs.fontFile.path, otfResult.font);
Expand All @@ -103,6 +104,7 @@ void _run(CliArguments parsedArgs) {
fontFileName: fontFileName,
familyName: otfResult.font.familyName,
package: parsedArgs.fontPackage,
iconList: parsedArgs.iconList,
);

if (parsedArgs.format ?? kDefaultFormat) {
Expand Down
Binary file modified example/flutter_usage/fonts/icons.otf
Binary file not shown.
10 changes: 9 additions & 1 deletion example/flutter_usage/lib/ui/icons.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated code: do not hand-edit.

// Generated using icon_font_generator.
// Copyright © 2023 icon_font_generator (https://pub.dev/packages/icon_font_generator).
// Copyright © 2024 icon_font_generator (https://pub.dev/packages/icon_font_generator).

import 'package:flutter/widgets.dart';

Expand Down Expand Up @@ -50,4 +50,12 @@ class UiIcons {
/// <image width='32px' src='data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMiIgaGVpZ2h0PSIzMiIgY2xpcC1ydWxlPSJldmVub2RkIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiPjxwYXRoIGQ9Ik0yMC42NDggNWgtOS4yOTVjLTEuMzExIDAtMi41MDMuOTcyLTIuNTAzIDIuMzF2MTguNzc5Yy4wMy4zMzQuMjM4LjYzOC41MzYuNzkuMjk4LjE1MS42NTYuMTUxLjkyNC0uMDNsNS42Ni0zLjczOSA1LjY2IDMuNzM4YS45OS45OSAwIDAwLjUwNy4xNTJjLjE3OSAwIC4zMjgtLjA2LjQ3Ny0uMTIyYS45OTkuOTk5IDAgMDAuNTM2LS43OVY3LjMxYzAtMS4zMzgtMS4xOTItMi4zMS0yLjUwMi0yLjMxeiIvPjwvc3ZnPg=='>
static const IconData collection =
IconData(0xe003, fontFamily: iconFontFamily);

/// List of all icons in this font.
static const List<IconData> values = <IconData>[
account,
arrowLeft,
arrowRight,
collection,
];
}
30 changes: 15 additions & 15 deletions example/flutter_usage/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.17.2"
version: "1.18.0"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -174,10 +174,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
package_config:
dependency: transitive
description:
Expand Down Expand Up @@ -243,18 +243,18 @@ packages:
dependency: transitive
description:
name: stack_trace
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.11.1"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "2.1.2"
string_scanner:
dependency: transitive
description:
Expand All @@ -275,10 +275,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.6.1"
typed_data:
dependency: transitive
description:
Expand All @@ -299,18 +299,18 @@ packages:
dependency: transitive
description:
name: watcher
sha256: "6a7f46926b01ce81bfc339da6a7f20afbe7733eff9846f6d6a5466aa4c6667c0"
sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8"
url: "https://pub.dev"
source: hosted
version: "1.0.2"
version: "1.1.0"
web:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
version: "0.3.0"
xml:
dependency: transitive
description:
Expand All @@ -328,5 +328,5 @@ packages:
source: hosted
version: "3.1.1"
sdks:
dart: ">=3.1.0-185.0.dev <4.0.0"
dart: ">=3.2.0-194.0.dev <4.0.0"
flutter: ">=3.7.5"
2 changes: 2 additions & 0 deletions example/flutter_usage/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ icon_font:
output_class_file: lib/ui/icons.dart
class_name: UiIcons
format: true
sort: true
list: true

font_name: Ui Icons
normalize: false
Expand Down
16 changes: 16 additions & 0 deletions lib/src/cli/arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ const _kArgAllowedTypes = <CliArgument, List<Type>>{
CliArgument.ignoreShapes: [bool],
CliArgument.recursive: [bool],
CliArgument.format: [bool],
CliArgument.sort: [bool],
CliArgument.iconList: [bool],
CliArgument.verbose: [bool],
CliArgument.help: [bool],
CliArgument.configFile: [String],
};

const kDefaultVerbose = false;
const kDefaultFormat = false;
const kDefaultSort = false;
const kDefaultIconList = false;
const kDefaultRecursive = false;

const kOptionNames = EnumClass<CliArgument, String>({
Expand All @@ -39,6 +43,8 @@ const kOptionNames = EnumClass<CliArgument, String>({
CliArgument.className: 'class-name',
CliArgument.fontPackage: 'package',
CliArgument.format: 'format',
CliArgument.sort: 'sort',
CliArgument.iconList: 'list',

CliArgument.fontName: 'font-name',
CliArgument.normalize: 'normalize',
Expand All @@ -59,6 +65,8 @@ const kConfigKeys = EnumClass<CliArgument, String>({
CliArgument.className: 'class_name',
CliArgument.fontPackage: 'package',
CliArgument.format: 'format',
CliArgument.sort: 'sort',
CliArgument.iconList: 'list',

CliArgument.fontName: 'font_name',
CliArgument.normalize: 'normalize',
Expand All @@ -85,6 +93,8 @@ enum CliArgument {
className,
fontPackage,
format,
sort,
iconList,

// Font-related
fontName,
Expand All @@ -109,6 +119,8 @@ class CliArguments {
this.className,
this.fontPackage,
this.format,
this.sort,
this.iconList,
this.fontName,
this.recursive,
this.ignoreShapes,
Expand All @@ -131,6 +143,8 @@ class CliArguments {
map[CliArgument.className] as String?,
map[CliArgument.fontPackage] as String?,
map[CliArgument.format] as bool?,
map[CliArgument.sort] as bool?,
map[CliArgument.iconList] as bool?,
map[CliArgument.fontName] as String?,
map[CliArgument.recursive] as bool?,
map[CliArgument.ignoreShapes] as bool?,
Expand All @@ -146,6 +160,8 @@ class CliArguments {
final String? className;
final String? fontPackage;
final bool? format;
final bool? sort;
final bool? iconList;
final String? fontName;
final bool? recursive;
final bool? ignoreShapes;
Expand Down
12 changes: 12 additions & 0 deletions lib/src/cli/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ void defineOptions(ArgParser argParser) {
help: 'Formate dart generated code.',
defaultsTo: kDefaultFormat,
)
..addFlag(
kOptionNames[CliArgument.sort]!,
abbr: 's',
help: 'Sort by generated icon names.',
defaultsTo: kDefaultSort,
)
..addFlag(
kOptionNames[CliArgument.iconList]!,
abbr: 'l',
help: 'Generate a list of icons.',
defaultsTo: kDefaultIconList,
)
..addSeparator('Font options:')
..addOption(
kOptionNames[CliArgument.fontName]!,
Expand Down
10 changes: 10 additions & 0 deletions lib/src/common/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class SvgToOtfResult {
/// * If [normalize] is set to true,
/// glyphs are resized and centered to fit in coordinates grid (unitsPerEm).
/// Defaults to true.
/// * If [sort] is set to true,
/// glyphs are sorted by name.
/// Defaults to false.
/// * [fontName] is a name for a generated font.
///
/// Returns an instance of [SvgToOtfResult] class containing glyphs and a font.
Expand All @@ -32,8 +35,10 @@ SvgToOtfResult svgToOtf({
bool? ignoreShapes,
bool? normalize,
String? fontName,
bool? sort,
}) {
normalize ??= true;
sort ??= false;

final svgList = [
for (final e in svgMap.entries)
Expand All @@ -52,6 +57,9 @@ SvgToOtfResult svgToOtf({
}
}
}
if (sort) {
svgList.sort((a, b) => a.name.compareTo(b.name));
}

final glyphList = svgList.map(GenericGlyph.fromSvg).toList();

Expand Down Expand Up @@ -83,6 +91,7 @@ String generateFlutterClass({
String? fontFileName,
String? package,
int? indent,
bool? iconList,
}) {
final generator = FlutterClassGenerator(
glyphList,
Expand All @@ -91,6 +100,7 @@ String generateFlutterClass({
fontFileName: fontFileName,
familyName: familyName,
package: package,
iconList: iconList,
);

return generator.generate();
Expand Down
23 changes: 20 additions & 3 deletions lib/src/utils/flutter_class_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ class FlutterClassGenerator {
String? fontFileName,
String? package,
int? indent,
bool? iconList = false,
}) : _indent = ' ' * (indent ?? _kDefaultIndent),
_className = _getVarName(className ?? _kDefaultClassName),
_familyName = familyName ?? kDefaultFontFamily,
_fontFileName = fontFileName ?? _kDefaultFontFileName,
_iconVarNames = _generateVariableNames(glyphList),
_package = package?.isEmpty ?? true ? null : package;
_package = package?.isEmpty ?? true ? null : package,
_iconList = iconList ?? false;

final List<GenericGlyph> glyphList;
final String _fontFileName;
Expand All @@ -47,6 +49,7 @@ class FlutterClassGenerator {
final String _indent;
final String? _package;
final List<String> _iconVarNames;
final bool _iconList;

static List<String> _generateVariableNames(List<GenericGlyph> glyphList) {
final iconNameSet = <String>{};
Expand Down Expand Up @@ -121,6 +124,19 @@ class FlutterClassGenerator {
];
}

String _generateIconList() {

return [
'',
'/// List of all icons in this font.',
'static const List<IconData> values = <IconData>[',
for (var i = 0; i < _iconVarNames.length; i++) ...[
'${_iconVarNames[i]},',
],
'];'
].join('\n');
}

/// Generates content for a class' file.
String generate() {
final classContent = [
Expand All @@ -129,6 +145,7 @@ class FlutterClassGenerator {
_fontFamilyConst,
if (_hasPackage) _fontPackageConst,
for (var i = 0; i < glyphList.length; i++) ..._generateIconConst(i),
if (_iconList) _generateIconList(),
];

final classContentString =
Expand All @@ -149,8 +166,8 @@ import 'package:flutter/widgets.dart';
///
/// To use this class, make sure you declare the font in your
/// project's `pubspec.yaml` file in the `fonts` section. This ensures that
/// the "$_familyName" font is included in your application. This font is used to
/// display the icons. For example:
/// the "$_familyName" font is included in your application.
/// This font is used to display the icons. For example:
///
/// ```yaml
/// flutter:
Expand Down
6 changes: 3 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@ packages:
dependency: transitive
description:
name: watcher
sha256: "6a7f46926b01ce81bfc339da6a7f20afbe7733eff9846f6d6a5466aa4c6667c0"
sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8"
url: "https://pub.dev"
source: hosted
version: "1.0.2"
version: "1.1.0"
web_socket_channel:
dependency: transitive
description:
Expand Down Expand Up @@ -426,4 +426,4 @@ packages:
source: hosted
version: "3.1.1"
sdks:
dart: ">=2.19.2 <4.0.0"
dart: ">=3.0.0 <4.0.0"
2 changes: 2 additions & 0 deletions test/assets/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ icon_font:
class_name: MyIcons
package: test_package
format: true
sort: true
list: true

font_name: My Icons
normalize: false
Expand Down
Loading