David PHAM-VAN

Implement Shading objects

@@ -46,6 +46,7 @@ part 'src/font.dart'; @@ -46,6 +46,7 @@ part 'src/font.dart';
46 part 'src/font_descriptor.dart'; 46 part 'src/font_descriptor.dart';
47 part 'src/font_metrics.dart'; 47 part 'src/font_metrics.dart';
48 part 'src/formxobject.dart'; 48 part 'src/formxobject.dart';
  49 +part 'src/function.dart';
49 part 'src/graphic_state.dart'; 50 part 'src/graphic_state.dart';
50 part 'src/graphics.dart'; 51 part 'src/graphics.dart';
51 part 'src/image.dart'; 52 part 'src/image.dart';
@@ -60,6 +61,7 @@ part 'src/page_format.dart'; @@ -60,6 +61,7 @@ part 'src/page_format.dart';
60 part 'src/page_list.dart'; 61 part 'src/page_list.dart';
61 part 'src/point.dart'; 62 part 'src/point.dart';
62 part 'src/rect.dart'; 63 part 'src/rect.dart';
  64 +part 'src/shading.dart';
63 part 'src/signature.dart'; 65 part 'src/signature.dart';
64 part 'src/stream.dart'; 66 part 'src/stream.dart';
65 part 'src/ttf_parser.dart'; 67 part 'src/ttf_parser.dart';
  1 +/*
  2 + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +// ignore_for_file: omit_local_variable_types
  18 +
  19 +part of pdf;
  20 +
  21 +class PdfFunction extends PdfObjectStream {
  22 + PdfFunction(
  23 + PdfDocument pdfDocument, {
  24 + this.colors,
  25 + }) : super(pdfDocument);
  26 +
  27 + final List<PdfColor> colors;
  28 +
  29 + @override
  30 + void _prepare() {
  31 + for (final PdfColor color in colors) {
  32 + buf.putBytes(<int>[
  33 + (color.red * 255.0).round() & 0xff,
  34 + (color.green * 255.0).round() & 0xff,
  35 + (color.blue * 255.0).round() & 0xff,
  36 + ]);
  37 + }
  38 +
  39 + super._prepare();
  40 +
  41 + params['/FunctionType'] = const PdfNum(0);
  42 + params['/BitsPerSample'] = const PdfNum(8);
  43 + params['/Order'] = const PdfNum(3);
  44 + params['/Domain'] = PdfArray.fromNum(const <num>[0, 1]);
  45 + params['/Range'] = PdfArray.fromNum(const <num>[0, 1, 0, 1, 0, 1]);
  46 + params['/Size'] = PdfNum(colors.length);
  47 + }
  48 +}
@@ -94,6 +94,13 @@ class PdfGraphics { @@ -94,6 +94,13 @@ class PdfGraphics {
94 buf.putString('W n\n'); 94 buf.putString('W n\n');
95 } 95 }
96 96
  97 + /// Apply a shader
  98 + void applyShader(PdfShading shader) {
  99 + // The shader needs to be registered in the page resources
  100 + page.shading[shader.name] = shader;
  101 + buf.putString('${shader.name} sh\n');
  102 + }
  103 +
97 /// This releases any resources used by this Graphics object. You must use 104 /// This releases any resources used by this Graphics object. You must use
98 /// this method once finished with it. 105 /// this method once finished with it.
99 /// 106 ///
@@ -199,5 +199,5 @@ class PdfImage extends PdfXObject { @@ -199,5 +199,5 @@ class PdfImage extends PdfXObject {
199 final PdfImageOrientation orientation; 199 final PdfImageOrientation orientation;
200 200
201 /// Name of the image 201 /// Name of the image
202 - String get name => '/Image$objser'; 202 + String get name => '/I$objser';
203 } 203 }
@@ -59,6 +59,9 @@ class PdfPage extends PdfObject { @@ -59,6 +59,9 @@ class PdfPage extends PdfObject {
59 /// The fonts associated with this page 59 /// The fonts associated with this page
60 final Map<String, PdfFont> fonts = <String, PdfFont>{}; 60 final Map<String, PdfFont> fonts = <String, PdfFont>{};
61 61
  62 + /// The fonts associated with this page
  63 + final Map<String, PdfShading> shading = <String, PdfShading>{};
  64 +
62 /// The xobjects or other images in the pdf 65 /// The xobjects or other images in the pdf
63 final Map<String, PdfXObject> xObjects = <String, PdfXObject>{}; 66 final Map<String, PdfXObject> xObjects = <String, PdfXObject>{};
64 67
@@ -117,11 +120,23 @@ class PdfPage extends PdfObject { @@ -117,11 +120,23 @@ class PdfPage extends PdfObject {
117 /// This holds any resources for this page 120 /// This holds any resources for this page
118 final PdfDict resources = PdfDict(); 121 final PdfDict resources = PdfDict();
119 122
  123 + resources['/ProcSet'] = PdfArray(const <PdfName>[
  124 + PdfName('/PDF'),
  125 + PdfName('/Text'),
  126 + PdfName('/ImageB'),
  127 + PdfName('/ImageC'),
  128 + ]);
  129 +
120 // fonts 130 // fonts
121 if (fonts.isNotEmpty) { 131 if (fonts.isNotEmpty) {
122 resources['/Font'] = PdfDict.fromObjectMap(fonts); 132 resources['/Font'] = PdfDict.fromObjectMap(fonts);
123 } 133 }
124 134
  135 + // shading
  136 + if (shading.isNotEmpty) {
  137 + resources['/Shading'] = PdfDict.fromObjectMap(shading);
  138 + }
  139 +
125 // Now the XObjects 140 // Now the XObjects
126 if (xObjects.isNotEmpty) { 141 if (xObjects.isNotEmpty) {
127 resources['/XObject'] = PdfDict.fromObjectMap(xObjects); 142 resources['/XObject'] = PdfDict.fromObjectMap(xObjects);
  1 +/*
  2 + * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +
  17 +// ignore_for_file: omit_local_variable_types
  18 +
  19 +part of pdf;
  20 +
  21 +enum PdfShadingType { function, axial, radial }
  22 +
  23 +class PdfShading extends PdfObject {
  24 + PdfShading(
  25 + PdfDocument pdfDocument, {
  26 + @required this.shadingType,
  27 + @required this.function,
  28 + @required this.start,
  29 + @required this.end,
  30 + }) : assert(shadingType != null),
  31 + assert(function != null),
  32 + assert(start != null),
  33 + assert(end != null),
  34 + super(pdfDocument);
  35 +
  36 + /// Name of the Shading object
  37 + String get name => '/S$objser';
  38 +
  39 + final PdfShadingType shadingType;
  40 +
  41 + final PdfFunction function;
  42 +
  43 + final PdfPoint start;
  44 +
  45 + final PdfPoint end;
  46 +
  47 + @override
  48 + void _prepare() {
  49 + super._prepare();
  50 +
  51 + params['/ShadingType'] = PdfNum(shadingType.index + 1);
  52 + params['/AntiAlias'] = const PdfBool(true);
  53 + params['/ColorSpace'] = const PdfName('/DeviceRGB');
  54 + params['/Coords'] =
  55 + PdfArray.fromNum(<double>[start.x, start.y, end.x, end.y]);
  56 + params['/Domain'] = PdfArray.fromNum(<num>[0, 1]);
  57 + params['/Extend'] = PdfArray(const <PdfBool>[PdfBool(true), PdfBool(true)]);
  58 + params['/Function'] = function.ref();
  59 + }
  60 +}