Commit 64531344 authored by Dileep Sankhla's avatar Dileep Sankhla Committed by Albert Astals Cid

Add annotation font color

parent f506b8bc
......@@ -792,6 +792,21 @@ Object AnnotColor::writeToObject(XRef *xref) const {
}
}
//------------------------------------------------------------------------
// DefaultAppearance
//------------------------------------------------------------------------
DefaultAppearance::DefaultAppearance(const GooString &fontTag, int fontPtSize, AnnotColor *fontColor)
: fontPtSize(fontPtSize), fontColor(fontColor) {
DefaultAppearance::fontTag = fontTag.copy();
}
DefaultAppearance::~DefaultAppearance() {
delete fontTag;
if (fontColor) {
delete fontColor;
}
}
//------------------------------------------------------------------------
// AnnotIconFit
//------------------------------------------------------------------------
......@@ -2594,12 +2609,13 @@ void AnnotLink::draw(Gfx *gfx, GBool printing) {
//------------------------------------------------------------------------
// AnnotFreeText
//------------------------------------------------------------------------
AnnotFreeText::AnnotFreeText(PDFDoc *docA, PDFRectangle *rect, GooString *da) :
AnnotFreeText::AnnotFreeText(PDFDoc *docA, PDFRectangle *rect, const DefaultAppearance &da) :
AnnotMarkup(docA, rect) {
type = typeFreeText;
GooString *daStr = constructAppearanceString(da.getFontTag(), da.getFontPtSize(), da.getFontColor());
annotObj.dictSet ("Subtype", Object(objName, "FreeText"));
annotObj.dictSet("DA", Object(da->copy()));
annotObj.dictSet("DA", Object(daStr));
initialize (docA, annotObj.getDict());
}
......@@ -2727,14 +2743,10 @@ void AnnotFreeText::setContents(GooString *new_content) {
invalidateAppearance();
}
void AnnotFreeText::setAppearanceString(GooString *new_string) {
void AnnotFreeText::setAppearanceString(const DefaultAppearance &da) {
delete appearanceString;
if (new_string) {
appearanceString = new GooString(new_string);
} else {
appearanceString = new GooString();
}
appearanceString = constructAppearanceString(da.getFontTag(), da.getFontPtSize(), da.getFontColor());
update ("DA", Object(appearanceString->copy()));
invalidateAppearance();
......@@ -2806,7 +2818,17 @@ void AnnotFreeText::setIntent(AnnotFreeTextIntent new_intent) {
update ("IT", Object(objName, intentName));
}
static GfxFont * createAnnotDrawFont(XRef * xref, Dict *fontResDict)
DefaultAppearance *AnnotFreeText::getDefaultAppearance() const {
double fontSize;
AnnotColor *fontColor;
GooString *fontTag;
parseAppearanceString(appearanceString, fontSize, fontColor, fontTag);
DefaultAppearance *da = new DefaultAppearance(*fontTag, fontSize, fontColor);
delete fontTag;
return da;
}
static GfxFont *createAnnotDrawFont(XRef * xref, Dict *fontResDict)
{
const Ref dummyRef = { -1, -1 };
......@@ -2823,16 +2845,41 @@ static GfxFont * createAnnotDrawFont(XRef * xref, Dict *fontResDict)
return GfxFont::makeFont(xref, "AnnotDrawFont", dummyRef, fontDict);
}
void AnnotFreeText::parseAppearanceString(GooString *da, double &fontsize, AnnotColor* &fontcolor) {
GooString *AnnotFreeText::constructAppearanceString(const GooString &fontTag, double fontSize, const AnnotColor *fontColor) {
const double *colorData = fontColor->getValues();
GooString * cstr = nullptr;
switch(fontColor->getSpace())
{
case AnnotColor::AnnotColorSpace::colorTransparent: // =0
cstr = new GooString();
break;
case AnnotColor::AnnotColorSpace::colorGray: //=1
cstr = GooString::format("{0:.2f} g ", colorData[0]);
break;
case AnnotColor::AnnotColorSpace::colorRGB: //=3
cstr = GooString::format("{0:.2f} {1:.2f} {2:.2f} rg ", colorData[0], colorData[1], colorData[2]);
break;
case AnnotColor::AnnotColorSpace::colorCMYK: //=4
cstr = GooString::format("{0:.2f} {1:.2f} {2:.2f} {3:.2f} k ", colorData[0], colorData[1], colorData[2], colorData[3]);
break;
}
const GooString * str = GooString::format("/{0:s} {1:.2f} Tf", &fontTag, fontSize);
return cstr->append( str );
}
void AnnotFreeText::parseAppearanceString(GooString *da, double &fontsize, AnnotColor* &fontcolor, GooString* &fontTag) {
fontsize = -1;
fontcolor = nullptr;
fontTag = nullptr;
if (da) {
GooList * daToks = new GooList();
int i = FormFieldText::tokenizeDA(da, daToks, "Tf");
if (i >= 1) {
fontsize = gatof(( (GooString *)daToks->get(i-1) )->getCString());
// TODO: Font name
}
if (i >= 2) {
fontTag = new GooString(( (GooString *)daToks->get(i-2) )->getCString());
}
// Scan backwards: we are looking for the last set value
for (i = daToks->getLength()-1; i >= 0; --i) {
......@@ -2873,7 +2920,9 @@ void AnnotFreeText::generateFreeTextAppearance()
// Parse some properties from the appearance string
double fontsize;
AnnotColor *fontcolor;
parseAppearanceString(appearanceString, fontsize, fontcolor);
GooString *fontTag = nullptr;
parseAppearanceString(appearanceString, fontsize, fontcolor, fontTag);
delete fontTag;
// Default values
if (fontsize <= 0)
fontsize = 10;
......
......@@ -347,6 +347,26 @@ private:
int length;
};
//------------------------------------------------------------------------
// DefaultAppearance
//------------------------------------------------------------------------
class DefaultAppearance {
public:
DefaultAppearance(const GooString &fontTag, int fontPtSize, AnnotColor *fontColor = nullptr);
const GooString &getFontTag() const { return *fontTag; }
int getFontPtSize() const { return fontPtSize; }
const AnnotColor *getFontColor() const { return fontColor; }
~DefaultAppearance();
DefaultAppearance(DefaultAppearance &ger) = delete;
private:
GooString *fontTag;
int fontPtSize;
AnnotColor *fontColor;
};
//------------------------------------------------------------------------
// AnnotIconFit
//------------------------------------------------------------------------
......@@ -969,7 +989,7 @@ public:
intentFreeTextTypeWriter // FreeTextTypeWriter
};
AnnotFreeText(PDFDoc *docA, PDFRectangle *rect, GooString *da);
AnnotFreeText(PDFDoc *docA, PDFRectangle *rect, const DefaultAppearance &da);
AnnotFreeText(PDFDoc *docA, Object *dictObject, Object *obj);
~AnnotFreeText();
......@@ -977,14 +997,14 @@ public:
Object getAppearanceResDict() override;
void setContents(GooString *new_content) override;
void setAppearanceString(GooString *new_string);
void setAppearanceString(const DefaultAppearance &da);
void setQuadding(AnnotFreeTextQuadding new_quadding);
void setStyleString(GooString *new_string);
void setCalloutLine(AnnotCalloutLine *line);
void setIntent(AnnotFreeTextIntent new_intent);
// getters
const GooString *getAppearanceString() const { return appearanceString; }
DefaultAppearance *getDefaultAppearance() const;
AnnotFreeTextQuadding getQuadding() const { return quadding; }
// return rc
const GooString *getStyleString() const { return styleString; }
......@@ -997,7 +1017,8 @@ public:
protected:
void initialize(PDFDoc *docA, Dict *dict);
static void parseAppearanceString(GooString *da, double &fontsize, AnnotColor* &fontcolor);
static GooString *constructAppearanceString(const GooString &fontTag, double fontSize, const AnnotColor *fontColor);
static void parseAppearanceString(GooString *da, double &fontSize, AnnotColor* &fontColor, GooString* &fontTag);
void generateFreeTextAppearance();
// required
......
......@@ -175,7 +175,7 @@ void XPDFReader::invTransform( double * M, const QPointF &p, double &x, double &
y = invM[1] * xt + invM[3] * yt;
}
QColor convertAnnotColor( AnnotColor *color );
QColor convertAnnotColor( const AnnotColor *color );
AnnotColor* convertQColor( const QColor &color );
}
......@@ -1816,17 +1816,17 @@ class TextAnnotationPrivate : public AnnotationPrivate
TextAnnotationPrivate();
Annotation * makeAlias() override;
Annot* createNativeAnnot(::Page *destPage, DocumentData *doc) override;
void setDefaultAppearanceToNative();
DefaultAppearance *getDefaultAppearanceFromNative() const;
// data fields
TextAnnotation::TextType textType;
QString textIcon;
QFont textFont;
QColor textColor;
int inplaceAlign; // 0:left, 1:center, 2:right
QVector<QPointF> inplaceCallout;
TextAnnotation::InplaceIntent inplaceIntent;
// Helper
static GooString * toAppearanceString(const QFont &font);
};
TextAnnotationPrivate::TextAnnotationPrivate()
......@@ -1841,13 +1841,6 @@ Annotation * TextAnnotationPrivate::makeAlias()
return new TextAnnotation(*this);
}
GooString * TextAnnotationPrivate::toAppearanceString(const QFont &font)
{
GooString * s = GooString::format("/Invalid_font {0:d} Tf", font.pointSize());
// TODO: Font family, style (bold, italic, ...) and pointSize as float
return s;
}
Annot* TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *doc)
{
// Setters are defined in the public class
......@@ -1859,15 +1852,11 @@ Annot* TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
// Set pdfAnnot
PDFRectangle rect = boundaryToPdfRectangle(boundary, flags);
if (textType == TextAnnotation::Linked)
{
pdfAnnot = new AnnotText(destPage->getDoc(), &rect);
}
else
{
GooString * da = toAppearanceString(textFont);
pdfAnnot = new AnnotFreeText(destPage->getDoc(), &rect, da);
delete da;
if (textType == TextAnnotation::Linked) {
pdfAnnot = new AnnotText(destPage->getDoc(), &rect);
} else {
DefaultAppearance da(GooString("Invalid_font"), textFont.pointSize(), convertQColor(textColor));
pdfAnnot = new AnnotFreeText(destPage->getDoc(), &rect, da);
}
// Set properties
......@@ -1884,6 +1873,26 @@ Annot* TextAnnotationPrivate::createNativeAnnot(::Page *destPage, DocumentData *
return pdfAnnot;
}
void TextAnnotationPrivate::setDefaultAppearanceToNative()
{
if (pdfAnnot && pdfAnnot->getType() == Annot::typeFreeText) {
AnnotFreeText * ftextann = static_cast<AnnotFreeText*>(pdfAnnot);
AnnotColor *color = convertQColor(textColor);
DefaultAppearance da(GooString("Invalid_font"), textFont.pointSize(), color);
ftextann->setAppearanceString(da);
}
}
DefaultAppearance *TextAnnotationPrivate::getDefaultAppearanceFromNative() const
{
if (pdfAnnot && pdfAnnot->getType() == Annot::typeFreeText) {
AnnotFreeText * ftextann = static_cast<AnnotFreeText*>(pdfAnnot);
return ftextann->getDefaultAppearance();
} else {
return nullptr;
}
}
TextAnnotation::TextAnnotation( TextAnnotation::TextType type )
: Annotation( *new TextAnnotationPrivate() )
{
......@@ -1916,6 +1925,11 @@ TextAnnotation::TextAnnotation( const QDomNode & node )
QFont font;
font.fromString( e.attribute( QStringLiteral("font") ) );
setTextFont(font);
if ( e.hasAttribute( QStringLiteral("fontColor") ) )
{
const QColor color = QColor(e.attribute( QStringLiteral("fontColor") ) );
setTextColor(color);
}
}
if ( e.hasAttribute( QStringLiteral("align") ) )
setInplaceAlign(e.attribute( QStringLiteral("align") ).toInt());
......@@ -1975,6 +1989,7 @@ void TextAnnotation::store( QDomNode & node, QDomDocument & document ) const
textElement.setAttribute( QStringLiteral("intent"), (int)inplaceIntent() );
textElement.setAttribute( QStringLiteral("font"), textFont().toString() );
textElement.setAttribute( QStringLiteral("fontColor"), textColor().name() );
// Sub-Node-1 - escapedText
if ( !contents().isEmpty() )
......@@ -2074,17 +2089,10 @@ QFont TextAnnotation::textFont() const
if (d->pdfAnnot->getType() == Annot::typeFreeText)
{
const AnnotFreeText * ftextann = static_cast<const AnnotFreeText*>(d->pdfAnnot);
const GooString * da = ftextann->getAppearanceString();
DefaultAppearance *da = d->getDefaultAppearanceFromNative();
if (da)
{
// At the moment, only font size is parsed
QString style = QString::fromLatin1( da->getCString() );
QRegExp rx(QStringLiteral("(\\d+)(\\.\\d*)? Tf"));
if (rx.indexIn(style) != -1)
font.setPointSize( rx.cap(1).toInt() );
// TODO: Other properties
}
font.setPointSize( da->getFontPtSize() );
delete da;
}
return font;
......@@ -2093,20 +2101,33 @@ QFont TextAnnotation::textFont() const
void TextAnnotation::setTextFont( const QFont &font )
{
Q_D( TextAnnotation );
d->textFont = font;
d->textColor = Qt::black;
if (!d->pdfAnnot)
{
d->textFont = font;
return;
}
d->setDefaultAppearanceToNative();
}
if (d->pdfAnnot->getType() != Annot::typeFreeText)
return;
QColor TextAnnotation::textColor() const
{
Q_D( const TextAnnotation );
AnnotFreeText * ftextann = static_cast<AnnotFreeText*>(d->pdfAnnot);
GooString * da = TextAnnotationPrivate::toAppearanceString(font);
ftextann->setAppearanceString(da);
if (!d->pdfAnnot)
return d->textColor;
QColor color;
DefaultAppearance *da = d->getDefaultAppearanceFromNative();
if (da)
color = convertAnnotColor(da->getFontColor());
delete da;
return color;
}
void TextAnnotation::setTextColor( const QColor &color )
{
Q_D( TextAnnotation );
d->textColor = color;
d->setDefaultAppearanceToNative();
}
int TextAnnotation::inplaceAlign() const
......@@ -5055,7 +5076,7 @@ RichMediaAnnotation::Content* RichMediaAnnotation::content() const
//BEGIN utility annotation functions
QColor convertAnnotColor( AnnotColor *color )
QColor convertAnnotColor( const AnnotColor *color )
{
if ( !color )
return QColor();
......
......@@ -484,6 +484,10 @@ class POPPLER_QT5_EXPORT TextAnnotation : public Annotation
QFont textFont() const;
void setTextFont( const QFont &font );
/// \since 0.69
QColor textColor() const;
/// \since 0.69
void setTextColor( const QColor &color );
int inplaceAlign() const;
void setInplaceAlign( int align );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment