2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2013-01-02 11:13:29 +00:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-09-19 12:28:29 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtGui module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2012-09-19 12:28:29 +00:00
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-04-27 10:05:43 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-05-24 09:34:08 +00:00
|
|
|
** GNU General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qdebug.h"
|
|
|
|
#include "qtextformat.h"
|
|
|
|
#include "qtextformat_p.h"
|
|
|
|
#include "qtextengine_p.h"
|
|
|
|
#include "qabstracttextdocumentlayout.h"
|
|
|
|
#include "qtextlayout.h"
|
|
|
|
#include "qtextboundaryfinder.h"
|
|
|
|
#include "qvarlengtharray.h"
|
|
|
|
#include "qfont.h"
|
|
|
|
#include "qfont_p.h"
|
|
|
|
#include "qfontengine_p.h"
|
|
|
|
#include "qstring.h"
|
|
|
|
#include "qtextdocument_p.h"
|
2011-12-16 18:26:40 +00:00
|
|
|
#include "qrawfont.h"
|
|
|
|
#include "qrawfont_p.h"
|
2011-05-04 07:31:54 +00:00
|
|
|
#include <qguiapplication.h>
|
2012-01-24 15:44:38 +00:00
|
|
|
#include <qinputmethod.h>
|
2012-09-18 21:41:59 +00:00
|
|
|
#include <algorithm>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-01-16 16:40:21 +00:00
|
|
|
#include "qfontengine_qpa_p.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2013-03-09 17:28:20 +00:00
|
|
|
#include <private/qharfbuzz_p.h>
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
2012-04-03 07:43:05 +00:00
|
|
|
static const float smallCapsFraction = 0.7f;
|
2011-12-16 18:26:40 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
namespace {
|
|
|
|
// Helper class used in QTextEngine::itemize
|
|
|
|
// keep it out here to allow us to keep supporting various compilers.
|
|
|
|
class Itemizer {
|
|
|
|
public:
|
|
|
|
Itemizer(const QString &string, const QScriptAnalysis *analysis, QScriptItemArray &items)
|
|
|
|
: m_string(string),
|
|
|
|
m_analysis(analysis),
|
|
|
|
m_items(items),
|
|
|
|
m_splitter(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~Itemizer()
|
|
|
|
{
|
|
|
|
delete m_splitter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// generate the script items
|
|
|
|
/// The caps parameter is used to choose the algoritm of splitting text and assiging roles to the textitems
|
|
|
|
void generate(int start, int length, QFont::Capitalization caps)
|
|
|
|
{
|
2012-10-10 16:57:05 +00:00
|
|
|
if ((int)caps == (int)QFont::SmallCaps)
|
2011-04-27 10:05:43 +00:00
|
|
|
generateScriptItemsSmallCaps(reinterpret_cast<const ushort *>(m_string.unicode()), start, length);
|
|
|
|
else if(caps == QFont::Capitalize)
|
|
|
|
generateScriptItemsCapitalize(start, length);
|
|
|
|
else if(caps != QFont::MixedCase) {
|
|
|
|
generateScriptItemsAndChangeCase(start, length,
|
|
|
|
caps == QFont::AllLowercase ? QScriptAnalysis::Lowercase : QScriptAnalysis::Uppercase);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
generateScriptItems(start, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum { MaxItemLength = 4096 };
|
|
|
|
|
|
|
|
void generateScriptItemsAndChangeCase(int start, int length, QScriptAnalysis::Flags flags)
|
|
|
|
{
|
|
|
|
generateScriptItems(start, length);
|
|
|
|
if (m_items.isEmpty()) // the next loop won't work in that case
|
|
|
|
return;
|
|
|
|
QScriptItemArray::Iterator iter = m_items.end();
|
|
|
|
do {
|
|
|
|
iter--;
|
2012-06-25 03:22:12 +00:00
|
|
|
if (iter->analysis.flags < QScriptAnalysis::LineOrParagraphSeparator)
|
2011-04-27 10:05:43 +00:00
|
|
|
iter->analysis.flags = flags;
|
|
|
|
} while (iter->position > start);
|
|
|
|
}
|
|
|
|
|
|
|
|
void generateScriptItems(int start, int length)
|
|
|
|
{
|
|
|
|
if (!length)
|
|
|
|
return;
|
|
|
|
const int end = start + length;
|
|
|
|
for (int i = start + 1; i < end; ++i) {
|
2012-02-08 10:12:16 +00:00
|
|
|
// According to the unicode spec we should be treating characters in the Common script
|
|
|
|
// (punctuation, spaces, etc) as being the same script as the surrounding text for the
|
|
|
|
// purpose of splitting up text. This is important because, for example, a fullstop
|
|
|
|
// (0x2E) can be used to indicate an abbreviation and so must be treated as part of a
|
|
|
|
// word. Thus it must be passed along with the word in languages that have to calculate
|
|
|
|
// word breaks. For example the thai word "ครม." has no word breaks but the word "ครม"
|
|
|
|
// does.
|
|
|
|
// Unfortuntely because we split up the strings for both wordwrapping and for setting
|
|
|
|
// the font and because Japanese and Chinese are also aliases of the script "Common",
|
|
|
|
// doing this would break too many things. So instead we only pass the full stop
|
|
|
|
// along, and nothing else.
|
|
|
|
if (m_analysis[i].bidiLevel == m_analysis[start].bidiLevel
|
|
|
|
&& m_analysis[i].flags == m_analysis[start].flags
|
2013-03-12 16:56:08 +00:00
|
|
|
&& (m_analysis[i].script == m_analysis[start].script || m_string[i] == QLatin1Char('.'))
|
2011-04-27 10:05:43 +00:00
|
|
|
&& m_analysis[i].flags < QScriptAnalysis::SpaceTabOrObject
|
|
|
|
&& i - start < MaxItemLength)
|
|
|
|
continue;
|
|
|
|
m_items.append(QScriptItem(start, m_analysis[start]));
|
|
|
|
start = i;
|
|
|
|
}
|
|
|
|
m_items.append(QScriptItem(start, m_analysis[start]));
|
|
|
|
}
|
|
|
|
|
|
|
|
void generateScriptItemsCapitalize(int start, int length)
|
|
|
|
{
|
|
|
|
if (!length)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!m_splitter)
|
|
|
|
m_splitter = new QTextBoundaryFinder(QTextBoundaryFinder::Word,
|
|
|
|
m_string.constData(), m_string.length(),
|
|
|
|
/*buffer*/0, /*buffer size*/0);
|
|
|
|
|
|
|
|
m_splitter->setPosition(start);
|
|
|
|
QScriptAnalysis itemAnalysis = m_analysis[start];
|
|
|
|
|
2012-10-08 13:51:41 +00:00
|
|
|
if (m_splitter->boundaryReasons() & QTextBoundaryFinder::StartOfItem)
|
2011-04-27 10:05:43 +00:00
|
|
|
itemAnalysis.flags = QScriptAnalysis::Uppercase;
|
2012-10-08 13:51:41 +00:00
|
|
|
|
|
|
|
m_splitter->toNextBoundary();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
const int end = start + length;
|
|
|
|
for (int i = start + 1; i < end; ++i) {
|
2012-10-08 13:51:41 +00:00
|
|
|
bool atWordStart = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (i == m_splitter->position()) {
|
2012-10-08 13:51:41 +00:00
|
|
|
if (m_splitter->boundaryReasons() & QTextBoundaryFinder::StartOfItem) {
|
|
|
|
Q_ASSERT(m_analysis[i].flags < QScriptAnalysis::TabOrObject);
|
|
|
|
atWordStart = true;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
m_splitter->toNextBoundary();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_analysis[i] == itemAnalysis
|
|
|
|
&& m_analysis[i].flags < QScriptAnalysis::TabOrObject
|
2012-10-08 13:51:41 +00:00
|
|
|
&& !atWordStart
|
2011-04-27 10:05:43 +00:00
|
|
|
&& i - start < MaxItemLength)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
m_items.append(QScriptItem(start, itemAnalysis));
|
|
|
|
start = i;
|
|
|
|
itemAnalysis = m_analysis[start];
|
|
|
|
|
2012-10-08 13:51:41 +00:00
|
|
|
if (atWordStart)
|
2011-04-27 10:05:43 +00:00
|
|
|
itemAnalysis.flags = QScriptAnalysis::Uppercase;
|
|
|
|
}
|
|
|
|
m_items.append(QScriptItem(start, itemAnalysis));
|
|
|
|
}
|
|
|
|
|
|
|
|
void generateScriptItemsSmallCaps(const ushort *uc, int start, int length)
|
|
|
|
{
|
|
|
|
if (!length)
|
|
|
|
return;
|
|
|
|
bool lower = (QChar::category(uc[start]) == QChar::Letter_Lowercase);
|
|
|
|
const int end = start + length;
|
|
|
|
// split text into parts that are already uppercase and parts that are lowercase, and mark the latter to be uppercased later.
|
|
|
|
for (int i = start + 1; i < end; ++i) {
|
|
|
|
bool l = (QChar::category(uc[i]) == QChar::Letter_Lowercase);
|
|
|
|
if ((m_analysis[i] == m_analysis[start])
|
|
|
|
&& m_analysis[i].flags < QScriptAnalysis::TabOrObject
|
|
|
|
&& l == lower
|
|
|
|
&& i - start < MaxItemLength)
|
|
|
|
continue;
|
|
|
|
m_items.append(QScriptItem(start, m_analysis[start]));
|
|
|
|
if (lower)
|
|
|
|
m_items.last().analysis.flags = QScriptAnalysis::SmallCaps;
|
|
|
|
|
|
|
|
start = i;
|
|
|
|
lower = l;
|
|
|
|
}
|
|
|
|
m_items.append(QScriptItem(start, m_analysis[start]));
|
|
|
|
if (lower)
|
|
|
|
m_items.last().analysis.flags = QScriptAnalysis::SmallCaps;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &m_string;
|
|
|
|
const QScriptAnalysis * const m_analysis;
|
|
|
|
QScriptItemArray &m_items;
|
|
|
|
QTextBoundaryFinder *m_splitter;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// The BiDi algorithm
|
|
|
|
//
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#define BIDI_DEBUG 0
|
|
|
|
#if (BIDI_DEBUG >= 1)
|
|
|
|
QT_BEGIN_INCLUDE_NAMESPACE
|
|
|
|
#include <iostream>
|
|
|
|
QT_END_INCLUDE_NAMESPACE
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
static const char *directions[] = {
|
|
|
|
"DirL", "DirR", "DirEN", "DirES", "DirET", "DirAN", "DirCS", "DirB", "DirS", "DirWS", "DirON",
|
|
|
|
"DirLRE", "DirLRO", "DirAL", "DirRLE", "DirRLO", "DirPDF", "DirNSM", "DirBN"
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct QBidiStatus {
|
|
|
|
QBidiStatus() {
|
|
|
|
eor = QChar::DirON;
|
|
|
|
lastStrong = QChar::DirON;
|
|
|
|
last = QChar:: DirON;
|
|
|
|
dir = QChar::DirON;
|
|
|
|
}
|
|
|
|
QChar::Direction eor;
|
|
|
|
QChar::Direction lastStrong;
|
|
|
|
QChar::Direction last;
|
|
|
|
QChar::Direction dir;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum { MaxBidiLevel = 61 };
|
|
|
|
|
|
|
|
struct QBidiControl {
|
|
|
|
inline QBidiControl(bool rtl)
|
|
|
|
: cCtx(0), base(rtl ? 1 : 0), level(rtl ? 1 : 0), override(false) {}
|
|
|
|
|
|
|
|
inline void embed(bool rtl, bool o = false) {
|
|
|
|
unsigned int toAdd = 1;
|
|
|
|
if((level%2 != 0) == rtl ) {
|
|
|
|
++toAdd;
|
|
|
|
}
|
|
|
|
if (level + toAdd <= MaxBidiLevel) {
|
|
|
|
ctx[cCtx].level = level;
|
|
|
|
ctx[cCtx].override = override;
|
|
|
|
cCtx++;
|
|
|
|
override = o;
|
|
|
|
level += toAdd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline bool canPop() const { return cCtx != 0; }
|
|
|
|
inline void pdf() {
|
|
|
|
Q_ASSERT(cCtx);
|
|
|
|
--cCtx;
|
|
|
|
level = ctx[cCtx].level;
|
|
|
|
override = ctx[cCtx].override;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QChar::Direction basicDirection() const {
|
|
|
|
return (base ? QChar::DirR : QChar:: DirL);
|
|
|
|
}
|
|
|
|
inline unsigned int baseLevel() const {
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
inline QChar::Direction direction() const {
|
|
|
|
return ((level%2) ? QChar::DirR : QChar:: DirL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
|
|
|
unsigned int level;
|
|
|
|
bool override;
|
|
|
|
} ctx[MaxBidiLevel];
|
|
|
|
unsigned int cCtx;
|
|
|
|
const unsigned int base;
|
|
|
|
unsigned int level;
|
|
|
|
bool override;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void appendItems(QScriptAnalysis *analysis, int &start, int &stop, const QBidiControl &control, QChar::Direction dir)
|
|
|
|
{
|
|
|
|
if (start > stop)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int level = control.level;
|
|
|
|
|
|
|
|
if(dir != QChar::DirON && !control.override) {
|
|
|
|
// add level of run (cases I1 & I2)
|
|
|
|
if(level % 2) {
|
|
|
|
if(dir == QChar::DirL || dir == QChar::DirAN || dir == QChar::DirEN)
|
|
|
|
level++;
|
|
|
|
} else {
|
|
|
|
if(dir == QChar::DirR)
|
|
|
|
level++;
|
|
|
|
else if(dir == QChar::DirAN || dir == QChar::DirEN)
|
|
|
|
level += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (BIDI_DEBUG >= 1)
|
|
|
|
qDebug("new run: dir=%s from %d, to %d level = %d override=%d", directions[dir], start, stop, level, control.override);
|
|
|
|
#endif
|
|
|
|
QScriptAnalysis *s = analysis + start;
|
|
|
|
const QScriptAnalysis *e = analysis + stop;
|
|
|
|
while (s <= e) {
|
|
|
|
s->bidiLevel = level;
|
|
|
|
++s;
|
|
|
|
}
|
|
|
|
++stop;
|
|
|
|
start = stop;
|
|
|
|
}
|
|
|
|
|
2011-06-22 12:23:47 +00:00
|
|
|
static QChar::Direction skipBoundryNeutrals(QScriptAnalysis *analysis,
|
|
|
|
const ushort *unicode, int length,
|
|
|
|
int &sor, int &eor, QBidiControl &control)
|
|
|
|
{
|
2011-07-12 07:31:07 +00:00
|
|
|
QChar::Direction dir = control.basicDirection();
|
2011-06-22 12:23:47 +00:00
|
|
|
int level = sor > 0 ? analysis[sor - 1].bidiLevel : control.level;
|
|
|
|
while (sor < length) {
|
|
|
|
dir = QChar::direction(unicode[sor]);
|
|
|
|
// Keep skipping DirBN as if it doesn't exist
|
|
|
|
if (dir != QChar::DirBN)
|
|
|
|
break;
|
|
|
|
analysis[sor++].bidiLevel = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
eor = sor;
|
|
|
|
if (eor == length)
|
|
|
|
dir = control.basicDirection();
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// creates the next QScript items.
|
|
|
|
static bool bidiItemize(QTextEngine *engine, QScriptAnalysis *analysis, QBidiControl &control)
|
|
|
|
{
|
|
|
|
bool rightToLeft = (control.basicDirection() == 1);
|
|
|
|
bool hasBidi = rightToLeft;
|
|
|
|
#if BIDI_DEBUG >= 2
|
|
|
|
qDebug() << "bidiItemize: rightToLeft=" << rightToLeft << engine->layoutData->string;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int sor = 0;
|
|
|
|
int eor = -1;
|
|
|
|
|
|
|
|
|
|
|
|
int length = engine->layoutData->string.length();
|
|
|
|
|
|
|
|
const ushort *unicode = (const ushort *)engine->layoutData->string.unicode();
|
|
|
|
int current = 0;
|
|
|
|
|
|
|
|
QChar::Direction dir = rightToLeft ? QChar::DirR : QChar::DirL;
|
|
|
|
QBidiStatus status;
|
|
|
|
|
|
|
|
QChar::Direction sdir = QChar::direction(*unicode);
|
|
|
|
if (sdir != QChar::DirL && sdir != QChar::DirR && sdir != QChar::DirEN && sdir != QChar::DirAN)
|
|
|
|
sdir = QChar::DirON;
|
|
|
|
else
|
|
|
|
dir = QChar::DirON;
|
|
|
|
status.eor = sdir;
|
|
|
|
status.lastStrong = rightToLeft ? QChar::DirR : QChar::DirL;
|
|
|
|
status.last = status.lastStrong;
|
|
|
|
status.dir = sdir;
|
|
|
|
|
|
|
|
|
|
|
|
while (current <= length) {
|
|
|
|
|
|
|
|
QChar::Direction dirCurrent;
|
|
|
|
if (current == (int)length)
|
|
|
|
dirCurrent = control.basicDirection();
|
|
|
|
else
|
|
|
|
dirCurrent = QChar::direction(unicode[current]);
|
|
|
|
|
|
|
|
#if (BIDI_DEBUG >= 2)
|
|
|
|
// qDebug() << "pos=" << current << " dir=" << directions[dir]
|
|
|
|
// << " current=" << directions[dirCurrent] << " last=" << directions[status.last]
|
|
|
|
// << " eor=" << eor << '/' << directions[status.eor]
|
|
|
|
// << " sor=" << sor << " lastStrong="
|
|
|
|
// << directions[status.lastStrong]
|
|
|
|
// << " level=" << (int)control.level << " override=" << (bool)control.override;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch(dirCurrent) {
|
|
|
|
|
|
|
|
// embedding and overrides (X1-X9 in the BiDi specs)
|
|
|
|
case QChar::DirRLE:
|
|
|
|
case QChar::DirRLO:
|
|
|
|
case QChar::DirLRE:
|
|
|
|
case QChar::DirLRO:
|
|
|
|
{
|
|
|
|
bool rtl = (dirCurrent == QChar::DirRLE || dirCurrent == QChar::DirRLO);
|
|
|
|
hasBidi |= rtl;
|
|
|
|
bool override = (dirCurrent == QChar::DirLRO || dirCurrent == QChar::DirRLO);
|
|
|
|
|
|
|
|
unsigned int level = control.level+1;
|
|
|
|
if ((level%2 != 0) == rtl) ++level;
|
|
|
|
if(level < MaxBidiLevel) {
|
|
|
|
eor = current-1;
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
eor = current;
|
|
|
|
control.embed(rtl, override);
|
|
|
|
QChar::Direction edir = (rtl ? QChar::DirR : QChar::DirL);
|
|
|
|
dir = status.eor = edir;
|
|
|
|
status.lastStrong = edir;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QChar::DirPDF:
|
|
|
|
{
|
|
|
|
if (control.canPop()) {
|
|
|
|
if (dir != control.direction()) {
|
|
|
|
eor = current-1;
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
dir = control.direction();
|
|
|
|
}
|
|
|
|
eor = current;
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
control.pdf();
|
|
|
|
dir = QChar::DirON; status.eor = QChar::DirON;
|
|
|
|
status.last = control.direction();
|
|
|
|
if (control.override)
|
|
|
|
dir = control.direction();
|
|
|
|
else
|
|
|
|
dir = QChar::DirON;
|
|
|
|
status.lastStrong = control.direction();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// strong types
|
|
|
|
case QChar::DirL:
|
|
|
|
if(dir == QChar::DirON)
|
|
|
|
dir = QChar::DirL;
|
|
|
|
switch(status.last)
|
|
|
|
{
|
|
|
|
case QChar::DirL:
|
|
|
|
eor = current; status.eor = QChar::DirL; break;
|
|
|
|
case QChar::DirR:
|
|
|
|
case QChar::DirAL:
|
|
|
|
case QChar::DirEN:
|
|
|
|
case QChar::DirAN:
|
|
|
|
if (eor >= 0) {
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
2011-06-22 12:23:47 +00:00
|
|
|
status.eor = dir = skipBoundryNeutrals(analysis, unicode, length, sor, eor, control);
|
2011-04-27 10:05:43 +00:00
|
|
|
} else {
|
|
|
|
eor = current; status.eor = dir;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QChar::DirES:
|
|
|
|
case QChar::DirET:
|
|
|
|
case QChar::DirCS:
|
|
|
|
case QChar::DirBN:
|
|
|
|
case QChar::DirB:
|
|
|
|
case QChar::DirS:
|
|
|
|
case QChar::DirWS:
|
|
|
|
case QChar::DirON:
|
|
|
|
if(dir != QChar::DirL) {
|
|
|
|
//last stuff takes embedding dir
|
|
|
|
if(control.direction() == QChar::DirR) {
|
|
|
|
if(status.eor != QChar::DirR) {
|
|
|
|
// AN or EN
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
status.eor = QChar::DirON;
|
|
|
|
dir = QChar::DirR;
|
|
|
|
}
|
|
|
|
eor = current - 1;
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
2011-06-22 12:23:47 +00:00
|
|
|
status.eor = dir = skipBoundryNeutrals(analysis, unicode, length, sor, eor, control);
|
2011-04-27 10:05:43 +00:00
|
|
|
} else {
|
|
|
|
if(status.eor != QChar::DirL) {
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
status.eor = QChar::DirON;
|
|
|
|
dir = QChar::DirL;
|
|
|
|
} else {
|
|
|
|
eor = current; status.eor = QChar::DirL; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eor = current; status.eor = QChar::DirL;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
status.lastStrong = QChar::DirL;
|
|
|
|
break;
|
|
|
|
case QChar::DirAL:
|
|
|
|
case QChar::DirR:
|
|
|
|
hasBidi = true;
|
|
|
|
if(dir == QChar::DirON) dir = QChar::DirR;
|
|
|
|
switch(status.last)
|
|
|
|
{
|
|
|
|
case QChar::DirL:
|
|
|
|
case QChar::DirEN:
|
|
|
|
case QChar::DirAN:
|
|
|
|
if (eor >= 0)
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
// fall through
|
|
|
|
case QChar::DirR:
|
|
|
|
case QChar::DirAL:
|
|
|
|
dir = QChar::DirR; eor = current; status.eor = QChar::DirR; break;
|
|
|
|
case QChar::DirES:
|
|
|
|
case QChar::DirET:
|
|
|
|
case QChar::DirCS:
|
|
|
|
case QChar::DirBN:
|
|
|
|
case QChar::DirB:
|
|
|
|
case QChar::DirS:
|
|
|
|
case QChar::DirWS:
|
|
|
|
case QChar::DirON:
|
|
|
|
if(status.eor != QChar::DirR && status.eor != QChar::DirAL) {
|
|
|
|
//last stuff takes embedding dir
|
|
|
|
if(control.direction() == QChar::DirR
|
|
|
|
|| status.lastStrong == QChar::DirR || status.lastStrong == QChar::DirAL) {
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
dir = QChar::DirR; status.eor = QChar::DirON;
|
|
|
|
eor = current;
|
|
|
|
} else {
|
|
|
|
eor = current - 1;
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
dir = QChar::DirR; status.eor = QChar::DirON;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eor = current; status.eor = QChar::DirR;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
status.lastStrong = dirCurrent;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// weak types:
|
|
|
|
|
|
|
|
case QChar::DirNSM:
|
|
|
|
if (eor == current-1)
|
|
|
|
eor = current;
|
|
|
|
break;
|
|
|
|
case QChar::DirEN:
|
|
|
|
// if last strong was AL change EN to AN
|
|
|
|
if(status.lastStrong != QChar::DirAL) {
|
|
|
|
if(dir == QChar::DirON) {
|
|
|
|
if(status.lastStrong == QChar::DirL)
|
|
|
|
dir = QChar::DirL;
|
|
|
|
else
|
|
|
|
dir = QChar::DirEN;
|
|
|
|
}
|
|
|
|
switch(status.last)
|
|
|
|
{
|
|
|
|
case QChar::DirET:
|
|
|
|
if (status.lastStrong == QChar::DirR || status.lastStrong == QChar::DirAL) {
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
status.eor = QChar::DirON;
|
|
|
|
dir = QChar::DirAN;
|
|
|
|
}
|
|
|
|
// fall through
|
|
|
|
case QChar::DirEN:
|
|
|
|
case QChar::DirL:
|
|
|
|
eor = current;
|
|
|
|
status.eor = dirCurrent;
|
|
|
|
break;
|
|
|
|
case QChar::DirR:
|
|
|
|
case QChar::DirAL:
|
|
|
|
case QChar::DirAN:
|
|
|
|
if (eor >= 0)
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
else
|
|
|
|
eor = current;
|
|
|
|
status.eor = QChar::DirEN;
|
|
|
|
dir = QChar::DirAN; break;
|
|
|
|
case QChar::DirES:
|
|
|
|
case QChar::DirCS:
|
|
|
|
if(status.eor == QChar::DirEN || dir == QChar::DirAN) {
|
|
|
|
eor = current; break;
|
|
|
|
}
|
|
|
|
case QChar::DirBN:
|
|
|
|
case QChar::DirB:
|
|
|
|
case QChar::DirS:
|
|
|
|
case QChar::DirWS:
|
|
|
|
case QChar::DirON:
|
|
|
|
if(status.eor == QChar::DirR) {
|
|
|
|
// neutrals go to R
|
|
|
|
eor = current - 1;
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
dir = QChar::DirON; status.eor = QChar::DirEN;
|
|
|
|
dir = QChar::DirAN;
|
|
|
|
}
|
|
|
|
else if(status.eor == QChar::DirL ||
|
|
|
|
(status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) {
|
|
|
|
eor = current; status.eor = dirCurrent;
|
|
|
|
} else {
|
|
|
|
// numbers on both sides, neutrals get right to left direction
|
|
|
|
if(dir != QChar::DirL) {
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
dir = QChar::DirON; status.eor = QChar::DirON;
|
|
|
|
eor = current - 1;
|
|
|
|
dir = QChar::DirR;
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
dir = QChar::DirON; status.eor = QChar::DirON;
|
|
|
|
dir = QChar::DirAN;
|
|
|
|
} else {
|
|
|
|
eor = current; status.eor = dirCurrent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QChar::DirAN:
|
|
|
|
hasBidi = true;
|
|
|
|
dirCurrent = QChar::DirAN;
|
|
|
|
if(dir == QChar::DirON) dir = QChar::DirAN;
|
|
|
|
switch(status.last)
|
|
|
|
{
|
|
|
|
case QChar::DirL:
|
|
|
|
case QChar::DirAN:
|
|
|
|
eor = current; status.eor = QChar::DirAN; break;
|
|
|
|
case QChar::DirR:
|
|
|
|
case QChar::DirAL:
|
|
|
|
case QChar::DirEN:
|
|
|
|
if (eor >= 0){
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
} else {
|
|
|
|
eor = current;
|
|
|
|
}
|
|
|
|
dir = QChar::DirAN; status.eor = QChar::DirAN;
|
|
|
|
break;
|
|
|
|
case QChar::DirCS:
|
|
|
|
if(status.eor == QChar::DirAN) {
|
|
|
|
eor = current; break;
|
|
|
|
}
|
|
|
|
case QChar::DirES:
|
|
|
|
case QChar::DirET:
|
|
|
|
case QChar::DirBN:
|
|
|
|
case QChar::DirB:
|
|
|
|
case QChar::DirS:
|
|
|
|
case QChar::DirWS:
|
|
|
|
case QChar::DirON:
|
|
|
|
if(status.eor == QChar::DirR) {
|
|
|
|
// neutrals go to R
|
|
|
|
eor = current - 1;
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
status.eor = QChar::DirAN;
|
|
|
|
dir = QChar::DirAN;
|
|
|
|
} else if(status.eor == QChar::DirL ||
|
|
|
|
(status.eor == QChar::DirEN && status.lastStrong == QChar::DirL)) {
|
|
|
|
eor = current; status.eor = dirCurrent;
|
|
|
|
} else {
|
|
|
|
// numbers on both sides, neutrals get right to left direction
|
|
|
|
if(dir != QChar::DirL) {
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
status.eor = QChar::DirON;
|
|
|
|
eor = current - 1;
|
|
|
|
dir = QChar::DirR;
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
status.eor = QChar::DirAN;
|
|
|
|
dir = QChar::DirAN;
|
|
|
|
} else {
|
|
|
|
eor = current; status.eor = dirCurrent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QChar::DirES:
|
|
|
|
case QChar::DirCS:
|
|
|
|
break;
|
|
|
|
case QChar::DirET:
|
|
|
|
if(status.last == QChar::DirEN) {
|
|
|
|
dirCurrent = QChar::DirEN;
|
|
|
|
eor = current; status.eor = dirCurrent;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// boundary neutrals should be ignored
|
|
|
|
case QChar::DirBN:
|
|
|
|
break;
|
|
|
|
// neutrals
|
|
|
|
case QChar::DirB:
|
|
|
|
// ### what do we do with newline and paragraph separators that come to here?
|
|
|
|
break;
|
|
|
|
case QChar::DirS:
|
|
|
|
// ### implement rule L1
|
|
|
|
break;
|
|
|
|
case QChar::DirWS:
|
|
|
|
case QChar::DirON:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//qDebug() << " after: dir=" << // dir << " current=" << dirCurrent << " last=" << status.last << " eor=" << status.eor << " lastStrong=" << status.lastStrong << " embedding=" << control.direction();
|
|
|
|
|
|
|
|
if(current >= (int)length) break;
|
|
|
|
|
|
|
|
// set status.last as needed.
|
|
|
|
switch(dirCurrent) {
|
|
|
|
case QChar::DirET:
|
|
|
|
case QChar::DirES:
|
|
|
|
case QChar::DirCS:
|
|
|
|
case QChar::DirS:
|
|
|
|
case QChar::DirWS:
|
|
|
|
case QChar::DirON:
|
|
|
|
switch(status.last)
|
|
|
|
{
|
|
|
|
case QChar::DirL:
|
|
|
|
case QChar::DirR:
|
|
|
|
case QChar::DirAL:
|
|
|
|
case QChar::DirEN:
|
|
|
|
case QChar::DirAN:
|
|
|
|
status.last = dirCurrent;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
status.last = QChar::DirON;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QChar::DirNSM:
|
|
|
|
case QChar::DirBN:
|
|
|
|
// ignore these
|
|
|
|
break;
|
|
|
|
case QChar::DirLRO:
|
|
|
|
case QChar::DirLRE:
|
|
|
|
status.last = QChar::DirL;
|
|
|
|
break;
|
|
|
|
case QChar::DirRLO:
|
|
|
|
case QChar::DirRLE:
|
|
|
|
status.last = QChar::DirR;
|
|
|
|
break;
|
|
|
|
case QChar::DirEN:
|
|
|
|
if (status.last == QChar::DirL) {
|
|
|
|
status.last = QChar::DirL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through
|
|
|
|
default:
|
|
|
|
status.last = dirCurrent;
|
|
|
|
}
|
|
|
|
|
|
|
|
++current;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (BIDI_DEBUG >= 1)
|
|
|
|
qDebug() << "reached end of line current=" << current << ", eor=" << eor;
|
|
|
|
#endif
|
|
|
|
eor = current - 1; // remove dummy char
|
|
|
|
|
|
|
|
if (sor <= eor)
|
|
|
|
appendItems(analysis, sor, eor, control, dir);
|
|
|
|
|
|
|
|
return hasBidi;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::bidiReorder(int numItems, const quint8 *levels, int *visualOrder)
|
|
|
|
{
|
|
|
|
|
|
|
|
// first find highest and lowest levels
|
|
|
|
quint8 levelLow = 128;
|
|
|
|
quint8 levelHigh = 0;
|
|
|
|
int i = 0;
|
|
|
|
while (i < numItems) {
|
|
|
|
//printf("level = %d\n", r->level);
|
|
|
|
if (levels[i] > levelHigh)
|
|
|
|
levelHigh = levels[i];
|
|
|
|
if (levels[i] < levelLow)
|
|
|
|
levelLow = levels[i];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// implements reordering of the line (L2 according to BiDi spec):
|
|
|
|
// L2. From the highest level found in the text to the lowest odd level on each line,
|
|
|
|
// reverse any contiguous sequence of characters that are at that level or higher.
|
|
|
|
|
|
|
|
// reversing is only done up to the lowest odd level
|
|
|
|
if(!(levelLow%2)) levelLow++;
|
|
|
|
|
|
|
|
#if (BIDI_DEBUG >= 1)
|
|
|
|
// qDebug() << "reorderLine: lineLow = " << (uint)levelLow << ", lineHigh = " << (uint)levelHigh;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int count = numItems - 1;
|
|
|
|
for (i = 0; i < numItems; i++)
|
|
|
|
visualOrder[i] = i;
|
|
|
|
|
|
|
|
while(levelHigh >= levelLow) {
|
|
|
|
int i = 0;
|
|
|
|
while (i < count) {
|
|
|
|
while(i < count && levels[i] < levelHigh) i++;
|
|
|
|
int start = i;
|
|
|
|
while(i <= count && levels[i] >= levelHigh) i++;
|
|
|
|
int end = i-1;
|
|
|
|
|
|
|
|
if(start != end) {
|
|
|
|
//qDebug() << "reversing from " << start << " to " << end;
|
|
|
|
for(int j = 0; j < (end-start+1)/2; j++) {
|
|
|
|
int tmp = visualOrder[start+j];
|
|
|
|
visualOrder[start+j] = visualOrder[end-j];
|
|
|
|
visualOrder[end-j] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
levelHigh--;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (BIDI_DEBUG >= 1)
|
|
|
|
// qDebug() << "visual order is:";
|
|
|
|
// for (i = 0; i < numItems; i++)
|
|
|
|
// qDebug() << visualOrder[i];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// ask the font engine to find out which glyphs (as an index in the specific font) to use for the text in one item.
|
|
|
|
static bool stringToGlyphs(HB_ShaperItem *item, QGlyphLayout *glyphs, QFontEngine *fontEngine)
|
|
|
|
{
|
|
|
|
int nGlyphs = item->num_glyphs;
|
|
|
|
|
2012-07-09 13:50:32 +00:00
|
|
|
QFontEngine::ShaperFlags shaperFlags(QFontEngine::GlyphIndicesOnly);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (item->item.bidiLevel % 2)
|
2012-07-09 13:50:32 +00:00
|
|
|
shaperFlags |= QFontEngine::RightToLeft;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
bool result = fontEngine->stringToCMap(reinterpret_cast<const QChar *>(item->string + item->item.pos), item->item.length, glyphs, &nGlyphs, shaperFlags);
|
|
|
|
item->num_glyphs = nGlyphs;
|
|
|
|
glyphs->numGlyphs = nGlyphs;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// shape all the items that intersect with the line, taking tab widths into account to find out what text actually fits in the line.
|
|
|
|
void QTextEngine::shapeLine(const QScriptLine &line)
|
|
|
|
{
|
|
|
|
QFixed x;
|
|
|
|
bool first = true;
|
|
|
|
const int end = findItem(line.from + line.length - 1);
|
|
|
|
int item = findItem(line.from);
|
|
|
|
if (item == -1)
|
|
|
|
return;
|
|
|
|
for (item = findItem(line.from); item <= end; ++item) {
|
|
|
|
QScriptItem &si = layoutData->items[item];
|
|
|
|
if (si.analysis.flags == QScriptAnalysis::Tab) {
|
|
|
|
ensureSpace(1);
|
|
|
|
si.width = calculateTabWidth(item, x);
|
|
|
|
} else {
|
|
|
|
shape(item);
|
|
|
|
}
|
|
|
|
if (first && si.position != line.from) { // that means our x position has to be offset
|
|
|
|
QGlyphLayout glyphs = shapedGlyphs(&si);
|
|
|
|
Q_ASSERT(line.from > si.position);
|
|
|
|
for (int i = line.from - si.position - 1; i >= 0; i--) {
|
|
|
|
x -= glyphs.effectiveAdvance(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
x += si.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-18 15:08:30 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void QTextEngine::shapeText(int item) const
|
|
|
|
{
|
|
|
|
Q_ASSERT(item < layoutData->items.size());
|
|
|
|
QScriptItem &si = layoutData->items[item];
|
|
|
|
|
|
|
|
if (si.num_glyphs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
shapeTextWithHarfbuzz(item);
|
|
|
|
|
|
|
|
si.width = 0;
|
|
|
|
|
|
|
|
if (!si.num_glyphs)
|
|
|
|
return;
|
|
|
|
QGlyphLayout glyphs = shapedGlyphs(&si);
|
|
|
|
|
2011-12-16 18:26:40 +00:00
|
|
|
bool letterSpacingIsAbsolute;
|
|
|
|
QFixed letterSpacing, wordSpacing;
|
|
|
|
#ifndef QT_NO_RAWFONT
|
|
|
|
if (useRawFont) {
|
|
|
|
QTextCharFormat f = format(&si);
|
|
|
|
wordSpacing = QFixed::fromReal(f.fontWordSpacing());
|
|
|
|
letterSpacing = QFixed::fromReal(f.fontLetterSpacing());
|
|
|
|
letterSpacingIsAbsolute = true;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
QFont font = this->font(si);
|
|
|
|
letterSpacingIsAbsolute = font.d->letterSpacingIsAbsolute;
|
|
|
|
letterSpacing = font.d->letterSpacing;
|
|
|
|
wordSpacing = font.d->wordSpacing;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-12-16 18:26:40 +00:00
|
|
|
if (letterSpacingIsAbsolute && letterSpacing.value())
|
|
|
|
letterSpacing *= font.d->dpi / qt_defaultDpiY();
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (letterSpacing != 0) {
|
|
|
|
for (int i = 1; i < si.num_glyphs; ++i) {
|
|
|
|
if (glyphs.attributes[i].clusterStart) {
|
|
|
|
if (letterSpacingIsAbsolute)
|
|
|
|
glyphs.advances_x[i-1] += letterSpacing;
|
|
|
|
else {
|
|
|
|
QFixed &advance = glyphs.advances_x[i-1];
|
|
|
|
advance += (letterSpacing - 100) * advance / 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (letterSpacingIsAbsolute)
|
|
|
|
glyphs.advances_x[si.num_glyphs-1] += letterSpacing;
|
|
|
|
else {
|
|
|
|
QFixed &advance = glyphs.advances_x[si.num_glyphs-1];
|
|
|
|
advance += (letterSpacing - 100) * advance / 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wordSpacing != 0) {
|
|
|
|
for (int i = 0; i < si.num_glyphs; ++i) {
|
|
|
|
if (glyphs.attributes[i].justification == HB_Space
|
|
|
|
|| glyphs.attributes[i].justification == HB_Arabic_Space) {
|
|
|
|
// word spacing only gets added once to a consecutive run of spaces (see CSS spec)
|
|
|
|
if (i + 1 == si.num_glyphs
|
|
|
|
||(glyphs.attributes[i+1].justification != HB_Space
|
|
|
|
&& glyphs.attributes[i+1].justification != HB_Arabic_Space))
|
|
|
|
glyphs.advances_x[i] += wordSpacing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < si.num_glyphs; ++i)
|
2011-09-08 08:14:33 +00:00
|
|
|
si.width += glyphs.advances_x[i] * !glyphs.attributes[i].dontPrint;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool hasCaseChange(const QScriptItem &si)
|
|
|
|
{
|
|
|
|
return si.analysis.flags == QScriptAnalysis::SmallCaps ||
|
|
|
|
si.analysis.flags == QScriptAnalysis::Uppercase ||
|
|
|
|
si.analysis.flags == QScriptAnalysis::Lowercase;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void moveGlyphData(const QGlyphLayout &destination, const QGlyphLayout &source, int num)
|
|
|
|
{
|
|
|
|
if (num > 0 && destination.glyphs != source.glyphs) {
|
2013-03-09 17:28:20 +00:00
|
|
|
memmove(destination.glyphs, source.glyphs, num * sizeof(glyph_t));
|
|
|
|
memmove(destination.attributes, source.attributes, num * sizeof(QGlyphAttributes));
|
|
|
|
memmove(destination.advances_x, source.advances_x, num * sizeof(QFixed));
|
|
|
|
memmove(destination.offsets, source.offsets, num * sizeof(QFixedPoint));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-09 17:28:20 +00:00
|
|
|
Q_STATIC_ASSERT(sizeof(HB_Glyph) == sizeof(glyph_t));
|
|
|
|
Q_STATIC_ASSERT(sizeof(HB_GlyphAttributes) == sizeof(QGlyphAttributes));
|
|
|
|
Q_STATIC_ASSERT(sizeof(HB_Fixed) == sizeof(QFixed));
|
|
|
|
Q_STATIC_ASSERT(sizeof(HB_FixedPoint) == sizeof(QFixedPoint));
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
/// take the item from layoutData->items and
|
|
|
|
void QTextEngine::shapeTextWithHarfbuzz(int item) const
|
|
|
|
{
|
|
|
|
QScriptItem &si = layoutData->items[item];
|
|
|
|
|
|
|
|
si.glyph_data_offset = layoutData->used;
|
|
|
|
|
|
|
|
QFontEngine *font = fontEngine(si, &si.ascent, &si.descent, &si.leading);
|
|
|
|
|
2011-12-16 18:26:40 +00:00
|
|
|
bool kerningEnabled;
|
|
|
|
#ifndef QT_NO_RAWFONT
|
|
|
|
if (useRawFont) {
|
|
|
|
QTextCharFormat f = format(&si);
|
|
|
|
kerningEnabled = f.fontKerning();
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
kerningEnabled = this->font(si).d->kerning;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
HB_ShaperItem entire_shaper_item;
|
2012-04-06 14:34:19 +00:00
|
|
|
memset(&entire_shaper_item, 0, sizeof(entire_shaper_item));
|
2011-04-27 10:05:43 +00:00
|
|
|
entire_shaper_item.string = reinterpret_cast<const HB_UChar16 *>(layoutData->string.constData());
|
|
|
|
entire_shaper_item.stringLength = layoutData->string.length();
|
2012-12-21 16:35:58 +00:00
|
|
|
entire_shaper_item.item.script = script_to_hbscript(si.analysis.script);
|
2011-04-27 10:05:43 +00:00
|
|
|
entire_shaper_item.item.pos = si.position;
|
|
|
|
entire_shaper_item.item.length = length(item);
|
|
|
|
entire_shaper_item.item.bidiLevel = si.analysis.bidiLevel;
|
|
|
|
|
2012-05-08 11:24:17 +00:00
|
|
|
QVarLengthArray<HB_UChar16, 256> casedString;
|
2011-04-27 10:05:43 +00:00
|
|
|
if (hasCaseChange(si)) {
|
2012-05-30 09:47:18 +00:00
|
|
|
if (casedString.size() < static_cast<int>(entire_shaper_item.item.length))
|
2012-05-08 11:24:17 +00:00
|
|
|
casedString.resize(entire_shaper_item.item.length);
|
|
|
|
HB_UChar16 *uc = casedString.data();
|
2011-04-27 10:05:43 +00:00
|
|
|
for (uint i = 0; i < entire_shaper_item.item.length; ++i) {
|
2012-10-22 16:27:31 +00:00
|
|
|
uint ucs4 = entire_shaper_item.string[si.position + i];
|
|
|
|
if (QChar::isHighSurrogate(ucs4)) {
|
|
|
|
uc[i] = ucs4; // high part never changes in simple casing
|
|
|
|
if (i + 1 < entire_shaper_item.item.length) {
|
|
|
|
ushort low = entire_shaper_item.string[si.position + i + 1];
|
|
|
|
if (QChar::isLowSurrogate(low)) {
|
|
|
|
ucs4 = QChar::surrogateToUcs4(ucs4, low);
|
|
|
|
ucs4 = si.analysis.flags == QScriptAnalysis::Lowercase ? QChar::toLower(ucs4)
|
|
|
|
: QChar::toUpper(ucs4);
|
|
|
|
uc[++i] = QChar::lowSurrogate(ucs4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uc[i] = si.analysis.flags == QScriptAnalysis::Lowercase ? QChar::toLower(ucs4)
|
|
|
|
: QChar::toUpper(ucs4);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
entire_shaper_item.item.pos = 0;
|
|
|
|
entire_shaper_item.string = uc;
|
|
|
|
entire_shaper_item.stringLength = entire_shaper_item.item.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
entire_shaper_item.shaperFlags = 0;
|
|
|
|
if (!kerningEnabled)
|
|
|
|
entire_shaper_item.shaperFlags |= HB_ShaperFlag_NoKerning;
|
|
|
|
if (option.useDesignMetrics())
|
|
|
|
entire_shaper_item.shaperFlags |= HB_ShaperFlag_UseDesignMetrics;
|
|
|
|
|
|
|
|
entire_shaper_item.num_glyphs = qMax(layoutData->glyphLayout.numGlyphs - layoutData->used, int(entire_shaper_item.item.length));
|
2012-05-08 11:24:17 +00:00
|
|
|
if (!ensureSpace(entire_shaper_item.num_glyphs))
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
QGlyphLayout initialGlyphs = availableGlyphs(&si).mid(0, entire_shaper_item.num_glyphs);
|
|
|
|
|
|
|
|
if (!stringToGlyphs(&entire_shaper_item, &initialGlyphs, font)) {
|
2012-05-08 11:24:17 +00:00
|
|
|
if (!ensureSpace(entire_shaper_item.num_glyphs))
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
initialGlyphs = availableGlyphs(&si).mid(0, entire_shaper_item.num_glyphs);
|
|
|
|
if (!stringToGlyphs(&entire_shaper_item, &initialGlyphs, font)) {
|
|
|
|
// ############ if this happens there's a bug in the fontengine
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// split up the item into parts that come from different font engines.
|
|
|
|
QVarLengthArray<int> itemBoundaries(2);
|
|
|
|
// k * 2 entries, array[k] == index in string, array[k + 1] == index in glyphs
|
|
|
|
itemBoundaries[0] = entire_shaper_item.item.pos;
|
|
|
|
itemBoundaries[1] = 0;
|
|
|
|
|
|
|
|
if (font->type() == QFontEngine::Multi) {
|
|
|
|
uint lastEngine = 0;
|
|
|
|
int charIdx = entire_shaper_item.item.pos;
|
|
|
|
const int stringEnd = charIdx + entire_shaper_item.item.length;
|
|
|
|
for (quint32 i = 0; i < entire_shaper_item.num_glyphs; ++i, ++charIdx) {
|
|
|
|
uint engineIdx = initialGlyphs.glyphs[i] >> 24;
|
|
|
|
if (engineIdx != lastEngine && i > 0) {
|
|
|
|
itemBoundaries.append(charIdx);
|
|
|
|
itemBoundaries.append(i);
|
|
|
|
}
|
|
|
|
lastEngine = engineIdx;
|
2013-03-09 17:28:20 +00:00
|
|
|
if (QChar::isHighSurrogate(entire_shaper_item.string[charIdx])
|
2011-04-27 10:05:43 +00:00
|
|
|
&& charIdx < stringEnd - 1
|
2013-03-09 17:28:20 +00:00
|
|
|
&& QChar::isLowSurrogate(entire_shaper_item.string[charIdx + 1]))
|
2011-04-27 10:05:43 +00:00
|
|
|
++charIdx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int remaining_glyphs = entire_shaper_item.num_glyphs;
|
|
|
|
int glyph_pos = 0;
|
|
|
|
// for each item shape using harfbuzz and store the results in our layoutData's glyphs array.
|
|
|
|
for (int k = 0; k < itemBoundaries.size(); k += 2) { // for the +2, see the comment at the definition of itemBoundaries
|
|
|
|
|
|
|
|
HB_ShaperItem shaper_item = entire_shaper_item;
|
|
|
|
|
|
|
|
shaper_item.item.pos = itemBoundaries[k];
|
|
|
|
if (k < itemBoundaries.size() - 3) {
|
|
|
|
shaper_item.item.length = itemBoundaries[k + 2] - shaper_item.item.pos;
|
|
|
|
shaper_item.num_glyphs = itemBoundaries[k + 3] - itemBoundaries[k + 1];
|
|
|
|
} else { // last combo in the list, avoid out of bounds access.
|
|
|
|
shaper_item.item.length -= shaper_item.item.pos - entire_shaper_item.item.pos;
|
|
|
|
shaper_item.num_glyphs -= itemBoundaries[k + 1];
|
|
|
|
}
|
|
|
|
shaper_item.initialGlyphCount = shaper_item.num_glyphs;
|
|
|
|
if (shaper_item.num_glyphs < shaper_item.item.length)
|
|
|
|
shaper_item.num_glyphs = shaper_item.item.length;
|
|
|
|
|
|
|
|
QFontEngine *actualFontEngine = font;
|
|
|
|
uint engineIdx = 0;
|
|
|
|
if (font->type() == QFontEngine::Multi) {
|
|
|
|
engineIdx = uint(availableGlyphs(&si).glyphs[glyph_pos] >> 24);
|
|
|
|
|
|
|
|
actualFontEngine = static_cast<QFontEngineMulti *>(font)->engine(engineIdx);
|
|
|
|
}
|
|
|
|
|
2011-04-27 14:05:09 +00:00
|
|
|
si.ascent = qMax(actualFontEngine->ascent(), si.ascent);
|
|
|
|
si.descent = qMax(actualFontEngine->descent(), si.descent);
|
|
|
|
si.leading = qMax(actualFontEngine->leading(), si.leading);
|
|
|
|
|
2013-03-09 19:39:21 +00:00
|
|
|
shaper_item.font = (HB_Font)actualFontEngine->harfbuzzFont();
|
|
|
|
shaper_item.face = (HB_Face)actualFontEngine->harfbuzzFace();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
shaper_item.glyphIndicesPresent = true;
|
|
|
|
|
|
|
|
remaining_glyphs -= shaper_item.initialGlyphCount;
|
|
|
|
|
|
|
|
do {
|
2012-05-08 11:24:17 +00:00
|
|
|
if (!ensureSpace(glyph_pos + shaper_item.num_glyphs + remaining_glyphs))
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
const QGlyphLayout g = availableGlyphs(&si).mid(glyph_pos);
|
|
|
|
if (shaper_item.num_glyphs > shaper_item.item.length)
|
|
|
|
moveGlyphData(g.mid(shaper_item.num_glyphs), g.mid(shaper_item.initialGlyphCount), remaining_glyphs);
|
|
|
|
|
2013-03-09 17:28:20 +00:00
|
|
|
shaper_item.glyphs = reinterpret_cast<HB_Glyph *>(g.glyphs);
|
|
|
|
shaper_item.attributes = reinterpret_cast<HB_GlyphAttributes *>(g.attributes);
|
2011-04-27 10:05:43 +00:00
|
|
|
shaper_item.advances = reinterpret_cast<HB_Fixed *>(g.advances_x);
|
|
|
|
shaper_item.offsets = reinterpret_cast<HB_FixedPoint *>(g.offsets);
|
|
|
|
|
2012-07-02 15:15:56 +00:00
|
|
|
if (engineIdx != 0 && shaper_item.glyphIndicesPresent) {
|
2013-03-09 17:28:20 +00:00
|
|
|
for (quint32 i = 0; i < shaper_item.initialGlyphCount; ++i)
|
2011-04-27 10:05:43 +00:00
|
|
|
shaper_item.glyphs[i] &= 0x00ffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
shaper_item.log_clusters = logClusters(&si) + shaper_item.item.pos - entire_shaper_item.item.pos;
|
|
|
|
|
|
|
|
// qDebug(" .. num_glyphs=%d, used=%d, item.num_glyphs=%d", num_glyphs, used, shaper_item.num_glyphs);
|
|
|
|
} while (!qShapeItem(&shaper_item)); // this does the actual shaping via harfbuzz.
|
|
|
|
|
|
|
|
QGlyphLayout g = availableGlyphs(&si).mid(glyph_pos, shaper_item.num_glyphs);
|
|
|
|
moveGlyphData(g.mid(shaper_item.num_glyphs), g.mid(shaper_item.initialGlyphCount), remaining_glyphs);
|
|
|
|
|
2013-03-09 17:28:20 +00:00
|
|
|
for (quint32 i = 0; i < shaper_item.item.length; ++i)
|
2011-04-27 10:05:43 +00:00
|
|
|
shaper_item.log_clusters[i] += glyph_pos;
|
|
|
|
|
|
|
|
if (kerningEnabled && !shaper_item.kerning_applied)
|
2012-07-09 13:50:32 +00:00
|
|
|
actualFontEngine->doKerning(&g, option.useDesignMetrics() ? QFontEngine::DesignMetrics : QFontEngine::ShaperFlags(0));
|
2012-07-02 15:15:56 +00:00
|
|
|
|
|
|
|
if (engineIdx != 0) {
|
2013-03-09 17:28:20 +00:00
|
|
|
for (quint32 i = 0; i < shaper_item.num_glyphs; ++i)
|
2012-07-02 15:15:56 +00:00
|
|
|
g.glyphs[i] |= (engineIdx << 24);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
glyph_pos += shaper_item.num_glyphs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// qDebug(" -> item: script=%d num_glyphs=%d", shaper_item.script, shaper_item.num_glyphs);
|
|
|
|
si.num_glyphs = glyph_pos;
|
|
|
|
|
|
|
|
layoutData->used += si.num_glyphs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init(QTextEngine *e)
|
|
|
|
{
|
|
|
|
e->ignoreBidi = false;
|
|
|
|
e->cacheGlyphs = false;
|
|
|
|
e->forceJustification = false;
|
2010-12-15 14:11:45 +00:00
|
|
|
e->visualMovement = false;
|
2012-03-01 16:14:38 +00:00
|
|
|
e->delayDecorations = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
e->layoutData = 0;
|
|
|
|
|
|
|
|
e->minWidth = 0;
|
|
|
|
e->maxWidth = 0;
|
|
|
|
|
|
|
|
e->underlinePositions = 0;
|
|
|
|
e->specialData = 0;
|
|
|
|
e->stackEngine = false;
|
2011-12-16 18:26:40 +00:00
|
|
|
#ifndef QT_NO_RAWFONT
|
|
|
|
e->useRawFont = false;
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QTextEngine::QTextEngine()
|
|
|
|
{
|
|
|
|
init(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextEngine::QTextEngine(const QString &str, const QFont &f)
|
|
|
|
: text(str),
|
|
|
|
fnt(f)
|
|
|
|
{
|
|
|
|
init(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextEngine::~QTextEngine()
|
|
|
|
{
|
|
|
|
if (!stackEngine)
|
|
|
|
delete layoutData;
|
|
|
|
delete specialData;
|
2011-09-09 12:31:39 +00:00
|
|
|
resetFontEngineCache();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2012-08-26 01:09:09 +00:00
|
|
|
const QCharAttributes *QTextEngine::attributes() const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
if (layoutData && layoutData->haveCharAttributes)
|
2012-08-26 01:09:09 +00:00
|
|
|
return (QCharAttributes *) layoutData->memory;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
itemize();
|
|
|
|
if (! ensureSpace(layoutData->string.length()))
|
|
|
|
return NULL;
|
|
|
|
|
2012-08-26 01:09:09 +00:00
|
|
|
QVarLengthArray<QUnicodeTools::ScriptItem> scriptItems(layoutData->items.size());
|
2011-04-27 10:05:43 +00:00
|
|
|
for (int i = 0; i < layoutData->items.size(); ++i) {
|
|
|
|
const QScriptItem &si = layoutData->items[i];
|
2012-08-26 01:09:09 +00:00
|
|
|
scriptItems[i].position = si.position;
|
|
|
|
scriptItems[i].script = si.analysis.script;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2013-03-09 17:28:20 +00:00
|
|
|
QUnicodeTools::initCharAttributes(reinterpret_cast<const ushort *>(layoutData->string.constData()),
|
2012-05-29 02:15:53 +00:00
|
|
|
layoutData->string.length(),
|
2012-08-26 01:09:09 +00:00
|
|
|
scriptItems.data(), scriptItems.size(),
|
|
|
|
(QCharAttributes *)layoutData->memory);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
layoutData->haveCharAttributes = true;
|
2012-08-26 01:09:09 +00:00
|
|
|
return (QCharAttributes *) layoutData->memory;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::shape(int item) const
|
|
|
|
{
|
|
|
|
if (layoutData->items[item].analysis.flags == QScriptAnalysis::Object) {
|
|
|
|
ensureSpace(1);
|
|
|
|
if (block.docHandle()) {
|
|
|
|
QTextFormat format = formats()->format(formatIndex(&layoutData->items[item]));
|
|
|
|
docLayout()->resizeInlineObject(QTextInlineObject(item, const_cast<QTextEngine *>(this)),
|
|
|
|
layoutData->items[item].position + block.position(), format);
|
|
|
|
}
|
|
|
|
} else if (layoutData->items[item].analysis.flags == QScriptAnalysis::Tab) {
|
|
|
|
// set up at least the ascent/descent/leading of the script item for the tab
|
|
|
|
fontEngine(layoutData->items[item],
|
|
|
|
&layoutData->items[item].ascent,
|
|
|
|
&layoutData->items[item].descent,
|
|
|
|
&layoutData->items[item].leading);
|
|
|
|
} else {
|
|
|
|
shapeText(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void releaseCachedFontEngine(QFontEngine *fontEngine)
|
|
|
|
{
|
|
|
|
if (fontEngine) {
|
|
|
|
fontEngine->ref.deref();
|
2011-09-29 09:50:08 +00:00
|
|
|
if (fontEngine->cache_count == 0 && fontEngine->ref.load() == 0)
|
2011-04-27 10:05:43 +00:00
|
|
|
delete fontEngine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-09 12:31:39 +00:00
|
|
|
void QTextEngine::resetFontEngineCache()
|
|
|
|
{
|
|
|
|
releaseCachedFontEngine(feCache.prevFontEngine);
|
|
|
|
releaseCachedFontEngine(feCache.prevScaledFontEngine);
|
|
|
|
feCache.reset();
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void QTextEngine::invalidate()
|
|
|
|
{
|
|
|
|
freeMemory();
|
|
|
|
minWidth = 0;
|
|
|
|
maxWidth = 0;
|
|
|
|
if (specialData)
|
|
|
|
specialData->resolvedFormatIndices.clear();
|
|
|
|
|
2011-09-09 12:31:39 +00:00
|
|
|
resetFontEngineCache();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::clearLineData()
|
|
|
|
{
|
|
|
|
lines.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::validate() const
|
|
|
|
{
|
|
|
|
if (layoutData)
|
|
|
|
return;
|
|
|
|
layoutData = new LayoutData();
|
|
|
|
if (block.docHandle()) {
|
|
|
|
layoutData->string = block.text();
|
|
|
|
if (option.flags() & QTextOption::ShowLineAndParagraphSeparators)
|
|
|
|
layoutData->string += QLatin1Char(block.next().isValid() ? 0xb6 : 0x20);
|
|
|
|
} else {
|
|
|
|
layoutData->string = text;
|
|
|
|
}
|
|
|
|
if (specialData && specialData->preeditPosition != -1)
|
|
|
|
layoutData->string.insert(specialData->preeditPosition, specialData->preeditText);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::itemize() const
|
|
|
|
{
|
|
|
|
validate();
|
|
|
|
if (layoutData->items.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int length = layoutData->string.length();
|
|
|
|
if (!length)
|
|
|
|
return;
|
2011-09-29 12:29:05 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
bool ignore = ignoreBidi;
|
|
|
|
|
|
|
|
bool rtl = isRightToLeft();
|
|
|
|
|
|
|
|
if (!ignore && !rtl) {
|
|
|
|
ignore = true;
|
|
|
|
const QChar *start = layoutData->string.unicode();
|
|
|
|
const QChar * const end = start + length;
|
|
|
|
while (start < end) {
|
|
|
|
if (start->unicode() >= 0x590) {
|
|
|
|
ignore = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVarLengthArray<QScriptAnalysis, 4096> scriptAnalysis(length);
|
|
|
|
QScriptAnalysis *analysis = scriptAnalysis.data();
|
|
|
|
|
|
|
|
QBidiControl control(rtl);
|
|
|
|
|
|
|
|
if (ignore) {
|
|
|
|
memset(analysis, 0, length*sizeof(QScriptAnalysis));
|
|
|
|
if (option.textDirection() == Qt::RightToLeft) {
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
analysis[i].bidiLevel = 1;
|
|
|
|
layoutData->hasBidi = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
layoutData->hasBidi = bidiItemize(const_cast<QTextEngine *>(this), analysis, control);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ushort *uc = reinterpret_cast<const ushort *>(layoutData->string.unicode());
|
|
|
|
const ushort *e = uc + length;
|
2012-12-21 16:35:58 +00:00
|
|
|
uchar lastScript = QChar::Script_Common;
|
2011-04-27 10:05:43 +00:00
|
|
|
while (uc < e) {
|
2011-07-13 00:41:45 +00:00
|
|
|
switch (*uc) {
|
|
|
|
case QChar::ObjectReplacementCharacter:
|
2012-12-21 16:35:58 +00:00
|
|
|
analysis->script = QChar::Script_Common;
|
2011-04-27 10:05:43 +00:00
|
|
|
analysis->flags = QScriptAnalysis::Object;
|
2011-07-13 00:41:45 +00:00
|
|
|
break;
|
|
|
|
case QChar::LineSeparator:
|
2011-04-27 10:05:43 +00:00
|
|
|
if (analysis->bidiLevel % 2)
|
|
|
|
--analysis->bidiLevel;
|
2012-12-21 16:35:58 +00:00
|
|
|
analysis->script = QChar::Script_Common;
|
2011-04-27 10:05:43 +00:00
|
|
|
analysis->flags = QScriptAnalysis::LineOrParagraphSeparator;
|
|
|
|
if (option.flags() & QTextOption::ShowLineAndParagraphSeparators)
|
|
|
|
*const_cast<ushort*>(uc) = 0x21B5; // visual line separator
|
2011-07-13 00:41:45 +00:00
|
|
|
break;
|
2012-05-14 15:12:48 +00:00
|
|
|
case QChar::Tabulation:
|
2012-12-21 16:35:58 +00:00
|
|
|
analysis->script = QChar::Script_Common;
|
2011-04-27 10:05:43 +00:00
|
|
|
analysis->flags = QScriptAnalysis::Tab;
|
|
|
|
analysis->bidiLevel = control.baseLevel();
|
2011-07-13 00:41:45 +00:00
|
|
|
break;
|
2012-05-14 15:12:48 +00:00
|
|
|
case QChar::Space:
|
2011-07-13 00:41:45 +00:00
|
|
|
case QChar::Nbsp:
|
|
|
|
if (option.flags() & QTextOption::ShowTabsAndSpaces) {
|
2012-12-21 16:35:58 +00:00
|
|
|
analysis->script = QChar::Script_Common;
|
2011-07-13 00:41:45 +00:00
|
|
|
analysis->flags = QScriptAnalysis::Space;
|
|
|
|
analysis->bidiLevel = control.baseLevel();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through
|
|
|
|
default:
|
2012-12-21 16:35:58 +00:00
|
|
|
analysis->script = QChar::script(*uc);
|
|
|
|
if (analysis->script == QChar::Script_Inherited)
|
|
|
|
analysis->script = lastScript;
|
2011-07-13 00:41:45 +00:00
|
|
|
analysis->flags = QScriptAnalysis::None;
|
|
|
|
break;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
lastScript = analysis->script;
|
2012-12-21 16:35:58 +00:00
|
|
|
analysis->script = hbscript_to_script(script_to_hbscript(analysis->script)); // retain the old behavior
|
2011-04-27 10:05:43 +00:00
|
|
|
++uc;
|
|
|
|
++analysis;
|
|
|
|
}
|
|
|
|
if (option.flags() & QTextOption::ShowLineAndParagraphSeparators) {
|
|
|
|
(analysis-1)->flags = QScriptAnalysis::LineOrParagraphSeparator; // to exclude it from width
|
|
|
|
}
|
|
|
|
|
|
|
|
Itemizer itemizer(layoutData->string, scriptAnalysis.data(), layoutData->items);
|
|
|
|
|
|
|
|
const QTextDocumentPrivate *p = block.docHandle();
|
|
|
|
if (p) {
|
|
|
|
SpecialData *s = specialData;
|
|
|
|
|
|
|
|
QTextDocumentPrivate::FragmentIterator it = p->find(block.position());
|
|
|
|
QTextDocumentPrivate::FragmentIterator end = p->find(block.position() + block.length() - 1); // -1 to omit the block separator char
|
|
|
|
int format = it.value()->format;
|
|
|
|
|
|
|
|
int prevPosition = 0;
|
|
|
|
int position = prevPosition;
|
|
|
|
while (1) {
|
|
|
|
const QTextFragmentData * const frag = it.value();
|
|
|
|
if (it == end || format != frag->format) {
|
|
|
|
if (s && position >= s->preeditPosition) {
|
|
|
|
position += s->preeditText.length();
|
|
|
|
s = 0;
|
|
|
|
}
|
|
|
|
Q_ASSERT(position <= length);
|
2012-10-10 16:57:05 +00:00
|
|
|
QFont::Capitalization capitalization =
|
|
|
|
formats()->charFormat(format).hasProperty(QTextFormat::FontCapitalization)
|
|
|
|
? formats()->charFormat(format).fontCapitalization()
|
|
|
|
: formats()->defaultFont().capitalization();
|
|
|
|
itemizer.generate(prevPosition, position - prevPosition, capitalization);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (it == end) {
|
2012-10-10 16:57:05 +00:00
|
|
|
if (position < length)
|
|
|
|
itemizer.generate(position, length - position, capitalization);
|
2011-04-27 10:05:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
format = frag->format;
|
|
|
|
prevPosition = position;
|
|
|
|
}
|
|
|
|
position += frag->size_array[0];
|
|
|
|
++it;
|
|
|
|
}
|
2012-02-10 19:42:13 +00:00
|
|
|
} else {
|
2012-10-10 16:57:05 +00:00
|
|
|
#ifndef QT_NO_RAWFONT
|
|
|
|
if (useRawFont && specialData) {
|
|
|
|
int lastIndex = 0;
|
|
|
|
for (int i = 0; i < specialData->addFormats.size(); ++i) {
|
|
|
|
const QTextLayout::FormatRange &range = specialData->addFormats.at(i);
|
|
|
|
if (range.format.fontCapitalization()) {
|
|
|
|
itemizer.generate(lastIndex, range.start - lastIndex, QFont::MixedCase);
|
|
|
|
itemizer.generate(range.start, range.length, range.format.fontCapitalization());
|
|
|
|
lastIndex = range.start + range.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
itemizer.generate(lastIndex, length - lastIndex, QFont::MixedCase);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
itemizer.generate(0, length, static_cast<QFont::Capitalization> (fnt.d->capital));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addRequiredBoundaries();
|
|
|
|
resolveAdditionalFormats();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QTextEngine::isRightToLeft() const
|
|
|
|
{
|
|
|
|
switch (option.textDirection()) {
|
|
|
|
case Qt::LeftToRight:
|
|
|
|
return false;
|
|
|
|
case Qt::RightToLeft:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-04-27 06:47:46 +00:00
|
|
|
if (!layoutData)
|
|
|
|
itemize();
|
2011-04-27 10:05:43 +00:00
|
|
|
// this places the cursor in the right position depending on the keyboard layout
|
|
|
|
if (layoutData->string.isEmpty())
|
2012-01-24 15:44:38 +00:00
|
|
|
return qApp ? qApp->inputMethod()->inputDirection() == Qt::RightToLeft : false;
|
2011-04-27 10:05:43 +00:00
|
|
|
return layoutData->string.isRightToLeft();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int QTextEngine::findItem(int strPos) const
|
|
|
|
{
|
|
|
|
itemize();
|
2011-07-28 12:51:54 +00:00
|
|
|
int left = 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
int right = layoutData->items.size()-1;
|
|
|
|
while(left <= right) {
|
|
|
|
int middle = ((right-left)/2)+left;
|
|
|
|
if (strPos > layoutData->items[middle].position)
|
|
|
|
left = middle+1;
|
|
|
|
else if(strPos < layoutData->items[middle].position)
|
|
|
|
right = middle-1;
|
|
|
|
else {
|
|
|
|
return middle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFixed QTextEngine::width(int from, int len) const
|
|
|
|
{
|
|
|
|
itemize();
|
|
|
|
|
|
|
|
QFixed w = 0;
|
|
|
|
|
|
|
|
// qDebug("QTextEngine::width(from = %d, len = %d), numItems=%d, strleng=%d", from, len, items.size(), string.length());
|
|
|
|
for (int i = 0; i < layoutData->items.size(); i++) {
|
|
|
|
const QScriptItem *si = layoutData->items.constData() + i;
|
|
|
|
int pos = si->position;
|
|
|
|
int ilen = length(i);
|
|
|
|
// qDebug("item %d: from %d len %d", i, pos, ilen);
|
|
|
|
if (pos >= from + len)
|
|
|
|
break;
|
|
|
|
if (pos + ilen > from) {
|
|
|
|
if (!si->num_glyphs)
|
|
|
|
shape(i);
|
|
|
|
|
|
|
|
if (si->analysis.flags == QScriptAnalysis::Object) {
|
|
|
|
w += si->width;
|
|
|
|
continue;
|
|
|
|
} else if (si->analysis.flags == QScriptAnalysis::Tab) {
|
|
|
|
w += calculateTabWidth(i, w);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QGlyphLayout glyphs = shapedGlyphs(si);
|
|
|
|
unsigned short *logClusters = this->logClusters(si);
|
|
|
|
|
|
|
|
// fprintf(stderr, " logclusters:");
|
|
|
|
// for (int k = 0; k < ilen; k++)
|
|
|
|
// fprintf(stderr, " %d", logClusters[k]);
|
|
|
|
// fprintf(stderr, "\n");
|
|
|
|
// do the simple thing for now and give the first glyph in a cluster the full width, all other ones 0.
|
|
|
|
int charFrom = from - pos;
|
|
|
|
if (charFrom < 0)
|
|
|
|
charFrom = 0;
|
|
|
|
int glyphStart = logClusters[charFrom];
|
|
|
|
if (charFrom > 0 && logClusters[charFrom-1] == glyphStart)
|
|
|
|
while (charFrom < ilen && logClusters[charFrom] == glyphStart)
|
|
|
|
charFrom++;
|
|
|
|
if (charFrom < ilen) {
|
|
|
|
glyphStart = logClusters[charFrom];
|
|
|
|
int charEnd = from + len - 1 - pos;
|
|
|
|
if (charEnd >= ilen)
|
|
|
|
charEnd = ilen-1;
|
|
|
|
int glyphEnd = logClusters[charEnd];
|
|
|
|
while (charEnd < ilen && logClusters[charEnd] == glyphEnd)
|
|
|
|
charEnd++;
|
|
|
|
glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd];
|
|
|
|
|
|
|
|
// qDebug("char: start=%d end=%d / glyph: start = %d, end = %d", charFrom, charEnd, glyphStart, glyphEnd);
|
|
|
|
for (int i = glyphStart; i < glyphEnd; i++)
|
|
|
|
w += glyphs.advances_x[i] * !glyphs.attributes[i].dontPrint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// qDebug(" --> w= %d ", w);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
glyph_metrics_t QTextEngine::boundingBox(int from, int len) const
|
|
|
|
{
|
|
|
|
itemize();
|
|
|
|
|
|
|
|
glyph_metrics_t gm;
|
|
|
|
|
|
|
|
for (int i = 0; i < layoutData->items.size(); i++) {
|
|
|
|
const QScriptItem *si = layoutData->items.constData() + i;
|
|
|
|
|
|
|
|
int pos = si->position;
|
|
|
|
int ilen = length(i);
|
|
|
|
if (pos > from + len)
|
|
|
|
break;
|
|
|
|
if (pos + ilen > from) {
|
|
|
|
if (!si->num_glyphs)
|
|
|
|
shape(i);
|
|
|
|
|
|
|
|
if (si->analysis.flags == QScriptAnalysis::Object) {
|
|
|
|
gm.width += si->width;
|
|
|
|
continue;
|
|
|
|
} else if (si->analysis.flags == QScriptAnalysis::Tab) {
|
|
|
|
gm.width += calculateTabWidth(i, gm.width);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short *logClusters = this->logClusters(si);
|
|
|
|
QGlyphLayout glyphs = shapedGlyphs(si);
|
|
|
|
|
|
|
|
// do the simple thing for now and give the first glyph in a cluster the full width, all other ones 0.
|
|
|
|
int charFrom = from - pos;
|
|
|
|
if (charFrom < 0)
|
|
|
|
charFrom = 0;
|
|
|
|
int glyphStart = logClusters[charFrom];
|
|
|
|
if (charFrom > 0 && logClusters[charFrom-1] == glyphStart)
|
|
|
|
while (charFrom < ilen && logClusters[charFrom] == glyphStart)
|
|
|
|
charFrom++;
|
|
|
|
if (charFrom < ilen) {
|
|
|
|
QFontEngine *fe = fontEngine(*si);
|
|
|
|
glyphStart = logClusters[charFrom];
|
|
|
|
int charEnd = from + len - 1 - pos;
|
|
|
|
if (charEnd >= ilen)
|
|
|
|
charEnd = ilen-1;
|
|
|
|
int glyphEnd = logClusters[charEnd];
|
|
|
|
while (charEnd < ilen && logClusters[charEnd] == glyphEnd)
|
|
|
|
charEnd++;
|
|
|
|
glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd];
|
|
|
|
if (glyphStart <= glyphEnd ) {
|
|
|
|
glyph_metrics_t m = fe->boundingBox(glyphs.mid(glyphStart, glyphEnd - glyphStart));
|
|
|
|
gm.x = qMin(gm.x, m.x + gm.xoff);
|
|
|
|
gm.y = qMin(gm.y, m.y + gm.yoff);
|
|
|
|
gm.width = qMax(gm.width, m.width+gm.xoff);
|
|
|
|
gm.height = qMax(gm.height, m.height+gm.yoff);
|
|
|
|
gm.xoff += m.xoff;
|
|
|
|
gm.yoff += m.yoff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gm;
|
|
|
|
}
|
|
|
|
|
|
|
|
glyph_metrics_t QTextEngine::tightBoundingBox(int from, int len) const
|
|
|
|
{
|
|
|
|
itemize();
|
|
|
|
|
|
|
|
glyph_metrics_t gm;
|
|
|
|
|
|
|
|
for (int i = 0; i < layoutData->items.size(); i++) {
|
|
|
|
const QScriptItem *si = layoutData->items.constData() + i;
|
|
|
|
int pos = si->position;
|
|
|
|
int ilen = length(i);
|
|
|
|
if (pos > from + len)
|
|
|
|
break;
|
|
|
|
if (pos + len > from) {
|
|
|
|
if (!si->num_glyphs)
|
|
|
|
shape(i);
|
|
|
|
unsigned short *logClusters = this->logClusters(si);
|
|
|
|
QGlyphLayout glyphs = shapedGlyphs(si);
|
|
|
|
|
|
|
|
// do the simple thing for now and give the first glyph in a cluster the full width, all other ones 0.
|
|
|
|
int charFrom = from - pos;
|
|
|
|
if (charFrom < 0)
|
|
|
|
charFrom = 0;
|
|
|
|
int glyphStart = logClusters[charFrom];
|
|
|
|
if (charFrom > 0 && logClusters[charFrom-1] == glyphStart)
|
|
|
|
while (charFrom < ilen && logClusters[charFrom] == glyphStart)
|
|
|
|
charFrom++;
|
|
|
|
if (charFrom < ilen) {
|
|
|
|
glyphStart = logClusters[charFrom];
|
|
|
|
int charEnd = from + len - 1 - pos;
|
|
|
|
if (charEnd >= ilen)
|
|
|
|
charEnd = ilen-1;
|
|
|
|
int glyphEnd = logClusters[charEnd];
|
|
|
|
while (charEnd < ilen && logClusters[charEnd] == glyphEnd)
|
|
|
|
charEnd++;
|
|
|
|
glyphEnd = (charEnd == ilen) ? si->num_glyphs : logClusters[charEnd];
|
|
|
|
if (glyphStart <= glyphEnd ) {
|
|
|
|
QFontEngine *fe = fontEngine(*si);
|
|
|
|
glyph_metrics_t m = fe->tightBoundingBox(glyphs.mid(glyphStart, glyphEnd - glyphStart));
|
|
|
|
gm.x = qMin(gm.x, m.x + gm.xoff);
|
|
|
|
gm.y = qMin(gm.y, m.y + gm.yoff);
|
|
|
|
gm.width = qMax(gm.width, m.width+gm.xoff);
|
|
|
|
gm.height = qMax(gm.height, m.height+gm.yoff);
|
|
|
|
gm.xoff += m.xoff;
|
|
|
|
gm.yoff += m.yoff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gm;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont QTextEngine::font(const QScriptItem &si) const
|
|
|
|
{
|
|
|
|
QFont font = fnt;
|
|
|
|
if (hasFormats()) {
|
|
|
|
QTextCharFormat f = format(&si);
|
|
|
|
font = f.font();
|
|
|
|
|
|
|
|
if (block.docHandle() && block.docHandle()->layout()) {
|
|
|
|
// Make sure we get the right dpi on printers
|
|
|
|
QPaintDevice *pdev = block.docHandle()->layout()->paintDevice();
|
|
|
|
if (pdev)
|
|
|
|
font = QFont(font, pdev);
|
|
|
|
} else {
|
|
|
|
font = font.resolve(fnt);
|
|
|
|
}
|
|
|
|
QTextCharFormat::VerticalAlignment valign = f.verticalAlignment();
|
|
|
|
if (valign == QTextCharFormat::AlignSuperScript || valign == QTextCharFormat::AlignSubScript) {
|
|
|
|
if (font.pointSize() != -1)
|
|
|
|
font.setPointSize((font.pointSize() * 2) / 3);
|
|
|
|
else
|
|
|
|
font.setPixelSize((font.pixelSize() * 2) / 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (si.analysis.flags == QScriptAnalysis::SmallCaps)
|
|
|
|
font = font.d->smallCapsFont();
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextEngine::FontEngineCache::FontEngineCache()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
//we cache the previous results of this function, as calling it numerous times with the same effective
|
|
|
|
//input is common (and hard to cache at a higher level)
|
|
|
|
QFontEngine *QTextEngine::fontEngine(const QScriptItem &si, QFixed *ascent, QFixed *descent, QFixed *leading) const
|
|
|
|
{
|
|
|
|
QFontEngine *engine = 0;
|
|
|
|
QFontEngine *scaledEngine = 0;
|
|
|
|
int script = si.analysis.script;
|
|
|
|
|
|
|
|
QFont font = fnt;
|
2011-12-16 18:26:40 +00:00
|
|
|
#ifndef QT_NO_RAWFONT
|
|
|
|
if (useRawFont && rawFont.isValid()) {
|
|
|
|
if (feCache.prevFontEngine && feCache.prevFontEngine->type() == QFontEngine::Multi && feCache.prevScript == script) {
|
2011-04-27 10:05:43 +00:00
|
|
|
engine = feCache.prevFontEngine;
|
|
|
|
} else {
|
2012-01-16 16:40:21 +00:00
|
|
|
engine = QFontEngineMultiQPA::createMultiFontEngine(rawFont.d->fontEngine, script);
|
2011-04-27 10:05:43 +00:00
|
|
|
feCache.prevFontEngine = engine;
|
|
|
|
feCache.prevScript = script;
|
2011-12-16 18:26:40 +00:00
|
|
|
engine->ref.ref();
|
|
|
|
if (feCache.prevScaledFontEngine)
|
|
|
|
releaseCachedFontEngine(feCache.prevScaledFontEngine);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2011-12-16 18:26:40 +00:00
|
|
|
if (si.analysis.flags & QFont::SmallCaps) {
|
|
|
|
if (feCache.prevScaledFontEngine) {
|
|
|
|
scaledEngine = feCache.prevScaledFontEngine;
|
|
|
|
} else {
|
|
|
|
QFontEngine *scEngine = rawFont.d->fontEngine->cloneWithSize(smallCapsFraction * rawFont.pixelSize());
|
2012-01-16 16:40:21 +00:00
|
|
|
scaledEngine = QFontEngineMultiQPA::createMultiFontEngine(scEngine, script);
|
2011-12-16 18:26:40 +00:00
|
|
|
scaledEngine->ref.ref();
|
|
|
|
feCache.prevScaledFontEngine = scaledEngine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (hasFormats()) {
|
|
|
|
if (feCache.prevFontEngine && feCache.prevPosition == si.position && feCache.prevLength == length(&si) && feCache.prevScript == script) {
|
|
|
|
engine = feCache.prevFontEngine;
|
|
|
|
scaledEngine = feCache.prevScaledFontEngine;
|
|
|
|
} else {
|
|
|
|
QTextCharFormat f = format(&si);
|
|
|
|
font = f.font();
|
|
|
|
|
|
|
|
if (block.docHandle() && block.docHandle()->layout()) {
|
|
|
|
// Make sure we get the right dpi on printers
|
|
|
|
QPaintDevice *pdev = block.docHandle()->layout()->paintDevice();
|
|
|
|
if (pdev)
|
|
|
|
font = QFont(font, pdev);
|
|
|
|
} else {
|
|
|
|
font = font.resolve(fnt);
|
|
|
|
}
|
|
|
|
engine = font.d->engineForScript(script);
|
|
|
|
QTextCharFormat::VerticalAlignment valign = f.verticalAlignment();
|
|
|
|
if (valign == QTextCharFormat::AlignSuperScript || valign == QTextCharFormat::AlignSubScript) {
|
|
|
|
if (font.pointSize() != -1)
|
|
|
|
font.setPointSize((font.pointSize() * 2) / 3);
|
|
|
|
else
|
|
|
|
font.setPixelSize((font.pixelSize() * 2) / 3);
|
|
|
|
scaledEngine = font.d->engineForScript(script);
|
|
|
|
}
|
2012-04-18 08:57:43 +00:00
|
|
|
|
2011-12-16 18:26:40 +00:00
|
|
|
if (engine)
|
|
|
|
engine->ref.ref();
|
2012-04-18 08:57:43 +00:00
|
|
|
if (feCache.prevFontEngine)
|
|
|
|
releaseCachedFontEngine(feCache.prevFontEngine);
|
|
|
|
feCache.prevFontEngine = engine;
|
|
|
|
|
2011-12-16 18:26:40 +00:00
|
|
|
if (scaledEngine)
|
|
|
|
scaledEngine->ref.ref();
|
2012-04-18 08:57:43 +00:00
|
|
|
if (feCache.prevScaledFontEngine)
|
|
|
|
releaseCachedFontEngine(feCache.prevScaledFontEngine);
|
|
|
|
feCache.prevScaledFontEngine = scaledEngine;
|
|
|
|
|
2011-12-16 18:26:40 +00:00
|
|
|
feCache.prevScript = script;
|
|
|
|
feCache.prevPosition = si.position;
|
|
|
|
feCache.prevLength = length(&si);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (feCache.prevFontEngine && feCache.prevScript == script && feCache.prevPosition == -1)
|
|
|
|
engine = feCache.prevFontEngine;
|
|
|
|
else {
|
|
|
|
engine = font.d->engineForScript(script);
|
2012-04-18 08:57:43 +00:00
|
|
|
|
2011-12-16 18:26:40 +00:00
|
|
|
if (engine)
|
|
|
|
engine->ref.ref();
|
2012-04-18 08:57:43 +00:00
|
|
|
if (feCache.prevFontEngine)
|
|
|
|
releaseCachedFontEngine(feCache.prevFontEngine);
|
|
|
|
feCache.prevFontEngine = engine;
|
|
|
|
|
2011-12-16 18:26:40 +00:00
|
|
|
feCache.prevScript = script;
|
|
|
|
feCache.prevPosition = -1;
|
|
|
|
feCache.prevLength = -1;
|
|
|
|
feCache.prevScaledFontEngine = 0;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2011-12-16 18:26:40 +00:00
|
|
|
if (si.analysis.flags == QScriptAnalysis::SmallCaps) {
|
|
|
|
QFontPrivate *p = font.d->smallCapsFontPrivate();
|
|
|
|
scaledEngine = p->engineForScript(script);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ascent) {
|
|
|
|
*ascent = engine->ascent();
|
|
|
|
*descent = engine->descent();
|
|
|
|
*leading = engine->leading();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scaledEngine)
|
|
|
|
return scaledEngine;
|
|
|
|
return engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct QJustificationPoint {
|
|
|
|
int type;
|
|
|
|
QFixed kashidaWidth;
|
|
|
|
QGlyphLayout glyph;
|
|
|
|
};
|
|
|
|
|
|
|
|
Q_DECLARE_TYPEINFO(QJustificationPoint, Q_PRIMITIVE_TYPE);
|
|
|
|
|
|
|
|
static void set(QJustificationPoint *point, int type, const QGlyphLayout &glyph, QFontEngine *fe)
|
|
|
|
{
|
|
|
|
point->type = type;
|
|
|
|
point->glyph = glyph;
|
|
|
|
|
|
|
|
if (type >= HB_Arabic_Normal) {
|
|
|
|
QChar ch(0x640); // Kashida character
|
|
|
|
QGlyphLayoutArray<8> glyphs;
|
|
|
|
int nglyphs = 7;
|
|
|
|
fe->stringToCMap(&ch, 1, &glyphs, &nglyphs, 0);
|
|
|
|
if (glyphs.glyphs[0] && glyphs.advances_x[0] != 0) {
|
|
|
|
point->kashidaWidth = glyphs.advances_x[0];
|
|
|
|
} else {
|
|
|
|
point->type = HB_NoJustification;
|
|
|
|
point->kashidaWidth = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QTextEngine::justify(const QScriptLine &line)
|
|
|
|
{
|
|
|
|
// qDebug("justify: line.gridfitted = %d, line.justified=%d", line.gridfitted, line.justified);
|
|
|
|
if (line.gridfitted && line.justified)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!line.gridfitted) {
|
|
|
|
// redo layout in device metrics, then adjust
|
|
|
|
const_cast<QScriptLine &>(line).gridfitted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((option.alignment() & Qt::AlignHorizontal_Mask) != Qt::AlignJustify)
|
|
|
|
return;
|
|
|
|
|
|
|
|
itemize();
|
|
|
|
|
|
|
|
if (!forceJustification) {
|
2012-12-11 13:07:16 +00:00
|
|
|
int end = line.from + (int)line.length + line.trailingSpaces;
|
2011-04-27 10:05:43 +00:00
|
|
|
if (end == layoutData->string.length())
|
|
|
|
return; // no justification at end of paragraph
|
|
|
|
if (end && layoutData->items[findItem(end-1)].analysis.flags == QScriptAnalysis::LineOrParagraphSeparator)
|
|
|
|
return; // no justification at the end of an explicitly separated line
|
|
|
|
}
|
|
|
|
|
|
|
|
// justify line
|
|
|
|
int maxJustify = 0;
|
|
|
|
|
|
|
|
// don't include trailing white spaces when doing justification
|
|
|
|
int line_length = line.length;
|
2012-08-26 01:09:09 +00:00
|
|
|
const QCharAttributes *a = attributes();
|
2011-04-27 10:05:43 +00:00
|
|
|
if (! a)
|
|
|
|
return;
|
|
|
|
a += line.from;
|
|
|
|
while (line_length && a[line_length-1].whiteSpace)
|
|
|
|
--line_length;
|
|
|
|
// subtract one char more, as we can't justfy after the last character
|
|
|
|
--line_length;
|
|
|
|
|
|
|
|
if (!line_length)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int firstItem = findItem(line.from);
|
|
|
|
int nItems = findItem(line.from + line_length - 1) - firstItem + 1;
|
|
|
|
|
|
|
|
QVarLengthArray<QJustificationPoint> justificationPoints;
|
|
|
|
int nPoints = 0;
|
|
|
|
// qDebug("justifying from %d len %d, firstItem=%d, nItems=%d (%s)", line.from, line_length, firstItem, nItems, layoutData->string.mid(line.from, line_length).toUtf8().constData());
|
|
|
|
QFixed minKashida = 0x100000;
|
|
|
|
|
|
|
|
// we need to do all shaping before we go into the next loop, as we there
|
|
|
|
// store pointers to the glyph data that could get reallocated by the shaping
|
|
|
|
// process.
|
|
|
|
for (int i = 0; i < nItems; ++i) {
|
|
|
|
QScriptItem &si = layoutData->items[firstItem + i];
|
|
|
|
if (!si.num_glyphs)
|
|
|
|
shape(firstItem + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < nItems; ++i) {
|
|
|
|
QScriptItem &si = layoutData->items[firstItem + i];
|
|
|
|
|
|
|
|
int kashida_type = HB_Arabic_Normal;
|
|
|
|
int kashida_pos = -1;
|
|
|
|
|
|
|
|
int start = qMax(line.from - si.position, 0);
|
|
|
|
int end = qMin(line.from + line_length - (int)si.position, length(firstItem+i));
|
|
|
|
|
|
|
|
unsigned short *log_clusters = logClusters(&si);
|
|
|
|
|
|
|
|
int gs = log_clusters[start];
|
|
|
|
int ge = (end == length(firstItem+i) ? si.num_glyphs : log_clusters[end]);
|
|
|
|
|
2012-12-11 13:07:16 +00:00
|
|
|
Q_ASSERT(ge <= si.num_glyphs);
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
const QGlyphLayout g = shapedGlyphs(&si);
|
|
|
|
|
|
|
|
for (int i = gs; i < ge; ++i) {
|
|
|
|
g.justifications[i].type = QGlyphJustification::JustifyNone;
|
|
|
|
g.justifications[i].nKashidas = 0;
|
|
|
|
g.justifications[i].space_18d6 = 0;
|
|
|
|
|
|
|
|
justificationPoints.resize(nPoints+3);
|
|
|
|
int justification = g.attributes[i].justification;
|
|
|
|
|
|
|
|
switch(justification) {
|
|
|
|
case HB_NoJustification:
|
|
|
|
break;
|
|
|
|
case HB_Space :
|
|
|
|
// fall through
|
|
|
|
case HB_Arabic_Space :
|
|
|
|
if (kashida_pos >= 0) {
|
|
|
|
// qDebug("kashida position at %d in word", kashida_pos);
|
|
|
|
set(&justificationPoints[nPoints], kashida_type, g.mid(kashida_pos), fontEngine(si));
|
|
|
|
if (justificationPoints[nPoints].kashidaWidth > 0) {
|
|
|
|
minKashida = qMin(minKashida, justificationPoints[nPoints].kashidaWidth);
|
|
|
|
maxJustify = qMax(maxJustify, justificationPoints[nPoints].type);
|
|
|
|
++nPoints;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
kashida_pos = -1;
|
|
|
|
kashida_type = HB_Arabic_Normal;
|
|
|
|
// fall through
|
|
|
|
case HB_Character :
|
|
|
|
set(&justificationPoints[nPoints++], justification, g.mid(i), fontEngine(si));
|
|
|
|
maxJustify = qMax(maxJustify, justification);
|
|
|
|
break;
|
|
|
|
case HB_Arabic_Normal :
|
|
|
|
case HB_Arabic_Waw :
|
|
|
|
case HB_Arabic_BaRa :
|
|
|
|
case HB_Arabic_Alef :
|
|
|
|
case HB_Arabic_HaaDal :
|
|
|
|
case HB_Arabic_Seen :
|
|
|
|
case HB_Arabic_Kashida :
|
|
|
|
if (justification >= kashida_type) {
|
|
|
|
kashida_pos = i;
|
|
|
|
kashida_type = justification;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (kashida_pos >= 0) {
|
|
|
|
set(&justificationPoints[nPoints], kashida_type, g.mid(kashida_pos), fontEngine(si));
|
|
|
|
if (justificationPoints[nPoints].kashidaWidth > 0) {
|
|
|
|
minKashida = qMin(minKashida, justificationPoints[nPoints].kashidaWidth);
|
|
|
|
maxJustify = qMax(maxJustify, justificationPoints[nPoints].type);
|
|
|
|
++nPoints;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-24 07:13:45 +00:00
|
|
|
QFixed leading = leadingSpaceWidth(line);
|
|
|
|
QFixed need = line.width - line.textWidth - leading;
|
2011-04-27 10:05:43 +00:00
|
|
|
if (need < 0) {
|
|
|
|
// line overflows already!
|
|
|
|
const_cast<QScriptLine &>(line).justified = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// qDebug("doing justification: textWidth=%x, requested=%x, maxJustify=%d", line.textWidth.value(), line.width.value(), maxJustify);
|
|
|
|
// qDebug(" minKashida=%f, need=%f", minKashida.toReal(), need.toReal());
|
|
|
|
|
|
|
|
// distribute in priority order
|
|
|
|
if (maxJustify >= HB_Arabic_Normal) {
|
|
|
|
while (need >= minKashida) {
|
|
|
|
for (int type = maxJustify; need >= minKashida && type >= HB_Arabic_Normal; --type) {
|
|
|
|
for (int i = 0; need >= minKashida && i < nPoints; ++i) {
|
|
|
|
if (justificationPoints[i].type == type && justificationPoints[i].kashidaWidth <= need) {
|
|
|
|
justificationPoints[i].glyph.justifications->nKashidas++;
|
|
|
|
// ############
|
|
|
|
justificationPoints[i].glyph.justifications->space_18d6 += justificationPoints[i].kashidaWidth.value();
|
|
|
|
need -= justificationPoints[i].kashidaWidth;
|
|
|
|
// qDebug("adding kashida type %d with width %x, neednow %x", type, justificationPoints[i].kashidaWidth, need.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Q_ASSERT(need >= 0);
|
|
|
|
if (!need)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
maxJustify = qMin(maxJustify, (int)HB_Space);
|
|
|
|
for (int type = maxJustify; need != 0 && type > 0; --type) {
|
|
|
|
int n = 0;
|
|
|
|
for (int i = 0; i < nPoints; ++i) {
|
|
|
|
if (justificationPoints[i].type == type)
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
// qDebug("number of points for justification type %d: %d", type, n);
|
|
|
|
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int i = 0; i < nPoints; ++i) {
|
|
|
|
if (justificationPoints[i].type == type) {
|
|
|
|
QFixed add = need/n;
|
|
|
|
// qDebug("adding %x to glyph %x", add.value(), justificationPoints[i].glyph->glyph);
|
|
|
|
justificationPoints[i].glyph.justifications[0].space_18d6 = add.value();
|
|
|
|
need -= add;
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_ASSERT(!need);
|
|
|
|
}
|
|
|
|
end:
|
|
|
|
const_cast<QScriptLine &>(line).justified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QScriptLine::setDefaultHeight(QTextEngine *eng)
|
|
|
|
{
|
|
|
|
QFont f;
|
|
|
|
QFontEngine *e;
|
|
|
|
|
|
|
|
if (eng->block.docHandle() && eng->block.docHandle()->layout()) {
|
|
|
|
f = eng->block.charFormat().font();
|
|
|
|
// Make sure we get the right dpi on printers
|
|
|
|
QPaintDevice *pdev = eng->block.docHandle()->layout()->paintDevice();
|
|
|
|
if (pdev)
|
|
|
|
f = QFont(f, pdev);
|
2012-12-21 16:35:58 +00:00
|
|
|
e = f.d->engineForScript(QChar::Script_Common);
|
2011-04-27 10:05:43 +00:00
|
|
|
} else {
|
2012-12-21 16:35:58 +00:00
|
|
|
e = eng->fnt.d->engineForScript(QChar::Script_Common);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QFixed other_ascent = e->ascent();
|
|
|
|
QFixed other_descent = e->descent();
|
|
|
|
QFixed other_leading = e->leading();
|
|
|
|
leading = qMax(leading + ascent, other_leading + other_ascent) - qMax(ascent, other_ascent);
|
|
|
|
ascent = qMax(ascent, other_ascent);
|
|
|
|
descent = qMax(descent, other_descent);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextEngine::LayoutData::LayoutData()
|
|
|
|
{
|
|
|
|
memory = 0;
|
|
|
|
allocated = 0;
|
|
|
|
memory_on_stack = false;
|
|
|
|
used = 0;
|
|
|
|
hasBidi = false;
|
|
|
|
layoutState = LayoutEmpty;
|
|
|
|
haveCharAttributes = false;
|
|
|
|
logClustersPtr = 0;
|
|
|
|
available_glyphs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextEngine::LayoutData::LayoutData(const QString &str, void **stack_memory, int _allocated)
|
|
|
|
: string(str)
|
|
|
|
{
|
|
|
|
allocated = _allocated;
|
|
|
|
|
2012-08-26 01:09:09 +00:00
|
|
|
int space_charAttributes = sizeof(QCharAttributes)*string.length()/sizeof(void*) + 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
int space_logClusters = sizeof(unsigned short)*string.length()/sizeof(void*) + 1;
|
|
|
|
available_glyphs = ((int)allocated - space_charAttributes - space_logClusters)*(int)sizeof(void*)/(int)QGlyphLayout::spaceNeededForGlyphLayout(1);
|
|
|
|
|
|
|
|
if (available_glyphs < str.length()) {
|
|
|
|
// need to allocate on the heap
|
|
|
|
allocated = 0;
|
|
|
|
|
|
|
|
memory_on_stack = false;
|
|
|
|
memory = 0;
|
|
|
|
logClustersPtr = 0;
|
|
|
|
} else {
|
|
|
|
memory_on_stack = true;
|
|
|
|
memory = stack_memory;
|
|
|
|
logClustersPtr = (unsigned short *)(memory + space_charAttributes);
|
|
|
|
|
|
|
|
void *m = memory + space_charAttributes + space_logClusters;
|
|
|
|
glyphLayout = QGlyphLayout(reinterpret_cast<char *>(m), str.length());
|
|
|
|
glyphLayout.clear();
|
|
|
|
memset(memory, 0, space_charAttributes*sizeof(void *));
|
|
|
|
}
|
|
|
|
used = 0;
|
|
|
|
hasBidi = false;
|
|
|
|
layoutState = LayoutEmpty;
|
|
|
|
haveCharAttributes = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextEngine::LayoutData::~LayoutData()
|
|
|
|
{
|
|
|
|
if (!memory_on_stack)
|
|
|
|
free(memory);
|
|
|
|
memory = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QTextEngine::LayoutData::reallocate(int totalGlyphs)
|
|
|
|
{
|
|
|
|
Q_ASSERT(totalGlyphs >= glyphLayout.numGlyphs);
|
|
|
|
if (memory_on_stack && available_glyphs >= totalGlyphs) {
|
|
|
|
glyphLayout.grow(glyphLayout.data(), totalGlyphs);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-26 01:09:09 +00:00
|
|
|
int space_charAttributes = sizeof(QCharAttributes)*string.length()/sizeof(void*) + 1;
|
2011-04-27 10:05:43 +00:00
|
|
|
int space_logClusters = sizeof(unsigned short)*string.length()/sizeof(void*) + 1;
|
|
|
|
int space_glyphs = QGlyphLayout::spaceNeededForGlyphLayout(totalGlyphs)/sizeof(void*) + 2;
|
|
|
|
|
|
|
|
int newAllocated = space_charAttributes + space_glyphs + space_logClusters;
|
|
|
|
// These values can be negative if the length of string/glyphs causes overflow,
|
|
|
|
// we can't layout such a long string all at once, so return false here to
|
|
|
|
// indicate there is a failure
|
|
|
|
if (space_charAttributes < 0 || space_logClusters < 0 || space_glyphs < 0 || newAllocated < allocated) {
|
|
|
|
layoutState = LayoutFailed;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void **newMem = memory;
|
|
|
|
newMem = (void **)::realloc(memory_on_stack ? 0 : memory, newAllocated*sizeof(void *));
|
|
|
|
if (!newMem) {
|
|
|
|
layoutState = LayoutFailed;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (memory_on_stack)
|
|
|
|
memcpy(newMem, memory, allocated*sizeof(void *));
|
|
|
|
memory = newMem;
|
|
|
|
memory_on_stack = false;
|
|
|
|
|
|
|
|
void **m = memory;
|
|
|
|
m += space_charAttributes;
|
|
|
|
logClustersPtr = (unsigned short *) m;
|
|
|
|
m += space_logClusters;
|
|
|
|
|
|
|
|
const int space_preGlyphLayout = space_charAttributes + space_logClusters;
|
|
|
|
if (allocated < space_preGlyphLayout)
|
|
|
|
memset(memory + allocated, 0, (space_preGlyphLayout - allocated)*sizeof(void *));
|
|
|
|
|
|
|
|
glyphLayout.grow(reinterpret_cast<char *>(m), totalGlyphs);
|
|
|
|
|
|
|
|
allocated = newAllocated;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// grow to the new size, copying the existing data to the new layout
|
|
|
|
void QGlyphLayout::grow(char *address, int totalGlyphs)
|
|
|
|
{
|
|
|
|
QGlyphLayout oldLayout(address, numGlyphs);
|
|
|
|
QGlyphLayout newLayout(address, totalGlyphs);
|
|
|
|
|
|
|
|
if (numGlyphs) {
|
|
|
|
// move the existing data
|
2013-03-09 17:28:20 +00:00
|
|
|
memmove(newLayout.attributes, oldLayout.attributes, numGlyphs * sizeof(QGlyphAttributes));
|
2011-04-27 10:05:43 +00:00
|
|
|
memmove(newLayout.justifications, oldLayout.justifications, numGlyphs * sizeof(QGlyphJustification));
|
|
|
|
memmove(newLayout.advances_y, oldLayout.advances_y, numGlyphs * sizeof(QFixed));
|
|
|
|
memmove(newLayout.advances_x, oldLayout.advances_x, numGlyphs * sizeof(QFixed));
|
2013-03-09 17:28:20 +00:00
|
|
|
memmove(newLayout.glyphs, oldLayout.glyphs, numGlyphs * sizeof(glyph_t));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear the new data
|
|
|
|
newLayout.clear(numGlyphs);
|
|
|
|
|
|
|
|
*this = newLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::freeMemory()
|
|
|
|
{
|
|
|
|
if (!stackEngine) {
|
|
|
|
delete layoutData;
|
|
|
|
layoutData = 0;
|
|
|
|
} else {
|
|
|
|
layoutData->used = 0;
|
|
|
|
layoutData->hasBidi = false;
|
|
|
|
layoutData->layoutState = LayoutEmpty;
|
|
|
|
layoutData->haveCharAttributes = false;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < lines.size(); ++i) {
|
|
|
|
lines[i].justified = 0;
|
|
|
|
lines[i].gridfitted = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTextEngine::formatIndex(const QScriptItem *si) const
|
|
|
|
{
|
|
|
|
if (specialData && !specialData->resolvedFormatIndices.isEmpty())
|
|
|
|
return specialData->resolvedFormatIndices.at(si - &layoutData->items[0]);
|
|
|
|
QTextDocumentPrivate *p = block.docHandle();
|
|
|
|
if (!p)
|
|
|
|
return -1;
|
|
|
|
int pos = si->position;
|
|
|
|
if (specialData && si->position >= specialData->preeditPosition) {
|
|
|
|
if (si->position < specialData->preeditPosition + specialData->preeditText.length())
|
2012-04-04 05:34:09 +00:00
|
|
|
pos = qMax(qMin(block.length(), specialData->preeditPosition) - 1, 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
else
|
|
|
|
pos -= specialData->preeditText.length();
|
|
|
|
}
|
|
|
|
QTextDocumentPrivate::FragmentIterator it = p->find(block.position() + pos);
|
|
|
|
return it.value()->format;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTextCharFormat QTextEngine::format(const QScriptItem *si) const
|
|
|
|
{
|
|
|
|
QTextCharFormat format;
|
2012-09-03 16:06:17 +00:00
|
|
|
const QTextFormatCollection *formats = this->formats();
|
|
|
|
|
|
|
|
if (formats)
|
2011-04-27 10:05:43 +00:00
|
|
|
format = formats->charFormat(formatIndex(si));
|
2012-09-03 16:06:17 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
if (specialData && specialData->resolvedFormatIndices.isEmpty()) {
|
|
|
|
int end = si->position + length(si);
|
|
|
|
for (int i = 0; i < specialData->addFormats.size(); ++i) {
|
|
|
|
const QTextLayout::FormatRange &r = specialData->addFormats.at(i);
|
|
|
|
if (r.start <= si->position && r.start + r.length >= end) {
|
|
|
|
if (!specialData->addFormatIndices.isEmpty())
|
|
|
|
format.merge(formats->format(specialData->addFormatIndices.at(i)));
|
|
|
|
else
|
|
|
|
format.merge(r.format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::addRequiredBoundaries() const
|
|
|
|
{
|
|
|
|
if (specialData) {
|
|
|
|
for (int i = 0; i < specialData->addFormats.size(); ++i) {
|
|
|
|
const QTextLayout::FormatRange &r = specialData->addFormats.at(i);
|
|
|
|
setBoundary(r.start);
|
|
|
|
setBoundary(r.start + r.length);
|
|
|
|
//qDebug("adding boundaries %d %d", r.start, r.start+r.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QTextEngine::atWordSeparator(int position) const
|
|
|
|
{
|
|
|
|
const QChar c = layoutData->string.at(position);
|
2012-05-21 11:21:19 +00:00
|
|
|
switch (c.unicode()) {
|
2011-04-27 10:05:43 +00:00
|
|
|
case '.':
|
|
|
|
case ',':
|
|
|
|
case '?':
|
|
|
|
case '!':
|
|
|
|
case '@':
|
|
|
|
case '#':
|
|
|
|
case '$':
|
|
|
|
case ':':
|
|
|
|
case ';':
|
|
|
|
case '-':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '[':
|
|
|
|
case ']':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '{':
|
|
|
|
case '}':
|
|
|
|
case '=':
|
|
|
|
case '/':
|
|
|
|
case '+':
|
|
|
|
case '%':
|
|
|
|
case '&':
|
|
|
|
case '^':
|
|
|
|
case '*':
|
|
|
|
case '\'':
|
|
|
|
case '"':
|
|
|
|
case '`':
|
|
|
|
case '~':
|
|
|
|
case '|':
|
|
|
|
return true;
|
|
|
|
default:
|
2012-05-21 11:21:19 +00:00
|
|
|
break;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
2012-05-21 11:21:19 +00:00
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QTextEngine::atSpace(int position) const
|
|
|
|
{
|
|
|
|
const QChar c = layoutData->string.at(position);
|
2012-05-21 11:21:19 +00:00
|
|
|
switch (c.unicode()) {
|
|
|
|
case QChar::Tabulation:
|
|
|
|
case QChar::Space:
|
|
|
|
case QChar::Nbsp:
|
|
|
|
case QChar::LineSeparator:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QTextEngine::indexAdditionalFormats()
|
|
|
|
{
|
|
|
|
specialData->addFormatIndices.resize(specialData->addFormats.count());
|
2012-09-03 16:06:17 +00:00
|
|
|
|
|
|
|
QTextFormatCollection *formats = this->formats();
|
|
|
|
|
|
|
|
if (!formats) {
|
|
|
|
Q_ASSERT(!block.docHandle());
|
|
|
|
specialData->formats.reset(new QTextFormatCollection);
|
|
|
|
formats = specialData->formats.data();
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < specialData->addFormats.count(); ++i) {
|
|
|
|
specialData->addFormatIndices[i] = formats->indexForFormat(specialData->addFormats.at(i).format);
|
|
|
|
specialData->addFormats[i].format = QTextCharFormat();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* These two helper functions are used to determine whether we need to insert a ZWJ character
|
|
|
|
between the text that gets truncated and the ellipsis. This is important to get
|
|
|
|
correctly shaped results for arabic text.
|
|
|
|
*/
|
2011-07-13 00:41:45 +00:00
|
|
|
static inline bool nextCharJoins(const QString &string, int pos)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
while (pos < string.length() && string.at(pos).category() == QChar::Mark_NonSpacing)
|
|
|
|
++pos;
|
|
|
|
if (pos == string.length())
|
|
|
|
return false;
|
|
|
|
return string.at(pos).joining() != QChar::OtherJoining;
|
|
|
|
}
|
|
|
|
|
2011-07-13 00:41:45 +00:00
|
|
|
static inline bool prevCharJoins(const QString &string, int pos)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
while (pos > 0 && string.at(pos - 1).category() == QChar::Mark_NonSpacing)
|
|
|
|
--pos;
|
|
|
|
if (pos == 0)
|
|
|
|
return false;
|
2011-07-13 00:41:45 +00:00
|
|
|
QChar::Joining joining = string.at(pos - 1).joining();
|
|
|
|
return (joining == QChar::Dual || joining == QChar::Center);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2012-05-21 11:21:19 +00:00
|
|
|
static inline bool isRetainableControlCode(QChar c)
|
2012-04-19 11:36:59 +00:00
|
|
|
{
|
|
|
|
return (c.unicode() == 0x202a // LRE
|
|
|
|
|| c.unicode() == 0x202b // LRE
|
|
|
|
|| c.unicode() == 0x202c // PDF
|
|
|
|
|| c.unicode() == 0x202d // LRO
|
|
|
|
|| c.unicode() == 0x202e // RLO
|
|
|
|
|| c.unicode() == 0x200e // LRM
|
|
|
|
|| c.unicode() == 0x200f); // RLM
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString stringMidRetainingBidiCC(const QString &string,
|
|
|
|
const QString &ellidePrefix,
|
|
|
|
const QString &ellideSuffix,
|
|
|
|
int subStringFrom,
|
|
|
|
int subStringTo,
|
|
|
|
int midStart,
|
|
|
|
int midLength)
|
|
|
|
{
|
|
|
|
QString prefix;
|
|
|
|
for (int i=subStringFrom; i<midStart; ++i) {
|
|
|
|
QChar c = string.at(i);
|
|
|
|
if (isRetainableControlCode(c))
|
|
|
|
prefix += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString suffix;
|
|
|
|
for (int i=midStart + midLength; i<subStringTo; ++i) {
|
|
|
|
QChar c = string.at(i);
|
|
|
|
if (isRetainableControlCode(c))
|
|
|
|
suffix += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefix + ellidePrefix + string.mid(midStart, midLength) + ellideSuffix + suffix;
|
|
|
|
}
|
|
|
|
|
2012-02-15 01:32:59 +00:00
|
|
|
QString QTextEngine::elidedText(Qt::TextElideMode mode, const QFixed &width, int flags, int from, int count) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
// qDebug() << "elidedText; available width" << width.toReal() << "text width:" << this->width(0, layoutData->string.length()).toReal();
|
|
|
|
|
|
|
|
if (flags & Qt::TextShowMnemonic) {
|
|
|
|
itemize();
|
2012-08-26 01:09:09 +00:00
|
|
|
QCharAttributes *attributes = const_cast<QCharAttributes *>(this->attributes());
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!attributes)
|
|
|
|
return QString();
|
|
|
|
for (int i = 0; i < layoutData->items.size(); ++i) {
|
|
|
|
QScriptItem &si = layoutData->items[i];
|
|
|
|
if (!si.num_glyphs)
|
|
|
|
shape(i);
|
|
|
|
|
|
|
|
unsigned short *logClusters = this->logClusters(&si);
|
|
|
|
QGlyphLayout glyphs = shapedGlyphs(&si);
|
|
|
|
|
|
|
|
const int end = si.position + length(&si);
|
|
|
|
for (int i = si.position; i < end - 1; ++i) {
|
|
|
|
if (layoutData->string.at(i) == QLatin1Char('&')) {
|
|
|
|
const int gp = logClusters[i - si.position];
|
|
|
|
glyphs.attributes[gp].dontPrint = true;
|
2012-08-26 01:09:09 +00:00
|
|
|
attributes[i + 1].graphemeBoundary = false;
|
|
|
|
attributes[i + 1].lineBreak = false;
|
2011-04-27 10:05:43 +00:00
|
|
|
attributes[i + 1].whiteSpace = false;
|
|
|
|
if (layoutData->string.at(i + 1) == QLatin1Char('&'))
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validate();
|
|
|
|
|
2012-02-15 01:32:59 +00:00
|
|
|
const int to = count >= 0 && count <= layoutData->string.length() - from
|
|
|
|
? from + count
|
|
|
|
: layoutData->string.length();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
if (mode == Qt::ElideNone
|
2012-02-15 01:32:59 +00:00
|
|
|
|| this->width(from, layoutData->string.length()) <= width
|
|
|
|
|| to - from <= 1)
|
|
|
|
return layoutData->string.mid(from, from - to);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QFixed ellipsisWidth;
|
|
|
|
QString ellipsisText;
|
|
|
|
{
|
|
|
|
QChar ellipsisChar(0x2026);
|
|
|
|
|
2012-12-21 16:35:58 +00:00
|
|
|
QFontEngine *fe = fnt.d->engineForScript(QChar::Script_Common);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QGlyphLayoutArray<1> ellipsisGlyph;
|
|
|
|
{
|
|
|
|
QFontEngine *feForEllipsis = (fe->type() == QFontEngine::Multi)
|
|
|
|
? static_cast<QFontEngineMulti *>(fe)->engine(0)
|
|
|
|
: fe;
|
|
|
|
|
|
|
|
if (feForEllipsis->type() == QFontEngine::Mac)
|
|
|
|
feForEllipsis = fe;
|
|
|
|
|
2012-06-24 22:18:10 +00:00
|
|
|
if (feForEllipsis->canRender(&ellipsisChar, 1)) {
|
|
|
|
int nGlyphs = 1;
|
|
|
|
feForEllipsis->stringToCMap(&ellipsisChar, 1, &ellipsisGlyph, &nGlyphs, 0);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ellipsisGlyph.glyphs[0]) {
|
|
|
|
ellipsisWidth = ellipsisGlyph.advances_x[0];
|
|
|
|
ellipsisText = ellipsisChar;
|
|
|
|
} else {
|
|
|
|
QString dotDotDot(QLatin1String("..."));
|
|
|
|
|
|
|
|
QGlyphLayoutArray<3> glyphs;
|
|
|
|
int nGlyphs = 3;
|
|
|
|
if (!fe->stringToCMap(dotDotDot.constData(), 3, &glyphs, &nGlyphs, 0))
|
|
|
|
// should never happen...
|
|
|
|
return layoutData->string;
|
|
|
|
for (int i = 0; i < nGlyphs; ++i)
|
|
|
|
ellipsisWidth += glyphs.advances_x[i];
|
|
|
|
ellipsisText = dotDotDot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QFixed availableWidth = width - ellipsisWidth;
|
|
|
|
if (availableWidth < 0)
|
|
|
|
return QString();
|
|
|
|
|
2012-08-26 01:09:09 +00:00
|
|
|
const QCharAttributes *attributes = this->attributes();
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!attributes)
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
if (mode == Qt::ElideRight) {
|
|
|
|
QFixed currentWidth;
|
|
|
|
int pos;
|
2012-02-15 01:32:59 +00:00
|
|
|
int nextBreak = from;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
pos = nextBreak;
|
|
|
|
|
|
|
|
++nextBreak;
|
2012-08-26 01:09:09 +00:00
|
|
|
while (nextBreak < layoutData->string.length() && !attributes[nextBreak].graphemeBoundary)
|
2011-04-27 10:05:43 +00:00
|
|
|
++nextBreak;
|
|
|
|
|
|
|
|
currentWidth += this->width(pos, nextBreak - pos);
|
2012-02-15 01:32:59 +00:00
|
|
|
} while (nextBreak < to
|
2011-04-27 10:05:43 +00:00
|
|
|
&& currentWidth < availableWidth);
|
|
|
|
|
|
|
|
if (nextCharJoins(layoutData->string, pos))
|
|
|
|
ellipsisText.prepend(QChar(0x200d) /* ZWJ */);
|
|
|
|
|
2012-04-19 11:36:59 +00:00
|
|
|
return stringMidRetainingBidiCC(layoutData->string,
|
|
|
|
QString(), ellipsisText,
|
|
|
|
from, to,
|
|
|
|
from, pos - from);
|
2011-04-27 10:05:43 +00:00
|
|
|
} else if (mode == Qt::ElideLeft) {
|
|
|
|
QFixed currentWidth;
|
|
|
|
int pos;
|
2012-02-15 01:32:59 +00:00
|
|
|
int nextBreak = to;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
pos = nextBreak;
|
|
|
|
|
|
|
|
--nextBreak;
|
2012-08-26 01:09:09 +00:00
|
|
|
while (nextBreak > 0 && !attributes[nextBreak].graphemeBoundary)
|
2011-04-27 10:05:43 +00:00
|
|
|
--nextBreak;
|
|
|
|
|
|
|
|
currentWidth += this->width(nextBreak, pos - nextBreak);
|
2012-02-15 01:32:59 +00:00
|
|
|
} while (nextBreak > from
|
2011-04-27 10:05:43 +00:00
|
|
|
&& currentWidth < availableWidth);
|
|
|
|
|
|
|
|
if (prevCharJoins(layoutData->string, pos))
|
|
|
|
ellipsisText.append(QChar(0x200d) /* ZWJ */);
|
|
|
|
|
2012-04-19 11:36:59 +00:00
|
|
|
return stringMidRetainingBidiCC(layoutData->string,
|
|
|
|
ellipsisText, QString(),
|
|
|
|
from, to,
|
|
|
|
pos, to - pos);
|
2011-04-27 10:05:43 +00:00
|
|
|
} else if (mode == Qt::ElideMiddle) {
|
|
|
|
QFixed leftWidth;
|
|
|
|
QFixed rightWidth;
|
|
|
|
|
2012-02-15 01:32:59 +00:00
|
|
|
int leftPos = from;
|
|
|
|
int nextLeftBreak = from;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-02-15 01:32:59 +00:00
|
|
|
int rightPos = to;
|
|
|
|
int nextRightBreak = to;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
leftPos = nextLeftBreak;
|
|
|
|
rightPos = nextRightBreak;
|
|
|
|
|
|
|
|
++nextLeftBreak;
|
2012-08-26 01:09:09 +00:00
|
|
|
while (nextLeftBreak < layoutData->string.length() && !attributes[nextLeftBreak].graphemeBoundary)
|
2011-04-27 10:05:43 +00:00
|
|
|
++nextLeftBreak;
|
|
|
|
|
|
|
|
--nextRightBreak;
|
2012-08-26 01:09:09 +00:00
|
|
|
while (nextRightBreak > from && !attributes[nextRightBreak].graphemeBoundary)
|
2011-04-27 10:05:43 +00:00
|
|
|
--nextRightBreak;
|
|
|
|
|
|
|
|
leftWidth += this->width(leftPos, nextLeftBreak - leftPos);
|
|
|
|
rightWidth += this->width(nextRightBreak, rightPos - nextRightBreak);
|
2012-02-15 01:32:59 +00:00
|
|
|
} while (nextLeftBreak < to
|
|
|
|
&& nextRightBreak > from
|
2011-04-27 10:05:43 +00:00
|
|
|
&& leftWidth + rightWidth < availableWidth);
|
|
|
|
|
|
|
|
if (nextCharJoins(layoutData->string, leftPos))
|
|
|
|
ellipsisText.prepend(QChar(0x200d) /* ZWJ */);
|
|
|
|
if (prevCharJoins(layoutData->string, rightPos))
|
|
|
|
ellipsisText.append(QChar(0x200d) /* ZWJ */);
|
|
|
|
|
2012-02-15 01:32:59 +00:00
|
|
|
return layoutData->string.mid(from, leftPos - from) + ellipsisText + layoutData->string.mid(rightPos, to - rightPos);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 01:32:59 +00:00
|
|
|
return layoutData->string.mid(from, to - from);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2012-07-19 12:42:37 +00:00
|
|
|
namespace {
|
|
|
|
struct QScriptItemComparator {
|
|
|
|
bool operator()(const QScriptItem &a, const QScriptItem &b) { return a.position < b.position; }
|
|
|
|
bool operator()(int p, const QScriptItem &b) { return p < b.position; }
|
2012-10-02 14:50:01 +00:00
|
|
|
#if defined(Q_CC_MSVC) && _MSC_VER < 1600
|
|
|
|
//The STL implementation of MSVC 2008 requires the definition
|
|
|
|
bool operator()(const QScriptItem &a, int p) { return a.position < p; }
|
|
|
|
#endif
|
2012-07-19 12:42:37 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void QTextEngine::setBoundary(int strPos) const
|
|
|
|
{
|
|
|
|
if (strPos <= 0 || strPos >= layoutData->string.length())
|
|
|
|
return;
|
|
|
|
|
2012-09-18 21:41:59 +00:00
|
|
|
const QScriptItem* it = std::upper_bound(layoutData->items.constBegin(), layoutData->items.constEnd(),
|
|
|
|
strPos, QScriptItemComparator());
|
2012-07-19 12:42:37 +00:00
|
|
|
Q_ASSERT(it > layoutData->items.constBegin());
|
|
|
|
--it;
|
|
|
|
if (it->position == strPos) {
|
2011-04-27 10:05:43 +00:00
|
|
|
// already a split at the requested position
|
|
|
|
return;
|
|
|
|
}
|
2012-07-19 12:42:37 +00:00
|
|
|
splitItem(it - layoutData->items.constBegin(), strPos - it->position);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::splitItem(int item, int pos) const
|
|
|
|
{
|
|
|
|
if (pos <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
layoutData->items.insert(item + 1, layoutData->items[item]);
|
|
|
|
QScriptItem &oldItem = layoutData->items[item];
|
|
|
|
QScriptItem &newItem = layoutData->items[item+1];
|
|
|
|
newItem.position += pos;
|
|
|
|
|
|
|
|
if (oldItem.num_glyphs) {
|
|
|
|
// already shaped, break glyphs aswell
|
|
|
|
int breakGlyph = logClusters(&oldItem)[pos];
|
|
|
|
|
|
|
|
newItem.num_glyphs = oldItem.num_glyphs - breakGlyph;
|
|
|
|
oldItem.num_glyphs = breakGlyph;
|
|
|
|
newItem.glyph_data_offset = oldItem.glyph_data_offset + breakGlyph;
|
|
|
|
|
|
|
|
for (int i = 0; i < newItem.num_glyphs; i++)
|
|
|
|
logClusters(&newItem)[i] -= breakGlyph;
|
|
|
|
|
|
|
|
QFixed w = 0;
|
|
|
|
const QGlyphLayout g = shapedGlyphs(&oldItem);
|
|
|
|
for(int j = 0; j < breakGlyph; ++j)
|
2011-09-08 08:14:33 +00:00
|
|
|
w += g.advances_x[j] * !g.attributes[j].dontPrint;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
newItem.width = oldItem.width - w;
|
|
|
|
oldItem.width = w;
|
|
|
|
}
|
|
|
|
|
|
|
|
// qDebug("split at position %d itempos=%d", pos, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
QFixed QTextEngine::calculateTabWidth(int item, QFixed x) const
|
|
|
|
{
|
|
|
|
const QScriptItem &si = layoutData->items[item];
|
|
|
|
|
|
|
|
QFixed dpiScale = 1;
|
|
|
|
if (block.docHandle() && block.docHandle()->layout()) {
|
|
|
|
QPaintDevice *pdev = block.docHandle()->layout()->paintDevice();
|
|
|
|
if (pdev)
|
|
|
|
dpiScale = QFixed::fromReal(pdev->logicalDpiY() / qreal(qt_defaultDpiY()));
|
|
|
|
} else {
|
|
|
|
dpiScale = QFixed::fromReal(fnt.d->dpi / qreal(qt_defaultDpiY()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QTextOption::Tab> tabArray = option.tabs();
|
|
|
|
if (!tabArray.isEmpty()) {
|
|
|
|
if (isRightToLeft()) { // rebase the tabArray positions.
|
|
|
|
QList<QTextOption::Tab> newTabs;
|
|
|
|
QList<QTextOption::Tab>::Iterator iter = tabArray.begin();
|
|
|
|
while(iter != tabArray.end()) {
|
|
|
|
QTextOption::Tab tab = *iter;
|
|
|
|
if (tab.type == QTextOption::LeftTab)
|
|
|
|
tab.type = QTextOption::RightTab;
|
|
|
|
else if (tab.type == QTextOption::RightTab)
|
|
|
|
tab.type = QTextOption::LeftTab;
|
|
|
|
newTabs << tab;
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
tabArray = newTabs;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < tabArray.size(); ++i) {
|
|
|
|
QFixed tab = QFixed::fromReal(tabArray[i].position) * dpiScale;
|
|
|
|
if (tab > x) { // this is the tab we need.
|
|
|
|
QTextOption::Tab tabSpec = tabArray[i];
|
|
|
|
int tabSectionEnd = layoutData->string.count();
|
|
|
|
if (tabSpec.type == QTextOption::RightTab || tabSpec.type == QTextOption::CenterTab) {
|
|
|
|
// find next tab to calculate the width required.
|
|
|
|
tab = QFixed::fromReal(tabSpec.position);
|
|
|
|
for (int i=item + 1; i < layoutData->items.count(); i++) {
|
|
|
|
const QScriptItem &item = layoutData->items[i];
|
|
|
|
if (item.analysis.flags == QScriptAnalysis::TabOrObject) { // found it.
|
|
|
|
tabSectionEnd = item.position;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tabSpec.type == QTextOption::DelimiterTab)
|
|
|
|
// find delimitor character to calculate the width required
|
|
|
|
tabSectionEnd = qMax(si.position, layoutData->string.indexOf(tabSpec.delimiter, si.position) + 1);
|
|
|
|
|
|
|
|
if (tabSectionEnd > si.position) {
|
|
|
|
QFixed length;
|
|
|
|
// Calculate the length of text between this tab and the tabSectionEnd
|
|
|
|
for (int i=item; i < layoutData->items.count(); i++) {
|
|
|
|
QScriptItem &item = layoutData->items[i];
|
|
|
|
if (item.position > tabSectionEnd || item.position <= si.position)
|
|
|
|
continue;
|
|
|
|
shape(i); // first, lets make sure relevant text is already shaped
|
|
|
|
QGlyphLayout glyphs = this->shapedGlyphs(&item);
|
|
|
|
const int end = qMin(item.position + item.num_glyphs, tabSectionEnd) - item.position;
|
|
|
|
for (int i=0; i < end; i++)
|
|
|
|
length += glyphs.advances_x[i] * !glyphs.attributes[i].dontPrint;
|
|
|
|
if (end + item.position == tabSectionEnd && tabSpec.type == QTextOption::DelimiterTab) // remove half of matching char
|
|
|
|
length -= glyphs.advances_x[end] / 2 * !glyphs.attributes[end].dontPrint;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tabSpec.type) {
|
|
|
|
case QTextOption::CenterTab:
|
|
|
|
length /= 2;
|
|
|
|
// fall through
|
|
|
|
case QTextOption::DelimiterTab:
|
|
|
|
// fall through
|
|
|
|
case QTextOption::RightTab:
|
|
|
|
tab = QFixed::fromReal(tabSpec.position) * dpiScale - length;
|
2012-01-10 19:09:58 +00:00
|
|
|
if (tab < x) // default to tab taking no space
|
2011-04-27 10:05:43 +00:00
|
|
|
return QFixed();
|
|
|
|
break;
|
|
|
|
case QTextOption::LeftTab:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tab - x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QFixed tab = QFixed::fromReal(option.tabStop());
|
|
|
|
if (tab <= 0)
|
|
|
|
tab = 80; // default
|
|
|
|
tab *= dpiScale;
|
|
|
|
QFixed nextTabPos = ((x / tab).truncate() + 1) * tab;
|
|
|
|
QFixed tabWidth = nextTabPos - x;
|
|
|
|
|
|
|
|
return tabWidth;
|
|
|
|
}
|
|
|
|
|
2012-07-19 12:54:10 +00:00
|
|
|
namespace {
|
|
|
|
class FormatRangeComparatorByStart {
|
|
|
|
const QList<QTextLayout::FormatRange> &list;
|
|
|
|
public:
|
|
|
|
FormatRangeComparatorByStart(const QList<QTextLayout::FormatRange> &list) : list(list) { }
|
|
|
|
bool operator()(int a, int b) {
|
|
|
|
return list.at(a).start < list.at(b).start;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
class FormatRangeComparatorByEnd {
|
|
|
|
const QList<QTextLayout::FormatRange> &list;
|
|
|
|
public:
|
|
|
|
FormatRangeComparatorByEnd(const QList<QTextLayout::FormatRange> &list) : list(list) { }
|
|
|
|
bool operator()(int a, int b) {
|
|
|
|
return list.at(a).start + list.at(a).length < list.at(b).start + list.at(b).length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void QTextEngine::resolveAdditionalFormats() const
|
|
|
|
{
|
|
|
|
if (!specialData || specialData->addFormats.isEmpty()
|
|
|
|
|| !specialData->resolvedFormatIndices.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2012-09-03 16:06:17 +00:00
|
|
|
QTextFormatCollection *collection = formats();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
specialData->resolvedFormatIndices.clear();
|
|
|
|
QVector<int> indices(layoutData->items.count());
|
2012-07-19 12:54:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
QVarLengthArray<int, 64> addFormatSortedByStart;
|
|
|
|
addFormatSortedByStart.reserve(specialData->addFormats.count());
|
2012-09-05 18:42:28 +00:00
|
|
|
for (int i = 0; i < specialData->addFormats.count(); ++i) {
|
|
|
|
if (specialData->addFormats.at(i).length >= 0)
|
|
|
|
addFormatSortedByStart.append(i);
|
|
|
|
}
|
2012-07-19 12:54:10 +00:00
|
|
|
QVarLengthArray<int, 64> addFormatSortedByEnd = addFormatSortedByStart;
|
|
|
|
qSort(addFormatSortedByStart.begin(), addFormatSortedByStart.end(),
|
|
|
|
FormatRangeComparatorByStart(specialData->addFormats));
|
|
|
|
qSort(addFormatSortedByEnd.begin(), addFormatSortedByEnd.end(),
|
|
|
|
FormatRangeComparatorByEnd(specialData->addFormats));
|
|
|
|
|
|
|
|
QVarLengthArray<int, 16> currentFormats;
|
|
|
|
const int *startIt = addFormatSortedByStart.constBegin();
|
|
|
|
const int *endIt = addFormatSortedByEnd.constBegin();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
for (int i = 0; i < layoutData->items.count(); ++i) {
|
2012-07-19 12:54:10 +00:00
|
|
|
const QScriptItem *si = &layoutData->items.at(i);
|
|
|
|
int end = si->position + length(si);
|
|
|
|
|
2012-10-12 04:08:18 +00:00
|
|
|
while (startIt != addFormatSortedByStart.constEnd() &&
|
2012-07-19 12:54:10 +00:00
|
|
|
specialData->addFormats.at(*startIt).start <= si->position) {
|
2012-09-18 21:41:59 +00:00
|
|
|
currentFormats.insert(std::upper_bound(currentFormats.begin(), currentFormats.end(), *startIt),
|
2012-07-19 12:54:10 +00:00
|
|
|
*startIt);
|
|
|
|
++startIt;
|
|
|
|
}
|
2012-10-12 04:08:18 +00:00
|
|
|
while (endIt != addFormatSortedByEnd.constEnd() &&
|
2012-07-19 12:54:10 +00:00
|
|
|
specialData->addFormats.at(*endIt).start + specialData->addFormats.at(*endIt).length < end) {
|
|
|
|
currentFormats.remove(qBinaryFind(currentFormats, *endIt) - currentFormats.begin());
|
|
|
|
++endIt;
|
|
|
|
}
|
|
|
|
QTextCharFormat format;
|
|
|
|
if (block.docHandle()) {
|
2012-09-03 16:06:17 +00:00
|
|
|
// when we have a docHandle, formatIndex might still return a valid index based
|
|
|
|
// on the preeditPosition. for all other cases, we cleared the resolved format indices
|
|
|
|
format = collection->charFormat(formatIndex(si));
|
2012-07-19 12:54:10 +00:00
|
|
|
}
|
2012-09-03 16:06:17 +00:00
|
|
|
|
2012-07-19 12:54:10 +00:00
|
|
|
foreach (int cur, currentFormats) {
|
|
|
|
const QTextLayout::FormatRange &r = specialData->addFormats.at(cur);
|
|
|
|
Q_ASSERT (r.start <= si->position && r.start + r.length >= end);
|
|
|
|
if (!specialData->addFormatIndices.isEmpty()) {
|
2012-09-03 16:06:17 +00:00
|
|
|
format.merge(collection->format(specialData->addFormatIndices.at(cur)));
|
2012-07-19 12:54:10 +00:00
|
|
|
} else {
|
|
|
|
format.merge(r.format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
indices[i] = collection->indexForFormat(format);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
specialData->resolvedFormatIndices = indices;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFixed QTextEngine::leadingSpaceWidth(const QScriptLine &line)
|
|
|
|
{
|
|
|
|
if (!line.hasTrailingSpaces
|
|
|
|
|| (option.flags() & QTextOption::IncludeTrailingSpaces)
|
|
|
|
|| !isRightToLeft())
|
|
|
|
return QFixed();
|
|
|
|
|
2012-05-09 06:14:34 +00:00
|
|
|
return width(line.from + line.length, line.trailingSpaces);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 14:11:45 +00:00
|
|
|
QFixed QTextEngine::alignLine(const QScriptLine &line)
|
|
|
|
{
|
|
|
|
QFixed x = 0;
|
|
|
|
justify(line);
|
|
|
|
// if width is QFIXED_MAX that means we used setNumColumns() and that implicitly makes this line left aligned.
|
|
|
|
if (!line.justified && line.width != QFIXED_MAX) {
|
|
|
|
int align = option.alignment();
|
|
|
|
if (align & Qt::AlignJustify && isRightToLeft())
|
|
|
|
align = Qt::AlignRight;
|
|
|
|
if (align & Qt::AlignRight)
|
2012-05-09 06:14:34 +00:00
|
|
|
x = line.width - (line.textAdvance);
|
2010-12-15 14:11:45 +00:00
|
|
|
else if (align & Qt::AlignHCenter)
|
2012-05-09 06:14:34 +00:00
|
|
|
x = (line.width - line.textAdvance)/2;
|
2010-12-15 14:11:45 +00:00
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFixed QTextEngine::offsetInLigature(const QScriptItem *si, int pos, int max, int glyph_pos)
|
|
|
|
{
|
|
|
|
unsigned short *logClusters = this->logClusters(si);
|
|
|
|
const QGlyphLayout &glyphs = shapedGlyphs(si);
|
|
|
|
|
|
|
|
int offsetInCluster = 0;
|
|
|
|
for (int i = pos - 1; i >= 0; i--) {
|
|
|
|
if (logClusters[i] == glyph_pos)
|
|
|
|
offsetInCluster++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in the case that the offset is inside a (multi-character) glyph,
|
|
|
|
// interpolate the position.
|
|
|
|
if (offsetInCluster > 0) {
|
|
|
|
int clusterLength = 0;
|
|
|
|
for (int i = pos - offsetInCluster; i < max; i++) {
|
|
|
|
if (logClusters[i] == glyph_pos)
|
|
|
|
clusterLength++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (clusterLength)
|
|
|
|
return glyphs.advances_x[glyph_pos] * offsetInCluster / clusterLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-19 08:29:49 +00:00
|
|
|
// Scan in logClusters[from..to-1] for glyph_pos
|
|
|
|
int QTextEngine::getClusterLength(unsigned short *logClusters,
|
2012-08-26 01:09:09 +00:00
|
|
|
const QCharAttributes *attributes,
|
2011-05-19 08:29:49 +00:00
|
|
|
int from, int to, int glyph_pos, int *start)
|
|
|
|
{
|
|
|
|
int clusterLength = 0;
|
|
|
|
for (int i = from; i < to; i++) {
|
2012-08-26 01:09:09 +00:00
|
|
|
if (logClusters[i] == glyph_pos && attributes[i].graphemeBoundary) {
|
2011-05-19 08:29:49 +00:00
|
|
|
if (*start < 0)
|
|
|
|
*start = i;
|
|
|
|
clusterLength++;
|
|
|
|
}
|
|
|
|
else if (clusterLength)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return clusterLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTextEngine::positionInLigature(const QScriptItem *si, int end,
|
|
|
|
QFixed x, QFixed edge, int glyph_pos,
|
|
|
|
bool cursorOnCharacter)
|
|
|
|
{
|
|
|
|
unsigned short *logClusters = this->logClusters(si);
|
|
|
|
int clusterStart = -1;
|
|
|
|
int clusterLength = 0;
|
|
|
|
|
2012-12-21 16:35:58 +00:00
|
|
|
if (si->analysis.script != QChar::Script_Common &&
|
|
|
|
si->analysis.script != QChar::Script_Greek) {
|
2011-05-19 08:29:49 +00:00
|
|
|
if (glyph_pos == -1)
|
|
|
|
return si->position + end;
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < end; i++)
|
|
|
|
if (logClusters[i] == glyph_pos)
|
|
|
|
break;
|
|
|
|
return si->position + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (glyph_pos == -1 && end > 0)
|
|
|
|
glyph_pos = logClusters[end - 1];
|
|
|
|
else {
|
|
|
|
if (x <= edge)
|
|
|
|
glyph_pos--;
|
|
|
|
}
|
|
|
|
|
2012-08-26 01:09:09 +00:00
|
|
|
const QCharAttributes *attrs = attributes();
|
2011-07-13 13:06:43 +00:00
|
|
|
logClusters = this->logClusters(si);
|
2011-05-19 08:29:49 +00:00
|
|
|
clusterLength = getClusterLength(logClusters, attrs, 0, end, glyph_pos, &clusterStart);
|
|
|
|
|
|
|
|
if (clusterLength) {
|
|
|
|
const QGlyphLayout &glyphs = shapedGlyphs(si);
|
|
|
|
QFixed glyphWidth = glyphs.effectiveAdvance(glyph_pos);
|
|
|
|
// the approximate width of each individual element of the ligature
|
|
|
|
QFixed perItemWidth = glyphWidth / clusterLength;
|
2012-02-27 08:51:46 +00:00
|
|
|
if (perItemWidth <= 0)
|
|
|
|
return si->position + clusterStart;
|
2011-05-19 08:29:49 +00:00
|
|
|
QFixed left = x > edge ? edge : edge - glyphWidth;
|
|
|
|
int n = ((x - left) / perItemWidth).floor().toInt();
|
|
|
|
QFixed dist = x - left - n * perItemWidth;
|
|
|
|
int closestItem = dist > (perItemWidth / 2) ? n + 1 : n;
|
|
|
|
if (cursorOnCharacter && closestItem > 0)
|
|
|
|
closestItem--;
|
|
|
|
int pos = si->position + clusterStart + closestItem;
|
2012-08-26 01:09:09 +00:00
|
|
|
// Jump to the next grapheme boundary
|
|
|
|
while (pos < end && !attrs[pos].graphemeBoundary)
|
2011-05-19 08:29:49 +00:00
|
|
|
pos++;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
return si->position + end;
|
|
|
|
}
|
|
|
|
|
2010-12-15 14:11:45 +00:00
|
|
|
int QTextEngine::previousLogicalPosition(int oldPos) const
|
|
|
|
{
|
2012-08-26 01:09:09 +00:00
|
|
|
const QCharAttributes *attrs = attributes();
|
2010-12-15 14:11:45 +00:00
|
|
|
if (!attrs || oldPos < 0)
|
|
|
|
return oldPos;
|
|
|
|
|
|
|
|
if (oldPos <= 0)
|
|
|
|
return 0;
|
|
|
|
oldPos--;
|
2012-08-26 01:09:09 +00:00
|
|
|
while (oldPos && !attrs[oldPos].graphemeBoundary)
|
2010-12-15 14:11:45 +00:00
|
|
|
oldPos--;
|
|
|
|
return oldPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTextEngine::nextLogicalPosition(int oldPos) const
|
|
|
|
{
|
2012-08-26 01:09:09 +00:00
|
|
|
const QCharAttributes *attrs = attributes();
|
2010-12-15 14:11:45 +00:00
|
|
|
int len = block.isValid() ? block.length() - 1
|
|
|
|
: layoutData->string.length();
|
|
|
|
Q_ASSERT(len <= layoutData->string.length());
|
|
|
|
if (!attrs || oldPos < 0 || oldPos >= len)
|
|
|
|
return oldPos;
|
|
|
|
|
|
|
|
oldPos++;
|
2012-08-26 01:09:09 +00:00
|
|
|
while (oldPos < len && !attrs[oldPos].graphemeBoundary)
|
2010-12-15 14:11:45 +00:00
|
|
|
oldPos++;
|
|
|
|
return oldPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTextEngine::lineNumberForTextPosition(int pos)
|
|
|
|
{
|
|
|
|
if (!layoutData)
|
|
|
|
itemize();
|
|
|
|
if (pos == layoutData->string.length() && lines.size())
|
|
|
|
return lines.size() - 1;
|
|
|
|
for (int i = 0; i < lines.size(); ++i) {
|
|
|
|
const QScriptLine& line = lines[i];
|
2011-09-08 14:31:53 +00:00
|
|
|
if (line.from + line.length + line.trailingSpaces > pos)
|
2010-12-15 14:11:45 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::insertionPointsForLine(int lineNum, QVector<int> &insertionPoints)
|
|
|
|
{
|
|
|
|
QTextLineItemIterator iterator(this, lineNum);
|
|
|
|
bool rtl = isRightToLeft();
|
|
|
|
bool lastLine = lineNum >= lines.size() - 1;
|
|
|
|
|
|
|
|
while (!iterator.atEnd()) {
|
|
|
|
iterator.next();
|
|
|
|
const QScriptItem *si = &layoutData->items[iterator.item];
|
|
|
|
if (si->analysis.bidiLevel % 2) {
|
|
|
|
int i = iterator.itemEnd - 1, min = iterator.itemStart;
|
|
|
|
if (lastLine && (rtl ? iterator.atBeginning() : iterator.atEnd()))
|
|
|
|
i++;
|
|
|
|
for (; i >= min; i--)
|
|
|
|
insertionPoints.push_back(i);
|
|
|
|
} else {
|
|
|
|
int i = iterator.itemStart, max = iterator.itemEnd;
|
|
|
|
if (lastLine && (rtl ? iterator.atBeginning() : iterator.atEnd()))
|
|
|
|
max++;
|
|
|
|
for (; i < max; i++)
|
|
|
|
insertionPoints.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTextEngine::endOfLine(int lineNum)
|
|
|
|
{
|
|
|
|
QVector<int> insertionPoints;
|
|
|
|
insertionPointsForLine(lineNum, insertionPoints);
|
|
|
|
|
|
|
|
if (insertionPoints.size() > 0)
|
|
|
|
return insertionPoints.last();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTextEngine::beginningOfLine(int lineNum)
|
|
|
|
{
|
|
|
|
QVector<int> insertionPoints;
|
|
|
|
insertionPointsForLine(lineNum, insertionPoints);
|
|
|
|
|
|
|
|
if (insertionPoints.size() > 0)
|
|
|
|
return insertionPoints.first();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTextEngine::positionAfterVisualMovement(int pos, QTextCursor::MoveOperation op)
|
|
|
|
{
|
|
|
|
if (!layoutData)
|
|
|
|
itemize();
|
|
|
|
|
|
|
|
bool moveRight = (op == QTextCursor::Right);
|
|
|
|
bool alignRight = isRightToLeft();
|
|
|
|
if (!layoutData->hasBidi)
|
|
|
|
return moveRight ^ alignRight ? nextLogicalPosition(pos) : previousLogicalPosition(pos);
|
|
|
|
|
|
|
|
int lineNum = lineNumberForTextPosition(pos);
|
|
|
|
Q_ASSERT(lineNum >= 0);
|
|
|
|
|
|
|
|
QVector<int> insertionPoints;
|
|
|
|
insertionPointsForLine(lineNum, insertionPoints);
|
|
|
|
int i, max = insertionPoints.size();
|
|
|
|
for (i = 0; i < max; i++)
|
|
|
|
if (pos == insertionPoints[i]) {
|
|
|
|
if (moveRight) {
|
|
|
|
if (i + 1 < max)
|
|
|
|
return insertionPoints[i + 1];
|
|
|
|
} else {
|
|
|
|
if (i > 0)
|
|
|
|
return insertionPoints[i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moveRight ^ alignRight) {
|
|
|
|
if (lineNum + 1 < lines.size())
|
|
|
|
return alignRight ? endOfLine(lineNum + 1) : beginningOfLine(lineNum + 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (lineNum > 0)
|
|
|
|
return alignRight ? beginningOfLine(lineNum - 1) : endOfLine(lineNum - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2012-03-01 16:14:38 +00:00
|
|
|
void QTextEngine::addItemDecoration(QPainter *painter, const QLineF &line, ItemDecorationList *decorationList)
|
|
|
|
{
|
|
|
|
if (delayDecorations) {
|
|
|
|
decorationList->append(ItemDecoration(line.x1(), line.x2(), line.y1(), painter->pen()));
|
|
|
|
} else {
|
|
|
|
painter->drawLine(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::addUnderline(QPainter *painter, const QLineF &line)
|
|
|
|
{
|
|
|
|
// qDebug() << "Adding underline:" << line;
|
|
|
|
addItemDecoration(painter, line, &underlineList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::addStrikeOut(QPainter *painter, const QLineF &line)
|
|
|
|
{
|
|
|
|
addItemDecoration(painter, line, &strikeOutList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::addOverline(QPainter *painter, const QLineF &line)
|
|
|
|
{
|
|
|
|
addItemDecoration(painter, line, &overlineList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::drawItemDecorationList(QPainter *painter, const ItemDecorationList &decorationList)
|
|
|
|
{
|
|
|
|
// qDebug() << "Drawing" << decorationList.size() << "decorations";
|
|
|
|
if (decorationList.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2012-06-23 09:06:43 +00:00
|
|
|
foreach (const ItemDecoration &decoration, decorationList) {
|
2012-03-01 16:14:38 +00:00
|
|
|
painter->setPen(decoration.pen);
|
2012-10-23 11:14:27 +00:00
|
|
|
painter->drawLine(QLineF(decoration.x1, decoration.y, decoration.x2, decoration.y));
|
2012-03-01 16:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::drawDecorations(QPainter *painter)
|
|
|
|
{
|
|
|
|
QPen oldPen = painter->pen();
|
|
|
|
|
2012-10-23 11:14:27 +00:00
|
|
|
bool wasCompatiblePainting = painter->renderHints()
|
|
|
|
& QPainter::Qt4CompatiblePainting;
|
|
|
|
|
|
|
|
if (wasCompatiblePainting)
|
|
|
|
painter->setRenderHint(QPainter::Qt4CompatiblePainting, false);
|
|
|
|
|
2012-03-01 16:14:38 +00:00
|
|
|
adjustUnderlines();
|
|
|
|
drawItemDecorationList(painter, underlineList);
|
|
|
|
drawItemDecorationList(painter, strikeOutList);
|
|
|
|
drawItemDecorationList(painter, overlineList);
|
|
|
|
|
|
|
|
clearDecorations();
|
2012-10-23 11:14:27 +00:00
|
|
|
|
|
|
|
if (wasCompatiblePainting)
|
|
|
|
painter->setRenderHint(QPainter::Qt4CompatiblePainting);
|
|
|
|
|
|
|
|
painter->setPen(oldPen);
|
2012-03-01 16:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::clearDecorations()
|
|
|
|
{
|
|
|
|
underlineList.clear();
|
|
|
|
strikeOutList.clear();
|
|
|
|
overlineList.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::adjustUnderlines()
|
|
|
|
{
|
|
|
|
// qDebug() << __PRETTY_FUNCTION__ << underlineList.count() << "underlines";
|
|
|
|
if (underlineList.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ItemDecorationList::iterator start = underlineList.begin();
|
|
|
|
ItemDecorationList::iterator end = underlineList.end();
|
|
|
|
ItemDecorationList::iterator it = start;
|
|
|
|
qreal underlinePos = start->y;
|
|
|
|
qreal penWidth = start->pen.widthF();
|
|
|
|
qreal lastLineEnd = start->x1;
|
|
|
|
|
|
|
|
while (it != end) {
|
|
|
|
if (qFuzzyCompare(lastLineEnd, it->x1)) { // no gap between underlines
|
|
|
|
underlinePos = qMax(underlinePos, it->y);
|
|
|
|
penWidth = qMax(penWidth, it->pen.widthF());
|
|
|
|
} else { // gap between this and the last underline
|
|
|
|
adjustUnderlines(start, it, underlinePos, penWidth);
|
|
|
|
start = it;
|
|
|
|
underlinePos = start->y;
|
|
|
|
penWidth = start->pen.widthF();
|
|
|
|
}
|
|
|
|
lastLineEnd = it->x2;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
adjustUnderlines(start, end, underlinePos, penWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTextEngine::adjustUnderlines(ItemDecorationList::iterator start,
|
|
|
|
ItemDecorationList::iterator end,
|
|
|
|
qreal underlinePos, qreal penWidth)
|
|
|
|
{
|
|
|
|
for (ItemDecorationList::iterator it = start; it != end; ++it) {
|
|
|
|
it->y = underlinePos;
|
2012-05-24 13:40:16 +00:00
|
|
|
it->pen.setWidthF(penWidth);
|
2012-03-01 16:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QStackTextEngine::QStackTextEngine(const QString &string, const QFont &f)
|
|
|
|
: QTextEngine(string, f),
|
|
|
|
_layoutData(string, _memory, MemSize)
|
|
|
|
{
|
|
|
|
stackEngine = true;
|
|
|
|
layoutData = &_layoutData;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextItemInt::QTextItemInt(const QScriptItem &si, QFont *font, const QTextCharFormat &format)
|
|
|
|
: justified(false), underlineStyle(QTextCharFormat::NoUnderline), charFormat(format),
|
|
|
|
num_chars(0), chars(0), logClusters(0), f(0), fontEngine(0)
|
2011-07-08 15:40:43 +00:00
|
|
|
{
|
|
|
|
f = font;
|
|
|
|
fontEngine = f->d->engineForScript(si.analysis.script);
|
|
|
|
Q_ASSERT(fontEngine);
|
|
|
|
|
|
|
|
initWithScriptItem(si);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextItemInt::QTextItemInt(const QGlyphLayout &g, QFont *font, const QChar *chars_, int numChars, QFontEngine *fe, const QTextCharFormat &format)
|
|
|
|
: flags(0), justified(false), underlineStyle(QTextCharFormat::NoUnderline), charFormat(format),
|
|
|
|
num_chars(numChars), chars(chars_), logClusters(0), f(font), glyphs(g), fontEngine(fe)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix up flags and underlineStyle with given info
|
|
|
|
void QTextItemInt::initWithScriptItem(const QScriptItem &si)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
// explicitly initialize flags so that initFontAttributes can be called
|
|
|
|
// multiple times on the same TextItem
|
|
|
|
flags = 0;
|
|
|
|
if (si.analysis.bidiLevel %2)
|
|
|
|
flags |= QTextItem::RightToLeft;
|
|
|
|
ascent = si.ascent;
|
|
|
|
descent = si.descent;
|
|
|
|
|
2011-07-08 15:40:43 +00:00
|
|
|
if (charFormat.hasProperty(QTextFormat::TextUnderlineStyle)) {
|
|
|
|
underlineStyle = charFormat.underlineStyle();
|
|
|
|
} else if (charFormat.boolProperty(QTextFormat::FontUnderline)
|
2011-04-27 10:05:43 +00:00
|
|
|
|| f->d->underline) {
|
|
|
|
underlineStyle = QTextCharFormat::SingleUnderline;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compat
|
|
|
|
if (underlineStyle == QTextCharFormat::SingleUnderline)
|
|
|
|
flags |= QTextItem::Underline;
|
|
|
|
|
2011-07-08 15:40:43 +00:00
|
|
|
if (f->d->overline || charFormat.fontOverline())
|
2011-04-27 10:05:43 +00:00
|
|
|
flags |= QTextItem::Overline;
|
2011-07-08 15:40:43 +00:00
|
|
|
if (f->d->strikeOut || charFormat.fontStrikeOut())
|
2011-04-27 10:05:43 +00:00
|
|
|
flags |= QTextItem::StrikeOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextItemInt QTextItemInt::midItem(QFontEngine *fontEngine, int firstGlyphIndex, int numGlyphs) const
|
|
|
|
{
|
|
|
|
QTextItemInt ti = *this;
|
|
|
|
const int end = firstGlyphIndex + numGlyphs;
|
|
|
|
ti.glyphs = glyphs.mid(firstGlyphIndex, numGlyphs);
|
|
|
|
ti.fontEngine = fontEngine;
|
|
|
|
|
|
|
|
if (logClusters && chars) {
|
|
|
|
const int logClusterOffset = logClusters[0];
|
|
|
|
while (logClusters[ti.chars - chars] - logClusterOffset < firstGlyphIndex)
|
|
|
|
++ti.chars;
|
|
|
|
|
|
|
|
ti.logClusters += (ti.chars - chars);
|
|
|
|
|
|
|
|
ti.num_chars = 0;
|
|
|
|
int char_start = ti.chars - chars;
|
|
|
|
while (char_start + ti.num_chars < num_chars && ti.logClusters[ti.num_chars] - logClusterOffset < end)
|
|
|
|
++ti.num_chars;
|
|
|
|
}
|
|
|
|
return ti;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTransform qt_true_matrix(qreal w, qreal h, QTransform x)
|
|
|
|
{
|
|
|
|
QRectF rect = x.mapRect(QRectF(0, 0, w, h));
|
|
|
|
return x * QTransform::fromTranslate(-rect.x(), -rect.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
glyph_metrics_t glyph_metrics_t::transformed(const QTransform &matrix) const
|
|
|
|
{
|
|
|
|
if (matrix.type() < QTransform::TxTranslate)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
glyph_metrics_t m = *this;
|
|
|
|
|
|
|
|
qreal w = width.toReal();
|
|
|
|
qreal h = height.toReal();
|
|
|
|
QTransform xform = qt_true_matrix(w, h, matrix);
|
|
|
|
|
|
|
|
QRectF rect(0, 0, w, h);
|
|
|
|
rect = xform.mapRect(rect);
|
|
|
|
m.width = QFixed::fromReal(rect.width());
|
|
|
|
m.height = QFixed::fromReal(rect.height());
|
|
|
|
|
|
|
|
QLineF l = xform.map(QLineF(x.toReal(), y.toReal(), xoff.toReal(), yoff.toReal()));
|
|
|
|
|
|
|
|
m.x = QFixed::fromReal(l.x1());
|
|
|
|
m.y = QFixed::fromReal(l.y1());
|
|
|
|
|
|
|
|
// The offset is relative to the baseline which is why we use dx/dy of the line
|
|
|
|
m.xoff = QFixed::fromReal(l.dx());
|
|
|
|
m.yoff = QFixed::fromReal(l.dy());
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2010-12-15 14:11:45 +00:00
|
|
|
QTextLineItemIterator::QTextLineItemIterator(QTextEngine *_eng, int _lineNum, const QPointF &pos,
|
|
|
|
const QTextLayout::FormatRange *_selection)
|
|
|
|
: eng(_eng),
|
|
|
|
line(eng->lines[_lineNum]),
|
|
|
|
si(0),
|
|
|
|
lineNum(_lineNum),
|
|
|
|
lineEnd(line.from + line.length),
|
|
|
|
firstItem(eng->findItem(line.from)),
|
|
|
|
lastItem(eng->findItem(lineEnd - 1)),
|
|
|
|
nItems((firstItem >= 0 && lastItem >= firstItem)? (lastItem-firstItem+1) : 0),
|
|
|
|
logicalItem(-1),
|
|
|
|
item(-1),
|
|
|
|
visualOrder(nItems),
|
|
|
|
levels(nItems),
|
|
|
|
selection(_selection)
|
|
|
|
{
|
|
|
|
pos_x = x = QFixed::fromReal(pos.x());
|
|
|
|
|
|
|
|
x += line.x;
|
|
|
|
|
|
|
|
x += eng->alignLine(line);
|
|
|
|
|
|
|
|
for (int i = 0; i < nItems; ++i)
|
|
|
|
levels[i] = eng->layoutData->items[i+firstItem].analysis.bidiLevel;
|
|
|
|
QTextEngine::bidiReorder(nItems, levels.data(), visualOrder.data());
|
|
|
|
|
|
|
|
eng->shapeLine(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
QScriptItem &QTextLineItemIterator::next()
|
|
|
|
{
|
|
|
|
x += itemWidth;
|
|
|
|
|
|
|
|
++logicalItem;
|
|
|
|
item = visualOrder[logicalItem] + firstItem;
|
|
|
|
itemLength = eng->length(item);
|
|
|
|
si = &eng->layoutData->items[item];
|
|
|
|
if (!si->num_glyphs)
|
|
|
|
eng->shape(item);
|
|
|
|
|
|
|
|
if (si->analysis.flags >= QScriptAnalysis::TabOrObject) {
|
|
|
|
itemWidth = si->width;
|
|
|
|
return *si;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short *logClusters = eng->logClusters(si);
|
|
|
|
QGlyphLayout glyphs = eng->shapedGlyphs(si);
|
|
|
|
|
|
|
|
itemStart = qMax(line.from, si->position);
|
|
|
|
glyphsStart = logClusters[itemStart - si->position];
|
|
|
|
if (lineEnd < si->position + itemLength) {
|
|
|
|
itemEnd = lineEnd;
|
|
|
|
glyphsEnd = logClusters[itemEnd-si->position];
|
|
|
|
} else {
|
|
|
|
itemEnd = si->position + itemLength;
|
|
|
|
glyphsEnd = si->num_glyphs;
|
|
|
|
}
|
|
|
|
// show soft-hyphen at line-break
|
|
|
|
if (si->position + itemLength >= lineEnd
|
2012-06-29 06:07:34 +00:00
|
|
|
&& eng->layoutData->string.at(lineEnd - 1).unicode() == QChar::SoftHyphen)
|
2010-12-15 14:11:45 +00:00
|
|
|
glyphs.attributes[glyphsEnd - 1].dontPrint = false;
|
|
|
|
|
|
|
|
itemWidth = 0;
|
|
|
|
for (int g = glyphsStart; g < glyphsEnd; ++g)
|
|
|
|
itemWidth += glyphs.effectiveAdvance(g);
|
|
|
|
|
|
|
|
return *si;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QTextLineItemIterator::getSelectionBounds(QFixed *selectionX, QFixed *selectionWidth) const
|
|
|
|
{
|
|
|
|
*selectionX = *selectionWidth = 0;
|
|
|
|
|
|
|
|
if (!selection)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (si->analysis.flags >= QScriptAnalysis::TabOrObject) {
|
|
|
|
if (si->position >= selection->start + selection->length
|
|
|
|
|| si->position + itemLength <= selection->start)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*selectionX = x;
|
|
|
|
*selectionWidth = itemWidth;
|
|
|
|
} else {
|
|
|
|
unsigned short *logClusters = eng->logClusters(si);
|
|
|
|
QGlyphLayout glyphs = eng->shapedGlyphs(si);
|
|
|
|
|
|
|
|
int from = qMax(itemStart, selection->start) - si->position;
|
|
|
|
int to = qMin(itemEnd, selection->start + selection->length) - si->position;
|
|
|
|
if (from >= to)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int start_glyph = logClusters[from];
|
|
|
|
int end_glyph = (to == eng->length(item)) ? si->num_glyphs : logClusters[to];
|
|
|
|
QFixed soff;
|
|
|
|
QFixed swidth;
|
|
|
|
if (si->analysis.bidiLevel %2) {
|
|
|
|
for (int g = glyphsEnd - 1; g >= end_glyph; --g)
|
|
|
|
soff += glyphs.effectiveAdvance(g);
|
|
|
|
for (int g = end_glyph - 1; g >= start_glyph; --g)
|
|
|
|
swidth += glyphs.effectiveAdvance(g);
|
|
|
|
} else {
|
|
|
|
for (int g = glyphsStart; g < start_glyph; ++g)
|
|
|
|
soff += glyphs.effectiveAdvance(g);
|
|
|
|
for (int g = start_glyph; g < end_glyph; ++g)
|
|
|
|
swidth += glyphs.effectiveAdvance(g);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the starting character is in the middle of a ligature,
|
|
|
|
// selection should only contain the right part of that ligature
|
|
|
|
// glyph, so we need to get the width of the left part here and
|
|
|
|
// add it to *selectionX
|
|
|
|
QFixed leftOffsetInLigature = eng->offsetInLigature(si, from, to, start_glyph);
|
|
|
|
*selectionX = x + soff + leftOffsetInLigature;
|
|
|
|
*selectionWidth = swidth - leftOffsetInLigature;
|
|
|
|
// If the ending character is also part of a ligature, swidth does
|
|
|
|
// not contain that part yet, we also need to find out the width of
|
|
|
|
// that left part
|
|
|
|
*selectionWidth += eng->offsetInLigature(si, to, eng->length(item), end_glyph);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QT_END_NAMESPACE
|