FontInfo.cc 6.52 KB
Newer Older
1 2 3 4 5
//========================================================================
//
// FontInfo.cc
//
// Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com>
6
// Copyright (C) 2005-2008, 2010, 2017-2019 Albert Astals Cid <aacid@kde.org>
7 8
// Copyright (C) 2005 Brad Hards <bradh@frogmouth.net>
// Copyright (C) 2006 Kouhei Sutou <kou@cozmixng.org>
9
// Copyright (C) 2009 Pino Toscano <pino@kde.org>
10
// Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
Albert Astals Cid's avatar
Albert Astals Cid committed
11
// Copyright (C) 2010, 2012 Adrian Johnson <ajohnson@redneon.com>
Thomas Freitag's avatar
Thomas Freitag committed
12
// Copyright (C) 2010, 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
Albert Astals Cid's avatar
Albert Astals Cid committed
13
// Copyright (C) 2011 Carlos Garcia Campos <carlosgc@gnome.org>
14
// Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
15
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
16
// Copyright (C) 2018, 2019 Adam Reichold <adam.reichold@t-online.de>
17
// Copyright (C) 2019 Oliver Sander <oliver.sander@tu-dresden.de>
18 19 20 21 22 23 24 25 26 27 28 29 30 31
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================

//========================================================================
//
// Based on code from pdffonts.cc
//
// Copyright 2001-2007 Glyph & Cog, LLC
//
//========================================================================

32
#include "config.h"
33 34 35 36 37
#include <cstdio>
#include <cstdlib>
#include <cstddef>
#include <cstring>
#include <cmath>
38 39 40 41 42 43 44 45 46
#include "GlobalParams.h"
#include "Error.h"
#include "Object.h"
#include "Dict.h"
#include "GfxFont.h"
#include "Annot.h"
#include "PDFDoc.h"
#include "FontInfo.h"

47
FontInfoScanner::FontInfoScanner(PDFDoc *docA, int firstPage) {
48
  doc = docA;
49
  currentPage = firstPage + 1;
50 51 52 53 54
}

FontInfoScanner::~FontInfoScanner() {
}

55
std::vector<FontInfo*> FontInfoScanner::scan(int nPages) {
56 57 58
  Page *page;
  Dict *resDict;
  Annots *annots;
59
  int lastPage;
60

61 62
  std::vector<FontInfo*> result;

63
  if (currentPage > doc->getNumPages()) {
64
    return result;
65 66 67
  }

  lastPage = currentPage + nPages;
68 69
  if (lastPage > doc->getNumPages() + 1) {
    lastPage = doc->getNumPages() + 1;
70 71
  }

72
  std::unique_ptr<XRef> xrefA(doc->getXRef()->copy());
73
  for (int pg = currentPage; pg < lastPage; ++pg) {
Hib Eris's avatar
Hib Eris committed
74 75 76
    page = doc->getPage(pg);
    if (!page) continue;

77 78
    if ((resDict = page->getResourceDictCopy(xrefA.get()))) {
      scanFonts(xrefA.get(), resDict, &result);
Thomas Freitag's avatar
Thomas Freitag committed
79
      delete resDict;
80
    }
81
    annots = page->getAnnots();
82
    for (int i = 0; i < annots->getNumAnnots(); ++i) {
Albert Astals Cid's avatar
Albert Astals Cid committed
83 84
      Object obj1 = annots->getAnnot(i)->getAppearanceResDict();
      if (obj1.isDict()) {
85
        scanFonts(xrefA.get(), obj1.getDict(), &result);
86 87 88 89
      }
    }
  }

90
  currentPage = lastPage;
91 92 93 94

  return result;
}

95
void FontInfoScanner::scanFonts(XRef *xrefA, Dict *resDict, std::vector<FontInfo*> *fontsList) {
96 97 98 99
  GfxFontDict *gfxFontDict;
  GfxFont *font;

  // scan the fonts in this resource dictionary
100
  gfxFontDict = nullptr;
101
  const Object &fontObj = resDict->lookupNF("Font");
102 103
  if (fontObj.isRef()) {
    Object obj2 = fontObj.fetch(xrefA);
104
    if (obj2.isDict()) {
105
      Ref r = fontObj.getRef();
Thomas Freitag's avatar
Thomas Freitag committed
106
      gfxFontDict = new GfxFontDict(xrefA, &r, obj2.getDict());
107
    }
108 109
  } else if (fontObj.isDict()) {
    gfxFontDict = new GfxFontDict(xrefA, nullptr, fontObj.getDict());
110 111
  }
  if (gfxFontDict) {
Albert Astals Cid's avatar
Albert Astals Cid committed
112
    for (int i = 0; i < gfxFontDict->getNumFonts(); ++i) {
113 114 115
      if ((font = gfxFontDict->getFont(i))) {
        Ref fontRef = *font->getID();

116
        // add this font to the list if not already found
117
	if (fonts.insert(fontRef.num).second) {
118
	  fontsList->push_back(new FontInfo(font, xrefA));
119 120 121 122 123 124 125 126
        }
      }
    }
    delete gfxFontDict;
  }

  // recursively scan any resource dictionaries in objects in this
  // resource dictionary
127
  const char *resTypes[] = { "XObject", "Pattern" };
128 129
  for (const char *resType : resTypes) {
    Object objDict = resDict->lookup(resType);
130
    if (objDict.isDict()) {
Albert Astals Cid's avatar
Albert Astals Cid committed
131
      for (int i = 0; i < objDict.dictGetLength(); ++i) {
132 133 134
        Ref obj2Ref;
        const Object obj2 = objDict.getDict()->getVal(i, &obj2Ref);
        if (obj2Ref != Ref::INVALID()) {
135
          // check for an already-seen object
136
	  if (!visitedObjects.insert(obj2Ref.num).second) {
137
            continue;
138
	  }
139 140
        }

141
        if (obj2.isStream()) {
142 143 144 145
          Ref resourcesRef;
          const Object resObj = obj2.streamGetDict()->lookup("Resources", &resourcesRef);

          if (resourcesRef != Ref::INVALID()) {
146
	    if (!visitedObjects.insert(resourcesRef.num).second) {
147
              continue;
148
	    }
149 150
          }

151
          if (resObj.isDict() && resObj.getDict() != resDict) {
Thomas Freitag's avatar
Thomas Freitag committed
152
            scanFonts(xrefA, resObj.getDict(), fontsList);
153
          }
154
        }
155 156 157 158 159
      }
    }
  }
}

Thomas Freitag's avatar
Thomas Freitag committed
160
FontInfo::FontInfo(GfxFont *font, XRef *xref) {
161
  const GooString *origName;
162 163 164 165

  fontRef = *font->getID();

  // font name
166
  origName = font->getName();
167
  if (origName != nullptr) {
168
    name = font->getName()->copy();
169
  } else {
170
    name = nullptr;
171
  }
172

173
  // font type
174
  type = (FontInfo::Type)font->getType();
175 176 177

  // check for an embedded font
  if (font->getType() == fontType3) {
178
    emb = true;
179 180 181 182
  } else {
    emb = font->getEmbeddedFontID(&embRef);
  }

183 184
  file = nullptr;
  substituteName = nullptr;
185 186
  if (!emb)
  {
187 188 189 190 191 192
    SysFontType dummy;
    int dummy2;
    GooString substituteNameAux;
    file = globalParams->findSystemFontFile(font, &dummy, &dummy2, &substituteNameAux);
    if (substituteNameAux.getLength() > 0)
	substituteName = substituteNameAux.copy();
193
  }
194
  encoding = font->getEncodingName()->copy();
195

196
  // look for a ToUnicode map
197
  hasToUnicode = false;
198
  Object fontObj = xref->fetch(fontRef);
Albert Astals Cid's avatar
Albert Astals Cid committed
199 200
  if (fontObj.isDict()) {
    hasToUnicode = fontObj.dictLookup("ToUnicode").isStream();
201 202 203 204
  }

  // check for a font subset name: capital letters followed by a '+'
  // sign
205
  subset = false;
206
  if (name) {
Albert Astals Cid's avatar
Albert Astals Cid committed
207
    int i;
208 209 210 211 212 213 214 215 216
    for (i = 0; i < name->getLength(); ++i) {
      if (name->getChar(i) < 'A' || name->getChar(i) > 'Z') {
	break;
      }
    }
    subset = i > 0 && i < name->getLength() && name->getChar(i) == '+';
  }
}

217
FontInfo::FontInfo(const FontInfo& f) {
218 219 220 221
  name = f.name ? f.name->copy() : nullptr;
  file = f.file ? f.file->copy() : nullptr;
  encoding = f.encoding ? f.encoding->copy() : nullptr;
  substituteName = f.substituteName ? f.substituteName->copy() : nullptr;
222
  type = f.type;
223 224 225 226
  emb = f.emb;
  subset = f.subset;
  hasToUnicode = f.hasToUnicode;
  fontRef = f.fontRef;
227
  embRef = f.embRef;
228 229 230 231
}

FontInfo::~FontInfo() {
  delete name;
232
  delete file;
233
  delete encoding;
234 235
  if (substituteName)
    delete substituteName;
236
}