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

fontLeading() and how to get rid of it #345

Closed
gferreira opened this issue Dec 10, 2019 · 2 comments
Closed

fontLeading() and how to get rid of it #345

gferreira opened this issue Dec 10, 2019 · 2 comments

Comments

@gferreira
Copy link
Contributor

for background info see TextBox and baseline alignment

  1. what does the value returned by fontLeading() refer to exactly?
  2. setting a lineHeight() with fonts that have a non-zero fontLeading() results in a different line height than the one specified (see example script below)
  3. is there a way to set an exact lineHeight value which is independent of the font’s leading? is it possible to set the leading to zero?
from random import choice

txt = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed dignissim risus ac ex auctor laoreet. Vestibulum auctor libero vitae nisi tincidunt condimentum. Pellentesque sed feugiat nunc. Proin turpis urna, molestie sed turpis eu, placerat tristique nisl. Phasellus auctor lectus mi, ut dictum mauris tincidunt vel. Maecenas congue et elit ut iaculis. Nullam gravida viverra arcu nec efficitur. Nulla convallis diam neque, vitae bibendum ligula interdum non. Mauris a elementum magna. Curabitur lectus erat, aliquet vitae libero a, lobortis tempus ipsum. In mattis egestas pretium. Vestibulum et ligula semper, eleifend purus nec, maximus tortor. ' * 10

size('A4')

allFonts = installedFonts()
aFont = choice(allFonts)
print('font:', aFont)

margin = 40
baselineHeight = 30

w = width() - margin * 2
h = height() - margin * 2
box = margin, margin, w, h

font(aFont, 16), lineHeight(baselineHeight)
print('line height:', fontLineHeight())
print('leading:', fontLeading())

# draw document baseline
stroke(0, 1, 1), strokeWidth(1), fill(None)
rect(*box)
yPos = height() - margin
while yPos > margin:
    line((margin, yPos), (width() - margin, yPos))
    yPos -= baselineHeight

# adjust first line posY
textBaselines = textBoxBaselines(txt, box)
firstLineGap = height() - margin - textBaselines[0][-1]
translate(0, firstLineGap - baselineHeight)

# draw texbox baseline
stroke(1, 0, 1)
for x, y in textBoxBaselines(txt, box):
    line((x, y), (x + w, y))

# draw texbox
stroke(None), fill(0)
textBox(txt, box)

thanks!

@typemytype
Copy link
Owner

  1. See
    def fontLeading(self):

@gferreira
Copy link
Contributor Author

@typemytype yep, I saw that – but the NSFont.leading docs doesn’t provide any additional info.

this is actually a duplicate of #52 and #297 – cannot set consistent and exact line height.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants