Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
380 changes: 341 additions & 39 deletions lib/bademagic_module/utils/converters.dart

Large diffs are not rendered by default.

12,548 changes: 12,548 additions & 0 deletions lib/bademagic_module/utils/font_cache.dart

Large diffs are not rendered by default.

231 changes: 231 additions & 0 deletions lib/bademagic_module/utils/font_matrix_logger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:badgemagic/providers/font_provider.dart';
import 'package:badgemagic/bademagic_module/utils/converters.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'package:get_it/get_it.dart';

class FontMatrixLogger {
static const List<String> _chars = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'!',
'@',
'#',
'\$',
'%',
'^',
'&',
'*',
'(',
')',
'-',
'_',
'+',
'=',
'{',
'}',
'[',
']',
'|',
'\\',
':',
';',
'"',
'\'',
'<',
'>',
',',
'.',
'?',
'/',
'~',
'`',
' ',
];

static Future<List<String>> getSaveLocations() async {
List<String> locations = [];

// Add the specific fontcache.txt location for Windows
locations.add(path.join('C:', 'Users', 'fontcache.txt'));

// Add the platform-specific BadgeMagic directory location
if (Platform.isAndroid) {
final dir = await getExternalStorageDirectory();
if (dir != null) {
final badgeMagicDir = Directory(path.join(dir.path, 'BadgeMagic'));
if (!await badgeMagicDir.exists()) {
await badgeMagicDir.create(recursive: true);
}
locations.add(badgeMagicDir.path);
}
} else {
final dir = await getApplicationDocumentsDirectory();
final badgeMagicDir = Directory(path.join(dir.path, 'BadgeMagic'));
if (!await badgeMagicDir.exists()) {
await badgeMagicDir.create(recursive: true);
}
locations.add(badgeMagicDir.path);
}

return locations;
}

static void logAllMatrices(Converters converters) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
final buffer = StringBuffer();
buffer.writeln('Font Matrix Cache - Generated ${DateTime.now()}\n');
buffer.writeln(
'const Map<String, Map<String, List<List<bool>>>> fontCache = {');

final fontProvider = GetIt.instance<FontProvider>();

// Then process all other fonts

await _processFont(fontProvider, converters, buffer);

buffer.writeln('};');

// Save to all file locations
try {
final locations = await getSaveLocations();
final content = buffer.toString();

for (final location in locations) {
if (location.endsWith('.txt')) {
// For the specific fontcache.txt file
final file = File(location);
await file.writeAsString(content);
print('Font matrices saved to: ${file.path}');
} else {
// For the BadgeMagic directory
final fileName =
'font_matrices_${DateTime.now().millisecondsSinceEpoch}.txt';
final filePath = path.join(location, fileName);
final file = File(filePath);
await file.writeAsString(content);
print('Font matrices saved to: ${file.path}');
}
}
} catch (e) {
print('Error saving font matrices: $e');
}
});
}

static Future<void> _processFont(FontProvider fontProvider,
Converters converters, StringBuffer buffer) async {
await Future.delayed(const Duration(milliseconds: 2));

final style = fontProvider.selectedTextStyle;
final fontKey = _createFontKey(
fontProvider.selectedTextStyle.fontFamily ?? 'Default',
style.fontSize ?? 12,
style.fontWeight ?? FontWeight.normal,
style.fontStyle == FontStyle.italic,
);

buffer.writeln(" '$fontKey': {");
print(
'Processing font: ${fontProvider.selectedTextStyle.fontFamily ?? "Default"}');

for (final char in _chars) {
try {
final result = await converters.renderTextToMatrix(
char,
fontProvider.selectedTextStyle,
rows: 11,
hasDescender: _hasDescender(char),
);

buffer.writeln(" '$char': [");
for (final row in result['matrix']!) {
buffer.writeln(' ${row.map((b) => b.toString()).toList()},');
}
buffer.writeln(' ],');
print(
'Processed char: $char for font: ${fontProvider.selectedTextStyle.fontFamily ?? "Default"}');
} catch (e) {
print(
'Error rendering $char for font ${fontProvider.selectedTextStyle.fontFamily ?? "Default"}: $e');
}
}

buffer.writeln(' },');
}

static String _createFontKey(
String fontFamily, double fontSize, FontWeight weight, bool italic) {
return '$fontFamily-${fontSize.round()}-${weight.index}-$italic';
}

static bool _hasDescender(String char) {
return char == 'g' ||
char == 'j' ||
char == 'p' ||
char == 'q' ||
char == 'y';
}
}
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:badgemagic/providers/font_provider.dart';
import 'package:badgemagic/providers/getitlocator.dart';
import 'package:badgemagic/providers/imageprovider.dart';
import 'package:badgemagic/view/about_us_screen.dart';
Expand All @@ -23,6 +24,8 @@ void main() {
providers: [
ChangeNotifierProvider<InlineImageProvider>(
create: (context) => getIt<InlineImageProvider>()),
ChangeNotifierProvider<FontProvider>(
create: (context) => getIt<FontProvider>()),
],
child: const MyApp(),
));
Expand Down
54 changes: 54 additions & 0 deletions lib/providers/font_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

class FontProvider extends ChangeNotifier {
String? _selectedFont;
final List<String> availableFonts = const [
'Roboto',
'Open Sans',
'Lato',
'Poppins',
'Montserrat',
'Orbitron',
'Lexend',
];

String? get selectedFont => _selectedFont;

void changeFont(String? newFont) {
_selectedFont = newFont;
notifyListeners();
}

TextStyle get selectedTextStyle {
const baseStyle = TextStyle(fontSize: 12, color: Colors.black);
if (_selectedFont == null) return baseStyle;

switch (_selectedFont!) {
case 'Roboto':
return GoogleFonts.roboto(
textStyle: baseStyle.copyWith(fontWeight: FontWeight.w700));
case 'Open Sans':
return GoogleFonts.openSans(
textStyle: baseStyle.copyWith(fontWeight: FontWeight.w700));
case 'Lato':
return GoogleFonts.lato(
textStyle: baseStyle.copyWith(fontWeight: FontWeight.w700));
case 'Poppins':
return GoogleFonts.poppins(
textStyle: baseStyle.copyWith(fontWeight: FontWeight.w500));
case 'Montserrat':
return GoogleFonts.montserrat(
textStyle: baseStyle.copyWith(fontWeight: FontWeight.w700));
case 'Orbitron':
return GoogleFonts.orbitron(
textStyle:
baseStyle.copyWith(fontSize: 10, fontWeight: FontWeight.w700));
case 'Lexend':
return GoogleFonts.lexend(
textStyle: baseStyle.copyWith(fontWeight: FontWeight.w700));
default:
return baseStyle;
}
}
}
2 changes: 2 additions & 0 deletions lib/providers/getitlocator.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:badgemagic/providers/font_provider.dart';
import 'package:badgemagic/providers/imageprovider.dart';
import 'package:get_it/get_it.dart';

final GetIt getIt = GetIt.instance;

void setupLocator() {
getIt.registerLazySingleton<InlineImageProvider>(() => InlineImageProvider());
getIt.registerLazySingleton<FontProvider>(() => FontProvider());
}
Loading