[Kst] extragear/graphics/kst/kst
George Staikos
staikos at kde.org
Fri Jun 17 18:14:26 CEST 2005
SVN commit 426540 by staikos:
Get rotation working in all cases and add adjustSizeForText() to
bindings
M +22 -0 extensions/js/bind_label.cpp
M +4 -0 extensions/js/bind_label.h
M +2 -0 extensions/js/examples/testlabels.js
M +20 -13 kstviewlabel.cpp
--- trunk/extragear/graphics/kst/kst/extensions/js/bind_label.cpp #426539:426540
@@ -93,6 +93,7 @@
static LabelBindings labelBindings[] = {
+ { "adjustSizeForText", &KstBindLabel::adjustSizeForText },
{ 0L, 0L }
};
@@ -404,6 +405,27 @@
}
+KJS::Value KstBindLabel::adjustSizeForText(KJS::ExecState *exec, const KJS::List& args) {
+ KstViewLabelPtr d = makeLabel(_d);
+ if (!d) {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::GeneralError);
+ exec->setException(eobj);
+ return KJS::Undefined();
+ }
+
+ if (args.size() != 0) {
+ KJS::Object eobj = KJS::Error::create(exec, KJS::SyntaxError);
+ exec->setException(eobj);
+ return KJS::Undefined();
+ }
+
+ KstWriteLocker rl(d);
+ d->adjustSizeForText();
+ KstApp::inst()->paintAll(P_PAINT);
+ return KJS::Value();
+}
+
+
#undef makeLabel
// vim: ts=2 sw=2 et
--- trunk/extragear/graphics/kst/kst/extensions/js/bind_label.h #426539:426540
@@ -53,6 +53,10 @@
int methodCount() const;
int propertyCount() const;
+ /* @method adjustSizeForText
+ @description Adjusts the size of the label to fit the text.
+ */
+ KJS::Value adjustSizeForText(KJS::ExecState *exec, const KJS::List& args);
/* @property string text
@description Contains the text contents of the label. This may include
carriage returns (\n), scalar references of the form
--- trunk/extragear/graphics/kst/kst/extensions/js/examples/testlabels.js #426539:426540
@@ -42,7 +42,9 @@
lh.text = "\\Sigma^{(x+5)^2}_{5+1} + \\Pi^{\\Sigma^{i-j}_{i+j}}_{x = 0} + 5";
lh.position = new Point(400, 394);
lh.fontSize = 32;
+lh.justification = 3 | (3 << 8);
lh.rotation = 45;
+lh.adjustSizeForText();
--- trunk/extragear/graphics/kst/kst/kstviewlabel.cpp #426539:426540
@@ -332,8 +332,6 @@
QPen pen;
pen.setColor(foregroundColor());
p.setPen(pen);
- p.rotate(_rotation);
- double rotationRadians = M_PI * (int(_rotation) % 360) / 180;
int hJust = KST_JUSTIFY_H(_justify);
if (QApplication::reverseLayout()) {
@@ -346,42 +344,51 @@
}
}
- int descent = _textHeight - 1 - _ascent;
+ RenderContext rc(_fontName, _symbolFontName, _fontSize, &p);
+ double rotationRadians = M_PI * (int(_rotation) % 360) / 180;
+ double absin = fabs(sin(rotationRadians));
+ double abcos = fabs(cos(rotationRadians));
- double up = _textHeight * sin(rotationRadians) * sin(rotationRadians);
- double in = _textHeight * sin(2 * rotationRadians) / 2;
+ int tx = 0, ty = 0; // translation
- RenderContext rc(_fontName, _symbolFontName, _fontSize, &p);
-
switch (hJust) {
case KST_JUSTIFY_H_RIGHT:
- rc.x = size().width() - _textWidth;
+ rc.x = -_textWidth / 2;
+ tx = size().width() - int(_textWidth * abcos + _textHeight * absin) / 2;
break;
case KST_JUSTIFY_H_CENTER:
- rc.x = (size().width() - _textWidth) / 2;
+ rc.x = -_textWidth / 2;
+ tx = size().width() / 2;
break;
case KST_JUSTIFY_H_NONE:
abort(); // should never be able to happen
case KST_JUSTIFY_H_LEFT:
default:
- rc.x = in;
+ rc.x = -_textWidth / 2;
+ tx = int(_textWidth * abcos + _textHeight * absin) / 2;
break;
}
switch (KST_JUSTIFY_V(_justify)) {
case KST_JUSTIFY_V_BOTTOM:
- rc.y = size().height() - descent;
+ rc.y = _ascent - _textHeight / 2;
+ ty = size().height() - int(_textHeight * abcos + _textWidth * absin) / 2;
break;
case KST_JUSTIFY_V_CENTER:
- rc.y = (size().height() + _textHeight) / 2 - descent;
+ rc.y = _ascent - _textHeight / 2;
+ ty = size().height() / 2;
break;
case KST_JUSTIFY_V_NONE:
case KST_JUSTIFY_V_TOP:
default:
- rc.y = _ascent - up;
+ rc.y = _ascent - _textHeight / 2;
+ ty = int(_textHeight * abcos + _textWidth * absin) / 2;
break;
}
+ p.translate(tx, ty);
+ p.rotate(_rotation);
+
renderLabel(rc, fi);
}
More information about the Kst
mailing list