From f1ee0d12e1cf9a2e705b3c12a9c1acd0abc13858 Mon Sep 17 00:00:00 2001 From: leafee98 Date: Sun, 4 Sep 2022 16:30:48 +0800 Subject: [PATCH] make the code public --- LICENSE | 21 +++++++++++++++++++++ README.md | 35 +++++++++++++++++++++++++++++++++++ main.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.cpp diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1ccca27 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..dadf575 --- /dev/null +++ b/README.md @@ -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 + +... +``` diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..ae639e7 --- /dev/null +++ b/main.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +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; +}