Browse Source

text alignment / baseline fixes by Olaf

v1.x
Tj Holowaychuk 14 years ago
parent
commit
4bf1f611f6
  1. 28
      src/CanvasRenderingContext2d.cc

28
src/CanvasRenderingContext2d.cc

@ -1442,37 +1442,45 @@ Context2d::StrokeText(const Arguments &args) {
void void
Context2d::setTextPath(const char *str, double x, double y) { Context2d::setTextPath(const char *str, double x, double y) {
// Text extents
cairo_text_extents_t te; cairo_text_extents_t te;
cairo_text_extents(_context, str, &te);
cairo_font_extents_t fe; cairo_font_extents_t fe;
cairo_font_extents(_context, &fe);
// Alignment // Alignment
switch (state->textAlignment) { switch (state->textAlignment) {
// center // center
case 0: case 0:
x -= te.width / 2 + te.x_bearing; // Olaf (2011-02-19): te.x_bearing does not concern the alignment
cairo_text_extents(_context, str, &te);
x -= te.width / 2;
break; break;
// right // right
case 1: case 1:
x -= te.width + te.x_bearing; // Olaf (2011-02-19): te.x_bearing does not concern the alignment
cairo_text_extents(_context, str, &te);
x -= te.width;
break; break;
} }
// Baseline approx // Baseline approx
// TODO:
switch (state->textBaseline) { switch (state->textBaseline) {
case TEXT_BASELINE_TOP: case TEXT_BASELINE_TOP:
case TEXT_BASELINE_HANGING: case TEXT_BASELINE_HANGING:
y += te.height; // Olaf (2011-02-26): fe.ascent approximates the distance between
// the top of the em square and the alphabetic baseline
cairo_font_extents(_context, &fe);
y += fe.ascent;
break; break;
case TEXT_BASELINE_MIDDLE: case TEXT_BASELINE_MIDDLE:
y += te.height / 2; // Olaf (2011-02-26): fe.ascent approximates the distance between
// the top of the em square and the alphabetic baseline
cairo_font_extents(_context, &fe);
y += fe.ascent / 2;
break; break;
case TEXT_BASELINE_BOTTOM: case TEXT_BASELINE_BOTTOM:
y -= te.height / 2; // Olaf (2011-02-26): we need to know the distance between the alphabetic
// baseline and the bottom of the em square
cairo_font_extents(_context, &fe);
y -= fe.height - fe.ascent;
break; break;
} }

Loading…
Cancel
Save