-
Notifications
You must be signed in to change notification settings - Fork 241
feat: Added Support for Fonts #1259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vishveshwara
wants to merge
16
commits into
fossasia:development
Choose a base branch
from
Vishveshwara:fonts
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+974
−542
Open
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
69d29bd
feat: Added Support for Fonts
Vishveshwara 487e49d
feat: optimized font width calculation and character caching
Vishveshwara 08b2404
Updated caching approach
Vishveshwara 4551181
Removed font_cache.dart
Vishveshwara c386e92
Updated comments in convertBitmapToLEDHex
Vishveshwara 41e3ea6
Merge branch 'flutter_app' into fonts
Vishveshwara 66bd095
Merge branch 'development' into fonts
Vishveshwara 8a56268
fix common build
Vishveshwara fd3bfc3
Merge branch 'fonts' of https://github.com/Vishveshwara/badgemagic-ap…
Vishveshwara 0b23dc6
Fixed common build
Vishveshwara 755a5d3
Fixed Artifacts Issue
Vishveshwara bb0b66e
Optimized renderTextToMatrix' For loops
Vishveshwara c47f7c8
Merge branch 'development' into fonts
Vishveshwara e7cf6ec
Merge branch 'development' into fonts
Vishveshwara bc9f569
Applied dart formatting
Vishveshwara b586e67
Merge branch 'development' into fonts
Vishveshwara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Vishveshwara marked this conversation as resolved.
Show resolved
Hide resolved
|
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.