David PHAM-VAN

Improve Build Context

@@ -104,8 +104,11 @@ class Page extends BasePage { @@ -104,8 +104,11 @@ class Page extends BasePage {
104 final Theme calculatedTheme = theme ?? document.theme ?? Theme.base(); 104 final Theme calculatedTheme = theme ?? document.theme ?? Theme.base();
105 final Map<Type, Inherited> inherited = <Type, Inherited>{}; 105 final Map<Type, Inherited> inherited = <Type, Inherited>{};
106 inherited[calculatedTheme.runtimeType] = calculatedTheme; 106 inherited[calculatedTheme.runtimeType] = calculatedTheme;
107 - final Context context =  
108 - Context(page: pdfPage, canvas: canvas, inherited: inherited); 107 + final Context context = Context(
  108 + document: document.document,
  109 + page: pdfPage,
  110 + canvas: canvas,
  111 + inherited: inherited);
109 if (_build != null) { 112 if (_build != null) {
110 final Widget child = _build(context); 113 final Widget child = _build(context);
111 layout(child, context, constraints); 114 layout(child, context, constraints);
@@ -187,7 +190,9 @@ class MultiPage extends Page { @@ -187,7 +190,9 @@ class MultiPage extends Page {
187 double offsetEnd; 190 double offsetEnd;
188 double offsetStart; 191 double offsetStart;
189 int index = 0; 192 int index = 0;
190 - final List<Widget> children = _buildList(Context(inherited: inherited)); 193 + final Context baseContext =
  194 + Context(document: document.document, inherited: inherited);
  195 + final List<Widget> children = _buildList(baseContext);
191 WidgetContext widgetContext; 196 WidgetContext widgetContext;
192 197
193 while (index < children.length) { 198 while (index < children.length) {
@@ -197,7 +202,7 @@ class MultiPage extends Page { @@ -197,7 +202,7 @@ class MultiPage extends Page {
197 final PdfPage pdfPage = 202 final PdfPage pdfPage =
198 PdfPage(document.document, pageFormat: pageFormat); 203 PdfPage(document.document, pageFormat: pageFormat);
199 final PdfGraphics canvas = pdfPage.getGraphics(); 204 final PdfGraphics canvas = pdfPage.getGraphics();
200 - context = Context(page: pdfPage, canvas: canvas, inherited: inherited); 205 + context = baseContext.copyWith(page: pdfPage, canvas: canvas);
201 assert(() { 206 assert(() {
202 if (Document.debug) { 207 if (Document.debug) {
203 debugPaint(context); 208 debugPaint(context);
@@ -125,7 +125,7 @@ class Font { @@ -125,7 +125,7 @@ class Font {
125 125
126 PdfFont getFont(Context context) { 126 PdfFont getFont(Context context) {
127 if (_pdfFont == null) { 127 if (_pdfFont == null) {
128 - final PdfDocument pdfDocument = context.page.pdfDocument; 128 + final PdfDocument pdfDocument = context.document;
129 _pdfFont = buildFont(pdfDocument); 129 _pdfFont = buildFont(pdfDocument);
130 } 130 }
131 131
@@ -18,7 +18,13 @@ part of widget; @@ -18,7 +18,13 @@ part of widget;
18 18
19 @immutable 19 @immutable
20 class Context { 20 class Context {
21 - const Context({this.page, this.canvas, this.inherited}); 21 + const Context({
  22 + @required this.document,
  23 + this.page,
  24 + this.canvas,
  25 + @required this.inherited,
  26 + }) : assert(document != null),
  27 + assert(inherited != null);
22 28
23 final PdfPage page; 29 final PdfPage page;
24 30
@@ -26,11 +32,19 @@ class Context { @@ -26,11 +32,19 @@ class Context {
26 32
27 final Map<Type, Inherited> inherited; 33 final Map<Type, Inherited> inherited;
28 34
29 - int get pageNumber => page.pdfDocument.pdfPageList.pages.indexOf(page) + 1; 35 + final PdfDocument document;
  36 +
  37 + int get pageNumber => document.pdfPageList.pages.indexOf(page) + 1;
  38 +
  39 + int get pagesCount => document.pdfPageList.pages.length;
30 40
31 Context copyWith( 41 Context copyWith(
32 - {PdfPage page, PdfGraphics canvas, Map<Type, Inherited> inherited}) { 42 + {PdfPage page,
  43 + PdfGraphics canvas,
  44 + Matrix4 ctm,
  45 + Map<Type, Inherited> inherited}) {
33 return Context( 46 return Context(
  47 + document: document,
34 page: page ?? this.page, 48 page: page ?? this.page,
35 canvas: canvas ?? this.canvas, 49 canvas: canvas ?? this.canvas,
36 inherited: inherited ?? this.inherited); 50 inherited: inherited ?? this.inherited);
@@ -48,12 +62,17 @@ class Inherited {} @@ -48,12 +62,17 @@ class Inherited {}
48 abstract class Widget { 62 abstract class Widget {
49 Widget(); 63 Widget();
50 64
  65 + /// The bounding box of this widget, calculated at layout time
51 PdfRect box; 66 PdfRect box;
52 67
  68 + /// First widget pass to calculate the children layout and
  69 + /// bounding [box]
53 @protected 70 @protected
54 void layout(Context context, BoxConstraints constraints, 71 void layout(Context context, BoxConstraints constraints,
55 {bool parentUsesSize = false}); 72 {bool parentUsesSize = false});
56 73
  74 + /// Draw itself and its children, according to the calculated
  75 + /// [box.offset]
57 @protected 76 @protected
58 void paint(Context context) { 77 void paint(Context context) {
59 assert(() { 78 assert(() {