Skip to content

Commit 561a9d7

Browse files
committed
migrate to null-safety, fix #7
1 parent f90587e commit 561a9d7

File tree

12 files changed

+65
-69
lines changed

12 files changed

+65
-69
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import 'package:enough_ascii_art/enough_ascii_art.dart' as art;
2020
import 'package:image/image.dart' as img;
2121
2222
void main() async {
23-
var image = img.decodeImage(File('./example/enough.jpg').readAsBytesSync());
23+
final bytes = await File('./example/enough.jpg').readAsBytes();
24+
final image = img.decodeImage(bytes)!;
2425
var asciiImage = art.convertImage(image, maxWidth: 40, invert: true);
2526
print('');
2627
print(asciiImage);

example/enough_ascii_art_example.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import 'package:enough_ascii_art/enough_ascii_art.dart' as art;
33
import 'package:image/image.dart' as img;
44

55
void main() async {
6-
var image = img.decodeImage(File('./example/enough.jpg').readAsBytesSync());
6+
final bytes = await File('./example/enough.jpg').readAsBytes();
7+
final image = img.decodeImage(bytes)!;
78
var asciiImage = art.convertImage(image, maxWidth: 40, invert: true);
89
print('');
910
print(asciiImage);

lib/enough_ascii_art.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const String _asciiGrayScaleCharacters = '#@%=+*:-. ';
3131
/// [fontHeightCompensationFactor] the optional factor between 0 and 1 that is used to adjust the height of the image. Most fonts have a greater height than width, so this factor allows to compensate this. Defaults to 0.6.
3232
String convertImage(Image image,
3333
{int maxWidth = 80,
34-
int maxHeight,
34+
int? maxHeight,
3535
String charset = _asciiGrayScaleCharacters,
3636
bool invert = false,
3737
double fontHeightCompensationFactor = 0.6}) {

lib/src/emoticon_converter.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class EmoticonConverter {
6363
/// [style] the optional emoticon style
6464
static String convertEmoticons(String text,
6565
[EmoticonStyle style = EmoticonStyle.western]) {
66-
if (text == null || text.isEmpty) {
66+
if (text.isEmpty) {
6767
return text;
6868
}
6969
switch (style) {
@@ -75,7 +75,7 @@ class EmoticonConverter {
7575

7676
static String _convert(String text, Map<String, String> emoticons) {
7777
for (var key in emoticons.keys) {
78-
text = text.replaceAll(key, emoticons[key]);
78+
text = text.replaceAll(key, emoticons[key]!);
7979
}
8080
return text;
8181
}

lib/src/figlet/figlet.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:io';
2-
import 'package:enough_ascii_art/src/figlet/parser.dart';
31
import 'package:enough_ascii_art/src/figlet/renderer.dart';
42

53
import 'font.dart';

lib/src/figlet/font.dart

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,19 @@ enum VerticalLayout {
7272
///
7373
/// Since a FIGfont file is a text file, it can be created with any text editing program on any platform. The filename of a FIGfont file must end with ".flf", which stands for "FIGLettering Font".
7474
class Font {
75-
int hardblank;
76-
int height;
77-
int baseLine;
78-
int maxCharacterLength;
79-
PrintDirection printDirection;
80-
List<HorizontalLayout> horizontalLayouts;
75+
int? hardblank;
76+
late int height;
77+
int? baseLine;
78+
int? maxCharacterLength;
79+
PrintDirection? printDirection;
80+
List<HorizontalLayout>? horizontalLayouts;
8181
bool get isFullWidthLayout =>
8282
horizontalLayouts == null ||
83-
(horizontalLayouts.length == 1 &&
84-
horizontalLayouts.first == HorizontalLayout.fullWidth);
83+
(horizontalLayouts!.length == 1 &&
84+
horizontalLayouts!.first == HorizontalLayout.fullWidth);
8585

86-
Character _zeroCharacter;
87-
final Map<int, Character> _charactersByRuneCode = <int, Character>{};
86+
Character? _zeroCharacter;
87+
final _charactersByRuneCode = <int, Character>{};
8888

8989
void addCharacter(
9090
int runeCode, int characterWidth, List<String> characterLines) {
@@ -95,16 +95,15 @@ class Font {
9595
}
9696
}
9797

98-
Character getCharacter(int rune) {
98+
Character? getCharacter(int rune) {
9999
var character = _charactersByRuneCode[rune];
100-
character ??= _zeroCharacter;
101-
return character;
100+
return character ?? _zeroCharacter;
102101
}
103102

104103
static Future<Font> network(String src) async {
105104
final response = await HttpHelper.httpGet(src);
106105
if (response.statusCode == 200) {
107-
return text(response.text);
106+
return text(response.text!);
108107
}
109108
throw StateError(
110109
'Unable to resolve url "$src": got response code ${response.statusCode}');

lib/src/figlet/parser.dart

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,19 @@ class Parser {
4545
var baseLine = int.tryParse(baseDefinitionElements[2]);
4646
var maxCharacterLength = int.tryParse(baseDefinitionElements[3]);
4747
var oldLayoutInt = int.tryParse(baseDefinitionElements[4]);
48-
var numberOfCommentLines = int.tryParse(baseDefinitionElements[5]);
48+
var numberOfCommentLines = int.tryParse(baseDefinitionElements[5]) ?? 0;
4949
var textDirectionInt = 0;
50-
int fullLayoutInt;
50+
int? fullLayoutInt;
5151
if (baseDefinitionElements.length > 7) {
52-
var tempDirectionInt = int.tryParse(baseDefinitionElements[6]);
53-
if (tempDirectionInt != null) {
54-
textDirectionInt = tempDirectionInt;
55-
}
52+
textDirectionInt = int.tryParse(baseDefinitionElements[6]) ?? 0;
5653
fullLayoutInt = int.tryParse(baseDefinitionElements[7]);
5754
}
5855
var font = Font()
5956
..hardblank = hardblank
6057
..height = height
6158
..baseLine = baseLine
6259
..maxCharacterLength = maxCharacterLength
63-
..printDirection = (textDirectionInt == 0 || textDirectionInt == null)
60+
..printDirection = (textDirectionInt == 0)
6461
? PrintDirection.leftToRight
6562
: PrintDirection.rightToLeft
6663
..horizontalLayouts = _getHorizontalLayouts(oldLayoutInt, fullLayoutInt);
@@ -121,23 +118,24 @@ class Parser {
121118
var lineIndex = 1 + numberOfCommentLines;
122119
var runeCode = 32;
123120
var parsePhase = _ParsePhase.ascii;
124-
var umlauts = [196, 214, 220, 228, 246, 252, 223];
121+
final umlauts = [196, 214, 220, 228, 246, 252, 223];
125122
var germanUmlautIndex = 0;
126123
while (lineIndex < lines.length) {
127124
if (parsePhase == _ParsePhase.utf8) {
128125
// read code unit in first line of character definition:
129126
var codeUnitLine = lines[lineIndex];
130127
lineIndex++;
131-
var spaceIndex = codeUnitLine.indexOf(' ');
128+
final spaceIndex = codeUnitLine.indexOf(' ');
132129
if (spaceIndex != -1) {
133130
codeUnitLine = codeUnitLine.substring(0, spaceIndex);
134131
}
135132
if (codeUnitLine.isEmpty) continue;
136-
runeCode = int.tryParse(codeUnitLine);
137-
if (runeCode == null) {
133+
final intValue = int.tryParse(codeUnitLine);
134+
if (intValue == null) {
138135
throw FormatException(
139136
'Invalid font definition at line [$lineIndex]: [$codeUnitLine] contains invalid code unit at beginning.');
140137
}
138+
runeCode = intValue;
141139
}
142140
var firstCharacterLine = lines[lineIndex];
143141
var characterWidth = firstCharacterLine.length - 1;
@@ -174,9 +172,10 @@ class Parser {
174172
return font;
175173
}
176174

177-
List<HorizontalLayout> _getHorizontalLayouts(int oldLayout, int fullLayout) {
175+
List<HorizontalLayout> _getHorizontalLayouts(
176+
int? oldLayout, int? fullLayout) {
178177
var layouts = <HorizontalLayout>[];
179-
var compatibleLayout = fullLayout ?? oldLayout;
178+
var compatibleLayout = fullLayout ?? oldLayout!;
180179
if (compatibleLayout & horizontalEqualCharacterSmushing ==
181180
horizontalEqualCharacterSmushing) {
182181
layouts.add(HorizontalLayout.equalCharacterSmushing);

lib/src/figlet/renderer.dart

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'figlet.dart';
2-
import 'figlet.dart';
32
import 'font.dart';
43

54
enum RenderOptions { none }
@@ -17,13 +16,13 @@ class Renderer {
1716
String text,
1817
Font font,
1918
FigletRenderDirection direction, {
20-
int maxLineWidth,
21-
List<HorizontalLayout> horizontalLayouts,
22-
List<VerticalLayout> verticalLayouts,
19+
int? maxLineWidth,
20+
List<HorizontalLayout>? horizontalLayouts,
21+
List<VerticalLayout>? verticalLayouts,
2322
}) {
2423
horizontalLayouts ??= font.horizontalLayouts;
2524
if (direction == FigletRenderDirection.LeftToRight) {
26-
return _renderLR(text, font, horizontalLayouts);
25+
return _renderLR(text, font, horizontalLayouts!);
2726
} else {
2827
return _renderTB(text, font);
2928
}
@@ -33,9 +32,9 @@ class Renderer {
3332
_RenderLine renderLine,
3433
int rune,
3534
Font font,
36-
List<HorizontalLayout> horizontalLayouts,
35+
List<HorizontalLayout>? horizontalLayouts,
3736
) {
38-
var character = font.getCharacter(rune);
37+
final character = font.getCharacter(rune);
3938
if (character != null) {
4039
renderLine.addCharacterToRight(character, horizontalLayouts);
4140
//TODO observe line width and wrap text when reaching maximum width
@@ -49,20 +48,20 @@ class Renderer {
4948
List<HorizontalLayout> horizontalLayouts,
5049
) {
5150
var renderLine = _RenderLine(font);
52-
var runes = text.runes;
51+
final runes = text.runes;
5352
for (var rune in runes) {
5453
_processRune(renderLine, rune, font, horizontalLayouts);
5554
}
56-
var result = renderLine.render();
55+
final result = renderLine.render();
5756
return result;
5857
}
5958

6059
String _renderTB(String text, Font font) {
61-
var runes = text.runes;
62-
var result = <String>[];
60+
final runes = text.runes;
61+
final result = <String>[];
6362

6463
for (var rune in runes) {
65-
var renderLine = _RenderLine(font);
64+
final renderLine = _RenderLine(font);
6665
_processRune(renderLine, rune, font, font.horizontalLayouts);
6766
result.add(renderLine.render());
6867
}
@@ -88,7 +87,7 @@ class _RenderLine {
8887

8988
// Underscore smushing: An underscore ("_") will be replaced by any of: "|", "/", "\", "[", "]", "{", "}", "(", ")", "<" or ">".
9089
// Hierarchy smushing: A hierarchy of six classes is used: "|", "/\", "[]", "{}", "()", and "<>".
91-
static const List<int> _underscoreOrHierarchyReplacementsRunes = [
90+
static const List<int?> _underscoreOrHierarchyReplacementsRunes = [
9291
_runeVerticalBar,
9392
_runeForwardSlash,
9493
_runeBackwardSlash,
@@ -108,7 +107,8 @@ class _RenderLine {
108107

109108
_RenderLine(this._font);
110109

111-
int addCharacterToRight(Character character, List<HorizontalLayout> layouts) {
110+
int addCharacterToRight(
111+
Character character, List<HorizontalLayout>? layouts) {
112112
var isFullWidthLayout = layouts == null ||
113113
layouts.isEmpty ||
114114
(layouts.length == 1 && layouts[0] == HorizontalLayout.fullWidth);
@@ -141,7 +141,7 @@ class _RenderLine {
141141
}
142142

143143
void _layoutLeftToRight(_RenderCharacter left, _RenderCharacter right,
144-
List<HorizontalLayout> layouts) {
144+
List<HorizontalLayout>? layouts) {
145145
var originalRightCharacter =
146146
right.character; // used to restore in case smushing needs to be aborted
147147
var hardblank = _font.hardblank;
@@ -171,7 +171,7 @@ class _RenderLine {
171171
var isLayoutRuleAppliedForLine = false;
172172
var leftRune = leftPos.rune;
173173
var rightRune = rightPos.rune;
174-
for (var layout in layouts) {
174+
for (var layout in layouts!) {
175175
switch (layout) {
176176
// Moves FIGcharacters closer together until they touch.
177177
// Typographers use the term "kerning" for this phenomenon when applied to the horizontal axis.
@@ -205,7 +205,7 @@ class _RenderLine {
205205
} else if ((rightIndex < leftIndex) &&
206206
(leftIndex & 1 == 1 || rightIndex < leftIndex - 1)) {
207207
isLayoutRuleAppliedForLine = true;
208-
replace(right, row, rightPos, String.fromCharCode(leftRune));
208+
replace(right, row, rightPos, String.fromCharCode(leftRune!));
209209
}
210210
}
211211
break;
@@ -300,7 +300,7 @@ class _RenderLine {
300300
} else {
301301
copyLine = copyLine.substring(0, pos) +
302302
replacement +
303-
copyLine.substring(pos + 1);
303+
copyLine.substring(pos! + 1);
304304
}
305305
copy.lines[row] = copyLine;
306306
//print('replaced ${renderCharacter.character.lines[row]} with $copyLine');
@@ -332,11 +332,10 @@ class _RenderLine {
332332
}
333333

334334
String render() {
335-
var runeHardblank = _font.hardblank;
336-
var buffer = StringBuffer();
335+
final runeHardblank = _font.hardblank;
336+
final buffer = StringBuffer();
337337
for (var row = 0; row < _font.height; row++) {
338-
//var lineOfRunes = List<int>(width);
339-
var lineOfRunes = List<int>(_width);
338+
final lineOfRunes = List.filled(_width, 32);
340339
var charIndex = 0;
341340
for (var char in _characters) {
342341
charIndex += char.relativeX;
@@ -348,8 +347,7 @@ class _RenderLine {
348347
if (rune == runeHardblank) {
349348
rune = _runeSpace;
350349
}
351-
if (charIndex >= 0 &&
352-
(rune != _runeSpace || lineOfRunes[charIndex] == null)) {
350+
if (charIndex >= 0 && (rune != _runeSpace)) {
353351
lineOfRunes[charIndex] = rune;
354352
}
355353
charIndex++;
@@ -365,13 +363,13 @@ class _RenderLine {
365363

366364
class _RenderCharacter {
367365
Character character;
368-
int relativeX;
366+
late int relativeX;
369367
_RenderCharacter(this.character);
370368
}
371369

372370
class _RunePos {
373-
final int rune;
374-
final int pos;
371+
final int? rune;
372+
final int? pos;
375373
final int steps;
376374
_RunePos(this.rune, this.pos, this.steps);
377375
}

lib/src/image_converter.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ class ImageConverter {
88
///
99
/// [image] the image to be converted
1010
/// [maxWidth] the optional maximum width of the image in characters, defaults to 80
11-
/// [maxHeight] the optional maximum height of th eimage in characters, defaults to null, so the image is caled linearily
11+
/// [maxHeight] the optional maximum height of the image in characters, defaults to null, so the image is scaled linearily
1212
/// [charSet] the optional charset from darkest to lightest character, defaults to '#@%=+*:-. '
1313
/// [invert] allows to invert pixels, so that a dark pixel gets a bright characater and vise versa. This is useful when printing bight text on a dark background (console). Defaults to false.
1414
/// [fontHeightCompensationFactor] the optional factor between 0 and 1 that is used to adjust the height of the image. Most fonts have a greater height than width, so this factor allows to compensate this. Defaults to 0.6.
1515
static String convertImage(Image image,
1616
{int maxWidth = 80,
17-
int maxHeight,
17+
int? maxHeight,
1818
String charset = _asciiGrayScaleCharacters,
1919
bool invert = false,
2020
double fontHeightCompensationFactor = 0.6}) {

lib/src/unicode_font_converter.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ enum UnicodeFont {
2626
class UnicodeFontConverter {
2727
static String encode(final String text, final UnicodeFont font) {
2828
final buffer = StringBuffer();
29-
final from = _fonts[UnicodeFont.normal];
30-
final to = _fonts[font];
29+
final from = _fonts[UnicodeFont.normal]!;
30+
final to = _fonts[font]!;
3131
for (var i = 0; i < text.length; i++) {
3232
final c = text[i];
3333
final index = from.indexOf(c);

0 commit comments

Comments
 (0)