corrected code from astelix, a.k.a. fabius

This commit is contained in:
David Rose 2008-10-24 21:48:12 +00:00
parent 9c547793a1
commit 37ce4c9329
5 changed files with 132 additions and 19 deletions

View File

@ -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,6 +1099,8 @@ assemble_paragraph(TextAssembler::PlacedGlyphs &placed_glyphs) {
_lr[1] = ypos - 0.2f * line_height;
// Apply the requested horizontal alignment to the row.
//[fabius] added a different concept of text alignement based upon a boxed region where his width is defined by the wordwrap size with the upper left corner starting from 0,0,0
// if the wordwrap size is unspecified the alignement could eventually result wrong.
float xpos;
switch (align) {
case TextProperties::A_left:
@ -1117,6 +1118,24 @@ assemble_paragraph(TextAssembler::PlacedGlyphs &placed_glyphs) {
_ul[0] = min(_ul[0], xpos);
_lr[0] = max(_lr[0], -xpos);
break;
case TextProperties::A_boxed_left:
xpos = 0.0f;
_lr[0] = max(_lr[0], max(row_width, wordwrap));
break;
case TextProperties::A_boxed_right:
xpos = -row_width;
if (wordwrap > row_width) xpos += wordwrap;
_ul[0] = min(_ul[0], xpos);
break;
case TextProperties::A_boxed_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;
}
mat.set_row(3, LVector3f(xpos, 0.0f, ypos));
@ -1153,7 +1172,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 +1208,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 +1221,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 +1245,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 +1319,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 +1378,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 +1399,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());*/
}
}

View File

@ -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.

View File

@ -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;
}

View File

@ -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 &copy) {
_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";
}
}
////////////////////////////////////////////////////////////////////

View File

@ -49,6 +49,9 @@ PUBLISHED:
A_left,
A_right,
A_center,
A_boxed_left,
A_boxed_right,
A_boxed_center
};
TextProperties();
@ -157,6 +160,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 +191,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 +214,7 @@ private:
float _tab_width;
float _glyph_scale;
float _glyph_shift;
float _text_scale;
static PT(TextFont) _default_font;
static bool _loaded_default_font;