Skip to content

Commit

Permalink
feat: github actions and provider changes
Browse files Browse the repository at this point in the history
  • Loading branch information
r1n0x committed Jul 23, 2024
1 parent db88998 commit 8c490ae
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 29 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Dart

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
validate_changes:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@9a04e6d73cca37bd455e0608d7e5092f881fd60
- name: Install dependencies
run: dart pub get
- name: Verify formatting
run: dart format lib example test --output=none --set-exit-if-changed .
- name: Analyze project source
run: dart analyze example lib test
windows_tests:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@9a04e6d73cca37bd455e0608d7e5092f881fd60
- name: Install dependencies
run: dart pub get
- name: Run tests
run: dart test
linux_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dart-lang/setup-dart@9a04e6d73cca37bd455e0608d7e5092f881fd60
- name: Install dependencies
run: dart pub get
- name: Run tests
run: dart test
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ installed on user device.

## Getting started / Usage

Follow [pub.dev tutorial](https://pub.dev/packages/type_face_provider/install) for installation.

Beside linux requiring 'fc-list' dependency installed, just use it as shown in
example.

Expand Down
22 changes: 12 additions & 10 deletions example/type_face_provider_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ void main() async {
for (final style in typeface.styles) {
print('\t\t- $style');
}
print('\tfonts:');
for (final font in typeface.fonts) {
print('\t\t- $font');
print('\tfull names:');
for (final fullName in typeface.fullNames) {
print('\t\t- $fullName');
}

/// File: '/usr/share/fonts/truetype/noto/NotoSansMono-Bold.ttf'
/// typefaces:
/// - Noto Sans Mono
/// styles:
/// - Bold
/// fonts:
/// - Noto Sans Mono Bold
/// ...
/// File: '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf'
/// typefaces:
/// - DejaVu Sans Mono
/// styles:
/// - Book
/// full names:
/// - DejaVu Sans Mono
/// ...
}
}
4 changes: 2 additions & 2 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import 'dart:io';
import 'package:collection/collection.dart';

class Typeface {
const Typeface(this.names, this.file, this.styles, this.fonts);
const Typeface(this.names, this.file, this.styles, this.fullNames);

final List<String> names;
final File file;
final List<String> styles;
final List<String> fonts;
final List<String> fullNames;
}

class Typefaces extends DelegatingList<Typeface> {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class TypefacesProvider {

factory TypefacesProvider._setPlatform() {
if (Platform.isLinux) {
return LinuxFontsProvider();
return LinuxTypefacesProvider();
} else if (Platform.isWindows) {
throw UnimplementedError(
'Current platform "${Platform.operatingSystem}" is not supported by $TypefacesProvider and it will be supported in the future.',
Expand Down
26 changes: 10 additions & 16 deletions lib/src/provider/linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:type_face_provider/src/fc_list_validator.dart';
import 'package:type_face_provider/src/model.dart';
import 'package:type_face_provider/type_face_provider.dart';

class LinuxFontsProvider extends TypefacesProvider {
LinuxFontsProvider();
class LinuxTypefacesProvider extends TypefacesProvider {
LinuxTypefacesProvider();

static final _fcListValidator = FcListValidator();
bool? _isFcListInstalled;
Expand All @@ -17,12 +17,13 @@ class LinuxFontsProvider extends TypefacesProvider {
static const _filePathSectionIndex = 0;
static const _nameSectionIndex = 1;
static const _stylesSectionIndex = 2;
static const _fullNameSectionIndex = 3;

@override
Future<Typefaces> getTypefaces() async {
await throwIfFcListIsNotInstalled();
final Process process = await Process.start('fc-list', [
'--format="%{file}$_sectionSeparator%{family}$_sectionSeparator%{style}$_lineSeparator"'
'--format="%{file}$_sectionSeparator%{family}$_sectionSeparator%{style}$_sectionSeparator%{fullname}$_lineSeparator"'
]);

await process.isFinished;
Expand All @@ -39,8 +40,11 @@ class LinuxFontsProvider extends TypefacesProvider {
.split(',')
.map((e) => e.trim())
.toList(growable: false);
List<String> fonts = _buildFonts(names, styles);
result.add(Typeface(names, file, styles, fonts));
final fullNames = sections[_fullNameSectionIndex]
.split(',')
.map((e) => e.trim())
.toList(growable: false);
result.add(Typeface(names, file, styles, fullNames));
}
return result;
}
Expand All @@ -49,18 +53,8 @@ class LinuxFontsProvider extends TypefacesProvider {
_isFcListInstalled ??= await _fcListValidator.isInstalled();
if (!_isFcListInstalled!) {
throw Exception(
'In order to use $LinuxFontsProvider platform you must have \'fc-list\' installed as your system level dependency.');
}
}

List<String> _buildFonts(List<String> names, List<String> styles) {
final List<String> fonts = [];
for (final name in names) {
for (final style in styles) {
fonts.add("$name $style");
}
'In order to use $LinuxTypefacesProvider platform you must have \'fc-list\' installed as your system level dependency.');
}
return fonts;
}

List<String> _parseSections(String line) {
Expand Down
32 changes: 32 additions & 0 deletions test/type_face_provider_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:io';

import 'package:collection/collection.dart';
import 'package:test/test.dart';
import 'package:type_face_provider/src/provider.dart';

void main() {
group('Fetching system fonts', () {
test('Platform - Linux', () async {
if (!Platform.isLinux) {
markTestSkipped('Skipped - test can be only ran on Linux.');
return;
}

final provider = TypefacesProvider.platform;
final typefaces = (await provider.getTypefaces());
expect(typefaces.isNotEmpty, true,
reason: "Any system must have at least one font installed!");
final font = typefaces.firstWhereOrNull((e) =>
e.file.path == '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf');
expect(font != null, true,
reason: 'DejaVu Sans was not found on the system.');
if (font != null) {
expect(font.names, ['DejaVu Sans Mono']);
expect(font.file.path,
'/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf');
expect(font.styles, ['Book']);
expect(font.fullNames, ['DejaVu Sans Mono']);
}
});
});
}

0 comments on commit 8c490ae

Please sign in to comment.