Skip to content

Commit 542eb36

Browse files
Merge pull request #3485 from NativeScript/vladimirov/fix-assets
fix(assets): Fix asset genration
2 parents c3661c6 + 920cb8e commit 542eb36

File tree

6 files changed

+176
-109
lines changed

6 files changed

+176
-109
lines changed

lib/common

lib/constants.ts

+3
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,7 @@ export class AssetConstants {
163163
public static assets = "assets";
164164

165165
public static sizeDelimiter = "x";
166+
167+
public static defaultScale = 1;
168+
public static defaultOverlayImageScale = 0.8;
166169
}

lib/definitions/project.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@ interface IAssetItem {
157157
height: number;
158158
filename: string;
159159
directory: string;
160-
scale: number;
160+
scale: string;
161161
idiom: string;
162162
resizeOperation?: string;
163+
overlayImageScale?: number;
163164
}
164165

165166
interface IAssetSubGroup {

lib/services/assets-generation/assets-generation-service.ts

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Jimp from "jimp";
22
import * as Color from "color";
33
import { exported } from "../../common/decorators";
4+
import { AssetConstants } from '../../constants';
45

56
export const enum Operations {
67
OverlayWith = "overlayWith",
@@ -39,12 +40,12 @@ export class AssetsGenerationService implements IAssetsGenerationService {
3940
const assetsStructure = await this.$projectDataService.getAssetsStructure(generationData);
4041

4142
const assetItems = _(assetsStructure)
42-
.filter((assetGroup: IAssetGroup, platform: string) => {
43-
return !generationData.platform || platform.toLowerCase() === generationData.platform.toLowerCase();
44-
})
43+
.filter((assetGroup: IAssetGroup, platform: string) =>
44+
!generationData.platform || platform.toLowerCase() === generationData.platform.toLowerCase()
45+
)
4546
.map((assetGroup: IAssetGroup) =>
4647
_.filter(assetGroup, (assetSubGroup: IAssetSubGroup, imageTypeKey: string) =>
47-
propertiesToEnumerate.indexOf(imageTypeKey) !== -1 && !assetSubGroup[imageTypeKey]
48+
assetSubGroup && propertiesToEnumerate.indexOf(imageTypeKey) !== -1
4849
)
4950
)
5051
.flatten<IAssetSubGroup>()
@@ -55,20 +56,34 @@ export class AssetsGenerationService implements IAssetsGenerationService {
5556

5657
for (const assetItem of assetItems) {
5758
const operation = assetItem.resizeOperation || Operations.Resize;
58-
const scale = assetItem.scale || 0.8;
59+
let tempScale: number = null;
60+
if (assetItem.scale) {
61+
if (_.isNumber(assetItem.scale)) {
62+
tempScale = assetItem.scale;
63+
} else {
64+
const splittedElements = `${assetItem.scale}`.split(AssetConstants.sizeDelimiter);
65+
tempScale = splittedElements && splittedElements.length && splittedElements[0] && +splittedElements[0];
66+
}
67+
}
68+
69+
const scale = tempScale || AssetConstants.defaultScale;
70+
5971
const outputPath = assetItem.path;
72+
const width = assetItem.width * scale;
73+
const height = assetItem.height * scale;
6074

6175
switch (operation) {
6276
case Operations.OverlayWith:
63-
const imageResize = Math.round(Math.min(assetItem.width, assetItem.height) * scale);
77+
const overlayImageScale = assetItem.overlayImageScale || AssetConstants.defaultOverlayImageScale;
78+
const imageResize = Math.round(Math.min(width, height) * overlayImageScale);
6479
const image = await this.resize(generationData.imagePath, imageResize, imageResize);
65-
await this.generateImage(generationData.background, assetItem.width, assetItem.height, outputPath, image);
80+
await this.generateImage(generationData.background, width, height, outputPath, image);
6681
break;
6782
case Operations.Blank:
68-
await this.generateImage(generationData.background, assetItem.width, assetItem.height, outputPath);
83+
await this.generateImage(generationData.background, width, height, outputPath);
6984
break;
7085
case Operations.Resize:
71-
const resizedImage = await this.resize(generationData.imagePath, assetItem.width, assetItem.height);
86+
const resizedImage = await this.resize(generationData.imagePath, width, height);
7287
resizedImage.write(outputPath);
7388
break;
7489
default:

lib/services/project-data-service.ts

+28-18
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,35 @@ export class ProjectDataService implements IProjectDataService {
137137
image.path = path.join(dirPath, image.filename);
138138
}
139139

140-
if (image.size) {
141-
// size is basically <width>x<height>
142-
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
143-
if (width && height) {
144-
image.width = +width;
145-
image.height = +height;
146-
}
147-
} else {
148-
// Find the image size based on the hardcoded values in the image-definitions.json
149-
_.each(imageDefinitions, (assetSubGroup: IAssetItem[]) => {
150-
_.each(assetSubGroup, assetItem => {
151-
if (assetItem.filename === image.filename && path.basename(assetItem.directory) === path.basename(dirPath)) {
152-
image.width = assetItem.width;
153-
image.height = assetItem.height;
154-
image.size = `${assetItem.width}${AssetConstants.sizeDelimiter}${assetItem.height}`;
140+
// Find the image size based on the hardcoded values in the image-definitions.json
141+
_.each(imageDefinitions, (assetSubGroup: IAssetItem[]) => {
142+
const assetItem = _.find(assetSubGroup, assetElement =>
143+
assetElement.filename === image.filename && path.basename(assetElement.directory) === path.basename(dirPath)
144+
);
145+
146+
if (assetItem) {
147+
if (image.size) {
148+
// size is basically <width>x<height>
149+
const [width, height] = image.size.toString().split(AssetConstants.sizeDelimiter);
150+
if (width && height) {
151+
image.width = +width;
152+
image.height = +height;
155153
}
156-
});
157-
});
158-
}
154+
}
155+
156+
if (!image.width || !image.height) {
157+
image.width = assetItem.width;
158+
image.height = assetItem.height;
159+
image.size = image.size || `${assetItem.width}${AssetConstants.sizeDelimiter}${assetItem.height}`;
160+
}
161+
162+
image.resizeOperation = image.resizeOperation || assetItem.resizeOperation;
163+
image.overlayImageScale = image.overlayImageScale || assetItem.overlayImageScale;
164+
image.scale = image.scale || assetItem.scale;
165+
// break each
166+
return false;
167+
}
168+
});
159169
});
160170

161171
return content;

0 commit comments

Comments
 (0)