David PHAM-VAN

Add Opacity Widget

... ... @@ -9,6 +9,7 @@
- Fix HSV and HSL Color constructors
- Add PageTheme.copyWith
- Add more font drawing options
- Add Opacity Widget
## 1.4.1
... ...
... ... @@ -40,22 +40,23 @@ part 'src/colors.dart';
part 'src/compatibility.dart';
part 'src/document.dart';
part 'src/encryption.dart';
part 'src/font_descriptor.dart';
part 'src/exif.dart';
part 'src/font_metrics.dart';
part 'src/font.dart';
part 'src/font_descriptor.dart';
part 'src/font_metrics.dart';
part 'src/formxobject.dart';
part 'src/graphic_state.dart';
part 'src/graphics.dart';
part 'src/image.dart';
part 'src/info.dart';
part 'src/names.dart';
part 'src/object_stream.dart';
part 'src/object.dart';
part 'src/object_stream.dart';
part 'src/outline.dart';
part 'src/output.dart';
part 'src/page.dart';
part 'src/page_format.dart';
part 'src/page_list.dart';
part 'src/page.dart';
part 'src/point.dart';
part 'src/polygon.dart';
part 'src/rect.dart';
... ...
... ... @@ -98,6 +98,9 @@ class PdfDocument {
/// Object used to sign the document
PdfSignature sign;
/// Graphics state, representing only opacity.
PdfGraphicStates _graphicStates;
/// The PDF specification version
final String version = '1.7';
... ... @@ -149,6 +152,14 @@ class PdfDocument {
return _outline;
}
/// Graphic states for opacity and transfer modes
PdfGraphicStates get graphicStates {
_graphicStates ??= PdfGraphicStates(this);
return _graphicStates;
}
bool get hasGraphicStates => _graphicStates != null;
/// This writes the document to an OutputStream.
///
/// Note: You can call this as many times as you wish, as long as
... ...
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General License for more details.
*
* You should have received a copy of the GNU Lesser General
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// ignore_for_file: omit_local_variable_types
part of pdf;
@immutable
class PdfGraphicState {
const PdfGraphicState({this.opacity});
final double opacity;
@protected
PdfStream _output() {
final Map<String, PdfStream> params = <String, PdfStream>{};
if (opacity != null) {
params['/CA'] = PdfStream.num(opacity);
params['/ca'] = PdfStream.num(opacity);
}
return PdfStream.dictionary(params);
}
@override
bool operator ==(dynamic other) {
if (!other is PdfGraphicState) {
return false;
}
return other.opacity == opacity;
}
@override
int get hashCode => opacity.hashCode;
}
class PdfGraphicStates extends PdfObject {
PdfGraphicStates(PdfDocument pdfDocument) : super(pdfDocument);
final List<PdfGraphicState> _states = <PdfGraphicState>[];
static const String _prefix = '/a';
String stateName(PdfGraphicState state) {
int index = _states.indexOf(state);
if (index < 0) {
index = _states.length;
_states.add(state);
}
return '$_prefix$index';
}
@override
void _prepare() {
super._prepare();
for (int index = 0; index < _states.length; index++) {
params['$_prefix$index'] = _states[index]._output();
}
}
}
... ...
... ... @@ -320,6 +320,12 @@ class PdfGraphics {
}
}
/// Set the graphic state for drawing
void setGraphicState(PdfGraphicState state) {
final String name = page.pdfDocument.graphicStates.stateName(state);
buf.putString('$name gs\n');
}
/// Set the transformation Matrix
void setTransform(Matrix4 t) {
final Float64List s = t.storage;
... ...
... ... @@ -41,6 +41,18 @@ class PdfPage extends PdfObject {
/// -1 indicates no thumbnail.
PdfObject thumbnail;
/// Isolated transparency: If this flag is true, objects within the group
/// shall be composited against a fully transparent initial backdrop;
/// if false, they shall be composited against the group’s backdrop
bool isolatedTransparency = false;
/// Whether the transparency group is a knockout group.
/// If this flag is false, later objects within the group shall be composited
/// with earlier ones with which they overlap; if true, they shall be
/// composited with the group’s initial backdrop and shall overwrite any
/// earlier overlapping objects.
bool knockoutTransparency = false;
/// This holds any Annotations contained within this page.
List<PdfAnnot> annotations = <PdfAnnot>[];
... ... @@ -115,6 +127,20 @@ class PdfPage extends PdfObject {
resources['/XObject'] = PdfStream()..putObjectDictionary(xObjects);
}
if (pdfDocument.hasGraphicStates) {
// Declare Transparency Group settings
params['/Group'] = PdfStream()
..putDictionary(<String, PdfStream>{
'/Type': PdfStream.string('/Group'),
'/S': PdfStream.string('/Transparency'),
'/CS': PdfStream.string('/DeviceRGB'),
'/I': PdfStream()..putBool(isolatedTransparency),
'/K': PdfStream()..putBool(knockoutTransparency),
});
resources['/ExtGState'] = pdfDocument.graphicStates.ref();
}
params['/Resources'] = PdfStream.dictionary(resources);
// The thumbnail
... ...
... ... @@ -740,3 +740,29 @@ class FullPage extends SingleChildWidget {
context.canvas.restoreContext();
}
}
class Opacity extends SingleChildWidget {
Opacity({
@required this.opacity,
Widget child,
}) : assert(opacity != null),
super(child: child);
final double opacity;
@override
void paint(Context context) {
super.paint(context);
if (child != null) {
final Matrix4 mat = Matrix4.identity();
mat.translate(box.x, box.y);
context.canvas
..saveContext()
..setTransform(mat)
..setGraphicState(PdfGraphicState(opacity: opacity));
child.paint(context);
context.canvas.restoreContext();
}
}
}
... ...