Implemented text auto shrink feature.

This commit is contained in:
Jim Evins
2018-07-08 20:39:14 -04:00
parent 0832acabf5
commit 12b02d2521
10 changed files with 451 additions and 282 deletions
+20
View File
@@ -634,6 +634,26 @@ namespace glabels
}
///
/// Virtual Text Auto Shrink Property Default Getter
/// (Overridden by concrete class)
///
bool ModelObject::textAutoShrink() const
{
return false;
}
///
/// Virtual Text Auto Shrink Property Default Setter
/// (Overridden by concrete class)
///
void ModelObject::setTextAutoShrink( bool value )
{
// empty
}
///
/// Virtual Filename Node Property Default Getter
/// (Overridden by concrete class)
+7
View File
@@ -265,6 +265,13 @@ namespace glabels
virtual void setTextLineSpacing( double value );
//
// Virtual Text Property: textAutoShrink
//
virtual bool textAutoShrink() const;
virtual void setTextAutoShrink( bool value );
///////////////////////////////////////////////////////////////
// Image Properties Virtual Interface
///////////////////////////////////////////////////////////////
+95 -1
View File
@@ -71,6 +71,7 @@ namespace glabels
mTextVAlign = Qt::AlignTop;
mTextWrapMode = QTextOption::WordWrap;
mTextLineSpacing = 1;
mTextAutoShrink = false;
}
@@ -92,6 +93,7 @@ namespace glabels
Qt::Alignment textVAlign,
QTextOption::WrapMode textWrapMode,
double textLineSpacing,
bool textAutoShrink,
const QMatrix& matrix,
bool shadowState,
const Distance& shadowX,
@@ -124,6 +126,7 @@ namespace glabels
mTextVAlign = textVAlign;
mTextWrapMode = textWrapMode;
mTextLineSpacing = textLineSpacing;
mTextAutoShrink = textAutoShrink;
update(); // Initialize cached editor layouts
}
@@ -146,6 +149,7 @@ namespace glabels
mTextVAlign = object->mTextVAlign;
mTextWrapMode = object->mTextWrapMode;
mTextLineSpacing = object->mTextLineSpacing;
mTextAutoShrink = object->mTextAutoShrink;
update(); // Initialize cached editor layouts
}
@@ -428,6 +432,29 @@ namespace glabels
}
///
/// TextAutoShrink Property Getter
///
bool ModelTextObject::textAutoShrink() const
{
return mTextAutoShrink;
}
///
/// TextAutoShrink Property Setter
///
void ModelTextObject::setTextAutoShrink( bool value )
{
if ( mTextAutoShrink != value )
{
mTextAutoShrink = value;
update();
emit changed();
}
}
///
/// NaturalSize Property Getter
///
@@ -677,7 +704,7 @@ namespace glabels
QFont font;
font.setFamily( mFontFamily );
font.setPointSizeF( mFontSize );
font.setPointSizeF( mTextAutoShrink ? autoShrinkFontSize( record ) : mFontSize );
font.setWeight( mFontWeight );
font.setItalic( mFontItalicFlag );
font.setUnderline( mFontUnderlineFlag );
@@ -758,5 +785,72 @@ namespace glabels
painter->restore();
}
///
/// Determine auto shrink font size
///
double
ModelTextObject::autoShrinkFontSize( merge::Record* record ) const
{
QFont font;
font.setFamily( mFontFamily );
font.setWeight( mFontWeight );
font.setItalic( mFontItalicFlag );
font.setUnderline( mFontUnderlineFlag );
QTextOption textOption;
textOption.setAlignment( mTextHAlign );
textOption.setWrapMode( mTextWrapMode );
QTextDocument document( mText.expand( record ) );
double candidateSize = mFontSize;
while ( candidateSize > 1.0 )
{
font.setPointSizeF( candidateSize );
// Line spacing is affected by font size
QFontMetricsF fontMetrics( font );
double dy = fontMetrics.lineSpacing() * mTextLineSpacing;
// Do candidate layouts, letting text flow according to wrap mode
double x = 0;
double y = 0;
QRectF layoutsRect;
for ( int i = 0; i < document.blockCount(); i++ )
{
QTextLayout layout( document.findBlockByNumber(i).text() );
layout.setFont( font );
layout.setTextOption( textOption );
layout.setCacheEnabled(true);
layout.beginLayout();
for ( QTextLine l = layout.createLine(); l.isValid(); l = layout.createLine() )
{
l.setLineWidth( mW.pt() - 2*marginPts );
l.setPosition( QPointF( x, y ) );
y += dy;
}
layout.endLayout();
layoutsRect = layout.boundingRect().united( layoutsRect );
}
// Did this candidate fit in our object's bounding box?
if ( ( (layoutsRect.width() + 2*marginPts) <= mW.pt() ) &&
( (layoutsRect.height() + 2*marginPts) <= mH.pt() ) )
{
break;
}
// If not, let's try a slightly smaller font size
candidateSize -= 0.5;
}
return candidateSize;
}
}
}
+10
View File
@@ -61,6 +61,7 @@ namespace glabels
Qt::Alignment textVAlign,
QTextOption::WrapMode textWrapMode,
double textLineSpacing,
bool textAutoShrink,
const QMatrix& matrix = QMatrix(),
bool shadowState = false,
const Distance& shadowX = 0,
@@ -160,6 +161,13 @@ namespace glabels
void setTextLineSpacing( double value ) override;
//
// Text Property: textAutoShrink
//
bool textAutoShrink() const override;
void setTextAutoShrink( bool value ) override;
//
// Property: naturalSize
//
@@ -191,6 +199,7 @@ namespace glabels
void drawTextInEditor( QPainter* painter, const QColor& color ) const;
void drawText( QPainter* painter, const QColor&color, merge::Record* record ) const;
QString expandText( QString text, merge::Record* record ) const;
double autoShrinkFontSize( merge::Record* record ) const;
///////////////////////////////////////////////////////////////
@@ -208,6 +217,7 @@ namespace glabels
Qt::Alignment mTextVAlign;
QTextOption::WrapMode mTextWrapMode;
double mTextLineSpacing;
bool mTextAutoShrink;
QList<QTextLayout*> mEditorLayouts;
QPainterPath mHoverPath;
+2 -1
View File
@@ -338,10 +338,11 @@ namespace glabels
XmlUtil::setBoolAttr( node, "font_underline", object->fontUnderlineFlag() );
/* text attrs */
XmlUtil::setDoubleAttr( node, "line_spacing", object->textLineSpacing() );
XmlUtil::setAlignmentAttr( node, "align", object->textHAlign() );
XmlUtil::setAlignmentAttr( node, "valign", object->textVAlign() );
XmlUtil::setWrapModeAttr( node, "wrap", object->textWrapMode() );
XmlUtil::setDoubleAttr( node, "line_spacing", object->textLineSpacing() );
XmlUtil::setBoolAttr( node, "auto_shrink", object->textAutoShrink() );
/* affine attrs */
createAffineAttrs( node, object );
+2
View File
@@ -633,6 +633,7 @@ namespace glabels
Qt::Alignment textHAlign = XmlUtil::getAlignmentAttr( node, "align", Qt::AlignLeft );
Qt::Alignment textVAlign = XmlUtil::getAlignmentAttr( node, "valign", Qt::AlignTop );
QTextOption::WrapMode textWrapMode = XmlUtil::getWrapModeAttr( node, "wrap", QTextOption::WordWrap );
bool textAutoShrink = XmlUtil::getBoolAttr( node, "auto_shrink", false );
/* affine attrs */
double a[6];
@@ -682,6 +683,7 @@ namespace glabels
text,
fontFamily, fontSize, fontWeight, fontItalicFlag, fontUnderlineFlag,
textColorNode, textHAlign, textVAlign, textWrapMode, textLineSpacing,
textAutoShrink,
QMatrix( a[0], a[1], a[2], a[3], a[4], a[5] ),
shadowState, shadowX, shadowY, shadowOpacity, shadowColorNode );
}