|
| 1 | +/* |
| 2 | + * Copyright 2016 Dhatim. |
| 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 | +package org.dhatim.fastexcel; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +/** |
| 21 | + * Represents an image embedded in a worksheet. |
| 22 | + */ |
| 23 | +public class Picture { |
| 24 | + |
| 25 | + private final int id; |
| 26 | + private final String name; |
| 27 | + private final PictureAnchor anchor; |
| 28 | + private final byte[] imageData; |
| 29 | + private final ImageType imageType; |
| 30 | + private final boolean lockAspectRatio; |
| 31 | + |
| 32 | + // Relationship ID (set when writing) |
| 33 | + private String relationshipId; |
| 34 | + |
| 35 | + Picture(int id, String name, PictureAnchor anchor, byte[] imageData, ImageType imageType, |
| 36 | + boolean lockAspectRatio) { |
| 37 | + this.id = id; |
| 38 | + this.name = name != null ? name : "Picture " + id; |
| 39 | + this.anchor = anchor; |
| 40 | + this.imageData = imageData; |
| 41 | + this.imageType = imageType; |
| 42 | + this.lockAspectRatio = lockAspectRatio; |
| 43 | + } |
| 44 | + |
| 45 | + void setRelationshipId(String relationshipId) { |
| 46 | + this.relationshipId = relationshipId; |
| 47 | + } |
| 48 | + |
| 49 | + String getRelationshipId() { |
| 50 | + return relationshipId; |
| 51 | + } |
| 52 | + |
| 53 | + int getId() { |
| 54 | + return id; |
| 55 | + } |
| 56 | + |
| 57 | + byte[] getImageData() { |
| 58 | + return imageData; |
| 59 | + } |
| 60 | + |
| 61 | + ImageType getImageType() { |
| 62 | + return imageType; |
| 63 | + } |
| 64 | + |
| 65 | + PictureAnchor getAnchor() { |
| 66 | + return anchor; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the name of this picture. |
| 71 | + * |
| 72 | + * @return Picture name |
| 73 | + */ |
| 74 | + public String getName() { |
| 75 | + return name; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Write the picture element to the drawing XML. |
| 80 | + */ |
| 81 | + void write(Writer w) throws IOException { |
| 82 | + if (anchor.isTwoCellAnchor()) { |
| 83 | + writeTwoCellAnchor(w); |
| 84 | + } else { |
| 85 | + writeOneCellAnchor(w); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private void writeOneCellAnchor(Writer w) throws IOException { |
| 90 | + w.append("<xdr:oneCellAnchor>"); |
| 91 | + anchor.writeFrom(w); |
| 92 | + anchor.writeExt(w); |
| 93 | + writePicElement(w); |
| 94 | + w.append("<xdr:clientData/>"); |
| 95 | + w.append("</xdr:oneCellAnchor>"); |
| 96 | + } |
| 97 | + |
| 98 | + private void writeTwoCellAnchor(Writer w) throws IOException { |
| 99 | + w.append("<xdr:twoCellAnchor>"); |
| 100 | + anchor.writeFrom(w); |
| 101 | + anchor.writeTo(w); |
| 102 | + writePicElement(w); |
| 103 | + w.append("<xdr:clientData/>"); |
| 104 | + w.append("</xdr:twoCellAnchor>"); |
| 105 | + } |
| 106 | + |
| 107 | + private void writePicElement(Writer w) throws IOException { |
| 108 | + w.append("<xdr:pic>"); |
| 109 | + |
| 110 | + // Non-visual properties |
| 111 | + w.append("<xdr:nvPicPr>"); |
| 112 | + w.append("<xdr:cNvPr id=\"").append(id).append("\" name=\""); |
| 113 | + w.appendEscaped(name); |
| 114 | + w.append("\"/>"); |
| 115 | + w.append("<xdr:cNvPicPr>"); |
| 116 | + if (lockAspectRatio) { |
| 117 | + w.append("<a:picLocks noChangeAspect=\"1\"/>"); |
| 118 | + } |
| 119 | + w.append("</xdr:cNvPicPr>"); |
| 120 | + w.append("</xdr:nvPicPr>"); |
| 121 | + |
| 122 | + // Blip fill (image reference) |
| 123 | + w.append("<xdr:blipFill>"); |
| 124 | + if (imageType == ImageType.SVG) { |
| 125 | + // SVG uses extension element with svgBlip (Office 2016+) |
| 126 | + w.append("<a:blip xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"); |
| 127 | + w.append("<a:extLst>"); |
| 128 | + w.append("<a:ext uri=\"{96DAC541-7B7A-43D3-8B79-37D633B846F1}\">"); |
| 129 | + w.append("<asvg:svgBlip xmlns:asvg=\"http://schemas.microsoft.com/office/drawing/2016/SVG/main\" "); |
| 130 | + w.append("r:embed=\"").append(relationshipId).append("\"/>"); |
| 131 | + w.append("</a:ext>"); |
| 132 | + w.append("</a:extLst>"); |
| 133 | + w.append("</a:blip>"); |
| 134 | + } else { |
| 135 | + // Raster images (PNG, JPEG, GIF) |
| 136 | + w.append("<a:blip xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" "); |
| 137 | + w.append("r:embed=\"").append(relationshipId).append("\"/>"); |
| 138 | + } |
| 139 | + w.append("<a:stretch><a:fillRect/></a:stretch>"); |
| 140 | + w.append("</xdr:blipFill>"); |
| 141 | + |
| 142 | + // Shape properties |
| 143 | + w.append("<xdr:spPr>"); |
| 144 | + w.append("<a:xfrm>"); |
| 145 | + w.append("<a:off x=\"0\" y=\"0\"/>"); |
| 146 | + if (anchor.isTwoCellAnchor()) { |
| 147 | + w.append("<a:ext cx=\"0\" cy=\"0\"/>"); |
| 148 | + } else { |
| 149 | + w.append("<a:ext cx=\"").append(anchor.getWidthEmu()).append("\" cy=\"") |
| 150 | + .append(anchor.getHeightEmu()).append("\"/>"); |
| 151 | + } |
| 152 | + w.append("</a:xfrm>"); |
| 153 | + w.append("<a:prstGeom prst=\"rect\"><a:avLst/></a:prstGeom>"); |
| 154 | + w.append("</xdr:spPr>"); |
| 155 | + |
| 156 | + w.append("</xdr:pic>"); |
| 157 | + } |
| 158 | +} |
0 commit comments