Skip to content

Commit

Permalink
Extract WMF-specific logic from ImgWMF into
Browse files Browse the repository at this point in the history
  • Loading branch information
samruddhithakor committed Nov 27, 2024
1 parent 1b065aa commit 9834d9b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
30 changes: 8 additions & 22 deletions openpdf/src/main/java/com/lowagie/text/ImgWMF.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@

package com.lowagie.text;

import com.lowagie.text.error_messages.MessageLocalization;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.codec.wmf.InputMeta;
import com.lowagie.text.pdf.codec.wmf.MetaDo;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -122,9 +120,7 @@ public ImgWMF(byte[] img) throws BadElementException, IOException {
* @throws IOException
*/

private void processParameters() throws BadElementException, IOException {
type = IMGTEMPLATE;
originalType = ORIGINAL_WMF;
private void processParameters() throws BadElementException, IOException {
InputStream is = null;
try {
String errorID;
Expand All @@ -135,23 +131,13 @@ private void processParameters() throws BadElementException, IOException {
is = new java.io.ByteArrayInputStream(rawData);
errorID = "Byte array";
}
InputMeta in = new InputMeta(is);
if (in.readInt() != 0x9AC6CDD7) {
throw new BadElementException(
MessageLocalization.getComposedMessage("1.is.not.a.valid.placeable.windows.metafile", errorID));
}
in.readWord();
int left = in.readShort();
int top = in.readShort();
int right = in.readShort();
int bottom = in.readShort();
int inch = in.readWord();
dpiX = 72;
dpiY = 72;
scaledHeight = (float) (bottom - top) / inch * 72f;
setTop(scaledHeight);
scaledWidth = (float) (right - left) / inch * 72f;
setRight(scaledWidth);

WMFData data = WMFProcessor.processWMF(is, errorID);
this.scaledHeight = data.scaledHeight;
setTop(data.scaledHeight);
this.scaledWidth = data.scaledWidth;
setRight(data.scaledWidth);

} finally {
if (is != null) {
is.close();
Expand Down
15 changes: 15 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/WMFData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.lowagie.text;

public class WMFData {
public final float scaledWidth;
public final float scaledHeight;
public final float dpiX;
public final float dpiY;

public WMFData(float scaledWidth, float scaledHeight, float dpiX, float dpiY) {
this.scaledWidth = scaledWidth;
this.scaledHeight = scaledHeight;
this.dpiX = dpiX;
this.dpiY = dpiY;
}
}
29 changes: 29 additions & 0 deletions openpdf/src/main/java/com/lowagie/text/WMFProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.lowagie.text;

import com.lowagie.text.error_messages.MessageLocalization;
import com.lowagie.text.pdf.codec.wmf.InputMeta;
import java.io.IOException;
import java.io.InputStream;

public class WMFProcessor {
public static WMFData processWMF(InputStream is, String errorID) throws IOException, BadElementException {
InputMeta in = new InputMeta(is);
if (in.readInt() != 0x9AC6CDD7) {
throw new BadElementException(
MessageLocalization.getComposedMessage("1.is.not.a.valid.placeable.windows.metafile", errorID));
}
in.readWord();
int left = in.readShort();
int top = in.readShort();
int right = in.readShort();
int bottom = in.readShort();
int inch = in.readWord();

float dpiX = 72;
float dpiY = 72;
float scaledHeight = (float) (bottom - top) / inch * 72f;
float scaledWidth = (float) (right - left) / inch * 72f;

return new WMFData(scaledWidth, scaledHeight, dpiX, dpiY);
}
}

0 comments on commit 9834d9b

Please sign in to comment.