Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
skiko
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liuqiang
skiko
Commits
e8b3b444
Unverified
Commit
e8b3b444
authored
Mar 01, 2022
by
Nikolay Igotti
Committed by
GitHub
Mar 01, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add API to access fonts from resources. (#509)
* Add API to access fonts from resources. * More font locations
parent
108f7eb3
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
11 deletions
+65
-11
build.gradle.kts
skiko/build.gradle.kts
+0
-1
FontInterop.awt.kt
...src/awtMain/kotlin/org/jetbrains/skiko/FontInterop.awt.kt
+50
-10
AwtFontInterop.kt
.../src/awtTest/kotlin/org/jetbrains/skiko/AwtFontInterop.kt
+15
-0
No files found.
skiko/build.gradle.kts
View file @
e8b3b444
...
...
@@ -4,7 +4,6 @@ import org.gradle.api.tasks.testing.AbstractTestTask
import
org.jetbrains.compose.internal.publishing.MavenCentralProperties
import
org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import
org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import
org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import
org.gradle.api.tasks.testing.logging.TestExceptionFormat
plugins
{
...
...
skiko/src/awtMain/kotlin/org/jetbrains/skiko/FontInterop.awt.kt
View file @
e8b3b444
...
...
@@ -5,6 +5,7 @@ import java.awt.Font
import
java.awt.FontFormatException
import
java.io.File
import
java.io.FileInputStream
import
java.io.FileOutputStream
import
java.io.IOException
import
java.util.concurrent.ConcurrentHashMap
...
...
@@ -42,6 +43,8 @@ class AwtFontManager(fontPaths: Array<String> = emptyArray()) {
OS
.
Linux
->
{
val
pathsToCheck
=
arrayOf
(
System
.
getProperty
(
"user.home"
)
+
File
.
separator
+
".fonts"
,
"/usr/share/fonts"
,
"/usr/local/share/fonts"
,
"/usr/share/fonts/truetype"
,
"/usr/share/fonts/TTF"
)
...
...
@@ -61,12 +64,18 @@ class AwtFontManager(fontPaths: Array<String> = emptyArray()) {
}
}
private
fun
isFont
(
extension
:
String
):
Boolean
{
return
extension
==
"ttf"
||
extension
==
"ttc"
||
extension
==
"otf"
}
private
fun
findFontFiles
(
paths
:
List
<
String
>):
List
<
File
>
{
val
files
=
mutableListOf
<
File
>()
paths
.
forEach
{
path
->
val
fontDirectory
=
File
(
path
)
if
(
fontDirectory
.
exists
())
{
fontDirectory
.
walk
().
filter
{
it
.
isFile
&&
i
t
.
extension
.
lowercase
()
==
"ttf"
}.
forEach
{
fontDirectory
.
walk
().
filter
{
it
.
isFile
&&
i
sFont
(
it
.
extension
.
lowercase
())
}.
forEach
{
files
.
add
(
it
)
}
}
...
...
@@ -74,20 +83,31 @@ class AwtFontManager(fontPaths: Array<String> = emptyArray()) {
return
files
}
private
suspend
fun
cacheAllFonts
()
{
fontsMap
.
clear
()
val
fontFiles
=
findFontFiles
(
customFontPaths
)
+
findFontFiles
(
systemFontsPaths
())
for
(
file
in
fontFiles
)
{
try
{
val
f
=
FileInputStream
(
file
.
absolutePath
).
use
{
private
fun
addFontFromFile
(
file
:
File
):
Boolean
{
val
f
=
try
{
FileInputStream
(
file
.
absolutePath
).
use
{
Font
.
createFont
(
Font
.
TRUETYPE_FONT
,
it
)
}
}
catch
(
e
:
FontFormatException
){
return
false
}
catch
(
e
:
IOException
)
{
return
false
}
val
name
=
f
.
family
val
list
=
fontsMap
.
computeIfAbsent
(
name
)
{
mutableListOf
()
}
synchronized
(
list
)
{
list
.
add
(
FontDescriptor
(
file
.
absoluteFile
,
f
.
style
))
}
return
true
}
private
suspend
fun
cacheAllFonts
()
{
fontsMap
.
clear
()
val
fontFiles
=
findFontFiles
(
customFontPaths
)
+
findFontFiles
(
systemFontsPaths
())
for
(
file
in
fontFiles
)
{
try
{
addFontFromFile
(
file
)
yield
()
}
catch
(
e
:
FontFormatException
)
{
}
catch
(
e
:
IOException
)
{
...
...
@@ -186,6 +206,26 @@ class AwtFontManager(fontPaths: Array<String> = emptyArray()) {
customFontPaths
+=
path
}
/**
* Add custom resource entry as a font known to this resource manager.
*
* @return true, if font was found and identified, and false otherwise
*/
fun
addResourceFont
(
resource
:
String
,
loader
:
ClassLoader
=
Thread
.
currentThread
().
contextClassLoader
):
Boolean
{
val
res
=
loader
.
getResourceAsStream
(
resource
)
?:
ClassLoader
.
getSystemResourceAsStream
(
resource
)
?:
return
false
val
file
=
File
.
createTempFile
(
"tmp"
,
".ttf"
)
file
.
deleteOnExit
()
FileOutputStream
(
file
).
use
{
out
->
out
.
write
(
res
.
readAllBytes
())
}
return
addFontFromFile
(
file
).
also
{
if
(
it
)
customFontPaths
+=
file
.
absolutePath
else
file
.
delete
()
}
}
/**
* If all AWT fonts were cached. Check this property before using non-suspend version
* of font conversion APIs.
...
...
skiko/src/awtTest/kotlin/org/jetbrains/skiko/AwtFontInterop.kt
View file @
e8b3b444
...
...
@@ -93,4 +93,19 @@ class AwtFontInterop {
assertTrue
(
"Font must be file"
,
path
.
exists
()
&&
path
.
isFile
)
}
}
// This test is disabled due to convoluted setup of tests.
// @Test
fun
addCustomResource
()
{
runTest
{
assumeOk
()
val
fontManager
=
AwtFontManager
()
assertTrue
(
"Custom resource must be found"
,
fontManager
.
addResourceFont
(
"/fonts/JetBrainsMono-Bold.ttf"
,
Library
.
javaClass
.
classLoader
))
val
path
=
fontManager
.
findFontFamilyFile
(
"JetBrains Mono"
)
assertTrue
(
"Custom font must be found"
,
path
!=
null
)
path
!!
assertTrue
(
"Font must be file"
,
path
.
exists
()
&&
path
.
isFile
)
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment