2012-01-12 23:32:09 +01:00
|
|
|
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
|
2007-07-29 03:21:28 +02:00
|
|
|
//
|
2012-01-12 23:32:09 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2007-07-29 03:21:28 +02:00
|
|
|
//
|
2012-01-12 23:32:09 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2007-07-29 03:21:28 +02:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
2014-07-05 19:31:53 +02:00
|
|
|
#include "font_file_lister.h"
|
2007-07-29 03:21:28 +02:00
|
|
|
|
2014-08-25 01:26:56 +02:00
|
|
|
#include <libaegisub/charset_conv_win.h>
|
2012-10-09 16:44:27 +02:00
|
|
|
#include <libaegisub/log.h>
|
2012-01-12 23:32:09 +01:00
|
|
|
|
2012-11-28 18:30:29 +01:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2014-04-16 04:33:37 +02:00
|
|
|
#include <fontconfig/fontconfig.h>
|
2012-01-12 23:32:09 +01:00
|
|
|
#include <wx/intl.h>
|
|
|
|
|
|
|
|
namespace {
|
2014-08-24 21:38:28 +02:00
|
|
|
bool pattern_matches(FcPattern *pat, const char *field, std::string const& name) {
|
|
|
|
FcChar8 *str;
|
|
|
|
for (int i = 0; FcPatternGetString(pat, field, i, &str) == FcResultMatch; ++i) {
|
|
|
|
std::string sstr((char *)str);
|
|
|
|
boost::to_lower(sstr);
|
|
|
|
if (sstr == name)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-11-28 18:30:29 +01:00
|
|
|
void find_font(FcFontSet *src, FcFontSet *dst, std::string const& family) {
|
|
|
|
if (!src) return;
|
|
|
|
|
|
|
|
for (FcPattern *pat : boost::make_iterator_range(&src->fonts[0], &src->fonts[src->nfont])) {
|
|
|
|
int val;
|
|
|
|
if (FcPatternGetBool(pat, FC_OUTLINE, 0, &val) != FcResultMatch || val != FcTrue) continue;
|
|
|
|
|
2014-08-24 21:38:28 +02:00
|
|
|
if (pattern_matches(pat, FC_FULLNAME, family) || pattern_matches(pat, FC_FAMILY, family))
|
|
|
|
FcFontSetAdd(dst, FcPatternDuplicate(pat));
|
2012-10-09 16:44:27 +02:00
|
|
|
}
|
2007-07-29 03:21:28 +02:00
|
|
|
}
|
|
|
|
|
2012-11-28 18:30:29 +01:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:31:53 +02:00
|
|
|
FontConfigFontFileLister::FontConfigFontFileLister(FontCollectorStatusCallback &cb)
|
2015-12-05 20:45:00 +01:00
|
|
|
: config(FcInitLoadConfig(), FcConfigDestroy)
|
2012-01-12 23:32:09 +01:00
|
|
|
{
|
|
|
|
cb(_("Updating font cache\n"), 0);
|
|
|
|
FcConfigBuildFonts(config);
|
|
|
|
}
|
2007-07-29 03:21:28 +02:00
|
|
|
|
2014-07-05 19:31:53 +02:00
|
|
|
CollectionResult FontConfigFontFileLister::GetFontPaths(std::string const& facename, int bold, bool italic, std::vector<int> const& characters) {
|
2012-02-02 20:18:40 +01:00
|
|
|
CollectionResult ret;
|
2007-09-22 17:27:46 +02:00
|
|
|
|
2014-06-29 19:10:46 +02:00
|
|
|
std::string family = facename[0] == '@' ? facename.substr(1) : facename;
|
2012-11-28 18:30:29 +01:00
|
|
|
boost::to_lower(family);
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2012-01-12 23:32:09 +01:00
|
|
|
int weight = bold == 0 ? 80 :
|
|
|
|
bold == 1 ? 200 :
|
|
|
|
bold;
|
|
|
|
int slant = italic ? 110 : 0;
|
2007-07-29 03:21:28 +02:00
|
|
|
|
2012-11-28 18:30:29 +01:00
|
|
|
// Create a fontconfig pattern to match the desired weight/slant
|
2012-02-20 19:22:12 +01:00
|
|
|
agi::scoped_holder<FcPattern*> pat(FcPatternCreate(), FcPatternDestroy);
|
2012-01-12 23:32:09 +01:00
|
|
|
if (!pat) return ret;
|
2007-07-29 03:21:28 +02:00
|
|
|
|
2012-01-12 23:32:09 +01:00
|
|
|
FcPatternAddBool(pat, FC_OUTLINE, true);
|
|
|
|
FcPatternAddInteger(pat, FC_SLANT, slant);
|
|
|
|
FcPatternAddInteger(pat, FC_WEIGHT, weight);
|
2007-07-29 03:21:28 +02:00
|
|
|
|
2012-01-12 23:32:09 +01:00
|
|
|
FcDefaultSubstitute(pat);
|
|
|
|
if (!FcConfigSubstitute(config, pat, FcMatchPattern)) return ret;
|
|
|
|
|
2012-11-28 18:30:29 +01:00
|
|
|
// Create a font set with only correctly named fonts
|
|
|
|
// This is needed because the patterns returned by font matching only
|
|
|
|
// include the first family and fullname, so we can't always verify that
|
|
|
|
// we got the actual font we were asking for after the fact
|
2012-02-20 19:22:12 +01:00
|
|
|
agi::scoped_holder<FcFontSet*> fset(FcFontSetCreate(), FcFontSetDestroy);
|
2012-11-28 18:30:29 +01:00
|
|
|
find_font(FcConfigGetFonts(config, FcSetApplication), fset, family);
|
|
|
|
find_font(FcConfigGetFonts(config, FcSetSystem), fset, family);
|
2012-01-12 23:32:09 +01:00
|
|
|
|
2012-11-28 18:30:29 +01:00
|
|
|
// Get the best match from fontconfig
|
|
|
|
FcResult result;
|
|
|
|
FcFontSet *sets[] = { (FcFontSet*)fset };
|
2014-08-24 21:38:28 +02:00
|
|
|
|
|
|
|
agi::scoped_holder<FcFontSet*> matches(FcFontSetSort(config, sets, 1, pat, false, nullptr, &result), FcFontSetDestroy);
|
|
|
|
if (matches->nfont == 0)
|
2012-12-28 16:32:55 +01:00
|
|
|
return ret;
|
2012-01-12 23:32:09 +01:00
|
|
|
|
2014-08-24 21:38:28 +02:00
|
|
|
auto match = matches->fonts[0];
|
|
|
|
|
2012-01-12 23:32:09 +01:00
|
|
|
FcChar8 *file;
|
2012-11-28 18:30:29 +01:00
|
|
|
if(FcPatternGetString(match, FC_FILE, 0, &file) != FcResultMatch)
|
2012-01-12 23:32:09 +01:00
|
|
|
return ret;
|
|
|
|
|
2012-02-01 05:17:33 +01:00
|
|
|
FcCharSet *charset;
|
2012-11-28 18:30:29 +01:00
|
|
|
if (FcPatternGetCharSet(match, FC_CHARSET, 0, &charset) == FcResultMatch) {
|
2014-07-05 19:31:53 +02:00
|
|
|
for (int chr : characters) {
|
2012-11-04 04:53:03 +01:00
|
|
|
if (!FcCharSetHasChar(charset, chr))
|
|
|
|
ret.missing += chr;
|
2012-02-01 05:17:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 16:13:32 +02:00
|
|
|
if (weight > 80) {
|
|
|
|
int actual_weight = weight;
|
|
|
|
if (FcPatternGetInteger(match, FC_WEIGHT, 0, &actual_weight) == FcResultMatch)
|
|
|
|
ret.fake_bold = actual_weight <= 80;
|
|
|
|
}
|
2014-04-18 01:07:01 +02:00
|
|
|
|
|
|
|
int actual_slant = slant;
|
|
|
|
if (FcPatternGetInteger(match, FC_SLANT, 0, &actual_slant) == FcResultMatch)
|
|
|
|
ret.fake_italic = italic && !actual_slant;
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
ret.paths.emplace_back((const char *)file);
|
2012-01-12 23:32:09 +01:00
|
|
|
return ret;
|
|
|
|
}
|