Skip to content

Commit

Permalink
MText: fix alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
dxli committed May 17, 2024
1 parent 298501f commit 4b5cd9d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 59 deletions.
69 changes: 15 additions & 54 deletions librecad/src/lib/engine/rs_mtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void RS_MText::update() {
if (static_cast<int>(data.text.length()) <= i) {
continue;
}
int ch{data.text.at(i).unicode()};
std::uint32_t ch{data.text.toUcs4().at(i)};
switch (ch) {
case 'P':
updateAddLine(oneLine, lineCounter++);
Expand Down Expand Up @@ -363,22 +363,17 @@ void RS_MText::update() {
} // outer switch (data.text.at(i).unicode())
} // for (i) loop

double tt{updateAddLine(oneLine, lineCounter)};
if (RS_MTextData::VABottom == data.valign) {
RS_Vector ot = RS_Vector{0.0, -tt}.rotate(data.angle);
RS_EntityContainer::move(ot);
}

usedTextHeight -=
data.height * data.lineSpacingFactor * 5.0 / 3.0 - data.height;
forcedCalculateBorders();
data.height * data.lineSpacingFactor * 5.0 / 3.0 - data.height;

updateAddLine(oneLine, lineCounter);

alignVertically();
RS_DEBUG->print("RS_MText::update: OK");
}

void RS_MText::alignVertically()
{
RS_EntityContainer::rotate(data.insertionPoint, - data.angle);
forcedCalculateBorders();

// Vertical Align:
Expand Down Expand Up @@ -482,61 +477,27 @@ double RS_MText::updateAddLine(RS_EntityContainer *textLine, int lineCounter) {
// RS_DEBUG->print("RS_MText::updateAddLine: width 2: %f",
// textLine->getSize().x);

// Move to correct line position:
textLine->move(
RS_Vector(0.0, -9.0 * lineCounter * data.lineSpacingFactor * ls));

if (!RS_EntityContainer::autoUpdateBorders) {
// only update borders when needed
textLine->forcedCalculateBorders();
}
RS_Vector textSize = textLine->getSize();
// Scale:
textLine->scale(RS_Vector{0., 0.},
RS_Vector(data.height / 9.0, data.height / 9.0));

RS_DEBUG->print("RS_MText::updateAddLine: width 2: %f", textSize.x);
textLine->forcedCalculateBorders();

// Horizontal Align:
if (data.drawingDirection == RS_MTextData::RightToLeft)
textSize.x = - textSize.x;
switch (data.halign) {
case RS_MTextData::HACenter:
RS_DEBUG->print("RS_MText::updateAddLine: move by: %f", -textSize.x / 2.0);
textLine->move(RS_Vector(-textSize.x / 2.0, 0.0));
textLine->move(RS_Vector{-0.5 * (textLine->getMin().x + textLine->getMax().x), 0.});
break;

case RS_MTextData::HARight:
if (data.drawingDirection != RS_MTextData::RightToLeft)
textLine->move(RS_Vector(textSize.x, 0.0));
break;

default:
if (data.drawingDirection == RS_MTextData::RightToLeft)
textLine->move(RS_Vector(-textSize.x, 0.0));
break;
}

// Vertical Align:
double vSize = getNumberOfLines() * 9.0 * data.lineSpacingFactor * ls -
(9.0 * data.lineSpacingFactor * ls - 9.0);

switch (data.valign) {
case RS_MTextData::VAMiddle:
textLine->move(RS_Vector(0.0, vSize / 2.0));
break;

case RS_MTextData::VABottom:
textLine->move(RS_Vector(0.0, vSize));
textLine->move(RS_Vector{- textLine->getMax().x, 0.});
break;

default:
textLine->move(RS_Vector{- textLine->getMin().x, 0.});
break;
}

// Scale:
textLine->scale(RS_Vector(0.0, 0.0),
RS_Vector(data.height / 9.0, data.height / 9.0));

textLine->forcedCalculateBorders();

// Update actual text size (before rotating, after scaling!):
if (textLine->getSize().x > usedTextWidth) {
usedTextWidth = textLine->getSize().x;
Expand All @@ -547,11 +508,11 @@ double RS_MText::updateAddLine(RS_EntityContainer *textLine, int lineCounter) {
// Gets the distance over text base-line (before rotating, after scaling!):
double textTail = textLine->getMin().y;

// Move:
textLine->move(data.insertionPoint + RS_Vector{0., -data.height * lineCounter * data.lineSpacingFactor * ls});
// Rotate:
textLine->rotate(RS_Vector(0.0, 0.0), data.angle);
// textLine->rotate(data.insertionPoint, data.angle);

// Move:
textLine->move(data.insertionPoint);
textLine->setPen(RS_Pen(RS2::FlagInvalid));
textLine->setLayer(nullptr);
textLine->forcedCalculateBorders();
Expand Down
29 changes: 24 additions & 5 deletions librecad/src/ui/forms/qg_dlgmtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
**********************************************************************/

#include <vector>
#include <QTextStream>

#include <QFileDialog>
#include <QLocale>
#include <QPalette>
#include <QTextStream>

#include "qg_dlgmtext.h"

Expand Down Expand Up @@ -112,6 +114,9 @@ void QG_DlgMText::init() {
teText->QFrame::setMidLineWidth(0);
teText->QFrame::setFrameStyle(QFrame::Box|QFrame::Plain);
teText->installEventFilter(this);

// events
connect(rbLeftToRight, &QRadioButton::toggled, this, &QG_DlgMText::layoutDirectionChanged);
}


Expand Down Expand Up @@ -175,7 +180,7 @@ void QG_DlgMText::setText(RS_MText& t, bool isNew) {
QString str;
//QString shape;
QString angle;
bool leftToRight = true;
bool leftToRight = locale().textDirection() == Qt::LeftToRight;

if (isNew) {
wPen->hide();
Expand Down Expand Up @@ -207,7 +212,7 @@ void QG_DlgMText::setText(RS_MText& t, bool isNew) {
//shape = RS_SETTINGS->readEntry("/TextShape", "0");
angle = RS_SETTINGS->readEntry("/TextAngle", "0");
//radius = RS_SETTINGS->readEntry("/TextRadius", "10");
leftToRight = RS_SETTINGS->readNumEntry("/TextLeftToRight", 1);
// leftToRight = RS_SETTINGS->readNumEntry("/TextLeftToRight", 1);
RS_SETTINGS->endGroup();
} else {
fon = text->getStyle();
Expand Down Expand Up @@ -275,11 +280,25 @@ void QG_DlgMText::setText(RS_MText& t, bool isNew) {
//leRadius->setText(radius);
teText->setFocus();
teText->selectAll();
text->setDrawingDirection(leftToRight ? RS_MTextData::LeftToRight : RS_MTextData::RightToLeft);
rbLeftToRight->setChecked(leftToRight);
rbRightToLeft->setChecked(!leftToRight);
layoutDirectionChanged();
}

void QG_DlgMText::layoutDirectionChanged()
{
bool leftToRight = rbLeftToRight->isChecked();
rbRightToLeft->setChecked(!leftToRight);
Qt::LayoutDirection direction = leftToRight ? Qt::LeftToRight : Qt::RightToLeft;
teText->setLayoutDirection(direction);
text->setDrawingDirection(leftToRight ? RS_MTextData::LeftToRight : RS_MTextData::RightToLeft);
QTextDocument* doc = teText->document();
if (doc) {
QTextOption option = doc->defaultTextOption();
option.setTextDirection(direction);
doc->setDefaultTextOption(option);
}
teText->update();
}

/**
* Updates the text entity represented by the dialog to fit the choices of the user.
Expand Down
1 change: 1 addition & 0 deletions librecad/src/ui/forms/qg_dlgmtext.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected slots:
virtual void languageChange();

private:
void layoutDirectionChanged();
bool isNew = false;
bool saveSettings = true;
RS_MText* text = nullptr;
Expand Down

0 comments on commit 4b5cd9d

Please sign in to comment.