Example #1
0
FontSupport::FontMatch FontSupport::matchFont(const WFont& font) const
{
  for (MatchCache::iterator i = cache_.begin(); i != cache_.end(); ++i) {
    if (i->font.genericFamily() == font.genericFamily() &&
	i->font.specificFamilies() == font.specificFamilies() &&
	i->font.weight() == font.weight() &&
	i->font.style() == font.style()) {
      cache_.splice(cache_.begin(), cache_, i); // implement LRU
      return cache_.front().match;
    }
  }

  FontMatch match;

  for (unsigned i = 0; i < fontCollections_.size(); ++i) {
    FontMatch m = matchFont(font, fontCollections_[i].directory,
			    fontCollections_[i].recursive);

    if (m.quality() > match.quality())
      match = m;
  }

  // implement LRU
  cache_.pop_back();
  cache_.push_front(Matched());
  cache_.front().font = font;
  cache_.front().match = match;

  return match;
}
Example #2
0
FontSupport::FontMatch FontSupport::matchFont(const WFont& font,
					      const std::string& directory,
					      bool recursive) const
{
  if (!FileUtils::exists(directory)
      || !FileUtils::isDirectory(directory)) {
    LOG_ERROR("cannot read directory '" << directory << "'");
    return FontMatch();
  }

  std::vector<std::string> fontNames;
  std::string families = font.specificFamilies().toUTF8();

  boost::split(fontNames, families, boost::is_any_of(","));
  for (unsigned i = 0; i < fontNames.size(); ++i) {
    std::string s = Utils::lowerCase(fontNames[i]); // UTF-8 !
    boost::trim_if(s, boost::is_any_of("\"'"));
    s = Utils::replace(s, ' ', std::string());
    fontNames[i] = s;
  }

  switch (font.genericFamily()) {
  case WFont::Serif:
    fontNames.push_back("times");
    fontNames.push_back("timesnewroman");
    break;
  case WFont::SansSerif:
    fontNames.push_back("helvetica");
    fontNames.push_back("arialunicode");
    fontNames.push_back("arial");
    fontNames.push_back("verdana");
    break;
  case WFont::Cursive:
    fontNames.push_back("zapfchancery");
    break;
  case WFont::Fantasy:
    fontNames.push_back("western");
    break;
  case WFont::Monospace:
    fontNames.push_back("courier");
    fontNames.push_back("mscouriernew");
    break;
  default:
    ;
  }

  FontMatch match;
  matchFont(font, fontNames, directory, recursive, match);

  return match;
}