Skip to content

Commit

Permalink
add :$limit option and fix series iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Jul 31, 2023
1 parent 1b60a05 commit 1b26d69
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
4 changes: 3 additions & 1 deletion docs/FontConfig/Pattern.md
Expand Up @@ -85,8 +85,10 @@ Create a new pattern from a parsed FontConfig pattern.
### match-series

```raku
method match(--> FontConfig::Match::Series)
method match(UInt :$limit, --> FontConfig::Match::Series)
```

This method returns a series of [FontConfig::Match](https://pdf-raku.github.io/FontConfig-raku/FontConfig/Match) objects ordered by closest match first.

This method sorted, but does not filter the list of available fonts. The `$limit` option can be used to limit the maximum numbe of fonts returned.

23 changes: 14 additions & 9 deletions lib/FontConfig/Match/Series.rakumod
Expand Up @@ -9,6 +9,7 @@ use FontConfig::Raw;
has FontConfig::Pattern:D $.pat is required;
has FcFontSet:D $.set .= new;
has Bool:D $.trim = False;
has UInt $.limit;

method elems { $!set.nfont }

Expand All @@ -19,21 +20,25 @@ multi method AT-POS(UInt:D $i where $i < $!set.nfont --> FontConfig::Match) {

multi method AT-POS(UInt:D) { FontConfig::Match }

method iterator(::?CLASS:D $set:) {
method iterator(::?CLASS:D $set:) handles<Seq List Array> {
class iterator does Iterator {
has uint $.i = 0;
has FontConfig::Match::Series:D $.set is required;
has uint $!n = $!set.elems;

submethod TWEAK(UInt :$limit) {
with $limit {
$!n = $_ if $_ < $!n;
}
}

method pull-one {
$!i >= $!set.elems
$!i >= $!n
?? IterationEnd
!! $!set.AT-POS($!i++);
}
}
iterator.new: :$set;
}

method Seq handles<List Array> {
(^$.elems).map: {$.AT-POS: $_}
iterator.new: :$set, :$!limit;
}

method parse(Str:D $query, |c) {
Expand All @@ -44,10 +49,10 @@ method parse(Str:D $query, |c) {
self.match: $pat, |c;
}

multi method match(FontConfig::Pattern:D $pat, Bool :$trim = False) {
method match(FontConfig::Pattern:D $pat, Bool :$trim = False, |c) {
my int32 $result-type;
my FcFontSet $set = $pat.configure.font-sort($pat.pattern, $trim, FcCharSet, $result-type);
self.new: :$pat, :$set;
self.new: :$pat, :$set, |c;
}

=begin pod
Expand Down
7 changes: 5 additions & 2 deletions lib/FontConfig/Pattern.rakumod
Expand Up @@ -24,7 +24,7 @@ method match(::?CLASS:D $pattern is copy:) {
}
}

method match-series(::?CLASS:D $pattern: |c) handles <Seq List Array iterator> {
method match-series(::?CLASS:D $pattern: |c) handles <iterator> {
(require ::('FontConfig::Match::Series')).match($pattern, |c);
}

Expand Down Expand Up @@ -103,10 +103,13 @@ Create a new pattern from a parsed FontConfig pattern.
=head3 match-series
=for code :lang<raku>
method match(--> FontConfig::Match::Series)
method match(UInt :$limit, --> FontConfig::Match::Series)
This method returns a series of L<FontConfig::Match> objects ordered by
closest match first.
This method sorted, but does not filter the list of available fonts. The `$limit`
option can be used to limit the maximum numbe of fonts returned.
=end pod

0 comments on commit 1b26d69

Please sign in to comment.