Skip to content

Commit

Permalink
convert remaining classes to unit classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Nov 5, 2024
1 parent d8b2e14 commit 9ff0fb8
Show file tree
Hide file tree
Showing 11 changed files with 987 additions and 995 deletions.
145 changes: 72 additions & 73 deletions lib/Font/FreeType.rakumod
Original file line number Diff line number Diff line change
@@ -1,91 +1,90 @@
use v6;

class Font::FreeType:ver<0.5.9> {
use NativeCall;
use Font::FreeType::Face;
use Font::FreeType::Error;
use Font::FreeType::Raw;
use Font::FreeType::Raw::Defs;
use Method::Also;

has FT_Library $.raw;
our $lock = Lock.new;

submethod BUILD {
my $p = Pointer[FT_Library].new;
ft-try { FT_Init_FreeType( $p ); };
$!raw = $p.deref;
}
method native is also<struct unbox> is DEPRECATED("Please use the 'raw' method") { $!raw }

submethod DESTROY {
$lock.protect: {
with $!raw {
ft-try { .FT_Done_FreeType };
}
}
}

multi method face(::?CLASS:U \class: |c --> Font::FreeType::Face:D) is hidden-from-backtrace {
class.new.face(|c);
}
unit class Font::FreeType:ver<0.5.9>;

use NativeCall;
use Font::FreeType::Face;
use Font::FreeType::Error;
use Font::FreeType::Raw;
use Font::FreeType::Raw::Defs;
use Method::Also;

has FT_Library $.raw;
our $lock = Lock.new;

submethod BUILD {
my $p = Pointer[FT_Library].new;
ft-try { FT_Init_FreeType( $p ); };
$!raw = $p.deref;
}
method native is also<struct unbox> is DEPRECATED("Please use the 'raw' method") { $!raw }

multi method face(::?CLASS:D $ft-lib:
IO:D() $file-io,
Int :$index = 0,
|c --> Font::FreeType::Face:D
) is hidden-from-backtrace {
my $p = Pointer[FT_Face].new;
$lock.protect: sub () is hidden-from-backtrace {
CATCH {
when Font::FreeType::Error {
.details = "loading file '{$file-io.path}'";
.rethrow;
}
}
ft-try { $!raw.FT_New_Face($file-io.path, $index, $p); };
submethod DESTROY {
$lock.protect: {
with $!raw {
ft-try { .FT_Done_FreeType };
}
my FT_Face:D $raw = $p.deref;
Font::FreeType::Face.new: :$raw, :$ft-lib, |c;
}
}

multi method face(::?CLASS:D $ft-lib:
Blob:D $buf,
Int :$size = $buf.bytes,
Int :$index = 0,
|c --> Font::FreeType::Face:D
) is hidden-from-backtrace {
my $p = Pointer[FT_Face].new;
ft-try(sub () is hidden-from-backtrace {
$!raw.FT_New_Memory_Face($buf, $size, $index, $p);
});
my FT_Face:D $raw = $p.deref;
Font::FreeType::Face.new: :$raw, :$ft-lib, |c;
}
multi method face(::?CLASS:U \class: |c --> Font::FreeType::Face:D) is hidden-from-backtrace {
class.new.face(|c);
}

multi method face(::?CLASS:D:
IO::Handle:D $fh,
|c --> Font::FreeType::Face:D
) is hidden-from-backtrace {
multi method face(::?CLASS:D $ft-lib:
IO:D() $file-io,
Int :$index = 0,
|c --> Font::FreeType::Face:D
) is hidden-from-backtrace {
my $p = Pointer[FT_Face].new;
$lock.protect: sub () is hidden-from-backtrace {
CATCH {
when Font::FreeType::Error {
.details = "loading IO handle '{$fh.path}'";
.details = "loading file '{$file-io.path}'";
.rethrow;
}
}
$fh.seek(0, SeekFromBeginning);
$.face($fh.slurp(:bin), |c);
ft-try { $!raw.FT_New_Face($file-io.path, $index, $p); };
}
my FT_Face:D $raw = $p.deref;
Font::FreeType::Face.new: :$raw, :$ft-lib, |c;
}

multi method version(::?CLASS:U:) {
self.new.version;
}
multi method version(::?CLASS:D:) returns Version:D {
$!raw.FT_Library_Version(my FT_Int $major, my FT_Int $minor, my FT_Int $patch);
Version.new: "{$major}.{$minor}.{$patch}";
multi method face(::?CLASS:D $ft-lib:
Blob:D $buf,
Int :$size = $buf.bytes,
Int :$index = 0,
|c --> Font::FreeType::Face:D
) is hidden-from-backtrace {
my $p = Pointer[FT_Face].new;
ft-try(sub () is hidden-from-backtrace {
$!raw.FT_New_Memory_Face($buf, $size, $index, $p);
});
my FT_Face:D $raw = $p.deref;
Font::FreeType::Face.new: :$raw, :$ft-lib, |c;
}

multi method face(::?CLASS:D:
IO::Handle:D $fh,
|c --> Font::FreeType::Face:D
) is hidden-from-backtrace {
CATCH {
when Font::FreeType::Error {
.details = "loading IO handle '{$fh.path}'";
.rethrow;
}
}
$fh.seek(0, SeekFromBeginning);
$.face($fh.slurp(:bin), |c);
}

multi method version(::?CLASS:U:) {
self.new.version;
}
multi method version(::?CLASS:D:) returns Version:D {
$!raw.FT_Library_Version(my FT_Int $major, my FT_Int $minor, my FT_Int $patch);
Version.new: "{$major}.{$minor}.{$patch}";
}


=begin pod
=head1 class Font::FreeType - Raku FreeType2 Library Instance
Expand Down
69 changes: 34 additions & 35 deletions lib/Font/FreeType/BBox.rakumod
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
#| A generic font or glyph bounding box
class Font::FreeType::BBox
is Array {

use Font::FreeType::Raw;
use Font::FreeType::Raw::Defs;

constant Dot6 = Font::FreeType::Raw::Defs::Dot6;
has Numeric $.x-scale = 1 / Dot6;
has Numeric $.y-scale = 1 / Dot6;

method x-min is rw { self[0] }
method y-min is rw { self[1] }
method x-max is rw { self[2] }
method y-max is rw { self[3] }
method width { self[2] - self[0] }
method height { self[3] - self[1] }

multi method new(FT_BBox:D :bbox($_)!, |c) {
my \bbox = self.bless: | c;
bbox[0] = .x-min * bbox.x-scale;
bbox[1] = .y-min * bbox.y-scale;
bbox[2] = .x-max * bbox.x-scale;
bbox[3] = .y-max * bbox.y-scale;
bbox;
}

multi method new(Array:D :bbox($_)! where .elems == 4, |c) {
my \bbox = self.bless: | c;
bbox[0] = .[0] * bbox.x-scale;
bbox[1] = .[1] * bbox.y-scale;
bbox[2] = .[2] * bbox.x-scale;
bbox[3] = .[3] * bbox.y-scale;
bbox;
}
unit class Font::FreeType::BBox
is Array;

use Font::FreeType::Raw;
use Font::FreeType::Raw::Defs;

constant Dot6 = Font::FreeType::Raw::Defs::Dot6;
has Numeric $.x-scale = 1 / Dot6;
has Numeric $.y-scale = 1 / Dot6;

method x-min is rw { self[0] }
method y-min is rw { self[1] }
method x-max is rw { self[2] }
method y-max is rw { self[3] }
method width { self[2] - self[0] }
method height { self[3] - self[1] }

multi method new(FT_BBox:D :bbox($_)!, |c) {
my \bbox = self.bless: | c;
bbox[0] = .x-min * bbox.x-scale;
bbox[1] = .y-min * bbox.y-scale;
bbox[2] = .x-max * bbox.x-scale;
bbox[3] = .y-max * bbox.y-scale;
bbox;
}

multi method new(|c) { fail }
multi method new(Array:D :bbox($_)! where .elems == 4, |c) {
my \bbox = self.bless: | c;
bbox[0] = .[0] * bbox.x-scale;
bbox[1] = .[1] * bbox.y-scale;
bbox[2] = .[2] * bbox.x-scale;
bbox[3] = .[3] * bbox.y-scale;
bbox;
}

multi method new(|c) { fail }

=begin pod
=head2 Synopsis
Expand Down
Loading

0 comments on commit 9ff0fb8

Please sign in to comment.