diff --git a/.gitignore b/.gitignore index f8353b3..7d5f96c 100644 --- a/.gitignore +++ b/.gitignore @@ -63,4 +63,4 @@ ios/Runner/GoogleService-Info.plist coverage/ test/coverage_helper_test.dart **/failures/*.png -screenshots/generated/raw/ \ No newline at end of file +screenshots/generated/ \ No newline at end of file diff --git a/screenshots/convert_to_store_screenshot.dart b/screenshots/convert_to_store_screenshot.dart index 8223324..09949ba 100644 --- a/screenshots/convert_to_store_screenshot.dart +++ b/screenshots/convert_to_store_screenshot.dart @@ -30,8 +30,7 @@ extension ScreenshotImage on Image { args.backgroundColor.a, ), ) - ._applyLayout(layout) - ._addText( + ._applyLayout( layout, _configs[args.name]!.title, _configs[args.name]!.subtitle, @@ -75,16 +74,20 @@ extension ScreenshotImage on Image { return compositeImage(expandedScreenshot, frame); } - Image _applyLayout(ScreenshotLayout layout) { - final scaledScreenshot = copyResize( - this, - width: layout.size.width - (layout.contentPadding.left + layout.contentPadding.right), - ); + Image _applyLayout(ScreenshotLayout layout, String title, String subtitle, {required bool isDark}) { + final textImage = _drawTitles(layout, title, subtitle, isDark: isDark); + final maxFrameHeight = + layout.size.height - (layout.contentPadding.top + textImage.height + 84 + layout.contentPadding.bottom); + int maxFrameWidth = layout.size.width - (layout.contentPadding.left + layout.contentPadding.right); + if (maxFrameWidth * height / width > maxFrameHeight) { + maxFrameWidth = maxFrameHeight * width ~/ height; + } + final scaledScreenshot = copyResize(this, width: maxFrameWidth); - return copyExpandCanvas( + final draft = copyExpandCanvas( copyExpandCanvas( scaledScreenshot, - newWidth: scaledScreenshot.width + layout.contentPadding.right, + newWidth: scaledScreenshot.width + (layout.size.width - scaledScreenshot.width) ~/ 2, newHeight: scaledScreenshot.height + layout.contentPadding.bottom, position: ExpandCanvasPosition.topLeft, backgroundColor: getPixel(0, 0), @@ -94,9 +97,16 @@ extension ScreenshotImage on Image { position: ExpandCanvasPosition.bottomRight, backgroundColor: getPixel(0, 0), ); + + return compositeImage( + draft, + textImage, + dstX: layout.contentPadding.left, + dstY: layout.contentPadding.top, + ); } - Image _addText(ScreenshotLayout layout, String title, String subtitle, {required bool isDark}) { + Image _drawTitles(ScreenshotLayout layout, String title, String subtitle, {required bool isDark}) { final titleFont = BitmapFont.fromZip(File(isDark ? layout.titleFontDarkPath : layout.titleFontPath).readAsBytesSync()); final subtitleFont = @@ -127,11 +137,6 @@ extension ScreenshotImage on Image { subtitleDy += subtitleFont.lineHeight; }); - return compositeImage( - this, - textImage, - dstX: layout.contentPadding.left, - dstY: layout.contentPadding.top, - ); + return textImage; } } diff --git a/screenshots/main.dart b/screenshots/main.dart index 11beb4f..c27c647 100644 --- a/screenshots/main.dart +++ b/screenshots/main.dart @@ -13,7 +13,8 @@ Future main(List args) async { final parser = ArgParser() ..addFlag('verbose', abbr: 'v', help: 'Verbose output.') ..addOption('platform', abbr: 'p', help: 'Device platform.', mandatory: true) - ..addOption('device', abbr: 'd', help: 'device_snake_name', mandatory: true); + ..addOption('device', abbr: 'd', help: 'device_snake_name', mandatory: true) + ..addOption('layout', abbr: 'l', help: 'Device platform.', mandatory: true); final ArgResults argResults = parser.parse(args); if (argResults['verbose'] as bool) { @@ -22,8 +23,10 @@ Future main(List args) async { Logger.root.level = Level.INFO; } - final device = argResults["device"] as String; final platform = argResults["platform"] as String; + final device = argResults["device"] as String; + final layout = ScreenshotLayout.values.firstWhere((e) => e.name == argResults["layout"] as String); + Directory('screenshots/generated/raw/$platform/$device').listSync().forEach((filePath) async { final screenshotName = filePath.path.split('/').last.replaceAll('.png', ''); final screenshotBytes = File(filePath.path).readAsBytesSync(); @@ -43,12 +46,13 @@ Future main(List args) async { isDark: filePath.path.contains('dark-'), ); - final file = await File(filePath.path.replaceAll('/raw', '')).create(recursive: true); + final file = + await File('screenshots/generated/$platform/${layout.name}/$screenshotName.png').create(recursive: true); file.writeAsBytesSync( encodePng( screenshot.convertToStoreScreenshot( args: screenshotArgs, - layout: ScreenshotLayout.iphone65inch, + layout: layout, ), ), ); diff --git a/screenshots/models/screenshot_layout.dart b/screenshots/models/screenshot_layout.dart index f18ce87..841c5e3 100644 --- a/screenshots/models/screenshot_layout.dart +++ b/screenshots/models/screenshot_layout.dart @@ -1,13 +1,13 @@ enum ScreenshotLayout { iphone65inch( size: (width: 1242, height: 2688), - contentPadding: (left: 150, top: 192, right: 150, bottom: 192), + contentPadding: (left: 144, top: 184, right: 144, bottom: 184), titleFontPath: 'screenshots/assets/fonts/SF-Pro-Display-Bold.zip', subtitleFontPath: 'screenshots/assets/fonts/SF-Pro-Display-Regular.zip', ), iphone55inch( size: (width: 1242, height: 2208), - contentPadding: (left: 150, top: 192, right: 150, bottom: 192), + contentPadding: (left: 144, top: 144, right: 144, bottom: 144), titleFontPath: 'screenshots/assets/fonts/SF-Pro-Display-Bold.zip', subtitleFontPath: 'screenshots/assets/fonts/SF-Pro-Display-Regular.zip', ); diff --git a/test_driver/screenshot_driver.dart b/test_driver/screenshot_driver.dart index d4e485d..ae1c85e 100644 --- a/test_driver/screenshot_driver.dart +++ b/test_driver/screenshot_driver.dart @@ -1,24 +1,15 @@ import 'dart:io'; -import 'dart:typed_data'; -import 'package:image/image.dart'; import 'package:integration_test/integration_test_driver_extended.dart'; -import '../screenshots/convert_to_store_screenshot.dart'; import '../screenshots/models/screenshot_args.dart'; -import '../screenshots/models/screenshot_layout.dart'; Future main() async { await integrationDriver( onScreenshot: (name, bytes, [_]) async { final screenshotArgs = ScreenshotArgs.fromString(name); final file = await File(screenshotArgs.toPathRaw()).create(recursive: true); - final screenshot = decodePng(Uint8List.fromList(bytes))!.convertToStoreScreenshot( - args: screenshotArgs, - layout: ScreenshotLayout.iphone65inch, - ); - - file.writeAsBytesSync(encodePng(screenshot)); + file.writeAsBytesSync(bytes); return true; }, );