2013-08-21 16:31:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2013-10-11 18:26:45 +00:00
|
|
|
// TODO(edisonn): this file not commented much on purpose.
|
|
|
|
// It will probably need heavy refactoring soon anyway to support all encodings, fonts and
|
|
|
|
// proper text sizing and spacing
|
|
|
|
|
2013-08-21 16:31:37 +00:00
|
|
|
#ifndef SkPdfFont_DEFINED
|
|
|
|
#define SkPdfFont_DEFINED
|
2013-06-21 21:43:09 +00:00
|
|
|
|
2013-11-20 22:33:39 +00:00
|
|
|
#include "SkPdfContext.h"
|
2013-06-21 21:43:09 +00:00
|
|
|
#include "SkPdfHeaders_autogen.h"
|
2013-07-02 22:42:53 +00:00
|
|
|
#include "SkPdfMapper_autogen.h"
|
2013-06-25 20:45:40 +00:00
|
|
|
#include "SkPdfUtils.h"
|
2013-10-10 20:58:22 +00:00
|
|
|
#include "SkTypeface.h"
|
2013-08-16 15:05:08 +00:00
|
|
|
#include "SkTDict.h"
|
2013-10-10 20:58:22 +00:00
|
|
|
#include "SkUtils.h"
|
2013-06-21 21:43:09 +00:00
|
|
|
|
|
|
|
class SkPdfType0Font;
|
|
|
|
class SkPdfType1Font;
|
|
|
|
class SkPdfType3Font;
|
|
|
|
class SkPdfTrueTypeFont;
|
|
|
|
class SkPdfMultiMasterFont;
|
|
|
|
class SkPdfFont;
|
|
|
|
|
|
|
|
struct SkPdfStandardFontEntry {
|
2013-07-10 17:44:55 +00:00
|
|
|
// We don't own this pointer!
|
2013-06-21 21:43:09 +00:00
|
|
|
const char* fName;
|
|
|
|
bool fIsBold;
|
|
|
|
bool fIsItalic;
|
2013-07-10 17:44:55 +00:00
|
|
|
SkPdfStandardFontEntry()
|
|
|
|
: fName(NULL),
|
|
|
|
fIsBold(false),
|
|
|
|
fIsItalic(false) {}
|
|
|
|
|
|
|
|
SkPdfStandardFontEntry(const char* name, bool bold, bool italic)
|
|
|
|
: fName(name),
|
|
|
|
fIsBold(bold),
|
|
|
|
fIsItalic(italic) {}
|
2013-06-21 21:43:09 +00:00
|
|
|
};
|
|
|
|
|
2013-08-16 15:05:08 +00:00
|
|
|
SkTDict<SkPdfStandardFontEntry>& getStandardFonts();
|
2013-06-21 21:43:09 +00:00
|
|
|
SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic);
|
2013-08-14 18:26:20 +00:00
|
|
|
SkPdfFont* fontFromName(SkPdfNativeDoc* doc, SkPdfNativeObject* obj, const char* fontName);
|
2013-06-21 21:43:09 +00:00
|
|
|
|
|
|
|
struct SkUnencodedText {
|
|
|
|
void* text;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
public:
|
2013-07-02 22:42:53 +00:00
|
|
|
SkUnencodedText(const SkPdfString* obj) {
|
|
|
|
text = (void*)obj->c_str();
|
2013-12-02 20:58:12 +00:00
|
|
|
len = (int) obj->lenstr();
|
2013-06-21 21:43:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SkDecodedText {
|
|
|
|
uint16_t* text;
|
|
|
|
int len;
|
2013-06-25 20:45:40 +00:00
|
|
|
public:
|
|
|
|
unsigned int operator[](int i) const { return text[i]; }
|
|
|
|
int size() const { return len; }
|
2013-06-21 21:43:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SkUnicodeText {
|
|
|
|
uint16_t* text;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
public:
|
|
|
|
unsigned int operator[](int i) const { return text[i]; }
|
|
|
|
int size() const { return len; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkPdfEncoding {
|
|
|
|
public:
|
2013-11-20 14:29:51 +00:00
|
|
|
virtual ~SkPdfEncoding() {}
|
2013-06-21 21:43:09 +00:00
|
|
|
virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const = 0;
|
2013-06-25 20:45:40 +00:00
|
|
|
static SkPdfEncoding* fromName(const char* name);
|
|
|
|
};
|
|
|
|
|
2013-08-16 15:05:08 +00:00
|
|
|
SkTDict<SkPdfEncoding*>& getStandardEncodings();
|
2013-06-25 20:45:40 +00:00
|
|
|
|
|
|
|
class SkPdfToUnicode {
|
|
|
|
// TODO(edisonn): hide public members
|
|
|
|
public:
|
|
|
|
unsigned short* fCMapEncoding;
|
|
|
|
unsigned char* fCMapEncodingFlag;
|
|
|
|
|
2013-08-14 18:26:20 +00:00
|
|
|
SkPdfToUnicode(SkPdfNativeDoc* parsed, SkPdfStream* stream);
|
2013-06-21 21:43:09 +00:00
|
|
|
};
|
|
|
|
|
2013-06-25 20:45:40 +00:00
|
|
|
|
2013-06-21 21:43:09 +00:00
|
|
|
class SkPdfIdentityHEncoding : public SkPdfEncoding {
|
|
|
|
public:
|
2013-11-20 14:29:51 +00:00
|
|
|
virtual ~SkPdfIdentityHEncoding() {}
|
2013-06-21 21:43:09 +00:00
|
|
|
virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const {
|
|
|
|
// TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
|
|
|
|
|
|
|
|
uint16_t* text = (uint16_t*)textIn.text;
|
|
|
|
textOut->text = new uint16_t[textIn.len / 2];
|
|
|
|
textOut->len = textIn.len / 2;
|
|
|
|
|
|
|
|
for (int i = 0; i < textOut->len; i++) {
|
|
|
|
textOut->text[i] = ((text[i] << 8) & 0xff00) | ((text[i] >> 8) & 0x00ff);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkPdfIdentityHEncoding* instance() {
|
|
|
|
static SkPdfIdentityHEncoding* inst = new SkPdfIdentityHEncoding();
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-27 20:03:43 +00:00
|
|
|
// TODO(edisonn): using this one when no encoding is specified
|
|
|
|
class SkPdfDefaultEncoding : public SkPdfEncoding {
|
|
|
|
public:
|
2013-11-20 14:29:51 +00:00
|
|
|
virtual ~SkPdfDefaultEncoding() {}
|
2013-06-27 20:03:43 +00:00
|
|
|
virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const {
|
|
|
|
// TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
|
|
|
|
|
|
|
|
unsigned char* text = (unsigned char*)textIn.text;
|
|
|
|
textOut->text = new uint16_t[textIn.len];
|
|
|
|
textOut->len = textIn.len;
|
|
|
|
|
|
|
|
for (int i = 0; i < textOut->len; i++) {
|
|
|
|
textOut->text[i] = text[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkPdfDefaultEncoding* instance() {
|
|
|
|
static SkPdfDefaultEncoding* inst = new SkPdfDefaultEncoding();
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
};
|
2013-06-25 20:45:40 +00:00
|
|
|
|
|
|
|
class SkPdfCIDToGIDMapIdentityEncoding : public SkPdfEncoding {
|
|
|
|
public:
|
2013-11-20 14:29:51 +00:00
|
|
|
virtual ~SkPdfCIDToGIDMapIdentityEncoding() {}
|
2013-06-25 20:45:40 +00:00
|
|
|
virtual bool decodeText(const SkUnencodedText& textIn, SkDecodedText* textOut) const {
|
|
|
|
// TODO(edisonn): SkASSERT(textIn.len % 2 == 0); or report error?
|
|
|
|
|
2013-07-10 17:09:50 +00:00
|
|
|
uint16_t* text = (uint16_t*)textIn.text;
|
|
|
|
textOut->text = new uint16_t[textIn.len / 2];
|
|
|
|
textOut->len = textIn.len / 2;
|
2013-06-25 20:45:40 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < textOut->len; i++) {
|
2013-07-10 17:09:50 +00:00
|
|
|
textOut->text[i] = ((text[i] << 8) & 0xff00) | ((text[i] >> 8) & 0x00ff);
|
2013-06-25 20:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkPdfCIDToGIDMapIdentityEncoding* instance() {
|
|
|
|
static SkPdfCIDToGIDMapIdentityEncoding* inst = new SkPdfCIDToGIDMapIdentityEncoding();
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-21 21:43:09 +00:00
|
|
|
class SkPdfFont {
|
|
|
|
public:
|
|
|
|
SkPdfFont* fBaseFont;
|
|
|
|
SkPdfEncoding* fEncoding;
|
2013-06-25 20:45:40 +00:00
|
|
|
SkPdfToUnicode* fToUnicode;
|
|
|
|
|
2013-06-21 21:43:09 +00:00
|
|
|
|
|
|
|
public:
|
2013-06-27 20:03:43 +00:00
|
|
|
SkPdfFont() : fBaseFont(NULL), fEncoding(SkPdfDefaultEncoding::instance()), fToUnicode(NULL) {}
|
2013-06-21 21:43:09 +00:00
|
|
|
|
2013-08-05 18:18:06 +00:00
|
|
|
virtual ~SkPdfFont() {
|
|
|
|
// TODO(edisonn): NYI (will leak for now)
|
|
|
|
}
|
|
|
|
|
2013-06-21 21:43:09 +00:00
|
|
|
const SkPdfEncoding* encoding() const {return fEncoding;}
|
|
|
|
|
2013-10-10 20:58:22 +00:00
|
|
|
void drawText(const SkDecodedText& text, SkPaint* paint, SkPdfContext* pdfContext,
|
|
|
|
SkCanvas* canvas) {
|
2013-06-21 21:43:09 +00:00
|
|
|
for (int i = 0 ; i < text.size(); i++) {
|
2013-08-07 18:04:15 +00:00
|
|
|
canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm);
|
|
|
|
#ifdef PDF_TRACE
|
|
|
|
SkPoint point = SkPoint::Make(SkDoubleToScalar(0), SkDoubleToScalar(0));
|
|
|
|
pdfContext->fGraphicsState.fMatrixTm.mapPoints(&point, 1);
|
2013-10-10 20:58:22 +00:00
|
|
|
printf("DrawText at (%f, %f)\n", SkScalarToDouble(point.x()),
|
|
|
|
SkScalarToDouble(point.y()));
|
2013-08-07 18:04:15 +00:00
|
|
|
#endif // PDF_TRACE
|
2013-08-14 18:26:20 +00:00
|
|
|
|
|
|
|
#ifdef PDF_TRACE_DRAWTEXT
|
|
|
|
SkPaint col;
|
|
|
|
col.setColor(SK_ColorMAGENTA);
|
2013-10-10 20:58:22 +00:00
|
|
|
SkRect rect = SkRect::MakeXYWH(SkDoubleToScalar(0.0),
|
|
|
|
SkDoubleToScalar(0.0),
|
|
|
|
SkDoubleToScalar(10.0),
|
|
|
|
SkDoubleToScalar(10.0));
|
2013-08-14 18:26:20 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->setMatrix(pdfContext->fGraphicsState.fMatrixTm);
|
|
|
|
canvas->drawRect(rect, col);
|
|
|
|
canvas->restore();
|
|
|
|
#endif
|
2013-06-27 20:03:43 +00:00
|
|
|
double width = drawOneChar(text[i], paint, pdfContext, canvas);
|
2013-10-10 20:58:22 +00:00
|
|
|
pdfContext->fGraphicsState.fMatrixTm.preTranslate(SkDoubleToScalar(width),
|
|
|
|
SkDoubleToScalar(0.0));
|
2013-06-21 21:43:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 20:45:40 +00:00
|
|
|
void ToUnicode(const SkDecodedText& textIn, SkUnicodeText* textOut) const {
|
|
|
|
if (fToUnicode) {
|
|
|
|
textOut->text = new uint16_t[textIn.len];
|
|
|
|
textOut->len = textIn.len;
|
|
|
|
for (int i = 0; i < textIn.len; i++) {
|
|
|
|
textOut->text[i] = fToUnicode->fCMapEncoding[textIn.text[i]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
textOut->text = textIn.text;
|
|
|
|
textOut->len = textIn.len;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline unsigned int ToUnicode(unsigned int ch) const {
|
2013-08-22 15:37:21 +00:00
|
|
|
if (fToUnicode && fToUnicode->fCMapEncoding) {
|
2013-06-25 20:45:40 +00:00
|
|
|
return fToUnicode->fCMapEncoding[ch];
|
|
|
|
} else {
|
|
|
|
return ch;
|
|
|
|
}
|
2013-06-21 21:43:09 +00:00
|
|
|
};
|
|
|
|
|
2013-08-14 18:26:20 +00:00
|
|
|
static SkPdfFont* fontFromPdfDictionary(SkPdfNativeDoc* doc, SkPdfFontDictionary* dict);
|
2013-07-10 17:09:50 +00:00
|
|
|
static SkPdfFont* Default() {return fontFromName(NULL, NULL, "TimesNewRoman");}
|
2013-06-21 21:43:09 +00:00
|
|
|
|
2013-10-10 20:58:22 +00:00
|
|
|
static SkPdfType0Font* fontFromType0FontDictionary(SkPdfNativeDoc* doc,
|
|
|
|
SkPdfType0FontDictionary* dict);
|
|
|
|
static SkPdfType1Font* fontFromType1FontDictionary(SkPdfNativeDoc* doc,
|
|
|
|
SkPdfType1FontDictionary* dict);
|
|
|
|
static SkPdfType3Font* fontFromType3FontDictionary(SkPdfNativeDoc* doc,
|
|
|
|
SkPdfType3FontDictionary* dict);
|
|
|
|
static SkPdfTrueTypeFont* fontFromTrueTypeFontDictionary(SkPdfNativeDoc* doc,
|
|
|
|
SkPdfTrueTypeFontDictionary* dict);
|
|
|
|
static SkPdfMultiMasterFont* fontFromMultiMasterFontDictionary(
|
|
|
|
SkPdfNativeDoc* doc, SkPdfMultiMasterFontDictionary* dict);
|
|
|
|
|
|
|
|
static SkPdfFont* fontFromFontDescriptor(SkPdfNativeDoc* doc,
|
|
|
|
SkPdfFontDescriptorDictionary* fd,
|
|
|
|
bool loadFromName = true);
|
2013-06-27 20:03:43 +00:00
|
|
|
|
2013-06-21 21:43:09 +00:00
|
|
|
public:
|
2013-10-10 20:58:22 +00:00
|
|
|
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
|
|
|
|
SkCanvas* canvas) = 0;
|
2013-06-21 21:43:09 +00:00
|
|
|
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) = 0;
|
2013-07-10 17:09:50 +00:00
|
|
|
|
|
|
|
private:
|
2013-08-14 18:26:20 +00:00
|
|
|
static SkPdfFont* fontFromPdfDictionaryOnce(SkPdfNativeDoc* doc, SkPdfFontDictionary* dict);
|
2013-06-21 21:43:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkPdfStandardFont : public SkPdfFont {
|
|
|
|
SkTypeface* fTypeface;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SkPdfStandardFont(SkTypeface* typeface) : fTypeface(typeface) {}
|
|
|
|
|
|
|
|
public:
|
2013-10-10 20:58:22 +00:00
|
|
|
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
|
|
|
|
SkCanvas* canvas) {
|
2013-06-21 21:43:09 +00:00
|
|
|
paint->setTypeface(fTypeface);
|
|
|
|
paint->setTextEncoding(SkPaint::kUTF8_TextEncoding);
|
|
|
|
|
|
|
|
unsigned long ch4 = ch;
|
|
|
|
char utf8[10];
|
2013-12-02 20:58:12 +00:00
|
|
|
size_t len = SkUTF8_FromUnichar((SkUnichar) ch4, utf8);
|
2013-06-21 21:43:09 +00:00
|
|
|
|
|
|
|
canvas->drawText(utf8, len, SkDoubleToScalar(0), SkDoubleToScalar(0), *paint);
|
|
|
|
|
|
|
|
SkScalar textWidth = paint->measureText(utf8, len);
|
2013-06-27 20:03:43 +00:00
|
|
|
return SkScalarToDouble(textWidth);
|
2013-06-21 21:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkPdfType0Font : public SkPdfFont {
|
|
|
|
public:
|
2013-08-14 18:26:20 +00:00
|
|
|
SkPdfType0Font(SkPdfNativeDoc* doc, SkPdfType0FontDictionary* dict);
|
2013-06-21 21:43:09 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2013-10-10 20:58:22 +00:00
|
|
|
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
|
|
|
|
SkCanvas* canvas) {
|
2013-06-27 20:03:43 +00:00
|
|
|
return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
|
2013-06-21 21:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkPdfType1Font : public SkPdfFont {
|
|
|
|
public:
|
2013-08-14 18:26:20 +00:00
|
|
|
SkPdfType1Font(SkPdfNativeDoc* doc, SkPdfType1FontDictionary* dict) {
|
2013-06-27 20:03:43 +00:00
|
|
|
if (dict->has_FontDescriptor()) {
|
2013-07-10 17:09:50 +00:00
|
|
|
fBaseFont = SkPdfFont::fontFromFontDescriptor(doc, dict->FontDescriptor(doc));
|
2013-06-27 20:03:43 +00:00
|
|
|
} else {
|
2013-07-10 17:09:50 +00:00
|
|
|
fBaseFont = fontFromName(doc, dict, dict->BaseFont(doc).c_str());
|
2013-06-27 20:03:43 +00:00
|
|
|
}
|
2013-08-14 18:26:20 +00:00
|
|
|
|
|
|
|
if (dict->isEncodingAName(doc)) {
|
|
|
|
fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(doc).c_str());
|
|
|
|
} else if (dict->isEncodingADictionary(doc)) {
|
|
|
|
//SkPdfDictionary* dictEnc = dict->getEncodingAsDictionary(doc);
|
|
|
|
}
|
|
|
|
dict->FontDescriptor(doc);
|
2013-06-21 21:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2013-10-10 20:58:22 +00:00
|
|
|
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
|
|
|
|
SkCanvas* canvas) {
|
2013-06-27 20:03:43 +00:00
|
|
|
return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
|
2013-06-21 21:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-27 20:03:43 +00:00
|
|
|
class SkPdfTrueTypeFont : public SkPdfType1Font {
|
2013-06-21 21:43:09 +00:00
|
|
|
public:
|
2013-10-10 20:58:22 +00:00
|
|
|
SkPdfTrueTypeFont(SkPdfNativeDoc* doc, SkPdfTrueTypeFontDictionary* dict)
|
|
|
|
: SkPdfType1Font(doc, dict) {}
|
2013-06-27 20:03:43 +00:00
|
|
|
};
|
2013-06-21 21:43:09 +00:00
|
|
|
|
2013-06-27 20:03:43 +00:00
|
|
|
class SkPdfMultiMasterFont : public SkPdfType1Font {
|
2013-06-21 21:43:09 +00:00
|
|
|
public:
|
2013-10-10 20:58:22 +00:00
|
|
|
SkPdfMultiMasterFont(SkPdfNativeDoc* doc, SkPdfMultiMasterFontDictionary* dict)
|
|
|
|
: SkPdfType1Font(doc, dict) {}
|
2013-06-21 21:43:09 +00:00
|
|
|
};
|
2013-06-25 20:45:40 +00:00
|
|
|
/*
|
|
|
|
class CIDToGIDMap {
|
|
|
|
virtual unsigned int map(unsigned int cid) = 0;
|
|
|
|
static CIDToGIDMap* fromName(const char* name);
|
|
|
|
};
|
|
|
|
|
|
|
|
class CIDToGIDMap_Identity {
|
|
|
|
virtual unsigned int map(unsigned int cid) { return cid; }
|
|
|
|
|
|
|
|
static CIDToGIDMap_Identity* instance() {
|
|
|
|
static CIDToGIDMap_Identity* inst = new CIDToGIDMap_Identity();
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CIDToGIDMap* CIDToGIDMap::fromName(const char* name) {
|
|
|
|
// The only one supported right now is Identity
|
|
|
|
if (strcmp(name, "Identity") == 0) {
|
|
|
|
return CIDToGIDMap_Identity::instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PDF_TRACE
|
|
|
|
// TODO(edisonn): warning/report
|
|
|
|
printf("Unknown CIDToGIDMap: %s\n", name);
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
CIDToGIDMap* fCidToGid;
|
|
|
|
*/
|
2013-06-21 21:43:09 +00:00
|
|
|
|
|
|
|
class SkPdfType3Font : public SkPdfFont {
|
2013-06-25 20:45:40 +00:00
|
|
|
struct Type3FontChar {
|
2013-08-14 18:26:20 +00:00
|
|
|
SkPdfNativeObject* fObj;
|
2013-06-25 20:45:40 +00:00
|
|
|
double fWidth;
|
|
|
|
};
|
|
|
|
|
|
|
|
SkPdfDictionary* fCharProcs;
|
|
|
|
SkPdfEncodingDictionary* fEncodingDict;
|
|
|
|
unsigned int fFirstChar;
|
|
|
|
unsigned int fLastChar;
|
|
|
|
|
|
|
|
SkRect fFontBBox;
|
|
|
|
SkMatrix fFonMatrix;
|
|
|
|
|
|
|
|
Type3FontChar* fChars;
|
|
|
|
|
2013-06-21 21:43:09 +00:00
|
|
|
public:
|
2013-08-14 18:26:20 +00:00
|
|
|
SkPdfType3Font(SkPdfNativeDoc* parsed, SkPdfType3FontDictionary* dict) {
|
2013-07-10 17:09:50 +00:00
|
|
|
fBaseFont = fontFromName(parsed, dict, dict->BaseFont(parsed).c_str());
|
2013-06-25 20:45:40 +00:00
|
|
|
|
|
|
|
if (dict->has_Encoding()) {
|
2013-07-10 17:09:50 +00:00
|
|
|
if (dict->isEncodingAName(parsed)) {
|
|
|
|
fEncoding = SkPdfEncoding::fromName(dict->getEncodingAsName(parsed).c_str());
|
|
|
|
} else if (dict->isEncodingAEncodingdictionary(parsed)) {
|
2013-07-23 19:43:48 +00:00
|
|
|
// No encoding.
|
|
|
|
fEncoding = SkPdfDefaultEncoding::instance();
|
2013-07-10 17:09:50 +00:00
|
|
|
fEncodingDict = dict->getEncodingAsEncodingdictionary(parsed);
|
2013-06-25 20:45:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// null?
|
2013-07-10 17:09:50 +00:00
|
|
|
fCharProcs = dict->CharProcs(parsed);
|
2013-06-25 20:45:40 +00:00
|
|
|
|
|
|
|
fToUnicode = NULL;
|
|
|
|
if (dict->has_ToUnicode()) {
|
2013-07-10 17:09:50 +00:00
|
|
|
fToUnicode = new SkPdfToUnicode(parsed, dict->ToUnicode(parsed));
|
2013-06-25 20:45:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 18:20:06 +00:00
|
|
|
fFirstChar = (unsigned int)dict->FirstChar(parsed);
|
|
|
|
fLastChar = (unsigned int)dict->LastChar(parsed);
|
2013-07-10 17:09:50 +00:00
|
|
|
fFonMatrix = dict->has_FontMatrix() ? dict->FontMatrix(parsed) : SkMatrix::I();
|
2013-06-25 20:45:40 +00:00
|
|
|
|
2013-07-10 17:09:50 +00:00
|
|
|
if (dict->has_FontBBox()) {
|
|
|
|
fFontBBox = dict->FontBBox(parsed);
|
2013-06-25 20:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fChars = new Type3FontChar[fLastChar - fFirstChar + 1];
|
|
|
|
|
|
|
|
memset(fChars, 0, sizeof(fChars[0]) * (fLastChar - fFirstChar + 1));
|
|
|
|
|
2013-07-10 17:09:50 +00:00
|
|
|
const SkPdfArray* widths = dict->Widths(parsed);
|
|
|
|
for (unsigned int i = 0 ; i < widths->size(); i++) {
|
2013-07-25 21:40:23 +00:00
|
|
|
if ((fFirstChar + i) >= fFirstChar && (fFirstChar + i) <= fLastChar) {
|
|
|
|
fChars[i].fWidth = (*widths)[i]->numberValue();
|
|
|
|
} else {
|
|
|
|
// TODO(edisonn): report pdf corruption
|
2013-06-25 20:45:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 17:09:50 +00:00
|
|
|
const SkPdfArray* diffs = fEncodingDict->Differences(parsed);
|
|
|
|
unsigned int j = fFirstChar;
|
|
|
|
for (unsigned int i = 0 ; i < diffs->size(); i++) {
|
|
|
|
if ((*diffs)[i]->isInteger()) {
|
2013-07-10 18:20:06 +00:00
|
|
|
j = (unsigned int)(*diffs)[i]->intValue();
|
2013-07-10 17:09:50 +00:00
|
|
|
} else if ((*diffs)[i]->isName()) {
|
2013-07-25 21:40:23 +00:00
|
|
|
if (j >= fFirstChar && j <= fLastChar) {
|
|
|
|
fChars[j - fFirstChar].fObj = fCharProcs->get((*diffs)[i]);
|
|
|
|
} else {
|
|
|
|
// TODO(edisonn): report pdf corruption
|
2013-06-25 20:45:40 +00:00
|
|
|
}
|
|
|
|
j++;
|
|
|
|
} else {
|
2013-07-25 21:40:23 +00:00
|
|
|
// TODO(edisonn): report bad pdf
|
2013-06-25 20:45:40 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 21:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2013-10-10 20:58:22 +00:00
|
|
|
virtual double drawOneChar(unsigned int ch, SkPaint* paint, SkPdfContext* pdfContext,
|
|
|
|
SkCanvas* canvas) {
|
2013-06-25 20:45:40 +00:00
|
|
|
if (ch < fFirstChar || ch > fLastChar || !fChars[ch - fFirstChar].fObj) {
|
2013-06-27 20:03:43 +00:00
|
|
|
return fBaseFont->drawOneChar(ToUnicode(ch), paint, pdfContext, canvas);
|
2013-06-25 20:45:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PDF_TRACE
|
|
|
|
printf("Type 3 char to unicode: %c\n", ToUnicode(ch));
|
|
|
|
if (ToUnicode(ch) == 'A') {
|
|
|
|
printf("break;\n");
|
|
|
|
}
|
|
|
|
#endif
|
2013-06-21 21:43:09 +00:00
|
|
|
|
2013-07-10 17:09:50 +00:00
|
|
|
// TODO(edisonn): is it better to resolve the reference at load time, or now?
|
2013-10-10 20:58:22 +00:00
|
|
|
doType3Char(pdfContext,
|
|
|
|
canvas,
|
|
|
|
pdfContext->fPdfDoc->resolveReference(fChars[ch - fFirstChar].fObj),
|
|
|
|
fFontBBox,
|
|
|
|
fFonMatrix,
|
|
|
|
pdfContext->fGraphicsState.fCurFontSize);
|
2013-06-21 21:43:09 +00:00
|
|
|
|
2013-06-27 20:03:43 +00:00
|
|
|
// TODO(edisonn): verify/test translate code, not tested yet
|
2013-10-10 20:58:22 +00:00
|
|
|
pdfContext->fGraphicsState.fMatrixTm.preTranslate(
|
|
|
|
SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize *
|
|
|
|
fChars[ch - fFirstChar].fWidth),
|
|
|
|
SkDoubleToScalar(0.0));
|
2013-07-10 17:09:50 +00:00
|
|
|
return fChars[ch - fFirstChar].fWidth;
|
2013-06-21 21:43:09 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 20:58:22 +00:00
|
|
|
virtual void afterWord(SkPaint* paint, SkMatrix* matrix) {}
|
2013-06-21 21:43:09 +00:00
|
|
|
};
|
|
|
|
|
2013-08-21 16:31:37 +00:00
|
|
|
#endif // SkPdfFont_DEFINED
|