Permalink
Browse files

Merge pull request #188 from uxebu/text-origin

Text#attr[textOrigin]
  • Loading branch information...
2 parents 783fca9 + 93e99f3 commit 3229320958b6c37acb3b4ac75fe5177d7bf0800b @davidaurelio davidaurelio committed Jan 24, 2013
Showing with 53 additions and 13 deletions.
  1. +12 −8 src/renderer/svg/svg.js
  2. +3 −2 src/runner/text.js
  3. +38 −3 test/renderer/svg-spec.js
@@ -91,6 +91,12 @@ define([
'mousewheel'
];
+ var textOriginMap = {
+ top: 'hanging',
+ center: 'middle',
+ bottom: 'auto'
+ };
+
// tools
var isArray = tools.isArray;
@@ -642,6 +648,8 @@ define([
proto.drawText = function(text, message) {
var attributes = message.attributes;
+ var style = text.style;
+ var textOrigin = attributes.textOrigin;
if (attributes.selectable !== undefined) {
if (attributes.selectable !== false) {
@@ -651,17 +659,13 @@ define([
}
}
- text.setAttributeNS(xlink, 'text-anchor', 'start');
+ setStyle(style, 'textAnchor', 'start');
- if (attributes.textOrigin != null) {
- text.setAttribute(
- 'alignment-baseline',
- attributes.textOrigin === 'top' ? 'hanging' : ''
- );
+ if (textOrigin != null) {
+ setStyle(style, 'alignmentBaseline', textOriginMap[textOrigin]);
+ setStyle(style, 'dominantBaseline', textOriginMap[textOrigin]);
}
- var style = text.style;
- style.textAnchor = 'start';
};
proto.drawVideo = function(foreignObject, message) {
View
@@ -10,7 +10,8 @@ define([
var accessor = tools.descriptorAccessor,
data = tools.descriptorData,
getter = tools.getter,
- parseColor = color.parse;
+ parseColor = color.parse,
+ isArray = tools.isArray;
// Getters and setters stolen from shape.js
@@ -174,7 +175,7 @@ define([
*/
proto.addChild = function(child, index) {
var isTextSpan =
- tools.isArray(child) ?
+ isArray(child) ?
child.every(function(child) {
return child instanceof TextSpan;
}) :
@@ -1,6 +1,7 @@
-define([
- 'bonsai/renderer/svg/svg'
-], function(SvgRenderer) {
+define(['bonsai/renderer/svg/svg'], function(SvgRenderer) {
+
+ 'use strict';
+
describe('SvgRenderer', function() {
function createFakeDomNode() {
return {
@@ -134,6 +135,40 @@ define([
});
});
+ describe('drawText', function() {
+ var textElement;
+ beforeEach(function() {
+ textElement = { style: {} };
+ });
+ it('is a function', function() {
+ expect(typeof createSvgRenderer().drawText).toBe('function');
+ });
+ it('sets baseline-alignment=hanging when attr.textOrigin=top', function() {
+ createSvgRenderer().drawText(textElement, { attributes: { textOrigin: 'top' } });
+ expect(textElement.style.alignmentBaseline).toBe('hanging');
+ });
+ it('sets dominant-alignment=hanging when attr.textOrigin=top', function() {
+ createSvgRenderer().drawText(textElement, { attributes: { textOrigin: 'top' } });
+ expect(textElement.style.dominantBaseline).toBe('hanging');
+ });
+ it('sets baseline-alignment=middle when attr.textOrigin=center', function() {
+ createSvgRenderer().drawText(textElement, { attributes: { textOrigin: 'center' } });
+ expect(textElement.style.alignmentBaseline).toBe('middle');
+ });
+ it('sets dominant-alignment=middle when attr.textOrigin=center', function() {
+ createSvgRenderer().drawText(textElement, { attributes: { textOrigin: 'center' } });
+ expect(textElement.style.dominantBaseline).toBe('middle');
+ });
+ it('sets baseline-alignment=auto when attr.textOrigin=center', function() {
+ createSvgRenderer().drawText(textElement, { attributes: { textOrigin: 'bottom' } });
+ expect(textElement.style.alignmentBaseline).toBe('auto');
+ });
+ it('sets dominant-alignment=auto when attr.textOrigin=center', function() {
+ createSvgRenderer().drawText(textElement, { attributes: { textOrigin: 'bottom' } });
+ expect(textElement.style.dominantBaseline).toBe('auto');
+ });
+ });
+
describe('Frame logging', function() {
function createSvgRenderer(fpsLog, getTime) {
var renderer = new SvgRenderer(createFakeDomNode(), 1, 1, {fpsLog: fpsLog});

0 comments on commit 3229320

Please sign in to comment.