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

Print #572

Merged
merged 6 commits into from
Sep 1, 2018
Merged

Print #572

Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 25 additions & 6 deletions packages/jimp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ image.composite(srcImage, 100, 0, Jimp.BLEND_MULTIPLY, 0.5, 0.9);
Jimp supports basic typography using BMFont format (.fnt) even ones in different languages! Just find a bitmap font that is suitable [bitmap fonts](https://en.wikipedia.org/wiki/Bitmap_fonts):

```js
Jimp.loadFont(path).then(function(font) {
Jimp.loadFont(path).then((font) => {
// load font from .fnt file
image.print(font, x, y, str); // print a message on an image
image.print(font, x, y, str, maxWidth); // print a message on an image with text wrapped at maxWidth
Expand All @@ -274,7 +274,7 @@ Jimp.loadFont(path).then(function(font) {
Alignment modes are supported by replacing the `str` argument with an object containing `text`, `alignmentX` and `alignmentY`. `alignmentX` defaults to `Jimp.HORIZONTAL_ALIGN_LEFT` and `alignmentY` defaults to `Jimp.VERTICAL_ALIGN_TOP`.

```js
Jimp.loadFont(path).then(function(font) {
Jimp.loadFont(path).then((font) => {
image.print(
font,
x,
Expand Down Expand Up @@ -316,7 +316,7 @@ Jimp.FONT_SANS_128_WHITE; // Open Sans, 128px, white
These can be used as follows:

```js
Jimp.loadFont(Jimp.FONT_SANS_32_BLACK).then(function(font) {
Jimp.loadFont(Jimp.FONT_SANS_32_BLACK).then((font) => {
image.print(font, 10, 10, 'Hello world!');
});
```
Expand All @@ -330,6 +330,25 @@ Jimp.measureText(Jimp.FONT_SANS_32_BLACK, 'Some string'); // width of text
Jimp.measureTextHeight(Jimp.FONT_SANS_32_BLACK, 'Some string', 100); // height of text
```

#### Staggering Text

If you need to stagger text position along the y-axis the print method also returns the final y position as an argument to the callback.

```js
Jimp.loadFont(Jimp.FONT_SANS_32_BLACK).then(font => {
image.print(
font,
10,
10,
'Hello world that wraps!',
50,
(err, image, yValue) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample needs to be changed with x/y value

image.print(font, 0, yValue + 20, 'More text on another line', 50);
}
);
});
```

Online tools are also available to convert TTF fonts to BMFont format. They can be used to create color font or sprite packs.

:star: [Littera](http://kvazars.com/littera/)
Expand Down Expand Up @@ -543,23 +562,23 @@ Jimp.cssColorToHex(cssColor); // e.g. converts #FF00FF to 0xFF00FFFF
If you want to begin with an empty Jimp image, you can call the Jimp constructor passing the width and height of the image to create and a Node-style callback:

```js
new Jimp(256, 256, function(err, image) {
new Jimp(256, 256, (err, image) => {
// this image is 256 x 256, every pixel is set to 0x00000000
});
```

You can optionally set the pixel colour as follows:

```js
new Jimp(256, 256, 0xff0000ff, function(err, image) {
new Jimp(256, 256, 0xff0000ff, (err, image) => {
// this image is 256 x 256, every pixel is set to 0xFF0000FF
});
```

Or you can use a css color format:

```js
new Jimp(256, 256, '#FF00FF', function(err, image) {
new Jimp(256, 256, '#FF00FF', (err, image) => {
// this image is 256 x 256, every pixel is set to #FF00FF
});
```
Expand Down