Skip to content

Commit 0934402

Browse files
committed
add 'aligned' style command; add angle argument to text()
aligned labels follow the direction of the line they're labelling.
1 parent 30adbf5 commit 0934402

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

‎eukleides.js‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,13 +2043,13 @@ class SVGDrawer extends Drawer {
20432043
}
20442044

20452045
draw_text(text,x,y) {
2046-
const e = this.element('text',{x:x,y:-y,'dominant-baseline': 'central', transform: 'scale(1,-1)'},clean_label(text));
2046+
const e = this.element('text',{'dominant-baseline': 'central', transform: `scale(1,-1) translate(${x},${-y})`},clean_label(text));
20472047
this.set_font(e);
20482048
this.set_fill(e,false);
20492049
return e;
20502050
}
20512051

2052-
label_point(A) {
2052+
label_point(A, align_angle) {
20532053
const text = this.local.label_text;
20542054
if(!text) {
20552055
return;
@@ -2066,27 +2066,36 @@ class SVGDrawer extends Drawer {
20662066
const y = A.y+dist*sin(angle);
20672067
const e = this.draw_text(text,x,y);
20682068
let textAlign = 'start';
2069-
let dy = '0';
2070-
if(angle>=3*PI/4 || angle<=-3*PI/4) {
2069+
let dy = 0;
2070+
if(this.local.align_label) {
2071+
dy = -0.5 * dist;
2072+
textAlign = 'middle';
2073+
} else if(angle>=3*PI/4 || angle<=-3*PI/4) {
20712074
textAlign = 'end';
20722075
} else if(angle>PI/4 || angle<-PI/4) {
20732076
textAlign = 'middle';
20742077
if(angle>PI/4) {
2075-
dy = '-0.5em';
2078+
dy = -0.5;
20762079
} else {
2077-
dy = '0.5em';
2080+
dy = 0.5;
20782081
}
20792082
}
20802083
if(dist==0) {
20812084
textAlign = 'middle';
20822085
}
2083-
e.setAttribute('dy',dy);
2086+
e.setAttribute('dy',`${dy}em`);
20842087
e.setAttribute('text-anchor',textAlign);
20852088
let desc = `label "${clean_label(text)}"`;
20862089
if(this.aria_mode==VERBOSE) {
20872090
desc += ` at ${A}`;
20882091
}
20892092
this.set_aria_label(e,desc);
2093+
if(align_angle === undefined) {
2094+
align_angle = angle;
2095+
}
2096+
if(this.local.align_label) {
2097+
this.transform(e, `rotate(${dp(RTOD(-align_angle))})`);
2098+
}
20902099
return e;
20912100
}
20922101

@@ -2102,9 +2111,8 @@ class SVGDrawer extends Drawer {
21022111
let desc = s.getAttribute('aria-label') || '';
21032112
g.appendChild(s);
21042113

2105-
let mark, text;
21062114
if(this.local.label_segment!=NONE) {
2107-
mark = this.transform(this.element('path'), `translate(${dp(x)} ${dp(y)}) scale(${dp(size)}) rotate(${dp(RTOD(angle))})`);
2115+
const mark = this.transform(this.element('path'), `translate(${dp(x)} ${dp(y)}) scale(${dp(size)}) rotate(${dp(RTOD(angle))})`);
21082116
this.set_stroke(mark);
21092117
mark.setAttribute('stroke-width',mark.getAttribute('stroke-width')/size);
21102118
this.set_style(mark);
@@ -2132,7 +2140,7 @@ class SVGDrawer extends Drawer {
21322140
this.local.label_direction = argument(A,B)+PI/2;
21332141
}
21342142
const m = Point.create_midpoint(new Set([A,B]));
2135-
text = this.label_point(m);
2143+
const text = this.label_point(m, angle);
21362144
this.pop_local_settings();
21372145
g.appendChild(text);
21382146
text.removeAttribute('aria-label');

‎extension.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,8 @@ Numbas.addExtension('eukleides',['math','jme','jme-display'], function(extension
11421142
'bold': {bold: true},
11431143
'italic': {italic: true},
11441144
'verbose': {aria_mode: 'verbose'},
1145-
'nospoilers': {aria_mode: 'nospoilers'}
1145+
'nospoilers': {aria_mode: 'nospoilers'},
1146+
'aligned': {align_label: true},
11461147
}
11471148

11481149
var colors = ['black','darkgray','gray','lightgray','white'];
@@ -1173,8 +1174,8 @@ Numbas.addExtension('eukleides',['math','jme','jme-display'], function(extension
11731174
return new TDrawing([],{description: description});
11741175
},{unwrapValues: true, description:'Set the accessible description for the object being drawn.'}));
11751176

1176-
extension.scope.addFunction(new funcObj('text',[TString],TDrawing,function(text) {
1177-
return new TDrawing([],{label:true, label_text: text, label_dist: 0});
1177+
extension.scope.addFunction(new funcObj('text',[TString, sig.optional(sig.type('eukleides_angle'))],TDrawing,function(text, angle) {
1178+
return new TDrawing([],{label:true, label_text: text, label_dist: 0, label_direction: angle});
11781179
}, {unwrapValues: true, description:'Draw text at a point.'}));
11791180

11801181
extension.scope.addFunction(new funcObj('angle',[TPoint,TPoint,TPoint],TAngleLabel,function(a,b,c) {

‎playground.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const modifier_descriptions = {
5454
'italic': 'Text is drawn in italic style.',
5555
'verbose': 'Accessible descriptions give information not explicitly given in the visual rendering, such as coordinates and bearings.',
5656
'nospoilers': 'Accessible descriptions will not give any information not explicitly given in the visual rendering.',
57+
'aligned': 'Labels on line segments are drawn parallel to the line.',
5758
};
5859

5960
function nice_type_name(name) {

0 commit comments

Comments
 (0)