"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/Plugins/Pdf/PDFWriter/PDFUsedFont.cpp" between
TeXmacs-1.99.4-src.tar.gz and TeXmacs-1.99.5-src.tar.gz

About: GNU TeXmacs is a what-you-see-is-what-you-get scientific text editor, which was both inspired by TeX and GNU Emacs.

PDFUsedFont.cpp  (TeXmacs-1.99.4-src):PDFUsedFont.cpp  (TeXmacs-1.99.5-src)
skipping to change at line 29 skipping to change at line 29
#include "PDFUsedFont.h" #include "PDFUsedFont.h"
#include "IWrittenFont.h" #include "IWrittenFont.h"
#include "UnicodeString.h" #include "UnicodeString.h"
#include "ObjectsContext.h" #include "ObjectsContext.h"
#include "DictionaryContext.h" #include "DictionaryContext.h"
#include "PDFParser.h" #include "PDFParser.h"
#include "PDFObjectCast.h" #include "PDFObjectCast.h"
#include "PDFDictionary.h" #include "PDFDictionary.h"
#include "PDFIndirectObjectReference.h" #include "PDFIndirectObjectReference.h"
#include FT_GLYPH_H
using namespace PDFHummus; using namespace PDFHummus;
PDFUsedFont::PDFUsedFont(FT_Face inInputFace, PDFUsedFont::PDFUsedFont(FT_Face inInputFace,
const std::string& inFontFilePat h, const std::string& inFontFilePat h,
const std::string& inAdditionalM etricsFontFilePath, const std::string& inAdditionalM etricsFontFilePath,
long inFontIndex, long inFontIndex,
ObjectsContext* inObjectsContext ):mFaceWrapper(inInputFace,inFontFilePath,inAdditionalMetricsFontFilePath,inFont Index) ObjectsContext* inObjectsContext ):mFaceWrapper(inInputFace,inFontFilePath,inAdditionalMetricsFontFilePath,inFont Index)
{ {
mObjectsContext = inObjectsContext; mObjectsContext = inObjectsContext;
mWrittenFont = NULL; mWrittenFont = NULL;
skipping to change at line 56 skipping to change at line 58
bool PDFUsedFont::IsValid() bool PDFUsedFont::IsValid()
{ {
return mFaceWrapper.IsValid(); return mFaceWrapper.IsValid();
} }
EStatusCode PDFUsedFont::EncodeStringForShowing(const GlyphUnicodeMappingList& i nText, EStatusCode PDFUsedFont::EncodeStringForShowing(const GlyphUnicodeMappingList& i nText,
ObjectIDType &outFontObjectToUse, ObjectIDType &outFontObjectToUse,
UShortList& outCharactersToUse, UShortList& outCharactersToUse,
bool& outTreatCharactersAsCID) bool& outTreatCharactersAsCID)
{ {
if (inText.empty()) {
outFontObjectToUse = 0;
outTreatCharactersAsCID = false;
return PDFHummus::eSuccess;
}
if(!mWrittenFont) if(!mWrittenFont)
mWrittenFont = mFaceWrapper.CreateWrittenFontObject(mObjectsConte xt); mWrittenFont = mFaceWrapper.CreateWrittenFontObject(mObjectsConte xt);
mWrittenFont->AppendGlyphs(inText,outCharactersToUse,outTreatCharactersAs CID,outFontObjectToUse); mWrittenFont->AppendGlyphs(inText,outCharactersToUse,outTreatCharactersAs CID,outFontObjectToUse);
return PDFHummus::eSuccess; return PDFHummus::eSuccess;
} }
EStatusCode PDFUsedFont::TranslateStringToGlyphs(const std::string& inText,Glyph UnicodeMappingList& outGlyphsUnicodeMapping) EStatusCode PDFUsedFont::TranslateStringToGlyphs(const std::string& inText,Glyph UnicodeMappingList& outGlyphsUnicodeMapping)
{ {
skipping to change at line 89 skipping to change at line 97
outGlyphsUnicodeMapping.push_back(GlyphUnicodeMapping(*itGlyphs,* itUnicode)); outGlyphsUnicodeMapping.push_back(GlyphUnicodeMapping(*itGlyphs,* itUnicode));
return status; return status;
} }
EStatusCode PDFUsedFont::EncodeStringsForShowing(const GlyphUnicodeMappingListLi st& inText, EStatusCode PDFUsedFont::EncodeStringsForShowing(const GlyphUnicodeMappingListLi st& inText,
ObjectIDType &outFontObjectToUse, ObjectIDType &outFontObjectToUse,
UShortListList& outCharactersToUse, UShortListList& outCharactersToUse,
bool& outTreatCharactersAsCID) bool& outTreatCharactersAsCID)
{ {
if (inText.empty()) {
outFontObjectToUse = 0;
outTreatCharactersAsCID = false;
return PDFHummus::eSuccess;
}
if(!mWrittenFont) if(!mWrittenFont)
mWrittenFont = mFaceWrapper.CreateWrittenFontObject(mObjectsConte xt); mWrittenFont = mFaceWrapper.CreateWrittenFontObject(mObjectsConte xt);
mWrittenFont->AppendGlyphs(inText,outCharactersToUse,outTreatCharactersAs CID,outFontObjectToUse); mWrittenFont->AppendGlyphs(inText,outCharactersToUse,outTreatCharactersAs CID,outFontObjectToUse);
return PDFHummus::eSuccess; return PDFHummus::eSuccess;
} }
EStatusCode PDFUsedFont::WriteFontDefinition() EStatusCode PDFUsedFont::WriteFontDefinition(bool inEmbedFont)
{ {
// note that written font may be empty, in case no glyphs were used for this font! in the empty case, just dont write the def // note that written font may be empty, in case no glyphs were used for this font! in the empty case, just dont write the def
if(!mWrittenFont) if(!mWrittenFont)
return eSuccess; return eSuccess;
else else
return mWrittenFont->WriteFontDefinition(mFaceWrapper); return mWrittenFont->WriteFontDefinition(mFaceWrapper,inEmbedFont);
} }
EStatusCode PDFUsedFont::WriteState(ObjectsContext* inStateWriter,ObjectIDType i nObjectID) EStatusCode PDFUsedFont::WriteState(ObjectsContext* inStateWriter,ObjectIDType i nObjectID)
{ {
inStateWriter->StartNewIndirectObject(inObjectID); inStateWriter->StartNewIndirectObject(inObjectID);
DictionaryContext* pdfUsedFontObject = inStateWriter->StartDictionary(); DictionaryContext* pdfUsedFontObject = inStateWriter->StartDictionary();
pdfUsedFontObject->WriteKey("Type"); pdfUsedFontObject->WriteKey("Type");
pdfUsedFontObject->WriteNameValue("PDFUsedFont"); pdfUsedFontObject->WriteNameValue("PDFUsedFont");
skipping to change at line 156 skipping to change at line 170
if(!mWrittenFont) if(!mWrittenFont)
return PDFHummus::eFailure; return PDFHummus::eFailure;
return mWrittenFont->ReadState(inStateReader,writtenFontReference->mObjec tID); return mWrittenFont->ReadState(inStateReader,writtenFontReference->mObjec tID);
} }
FreeTypeFaceWrapper* PDFUsedFont::GetFreeTypeFont() FreeTypeFaceWrapper* PDFUsedFont::GetFreeTypeFont()
{ {
return &mFaceWrapper; return &mFaceWrapper;
} }
void PDFUsedFont::GetUnicodeGlyphs(const std::string& inText, UIntList& glyphs)
{
UnicodeString unicode;
unicode.FromUTF8(inText);
mFaceWrapper.GetGlyphsForUnicodeText(unicode.GetUnicodeList(),glyphs);
}
PDFUsedFont::TextMeasures PDFUsedFont::CalculateTextDimensions(const std::string
& inText,long inFontSize)
{
UIntList glyphs;
UnicodeString unicode;
unicode.FromUTF8(inText);
mFaceWrapper.GetGlyphsForUnicodeText(unicode.GetUnicodeList(),glyphs);
return CalculateTextDimensions(glyphs,inFontSize);
}
PDFUsedFont::TextMeasures PDFUsedFont::CalculateTextDimensions(const UIntList& i
nGlyphsList,long inFontSize)
{
// now calculate the placement bounding box. using the algorithm described i
n the FreeType turtorial part 2, minus the kerning part, and with no scale
// first, calculate the pen advancements
int pen_x, pen_y;
std::list<FT_Vector> pos;
pen_x = 0; /* start at (0,0) */
pen_y = 0;
UIntList::const_iterator it = inGlyphsList.begin();
for(; it != inGlyphsList.end();++it)
{
pos.push_back(FT_Vector());
pos.back().x = pen_x;
pos.back().y = pen_y;
pen_x += mFaceWrapper.GetGlyphWidth(*it);
}
// now let's combine with the bbox, so we get the nice bbox for the whole st
ring
FT_BBox bbox;
FT_BBox glyph_bbox;
bbox.xMin = bbox.yMin = 32000;
bbox.xMax = bbox.yMax = -32000;
it = inGlyphsList.begin();
std::list<FT_Vector>::iterator itPos = pos.begin();
for(; it != inGlyphsList.end();++it,++itPos)
{
mFaceWrapper.LoadGlyph(mFaceWrapper.GetGlyphIndexInFreeTypeIndexe
s(*it));
FT_Glyph aGlyph;
FT_Get_Glyph( mFaceWrapper->glyph,&aGlyph);
FT_Glyph_Get_CBox(aGlyph, FT_GLYPH_BBOX_UNSCALED,&glyph_bbox);
FT_Done_Glyph(aGlyph);
glyph_bbox.xMin = mFaceWrapper.GetInPDFMeasurements(glyph_bbox.xMin);
glyph_bbox.xMax = mFaceWrapper.GetInPDFMeasurements(glyph_bbox.xMax);
glyph_bbox.yMin = mFaceWrapper.GetInPDFMeasurements(glyph_bbox.yMin);
glyph_bbox.yMax = mFaceWrapper.GetInPDFMeasurements(glyph_bbox.yMax);
glyph_bbox.xMin += itPos->x;
glyph_bbox.xMax += itPos->x;
glyph_bbox.yMin += itPos->y;
glyph_bbox.yMax += itPos->y;
if ( glyph_bbox.xMin < bbox.xMin )
bbox.xMin = glyph_bbox.xMin;
if ( glyph_bbox.yMin < bbox.yMin )
bbox.yMin = glyph_bbox.yMin;
if ( glyph_bbox.xMax > bbox.xMax )
bbox.xMax = glyph_bbox.xMax;
if ( glyph_bbox.yMax > bbox.yMax )
bbox.yMax = glyph_bbox.yMax;
}
if ( bbox.xMin > bbox.xMax )
{
bbox.xMin = 0;
bbox.yMin = 0;
bbox.xMax = 0;
bbox.yMax = 0;
}
PDFUsedFont::TextMeasures result;
result.xMin = bbox.xMin*inFontSize/1000;
result.yMin = bbox.yMin*inFontSize/1000;
result.xMax = bbox.xMax*inFontSize/1000;
result.yMax = bbox.yMax*inFontSize/1000;
result.width = (bbox.xMax-bbox.xMin)*inFontSize/1000;
result.height = (bbox.yMax-bbox.yMin)*inFontSize/1000;
return result;
}
double PDFUsedFont::CalculateTextAdvance(const std::string& inText,double inFont
Size)
{
UIntList glyphs;
GetUnicodeGlyphs(inText, glyphs);
return CalculateTextAdvance(glyphs,inFontSize);
}
double PDFUsedFont::CalculateTextAdvance(const UIntList& inGlyphsList,double inF
ontSize)
{
FT_Pos pen = 0;
UIntList::const_iterator it = inGlyphsList.begin();
for(; it != inGlyphsList.end();++it)
{
FT_Pos adv;
if (mAdvanceCache.count(*it) > 0)
adv = mAdvanceCache[*it];
else {
adv = mFaceWrapper.GetGlyphWidth(*it); //potentially very
expensive!
if (mAdvanceCache.size() <= AdvanceCacheLimit) //dumb lim
it should cover typical usage, implement LRU if it's a problem
mAdvanceCache[*it] = adv;
}
pen += adv;
}
return pen * inFontSize / 1000.0;
}
bool PDFUsedFont::EnumeratePaths(IOutlineEnumerator& target, const std::string&
inText,double inFontSize)
{
UIntList glyphs;
GetUnicodeGlyphs(inText, glyphs);
return EnumeratePaths(target, glyphs,inFontSize);
}
bool PDFUsedFont::EnumeratePaths(IOutlineEnumerator& target, const UIntList& inG
lyphsList,double inFontSize)
{
bool status = true;
target.BeginEnum(inFontSize);
for( UIntList::const_iterator it = inGlyphsList.begin(); it != inGlyphsLi
st.end(); ++it )
{
status = mFaceWrapper.GetGlyphOutline(*it, target);
if (!status) break;
// Keep track of glyphs' advance
double adv = mFaceWrapper.GetGlyphWidth(*it);
target.MoveBasepoint(adv * inFontSize / 1000.0, 0);
}
return status;
}
void PDFUsedFont::IOutlineEnumerator::BeginEnum(double scale)
{
mFontScale = scale;
}
PDFUsedFont::IOutlineEnumerator::IOutlineEnumerator(double base_x, double base_y
)
{
mBase_x = base_x;
mBase_y = base_y;
}
void PDFUsedFont::IOutlineEnumerator::MoveBasepoint(double dx, double dy)
{
mBase_x += dx;
mBase_y += dy;
}
bool PDFUsedFont::IOutlineEnumerator::Moveto(FT_Short x, FT_Short y)
{
double scale = mFontScale / UPM();
return Moveto(mBase_x + scale * x, mBase_y + scale * y);
}
bool PDFUsedFont::IOutlineEnumerator::Lineto(FT_Short x, FT_Short y)
{
double scale = mFontScale / UPM();
return Lineto(mBase_x + scale * x, mBase_y + scale * y);
}
bool PDFUsedFont::IOutlineEnumerator::Curveto(FT_Short x1, FT_Short y1, FT_Short
x2, FT_Short y2, FT_Short x3, FT_Short y3)
{
double scale = mFontScale / UPM();
return Curveto(mBase_x + scale * x1, mBase_y + scale * y1,
mBase_x + scale * x2, mBase_y + scale * y2,
mBase_x + scale * x3, mBase_y + scale * y3);
}
bool PDFUsedFont::IOutlineEnumerator::Close()
{
return Closepath();
}
 End of changes. 6 change blocks. 
2 lines changed or deleted 16 lines changed or added

Home  |  About  |  All  |  Newest  |  Fossies Dox  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTPS