123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877 |
- goog.provide('goog.graphics.SvgGraphics');
- goog.require('goog.Timer');
- goog.require('goog.dom');
- goog.require('goog.events.EventHandler');
- goog.require('goog.events.EventType');
- goog.require('goog.graphics.AbstractGraphics');
- goog.require('goog.graphics.Font');
- goog.require('goog.graphics.LinearGradient');
- goog.require('goog.graphics.Path');
- goog.require('goog.graphics.SolidFill');
- goog.require('goog.graphics.Stroke');
- goog.require('goog.graphics.SvgEllipseElement');
- goog.require('goog.graphics.SvgGroupElement');
- goog.require('goog.graphics.SvgImageElement');
- goog.require('goog.graphics.SvgPathElement');
- goog.require('goog.graphics.SvgRectElement');
- goog.require('goog.graphics.SvgTextElement');
- goog.require('goog.math');
- goog.require('goog.math.Size');
- goog.require('goog.style');
- goog.require('goog.userAgent');
- goog.graphics.SvgGraphics = function(
- width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {
- goog.graphics.AbstractGraphics.call(
- this, width, height, opt_coordWidth, opt_coordHeight, opt_domHelper);
-
- this.defs_ = {};
-
- this.useManualViewbox_ =
- goog.userAgent.WEBKIT && !goog.userAgent.isVersionOrHigher(526);
-
- this.handler_ = new goog.events.EventHandler(this);
- };
- goog.inherits(goog.graphics.SvgGraphics, goog.graphics.AbstractGraphics);
- goog.graphics.SvgGraphics.SVG_NS_ = 'http://www.w3.org/2000/svg';
- goog.graphics.SvgGraphics.DEF_ID_PREFIX_ = '_svgdef_';
- goog.graphics.SvgGraphics.nextDefId_ = 0;
- goog.graphics.SvgGraphics.prototype.defsElement_;
- goog.graphics.SvgGraphics.prototype.createSvgElement_ = function(
- tagName, opt_attributes) {
- var element = this.dom_.getDocument().createElementNS(
- goog.graphics.SvgGraphics.SVG_NS_, tagName);
- if (opt_attributes) {
- this.setElementAttributes(element, opt_attributes);
- }
- return element;
- };
- goog.graphics.SvgGraphics.prototype.setElementAttributes = function(
- element, attributes) {
- for (var key in attributes) {
- element.setAttribute(key, attributes[key]);
- }
- };
- goog.graphics.SvgGraphics.prototype.append_ = function(element, opt_group) {
- var parent = opt_group || this.canvasElement;
- parent.getElement().appendChild(element.getElement());
- };
- goog.graphics.SvgGraphics.prototype.setElementFill = function(element, fill) {
- var svgElement = element.getElement();
- if (fill instanceof goog.graphics.SolidFill) {
- svgElement.setAttribute('fill', fill.getColor());
- svgElement.setAttribute('fill-opacity', fill.getOpacity());
- } else if (fill instanceof goog.graphics.LinearGradient) {
-
- var defKey = 'lg-' + fill.getX1() + '-' + fill.getY1() + '-' +
- fill.getX2() + '-' + fill.getY2() + '-' + fill.getColor1() + '-' +
- fill.getColor2();
-
- var id = this.getDef(defKey);
- if (!id) {
-
- var gradient = this.createSvgElement_('linearGradient', {
- 'x1': fill.getX1(),
- 'y1': fill.getY1(),
- 'x2': fill.getX2(),
- 'y2': fill.getY2(),
- 'gradientUnits': 'userSpaceOnUse'
- });
- var gstyle = 'stop-color:' + fill.getColor1();
- if (goog.isNumber(fill.getOpacity1())) {
- gstyle += ';stop-opacity:' + fill.getOpacity1();
- }
- var stop1 =
- this.createSvgElement_('stop', {'offset': '0%', 'style': gstyle});
- gradient.appendChild(stop1);
-
-
-
-
-
- gstyle = 'stop-color:' + fill.getColor2();
- if (goog.isNumber(fill.getOpacity2())) {
- gstyle += ';stop-opacity:' + fill.getOpacity2();
- }
- var stop2 =
- this.createSvgElement_('stop', {'offset': '100%', 'style': gstyle});
- gradient.appendChild(stop2);
-
-
-
-
-
- id = this.addDef(defKey, gradient);
- }
-
- svgElement.setAttribute('fill', 'url(#' + id + ')');
- } else {
- svgElement.setAttribute('fill', 'none');
- }
- };
- goog.graphics.SvgGraphics.prototype.setElementStroke = function(
- element, stroke) {
- var svgElement = element.getElement();
- if (stroke) {
- svgElement.setAttribute('stroke', stroke.getColor());
- svgElement.setAttribute('stroke-opacity', stroke.getOpacity());
- var width = stroke.getWidth();
- if (goog.isString(width) && width.indexOf('px') != -1) {
- svgElement.setAttribute(
- 'stroke-width', parseFloat(width) / this.getPixelScaleX());
- } else {
- svgElement.setAttribute('stroke-width', width);
- }
- } else {
- svgElement.setAttribute('stroke', 'none');
- }
- };
- goog.graphics.SvgGraphics.prototype.setElementTransform = function(
- element, x, y, angle, centerX, centerY) {
- element.getElement().setAttribute(
- 'transform', 'translate(' + x + ',' + y + ') rotate(' + angle + ' ' +
- centerX + ' ' + centerY + ')');
- };
- goog.graphics.SvgGraphics.prototype.setElementAffineTransform = function(
- element, affineTransform) {
- var t = affineTransform;
- var substr = [
- t.getScaleX(), t.getShearY(), t.getShearX(), t.getScaleY(),
- t.getTranslateX(), t.getTranslateY()
- ].join(',');
- element.getElement().setAttribute('transform', 'matrix(' + substr + ')');
- };
- goog.graphics.SvgGraphics.prototype.createDom = function() {
-
- var attributes =
- {'width': this.width, 'height': this.height, 'overflow': 'hidden'};
- var svgElement = this.createSvgElement_('svg', attributes);
- var groupElement = this.createSvgElement_('g');
- this.defsElement_ = this.createSvgElement_('defs');
- this.canvasElement = new goog.graphics.SvgGroupElement(groupElement, this);
- svgElement.appendChild(this.defsElement_);
- svgElement.appendChild(groupElement);
-
- this.setElementInternal(svgElement);
-
- this.setViewBox_();
- };
- goog.graphics.SvgGraphics.prototype.setCoordOrigin = function(left, top) {
- this.coordLeft = left;
- this.coordTop = top;
- this.setViewBox_();
- };
- goog.graphics.SvgGraphics.prototype.setCoordSize = function(
- coordWidth, coordHeight) {
- goog.graphics.SvgGraphics.superClass_.setCoordSize.apply(this, arguments);
- this.setViewBox_();
- };
- goog.graphics.SvgGraphics.prototype.getViewBox_ = function() {
- return this.coordLeft + ' ' + this.coordTop + ' ' +
- (this.coordWidth ? this.coordWidth + ' ' + this.coordHeight : '');
- };
- goog.graphics.SvgGraphics.prototype.setViewBox_ = function() {
- if (this.coordWidth || this.coordLeft || this.coordTop) {
- this.getElement().setAttribute('preserveAspectRatio', 'none');
- if (this.useManualViewbox_) {
- this.updateManualViewBox_();
- } else {
- this.getElement().setAttribute('viewBox', this.getViewBox_());
- }
- }
- };
- goog.graphics.SvgGraphics.prototype.updateManualViewBox_ = function() {
- if (!this.isInDocument() ||
- !(this.coordWidth || this.coordLeft || !this.coordTop)) {
- return;
- }
- var size = this.getPixelSize();
- if (size.width == 0) {
-
- this.getElement().style.visibility = 'hidden';
- return;
- }
- this.getElement().style.visibility = '';
- var offsetX = -this.coordLeft;
- var offsetY = -this.coordTop;
- var scaleX = size.width / this.coordWidth;
- var scaleY = size.height / this.coordHeight;
- this.canvasElement.getElement().setAttribute(
- 'transform', 'scale(' + scaleX + ' ' + scaleY + ') ' +
- 'translate(' + offsetX + ' ' + offsetY + ')');
- };
- goog.graphics.SvgGraphics.prototype.setSize = function(
- pixelWidth, pixelHeight) {
- goog.style.setSize(this.getElement(), pixelWidth, pixelHeight);
- };
- goog.graphics.SvgGraphics.prototype.getPixelSize = function() {
- if (!goog.userAgent.GECKO) {
- return this.isInDocument() ?
- goog.style.getSize(this.getElement()) :
- goog.graphics.SvgGraphics.base(this, 'getPixelSize');
- }
-
-
- var width = this.width;
- var height = this.height;
- var computeWidth = goog.isString(width) && width.indexOf('%') != -1;
- var computeHeight = goog.isString(height) && height.indexOf('%') != -1;
- if (!this.isInDocument() && (computeWidth || computeHeight)) {
- return null;
- }
- var parent;
- var parentSize;
- if (computeWidth) {
- parent = (this.getElement().parentNode);
- parentSize = goog.style.getSize(parent);
- width = parseFloat( (width)) * parentSize.width / 100;
- }
- if (computeHeight) {
- parent = parent || (this.getElement().parentNode);
- parentSize = parentSize || goog.style.getSize(parent);
- height =
- parseFloat( (height)) * parentSize.height / 100;
- }
- return new goog.math.Size(
- (width),
- (height));
- };
- goog.graphics.SvgGraphics.prototype.clear = function() {
- this.canvasElement.clear();
- goog.dom.removeChildren(this.defsElement_);
- this.defs_ = {};
- };
- goog.graphics.SvgGraphics.prototype.drawEllipse = function(
- cx, cy, rx, ry, stroke, fill, opt_group) {
- var element = this.createSvgElement_(
- 'ellipse', {'cx': cx, 'cy': cy, 'rx': rx, 'ry': ry});
- var wrapper =
- new goog.graphics.SvgEllipseElement(element, this, stroke, fill);
- this.append_(wrapper, opt_group);
- return wrapper;
- };
- goog.graphics.SvgGraphics.prototype.drawRect = function(
- x, y, width, height, stroke, fill, opt_group) {
- var element = this.createSvgElement_(
- 'rect', {'x': x, 'y': y, 'width': width, 'height': height});
- var wrapper = new goog.graphics.SvgRectElement(element, this, stroke, fill);
- this.append_(wrapper, opt_group);
- return wrapper;
- };
- goog.graphics.SvgGraphics.prototype.drawImage = function(
- x, y, width, height, src, opt_group) {
- var element = this.createSvgElement_('image', {
- 'x': x,
- 'y': y,
- 'width': width,
- 'height': height,
- 'image-rendering': 'optimizeQuality',
- 'preserveAspectRatio': 'none'
- });
- element.setAttributeNS('http://www.w3.org/1999/xlink', 'href', src);
- var wrapper = new goog.graphics.SvgImageElement(element, this);
- this.append_(wrapper, opt_group);
- return wrapper;
- };
- goog.graphics.SvgGraphics.prototype.drawTextOnLine = function(
- text, x1, y1, x2, y2, align, font, stroke, fill, opt_group) {
- var angle = Math.round(goog.math.angle(x1, y1, x2, y2));
- var dx = x2 - x1;
- var dy = y2 - y1;
- var lineLength = Math.round(Math.sqrt(dx * dx + dy * dy));
-
-
- var fontSize = font.size;
- var attributes = {'font-family': font.family, 'font-size': fontSize};
- var baseline = Math.round(fontSize * 0.85);
- var textY = Math.round(y1 - (fontSize / 2) + baseline);
- var textX = x1;
- if (align == 'center') {
- textX += Math.round(lineLength / 2);
- attributes['text-anchor'] = 'middle';
- } else if (align == 'right') {
- textX += lineLength;
- attributes['text-anchor'] = 'end';
- }
- attributes['x'] = textX;
- attributes['y'] = textY;
- if (font.bold) {
- attributes['font-weight'] = 'bold';
- }
- if (font.italic) {
- attributes['font-style'] = 'italic';
- }
- if (angle != 0) {
- attributes['transform'] = 'rotate(' + angle + ' ' + x1 + ' ' + y1 + ')';
- }
- var element = this.createSvgElement_('text', attributes);
- element.appendChild(this.dom_.getDocument().createTextNode(text));
-
-
- if (stroke == null && goog.userAgent.GECKO && goog.userAgent.MAC) {
- var color = 'black';
-
- if (fill instanceof goog.graphics.SolidFill) {
- color = fill.getColor();
- }
- stroke = new goog.graphics.Stroke(1, color);
- }
- var wrapper = new goog.graphics.SvgTextElement(element, this, stroke, fill);
- this.append_(wrapper, opt_group);
- return wrapper;
- };
- goog.graphics.SvgGraphics.prototype.drawPath = function(
- path, stroke, fill, opt_group) {
- var element = this.createSvgElement_(
- 'path', {'d': goog.graphics.SvgGraphics.getSvgPath(path)});
- var wrapper = new goog.graphics.SvgPathElement(element, this, stroke, fill);
- this.append_(wrapper, opt_group);
- return wrapper;
- };
- goog.graphics.SvgGraphics.getSvgPath = function(path) {
- var list = [];
- path.forEachSegment(function(segment, args) {
- switch (segment) {
- case goog.graphics.Path.Segment.MOVETO:
- list.push('M');
- Array.prototype.push.apply(list, args);
- break;
- case goog.graphics.Path.Segment.LINETO:
- list.push('L');
- Array.prototype.push.apply(list, args);
- break;
- case goog.graphics.Path.Segment.CURVETO:
- list.push('C');
- Array.prototype.push.apply(list, args);
- break;
- case goog.graphics.Path.Segment.ARCTO:
- var extent = args[3];
- list.push(
- 'A', args[0], args[1], 0, Math.abs(extent) > 180 ? 1 : 0,
- extent > 0 ? 1 : 0, args[4], args[5]);
- break;
- case goog.graphics.Path.Segment.CLOSE:
- list.push('Z');
- break;
- }
- });
- return list.join(' ');
- };
- goog.graphics.SvgGraphics.prototype.createGroup = function(opt_group) {
- var element = this.createSvgElement_('g');
- var parent = opt_group || this.canvasElement;
- parent.getElement().appendChild(element);
- return new goog.graphics.SvgGroupElement(element, this);
- };
- goog.graphics.SvgGraphics.prototype.getTextWidth = function(text, font) {
-
- throw new Error("unimplemented method");
- };
- goog.graphics.SvgGraphics.prototype.addDef = function(defKey, defElement) {
- if (defKey in this.defs_) {
- return this.defs_[defKey];
- }
- var id = goog.graphics.SvgGraphics.DEF_ID_PREFIX_ +
- goog.graphics.SvgGraphics.nextDefId_++;
- defElement.setAttribute('id', id);
- this.defs_[defKey] = id;
-
- var defs = this.defsElement_;
- defs.appendChild(defElement);
- return id;
- };
- goog.graphics.SvgGraphics.prototype.getDef = function(defKey) {
- return defKey in this.defs_ ? this.defs_[defKey] : null;
- };
- goog.graphics.SvgGraphics.prototype.removeDef = function(defKey) {
- var id = this.getDef(defKey);
- if (id) {
- var element = this.dom_.getElement(id);
- this.defsElement_.removeChild(element);
- delete this.defs_[defKey];
- }
- };
- goog.graphics.SvgGraphics.prototype.enterDocument = function() {
- var oldPixelSize = this.getPixelSize();
- goog.graphics.SvgGraphics.superClass_.enterDocument.call(this);
-
- if (!oldPixelSize) {
- this.dispatchEvent(goog.events.EventType.RESIZE);
- }
-
- if (this.useManualViewbox_) {
- var width = this.width;
- var height = this.height;
- if (typeof width == 'string' && width.indexOf('%') != -1 &&
- typeof height == 'string' && height.indexOf('%') != -1) {
-
-
- this.handler_.listen(
- goog.graphics.SvgGraphics.getResizeCheckTimer_(), goog.Timer.TICK,
- this.updateManualViewBox_);
- }
- this.updateManualViewBox_();
- }
- };
- goog.graphics.SvgGraphics.prototype.exitDocument = function() {
- goog.graphics.SvgGraphics.superClass_.exitDocument.call(this);
-
- if (this.useManualViewbox_) {
- this.handler_.unlisten(
- goog.graphics.SvgGraphics.getResizeCheckTimer_(), goog.Timer.TICK,
- this.updateManualViewBox_);
- }
- };
- goog.graphics.SvgGraphics.prototype.disposeInternal = function() {
- delete this.defs_;
- delete this.defsElement_;
- delete this.canvasElement;
- this.handler_.dispose();
- delete this.handler_;
- goog.graphics.SvgGraphics.superClass_.disposeInternal.call(this);
- };
- goog.graphics.SvgGraphics.resizeCheckTimer_;
- goog.graphics.SvgGraphics.getResizeCheckTimer_ = function() {
- if (!goog.graphics.SvgGraphics.resizeCheckTimer_) {
- goog.graphics.SvgGraphics.resizeCheckTimer_ = new goog.Timer(400);
- goog.graphics.SvgGraphics.resizeCheckTimer_.start();
- }
- return (
- goog.graphics.SvgGraphics.resizeCheckTimer_);
- };
- goog.graphics.SvgGraphics.prototype.isDomClonable = function() {
- return true;
- };
|