createGraphics method
Initializes Graphics context of the layer.
Implementation
PdfGraphics createGraphics(PdfPage page) {
_helper.page = page;
if (_graphics == null) {
PdfPageHelper.getHelper(page)
.contents
.add(PdfReferenceHolder(_helper.layer));
}
final PdfResources? resource = PdfPageHelper.getHelper(page).getResources();
if (PdfLayerHelper.getHelper(_helper.layer!).layerId == null ||
PdfLayerHelper.getHelper(_helper.layer!).layerId!.isEmpty) {
PdfLayerHelper.getHelper(_helper.layer!).layerId =
'OCG_${PdfResources.globallyUniqueIdentifier}';
}
if (resource != null &&
resource.containsKey(PdfDictionaryProperties.properties)) {
final PdfDictionary? propertie =
resource[PdfDictionaryProperties.properties] as PdfDictionary?;
if (propertie != null) {
if (PdfLayerHelper.getHelper(_helper.layer!).layerId![0] == '/') {
PdfLayerHelper.getHelper(_helper.layer!).layerId =
PdfLayerHelper.getHelper(_helper.layer!).layerId!.substring(1);
}
propertie[PdfLayerHelper.getHelper(_helper.layer!).layerId] =
PdfLayerHelper.getHelper(_helper.layer!).referenceHolder;
} else {
resource.properties[PdfLayerHelper.getHelper(_helper.layer!).layerId] =
PdfLayerHelper.getHelper(_helper.layer!).referenceHolder;
resource[PdfDictionaryProperties.properties] = resource.properties;
}
} else {
resource!.properties[PdfLayerHelper.getHelper(_helper.layer!).layerId] =
PdfLayerHelper.getHelper(_helper.layer!).referenceHolder;
resource[PdfDictionaryProperties.properties] = resource.properties;
}
if (_graphics == null) {
final Function resources = PdfPageHelper.getHelper(page).getResources;
bool isPageHasMediaBox = false;
if (PdfPageHelper.getHelper(page)
.dictionary!
.containsKey(PdfName(PdfDictionaryProperties.mediaBox))) {
isPageHasMediaBox = true;
}
double llx = 0;
double lly = 0;
double urx = 0;
double ury = 0;
final PdfArray? mediaBox = PdfPageHelper.getHelper(page)
.dictionary!
.getValue(PdfDictionaryProperties.mediaBox,
PdfDictionaryProperties.parent) as PdfArray?;
if (mediaBox != null) {
// Lower Left X co-ordinate Value.
llx = (mediaBox[0]! as PdfNumber).value!.toDouble();
// Lower Left Y co-ordinate value.
lly = (mediaBox[1]! as PdfNumber).value!.toDouble();
// Upper right X co-ordinate value.
urx = (mediaBox[2]! as PdfNumber).value!.toDouble();
// Upper right Y co-ordinate value.
ury = (mediaBox[3]! as PdfNumber).value!.toDouble();
}
PdfArray? cropBox;
if (PdfPageHelper.getHelper(page)
.dictionary!
.containsKey(PdfDictionaryProperties.cropBox)) {
cropBox = PdfPageHelper.getHelper(page).dictionary!.getValue(
PdfDictionaryProperties.cropBox, PdfDictionaryProperties.parent)
as PdfArray?;
final double cropX = (cropBox![0]! as PdfNumber).value!.toDouble();
final double cropY = (cropBox[1]! as PdfNumber).value!.toDouble();
final double cropRX = (cropBox[2]! as PdfNumber).value!.toDouble();
final double cropRY = (cropBox[3]! as PdfNumber).value!.toDouble();
if ((cropX < 0 || cropY < 0 || cropRX < 0 || cropRY < 0) &&
(cropY.abs().floor() == page.size.height.abs().floor()) &&
(cropX.abs().floor()) == page.size.width.abs().floor()) {
final Size pageSize = Size(<double>[cropX, cropRX].reduce(max),
<double>[cropY, cropRY].reduce(max));
_graphics =
PdfGraphicsHelper.load(pageSize, resources, _helper._content!);
} else {
_graphics =
PdfGraphicsHelper.load(page.size, resources, _helper._content!);
PdfGraphicsHelper.getHelper(_graphics!).cropBox = cropBox;
}
} else if ((llx < 0 || lly < 0 || urx < 0 || ury < 0) &&
(lly.abs().floor() == page.size.height.abs().floor()) &&
(urx.abs().floor() == page.size.width.abs().floor())) {
final Size pageSize = Size(
<double>[llx, urx].reduce(max), <double>[lly, ury].reduce(max));
if (pageSize.width <= 0 || pageSize.height <= 0) {
_graphics =
PdfGraphicsHelper.load(pageSize, resources, _helper._content!);
}
} else {
_graphics =
PdfGraphicsHelper.load(page.size, resources, _helper._content!);
}
if (isPageHasMediaBox) {
PdfGraphicsHelper.getHelper(_graphics!).mediaBoxUpperRightBound = ury;
}
if (!PdfPageHelper.getHelper(page).isLoadedPage) {
final PdfSectionCollection? sectionCollection =
PdfSectionHelper.getHelper(PdfPageHelper.getHelper(page).section!)
.parent;
if (sectionCollection != null) {
_graphics!.colorSpace =
PdfSectionCollectionHelper.getHelper(sectionCollection)
.document!
.colorSpace;
}
}
if (!_graphicsMap.containsKey(_graphics)) {
_graphicsMap[_graphics!] = _graphics!;
}
if (!_pageGraphics.containsKey(page)) {
_pageGraphics[page] = _graphics!;
}
_helper._content!.beginSave = _beginSaveContent;
} else {
if (!_helper.pages.contains(page)) {
_graphicsContent(page);
} else if (_pageGraphics.containsKey(page)) {
_graphics = _pageGraphics[page];
return _graphics!;
}
}
PdfGraphicsHelper.getHelper(_graphics!)
.streamWriter!
.write(PdfOperators.newLine);
_graphics!.save();
PdfGraphicsHelper.getHelper(_graphics!).initializeCoordinates();
if (PdfGraphicsHelper.getHelper(_graphics!).hasTransparencyBrush) {
PdfGraphicsHelper.getHelper(_graphics!).setTransparencyGroup(page);
}
if (PdfPageHelper.getHelper(page).isLoadedPage &&
(page.rotation != PdfPageRotateAngle.rotateAngle0 ||
PdfPageHelper.getHelper(page)
.dictionary!
.containsKey(PdfDictionaryProperties.rotate))) {
PdfArray? cropBox;
if (PdfPageHelper.getHelper(page)
.dictionary!
.containsKey(PdfDictionaryProperties.cropBox)) {
cropBox = PdfPageHelper.getHelper(page).dictionary!.getValue(
PdfDictionaryProperties.cropBox, PdfDictionaryProperties.parent)
as PdfArray?;
}
_updatePageRotation(page, _graphics, cropBox);
}
if (!PdfPageHelper.getHelper(page).isLoadedPage) {
final PdfRectangle clipRect =
PdfSectionHelper.getHelper(PdfPageHelper.getHelper(page).section!)
.getActualBounds(page, true);
if (_clipPageTemplates) {
if (PdfPageHelper.getHelper(page).origin.dx >= 0 &&
PdfPageHelper.getHelper(page).origin.dy >= 0) {
PdfGraphicsHelper.getHelper(_graphics!)
.clipTranslateMarginsWithBounds(clipRect);
}
} else {
final PdfMargins margins =
PdfPageHelper.getHelper(page).section!.pageSettings.margins;
PdfGraphicsHelper.getHelper(_graphics!).clipTranslateMargins(
clipRect.x,
clipRect.y,
margins.left,
margins.top,
margins.right,
margins.bottom);
}
}
if (!_helper.pages.contains(page)) {
_helper.pages.add(page);
}
PdfGraphicsHelper.getHelper(_graphics!).setLayer(null, this);
return _graphics!;
}