1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/* This file is part of the KDE project
* Copyright (C) 2006-2007, 2010 Thomas Zander <zander@kde.org>
* Copyright (C) 2010 Ko Gmbh <cbo@kogmbh.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KoTextLayoutObstruction.h"
#include <KoShapeStrokeModel.h>
#include <KoShapeShadow.h>
#include <KoShapeGroup.h>
#include <KoClipPath.h>
#include <KoInsets.h>
#include <QTransform>
#include <QPainterPath>
KoTextLayoutObstruction::KoTextLayoutObstruction(KoShape *shape, const QTransform &matrix)
: m_side(None),
m_polygon(QPolygonF()),
m_line(QRectF()),
m_shape(shape),
m_runAroundThreshold(0)
{
qreal borderHalfWidth;
QPainterPath path = decoratedOutline(m_shape, borderHalfWidth);
//TODO check if path is convex. otherwise do triangulation and create more convex obstructions
init(matrix, path, shape->textRunAroundDistanceLeft(), shape->textRunAroundDistanceTop(), shape->textRunAroundDistanceRight(), shape->textRunAroundDistanceBottom(), borderHalfWidth);
if (shape->textRunAroundSide() == KoShape::NoRunAround) {
// make the shape take the full width of the text area
m_side = Empty;
} else if (shape->textRunAroundSide() == KoShape::RunThrough) {
m_distanceLeft = 0;
m_distanceTop = 0;
m_distanceRight = 0;
m_distanceBottom = 0;
// We don't exist.
return;
} else if (shape->textRunAroundSide() == KoShape::LeftRunAroundSide) {
m_side = Left;
} else if (shape->textRunAroundSide() == KoShape::RightRunAroundSide) {
m_side = Right;
} else if (shape->textRunAroundSide() == KoShape::BothRunAroundSide) {
m_side = Both;
} else if (shape->textRunAroundSide() == KoShape::BiggestRunAroundSide) {
m_side = Bigger;
} else if (shape->textRunAroundSide() == KoShape::EnoughRunAroundSide) {
m_side = Enough;
m_runAroundThreshold = shape->textRunAroundThreshold();
}
}
KoTextLayoutObstruction::KoTextLayoutObstruction(const QRectF &rect, bool rtl)
: m_side(None),
m_polygon(QPolygonF()),
m_line(QRectF()),
m_shape(0),
m_runAroundThreshold(0)
{
qreal borderHalfWidth = 0;
qreal textRunAroundDistance = 1;
QPainterPath path;
path.addRect(rect);
init(QTransform(), path, textRunAroundDistance, 0.0, textRunAroundDistance, 0.0, borderHalfWidth);
if (rtl) {
m_side = Right;
} else {
m_side = Left;
}
}
QPainterPath KoTextLayoutObstruction::decoratedOutline(const KoShape *shape, qreal &borderHalfWidth) const
{
const KoShapeGroup *shapeGroup = dynamic_cast<const KoShapeGroup *>(shape);
if (shapeGroup) {
QPainterPath groupPath;
foreach (const KoShape *child, shapeGroup->shapes()) {
groupPath += decoratedOutline(child, borderHalfWidth);
}
return groupPath;
}
QPainterPath path;
if (shape->textRunAroundContour() != KoShape::ContourBox) {
KoClipPath *clipPath = shape->clipPath();
if (clipPath) {
path = clipPath->pathForSize(shape->size());
} else {
path = shape->outline();
}
} else {
path.addRect(shape->outlineRect());
}
QRectF bb = shape->outlineRect();
borderHalfWidth = 0;
if (shape->stroke()) {
KoInsets insets;
shape->stroke()->strokeInsets(shape, insets);
/*
bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
path = QPainterPath();
path.addRect(bb);
*/
borderHalfWidth = qMax(qMax(insets.left, insets.top),qMax(insets.right, insets.bottom));
}
if (shape->shadow() && shape->shadow()->isVisible()) {
QTransform transform = shape->absoluteTransformation(0);
bb = transform.mapRect(bb);
KoInsets insets;
shape->shadow()->insets(insets);
bb.adjust(-insets.left, -insets.top, insets.right, insets.bottom);
path = QPainterPath();
path.addRect(bb);
path = transform.inverted().map(path);
}
return path;
}
void KoTextLayoutObstruction::init(const QTransform &matrix, const QPainterPath &obstruction, qreal distanceLeft, qreal distanceTop, qreal distanceRight, qreal distanceBottom, qreal borderHalfWidth)
{
m_distanceLeft = distanceLeft;
m_distanceTop = distanceTop;
m_distanceRight = distanceRight;
m_distanceBottom = distanceBottom;
QPainterPath path = matrix.map(obstruction);
distanceLeft += borderHalfWidth;
distanceTop += borderHalfWidth;
distanceRight += borderHalfWidth;
distanceBottom += borderHalfWidth;
qreal extraWidth = distanceLeft + distanceRight;
qreal extraHeight = distanceTop + distanceBottom;
if (extraWidth != 0.0 || extraHeight != 0.0) {
// Let's extend the outline with at least the border half width in all directions.
// However since the distance can be express in 4 directions and QPainterPathStroker only
// handles a penWidth we do some tricks to get it working.
//
// Explanation in one dimension only: we sum the distances top and below and use that as the
// penWidth. afterwards we translate the result so it is distributed correctly by top and bottom
// Now by doing that we would also implicitly set the left+right size of the pen which is no good,
// so in order to set that to a minimal value (we choose 1, as 0 would give division by 0) we do
// the following:. We scale the original path by sumX, stroke the path with penwidth=sumY, then
// scale it back. Effectively we have now stroked with a pen sized 1 x sumY.
//
// The math to do both x an y in one go becomes a little more complex, but only a little.
extraWidth = qMax(qreal(0.1), extraWidth);
extraHeight = qMax(qreal(0.1), extraHeight);
QPainterPathStroker stroker;
stroker.setWidth(extraWidth);
stroker.setJoinStyle(Qt::MiterJoin);
stroker.setCapStyle(Qt::SquareCap);
QPainterPath bigPath = stroker.createStroke(QTransform().scale(1.0, extraWidth / extraHeight).map(path));
bigPath = QTransform().scale(1.0, extraHeight / extraWidth). map(bigPath);
path += bigPath.translated(extraWidth / 2 - distanceLeft, extraHeight / 2 - distanceTop);
}
m_bounds = path.boundingRect();
// Now we need to change the path into a polygon for easier handling later on
m_polygon = path.toFillPolygon();
QPointF &prev(m_polygon.first());
// There exists a problem on msvc with for(each) and QVector<QPointF>
for (int i = 0; i < m_polygon.count(); ++i) {
const QPointF &vtx(m_polygon[i]);
if (vtx.x() == prev.x() && vtx.y() == prev.y())
continue;
QLineF line;
if (prev.y() < vtx.y()) // Make sure the vector lines all point downwards.
line = QLineF(prev, vtx);
else
line = QLineF(vtx, prev);
m_edges.insert(line.y1(), line);
prev = vtx;
}
}
qreal KoTextLayoutObstruction::xAtY(const QLineF &line, qreal y)
{
if (line.dx() == 0)
return line.x1();
return line.x1() + (y - line.y1()) / line.dy() * line.dx();
}
void KoTextLayoutObstruction::changeMatrix(const QTransform &matrix)
{
m_edges.clear();
qreal borderHalfWidth;
QPainterPath path = decoratedOutline(m_shape, borderHalfWidth);
init(matrix, path, m_distanceLeft, m_distanceTop, m_distanceRight, m_distanceBottom, borderHalfWidth);
}
QRectF KoTextLayoutObstruction::cropToLine(const QRectF &lineRect)
{
if (m_bounds.intersects(lineRect)) {
m_line = lineRect;
bool untilFirst = true;
//check inner points
// There exists a problem on msvc with for(each) and QVector<QPointF>
for (int i = 0; i < m_polygon.count(); ++i) {
const QPointF point(m_polygon[i]);
if (lineRect.contains(point)) {
if (untilFirst) {
m_line.setLeft(point.x());
m_line.setRight(point.x());
untilFirst = false;
} else {
if (point.x() < m_line.left()) {
m_line.setLeft(point.x());
} else if (point.x() > m_line.right()) {
m_line.setRight(point.x());
}
}
}
}
//check edges
qreal points[2] = { lineRect.top(), lineRect.bottom() };
for (int i = 0; i < 2; i++) {
const qreal y = points[i];
QMap<qreal, QLineF>::const_iterator iter = m_edges.constBegin();
for (;iter != m_edges.constEnd(); ++iter) {
QLineF line = iter.value();
if (line.y2() < y) // not a section that will intersect with ou Y yet
continue;
if (line.y1() > y) // section is below our Y, so abort loop
//break;
continue;
if (qAbs(line.dy()) < 1E-10) // horizontal lines don't concern us.
continue;
qreal intersect = xAtY(iter.value(), y);
if (untilFirst) {
m_line.setLeft(intersect);
m_line.setRight(intersect);
untilFirst = false;
} else {
if (intersect < m_line.left()) {
m_line.setLeft(intersect);
} else if (intersect > m_line.right()) {
m_line.setRight(intersect);
}
}
}
}
} else {
m_line = QRectF();
}
return m_line;
}
QRectF KoTextLayoutObstruction::getLeftLinePart(const QRectF &lineRect) const
{
QRectF leftLinePart = lineRect;
leftLinePart.setRight(m_line.left());
return leftLinePart;
}
QRectF KoTextLayoutObstruction::getRightLinePart(const QRectF &lineRect) const
{
QRectF rightLinePart = lineRect;
if (m_line.right() > rightLinePart.left()) {
rightLinePart.setLeft(m_line.right());
}
return rightLinePart;
}
bool KoTextLayoutObstruction::textOnLeft() const
{
return m_side == Left;
}
bool KoTextLayoutObstruction::textOnRight() const
{
return m_side == Right;
}
bool KoTextLayoutObstruction::textOnBiggerSide() const
{
return m_side == Bigger;
}
bool KoTextLayoutObstruction::textOnEnoughSides() const
{
return m_side == Enough;
}
qreal KoTextLayoutObstruction::runAroundThreshold() const
{
return m_runAroundThreshold;
}
bool KoTextLayoutObstruction::noTextAround() const
{
return m_side == Empty;
}
bool KoTextLayoutObstruction::compareRectLeft(KoTextLayoutObstruction *o1, KoTextLayoutObstruction *o2)
{
return o1->m_line.left() < o2->m_line.left();
}
|