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

Expose y position in print callback #506

Closed
Zyberg opened this issue Aug 3, 2018 · 5 comments · Fixed by #572
Closed

Expose y position in print callback #506

Zyberg opened this issue Aug 3, 2018 · 5 comments · Fixed by #572
Labels
enhancement a request for a new feature or change in behavior

Comments

@Zyberg
Copy link

Zyberg commented Aug 3, 2018

I have to print 2 text blocks onto an image and the second one needs to be separated by exactly 100px after the first.

What I am doing right now is simply looping through the text and by using measureText function trying to find the number of lines that my text block has, then finding the max y value of each line of the text (which turns out to be 2px higher for every line than font.common.lineHeight, so I suppose there's some padding or something? Is it consistent?)
It's all kind of nice logically, BUT the way the text is being drawn on an image in the library itself is exactly by looping through all the text, counting the number of lines etc. So, I am doing practically same loop twice and increase the running time of my function twice :/

Is there a good way around this?

@hipstersmoothie
Copy link
Collaborator

You could expose this the line counting functionality. IDK of any current way to get what you want. PRs are welcome! All the printing code is found in src/image-manipulation/text.js

@hipstersmoothie hipstersmoothie changed the title What is an efficient way to find the size of of a text block? Expose function for # of lines in text block Aug 12, 2018
@hipstersmoothie
Copy link
Collaborator

Exposing the function would accomplish the same thing though. Calling the measureText function twice...

@hipstersmoothie
Copy link
Collaborator

hipstersmoothie commented Aug 18, 2018

@Zyberg I dont think you'll be able to get past calling this function twice.

We can't return anything other than this or else it would break chaining. We could use the callback though. Would something like this work

image.print(font, 0, 0, "Some string", (err, image, yValue) => {
	image.print(font, 0, yValue, "Some other string")
})

@Zyberg
Copy link
Author

Zyberg commented Aug 19, 2018

@hipstersmoothie this would work without calling the same function twice, so it looks like a great solution.

@hipstersmoothie hipstersmoothie changed the title Expose function for # of lines in text block Expose y position in print callback Aug 21, 2018
@hipstersmoothie hipstersmoothie added enhancement a request for a new feature or change in behavior and removed enhancement-issue labels Aug 26, 2018
@hipstersmoothie hipstersmoothie mentioned this issue Aug 27, 2018
4 tasks
@hipstersmoothie
Copy link
Collaborator

Released in v0.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement a request for a new feature or change in behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants