src/opengl/gl2paintengineex/qtriangulatingstroker_p.h
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    41 
    41 
    42 #ifndef QTRIANGULATINGSTROKER_P_H
    42 #ifndef QTRIANGULATINGSTROKER_P_H
    43 #define QTRIANGULATINGSTROKER_P_H
    43 #define QTRIANGULATINGSTROKER_P_H
    44 
    44 
    45 #include <private/qdatabuffer_p.h>
    45 #include <private/qdatabuffer_p.h>
       
    46 #include <qvarlengtharray.h>
    46 #include <private/qvectorpath_p.h>
    47 #include <private/qvectorpath_p.h>
    47 #include <private/qbezier_p.h>
    48 #include <private/qbezier_p.h>
    48 #include <private/qnumeric_p.h>
    49 #include <private/qnumeric_p.h>
    49 #include <private/qmath_p.h>
    50 #include <private/qmath_p.h>
    50 
    51 
       
    52 QT_BEGIN_NAMESPACE
    51 
    53 
    52 class QTriangulatingStroker
    54 class QTriangulatingStroker
    53 {
    55 {
    54 public:
    56 public:
    55     void process(const QVectorPath &path, const QPen &pen);
    57     void process(const QVectorPath &path, const QPen &pen);
    59 
    61 
    60     inline void setInvScale(qreal invScale) { m_inv_scale = invScale; }
    62     inline void setInvScale(qreal invScale) { m_inv_scale = invScale; }
    61 
    63 
    62 private:
    64 private:
    63     inline void emitLineSegment(float x, float y, float nx, float ny);
    65     inline void emitLineSegment(float x, float y, float nx, float ny);
    64     inline void moveTo(const qreal *pts);
    66     void moveTo(const qreal *pts);
    65     inline void lineTo(const qreal *pts);
    67     inline void lineTo(const qreal *pts);
    66     void cubicTo(const qreal *pts);
    68     void cubicTo(const qreal *pts);
    67     inline void join(const qreal *pts);
    69     void join(const qreal *pts);
    68     inline void normalVector(float x1, float y1, float x2, float y2, float *nx, float *ny);
    70     inline void normalVector(float x1, float y1, float x2, float y2, float *nx, float *ny);
    69     inline void endCap(const qreal *pts);
    71     void endCap(const qreal *pts);
    70     inline void arc(float x, float y);
    72     void arcPoints(float cx, float cy, float fromX, float fromY, float toX, float toY, QVarLengthArray<float> &points);
    71     void endCapOrJoinClosed(const qreal *start, const qreal *cur, bool implicitClose, bool endsAtStart);
    73     void endCapOrJoinClosed(const qreal *start, const qreal *cur, bool implicitClose, bool endsAtStart);
    72 
    74 
    73 
    75 
    74     QDataBuffer<float> m_vertices;
    76     QDataBuffer<float> m_vertices;
    75 
    77 
   113     QDataBuffer<QPainterPath::ElementType> m_types;
   115     QDataBuffer<QPainterPath::ElementType> m_types;
   114     QDashStroker m_dash_stroker;
   116     QDashStroker m_dash_stroker;
   115     qreal m_inv_scale;
   117     qreal m_inv_scale;
   116 };
   118 };
   117 
   119 
   118 
       
   119 
       
   120 
       
   121 
       
   122 inline void QTriangulatingStroker::normalVector(float x1, float y1, float x2, float y2,
   120 inline void QTriangulatingStroker::normalVector(float x1, float y1, float x2, float y2,
   123                                                 float *nx, float *ny)
   121                                                 float *nx, float *ny)
   124 {
   122 {
   125     float dx = x2 - x1;
   123     float dx = x2 - x1;
   126     float dy = y2 - y1;
   124     float dy = y2 - y1;
   127     float pw = m_width / sqrt(dx*dx + dy*dy);
   125 
       
   126     float pw;
       
   127 
       
   128     if (dx == 0)
       
   129         pw = m_width / qAbs(dy);
       
   130     else if (dy == 0)
       
   131         pw = m_width / qAbs(dx);
       
   132     else
       
   133         pw = m_width / sqrt(dx*dx + dy*dy);
       
   134 
   128     *nx = -dy * pw;
   135     *nx = -dy * pw;
   129     *ny = dx * pw;
   136     *ny = dx * pw;
   130 }
   137 }
   131 
       
   132 
       
   133 
   138 
   134 inline void QTriangulatingStroker::emitLineSegment(float x, float y, float vx, float vy)
   139 inline void QTriangulatingStroker::emitLineSegment(float x, float y, float vx, float vy)
   135 {
   140 {
   136     m_vertices.add(x + vx);
   141     m_vertices.add(x + vx);
   137     m_vertices.add(y + vy);
   142     m_vertices.add(y + vy);
   138     m_vertices.add(x - vx);
   143     m_vertices.add(x - vx);
   139     m_vertices.add(y - vy);
   144     m_vertices.add(y - vy);
   140 }
   145 }
   141 
   146 
   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)
   147 void QTriangulatingStroker::lineTo(const qreal *pts)
   244 {
   148 {
   245     emitLineSegment(pts[0], pts[1], m_nvx, m_nvy);
   149     emitLineSegment(pts[0], pts[1], m_nvx, m_nvy);
   246     m_cx = pts[0];
   150     m_cx = pts[0];
   247     m_cy = pts[1];
   151     m_cy = pts[1];
   248 }
   152 }
   249 
   153 
   250 
   154 QT_END_NAMESPACE
   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 
   155 
   299 #endif
   156 #endif