00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 var VML_TRUTH_TABLE = [ "f", "t"],
00024 VML_LINECAP_TABLE = [ "flat", "round", "square" ],
00025 VML_LINEJOIN_TABLE = [ "miter", "round", "bevel" ],
00026 VML_ELEMENT_TABLE = [ " m ", " l ", "qb", " c ", " x ", [" at ", " wa "]];
00027
00028 var _CGBitmapGraphicsContextCreate = CGBitmapGraphicsContextCreate;
00029
00030 function CGBitmapGraphicsContextCreate()
00031 {
00032
00033 document.namespaces.add("cg_vml_", "urn:schemas-microsoft-com:vml");
00034 document.createStyleSheet().cssText = "cg_vml_\\:*{behavior:url(#default#VML)}";
00035
00036 CGBitmapGraphicsContextCreate = _CGBitmapGraphicsContextCreate;
00037
00038 return _CGBitmapGraphicsContextCreate();
00039 }
00040
00041
00042 function CGContextClearRect(aContext, aRect)
00043 {
00044 if (aContext.buffer != nil)
00045 aContext.buffer = "";
00046 else
00047 aContext.DOMElement.innerHTML = "";
00048
00049 aContext.path = NULL;
00050 }
00051
00052 var W = 10.0,
00053 H = 10.0,
00054 Z = 10.0,
00055 Z_2 = Z / 2.0;
00056
00057 #define COORD(aCoordinate) (ROUND(Z * (aCoordinate) - Z_2))
00058
00059 function CGContextDrawImage(aContext, aRect, anImage)
00060 {
00061 var string = "";
00062
00063 if (anImage.buffer != nil)
00064 string = anImage.buffer;
00065 else
00066 {
00067 var ctm = aContext.gState.CTM,
00068 origin = CGPointApplyAffineTransform(aRect.origin, ctm),
00069 similarity = ctm.a == ctm.d && ctm.b == -ctm.c,
00070 vml = ["<cg_vml_:group coordsize=\"1,1\" coordorigin=\"0,0\" style=\"width:1;height:1;position:absolute"];
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 {
00087 var transformedRect = CGRectApplyAffineTransform(aRect, ctm);
00088
00089 vml.push( ";padding:0 ", ROUND(_CGRectGetMaxX(transformedRect)), "px ", ROUND(_CGRectGetMaxY(transformedRect)),
00090 "px 0;filter:progid:DXImageTransform.Microsoft.Matrix(",
00091 "M11='", ctm.a, "',M12='", ctm.c, "',M21='", ctm.b, "',M22='", ctm.d, "',",
00092 "Dx='", ROUND(origin.x), "', Dy='", ROUND(origin.y), "', sizingmethod='clip');");
00093 }
00094
00095
00096
00097 vml.push( "\"><cg_vml_:image src=\"", anImage._image.src,
00098 "\" style=\"width:", _CGRectGetWidth(aRect), "px;height:", _CGRectGetHeight(aRect),
00099 "px;\"/></g_vml_:group>");
00100
00101 string = vml.join("");
00102 }
00103
00104 if (aContext.buffer != nil)
00105 aContext.buffer += string;
00106 else
00107 aContext.DOMElement.insertAdjacentHTML("BeforeEnd", string);
00108 }
00109
00110 function CGContextDrawPath(aContext, aMode)
00111 {
00112 if (!aContext || CGPathIsEmpty(aContext.path))
00113 return;
00114
00115 var elements = aContext.path.elements,
00116
00117 i = 0,
00118 count = aContext.path.count,
00119
00120 gState = aContext.gState,
00121 fill = (aMode == kCGPathFill || aMode == kCGPathFillStroke) ? 1 : 0,
00122 stroke = (aMode == kCGPathStroke || aMode == kCGPathFillStroke) ? 1 : 0,
00123 opacity = gState.alpha,
00124 vml = [ "<cg_vml_:shape",
00125 " fillcolor=\"", gState.fillStyle,
00126 "\" filled=\"", VML_TRUTH_TABLE[fill],
00127 "\" style=\"position:absolute;width:", W, ";height:", H,
00128 ";\" coordorigin=\"0 0\" coordsize=\"", Z * W, " ", Z * H,
00129 "\" stroked=\"", VML_TRUTH_TABLE[stroke],
00130 "\" strokeweight=\"", gState.lineWidth,
00131 "\" strokecolor=\"", gState.strokeStyle,
00132 "\" path=\""];
00133
00134 for (; i < count; ++i)
00135 {
00136 var element = elements[i],
00137 type = element.type;
00138
00139 switch(type)
00140 {
00141 case kCGPathElementMoveToPoint:
00142 case kCGPathElementAddLineToPoint: vml.push(VML_ELEMENT_TABLE[type], COORD(element.x), ',', COORD(element.y));
00143 break;
00144
00145 case kCGPathElementAddQuadCurveToPoint: vml.push(VML_ELEMENT_TABLE[type],
00146 COORD(element.cpx), ',', COORD(element.cpy), ',',
00147 COORD(element.x), ',', COORD(element.y));
00148 break;
00149
00150 case kCGPathElementAddCurveToPoint: vml.push(VML_ELEMENT_TABLE[type],
00151 COORD(element.cp1x), ',', COORD(element.cp1y), ',',
00152 COORD(element.cp2x), ',', COORD(element.cp2y), ',',
00153 COORD(element.x), ',', COORD(element.y));
00154 break;
00155
00156 case kCGPathElementCloseSubpath: vml.push(VML_ELEMENT_TABLE[type]);
00157 break;
00158
00159 case kCGPathElementAddArc: var x = element.x,
00160 y = element.y,
00161 radius = element.radius,
00162 clockwise = element.clockwise ? 1 : 0,
00163 endAngle = element.endAngle,
00164 startAngle = element.startAngle,
00165
00166 start = _CGPointMake(x + radius * COS(startAngle), y + radius * SIN(startAngle));
00167
00168
00169
00170
00171
00172 if (startAngle == endAngle && !clockwise)
00173 {
00174 vml.push(VML_ELEMENT_TABLE[kCGPathElementMoveToPoint], COORD(start.x), ',', COORD(start.y));
00175
00176 continue;
00177 }
00178
00179 var end = _CGPointMake(x + radius * COS(endAngle), y + radius * SIN(endAngle));
00180
00181
00182
00183
00184 if (clockwise && startAngle != endAngle && _CGPointEqualToPoint(start, end))
00185 if (start.x >= x)
00186 {
00187 if (start.y < y)
00188 start.x += 0.125;
00189 else
00190 start.y += 0.125;
00191 }
00192 else
00193 {
00194 if (end.y <= y)
00195 end.x += 0.125;
00196 else
00197 end.y += 0.125;
00198 }
00199
00200 vml.push(VML_ELEMENT_TABLE[type][clockwise],
00201 COORD(x - radius), ',', COORD(y - radius), " ",
00202 COORD(x + radius), ',', COORD(y + radius), " ",
00203 COORD(start.x), ',', COORD(start.y), " ",
00204 COORD(end.x), ',', COORD(end.y));
00205 break;
00206 case kCGPathElementAddArcTo: break;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 }
00229
00230 vml.push("\">");
00231
00232 if (gState.gradient)
00233 vml.push(gState.gradient)
00234
00235 else if (fill)
00236 vml.push("<cg_vml_:fill color=\"", gState.fillStyle, "\" opacity=\"", opacity, "\" />");
00237
00238 if (stroke)
00239 vml.push( "<cg_vml_:stroke opacity=\"", opacity,
00240 "\" joinstyle=\"", VML_LINEJOIN_TABLE[gState.lineJoin],
00241 "\" miterlimit=\"", gState.miterLimit,
00242 "\" endcap=\"", VML_LINECAP_TABLE[gState.lineCap],
00243 "\" weight=\"", gState.lineWidth, "",
00244 "px\" color=\"", gState.strokeStyle,"\" />");
00245
00246 var shadowColor = gState.shadowColor;
00247
00248 if (shadowColor)
00249 {
00250 var shadowOffset = gState.shadowOffset;
00251
00252 vml.push("<cg_vml_:shadow on=\"t\" offset=\"",
00253 shadowOffset.width, "pt ", shadowOffset.height, "pt\" opacity=\"", [shadowColor alphaComponent], "\" color=black />");
00254 }
00255
00256 vml.push("</cg_vml_:shape>");
00257
00258 aContext.path = NULL;
00259
00260 if (aContext.buffer != nil)
00261 aContext.buffer += vml.join("");
00262 else
00263 aContext.DOMElement.insertAdjacentHTML("BeforeEnd", vml.join(""));
00264 }
00265
00266 function to_string(aColor)
00267 {
00268 return "rgb(" + ROUND(aColor.components[0] * 255) + ", " + ROUND(aColor.components[1] * 255) + ", " + ROUND(255 * aColor.components[2]) + ")";
00269 }
00270
00271 function CGContextDrawLinearGradient(aContext, aGradient, aStartPoint, anEndPoint, options)
00272 {
00273 if (!aContext || !aGradient)
00274 return;
00275
00276 var vml = nil;
00277
00278 if (aGradient.vml_gradient)
00279 {
00280 var stops = [[aGradient.vml_gradient stops] sortedArrayUsingSelector:@selector(comparePosition:)],
00281 count = [stops count];
00282
00283 vml = ["<cg_vml_:fill type=\"gradient\" method=\"linear sigma\" "];
00284 vml.push("angle=\"" + ([aGradient.vml_gradient angle] + 90) +"\" ");
00285
00286 vml.push("colors=\"");
00287
00288 for (var i = 0; i < count; i++)
00289 {
00290 vml.push(([stops[i] position]*100).toFixed(0)+"% ");
00291 vml.push([[[stops[i] color] colorForSlideBase:nil] cssString]);
00292
00293 if (i < count-1)
00294 vml.push(",");
00295 }
00296
00297 vml.push("\" />");
00298 }
00299 else
00300 {
00301 var colors = aGradient.colors,
00302 count = colors.length;
00303
00304 vml = ["<cg_vml_:fill type=\"gradient\" "];
00305
00306 vml.push("colors=\"");
00307
00308 for (var i = 0; i < count; i++)
00309 vml.push((aGradient.locations[i]*100).toFixed(0)+"% "+to_string(colors[i])+(i<count-1 ? "," : ""));
00310
00311 vml.push("\" />");
00312 }
00313
00314 aContext.gState.gradient = vml.join("");
00315
00316
00317
00318
00319
00320 }