Skip to content

Commit

Permalink
Fix pdf exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Feb 5, 2024
1 parent 59c4568 commit 0b59e3b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 100 deletions.
5 changes: 4 additions & 1 deletion app/lib/cubits/current_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -625,14 +625,17 @@ class CurrentIndexCubit extends Cubit<CurrentIndex> {
Future<pw.Document> renderPDF(
NoteData document,
DocumentInfo info, {
DocumentState? state,
required List<AreaPreset> areas,
bool renderBackground = true,
}) async {
final pdf = pw.Document();
for (final preset in areas) {
final areaName = preset.name;
final quality = preset.quality;
final page = document.getPage(preset.page);
final page = state?.pageName == preset.page
? state?.page
: document.getPage(preset.page);
final area = preset.area ?? page?.getAreaByName(areaName);
if (area == null || page == null) {
continue;
Expand Down
204 changes: 109 additions & 95 deletions app/lib/dialogs/export/pdf.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ class _PdfExportDialogState extends State<PdfExportDialog> {
),
IconButton(
onPressed: () async {
final result = await showDialog<(String, String)>(
final result = await showDialog<(String, Area)>(
context: context,
builder: (context) =>
_AreaSelectionDialog(document: state.data),
builder: (_) => BlocProvider.value(
value: context.read<DocumentBloc>(),
child: _AreaSelectionDialog(document: state.data),
),
);
if (result != null) {
final (page, area) = result;
setState(() {
areas.add(AreaPreset(name: area, page: page));
areas.add(AreaPreset(name: area.name, page: page));
});
}
},
Expand All @@ -96,77 +98,83 @@ class _PdfExportDialogState extends State<PdfExportDialog> {
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20, vertical: 15),
child: Column(mainAxisSize: MainAxisSize.min, children: [
Flexible(
child: SingleChildScrollView(
child: Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
children: areas.mapIndexed((i, e) {
final area =
e.area ?? state.page.getAreaByName(e.name);
if (area == null) {
return Container();
}
return FutureBuilder<ByteData?>(
future: currentIndex.render(
state.data, state.page, state.info,
width: area.width,
height: area.height,
quality: e.quality,
x: area.position.x,
y: area.position.y),
builder: (context, snapshot) => _AreaPreview(
area: area,
quality: e.quality,
onRemove: () {
setState(() {
areas.removeAt(i);
});
},
onQualityChanged: (value) {
setState(() {
areas[i] = e.copyWith(quality: value);
});
},
image: snapshot.data?.buffer.asUint8List(),
),
);
}).toList(),
),
),
),
const Divider(),
Row(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(child: Container()),
TextButton(
child: Text(AppLocalizations.of(context).cancel),
onPressed: () => Navigator.of(context).pop(),
Flexible(
child: SingleChildScrollView(
child: Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
children: areas.mapIndexed((i, e) {
final area = e.area ??
state.page.getAreaByName(e.name);
if (area == null) {
return Container();
}
return FutureBuilder<ByteData?>(
future: currentIndex.render(
state.data, state.page, state.info,
width: area.width,
height: area.height,
quality: e.quality,
x: area.position.x,
y: area.position.y),
builder: (context, snapshot) =>
_AreaPreview(
area: area,
quality: e.quality,
onRemove: () {
setState(() {
areas.removeAt(i);
});
},
onQualityChanged: (value) {
setState(() {
areas[i] = e.copyWith(quality: value);
});
},
image:
snapshot.data?.buffer.asUint8List(),
),
);
}).toList(),
),
),
),
ElevatedButton(
child: Text(widget.print
? AppLocalizations.of(context).print
: AppLocalizations.of(context).export),
onPressed: () async {
Future<Uint8List> getBytes() async =>
(await currentIndex.renderPDF(
state.data, state.info,
areas: areas))
.save();
Navigator.of(context).pop();
if (widget.print) {
Printing.layoutPdf(
onLayout: (_) => getBytes(),
);
return;
}
exportPdf(context, await getBytes());
},
),
],
)
]),
const Divider(),
Row(
children: [
Expanded(child: Container()),
TextButton(
child:
Text(AppLocalizations.of(context).cancel),
onPressed: () => Navigator.of(context).pop(),
),
ElevatedButton(
child: Text(widget.print
? AppLocalizations.of(context).print
: AppLocalizations.of(context).export),
onPressed: () async {
Future<Uint8List> getBytes() async =>
(await currentIndex.renderPDF(
state.data, state.info,
areas: areas, state: state))
.save();
Navigator.of(context).pop();
if (widget.print) {
Printing.layoutPdf(
onLayout: (_) => getBytes(),
);
return;
}
exportPdf(context, await getBytes());
},
),
],
)
]),
),
),
]);
Expand Down Expand Up @@ -255,28 +263,34 @@ class _AreaSelectionDialogState extends State<_AreaSelectionDialog> {
),
),
Flexible(
child: Column(
mainAxisSize: MainAxisSize.min,
children: widget.document
.getPages()
.expand(
(page) =>
widget.document
.getPage(page)
?.areas
.where((element) =>
element.name.contains(_searchQuery))
.map((e) {
return ListTile(
title: Text(e.name),
subtitle: Text(page),
onTap: () => Navigator.of(context).pop((page, e)),
);
}).toList() ??
<Widget>[],
)
.toList(),
),
child: BlocBuilder<DocumentBloc, DocumentState>(
buildWhen: (previous, current) =>
previous.page != current.page ||
previous.pageName != current.pageName,
builder: (context, state) => Column(
mainAxisSize: MainAxisSize.min,
children: widget.document
.getPages()
.expand(
(page) =>
(page == state.pageName
? state.page
: widget.document.getPage(page))
?.areas
.where((element) =>
element.name.contains(_searchQuery))
.map((e) {
return ListTile(
title: Text(e.name),
subtitle: Text(page),
onTap: () =>
Navigator.of(context).pop((page, e)),
);
}).toList() ??
<Widget>[],
)
.toList(),
)),
),
Padding(
padding: const EdgeInsets.all(8.0),
Expand Down
25 changes: 21 additions & 4 deletions app/lib/handlers/area.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,32 @@ class AreaHandler extends Handler<AreaTool> {
void onScaleUpdate(ScaleUpdateDetails details, EventContext context) {
final currentIndex = context.getCurrentIndex();
if (details.pointerCount > 1 ||
currentIndex.buttons == kSecondaryMouseButton) {
currentIndex.buttons == kSecondaryMouseButton ||
start == null) {
currentRect = null;
context.refresh();
return;
}
final globalPosition =
var end =
context.getCameraTransform().localToGlobal(details.localFocalPoint);

end = globalPosition;
if (data.constrainedWidth != 0) {
end = Offset(data.constrainedWidth + start!.dx, end.dy);
}
if (data.constrainedHeight != 0) {
end = Offset(end.dx, data.constrainedHeight + start!.dy);
}
if (data.constrainedAspectRatio != 0) {
final aspectRatio = data.constrainedAspectRatio;
final width = end.dx - start!.dx;
final height = end.dy - start!.dy;
final currentAspectRatio = width / height;
if (currentAspectRatio < aspectRatio) {
end = Offset(start!.dx + height * aspectRatio, end.dy);
} else {
end = Offset(end.dx, start!.dy + width / aspectRatio);
}
}
currentRect = Rect.fromPoints(start!, end);
context.refresh();
}

Expand Down

0 comments on commit 0b59e3b

Please sign in to comment.