Fix: export path with holes to PDF #269

This commit is contained in:
cuixiping
2020-01-15 16:13:14 +08:00
parent 980aa80169
commit f9cc6821c7

View File

@@ -38,7 +38,7 @@ const pdfSvgAttr = {
const attributeIsNotEmpty = function (node, attr) { const attributeIsNotEmpty = function (node, attr) {
const attVal = attr ? node.getAttribute(attr) : node; const attVal = attr ? node.getAttribute(attr) : node;
return attVal !== '' && attVal !== null; return attVal !== '' && attVal !== null && attVal !== 'null';
}; };
const nodeIs = function (node, possible) { const nodeIs = function (node, possible) {
@@ -176,9 +176,10 @@ const svgElementToPdf = function (element, pdf, options) {
// let hasStrokeColor = false; // let hasStrokeColor = false;
let hasFillColor = false; let hasFillColor = false;
let fillRGB; let fillRGB;
colorMode = null;
if (nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle', 'polygon', 'polyline', 'path', 'text'])) { if (nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle', 'polygon', 'polyline', 'path', 'text'])) {
const fillColor = node.getAttribute('fill'); const fillColor = node.getAttribute('fill');
if (attributeIsNotEmpty(fillColor)) { if (attributeIsNotEmpty(fillColor) && node.getAttribute('fill-opacity') !== '0') {
fillRGB = new RGBColor(fillColor); fillRGB = new RGBColor(fillColor);
if (fillRGB.ok) { if (fillRGB.ok) {
hasFillColor = true; hasFillColor = true;
@@ -196,12 +197,12 @@ const svgElementToPdf = function (element, pdf, options) {
pdf.setLineWidth(k * parseInt(node.getAttribute('stroke-width'))); pdf.setLineWidth(k * parseInt(node.getAttribute('stroke-width')));
} }
const strokeColor = node.getAttribute('stroke'); const strokeColor = node.getAttribute('stroke');
if (attributeIsNotEmpty(strokeColor)) { if (attributeIsNotEmpty(strokeColor) && node.getAttribute('stroke-width') !== '0' && node.getAttribute('stroke-opacity') !== '0') {
const strokeRGB = new RGBColor(strokeColor); const strokeRGB = new RGBColor(strokeColor);
if (strokeRGB.ok) { if (strokeRGB.ok) {
// hasStrokeColor = true; // hasStrokeColor = true;
pdf.setDrawColor(strokeRGB.r, strokeRGB.g, strokeRGB.b); pdf.setDrawColor(strokeRGB.r, strokeRGB.g, strokeRGB.b);
if (colorMode === 'F') { if (hasFillColor) {
colorMode = 'FD'; colorMode = 'FD';
} else { } else {
colorMode = 'S'; colorMode = 'S';
@@ -273,17 +274,33 @@ const svgElementToPdf = function (element, pdf, options) {
removeAttributes(node, pdfSvgAttr.polygon); removeAttributes(node, pdfSvgAttr.polygon);
break; break;
} case 'path': { } case 'path': {
const linesOptionsList = getLinesOptionsOfPath(node); if (colorMode) {
linesOptionsList.forEach(function (linesOptions) { const linesOptionsList = getLinesOptionsOfPath(node);
pdf.lines( if (linesOptionsList.length > 0) {
linesOptions.lines, linesOptionsList.forEach(function (linesOptions) {
k * linesOptions.x, pdf.lines(
k * linesOptions.y, linesOptions.lines,
[k, k], k * linesOptions.x,
colorMode, k * linesOptions.y,
linesOptions.closed [k, k],
); null,
}); linesOptions.closed
);
});
// svg fill rule default is nonzero
const fillRule = node.getAttribute('fill-rule');
if (fillRule === 'evenodd') {
// f* : fill using even-odd rule
// B* : stroke and fill using even-odd rule
if (colorMode === 'F') {
colorMode = 'f*';
} else if (colorMode === 'FD') {
colorMode = 'B*';
}
}
pdf.internal.write(pdf.internal.getStyle(colorMode));
}
}
removeAttributes(node, pdfSvgAttr.path); removeAttributes(node, pdfSvgAttr.path);
break; break;
} case 'text': { } case 'text': {