Skip to content

Commit f7ed797

Browse files
Translate Android font rules to fontconfig
1 parent 2d48149 commit f7ed797

6 files changed

Lines changed: 480 additions & 1 deletion

File tree

pdf2htmlEX/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ android {
4545
buildTypes.release.externalNativeBuild {
4646
cmake.arguments '-DCMAKE_BUILD_TYPE=MinSizeRel'
4747
}
48+
packagingOptions {
49+
exclude 'lib/*/libnativeFontconfigInstrumentedTests.so'
50+
}
4851
externalNativeBuild.cmake {
4952
path "src/main/cpp/CMakeLists.txt"
5053
version "3.10.2"
5154
}
52-
sourceSets.main.assets.srcDirs = ["${projectDir}/../dependency-builder/build/assets/"]
55+
sourceSets.main.assets.srcDirs += "${projectDir}/../dependency-builder/build/assets/"
5356
ndkVersion "20.1.5948944"
5457
}
5558

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* FontconfigInstrumentedTests.java
3+
*
4+
* Copyright (C) 2019 Vilius Sutkus'89
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package com.viliussutkus89.android.pdf2htmlex;
21+
22+
import androidx.test.ext.junit.runners.AndroidJUnit4;
23+
import androidx.test.platform.app.InstrumentationRegistry;
24+
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
28+
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertTrue;
30+
31+
import java.util.Arrays;
32+
import java.util.HashSet;
33+
import java.util.Set;
34+
35+
@RunWith(AndroidJUnit4.class)
36+
public class FontconfigInstrumentedTests {
37+
static {
38+
System.loadLibrary("nativeFontconfigInstrumentedTests");
39+
}
40+
41+
//pdf base14 fonts:
42+
//Courier, Courier Bold, Courier Oblique, Courier Bold-Oblique
43+
//Helvetica, Helvetica Bold, Helvetica Oblique, Helvetica Bold-Oblique
44+
//Times Roman, Times Bold, Times Italic, Times Bold-Italic
45+
//Symbol
46+
//Zapf Dingbats
47+
48+
@Test
49+
public synchronized void matchCourierTest() {
50+
Set<String> acceptedValues = new HashSet<>(Arrays.asList("CutiveMono.ttf", "DroidSansMono.ttf"));
51+
52+
// @TODO: init only FontconfigAndroid, not the whole pdf2htmlEX
53+
new pdf2htmlEX(InstrumentationRegistry.getInstrumentation().getTargetContext());
54+
assertTrue(acceptedValues.contains(getFontFilenameFromFontconfig("Courier")));
55+
56+
// @TODO: these should be downloaded at run time
57+
assertTrue(acceptedValues.contains(getFontFilenameFromFontconfig("Courier:Bold")));
58+
assertTrue(acceptedValues.contains(getFontFilenameFromFontconfig("Courier:Oblique")));
59+
assertTrue(acceptedValues.contains(getFontFilenameFromFontconfig("Courier:Bold:Oblique")));
60+
}
61+
62+
@Test
63+
public synchronized void matchHelveticaTest() {
64+
// @TODO: init only FontconfigAndroid, not the whole pdf2htmlEX
65+
new pdf2htmlEX(InstrumentationRegistry.getInstrumentation().getTargetContext());
66+
67+
// /system/fonts/DroidSans.ttf symlinked to Roboto-Regular.ttf
68+
assertTrue(new HashSet<>(Arrays.asList("Roboto-Regular.ttf", "DroidSans.ttf"))
69+
.contains(getFontFilenameFromFontconfig("Helvetica")));
70+
71+
// /system/fonts/DroidSans-Bold.ttf symlinked to Roboto-Bold.ttf
72+
assertTrue(new HashSet<>(Arrays.asList("Roboto-Bold.ttf", "DroidSans-Bold.ttf"))
73+
.contains(getFontFilenameFromFontconfig("Helvetica:Bold")));
74+
75+
assertEquals("Roboto-Italic.ttf", getFontFilenameFromFontconfig("Helvetica:Oblique"));
76+
assertEquals("Roboto-BoldItalic.ttf", getFontFilenameFromFontconfig("Helvetica:Bold:Oblique"));
77+
}
78+
79+
@Test
80+
public synchronized void matchTimesTest() {
81+
// @TODO: init only FontconfigAndroid, not the whole pdf2htmlEX
82+
new pdf2htmlEX(InstrumentationRegistry.getInstrumentation().getTargetContext());
83+
84+
assertTrue(new HashSet<>(Arrays.asList("NotoSerif-Regular.ttf", "DroidSerif-Regular.ttf"))
85+
.contains(getFontFilenameFromFontconfig("Times")));
86+
87+
assertTrue(new HashSet<>(Arrays.asList("NotoSerif-Bold.ttf", "DroidSerif-Bold.ttf"))
88+
.contains(getFontFilenameFromFontconfig("Times:Bold")));
89+
90+
assertTrue(new HashSet<>(Arrays.asList("NotoSerif-Italic.ttf", "DroidSerif-Italic.ttf"))
91+
.contains(getFontFilenameFromFontconfig("Times:Italic")));
92+
93+
assertTrue(new HashSet<>(Arrays.asList("NotoSerif-BoldItalic.ttf", "DroidSerif-BoldItalic.ttf"))
94+
.contains(getFontFilenameFromFontconfig("Times:Bold:Italic")));
95+
}
96+
97+
private native String getFontFilenameFromFontconfig(String pattern);
98+
}

pdf2htmlEX/src/main/cpp/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ target_link_libraries(pdf2htmlEX-android
4040
set_target_properties(pdf2htmlEX-android PROPERTIES COMPILE_FLAGS ${pdf2htmlEX_CFLAGS})
4141
target_include_directories(pdf2htmlEX-android PRIVATE ${pdf2htmlEX_INCLUDE_DIRS})
4242

43+
add_library(nativeFontconfigInstrumentedTests SHARED nativeFontconfigInstrumentedTests.cpp)
44+
target_link_libraries(nativeFontconfigInstrumentedTests ${pdf2htmlEX_LIBRARIES})
45+
46+
set_target_properties(nativeFontconfigInstrumentedTests PROPERTIES COMPILE_FLAGS ${pdf2htmlEX_CFLAGS})
47+
target_include_directories(nativeFontconfigInstrumentedTests PRIVATE ${pdf2htmlEX_INCLUDE_DIRS})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <jni.h>
2+
#include <fontconfig/fontconfig.h>
3+
4+
extern "C"
5+
JNIEXPORT jstring JNICALL
6+
Java_com_viliussutkus89_android_pdf2htmlex_FontconfigInstrumentedTests_getFontFilenameFromFontconfig(
7+
JNIEnv *env, jobject, jstring pattern_) {
8+
const char * pattern = env->GetStringUTFChars(pattern_, nullptr);
9+
FcFontSet *fs = FcFontSetCreate();
10+
FcPattern *pat = FcNameParse (reinterpret_cast<const FcChar8 *>(pattern));
11+
12+
env->ReleaseStringUTFChars(pattern_, pattern);
13+
14+
FcConfigSubstitute (0, pat, FcMatchPattern);
15+
FcDefaultSubstitute (pat);
16+
FcResult result;
17+
FcPattern *match = FcFontMatch (0, pat, &result);
18+
if (match)
19+
FcFontSetAdd (fs, match);
20+
FcPatternDestroy (pat);
21+
22+
jstring matchedFilename = nullptr;
23+
24+
if (fs) {
25+
for (int j = 0; j < fs->nfont; j++) {
26+
FcPattern *font = FcPatternFilter (fs->fonts[j], 0);
27+
FcChar8 *s = FcPatternFormat (font, reinterpret_cast<const FcChar8 *>("%{file|basename}"));
28+
if (s) {
29+
matchedFilename = env->NewStringUTF(reinterpret_cast<char *>(s));
30+
FcStrFree (s);
31+
}
32+
FcPatternDestroy (font);
33+
}
34+
FcFontSetDestroy (fs);
35+
}
36+
FcFini ();
37+
return matchedFilename ? matchedFilename : env->NewStringUTF("");
38+
}

0 commit comments

Comments
 (0)