Skip to content

Commit

Permalink
Fix path text
Browse files Browse the repository at this point in the history
Based on Dawood’s work, rebased on top of recent changes.
Fixes scribusproject#7.
  • Loading branch information
khaledhosny committed Mar 23, 2016
1 parent 6d5cb1f commit ab73f1f
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 1,008 deletions.
2 changes: 2 additions & 0 deletions scribus/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,7 @@ void Canvas::DrawPageGuides(ScPainter *p, QRectF clip, bool master)
void Canvas::DrawPageIndicatorSub(ScPainter *p, ScPage *page)
{
p->save();
int fm = p->fillMode();
p->setAntialiasing(false);
p->translate(page->xOffset(), page->yOffset());
double lineWidth = 1.0 / m_viewMode.scale;
Expand All @@ -2072,6 +2073,7 @@ void Canvas::DrawPageIndicatorSub(ScPainter *p, ScPage *page)
p->setPen(PrefsManager::instance()->appPrefs.displayPrefs.pageBorderColor, 1 / m_viewMode.scale, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
p->drawRect(0, 0, pageWidth, pageHeight);
p->setAntialiasing(true);
p->setFillMode(fm);
p->restore();
}

Expand Down
97 changes: 44 additions & 53 deletions scribus/pageitem_pathtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ for which a new license (GPL+exception) is in place.
#include "undostate.h"
#include "util.h"
#include "util_math.h"
#include "text/boxes.h"
#include "text/textlayoutpainter.h"
#include "text/screenpainter.h"

using namespace std;

Expand Down Expand Up @@ -79,18 +82,8 @@ void PageItem_PathText::DrawObj_Item(ScPainter *p, QRectF cullingArea)
double dx;
FPoint point = FPoint(0, 0);
FPoint tangent = FPoint(0, 0);
QColor tmp;
CurX = m_textDistanceMargins.left();
QString cachedStroke = "";
QString cachedFill = "";
double cachedFillShade = -1;
double cachedStrokeShade = -1;
QString actStroke = "";
QString actFill = "";
double actFillShade = -1;
double actStrokeShade = -1;
QColor cachedFillQ;
QColor cachedStrokeQ;

if (!m_Doc->layerOutline(LayerID))
{
if (PoShow)
Expand Down Expand Up @@ -249,6 +242,7 @@ void PageItem_PathText::DrawObj_Item(ScPainter *p, QRectF cullingArea)
QList<QPainterPath> pathList = decomposePath(guidePath);
QPainterPath currPath = pathList[0];
int currPathIndex = 0;
LineBox* linebox = new LineBox();
for (a = firstChar; a < itemRenderText.length(); ++a)
{
CurY = 0;
Expand All @@ -259,13 +253,16 @@ void PageItem_PathText::DrawObj_Item(ScPainter *p, QRectF cullingArea)
continue;
if (a < itemRenderText.length()-1)
chstr += itemRenderText.text(a+1, 1);

GlyphLayout glyphs = layoutGlyphs(itemRenderText.charStyle(a), chstr, itemRenderText.flags(a));
GlyphRun run(&itemRenderText.charStyle(a), itemRenderText.flags(a), a, a, itemRenderText.object(a));
run.glyphs().append(glyphs);

if (itemRenderText.hasObject(a))
dx = (itemRenderText.object(a)->width() + itemRenderText.object(a)->lineWidth()) * glyphs.scaleH / 2.0;
else
dx = glyphs.xadvance / 2.0;
CurX += dx;

double currPerc = currPath.percentAtLength(CurX);
if (currPerc >= 0.9999999)
{
Expand Down Expand Up @@ -309,54 +306,34 @@ void PageItem_PathText::DrawObj_Item(ScPainter *p, QRectF cullingArea)
else
trafo *= QTransform( a, 4 * a, 0, -1, point.x(), point.y() );
}
QTransform sca = p->worldMatrix();
trafo *= sca;
p->save();
QTransform savWM = p->worldMatrix();
p->setWorldMatrix(trafo);
if (!m_Doc->RePos)

// if (!m_Doc->RePos)
{
actFill = itemRenderText.charStyle(a).fillColor();
actFillShade = itemRenderText.charStyle(a).fillShade();
if (actFill != CommonStrings::None)
const CharStyle& cStyle(run.style());
double scaleV = cStyle.scaleV() / 1000.0;
double offset = (cStyle.fontSize() / 10) * (cStyle.baselineOffset() / 1000.0);
double ascent = cStyle.font().ascent(cStyle.fontSize()/10.00) * scaleV + offset;
double descent = cStyle.font().descent(cStyle.fontSize()/10.00) * scaleV - offset;
linebox->setAscent(ascent);
linebox->setDescent(descent);
Box* box;
if (itemRenderText.hasObject(a))
{
p->setFillMode(ScPainter::Solid);
if ((cachedFillShade != actFillShade) || (cachedFill != actFill))
{
SetQColor(&tmp, actFill, actFillShade);
p->setBrush(tmp);
cachedFillQ = tmp;
cachedFill = actFill;
cachedFillShade = actFillShade;
}
else
p->setBrush(cachedFillQ);
box = new ObjectBox(run);
box->setAscent(run.object()->height() - run.object()->lineWidth());
box->setDescent(0);
}
else
p->setFillMode(ScPainter::None);
actStroke = itemRenderText.charStyle(a).strokeColor();
actStrokeShade = itemRenderText.charStyle(a).strokeShade();
if (actStroke != CommonStrings::None)
{
if ((cachedStrokeShade != actStrokeShade) || (cachedStroke != actStroke))
{
SetQColor(&tmp, actStroke, actStrokeShade);
p->setPen(tmp, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
cachedStrokeQ = tmp;
cachedStroke = actStroke;
cachedStrokeShade = actStrokeShade;
}
else
p->setPen(cachedStrokeQ, 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin);
box = new GlyphBox(run);
box->setAscent(linebox->ascent());
box->setDescent(linebox->descent());
}
p->translate(0.0, BaseOffs);
if (itemRenderText.hasObject(a))
DrawObj_Embedded(p, cullingArea, itemRenderText.charStyle(a), itemRenderText.object(a));
else
drawGlyphs(p, itemRenderText.charStyle(a), itemRenderText.flags(a), glyphs);

box->setMatrix(trafo);
linebox->addBox(box);
}
p->setWorldMatrix(savWM);
p->restore();

MaxChars = a+1;
CurX -= dx;
if (itemRenderText.hasObject(a))
Expand All @@ -366,6 +343,20 @@ void PageItem_PathText::DrawObj_Item(ScPainter *p, QRectF cullingArea)
else
CurX += glyphs.xadvance+itemRenderText.charStyle(a).fontSize() *itemRenderText.charStyle(a).tracking() / 10000.0 + extraOffset;
}

textLayout.addColumn(0);
textLayout.appendLine(linebox);

if (!m_Doc->RePos)
{
int fm = p->fillMode();
p->setFillMode(1);
p->save();
ScreenPainter painter(p, this);
textLayout.render(&painter);
p->setFillMode(fm);
p->restore();
}
}

bool PageItem_PathText::createInfoGroup(QFrame *infoGroup, QGridLayout *infoGroupLayout)
Expand Down
58 changes: 38 additions & 20 deletions scribus/pdflib_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ class PdfPainter: public TextLayoutPainter
uint m_PNr;
const ScPage* m_page;

QByteArray transformToStr(QTransform tr)
{
return FToStr(tr.m11()) + " " + FToStr(-tr.m12()) + " " + FToStr(-tr.m21()) + " " + FToStr(tr.m22()) + " " + FToStr(tr.dx()) + " " + FToStr(-tr.dy());
}

public:
PdfPainter(PageItem *ite, PDFLibCore *pdf, uint num, const ScPage* pag) :
m_glyphBuffer(),
Expand All @@ -143,6 +148,7 @@ class PdfPainter: public TextLayoutPainter
PdfFont pdfFont = m_pdf->UsedFontsP[font().replacementName()];
QByteArray StrokeColor;
QByteArray FillColor;
QTransform transform = matrix();

if (strokeColor().color != CommonStrings::None)
StrokeColor = m_pdf->putColor(strokeColor().color, strokeColor().shade, false);
Expand All @@ -160,7 +166,8 @@ class PdfPainter: public TextLayoutPainter
if (gl.scaleV != 1.0)
m_pathBuffer += "1 0 0 1 0 " + FToStr(((fontSize() - fontSize() * gl.scaleV) / fontSize()) * -1) + " cm\n";

m_pathBuffer += FToStr(qMax(gl.scaleH, 0.1)) + " 0 0 " + FToStr(qMax(gl.scaleV, 0.1)) + " 0 0 cm\n";
transform.scale(qMax(gl.scaleH, 0.1), qMax(gl.scaleV, 0.1));
m_glyphBuffer += transformToStr(transform) + " cm\n";

if (!FillColor.isEmpty())
m_pathBuffer += pdfFont.name + Pdf::toPdf(gl.glyph) + " Do\n";
Expand Down Expand Up @@ -201,7 +208,11 @@ class PdfPainter: public TextLayoutPainter
m_glyphBuffer += FillColor;

m_glyphBuffer += "0 Tr\n";
m_glyphBuffer += FToStr(qMax(gl.scaleH, 0.1)) + " 0 0 " + FToStr(qMax(gl.scaleV, 0.1)) + " " + FToStr(x()) + " " + FToStr(-y()) + " Tm\n";

transform.translate(x(), y());
transform.scale(qMax(gl.scaleH, 0.1), qMax(gl.scaleV, 0.1));
m_glyphBuffer += transformToStr(transform) + " Tm\n";


if (pdfFont.method != Use_Type3 || !FillColor.isEmpty())
{
Expand All @@ -228,6 +239,7 @@ class PdfPainter: public TextLayoutPainter
PdfFont pdfFont = m_pdf->UsedFontsP[font().replacementName()];
QByteArray StrokeColor;
QByteArray FillColor;
QTransform transform = matrix();

if (strokeColor().color != CommonStrings::None)
StrokeColor = m_pdf->putColor(strokeColor().color, strokeColor().shade, false);
Expand All @@ -251,7 +263,8 @@ class PdfPainter: public TextLayoutPainter
if (gl.scaleV != 1.0)
m_pathBuffer += "1 0 0 1 0 " + FToStr(((fontSize() - fontSize() * (gl.scaleV)) / fontSize()) * -1) + " cm\n";

m_pathBuffer += FToStr(qMax(gl.scaleH, 0.1)) + " 0 0 " + FToStr(qMax(gl.scaleV, 0.1)) + " 0 0 cm\n";
transform.scale(qMax(gl.scaleH, 0.1), qMax(gl.scaleV, 0.1));
m_pathBuffer += transformToStr(transform) + " cm\n";

if (!FillColor.isEmpty())
m_pathBuffer += pdfFont.name + Pdf::toPdf(gl.glyph) + " Do\n";
Expand Down Expand Up @@ -331,12 +344,13 @@ class PdfPainter: public TextLayoutPainter
{
m_pathBuffer += "q\n";
m_pathBuffer += FToStr(strokeWidth()) + " w\n[] 0 d\n0 J\n0 j\n";
m_pathBuffer += FToStr(fontSize()) + " 0 0 " + FToStr(fontSize()) + " "+ FToStr(x()) + " " + FToStr(y()) + " cm\n";

transform.scale(fontSize(), fontSize());
transform.translate(x(), y());
if (gl.scaleV != 1.0)
m_pathBuffer += "1 0 0 1 0 " + FToStr(((fontSize() - fontSize() * gl.scaleV) / fontSize()) * -1) + " cm\n";

m_pathBuffer += FToStr(qMax(gl.scaleH, 0.1)) + " 0 0 " + FToStr(qMax(gl.scaleV, 0.1)) + " 0 0 cm\n";
transform.translate(0, ((fontSize() - fontSize() * gl.scaleV) / fontSize()) * -1);
transform.scale(qMax(gl.scaleH, 0.1), qMax(gl.scaleV, 0.1));
m_pathBuffer += transformToStr(transform) + " cm\n";

/* paint outline */
FPointArray outline = font().glyphOutline(gl.glyph);
Expand Down Expand Up @@ -383,7 +397,9 @@ class PdfPainter: public TextLayoutPainter
m_glyphBuffer += "1 Tr\n";
}

m_glyphBuffer += FToStr(qMax(gl.scaleH, 0.1)) + " 0 0 " + FToStr(qMax(gl.scaleV, 0.1)) + " " + FToStr(x()) + " " + FToStr(-y()) + " Tm\n";
transform.translate(x(), y());
transform.scale(qMax(gl.scaleH, 0.1), qMax(gl.scaleV, 0.1));
m_glyphBuffer += transformToStr(transform) + " Tm\n";

if (pdfFont.method != Use_Type3 || !FillColor.isEmpty())
{
Expand All @@ -404,11 +420,16 @@ class PdfPainter: public TextLayoutPainter

void drawLine(QPointF start, QPointF end)
{
QTransform transform = matrix();
transform.translate(x(), y());
m_pathBuffer += "q\n";
m_pathBuffer += transformToStr(transform) + " cm\n";
m_pathBuffer += m_pdf->putColor(strokeColor().color, strokeColor().shade, false);
m_pathBuffer += FToStr(strokeWidth())+" w\n";
m_pathBuffer += FToStr(x() + start.x()) + " " + FToStr(-y() -start.y()) + " m\n";
m_pathBuffer += FToStr(x() + end.x()) + " " + FToStr(-y() - end.y()) + " l\n";
m_pathBuffer += FToStr(start.x()) + " " + FToStr(-start.y()) + " m\n";
m_pathBuffer += FToStr(end.x()) + " " + FToStr(-end.y()) + " l\n";
m_pathBuffer += "S\n";
m_pathBuffer += "Q\n";
}

QByteArray getBuffer()
Expand All @@ -418,11 +439,13 @@ class PdfPainter: public TextLayoutPainter

void drawRect(QRectF rect)
{
QTransform transform = matrix();
// transform.translate(x(), y());
double rectX = x() + rect.x();
double rectY = -y() - rect.y();
m_glyphBuffer += "q\n";
m_glyphBuffer += transformToStr(transform) + " cm\n";
m_glyphBuffer += "n\n";
m_glyphBuffer += m_pdf->SetPathAndClip(m_item);
m_glyphBuffer += m_pdf->putColor(fillColor().color, fillColor().shade, true);
m_glyphBuffer += m_pdf->putColor(strokeColor().color, strokeColor().shade, false);
m_glyphBuffer += FToStr(rectX) + " " + FToStr(rectY) + " m\n";
Expand All @@ -435,11 +458,8 @@ class PdfPainter: public TextLayoutPainter

void drawObject(PageItem* embedded)
{
if (!m_item->asPathText())
{
m_glyphBuffer += "ET\n"+m_pathBuffer;
m_pathBuffer = "";
}
m_glyphBuffer += "ET\n"+m_pathBuffer;
m_pathBuffer = "";

m_pathBuffer += "q\n";
m_pathBuffer += FToStr(scaleH()) + " 0 0 " + FToStr(scaleV()) + " " + FToStr(x()) + " " + FToStr(-y()) + " cm\n";
Expand All @@ -451,10 +471,8 @@ class PdfPainter: public TextLayoutPainter
m_pathBuffer += "Q\n";
m_glyphBuffer += m_pathBuffer+"\n";
m_pathBuffer = "";
if (m_item->asPathText())
m_glyphBuffer += "Q\n";
else
m_glyphBuffer += "BT\n";

m_glyphBuffer += "BT\n";
}
};

Expand Down
Loading

0 comments on commit ab73f1f

Please sign in to comment.