1+ package com.coder.ffmpeg.jni
2+
3+ import android.content.Context
4+ import java.io.File
5+ import java.io.FileOutputStream
6+ import java.io.IOException
7+ import java.util.Collections
8+ import java.util.concurrent.atomic.AtomicReference
9+
10+
11+ class FFmpegConfig {
12+
13+ companion object {
14+
15+ init {
16+ System .loadLibrary(" ffmpeg-org" )
17+ System .loadLibrary(" ffmpeg-command" )
18+ }
19+
20+ /* *
21+ * Set the env of native
22+ * @param name env name
23+ * @param value env value
24+ */
25+ private external fun setNativeEnv (name : String , value : String )
26+
27+ /* *
28+ * Whether to enable debugging mode
29+ * @param debug true or false
30+ */
31+ external fun setDebug (debug : Boolean )
32+
33+ /* *
34+ * Set font config dir for fontconfig
35+ * Note:It's a config dir not font dir
36+ * @param configPath the font config dir
37+ */
38+ fun setFontConfigPath (configPath : String ) {
39+ setNativeEnv(" FONTCONFIG_PATH" , configPath)
40+ }
41+
42+ /* *
43+ * Set font config file for fontconfig
44+ * Note:It's a config file not font file
45+ * @param configFile the font config file
46+ */
47+ fun setFontConfigFile (configFile : String ) {
48+ setNativeEnv(" FONTCONFIG_FILE" , configFile)
49+ }
50+
51+ /* *
52+ * Set font dir for fontconfig
53+ * @param context context for application
54+ * @param fontDir the font dir contain fonts (.ttf and .otf files)
55+ * @param fontNameMapping
56+ */
57+ fun setFontDir (context : Context , fontDir : String , fontNameMapping : Map <String , String >){
58+ setFontDirList(context, Collections .singletonList(fontDir),fontNameMapping)
59+ }
60+ /* *
61+ * Set font dir for fontconfig
62+ * @param context context for application
63+ * @param fontDirList list of directories that contain fonts (.ttf and .otf files)
64+ */
65+ fun setFontDirList (context : Context , fontDirList : List <String >, fontNameMapping : Map <String , String >) {
66+ var validFontNameMappingCount = 0
67+ val cacheDir = context.cacheDir
68+ val fontConfigDir = File (cacheDir, " fontconfig" )
69+ if (! fontConfigDir.exists()) {
70+ fontConfigDir.mkdirs()
71+ }
72+ // Create font config
73+ val fontConfigFile = File (fontConfigDir, " fonts.conf" )
74+ if (fontConfigFile.exists() && fontConfigFile.isFile) {
75+ fontConfigFile.delete()
76+ }
77+ fontConfigFile.createNewFile()
78+ val fontNameMappingBlock = StringBuilder ()
79+ if (fontNameMapping.isNotEmpty()){
80+ for (entry in fontNameMapping.entries) {
81+ val fontName: String = entry.key
82+ val mappedFontName: String = entry.value
83+
84+ if ((fontName.trim().isNotEmpty()) && (mappedFontName.trim().isNotEmpty())) {
85+ fontNameMappingBlock.append(" <match target=\" pattern\" >\n " );
86+ fontNameMappingBlock.append(" <test qual=\" any\" name=\" family\" >\n " );
87+ fontNameMappingBlock.append(String .format(" <string>%s</string>\n " , fontName));
88+ fontNameMappingBlock.append(" </test>\n " );
89+ fontNameMappingBlock.append(" <edit name=\" family\" mode=\" assign\" binding=\" same\" >\n " );
90+ fontNameMappingBlock.append(String .format(" <string>%s</string>\n " , mappedFontName));
91+ fontNameMappingBlock.append(" </edit>\n " );
92+ fontNameMappingBlock.append(" </match>\n " );
93+
94+ validFontNameMappingCount++
95+ }
96+ }
97+ }
98+ val fontConfigBuilder = StringBuilder ()
99+ fontConfigBuilder.append(" <?xml version=\" 1.0\" ?>\n " )
100+ fontConfigBuilder.append(" <!DOCTYPE fontconfig SYSTEM \" fonts.dtd\" >\n " )
101+ fontConfigBuilder.append(" <fontconfig>\n " )
102+ fontConfigBuilder.append(" <dir prefix=\" cwd\" >.</dir>\n " )
103+ for (fontDirectoryPath in fontDirList) {
104+ fontConfigBuilder.append(" <dir>" )
105+ fontConfigBuilder.append(fontDirectoryPath)
106+ fontConfigBuilder.append(" </dir>\n " )
107+ }
108+ fontConfigBuilder.append(fontNameMappingBlock)
109+ fontConfigBuilder.append(" </fontconfig>\n " )
110+ val reference = AtomicReference <FileOutputStream >()
111+ try {
112+ val outputStream = FileOutputStream (fontConfigFile)
113+ reference.set(outputStream)
114+
115+ outputStream.write(fontConfigBuilder.toString().toByteArray())
116+ outputStream.flush()
117+ setFontConfigPath(fontConfigDir.absolutePath)
118+ }catch (e: IOException ){
119+ e.printStackTrace()
120+ }finally {
121+ reference.get().close()
122+ }
123+ }
124+
125+ }
126+ }
0 commit comments