App cannot find font in snap

I made a snap of a debian package called critterding so I can run it on my OpenSuse laptop.

The only problem with the snap is that the application is searching for a font file whose location seems to be hardcoded in the binary of the application.

When I run the application, it stops with the error: “Count not find font /usr/share/fonts/TTF/DejaVuSans.ttf”

The font is installed in a different location inside the snap: /snap/critterding/x1/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf

So, I added this to the priming step:

mkdir usr/share/fonts/TTF
cp usr/share/fonts/truetype/dejavu/. usr/share/fonts/TTF

After installing the snap, I can see that the font files are in the proper location inside the snap, but running the snap results in the same error. If I copy the font from the snap into the same location, but in the file system of my laptop, the program runs fine. That means that somehow the application that is inside the snap only looks for fonts outside the snap directory.

Here is my snapcraft.yaml

name: critterding
version: "1.0"
summary: Self Learning Critters
description: |
  AI critters that evolve
base: core18
confinement: devmode
grade: devel
license: LGPL-2.0


parts:
  critterding:
    plugin: nil
    stage-packages:
      - critterding
      - libftgl2
      - libgcc1
      - libgl1-mesa-glx
      - libgl1
      - libgomp1
      - libsdl1.2debian
      - libstdc++6
      - freeglut3
      - libslang2
      - ttf-dejavu-core
      
#    override-stage: |
#      snapcraftctl stage
    override-prime: |
      snapcraftctl prime
      sed -i.bak -e 's|Icon=critterding|Icon=/usr/share/icons/hicolor/22x22/apps/critterding.png|g' usr/share/applications/critterding.desktop
      mkdir usr/share/fonts/TTF
      cp usr/share/fonts/truetype/dejavu/*.* usr/share/fonts/TTF
#      export FONTCONFIG_PATH=/etc/fonts
#      fc-cache

apps:
  critterding:
    command: usr/bin/critterding
    desktop: usr/share/applications/critterding.desktop
    environment:
      LD_LIBRARY_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/dri:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/mesa:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/mesa-gl:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/xorg:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio/
      LIBGL_DRIVERS_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/dri 
      DISABLE_WAYLAND: 1

    plugs:
      - home
      - network
      - opengl
      - removable-media
      - unity7
      - x11
      - wayland
      - desktop
      - desktop-legacy

If the applications uses a hardcoded, absolute path like that, you can use a layout: https://snapcraft.io/docs/snap-layouts

You can check what your snap “sees” in its namespace by running snap run --shell critterding and then accessing /usr/share/fonts/TTF/DejaVuSans.ttf (ls, stat, …).

layout:
  /usr/share/fonts/TTF/DejaVuSans.ttf:
    symlink: $SNAP/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
1 Like

Yes, that works perfectly. Thank you!

I’m so happy. Now I can play with critterding as much as I want without having to recompile it every time OpenSuse comes up with a big update.

For other snapcraft beginners, here is my complete snapcraft.yaml file:

name: critterding
version: "1.0"
summary: Self Learning Critters
description: |
  AI critters that evolve
base: core18
confinement: devmode
grade: devel
license: LGPL-2.0


parts:
  critterding:
    plugin: nil
    stage-packages:
      - critterding
      - libftgl2
      - libgcc1
      - libgl1-mesa-glx
      - libgl1
      - libgomp1
      - libsdl1.2debian
      - libstdc++6
      - freeglut3
      - libslang2
      - ttf-dejavu-core
      
#    override-stage: |
#      snapcraftctl stage
    override-prime: |
      snapcraftctl prime
      sed -i.bak -e 's|Icon=critterding|Icon=/usr/share/icons/hicolor/22x22/apps/critterding.png|g' usr/share/applications/critterding.desktop
      #mkdir usr/share/fonts/TTF
#      cp usr/share/fonts/truetype/dejavu/*.* usr/share/fonts/TTF
#      export FONTCONFIG_PATH=/etc/fonts
#      fc-cache

apps:
  critterding:
    command: usr/bin/critterding
    desktop: usr/share/applications/critterding.desktop
    environment:
      LD_LIBRARY_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/dri:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/mesa:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/mesa-gl:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/xorg:$SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio/
      LIBGL_DRIVERS_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/dri 
      DISABLE_WAYLAND: 1

    plugs:
      - home
      - network
      - opengl
      - removable-media
      - unity7
      - x11
      - wayland
      - desktop
      - desktop-legacy

layout:
  /usr/share/fonts/TTF/DejaVuSans.ttf:
    symlink: $SNAP/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
1 Like