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
void FontSupport::matchFont(const WFont& font,
			    const std::vector<std::string>& fontNames,
			    const std::string& path,
			    FontMatch& match) const
{
  if (boost::ends_with(path, ".ttf")
      || boost::ends_with(path, ".ttc")) {
    std::string name = Utils::lowerCase(FileUtils::leaf(path));
    name = name.substr(0, name.length() - 4);
    Utils::replace(name, ' ', std::string());

    std::vector<const char*> weightVariants, styleVariants;

    if (font.weight() == WFont::Bold) {
      weightVariants.push_back("bold");
      weightVariants.push_back("bf");
    } else {
      weightVariants.push_back("");
    }

    switch (font.style()) {
    case WFont::NormalStyle: 
      styleVariants.push_back("regular");
      styleVariants.push_back(""); 
      break;
    case WFont::Italic:  
      styleVariants.push_back("italic");
      styleVariants.push_back("oblique");  
      break;
    case WFont::Oblique: 
      styleVariants.push_back("oblique"); 
      break;
    }

    for (unsigned i = 0; i < fontNames.size(); ++i) {
      double q = 1.0 - 0.1 * i;

      if (q <= match.quality())
	return;
      
      for (unsigned w = 0; w < weightVariants.size(); ++w)
	for (unsigned s = 0; s < styleVariants.size(); ++s) {
	  std::string fn = fontNames[i] + weightVariants[w] + styleVariants[s];
	  if (fn == name) {
	    match = FontMatch(path, q);
	    return;
	  }
	}
    }
  }
}