Skip to content

Commit

Permalink
Image scaling, to fit in a specified max width
Browse files Browse the repository at this point in the history
  • Loading branch information
plutext committed Mar 14, 2016
1 parent 279b5bc commit 81fecde
Showing 1 changed file with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,34 @@ public Inline createImageInline(String filenameHint, String altText,
return createImageInline(filenameHint, altText,
id1, id2, cxcy.getCx(), cxcy.getCy(), link);
}

/**
* @param filenameHint
* @param altText
* @param id1
* @param id2
* @param link
* @param maxWidth
* @return
* @throws Exception
* @since 3.3.0
*/
public Inline createImageInline(String filenameHint, String altText,
int id1, int id2, boolean link, int maxWidth) throws Exception {

// This signature can scale the image to specified maxWidth

WordprocessingMLPackage wmlPackage = ((WordprocessingMLPackage) this.getPackage());

List<SectionWrapper> sections = wmlPackage.getDocumentModel().getSections();
PageDimensions page = sections.get(sections.size() - 1).getPageDimensions();

CxCy cxcy = CxCy.scale(imageInfo, page, maxWidth);

return createImageInline(filenameHint, altText,
id1, id2, cxcy.getCx(), cxcy.getCy(), link);
}


/**
* Create a <wp:inline> element suitable for this image,
Expand Down Expand Up @@ -1160,12 +1188,31 @@ public boolean isScaled() {
this.scaled = scaled;

}

public static CxCy scale(ImageInfo imageInfo, PageDimensions page) {
return scale( imageInfo, page, -1);
}

/**
* Return scaling values constrained by specified maxWidth.
* Useful if the image is to be inserted into a table cell of known width.
*
* @param imageInfo
* @param page
* @param maxWidth
* @return
* @since 3.3.0
*/
public static CxCy scale(ImageInfo imageInfo, PageDimensions page, int maxWidth) {

double writableWidthTwips = page.getWritableWidthTwips();
log.debug("writableWidthTwips: " + writableWidthTwips);

if (maxWidth>0 && maxWidth<writableWidthTwips) {
writableWidthTwips = maxWidth;
log.debug("reduced to: " + writableWidthTwips);
}

ImageSize size = imageInfo.getSize();

Dimension2D dPt = size.getDimensionPt();
Expand Down

0 comments on commit 81fecde

Please sign in to comment.