src/opengl/gl2paintengineex/qtriangulatingstroker_p.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef QTRIANGULATINGSTROKER_P_H
       
    43 #define QTRIANGULATINGSTROKER_P_H
       
    44 
       
    45 #include <private/qdatabuffer_p.h>
       
    46 #include <private/qvectorpath_p.h>
       
    47 #include <private/qbezier_p.h>
       
    48 #include <private/qnumeric_p.h>
       
    49 #include <private/qmath_p.h>
       
    50 
       
    51 
       
    52 class QTriangulatingStroker
       
    53 {
       
    54 public:
       
    55     void process(const QVectorPath &path, const QPen &pen);
       
    56 
       
    57     inline int vertexCount() const { return m_vertices.size(); }
       
    58     inline const float *vertices() const { return m_vertices.data(); }
       
    59 
       
    60     inline void setInvScale(qreal invScale) { m_inv_scale = invScale; }
       
    61 
       
    62 private:
       
    63     inline void emitLineSegment(float x, float y, float nx, float ny);
       
    64     inline void moveTo(const qreal *pts);
       
    65     inline void lineTo(const qreal *pts);
       
    66     void cubicTo(const qreal *pts);
       
    67     inline void join(const qreal *pts);
       
    68     inline void normalVector(float x1, float y1, float x2, float y2, float *nx, float *ny);
       
    69     inline void endCap(const qreal *pts);
       
    70     inline void arc(float x, float y);
       
    71     void endCapOrJoinClosed(const qreal *start, const qreal *cur, bool implicitClose, bool endsAtStart);
       
    72 
       
    73 
       
    74     QDataBuffer<float> m_vertices;
       
    75 
       
    76     float m_cx, m_cy;           // current points
       
    77     float m_nvx, m_nvy;         // normal vector...
       
    78     float m_width;
       
    79     qreal m_miter_limit;
       
    80 
       
    81     int m_roundness;            // Number of line segments in a round join
       
    82     qreal m_sin_theta;          // sin(m_roundness / 360);
       
    83     qreal m_cos_theta;          // cos(m_roundness / 360);
       
    84     qreal m_inv_scale;
       
    85     float m_curvyness_mul;
       
    86     float m_curvyness_add;
       
    87 
       
    88     Qt::PenJoinStyle m_join_style;
       
    89     Qt::PenCapStyle m_cap_style;
       
    90 };
       
    91 
       
    92 class QDashedStrokeProcessor
       
    93 {
       
    94 public:
       
    95     QDashedStrokeProcessor();
       
    96 
       
    97     void process(const QVectorPath &path, const QPen &pen);
       
    98 
       
    99     inline void addElement(QPainterPath::ElementType type, qreal x, qreal y) {
       
   100         m_points.add(x);
       
   101         m_points.add(y);
       
   102         m_types.add(type);
       
   103     }
       
   104 
       
   105     inline int elementCount() const { return m_types.size(); }
       
   106     inline qreal *points() const { return m_points.data(); }
       
   107     inline QPainterPath::ElementType *elementTypes() const { return m_types.data(); }
       
   108 
       
   109     inline void setInvScale(qreal invScale) { m_inv_scale = invScale; }
       
   110 
       
   111 private:
       
   112     QDataBuffer<qreal> m_points;
       
   113     QDataBuffer<QPainterPath::ElementType> m_types;
       
   114     QDashStroker m_dash_stroker;
       
   115     qreal m_inv_scale;
       
   116 };
       
   117 
       
   118 
       
   119 
       
   120 
       
   121 
       
   122 inline void QTriangulatingStroker::normalVector(float x1, float y1, float x2, float y2,
       
   123                                                 float *nx, float *ny)
       
   124 {
       
   125     float dx = x2 - x1;
       
   126     float dy = y2 - y1;
       
   127     float pw = m_width / sqrt(dx*dx + dy*dy);
       
   128     *nx = -dy * pw;
       
   129     *ny = dx * pw;
       
   130 }
       
   131 
       
   132 
       
   133 
       
   134 inline void QTriangulatingStroker::emitLineSegment(float x, float y, float vx, float vy)
       
   135 {
       
   136     m_vertices.add(x + vx);
       
   137     m_vertices.add(y + vy);
       
   138     m_vertices.add(x - vx);
       
   139     m_vertices.add(y - vy);
       
   140 }
       
   141 
       
   142 
       
   143 
       
   144 // We draw a full circle for any round join or round cap which is a
       
   145 // bit of overkill...
       
   146 inline void QTriangulatingStroker::arc(float x, float y)
       
   147 {
       
   148     float dx = m_width;
       
   149     float dy = 0;
       
   150     for (int i=0; i<=m_roundness; ++i) {
       
   151         float tmpx = dx * m_cos_theta - dy * m_sin_theta;
       
   152         float tmpy = dx * m_sin_theta + dy * m_cos_theta;
       
   153         dx = tmpx;
       
   154         dy = tmpy;
       
   155         emitLineSegment(x, y, dx, dy);
       
   156     }
       
   157 }
       
   158 
       
   159 
       
   160 
       
   161 inline void QTriangulatingStroker::endCap(const qreal *pts)
       
   162 {
       
   163     switch (m_cap_style) {
       
   164     case Qt::FlatCap:
       
   165         break;
       
   166     case Qt::SquareCap: {
       
   167         float dx = m_cx - *(pts - 2);
       
   168         float dy = m_cy - *(pts - 1);
       
   169 
       
   170         float len = m_width / sqrt(dx * dx + dy * dy);
       
   171         dx = dx * len;
       
   172         dy = dy * len;
       
   173 
       
   174         emitLineSegment(m_cx + dx, m_cy + dy, m_nvx, m_nvy);
       
   175         break; }
       
   176     case Qt::RoundCap:
       
   177         arc(m_cx, m_cy);
       
   178         break;
       
   179     default: break; // to shut gcc up...
       
   180     }
       
   181 
       
   182     int count = m_vertices.size();
       
   183     m_vertices.add(m_vertices.at(count-2));
       
   184     m_vertices.add(m_vertices.at(count-1));
       
   185 }
       
   186 
       
   187 
       
   188 void QTriangulatingStroker::moveTo(const qreal *pts)
       
   189 {
       
   190     m_cx = pts[0];
       
   191     m_cy = pts[1];
       
   192 
       
   193     float x2 = pts[2];
       
   194     float y2 = pts[3];
       
   195     normalVector(m_cx, m_cy, x2, y2, &m_nvx, &m_nvy);
       
   196 
       
   197 
       
   198     // To acheive jumps we insert zero-area tringles. This is done by
       
   199     // adding two identical points in both the end of previous strip
       
   200     // and beginning of next strip
       
   201     bool invisibleJump = m_vertices.size();
       
   202 
       
   203     switch (m_cap_style) {
       
   204     case Qt::FlatCap:
       
   205         if (invisibleJump) {
       
   206             m_vertices.add(m_cx + m_nvx);
       
   207             m_vertices.add(m_cy + m_nvy);
       
   208         }
       
   209         break;
       
   210     case Qt::SquareCap: {
       
   211         float dx = x2 - m_cx;
       
   212         float dy = y2 - m_cy;
       
   213         float len = m_width / sqrt(dx * dx + dy * dy);
       
   214         dx = dx * len;
       
   215         dy = dy * len;
       
   216         float sx = m_cx - dx;
       
   217         float sy = m_cy - dy;
       
   218         if (invisibleJump) {
       
   219             m_vertices.add(sx + m_nvx);
       
   220             m_vertices.add(sy + m_nvy);
       
   221         }
       
   222         emitLineSegment(sx, sy, m_nvx, m_nvy);
       
   223         break; }
       
   224     case Qt::RoundCap:
       
   225         if (invisibleJump) {
       
   226             m_vertices.add(m_cx + m_nvx);
       
   227             m_vertices.add(m_cy + m_nvy);
       
   228         }
       
   229 
       
   230         // This emitLineSegment is not needed for the arc, but we need
       
   231         // to start where we put the invisibleJump vertex, otherwise
       
   232         // we'll have visible triangles between subpaths.
       
   233         emitLineSegment(m_cx, m_cy, m_nvx, m_nvy);
       
   234         arc(m_cx, m_cy);
       
   235         break;
       
   236     default: break; // ssssh gcc...
       
   237     }
       
   238     emitLineSegment(m_cx, m_cy, m_nvx, m_nvy);
       
   239 }
       
   240 
       
   241 
       
   242 
       
   243 void QTriangulatingStroker::lineTo(const qreal *pts)
       
   244 {
       
   245     emitLineSegment(pts[0], pts[1], m_nvx, m_nvy);
       
   246     m_cx = pts[0];
       
   247     m_cy = pts[1];
       
   248 }
       
   249 
       
   250 
       
   251 
       
   252 
       
   253 
       
   254 void QTriangulatingStroker::join(const qreal *pts)
       
   255 {
       
   256     // Creates a join to the next segment (m_cx, m_cy) -> (pts[0], pts[1])
       
   257     normalVector(m_cx, m_cy, pts[0], pts[1], &m_nvx, &m_nvy);
       
   258 
       
   259     switch (m_join_style) {
       
   260     case Qt::BevelJoin:
       
   261         break;
       
   262     case Qt::MiterJoin: {
       
   263         int p1 = m_vertices.size() - 6;
       
   264         int p2 = m_vertices.size() - 2;
       
   265         QLineF line(m_vertices.at(p1), m_vertices.at(p1+1),
       
   266                     m_vertices.at(p2), m_vertices.at(p2+1));
       
   267         QLineF nextLine(m_cx - m_nvx, m_cy - m_nvy,
       
   268                         pts[0] - m_nvx, pts[1] - m_nvy);
       
   269 
       
   270         QPointF isect;
       
   271         if (line.intersect(nextLine, &isect) != QLineF::NoIntersection
       
   272             && QLineF(line.p2(), isect).length() <= m_miter_limit) {
       
   273             // The intersection point mirrored over the m_cx, m_cy point
       
   274             m_vertices.add(m_cx - (isect.x() - m_cx));
       
   275             m_vertices.add(m_cy - (isect.y() - m_cy));
       
   276 
       
   277             // The intersection point
       
   278             m_vertices.add(isect.x());
       
   279             m_vertices.add(isect.y());
       
   280         }
       
   281         // else
       
   282         // Do a plain bevel join if the miter limit is exceeded or if
       
   283         // the lines are parallel. This is not what the raster
       
   284         // engine's stroker does, but it is both faster and similar to
       
   285         // what some other graphics API's do.
       
   286 
       
   287         break; }
       
   288     case Qt::RoundJoin:
       
   289         arc(m_cx, m_cy);
       
   290         break;
       
   291 
       
   292     default: break; // gcc warn--
       
   293     }
       
   294 
       
   295     emitLineSegment(m_cx, m_cy, m_nvx, m_nvy);
       
   296 }
       
   297 
       
   298 
       
   299 #endif