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

core: Add loading font from InputStream #328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ All notable changes to this project will be documented in this file.
- onScroll event.
- Style property for SingleLayerVisuals.
- flip() function in CardView.
- loadFont(InputStream) for loading custom fonts from within .jar-files.

### Changed
- BoardGameApplication.runOnGUIThread may now be called without starting an application for headless testing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,8 @@ In order to load a custom font use the [BoardGameApplication][BoardGameApplicati

````kotlin
// Load Roboto-Regular.ttf from root of resource folder
val resource = UIComponentExample::class.java.getResource("/Roboto-Regular.ttf") ?: throw FileNotFoundException()
val fontFile = File(resource.toURI())
BoardGameApplication.loadFont(fontFile)
val fontStream = SwimApplication::class.java.getResourceAsStream("/Roboto-Regular.ttf") ?: throw FileNotFoundException()
BoardGameApplication.loadFont(fontStream)
````
Now the font is registered and ready to use.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import tools.aqua.bgw.event.KeyEvent
import tools.aqua.bgw.observable.properties.Property
import tools.aqua.bgw.visual.ImageVisual
import tools.aqua.bgw.visual.Visual
import java.io.InputStream

/**
* Baseclass for all BGW Applications. Extend from this class in order to create your own game
Expand Down Expand Up @@ -354,16 +355,25 @@ open class BoardGameApplication(

/**
* Loads a font file and registers it in the JFX graphics system.
* @param font The font file off type .ttf which is to be loaded
* @param fontStream An input stream to the font file of type .ttf which is to be loaded
* @return A boolean weather the font could be loaded or not
*/
fun loadFont(fontStream: InputStream): Boolean {
val jfxFont = JFXFont.loadFont(fontStream, DEFAULT_FONT_SIZE) ?: return false
return JFXFont.getFamilies().contains(jfxFont.family)
}

/**
* Loads a font file and registers it in the JFX graphics system.
* @param font The font file of type .ttf which is to be loaded
* @throws NoSuchFileException if the file doesn't exist
* @throws AccessDeniedException if the file can't be read
* @return A boolean weather the file could be loaded or not
*/
fun loadFont(font: File): Boolean {
if (!font.exists()) throw NoSuchFileException(font)
if (!font.canRead()) throw AccessDeniedException(font)
val jfxFont = JFXFont.loadFont(font.inputStream(), DEFAULT_FONT_SIZE) ?: return false
return JFXFont.getFamilies().contains(jfxFont.family)
return loadFont(font.inputStream())
}
}
}