diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2023-03-23 11:24:30 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2023-03-26 15:07:39 +0000 |
commit | 628275acb1b9652e65b8c5c013549dce5ad6f5bf (patch) | |
tree | 1678c423d242e0899cf73a2ab23f37faafd35232 /svx/source/svdraw | |
parent | a5fde5d749d27b0da216da4412f8b750178b1b08 (diff) |
tdf#90407 Change the auto-fit alg. to match better with OOXML
The auto-fit algorithm has been tweaked to be more in-line with
the expectations of OOXML. This means a couple of changes to what
properties are scaled by the algorithm have been made:
- most properties that influence the X axis position or size (for
example indent) are not scaled down or changed by scaling.
- properties that influence y axis position and size are scaled
by a separate parameter (like in the OOXML). This is used in the
auto-fit algorithm in a different way.
- if line spacing is proportional, it is now scaled with the
spacing parameter. Fixed line spacing doesn't get scaled.
- the main scaling X,Y parameter only scales the fonts.
- trying hard to scale the fonts to the nearest pt (point) value
With this change the scaling is much more stable than it was
before - for example it doesn't matter what the unscaled font
size is, when it is scaled down to the text box size, it (should)
always look the same (for example scaling from 32pt -> 10pt or
64pt -> 10pt or even 999pt -> 10pt).
The algorithm is also rewritten to be better at finding a fit and
is also better at find a good fit, but it can take more iterations
by doing so (there are ways to improve it however). Previous
algorithm used a linear search to converge to the best fit in less
iterations, but the issue with that was that it could in some cases
miss a solution (especially since change to floating point scaling
parameter). The new algorithm now uses a binary search - always
trying the middle of the search space.
OOXML export and import was also changed to take advantage of the
font scaling and spacing scaling parameters. The additional
scaling at export that was needed to have consistent OOXML support
was removed.
Change-Id: I8f3bb8d43a01931f18bd7ffdf8e0ba40caa73d8b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/149207
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'svx/source/svdraw')
-rw-r--r-- | svx/source/svdraw/svdotext.cxx | 137 | ||||
-rw-r--r-- | svx/source/svdraw/svdotextdecomposition.cxx | 2 | ||||
-rw-r--r-- | svx/source/svdraw/svdoutl.cxx | 2 |
3 files changed, 127 insertions, 14 deletions
diff --git a/svx/source/svdraw/svdotext.cxx b/svx/source/svdraw/svdotext.cxx index 20e54ca976ac..65f1dab7db65 100644 --- a/svx/source/svdraw/svdotext.cxx +++ b/svx/source/svdraw/svdotext.cxx @@ -43,6 +43,7 @@ #include <svx/sdtfsitm.hxx> #include <svx/sdtmfitm.hxx> #include <svx/xtextit0.hxx> +#include <svx/compatflags.hxx> #include <sdr/properties/textproperties.hxx> #include <sdr/contact/viewcontactoftextobj.hxx> #include <basegfx/tuple/b2dtuple.hxx> @@ -51,8 +52,11 @@ #include <vcl/virdev.hxx> #include <basegfx/matrix/b2dhommatrixtools.hxx> #include <sal/log.hxx> +#include <o3tl/unit_conversion.hxx> #include <o3tl/temporary.hxx> #include <unotools/configmgr.hxx> +#include <editeng/eeitem.hxx> +#include <editeng/fhgtitem.hxx> using namespace com::sun::star; @@ -908,6 +912,9 @@ void SdrTextObj::ImpSetCharStretching(SdrOutliner& rOutliner, const Size& rTextS } #endif } + + rOutliner.setRoundFontSizeToPt(false); + unsigned nLoopCount=0; bool bNoMoreLoop = false; tools::Long nXDiff0=0x7FFFFFFF; @@ -982,7 +989,7 @@ void SdrTextObj::ImpSetCharStretching(SdrOutliner& rOutliner, const Size& rTextS nY = nX; bNoMoreLoop = true; } - rOutliner.SetGlobalCharStretching(nX, nY); + rOutliner.setGlobalScale(nX, nY); nLoopCount++; Size aSiz(rOutliner.CalcTextSize()); tools::Long nXDiff = aSiz.Width() - nWantWdt; @@ -1172,7 +1179,8 @@ void SdrTextObj::ImpInitDrawOutliner( SdrOutliner& rOutl ) const nOutlinerMode = OutlinerMode::TextObject; rOutl.Init( nOutlinerMode ); - rOutl.SetGlobalCharStretching(100.0, 100.0); + rOutl.setGlobalScale(100.0, 100.0, 100.0, 100.0); + EEControlBits nStat=rOutl.GetControlWord(); nStat &= ~EEControlBits(EEControlBits::STRETCHING|EEControlBits::AUTOPAGESIZE); rOutl.SetControlWord(nStat); @@ -1230,15 +1238,26 @@ void SdrTextObj::ImpSetupDrawOutlinerForPaint( bool bContourFrame, } } -sal_uInt16 SdrTextObj::GetFontScaleY() const +double SdrTextObj::GetFontScale() const { SdrOutliner& rOutliner = ImpGetDrawOutliner(); // This eventually calls ImpAutoFitText UpdateOutlinerFormatting(rOutliner, o3tl::temporary(tools::Rectangle())); - double nStretchY; - rOutliner.GetGlobalCharStretching(o3tl::temporary(double()), nStretchY); - return nStretchY; + double fScaleY; + rOutliner.getGlobalScale(o3tl::temporary(double()), fScaleY, o3tl::temporary(double()), o3tl::temporary(double())); + return fScaleY; +} + +double SdrTextObj::GetSpacingScale() const +{ + SdrOutliner& rOutliner = ImpGetDrawOutliner(); + // This eventually calls ImpAutoFitText + UpdateOutlinerFormatting(rOutliner, o3tl::temporary(tools::Rectangle())); + + double fSpacingScaleY; + rOutliner.getGlobalScale(o3tl::temporary(double()), o3tl::temporary(double()), o3tl::temporary(double()), fSpacingScaleY); + return fSpacingScaleY; } void SdrTextObj::ImpAutoFitText( SdrOutliner& rOutliner ) const @@ -1253,6 +1272,12 @@ void SdrTextObj::ImpAutoFitText( SdrOutliner& rOutliner ) const void SdrTextObj::ImpAutoFitText(SdrOutliner& rOutliner, const Size& rTextSize, bool bIsVerticalWriting) const { + if (!bIsVerticalWriting) + { + autoFitTextForCompatibility(rOutliner, rTextSize); + return; + } + // EditEngine formatting is unstable enough for // line-breaking text that we need some more samples @@ -1260,6 +1285,7 @@ void SdrTextObj::ImpAutoFitText(SdrOutliner& rOutliner, const Size& rTextSize, double nMinStretchX = 0.0; double nMinStretchY = 0.0; std::array<sal_Int32, 10> aOldStretchXVals = {0,0,0,0,0,0,0,0,0,0}; + rOutliner.setRoundFontSizeToPt(false); for (size_t i = 0; i < aOldStretchXVals.size(); ++i) { const Size aCurrTextSize = rOutliner.CalcTextSizeNTP(); @@ -1283,7 +1309,7 @@ void SdrTextObj::ImpAutoFitText(SdrOutliner& rOutliner, const Size& rTextSize, fFactor = std::sqrt(fFactor); double nCurrStretchX, nCurrStretchY; - rOutliner.GetGlobalCharStretching(nCurrStretchX, nCurrStretchY); + rOutliner.getGlobalScale(nCurrStretchX, nCurrStretchY, o3tl::temporary(double()), o3tl::temporary(double())); if (fFactor >= 0.98) { @@ -1302,20 +1328,107 @@ void SdrTextObj::ImpAutoFitText(SdrOutliner& rOutliner, const Size& rTextSize, nCurrStretchX = double(basegfx::fround(nCurrStretchX * fFactor * 100.0)) / 100.00; nCurrStretchY = double(basegfx::fround(nCurrStretchY * fFactor * 100.0)) / 100.00; - rOutliner.SetGlobalCharStretching(std::min(100.0, nCurrStretchX), std::min(100.0, nCurrStretchY)); + double nStretchX = std::min(100.0, nCurrStretchX); + double nStretchY = std::min(100.0, nCurrStretchY); + + rOutliner.setGlobalScale(nStretchX, nStretchY, nStretchX, nStretchY); SAL_INFO("svx", "zoom is " << nCurrStretchX); } } const SdrTextFitToSizeTypeItem& rItem = GetObjectItem(SDRATTR_TEXT_FITTOSIZE); - if (rItem.GetMaxScale() > 0) + if (rItem.GetMaxScale() > 0.0) { - nMinStretchX = std::min<sal_uInt16>(rItem.GetMaxScale(), nMinStretchX); - nMinStretchY = std::min<sal_uInt16>(rItem.GetMaxScale(), nMinStretchY); + nMinStretchX = std::min(rItem.GetMaxScale(), nMinStretchX); + nMinStretchY = std::min(rItem.GetMaxScale(), nMinStretchY); } SAL_INFO("svx", "final zoom is " << nMinStretchX); - rOutliner.SetGlobalCharStretching(std::min(100.0, nMinStretchX), std::min(100.0, nMinStretchY)); + + nMinStretchX = std::min(100.0, nMinStretchX); + nMinStretchY = std::min(100.0, nMinStretchY); + + rOutliner.setGlobalScale(nMinStretchX, nMinStretchY, nMinStretchX, nMinStretchY); +} + +void SdrTextObj::autoFitTextForCompatibility(SdrOutliner& rOutliner, const Size& rTextBoxSize) const +{ + rOutliner.setRoundFontSizeToPt(true); + + const SdrTextFitToSizeTypeItem& rItem = GetObjectItem(SDRATTR_TEXT_FITTOSIZE); + double fMaxScale = rItem.GetMaxScale(); + if (fMaxScale > 0.0) + { + rOutliner.setGlobalScale(fMaxScale, fMaxScale, 100.0, 100.0); + } + else + { + fMaxScale = 100.0; + } + + Size aCurrentTextBoxSize = rOutliner.CalcTextSizeNTP(); + tools::Long nExtendTextBoxBy = -50; + aCurrentTextBoxSize.extendBy(0, nExtendTextBoxBy); + double fCurrentFitFactor = double(rTextBoxSize.Height()) / aCurrentTextBoxSize.Height(); + + if (fCurrentFitFactor >= 1.0) + return; + + sal_Int32 nFontHeight = GetObjectItemSet().Get(EE_CHAR_FONTHEIGHT).GetHeight(); + + double fFontHeightPt = o3tl::convert(double(nFontHeight), o3tl::Length::mm100, o3tl::Length::pt); + double fMinY = 0.0; + double fMaxY = fMaxScale; + + double fBestFontScale = 0.0; + double fBestSpacing = fMaxScale; + double fBestFitFactor = fCurrentFitFactor; + + double fInTheMidle = 0.5; + + int iteration = 0; + double fFitFactorTarget = 1.00; + + while (iteration < 10) + { + iteration++; + double fScaleY = fMinY + (fMaxY - fMinY) * fInTheMidle; + + double fScaledFontHeight = fFontHeightPt * (fScaleY / 100.0); + double fRoundedScaledFontHeight = std::floor(fScaledFontHeight * 10.0) / 10.0; + double fCurrentFontScale = (fRoundedScaledFontHeight / fFontHeightPt) * 100.0; + + fCurrentFitFactor = 0.0; // reset fit factor; + + for (double fCurrentSpacing : {100.0, 90.0, 80.0}) + { + if (fCurrentFitFactor >= fFitFactorTarget) + continue; + + rOutliner.setGlobalScale(fCurrentFontScale, fCurrentFontScale, 100.0, fCurrentSpacing); + + aCurrentTextBoxSize = rOutliner.CalcTextSizeNTP(); + aCurrentTextBoxSize.extendBy(0, nExtendTextBoxBy); + fCurrentFitFactor = double(rTextBoxSize.Height()) / aCurrentTextBoxSize.Height(); + + if (fCurrentSpacing == 100.0) + { + if (fCurrentFitFactor > fFitFactorTarget) + fMinY = fCurrentFontScale; + else + fMaxY = fCurrentFontScale; + } + + if ((fBestFitFactor < fFitFactorTarget && fCurrentFitFactor > fBestFitFactor) + || (fCurrentFitFactor >= fFitFactorTarget && fCurrentFitFactor < fBestFitFactor)) + { + fBestFontScale = fCurrentFontScale; + fBestSpacing = fCurrentSpacing; + fBestFitFactor = fCurrentFitFactor; + } + } + } + rOutliner.setGlobalScale(fBestFontScale, fBestFontScale, 100.0, fBestSpacing); } void SdrTextObj::SetupOutlinerFormatting( SdrOutliner& rOutl, tools::Rectangle& rPaintRect ) const diff --git a/svx/source/svdraw/svdotextdecomposition.cxx b/svx/source/svdraw/svdotextdecomposition.cxx index ad6edb104e01..ddbc6bfe540a 100644 --- a/svx/source/svdraw/svdotextdecomposition.cxx +++ b/svx/source/svdraw/svdotextdecomposition.cxx @@ -1226,7 +1226,7 @@ void SdrTextObj::impDecomposeStretchTextPrimitive( // to layout without mirroring const double fScaleX(fabs(aScale.getX()) / aOutlinerScale.getX()); const double fScaleY(fabs(aScale.getY()) / aOutlinerScale.getY()); - rOutliner.SetGlobalCharStretching(fScaleX * 100.0, fScaleY * 100.0); + rOutliner.setGlobalScale(fScaleX * 100.0, fScaleY * 100.0, 100.0, 100.0); // When mirroring in X and Y, // move the null point which was top left to bottom right. diff --git a/svx/source/svdraw/svdoutl.cxx b/svx/source/svdraw/svdoutl.cxx index 5e89f5d42874..94f73bfbf206 100644 --- a/svx/source/svdraw/svdoutl.cxx +++ b/svx/source/svdraw/svdoutl.cxx @@ -50,7 +50,7 @@ void SdrOutliner::SetTextObj( const SdrTextObj* pObj ) nOutlinerMode2 = OutlinerMode::TextObject; Init( nOutlinerMode2 ); - SetGlobalCharStretching(); + setGlobalScale(100.0, 100.0, 100.0, 100.0); EEControlBits nStat = GetControlWord(); nStat &= ~EEControlBits( EEControlBits::STRETCHING | EEControlBits::AUTOPAGESIZE ); |