<commit_before>// Filename: fontPool.cxx
// Created by:  drose (31Jan03)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license.  You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://etc.cmu.edu/panda3d/docs/license/ .
//
// To contact the maintainers of this program write to
// <EMAIL> .
//
////////////////////////////////////////////////////////////////////

#include "fontPool.h"
#include "staticTextFont.h"
#include "dynamicTextFont.h"
#include "config_util.h"
#include "config_express.h"
#include "virtualFileSystem.h"
#include "nodePath.h"
#include "loader.h"
#include "mutexHolder.h"

FontPool *FontPool::_global_ptr = (FontPool *)NULL;

static Loader model_loader;

////////////////////////////////////////////////////////////////////
//     Function: FontPool::write
//       Access: Published, Static
//  Description: Lists the contents of the font pool to the
//               indicated output stream.
////////////////////////////////////////////////////////////////////
void FontPool::
write(ostream &out) {
  get_ptr()->ns_list_contents(out);
}

////////////////////////////////////////////////////////////////////
//     Function: FontPool::ns_has_font
//       Access: Private
//  Description: The nonstatic implementation of has_font().
////////////////////////////////////////////////////////////////////
bool FontPool::
ns_has_font(const string &str) {
  MutexHolder holder(_lock);

  string index_str;
  Filename filename;
  int face_index;
  lookup_filename(str, index_str, filename, face_index);

  Fonts::const_iterator ti;
  ti = _fonts.find(index_str);
  if (ti != _fonts.end()) {
    // This font was previously loaded.
    return true;
  }

  return false;
}

////////////////////////////////////////////////////////////////////
//     Function: FontPool::ns_load_font
//       Access: Private
//  Description: The nonstatic implementation of load_font().
////////////////////////////////////////////////////////////////////
TextFont *FontPool::
ns_load_font(const string &str) {
  string index_str;
  Filename filename;
  int face_index;
  lookup_filename(str, index_str, filename, face_index);

  {
    MutexHolder holder(_lock);
    
    Fonts::const_iterator ti;
    ti = _fonts.find(index_str);
    if (ti != _fonts.end()) {
      // This font was previously loaded.
      return (*ti).second;
    }
  }

  text_cat.info()
    << "Loading font " << filename << "\n";

  // Now, figure out how to load the font.  If its filename extension
  // is "egg" or "bam", or if it's unspecified, assume it's a model
  // file, representing a static font.
  PT(TextFont) font;

  string extension = filename.get_extension();
  if (extension.empty() || extension == "egg" || extension == "bam") {
    PT(PandaNode) node = model_loader.load_sync(filename);
    if (node != (PandaNode *)NULL) {
      // It is a model.  Elevate all the priorities by 1, and make a
      // font out of it.
      NodePath np(node);
      np.adjust_all_priorities(1);
      font = new StaticTextFont(node);
    }
  }

#ifdef HAVE_FREETYPE
  if (font == (TextFont *)NULL || !font->is_valid()) {
    // If we couldn't load the font as a model, try using FreeType to
    // load it as a font file.
    font = new DynamicTextFont(filename, face_index);
  }
#endif

  if (font == (TextFont *)NULL || !font->is_valid()) {
    // This font was not found or could not be read.
    return NULL;
  }


  {
    MutexHolder holder(_lock);

    // Look again.  It may have been loaded by another thread.
    Fonts::const_iterator ti;
    ti = _fonts.find(index_str);
    if (ti != _fonts.end()) {
      // This font was previously loaded.
      return (*ti).second;
    }<commit_msg>fix more white text problems<commit_after>// Filename: fontPool.cxx
// Created by:  drose (31Jan03)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license.  You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://etc.cmu.edu/panda3d/docs/license/ .
//
// To contact the maintainers of this program write to
// <EMAIL> .
//
////////////////////////////////////////////////////////////////////

#include "fontPool.h"
#include "staticTextFont.h"
#include "dynamicTextFont.h"
#include "config_util.h"
#include "config_express.h"
#include "virtualFileSystem.h"
#include "nodePath.h"
#include "loader.h"
#include "mutexHolder.h"

FontPool *FontPool::_global_ptr = (FontPool *)NULL;

static Loader model_loader;

////////////////////////////////////////////////////////////////////
//     Function: FontPool::write
//       Access: Published, Static
//  Description: Lists the contents of the font pool to the
//               indicated output stream.
////////////////////////////////////////////////////////////////////
void FontPool::
write(ostream &out) {
  get_ptr()->ns_list_contents(out);
}

////////////////////////////////////////////////////////////////////
//     Function: FontPool::ns_has_font
//       Access: Private
//  Description: The nonstatic implementation of has_font().
////////////////////////////////////////////////////////////////////
bool FontPool::
ns_has_font(const string &str) {
  MutexHolder holder(_lock);

  string index_str;
  Filename filename;
  int face_index;
  lookup_filename(str, index_str, filename, face_index);

  Fonts::const_iterator ti;
  ti = _fonts.find(index_str);
  if (ti != _fonts.end()) {
    // This font was previously loaded.
    return true;
  }

  return false;
}

////////////////////////////////////////////////////////////////////
//     Function: FontPool::ns_load_font
//       Access: Private
//  Description: The nonstatic implementation of load_font().
////////////////////////////////////////////////////////////////////
TextFont *FontPool::
ns_load_font(const string &str) {
  string index_str;
  Filename filename;
  int face_index;
  lookup_filename(str, index_str, filename, face_index);

  {
    MutexHolder holder(_lock);
    
    Fonts::const_iterator ti;
    ti = _fonts.find(index_str);
    if (ti != _fonts.end()) {
      // This font was previously loaded.
      return (*ti).second;
    }
  }

  text_cat.info()
    << "Loading font " << filename << "\n";

  // Now, figure out how to load the font.  If its filename extension
  // is "egg" or "bam", or if it's unspecified, assume it's a model
  // file, representing a static font.
  PT(TextFont) font;

  string extension = filename.get_extension();
  if (extension.empty() || extension == "egg" || extension == "bam") {
    PT(PandaNode) node = model_loader.load_sync(filename);
    if (node != (PandaNode *)NULL) {
      // It is a model.  Elevate all the priorities by 1, and make a
      // font out of it.

      // On second thought, why should we elevate the priorities?  The
      // DynamicTextFont doesn't do this, and doing so for the
      // StaticTextFont only causes problems (it changes the default
      // ColorAttrib from pri -1 to pri 0).
      /*
      NodePath np(node);
      np.adjust_all_priorities(1);
      */

      font = new StaticTextFont(node);
    }
  }

#ifdef HAVE_FREETYPE
  if (font == (TextFont *)NULL || !font->is_valid()) {
    // If we couldn't load the font as a model, try using FreeType to
    // load it as a font file.
    font = new DynamicTextFont(filename, face_index);
  }
#endif

  if (font == (TextFont *)NULL || !font->is_valid()) {
    // This font was not found or could not be read.
    return NULL;
  }


  {
    MutexHolder holder(_lock);

    // Look again.  It may have been loaded by another thread.
    Fonts::const_iterator ti;
    ti = _fonts.find(index_str);
    if (ti != _fonts.end()) {
      // This font was previously loaded.
      return (*ti).second;
    }
{'fraction_non_alphanumeric': 0.23831548198636807, 'fraction_numerical': 0.004381694255111977, 'mean_word_length': 1.8471933471933473, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4303693', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2263, 'n_words': 744}