Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work for #268: fix imageWidth, logoWidth '100%' behaviour #273

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions src/flat_layout/flat_image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,8 @@ export class FlatImage extends FlatQuestion {
super(survey, question, controller);
this.question = <QuestionImageModel>question;
}
private isSizeEmpty(val: any) {
return !val || val === 'auto';
}
private async getCorrectImageSize(): Promise<ISize> {
let widthPt: number = SurveyHelper.pxToPt(<any>this.question.imageWidth);
let heightPt: number = SurveyHelper.pxToPt(<any>this.question.imageHeight);
if(this.isSizeEmpty(this.question.imageWidth) || this.isSizeEmpty(this.question.imageHeight)) {
const imageSize = await SurveyHelper.getImageSize(this.question.imageLink);
if(!this.isSizeEmpty(this.question.imageWidth)) {
if(imageSize && imageSize.width) {
heightPt = imageSize.height * widthPt / imageSize.width;
} else {
heightPt = 0;
}
}
if(!this.isSizeEmpty(this.question.imageHeight)) {
if(imageSize && imageSize.height) {
widthPt = imageSize.width * heightPt / imageSize.height;
} else {
widthPt = 0;
}
}
}
return { width: widthPt, height: heightPt };
return await SurveyHelper.getCorrectedImageSize(this.controller, { imageWidth: this.question.imageWidth, imageHeight: this.question.imageHeight, imageLink: this.question.imageLink });
}
public async generateFlatsContent(point: IPoint): Promise<IPdfBrick[]> {
const imageSize = await this.getCorrectImageSize();
Expand Down
6 changes: 4 additions & 2 deletions src/flat_layout/flat_survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ export class FlatSurvey {
}
private static async generateFlatLogoImage(survey: SurveyPDF, controller: DocController,
point: IPoint): Promise<IPdfBrick> {
const logoUrl = SurveyHelper.getLocString(survey.locLogo);
const logoSize = await SurveyHelper.getCorrectedImageSize(controller, { imageLink: logoUrl, imageHeight: survey.logoHeight, imageWidth: survey.logoWidth });
const logoFlat: IPdfBrick = await SurveyHelper.createImageFlat(
point, null, controller, SurveyHelper.getLocString(survey.locLogo),
SurveyHelper.pxToPt(survey.logoWidth), SurveyHelper.pxToPt(survey.logoHeight));
point, null, controller, logoUrl,
logoSize.width, logoSize.height);
let shift: number = 0;
if (survey.logoPosition === 'right') {
shift = SurveyHelper.getPageAvailableWidth(controller) - logoFlat.width;
Expand Down
35 changes: 33 additions & 2 deletions src/helper_survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ export class SurveyHelper {
public static CUSTOM_FONT_ENCODING: string = 'Identity-H';

public static parseWidth(width: string, maxWidth: number,
columnsCount: number = 1): number {
columnsCount: number = 1, defaultUnit?: string): number {
if (width.indexOf('calc') === 0) {
return maxWidth / columnsCount;
}
const value: number = parseFloat(width);
const unit: string = width.replace(/[^A-Za-z]/g, '');
const unit: string = width.replace(/[^A-Za-z%]/g, '') || defaultUnit;
let k: number;
switch (unit) {
case 'pt':
Expand Down Expand Up @@ -722,4 +722,35 @@ export class SurveyHelper {
return ((!!question && question.isReadOnly || readOnly) && SurveyHelper.getReadonlyRenderAs(
<Question>question, controller) !== 'acroform') || controller?.compress;
}
public static isSizeEmpty(val: any): boolean {
return !val || val === 'auto';
}
public static isHeightEmpty(val: any): boolean {
return this.isSizeEmpty(val) || val == '100%';
}
public static async getCorrectedImageSize(controller: DocController, imageOptions: { imageWidth: any, imageHeight: any, imageLink: string }): Promise<ISize> {
let { imageWidth, imageLink, imageHeight } = imageOptions;
imageWidth = typeof imageWidth === 'number' ? imageWidth.toString() : imageWidth;
imageHeight = typeof imageHeight === 'number' ? imageHeight.toString() : imageHeight;
let widthPt: number = imageWidth && SurveyHelper.parseWidth(imageWidth, SurveyHelper.getPageAvailableWidth(controller), 1, 'px');
let heightPt: number = imageHeight && SurveyHelper.parseWidth(imageHeight, SurveyHelper.getPageAvailableWidth(controller), 1, 'px');
if(SurveyHelper.isSizeEmpty(imageWidth) || SurveyHelper.isHeightEmpty(imageHeight)) {
const imageSize = await SurveyHelper.getImageSize(imageLink);
if(!SurveyHelper.isSizeEmpty(imageWidth)) {
if(imageSize && imageSize.width) {
heightPt = imageSize.height * widthPt / imageSize.width;
} else {
heightPt = 0;
}
}
if(!SurveyHelper.isHeightEmpty(imageHeight)) {
if(imageSize && imageSize.height) {
widthPt = imageSize.width * heightPt / imageSize.height;
} else {
widthPt = 0;
}
}
}
return { width: widthPt, height: heightPt };
}
}
37 changes: 37 additions & 0 deletions tests/flat_image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,43 @@ test('Check image question with "auto"', async () => {
SurveyHelper.getImageSize = getOldImageSize;
});

test('Check image question with "auto" and 100%', async () => {
const getOldImageSize = SurveyHelper.getImageSize;
SurveyHelper.getImageSize = async () => {
return { width: 100, height: 75 };
};
SurveyHelper.shouldConvertImageToPng = false;
const json: any = {
elements: [
{
type: 'image',
name: 'image_question',
titleLocation: 'hidden',
imageHeight: 'auto',
imageWidth: '100%',
imageLink: TestHelper.BASE64_IMAGE_100PX
}
]
};
const survey: SurveyPDF = new SurveyPDF(json, TestHelper.defaultOptions);
const controller: DocController = new DocController(TestHelper.defaultOptions);
let flats: IPdfBrick[][] = await FlatSurvey.generateFlats(survey, controller);
expect(flats.length).toBe(1);
expect(flats[0].length).toBe(1);
controller.margins.left += controller.unitWidth;
let width: string = (<any>survey.getAllQuestions()[0]).imageWidth;
let widthPt: number = SurveyHelper.parseWidth('100%', SurveyHelper.getPageAvailableWidth(controller));
let heightPt: number = 75 * widthPt / 100;
let assumeImage: IRect = {
xLeft: controller.leftTopPoint.xLeft,
xRight: controller.leftTopPoint.xLeft + widthPt,
yTop: controller.leftTopPoint.yTop,
yBot: controller.leftTopPoint.yTop + heightPt
};
TestHelper.equalRect(expect, flats[0][0], assumeImage);
SurveyHelper.getImageSize = getOldImageSize;
});

test('Check image question 100x100px with set size server-side', async () => {
SurveyHelper.inBrowser = false;
const json: any = {
Expand Down