From 1ab6b0cb82d0248c845ba969089b66acca920e67 Mon Sep 17 00:00:00 2001 From: twanvl Date: Sun, 24 Dec 2006 16:04:24 +0000 Subject: [PATCH] text scaling git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@168 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/render/text/compound.cpp | 3 +++ src/render/text/element.cpp | 7 +++++++ src/render/text/element.hpp | 31 +++++++------------------------ src/render/text/font.cpp | 3 +++ src/render/text/symbol.cpp | 3 +++ src/render/text/viewer.cpp | 32 ++++++++++++++++++++++++-------- src/util/real_point.hpp | 22 ++-------------------- 7 files changed, 49 insertions(+), 52 deletions(-) diff --git a/src/render/text/compound.cpp b/src/render/text/compound.cpp index 274e1b64..0d06011f 100644 --- a/src/render/text/compound.cpp +++ b/src/render/text/compound.cpp @@ -19,6 +19,9 @@ void CompoundTextElement::getCharInfo(RotatedDC& dc, double scale, vectorscaleStep()); + } + return m; +} // Helper class for TextElements::fromString, to allow persistent formating state accross recusive calls struct TextElementsFromString { diff --git a/src/render/text/element.hpp b/src/render/text/element.hpp index 348b1bc5..a7a30096 100644 --- a/src/render/text/element.hpp +++ b/src/render/text/element.hpp @@ -64,30 +64,8 @@ class TextElement { virtual void getCharInfo(RotatedDC& dc, double scale, vector& out) const = 0; /// Return the minimum scale factor allowed (starts at 1) virtual double minScale() const = 0; -/* - // draw the section { void getCharInfo(RotatedDC& dc, double scale, size_t start, size_t end, vector& out) const; /// Return the minimum scale factor allowed by all elements double minScale() const; + /// Return the steps the scale factor should take + double scaleStep() const; /// The actual elements /** They must be in order of positions and not overlap, i.e. @@ -134,6 +114,7 @@ class FontTextElement : public SimpleTextElement { virtual void draw (RotatedDC& dc, double scale, const RealRect& rect, const double* xs, DrawWhat what, size_t start, size_t end) const; virtual void getCharInfo(RotatedDC& dc, double scale, vector& out) const; virtual double minScale() const; + virtual double scaleStep() const; private: FontP font; DrawWhat draw_as; @@ -151,6 +132,7 @@ class SymbolTextElement : public SimpleTextElement { virtual void draw (RotatedDC& dc, double scale, const RealRect& rect, const double* xs, DrawWhat what, size_t start, size_t end) const; virtual void getCharInfo(RotatedDC& dc, double scale, vector& out) const; virtual double minScale() const; + virtual double scaleStep() const; private: const SymbolFontRef& font; // owned by TextStyle Context& ctx; @@ -166,6 +148,7 @@ class CompoundTextElement : public TextElement { virtual void draw (RotatedDC& dc, double scale, const RealRect& rect, const double* xs, DrawWhat what, size_t start, size_t end) const; virtual void getCharInfo(RotatedDC& dc, double scale, vector& out) const; virtual double minScale() const; + virtual double scaleStep() const; TextElements elements; ///< the elements }; diff --git a/src/render/text/font.cpp b/src/render/text/font.cpp index 998a77ca..2fa531b6 100644 --- a/src/render/text/font.cpp +++ b/src/render/text/font.cpp @@ -51,3 +51,6 @@ void FontTextElement::getCharInfo(RotatedDC& dc, double scale, vector& double FontTextElement::minScale() const { return min(font->size, font->scale_down_to) / max(0.01, font->size); } +double FontTextElement::scaleStep() const { + return 1. / max(font->size, 1.); +} diff --git a/src/render/text/symbol.cpp b/src/render/text/symbol.cpp index 421a0bdf..6182986c 100644 --- a/src/render/text/symbol.cpp +++ b/src/render/text/symbol.cpp @@ -26,3 +26,6 @@ void SymbolTextElement::getCharInfo(RotatedDC& dc, double scale, vector chars; // try to layout, at different scales + vector chars; scale = 1; -// double min_scale = elements.minScale(); -// while - chars.clear(); - elements.getCharInfo(dc, scale, 0, text.size(), chars); - prepareLinesScale(dc, chars, style, false); + double min_scale = elements.minScale(); + double scale_step = max(0.1,elements.scaleStep()); + while (true) { + double next_scale = scale - scale_step; + bool last = next_scale < min_scale; + // fits? + chars.clear(); + elements.getCharInfo(dc, scale, 0, text.size(), chars); + bool fits = prepareLinesScale(dc, chars, style, last); + if (fits && (lines.empty() || lines.back().bottom() <= dc.getInternalSize().height - style.padding_bottom)) { + break; // text fits in box + } + if (last) break; + // TODO: smarter iteration + scale = next_scale; + } + // no text, find a dummy height for the single line we have if (lines.size() == 1 && lines[0].width() < 0.0001) { if (style.always_symbol && style.symbol_font.valid()) { @@ -322,8 +333,10 @@ void TextViewer::prepareLines(RotatedDC& dc, const String& text, const TextStyle lines[0].line_height = dc.GetTextExtent(_(" ")).height; } } + // align alignLines(dc, chars, style); + // HACK : fix empty first line before , do this after align, so layout is not affected if (lines.size() > 1 && lines[0].line_height == 0) { dc.SetFont(style.font.font); @@ -338,6 +351,7 @@ bool TextViewer::prepareLinesScale(RotatedDC& dc, const vector& chars, // first line lines.clear(); Line line; + line.top = style.padding_top; // size of the line so far RealSize line_size(lineLeft(dc, style, 0), 0); line.positions.push_back(line_size.width); @@ -462,7 +476,9 @@ void TextViewer::alignLines(RotatedDC& dc, const vector& chars, const if (l.line_height) break; // not an empty line } // amount to shift all lines vertically - RealSize s = dc.getInternalSize(); + RealSize s = addDiagonal( + dc.getInternalSize(), + -RealSize(style.padding_left+style.padding_right, style.padding_top + style.padding_bottom)); double vdelta = align_delta_y(style.alignment, s.height, height); // align all lines FOR_EACH(l, lines) { diff --git a/src/util/real_point.hpp b/src/util/real_point.hpp index 426c761e..d9ea76f9 100644 --- a/src/util/real_point.hpp +++ b/src/util/real_point.hpp @@ -40,29 +40,11 @@ class RealSize { : width(s.GetWidth()), height(s.GetHeight()) {} - /// Addition of two sizes -/* inline void operator += (const RealSize& s2) { - width += s2.width; - height += s2.height; - } - /// Addition of two sizes - inline RealSize operator + (const RealSize& s2) const { - return RealSize(width + s2.width, height + s2.height); - } - /// Difference of two sizes - inline void operator -= (const RealSize& s2){ - width -= s2.width; - height -= s2.height; - } - /// Difference of two sizes - inline RealSize operator - (const RealSize& s2) const { - return RealSize(width - s2.width, height - s2.height); - } - /// Inversion of a size, inverts both components + /// Negation of a size, negates both components inline RealSize operator - () const { return RealSize(-width, -height); } -*/ + /// Multiplying a size by a scalar r, multiplies both components inline void operator *= (double r) { width *= r;