Skip to content

Commit 5f7b1b9

Browse files
committed
docs: improve comments
1 parent c901efb commit 5f7b1b9

File tree

4 files changed

+63
-60
lines changed

4 files changed

+63
-60
lines changed

src/tests/fileWriter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fileWriter from '../extensions/fileWriter';
66

77
describe('fileWriter extension', () => {
88
afterEach(async () => {
9-
// remove all pngs created by the fileWriter
9+
// Remove all pngs created by the fileWriter
1010
const pngs = glob.sync(path.join(process.cwd(), '*.png'));
1111
await Promise.all(pngs.map((item) => fs.promises.unlink(item)));
1212
});

src/tests/helpers/takeSnapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// For development purposes only.
22
// We can't use the snapshots in the CI environment since they
3-
// will differ ever so slighly from the development env
3+
// will differ ever so slightly from the development env
44
// and fail our tests. So we use the snapshots only as
55
// regression detectors when developing.
66
export default function takeSnapshot(data: string) {

src/tests/textToImage.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ describe('the text-to-image generator', () => {
201201
const boldMap = extractColors(boldImg);
202202
const normalMap = extractColors(normalImg);
203203

204-
// check that we have more black and less white in the bold text image
204+
// Check that we have more black and less white in the bold text image
205205
expect(boldMap['#000000']).toBeGreaterThan(normalMap['#000000']);
206206
expect(boldMap['#ffffff']).toBeLessThan(normalMap['#ffffff']);
207207

@@ -216,7 +216,7 @@ describe('the text-to-image generator', () => {
216216

217217
const rightAlignData = await readImageData(uriToBuf(uri));
218218

219-
// expect all pixels on left side (up to 150px) to be white
219+
// Expect all pixels on left side (up to 150px) to be white
220220
const whitePixels = countWhitePixels(
221221
rightAlignData,
222222
0,
@@ -226,7 +226,7 @@ describe('the text-to-image generator', () => {
226226
);
227227
expect(whitePixels).toBe(rightAlignData.height * 150);
228228

229-
// expect some pixels on right side (from 150px) include non-white
229+
// Expect some pixels on right side (from 150px) include non-white
230230
const nonWhitePixels = countWhitePixels(
231231
rightAlignData,
232232
150,
@@ -246,7 +246,7 @@ describe('the text-to-image generator', () => {
246246

247247
const leftAlignData = await readImageData(uriToBuf(uri));
248248

249-
// expect all pixels on right side (from 250px) to be white
249+
// Expect all pixels on right side (from 250px) to be white
250250
const whitePixels = countWhitePixels(
251251
leftAlignData,
252252
250,
@@ -256,7 +256,7 @@ describe('the text-to-image generator', () => {
256256
);
257257
expect(whitePixels).toBe(leftAlignData.height * 150);
258258

259-
// expect some pixels on left side (up to 250px) to be non-white
259+
// Expect some pixels on left side (up to 250px) to be non-white
260260
const nonWhitePixels = countWhitePixels(
261261
leftAlignData,
262262
0,
@@ -276,7 +276,7 @@ describe('the text-to-image generator', () => {
276276

277277
const centerAlignData = await readImageData(uriToBuf(uri));
278278

279-
// expect all pixels on left side (up to 80px) to be white
279+
// Expect all pixels on left side (up to 80px) to be white
280280
const leftWhitePixels = countWhitePixels(
281281
centerAlignData,
282282
0,
@@ -286,7 +286,7 @@ describe('the text-to-image generator', () => {
286286
);
287287
expect(leftWhitePixels).toBe(centerAlignData.height * 80);
288288

289-
// expect all pixels on left side (last 80px) to be white
289+
// Expect all pixels on right side (last 80px) to be white
290290
const rightWhitePixels = countWhitePixels(
291291
centerAlignData,
292292
centerAlignData.width - 80,
@@ -296,7 +296,7 @@ describe('the text-to-image generator', () => {
296296
);
297297
expect(rightWhitePixels).toBe(centerAlignData.height * 80);
298298

299-
// expect some pixels in the center (between 80 and width-80) to be non-white
299+
// Expect some pixels in the center (between 80 and width-80) to be non-white
300300
const centerWhitePixels = countWhitePixels(
301301
centerAlignData,
302302
80,
@@ -348,7 +348,7 @@ describe('the text-to-image generator', () => {
348348

349349
const verticalCenter = await readImageData(uriToBuf(uri));
350350

351-
// first 35 pixel rows should be white
351+
// First 35 pixel rows should be white
352352
const topWhitePixels = countWhitePixels(
353353
verticalCenter,
354354
0,
@@ -358,7 +358,7 @@ describe('the text-to-image generator', () => {
358358
);
359359
expect(topWhitePixels).toBe(verticalCenter.width * 35);
360360

361-
// middle pixel rows should contain non-whites
361+
// Middle pixel rows should contain non-whites
362362
const centerWhitePixels = countWhitePixels(
363363
verticalCenter,
364364
0,
@@ -370,7 +370,7 @@ describe('the text-to-image generator', () => {
370370
verticalCenter.width * (verticalCenter.height - 70),
371371
);
372372

373-
// bottom 35 rows should be white
373+
// Bottom 35 rows should be white
374374
const bottomWhitePixels = countWhitePixels(
375375
verticalCenter,
376376
0,
@@ -385,14 +385,14 @@ describe('the text-to-image generator', () => {
385385

386386
it('should support custom font paths', async () => {
387387
const uri = await generate('S', {
388-
// use a font that renders a black square with the 'S' character
388+
// Use a font that renders a black square with the 'S' character
389389
fontPath: path.resolve(__dirname, 'helpers', 'heydings_controls.ttf'),
390390
fontFamily: 'Heydings Controls',
391391
margin: 0,
392392
});
393393

394394
const customFontData = await readImageData(uriToBuf(uri));
395-
// check that we only have black pixels in the rendered square
395+
// Check that we only have black pixels in the rendered square
396396
const whitePixels = countWhitePixels(customFontData, 5, 9, 13, 17);
397397
expect(whitePixels).toBe(0);
398398

@@ -414,7 +414,7 @@ describe('the text-to-image generator', () => {
414414
);
415415

416416
const imageData = await readImageData(uriToBuf(uri));
417-
// check that we only have only white pixels in the top left corner
417+
// Check that we only have only white pixels in the top left corner
418418
const whitePixels1 = countWhitePixels(imageData, 0, 0, 35, 35);
419419
expect(whitePixels1).toBe(35 * 35);
420420

@@ -430,7 +430,7 @@ describe('the text-to-image generator', () => {
430430
);
431431

432432
const imageData = await readImageData(uriToBuf(uri));
433-
// check that we only have only white pixels in the top left corner
433+
// Check that we only have only white pixels in the top left corner
434434
const whitePixels1 = countWhitePixels(imageData, 0, 0, 20, 30);
435435
expect(whitePixels1).toBe(20 * 30);
436436

@@ -482,7 +482,7 @@ describe('the text-to-image generator', () => {
482482
const {
483483
frames: [{ data }],
484484
} = await readImageData(uriToBuf(uri));
485-
// the top 100 pixel rows should not have any data
485+
// The top 100 pixel rows should not have any data
486486
const topRowsData = data.slice(0, 400 * 100 * 4); // 400px wide, 100px high, 4 values per pixel
487487
const rgbaSum = topRowsData.reduce((acc, cur) => acc + cur, 0);
488488

src/textToImage.ts

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ const createTextData = (
4646
}
4747

4848
// Use the supplied canvas (which should have a suitable width and height)
49-
// for the final image
50-
// OR
51-
// create a temporary canvas just for measuring how long the canvas needs to be
49+
// for the final image.
50+
// Or create a temporary canvas just for measuring how long the canvas
51+
// needs to be.
5252
const textCanvas = canvas ?? createCanvas(maxWidth, 100);
5353
const textContext = textCanvas.getContext('2d');
5454

55-
// set the text alignment and start position
55+
// Set the text alignment and start position
5656
let textX = 0;
5757
let textY = 0;
5858

@@ -64,20 +64,20 @@ const createTextData = (
6464
}
6565
textContext.textAlign = textAlign;
6666

67-
// set background color
67+
// Set background color
6868
textContext.fillStyle = bgColor;
6969
textContext.fillRect(0, 0, textCanvas.width, textCanvas.height);
7070

71-
// set text styles
71+
// Set text styles
7272
textContext.fillStyle = textColor;
7373
textContext.font = `${fontWeight.toString()} ${fontSize.toString()}px ${fontFamily}`;
7474
textContext.textBaseline = 'top';
7575

76-
// split the text into words
76+
// Split the text into words
7777
const words = text.split(' ');
7878
let wordCount = words.length;
7979

80-
// the start of the first line
80+
// The start of the first line
8181
let line = '';
8282
const addNewLines = [];
8383

@@ -86,50 +86,49 @@ const createTextData = (
8686

8787
if (words[n].includes('\n')) {
8888
const parts = words[n].split('\n');
89-
// use the first word before the newline(s)
89+
// Use the first word before the newline(s)
9090
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
9191
word = parts.shift() || '';
92-
// mark the next word as beginning with newline
92+
// Mark the next word as beginning with newline
9393
addNewLines.push(n + 1);
94-
// return the rest of the parts to the words array at the same index
94+
// Return the rest of the parts to the words array at the same index
9595
words.splice(n + 1, 0, parts.join('\n'));
9696
wordCount += 1;
9797
}
9898

99-
// append one word to the line and see
100-
// if its width exceeds the maxWidth
101-
// also trim the testLine since `line` will be empty in the beginning,
102-
// causing a leading white space character otherwise.
99+
// Append one word to the line and see if its width exceeds the maxWidth.
100+
// Also trim the testLine since `line` will be empty in the
101+
// beginning, causing a leading white space character otherwise.
103102
// Use a negative lookbehind in the regex due to
104-
// https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS
103+
// https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS.
105104
const testLine = `${line} ${word}`.replace(/^ +|(?<! ) +$/g, '');
106105
const testLineWidth = textContext.measureText(testLine).width;
107106

108-
// if the line is marked as starting with a newline
109-
// OR if the line is too long, add a newline
107+
// If the line is marked as starting with a newline or if the line is
108+
// too long, add a newline
110109
if (addNewLines.includes(n) || (testLineWidth > maxWidth && n > 0)) {
111-
// if the line exceeded the width with one additional word
112-
// just paint the line without the word
110+
// If the line exceeded the width with one additional word, just
111+
// paint the line without the word
113112
textContext.fillText(line, textX, textY);
114113

115-
// start a new line with the last word
116-
// and add the following (if this word was a newline word)
114+
// Start a new line with the last word and add the following (if
115+
// this word was a newline word)
117116
line = word;
118117

119-
// move the pen down
118+
// Move the pen down
120119
textY += lineHeight;
121120
} else {
122-
// if not exceeded, just continue
121+
// If not exceeded, just continue
123122
line = testLine;
124123
}
125124
}
126125

127-
// paint the last line
126+
// Paint the last line
128127
textContext.fillText(line, textX, textY);
129128

130-
// increase the size of the text layer by the line height,
131-
// but in case the line height is less than the font size
132-
// we increase by font size in order to prevent clipping
129+
// Increase the size of the text layer by the line height
130+
// But in case the line height is less than the font size, we increase
131+
// by font size in order to prevent clipping.
133132
const height = textY + Math.max(lineHeight, fontSize);
134133

135134
return {
@@ -139,13 +138,15 @@ const createTextData = (
139138
};
140139

141140
const createImageCanvas = (content: string, conf: ComputedOptions) => {
142-
// First pass: measure the text so we can create a canvas
143-
// big enough to fit the text. This has to be done since we can't
144-
// resize the canvas on the fly without losing the settings of the 2D context
141+
// First pass: Measure the text so we can create a canvas big enough
142+
// to fit the text.
143+
// This has to be done since we can't resize the canvas on the fly
144+
// without losing the settings of the 2D context
145145
// https://github.com/Automattic/node-canvas/issues/1625
146146
const { textHeight } = createTextData(
147147
content,
148-
// max width of text itself must be the image max width reduced by left-right margins
148+
// Max width of text itself must be the image max width reduced by
149+
// left-right margins
149150
{
150151
maxWidth: conf.maxWidth - conf.margin * 2,
151152
fontSize: conf.fontSize,
@@ -165,15 +166,16 @@ const createImageCanvas = (content: string, conf: ComputedOptions) => {
165166
console.warn('Text is longer than customHeight, clipping will occur.');
166167
}
167168

168-
// Second pass: we now know the height of the text on the canvas,
169-
// so let's create the final canvas with the given height and width
170-
// and pass that to createTextData so we can get the text data from it
169+
// Second pass: We now know the height of the text on the canvas, so
170+
// let's create the final canvas with the given height and width and
171+
// pass that to createTextData so we can get the text data from it
171172
const height = conf.customHeight || textHeightWithMargins;
172173
const canvas = createCanvas(conf.maxWidth, height);
173174

174175
const { textData } = createTextData(
175176
content,
176-
// max width of text itself must be the image max width reduced by left-right margins
177+
// Max width of text itself must be the image max width reduced by
178+
// left-right margins
177179
{
178180
maxWidth: conf.maxWidth - conf.margin * 2,
179181
fontSize: conf.fontSize,
@@ -189,8 +191,8 @@ const createImageCanvas = (content: string, conf: ComputedOptions) => {
189191
);
190192
const ctx = canvas.getContext('2d');
191193

192-
// the canvas will have the text from the first pass on it,
193-
// so start by clearing the whole canvas and start from a clean slate
194+
// The canvas will have the text from the first pass on it, so start by
195+
// clearing the whole canvas and start from a clean slate
194196
ctx.clearRect(0, 0, canvas.width, canvas.height);
195197
ctx.globalAlpha = 1;
196198
ctx.fillStyle = conf.bgColor;
@@ -200,11 +202,12 @@ const createImageCanvas = (content: string, conf: ComputedOptions) => {
200202
let textY = conf.margin;
201203
if (conf.customHeight && conf.verticalAlign === 'center') {
202204
textY =
203-
// divide the leftover whitespace by 2
205+
// Divide the leftover whitespace by 2
204206
(conf.customHeight - textData.height) / 2 +
205-
// offset for the extra space under the last line to make bottom and top whitespace equal
206-
// but only up until the bottom of the text
207-
// (i.e. don't consider a linheight less than the font size)
207+
// Offset for the extra space under the last line to make bottom
208+
// and top whitespace equal.
209+
// But only up until the bottom of the text (i.e. don't consider a
210+
// line height less than the font size)
208211
Math.max(0, (conf.lineHeight - conf.fontSize) / 2);
209212
}
210213

0 commit comments

Comments
 (0)