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

Fonts are not reloaded even when changing on disk #112

Closed
frankrolf opened this issue Aug 25, 2017 · 18 comments · Fixed by #422
Closed

Fonts are not reloaded even when changing on disk #112

frankrolf opened this issue Aug 25, 2017 · 18 comments · Fixed by #422
Labels

Comments

@frankrolf
Copy link
Contributor

I am using DrawBot to create proofs, the fonts used in those proofs are re-generated frequently.
When I run the script from the DrawBot window, the font file that is picked up first stays active forever.

My script is wrapped like this:

newDrawing()
	for fontFile in fontFiles:
		newPage()
		fs = FormattedString(
			proofText, fontFile
		)
		text(fs)
	for fontFile in fontFiles:
		uninstallFont(fontFile)
endDrawing()

I explicitly added the newDrawing() and endDrawing() hoping it would have some kind of effect, but no.
It works as expected when I run the script from the command line, but it sometimes is nice to have that visual output …

@typemytype
Copy link
Owner

I guess this is an apple CoreText issue... it seems like the fonts are cached. When installing fonts from the same source (path on disk) with the same fontName (aka postscriptName) within the same runtime of an app, their is no visual update... which is idd not nice...

@justvanrossum
Copy link
Collaborator

How does RF manage to get this right with test install? Seems like it should be more or less the same mechanism.

@frankrolf
Copy link
Contributor Author

Here is a tangentially related problem:
I want to compare revisions of the same design, and pick up two OTF files that have different file names but same PS name. Only the first one gets picked up throughout the whole output PDF (and GIF, etc). When I edit the PS name, it works as expected.
I assume this is a limitation within the PDF format? Any suggestions on how to modify the PS name on the fly?

@typemytype
Copy link
Owner

I guess you can embed two fonts with the same psname...

  • open with fontTools
  • edit the name table
  • store it in a separate folder
  • install from from
  • remove that folder

@frankrolf
Copy link
Contributor Author

That works so well! Thank you Frederik!

@typoman
Copy link
Contributor

typoman commented Feb 14, 2019

See:
#261 (comment)
I have similar issue with the app. But I'm going to see if I can reprodue it using the module too.

@typoman
Copy link
Contributor

typoman commented Feb 23, 2019

I have tested drawbot as a module and there is no font caching happening in my case when I generate a font and change it constantly and use it in a text inside drawbot. I also tested the script simultaneously inside the app and it seems font caching is persistent there. I don't know if this helps but there is a way to clear the font cache in the shell but maybe it's not what drawBot app is supposed to do or has the privilege to do?

Clear font cache in shell:

atsutil databases -removeUser && \
atsutil server -shutdown && \
atsutil server -ping

@typemytype
Copy link
Owner

that is true and also logic to explain as a font activation in the same runloop is different then activated in a separate runloop. Inside DrawBot every script runs in the same runloop while in terminal each time a script runs its a different runloop.

Changing postscript names in the font makes a difference... I know this not always possible and just a dirty detour

@arrowtype
Copy link
Sponsor

arrowtype commented Sep 26, 2019

I am experiencing some difficult font caching while using DrawBot as a module.

I've tried to follow this set of steps exactly, but I'm still running into a cached font:

open with fontTools
edit the name table
store it in a separate folder
install from from
remove that folder

Here is the current version of the folder I am working in, including the fonts:
https://github.com/arrowtype/recursive/tree/9577545c44ded1547168f138a143976263820670/src/proofs/vietnamese-diacritics

Within that, here's my DrawBot script:
https://github.com/arrowtype/recursive/blob/9577545c44ded1547168f138a143976263820670/src/proofs/vietnamese-diacritics/recursive-vietnamese-diacritics-sep_23_2019.drawbot.py

Pages 8 and 9 of my output PDF use the glyph . The previous font lacked this glyph, and the new font includes it. I have confirmed that the new font supports this glyph using multiple apps/sites, including https://wakamaifondue.com/. I have also confirmed that my naming updates with fontTools are working to update names 3 & 6, and that it is working on the correct font.

However, I am so far completely unable to see the change in the DrawBot output, even after implementing a temp file with renamed name IDs 6 & 3.

I have also tried using @typoman's command to clear my shell's font cache, to no avail.

Is there anything I might be missing, here?

@frankrolf
Copy link
Contributor Author

I just do the following, which seems to work (dummy code):

def make_temp_font(file_index, font_file):
    '''
    Make a temporary font file with unique PS name, because the same PS name
    implies that the same font is seen throughout the PDF
    '''
    ttf = ttLib.TTFont(os.path.abspath(font_file))
    font_dir = os.path.dirname(font_file)
    temp_font_name = os.path.splitext(os.path.basename(font_file))[0]
    temp_font_file = os.path.join(
        font_dir, '{}_temp_{}.otf'.format(temp_font_name, file_index))
    ttf["name"].setName(
        temp_font_name, nameID=6, platformID=1, platEncID=0, langID=0)
    ttf.save(temp_font_file)
    return(temp_font_file)

temp_fonts = [
    make_temp_font(input_index, input_path) for
    (input_index, input_path) in enumerate(input_paths)]

for page in proof_text:
    for input_index, input_path in enumerate(temp_fonts):
        make_page(input_path)

@arrowtype
Copy link
Sponsor

Thanks so much for sharing your approach, @frankrolf! Strangely, this is generating a font for me, but it's still not working. Maybe it's something to do with this being a variable font? I'm not really sure. I adapted the code to change all the font family name IDs, and still no luck.

https://github.com/arrowtype/recursive/blob/82aa3f04e906c16f1387e23b168d83f63121ce60/src/proofs/vietnamese-diacritics/recursive-vietnamese-diacritics-sep_23_2019.drawbot.py

def make_temp_font(timestamp, font_file):
    '''
    Make a temporary font file with unique PS name, because the same PS name
    implies that the same font is seen throughout the PDF
    '''
    ttf = TTFont(os.path.abspath(font_file))
    font_dir = os.path.dirname(font_file)
    temp_font_name = os.path.splitext(os.path.basename(font_file))[0]
    tempName6 = f"tempFont-{timestamp}"
    temp_font_file = os.path.join(font_dir, f"{tempName6}.ttf")
    ttf["name"].setName(tempName6, nameID=6, platformID=1, platEncID=0, langID=0)
    ttf["name"].setName(tempName6, nameID=1, platformID=1, platEncID=0, langID=0)
    ttf["name"].setName(tempName6, nameID=4, platformID=1, platEncID=0, langID=0)
    ttf["name"].setName(tempName6, nameID=16, platformID=1, platEncID=0, langID=0)
    oldName3 = ttf['name'].getName(3, 3, 1)
    tempName3 = str(oldName3).replace(str(oldName3).split(';')[-1],tempName6)
    ttf["name"].setName(tempName6, nameID=3, platformID=1, platEncID=0, langID=0)
    ttf.save(temp_font_file)
    return(temp_font_file)

tempFile = make_temp_font(now, fontFam)

@justvanrossum
Copy link
Collaborator

justvanrossum commented Mar 23, 2021

Ok, I've experimented, and I can get this to work for TTF and OTF (possibly woff/woff2), but not for TTC/OTC. However, for efficiency's sake, I propose a new function that explicitly reloads the font:

reloadFont(fontNameOrPath)

How about that?

@typemytype
Copy link
Owner

+1 for an explicit reloadFont

@justvanrossum
Copy link
Collaborator

I managed efficient automatic reloading without the need for an explicit reloadFont() call: #422.

typemytype pushed a commit that referenced this issue Mar 24, 2021
* remove unneeded call to installedFonts during newDrawing: that must have been a debugging leftover?

* Implement implicit reloading of fonts if they're changed on disk. Fixes #112

* fix typo in comment

* tweak comment

* tweak comment
@justvanrossum
Copy link
Collaborator

justvanrossum commented Mar 24, 2021

@frankrolf:

Here is a tangentially related problem:
I want to compare revisions of the same design, and pick up two OTF files that have different file names but same PS name. Only the first one gets picked up throughout the whole output PDF (and GIF, etc). When I edit the PS name, it works as expected.
I assume this is a limitation within the PDF format? Any suggestions on how to modify the PS name on the fly?

Definitely related. I just tried this particular scenario, and indeed, #421 + #422 fix this, too.

@justvanrossum
Copy link
Collaborator

justvanrossum commented Mar 24, 2021

I want to compare revisions of the same design, and pick up two OTF files

After further thinking: #421 fixed this, by avoiding the font manager altogether. The (ps) font name is now completely irrelevant when using font paths.

@arrowtype
Copy link
Sponsor

Awesome, thanks for making these upgrades!

@justvanrossum
Copy link
Collaborator

but not for TTC/OTC

Automatic reloading as implemented in #422 does work for TTC/OTC after all.

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

Successfully merging a pull request may close this issue.

5 participants