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

[feat] intelligent word-wrap for title + subtitle text #7

Open
nberlette opened this issue Sep 13, 2022 · 0 comments
Open

[feat] intelligent word-wrap for title + subtitle text #7

nberlette opened this issue Sep 13, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@nberlette
Copy link
Owner

Currently there is no word-wrap functionality baked into the SVG rendering step, which is just shameful ☹️. This issue is mostly a place for me to store some thoughts, notes, and log my results throughout the process of implementing a "smart" wrapper function for migo.

Can we get away with using SVGTextElement.getComputedTextLength()? Does it produce accurate and dependable results...? On that note, what about SVGTextElement.getBoundingClientRect? Or SVGTextElement.getBBox? It sure would save me a lot of time to just be able to use a built-in interface for 90% of the logic in this scenario.

If not, heres a basic outline of the steps involved:

  1. calculate glyph width from font size + weight
  • (like @nicktaras/getFontCharWidth, or digging into the SVG font interfaces)
  1. estimate required width for a given string in the desired font/size/weight
  • (e.g. textLength * (glyphWidthFromStep1 + letterSpacing))
  1. subtract a small percentage of the window width to determine a "safe-zone"; ensuring
    any breakable-words will be wrapped before it reaches the edge of the canvas or screen.
  • e.g. width - (width * 0.05) ~> 950px / 1000px total (25px margin)
  1. diff the required text width with the image safe-zone width.
let d = Math.ceil(requiredWidthFromStep2 - safeWidthFromStep3);
if (d > 0) {
  wrapLine();
  // if a line is reeeeeally long and requires 2+ screen breaks to fit
  // this re-wrap the text until all lines fit within the canvas.
  repeatStartingFromStep2ForEveryLine();
}
  1. if all lines don't fit within the canvas size, attempt to scale the font size
    down until they do. Truncating the text should only be used as THE last resort.
@nberlette nberlette added the enhancement New feature or request label Sep 13, 2022
@nberlette nberlette self-assigned this Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant