Logo Search packages:      
Sourcecode: codeblocks version File versions  Download package

bool wxPdfDocument::AddFont ( const wxString &  family,
const wxString &  style = wxEmptyString,
const wxString &  file = wxEmptyString 
) [virtual]

Imports a TrueType, TrueTypeUnicode or Type1 font and makes it available.

It is necessary to generate a font definition file first with the makefont utility. The definition file (and the font file itself when embedding) must be present either in the subdirectory 'fonts' of the current working directory or in the one indicated by WXPDF_FONTPATH if this environment variable is defined.

Parameters:
familyFont family. The name can be chosen arbitrarily. If it is a standard family name, it will override the corresponding font.
styleFont style. Possible values are (case insensitive):

  • empty string: regular (default)
  • B: bold
  • I: italic
  • BI or IB: bold italic
fileThe font definition file. By default, the name is built from the family and style, in lower case with no space.
See also:
SetFont(), SetFontPath()

Definition at line 603 of file pdfdoc.cpp.

References wxPdfFont::GetDiffs(), GetFontPath(), wxPdfFont::HasDiffs(), wxPdfFont::LoadFontMetrics(), m_PDFVersion, wxPdfFont::SetDiffIndex(), and wxPdfFont::SetFilePath().

{
  if (family.Length() == 0) return false;

  // Add a TrueType or Type1 font
  wxString lcFamily = family.Lower();
  wxString lcStyle = style.Lower();
  wxString ucStyle = style.Upper();

  wxString fileName = file;
  if (fileName.Length() == 0)
  {
    fileName = lcFamily + lcStyle + wxString(_T(".xml"));
    fileName.Replace(_T(" "),_T(""));
  }

  if (ucStyle == _T("IB"))
  {
    ucStyle = _T("BI");
  }

  // check if the font has been already added
  wxString fontkey = lcFamily + ucStyle;
  wxPdfFontHashMap::iterator font = (*m_fonts).find(fontkey);
  if (font != (*m_fonts).end())
  {
    // Font already loaded
    return true;
  }

  // Open font metrics XML file
  wxFileName fontFileName(fileName);
  fontFileName.MakeAbsolute(GetFontPath());
  wxFileSystem fs;
  wxFSFile* xmlFontMetrics = fs.OpenFile(fontFileName.GetFullPath());
  if (!xmlFontMetrics)
  {
    // Font metrics XML file not found
    wxLogDebug(_T("wxPdfDocument::AddFont: Font metrics file '%s' not found."), fileName.c_str());
    return false;
  }

  // Load the XML file
  wxXmlDocument fontMetrics;
  bool loaded = fontMetrics.Load(*xmlFontMetrics->GetStream());
  delete xmlFontMetrics;
  if (!loaded)
  {
    // Font metrics file loading failed
    wxLogDebug(_T("wxPdfDocument::AddFont: Loading of font metrics file '%s' failed."), fileName.c_str());
    return false;
  }
  if (!fontMetrics.IsOk() || fontMetrics.GetRoot()->GetName() != wxT("wxpdfdoc-font-metrics"))
  {
    // Not a font metrics file
    wxLogDebug(_T("wxPdfDocument::AddFont: Font metrics file '%s' invalid."), fileName.c_str());
    return false;
  }

  wxString fontType;
  wxXmlNode* root = fontMetrics.GetRoot();
  #if wxCHECK_VERSION(2, 9, 0)
  if (!root->GetAttribute(_T("type"), &fontType))
  #else
  if (!root->GetPropVal(_T("type"), &fontType))
  #endif
  {
    // Font type not specified
    wxLogDebug(_T("wxPdfDocument::AddFont: Font type not specified for font '%s'."), family.c_str());
    return false;
  }

  int i = (*m_fonts).size() + 1;
  wxPdfFont* addedFont = NULL;
  if (fontType == _T("TrueType"))
  {
    addedFont = new wxPdfFontTrueType(i);
  }
  else if (fontType == _T("Type1"))
  {
    addedFont = new wxPdfFontType1(i);
  }
#if wxUSE_UNICODE
  else if (fontType == _T("TrueTypeUnicode"))
  {
    addedFont = new wxPdfFontTrueTypeUnicode(i);
  }
  else if (fontType == _T("OpenTypeUnicode"))
  {
    addedFont = new wxPdfFontOpenTypeUnicode(i);
    if (m_PDFVersion < _T("1.6"))
    {
      m_PDFVersion = _T("1.6");
    }
  }
  else if (fontType == _T("Type0"))
  {
    addedFont = new wxPdfFontType0(i);
  }
#endif
  else
  {
    // Unknown font type
    wxLogDebug(_T("wxPdfDocument::AddFont: Unknown font type '%s'."), fontType.c_str());
    return false;
  }
  if (!addedFont->LoadFontMetrics(root))
  {
    delete addedFont;
    return false;
  }
  addedFont->SetFilePath(fontFileName.GetPath());
  (*m_fonts)[fontkey] = addedFont;

  if (addedFont->HasDiffs())
  {
    // Search existing encodings
    int d = 0;
    int nb = (*m_diffs).size();
    for (i = 1; i <= nb; i++)
    {
      if (*(*m_diffs)[i] == addedFont->GetDiffs())
      {
        d = i;
        break;
      }
    }
    if (d == 0)
    {
      d = nb + 1;
      (*m_diffs)[d] = new wxString(addedFont->GetDiffs());
    }
    addedFont->SetDiffIndex(d);
  }

  return true;
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index