From 031a0b376e833dfe0c8b72de4024c67931d8b597 Mon Sep 17 00:00:00 2001 From: twanvl Date: Tue, 19 Dec 2006 16:46:30 +0000 Subject: [PATCH] alignment of text git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@125 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/render/text/viewer.cpp | 71 ++++++++++++++++++++++++++++++++++---- src/render/text/viewer.hpp | 5 ++- 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/render/text/viewer.cpp b/src/render/text/viewer.cpp index dc4f1267..c89e62bb 100644 --- a/src/render/text/viewer.cpp +++ b/src/render/text/viewer.cpp @@ -220,14 +220,17 @@ void TextViewer::prepareElements(const String& text, const TextStyle& style, Con void TextViewer::prepareLines(RotatedDC& dc, const String& text, const TextStyle& style) { scale = 1; - prepareLinesScale(dc, text, style, false); -} - -bool TextViewer::prepareLinesScale(RotatedDC& dc, const String& text, const TextStyle& style, bool stop_if_too_long) { - // Try to layout the text at the current scale // find character sizes vector chars; elements.getCharInfo(dc, scale, 0, text.size(), chars); + // try to layout + prepareLinesScale(dc, chars, style, false); + // align + alignLines(dc, chars, style); +} + +bool TextViewer::prepareLinesScale(RotatedDC& dc, const vector& chars, const TextStyle& style, bool stop_if_too_long) { + // Try to layout the text at the current scale // first line lines.clear(); Line line; @@ -240,7 +243,7 @@ bool TextViewer::prepareLinesScale(RotatedDC& dc, const String& text, const Text size_t word_start = 0; // For each character ... for(size_t i = 0 ; i < chars.size() ; ++i) { - CharInfo& c = chars[i]; + const CharInfo& c = chars[i]; // Should we break? bool break_now = false; bool accept_word = false; // the current word should be added to the line @@ -342,5 +345,59 @@ double TextViewer::lineRight(RotatedDC& dc, const TextStyle& style, double y) { return style.width - style.padding_right; // return style.mask.rowRight(y, dc.getInternalSize()) - style.padding_right; } + ContourMask::ContourMask() {} // MOVEME //@@ -ContourMask::~ContourMask() {} \ No newline at end of file +ContourMask::~ContourMask() {} + +void TextViewer::alignLines(RotatedDC& dc, const vector& chars, const TextStyle& style) { + if (style.alignment == ALIGN_TOP_LEFT) return; + // Find height of the text, don't count the last lines if they are empty + double height = 0; + FOR_EACH_REVERSE(l, lines) { + height = l.top + l.line_height; + if (l.line_height) break; // not an empty line + } + // amount to shift all lines vertically + RealSize s = dc.getInternalSize(); + double vdelta = align_delta_y(style.alignment, s.height, height); + // align all lines + FOR_EACH(l, lines) { + l.top += vdelta; + // amount to shift all characters horizontally + double width = l.positions.back(); + if ((style.alignment & ALIGN_JUSTIFY) || + (style.alignment & ALIGN_JUSTIFY_OVERFLOW && width > s.width)) { + // justify text +// justifying = true; + double hdelta = s.width - width; // amount of space to distribute + int count = (int)l.positions.size() - 1; // distribute it among this many characters + if (count == 0) count = 1; // prevent div by 0 + int i = 0; + FOR_EACH(c, l.positions) { + c += hdelta * i++ / count; + } + } else if (style.alignment & ALIGN_JUSTIFY) { + // justify text, by words +// justifying = true; + double hdelta = s.width - width; // amount of space to distribute + int count = 0; // distribute it among this many words + for (size_t k = l.start + 1 ; k < l.end() - 1 ; ++k) { + if (chars[k].break_after == BREAK_SOFT) ++count; + } + if (count == 0) count = 1; // prevent div by 0 + int i = 0; size_t j = l.start; + FOR_EACH(c, l.positions) { + c += hdelta * i / count; + if (j < l.end() && chars[j++].break_after == BREAK_SOFT) i++; + } + } else { + // simple alignment +// justifying = false; + double hdelta = align_delta_x(style.alignment, s.width, width); + FOR_EACH(c, l.positions) { + c += hdelta; + } + } + } + // TODO : work well with mask +} diff --git a/src/render/text/viewer.hpp b/src/render/text/viewer.hpp index 3840ea19..325f5119 100644 --- a/src/render/text/viewer.hpp +++ b/src/render/text/viewer.hpp @@ -104,7 +104,10 @@ class TextViewer { /// Prepare the lines, layout the text void prepareLines(RotatedDC& dc, const String& text, const TextStyle& style); /// Prepare the lines, layout the text; at a specific scale - bool prepareLinesScale(RotatedDC& dc, const String& text, const TextStyle& style, bool stop_if_too_long); + bool prepareLinesScale(RotatedDC& dc, const vector& chars, const TextStyle& style, bool stop_if_too_long); + /// Align the lines within the textbox + void alignLines(RotatedDC& dc, const vector& chars, const TextStyle& style); + /// Find the line the given index is on, returns the first line if the index is not found const Line& findLine(size_t index) const;