FontInfo.cc 6.58 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>
Albert Astals Cid's avatar
Albert Astals Cid committed
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 38 39 40 41 42 43 44 45 46
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <math.h>
#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() {
}

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

  if (currentPage > doc->getNumPages()) {
62
    return nullptr;
63 64
  }
 
Oliver Sander's avatar
Oliver Sander committed
65
  auto result = new std::vector<FontInfo*>();
66 67

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

Thomas Freitag's avatar
Thomas Freitag committed
72
  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
    if ((resDict = page->getResourceDictCopy(xrefA))) {
Thomas Freitag's avatar
Thomas Freitag committed
78 79
      scanFonts(xrefA, resDict, result);
      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()) {
Thomas Freitag's avatar
Thomas Freitag committed
85
        scanFonts(xrefA, obj1.getDict(), result);
86 87 88 89
      }
    }
  }

90
  currentPage = lastPage;
91

Thomas Freitag's avatar
Thomas Freitag committed
92
  delete xrefA;
93 94 95
  return result;
}

Oliver Sander's avatar
Oliver Sander committed
96
void FontInfoScanner::scanFonts(XRef *xrefA, Dict *resDict, std::vector<FontInfo*> *fontsList) {
97 98 99 100
  GfxFontDict *gfxFontDict;
  GfxFont *font;

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

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

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

Adrian Johnson's avatar
Adrian Johnson committed
142
        if (obj2.isStream()) {
143 144 145 146
          Ref resourcesRef;
          const Object resObj = obj2.streamGetDict()->lookup("Resources", &resourcesRef);

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

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

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

  fontRef = *font->getID();

  // font name
167
  origName = font->getName();
168
  if (origName != nullptr) {
169
    name = font->getName()->copy();
170
  } else {
171
    name = nullptr;
172
  }
Albert Astals Cid's avatar
Albert Astals Cid committed
173

Albert Astals Cid's avatar
Albert Astals Cid committed
174
  // font type
175
  type = (FontInfo::Type)font->getType();
176 177 178

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

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

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

  // check for a font subset name: capital letters followed by a '+'
  // sign
206
  subset = false;
207
  if (name) {
Albert Astals Cid's avatar
Albert Astals Cid committed
208
    int i;
209 210 211 212 213 214 215 216 217 218
    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) == '+';
  }
}

FontInfo::FontInfo(FontInfo& f) {
219 220 221 222
  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;
223
  type = f.type;
224 225 226 227
  emb = f.emb;
  subset = f.subset;
  hasToUnicode = f.hasToUnicode;
  fontRef = f.fontRef;
228
  embRef = f.embRef;
229 230 231 232
}

FontInfo::~FontInfo() {
  delete name;
Albert Astals Cid's avatar
Albert Astals Cid committed
233
  delete file;
234
  delete encoding;
235 236
  if (substituteName)
    delete substituteName;
237
}