"Fossies" - the Fresh Open Source Software Archive

Member "poppler-0.82.0/splash/SplashFontEngine.cc" (25 Oct 2019, 7664 Bytes) of package /linux/misc/poppler-0.82.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "SplashFontEngine.cc" see the Fossies "Dox" file reference documentation.

    1 //========================================================================
    2 //
    3 // SplashFontEngine.cc
    4 //
    5 //========================================================================
    6 
    7 //========================================================================
    8 //
    9 // Modified under the Poppler project - http://poppler.freedesktop.org
   10 //
   11 // All changes made under the Poppler project to this file are licensed
   12 // under GPL version 2 or later
   13 //
   14 // Copyright (C) 2006 Takashi Iwai <tiwai@suse.de>
   15 // Copyright (C) 2009 Petr Gajdos <pgajdos@novell.com>
   16 // Copyright (C) 2009 Kovid Goyal <kovid@kovidgoyal.net>
   17 // Copyright (C) 2009 Albert Astals Cid <aacid@kde.org>
   18 // Copyright (C) 2011 Andreas Hartmetz <ahartmetz@gmail.com>
   19 // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
   20 // Copyright (C) 2015 Dmytro Morgun <lztoad@gmail.com>
   21 // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
   22 // Copyright (C) 2018 Oliver Sander <oliver.sander@tu-dresden.de>
   23 // Copyright (C) 2019 Christian Persch <chpe@src.gnome.org>
   24 //
   25 // To see a description of the changes please see the Changelog file that
   26 // came with your tarball or type make ChangeLog if you are building from git
   27 //
   28 //========================================================================
   29 
   30 #include <config.h>
   31 
   32 #include <stdlib.h>
   33 #include <stdio.h>
   34 #ifdef HAVE_UNISTD_H
   35 #  include <unistd.h>
   36 #endif
   37 #include <algorithm>
   38 
   39 #include "goo/gmem.h"
   40 #include "goo/GooString.h"
   41 #include "SplashMath.h"
   42 #include "SplashFTFontEngine.h"
   43 #include "SplashFontFile.h"
   44 #include "SplashFontFileID.h"
   45 #include "SplashFont.h"
   46 #include "SplashFontEngine.h"
   47 
   48 //------------------------------------------------------------------------
   49 // SplashFontEngine
   50 //------------------------------------------------------------------------
   51 
   52 SplashFontEngine::SplashFontEngine(
   53                    bool enableFreeType,
   54                    bool enableFreeTypeHinting,
   55                    bool enableSlightHinting,
   56                    bool aa) {
   57   std::fill(fontCache.begin(), fontCache.end(), nullptr);
   58 
   59   if (enableFreeType) {
   60     ftEngine = SplashFTFontEngine::init(aa, enableFreeTypeHinting, enableSlightHinting);
   61   } else {
   62     ftEngine = nullptr;
   63   }
   64 }
   65 
   66 SplashFontEngine::~SplashFontEngine() {
   67   for (auto font : fontCache) {
   68     delete font;
   69   }
   70 
   71   if (ftEngine) {
   72     delete ftEngine;
   73   }
   74 }
   75 
   76 SplashFontFile *SplashFontEngine::getFontFile(SplashFontFileID *id) {
   77   for (auto font : fontCache) {
   78     if (font) {
   79       SplashFontFile *fontFile = font->getFontFile();
   80       if (fontFile && fontFile->getID()->matches(id)) {
   81     return fontFile;
   82       }
   83     }
   84   }
   85   return nullptr;
   86 }
   87 
   88 SplashFontFile *SplashFontEngine::loadType1Font(SplashFontFileID *idA,
   89                         SplashFontSrc *src,
   90                         const char **enc) {
   91   SplashFontFile *fontFile = nullptr;
   92 
   93   if (ftEngine) {
   94     fontFile = ftEngine->loadType1Font(idA, src, enc);
   95   }
   96 
   97   // delete the (temporary) font file -- with Unix hard link
   98   // semantics, this will remove the last link; otherwise it will
   99   // return an error, leaving the file to be deleted later (if
  100   // loadXYZFont failed, the file will always be deleted)
  101   if (src->isFile)
  102     src->unref();
  103 
  104   return fontFile;
  105 }
  106 
  107 SplashFontFile *SplashFontEngine::loadType1CFont(SplashFontFileID *idA,
  108                          SplashFontSrc *src,
  109                          const char **enc) {
  110   SplashFontFile *fontFile = nullptr;
  111 
  112   if (ftEngine) {
  113     fontFile = ftEngine->loadType1CFont(idA, src, enc);
  114   }
  115 
  116   // delete the (temporary) font file -- with Unix hard link
  117   // semantics, this will remove the last link; otherwise it will
  118   // return an error, leaving the file to be deleted later (if
  119   // loadXYZFont failed, the file will always be deleted)
  120   if (src->isFile)
  121     src->unref();
  122 
  123   return fontFile;
  124 }
  125 
  126 SplashFontFile *SplashFontEngine::loadOpenTypeT1CFont(SplashFontFileID *idA,
  127                               SplashFontSrc *src,
  128                               const char **enc) {
  129   SplashFontFile *fontFile = nullptr;
  130 
  131   if (ftEngine) {
  132     fontFile = ftEngine->loadOpenTypeT1CFont(idA, src, enc);
  133   }
  134 
  135   // delete the (temporary) font file -- with Unix hard link
  136   // semantics, this will remove the last link; otherwise it will
  137   // return an error, leaving the file to be deleted later (if
  138   // loadXYZFont failed, the file will always be deleted)
  139   if (src->isFile)
  140     src->unref();
  141 
  142   return fontFile;
  143 }
  144 
  145 SplashFontFile *SplashFontEngine::loadCIDFont(SplashFontFileID *idA,
  146                           SplashFontSrc *src) {
  147   SplashFontFile *fontFile = nullptr;
  148 
  149   if (ftEngine) {
  150     fontFile = ftEngine->loadCIDFont(idA, src);
  151   }
  152 
  153   // delete the (temporary) font file -- with Unix hard link
  154   // semantics, this will remove the last link; otherwise it will
  155   // return an error, leaving the file to be deleted later (if
  156   // loadXYZFont failed, the file will always be deleted)
  157   if (src->isFile)
  158     src->unref();
  159 
  160   return fontFile;
  161 }
  162 
  163 SplashFontFile *SplashFontEngine::loadOpenTypeCFFFont(SplashFontFileID *idA,
  164                               SplashFontSrc *src,
  165                                                       int *codeToGID,
  166                                                       int codeToGIDLen) {
  167   SplashFontFile *fontFile = nullptr;
  168 
  169   if (ftEngine) {
  170     fontFile = ftEngine->loadOpenTypeCFFFont(idA, src, codeToGID, codeToGIDLen);
  171   }
  172 
  173   // delete the (temporary) font file -- with Unix hard link
  174   // semantics, this will remove the last link; otherwise it will
  175   // return an error, leaving the file to be deleted later (if
  176   // loadXYZFont failed, the file will always be deleted)
  177   if (src->isFile)
  178     src->unref();
  179 
  180   return fontFile;
  181 }
  182 
  183 SplashFontFile *SplashFontEngine::loadTrueTypeFont(SplashFontFileID *idA,
  184                            SplashFontSrc *src,
  185                            int *codeToGID,
  186                            int codeToGIDLen,
  187                            int faceIndex) {
  188   SplashFontFile *fontFile = nullptr;
  189 
  190   if (ftEngine) {
  191     fontFile = ftEngine->loadTrueTypeFont(idA, src,
  192                                         codeToGID, codeToGIDLen, faceIndex);
  193   }
  194 
  195   if (!fontFile) {
  196     gfree(codeToGID);
  197   }
  198 
  199   // delete the (temporary) font file -- with Unix hard link
  200   // semantics, this will remove the last link; otherwise it will
  201   // return an error, leaving the file to be deleted later (if
  202   // loadXYZFont failed, the file will always be deleted)
  203   if (src->isFile)
  204     src->unref();
  205 
  206   return fontFile;
  207 }
  208 
  209 bool SplashFontEngine::getAA() {
  210   return (ftEngine == nullptr) ? false : ftEngine->getAA();
  211 }
  212 
  213 void SplashFontEngine::setAA(bool aa) {
  214   if (ftEngine != nullptr) {
  215     ftEngine->setAA(aa);
  216   }
  217 }
  218 
  219 SplashFont *SplashFontEngine::getFont(SplashFontFile *fontFile,
  220                       const SplashCoord *textMat,
  221                       const SplashCoord *ctm) {
  222   SplashCoord mat[4];
  223 
  224   mat[0] = textMat[0] * ctm[0] + textMat[1] * ctm[2];
  225   mat[1] = -(textMat[0] * ctm[1] + textMat[1] * ctm[3]);
  226   mat[2] = textMat[2] * ctm[0] + textMat[3] * ctm[2];
  227   mat[3] = -(textMat[2] * ctm[1] + textMat[3] * ctm[3]);
  228   if (!splashCheckDet(mat[0], mat[1], mat[2], mat[3], 0.01)) {
  229     // avoid a singular (or close-to-singular) matrix
  230     mat[0] = 0.01;  mat[1] = 0;
  231     mat[2] = 0;     mat[3] = 0.01;
  232   }
  233 
  234   // Try to find the font in the cache
  235   auto fontIt = std::find_if(fontCache.begin(), fontCache.end(),
  236                              [&](const SplashFont* font){return font && font->matches(fontFile, mat, textMat);}
  237                             );
  238 
  239   // The requested font has been found in the cache
  240   if (fontIt != fontCache.end()) {
  241     std::rotate(fontCache.begin(), fontIt, fontIt+1);
  242     return fontCache[0];
  243   }
  244 
  245   // The requested font has not been found in the cache
  246   auto newFont = fontFile->makeFont(mat, textMat);
  247   if (fontCache.back()) {
  248     delete fontCache.back();
  249   }
  250   std::rotate(fontCache.begin(), fontCache.end()-1, fontCache.end());
  251 
  252   fontCache[0] = newFont;
  253   return fontCache[0];
  254 }