diff --git a/panda/src/text/textAssembler.cxx b/panda/src/text/textAssembler.cxx index cb6965c644..c468bb8918 100644 --- a/panda/src/text/textAssembler.cxx +++ b/panda/src/text/textAssembler.cxx @@ -636,7 +636,7 @@ calc_width(wchar_t character, const TextProperties &properties) { // A space is a special case. TextFont *font = properties.get_font(); nassertr(font != (TextFont *)NULL, 0.0f); - return font->get_space_advance(); + return font->get_space_advance() * properties.get_glyph_scale() * properties.get_text_scale(); } bool got_glyph; @@ -659,7 +659,7 @@ calc_width(wchar_t character, const TextProperties &properties) { advance += second_glyph->get_advance(); } - glyph_scale *= properties.get_glyph_scale(); + glyph_scale *= properties.get_glyph_scale() * properties.get_text_scale(); return advance * glyph_scale; } @@ -672,7 +672,7 @@ calc_width(wchar_t character, const TextProperties &properties) { float TextAssembler:: calc_width(const TextGraphic *graphic, const TextProperties &properties) { LVecBase4f frame = graphic->get_frame(); - return (frame[1] - frame[0]) * properties.get_glyph_scale(); + return (frame[1] - frame[0]) * properties.get_glyph_scale() * properties.get_text_scale(); } #ifndef CPPPARSER // interrogate has a bit of trouble with wstring. @@ -1079,11 +1079,10 @@ assemble_paragraph(TextAssembler::PlacedGlyphs &placed_glyphs) { // First, assemble all the glyphs of this row. PlacedGlyphs row_placed_glyphs; - float row_width, line_height; + float row_width, line_height, wordwrap; TextProperties::Alignment align; assemble_row(row, row_placed_glyphs, - row_width, line_height, align); - + row_width, line_height, align, wordwrap); // Now move the row to its appropriate position. This might // involve a horizontal as well as a vertical translation. LMatrix4f mat = LMatrix4f::ident_mat(); @@ -1100,20 +1099,23 @@ assemble_paragraph(TextAssembler::PlacedGlyphs &placed_glyphs) { _lr[1] = ypos - 0.2f * line_height; // Apply the requested horizontal alignment to the row. + //[fabius] now we put wordwrap into the calculations float xpos; switch (align) { case TextProperties::A_left: xpos = 0.0f; - _lr[0] = max(_lr[0], row_width); + _lr[0] = max(_lr[0], max(row_width, wordwrap)); break; case TextProperties::A_right: xpos = -row_width; + if (wordwrap > row_width) xpos += wordwrap; _ul[0] = min(_ul[0], xpos); break; case TextProperties::A_center: xpos = -0.5f * row_width; + if (wordwrap > row_width) xpos += (wordwrap * 0.5f); _ul[0] = min(_ul[0], xpos); _lr[0] = max(_lr[0], -xpos); break; @@ -1153,7 +1155,7 @@ void TextAssembler:: assemble_row(TextAssembler::TextRow &row, TextAssembler::PlacedGlyphs &row_placed_glyphs, float &row_width, float &line_height, - TextProperties::Alignment &align) { + TextProperties::Alignment &align, float &wordwrap) { Thread *current_thread = Thread::get_current_thread(); line_height = 0.0f; @@ -1189,6 +1191,11 @@ assemble_row(TextAssembler::TextRow &row, // We get the row's alignment property from that of the last // character to be placed in the row (or the newline character). + //[fabius] differently as stated above this is not a sure way to get it so we'll set it as soon as we found it + if ( + (align == TextProperties::A_left) && + (properties->get_align() != TextProperties::A_left) + ) align = properties->get_align(); // And the height of the row is the maximum of all the fonts used @@ -1197,7 +1204,8 @@ assemble_row(TextAssembler::TextRow &row, LVecBase4f frame = graphic->get_frame(); line_height = max(line_height, frame[3] - frame[2]); } else { - line_height = max(line_height, font->get_line_height()); + //[fabius] this is not the right place to calc line height (see below) +// line_height = max(line_height, font->get_line_height()); } if (character == ' ') { @@ -1220,7 +1228,7 @@ assemble_row(TextAssembler::TextRow &row, placement->_graphic_model = graphic->get_model().node(); LVecBase4f frame = graphic->get_frame(); - float glyph_scale = properties->get_glyph_scale(); + float glyph_scale = properties->get_glyph_scale() * properties->get_text_scale(); float advance = (frame[1] - frame[0]); @@ -1294,8 +1302,11 @@ assemble_row(TextAssembler::TextRow &row, advance += second_glyph->get_advance(); } - glyph_scale *= properties->get_glyph_scale(); - + glyph_scale *= properties->get_glyph_scale() * properties->get_text_scale(); + //[fabius] a good place to take wordwrap size + if (properties->get_wordwrap() > 0.0f) { + wordwrap = properties->get_wordwrap(); + } // Now compute the matrix that will transform the glyph (or // glyphs) into position. LMatrix4f glyph_xform = LMatrix4f::scale_mat(glyph_scale); @@ -1350,6 +1361,8 @@ assemble_row(TextAssembler::TextRow &row, placement->_properties = properties; xpos += advance * glyph_scale; + //[fabius] the line height is calculated char by char here + line_height = max(line_height, font->get_line_height()*glyph_scale); } } @@ -1369,8 +1382,9 @@ assemble_row(TextAssembler::TextRow &row, TextFont *font = properties->get_font(); nassertv(font != (TextFont *)NULL); - align = properties->get_align(); - line_height = max(line_height, font->get_line_height()); + //[fabius] not here but above +/* align = properties->get_align(); + line_height = max(line_height, font->get_line_height());*/ } } diff --git a/panda/src/text/textAssembler.h b/panda/src/text/textAssembler.h index 47e754de0d..b2f83ab3f9 100644 --- a/panda/src/text/textAssembler.h +++ b/panda/src/text/textAssembler.h @@ -195,7 +195,7 @@ private: void assemble_row(TextRow &row, PlacedGlyphs &row_placed_glyphs, float &row_width, float &line_height, - TextProperties::Alignment &align); + TextProperties::Alignment &align, float &wordwrap); // These interfaces are for implementing cheesy accent marks and // ligatures when the font doesn't support them. diff --git a/panda/src/text/textProperties.I b/panda/src/text/textProperties.I index b0414ea232..7f27c786c5 100644 --- a/panda/src/text/textProperties.I +++ b/panda/src/text/textProperties.I @@ -850,9 +850,18 @@ get_tab_width() const { // Function: TextProperties::set_glyph_scale // Access: Published // Description: Specifies the factor by which to scale each letter of -// the text as it is placed. This can be used (possibly -// in conjunction with set_glyph_shift()) to implement -// superscripting or subscripting. +// the text as it is placed, in addition to any scales +// inherited from the node or from set_text_scale(). +// This can be used (possibly in conjunction with +// set_glyph_shift()) to implement superscripting or +// subscripting. +// +// The glyph scale is cumulative when applied to nested +// TextProperties. It is intended primarily for +// implementing superscripts, not for scaling the text +// in general. See also set_text_scale(), which is +// intended primarily for scaling the text in general, +// and is not cumulative. //////////////////////////////////////////////////////////////////// INLINE void TextProperties:: set_glyph_scale(float glyph_scale) { @@ -937,3 +946,52 @@ INLINE float TextProperties:: get_glyph_shift() const { return _glyph_shift; } + +//////////////////////////////////////////////////////////////////// +// Function: TextProperties::set_text_scale +// Access: Published +// Description: Specifies the factor by which to scale the text, in +// addition to any scalings imposed by the node, as well +// as in addition to the glyph scale. +// +// The text scale is not cumulative when applied to +// nested TextProperties. See also set_glyph_scale(), +// which is cumulative. +//////////////////////////////////////////////////////////////////// +INLINE void TextProperties:: +set_text_scale(float text_scale) { + _text_scale = text_scale; + _specified |= F_has_text_scale; +} + +//////////////////////////////////////////////////////////////////// +// Function: TextProperties::clear_text_scale +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void TextProperties:: +clear_text_scale() { + _specified &= ~F_has_text_scale; + _text_scale = 0.0f; +} + +//////////////////////////////////////////////////////////////////// +// Function: TextProperties::has_text_scale +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool TextProperties:: +has_text_scale() const { + return (_specified & F_has_text_scale) != 0; +} + +//////////////////////////////////////////////////////////////////// +// Function: TextProperties::get_text_scale +// Access: Published +// Description: Returns the scale factor of the text as specified +// by set_text_scale(). +//////////////////////////////////////////////////////////////////// +INLINE float TextProperties:: +get_text_scale() const { + return _text_scale; +} diff --git a/panda/src/text/textProperties.cxx b/panda/src/text/textProperties.cxx index 369e41daa6..f1a050e084 100644 --- a/panda/src/text/textProperties.cxx +++ b/panda/src/text/textProperties.cxx @@ -50,6 +50,7 @@ TextProperties() { _tab_width = text_tab_width; _glyph_scale = 1.0f; _glyph_shift = 0.0f; + _text_scale = 1.0f; } //////////////////////////////////////////////////////////////////// @@ -89,6 +90,7 @@ operator = (const TextProperties ©) { _tab_width = copy._tab_width; _glyph_scale = copy._glyph_scale; _glyph_shift = copy._glyph_shift; + _text_scale = copy._text_scale; } //////////////////////////////////////////////////////////////////// @@ -159,6 +161,9 @@ operator == (const TextProperties &other) const { if ((_specified & F_has_glyph_shift) && _glyph_shift != other._glyph_shift) { return false; } + if ((_specified & F_has_text_scale) && _text_scale != other._text_scale) { + return false; + } return true; } @@ -227,15 +232,19 @@ add_properties(const TextProperties &other) { set_tab_width(other.get_tab_width()); } - // The glyph scale and shift are a special case: rather than - // replacing the previous value, they modify it. + // replacing the previous value, they modify it, so that they apply + // cumulatively to nested TextProperties. if (other.has_glyph_shift()) { set_glyph_shift(other.get_glyph_shift() * get_glyph_scale() + get_glyph_shift()); } if (other.has_glyph_scale()) { set_glyph_scale(other.get_glyph_scale() * get_glyph_scale()); } + + if (other.has_text_scale()) { + set_text_scale(other.get_text_scale()); + } } @@ -344,6 +353,11 @@ write(ostream &out, int indent_level) const { indent(out, indent_level) << "glyph shift is " << get_glyph_shift() << "\n"; } + + if (has_text_scale()) { + indent(out, indent_level) + << "text scale is " << get_text_scale() << "\n"; + } } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/text/textProperties.h b/panda/src/text/textProperties.h index a7682d84cf..46587d96f9 100644 --- a/panda/src/text/textProperties.h +++ b/panda/src/text/textProperties.h @@ -157,6 +157,11 @@ PUBLISHED: INLINE bool has_glyph_shift() const; INLINE float get_glyph_shift() const; + INLINE void set_text_scale(float text_scale); + INLINE void clear_text_scale(); + INLINE bool has_text_scale() const; + INLINE float get_text_scale() const; + void add_properties(const TextProperties &other); void write(ostream &out, int indent_level = 0) const; @@ -183,6 +188,7 @@ private: F_has_glyph_shift = 0x00008000, F_has_underscore = 0x00010000, F_has_underscore_height = 0x00020000, + F_has_text_scale = 0x00040000, }; int _specified; @@ -205,6 +211,7 @@ private: float _tab_width; float _glyph_scale; float _glyph_shift; + float _text_scale; static PT(TextFont) _default_font; static bool _loaded_default_font;