make the code public

This commit is contained in:
leafee98 2022-09-04 16:30:48 +08:00
parent a444e4da82
commit f1ee0d12e1
3 changed files with 101 additions and 0 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 leafee98
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

35
README.md Normal file
View file

@ -0,0 +1,35 @@
# fontconfig-pattern-test
This is a simple code to call fontconfig and do pattern match, like Firefox did.
In a normal fontconfig call, there should be two steps, pattern match and sort by score. But for test purpose and do simplify the code, we only do the pattern match without sort, also like what Firefox did.
So this code can help you to debug you configuration of fontconfig, and see why Firefox use another font instead of what you want.
## Compile
Make sure you have fontconfig library installed on your system.
```
g++ main.cpp -o main -lfontconfig
```
## Usage
The lines started with '>' mean it's the input, and lines without it is output. The output is too long and I cutted much many.
```
$ ./main
>sans,mono
========================
DejaVu Sans
DejaVu LGC Sans
Noto Sans
Noto Sans CJK SC
DejaVu LGC Sans
Noto Sans
DejaVu Sans
Verdana
...
```

45
main.cpp Normal file
View file

@ -0,0 +1,45 @@
#include <iostream>
#include <fontconfig/fontconfig.h>
#include <new>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
FcChar8 * toFcChar8(const char * sc) {
FcChar8 * result = new FcChar8[strlen(sc)];
for (size_t i = 0, l = strlen(sc); i < l; ++i) {
result[i] = (unsigned char)(sc[i]);
}
return result;
}
int main() {
FcPattern * fcp = FcPatternCreate();
string line;
getline(cin, line);
line += ',';
size_t l = 0, r = line.find(",", 0);
while (r != string::npos) {
FcChar8 * queryName = toFcChar8(line.substr(l, r - l).c_str());
FcPatternAddString(fcp, FC_FAMILY, queryName);
delete[] queryName;
l = r + 1;
r = line.find(",", l + 1);
}
FcChar8 * resultName = nullptr;
FcConfigSubstitute(nullptr, fcp, FcMatchPattern);
cout << "========================" << endl;
for (int i = 0; FcPatternGetString(fcp, FC_FAMILY, i, &resultName) == FcResultMatch; ++i) {
cout << resultName << endl;
}
FcPatternDestroy(fcp);
return 0;
}