1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is IBM Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is IBM Corporation.
18 : * Portions created by the Initial Developer are Copyright (C) 2007
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include "gfxPath.h"
38 : #include "gfxPoint.h"
39 :
40 : #include "cairo.h"
41 :
42 0 : gfxPath::gfxPath(cairo_path_t* aPath) : mPath(aPath)
43 : {
44 0 : }
45 :
46 0 : gfxPath::~gfxPath()
47 : {
48 0 : cairo_path_destroy(mPath);
49 0 : }
50 :
51 0 : gfxFlattenedPath::gfxFlattenedPath(cairo_path_t* aPath) : gfxPath(aPath)
52 : {
53 0 : }
54 :
55 0 : gfxFlattenedPath::~gfxFlattenedPath()
56 : {
57 0 : }
58 :
59 : static gfxFloat
60 0 : CalcSubLengthAndAdvance(cairo_path_data_t *aData,
61 : gfxPoint &aPathStart, gfxPoint &aCurrent)
62 : {
63 0 : float sublength = 0;
64 :
65 0 : switch (aData->header.type) {
66 : case CAIRO_PATH_MOVE_TO:
67 : {
68 0 : aCurrent = aPathStart = gfxPoint(aData[1].point.x,
69 0 : aData[1].point.y);
70 0 : break;
71 : }
72 : case CAIRO_PATH_LINE_TO:
73 : {
74 : gfxPoint diff =
75 0 : gfxPoint(aData[1].point.x, aData[1].point.y) - aCurrent;
76 0 : sublength = sqrt(diff.x * diff.x + diff.y * diff.y);
77 0 : aCurrent = gfxPoint(aData[1].point.x, aData[1].point.y);
78 0 : break;
79 : }
80 : case CAIRO_PATH_CURVE_TO:
81 : /* should never happen with a flattened path */
82 0 : NS_WARNING("curve_to in flattened path");
83 0 : break;
84 : case CAIRO_PATH_CLOSE_PATH:
85 : {
86 0 : gfxPoint diff = aPathStart - aCurrent;
87 0 : sublength = sqrt(diff.x * diff.x + diff.y * diff.y);
88 0 : aCurrent = aPathStart;
89 0 : break;
90 : }
91 : }
92 0 : return sublength;
93 : }
94 :
95 : gfxFloat
96 0 : gfxFlattenedPath::GetLength()
97 : {
98 0 : gfxPoint start(0, 0); // start of current subpath
99 0 : gfxPoint current(0, 0); // current point
100 0 : gfxFloat length = 0; // current summed length
101 :
102 0 : for (PRInt32 i = 0;
103 : i < mPath->num_data;
104 0 : i += mPath->data[i].header.length) {
105 0 : length += CalcSubLengthAndAdvance(&mPath->data[i], start, current);
106 : }
107 0 : return length;
108 : }
109 :
110 : gfxPoint
111 0 : gfxFlattenedPath::FindPoint(gfxPoint aOffset, gfxFloat *aAngle)
112 : {
113 0 : gfxPoint start(0, 0); // start of current subpath
114 0 : gfxPoint current(0, 0); // current point
115 0 : gfxFloat length = 0; // current summed length
116 :
117 0 : for (PRInt32 i = 0;
118 : i < mPath->num_data;
119 0 : i += mPath->data[i].header.length) {
120 0 : gfxPoint prev = current;
121 :
122 : gfxFloat sublength = CalcSubLengthAndAdvance(&mPath->data[i],
123 0 : start, current);
124 :
125 0 : gfxPoint diff = current - prev;
126 0 : if (aAngle)
127 0 : *aAngle = atan2(diff.y, diff.x);
128 :
129 0 : if (sublength != 0 && length + sublength >= aOffset.x) {
130 0 : gfxFloat ratio = (aOffset.x - length) / sublength;
131 : gfxFloat normalization =
132 0 : 1.0 / sqrt(diff.x * diff.x + diff.y * diff.y);
133 :
134 0 : return prev * (1.0f - ratio) + current * ratio +
135 0 : gfxPoint(-diff.y, diff.x) * aOffset.y * normalization;
136 : }
137 0 : length += sublength;
138 : }
139 :
140 : // requested offset is past the end of the path - return last point
141 0 : return current;
142 : }
|