summaryrefslogtreecommitdiffstats
path: root/src/libnrtype
diff options
context:
space:
mode:
authorTavmjong Bah <tavmjong@free.fr>2015-11-24 09:49:17 +0000
committertavmjong-free <tavmjong@free.fr>2015-11-24 09:49:17 +0000
commitbbaac37a5345ff1e8e8f75edf39e260ae516ac84 (patch)
tree7f0cf0b0fa58bd8dd4835c515ff31dff805dff3c /src/libnrtype
parentChange context from gradient to mesh. (diff)
parentRemove unintended change that added C++11 flag. (diff)
downloadinkscape-bbaac37a5345ff1e8e8f75edf39e260ae516ac84.tar.gz
inkscape-bbaac37a5345ff1e8e8f75edf39e260ae516ac84.zip
Merge 'writing-modes' branch.
(bzr r14487)
Diffstat (limited to 'src/libnrtype')
-rw-r--r--src/libnrtype/FontInstance.cpp238
-rw-r--r--src/libnrtype/Layout-TNG-Compute.cpp349
-rw-r--r--src/libnrtype/Layout-TNG-Input.cpp56
-rw-r--r--src/libnrtype/Layout-TNG-OutIter.cpp15
-rw-r--r--src/libnrtype/Layout-TNG-Output.cpp58
-rw-r--r--src/libnrtype/Layout-TNG-Scanline-Maker.h14
-rw-r--r--src/libnrtype/Layout-TNG-Scanline-Makers.cpp32
-rw-r--r--src/libnrtype/Layout-TNG.h103
-rw-r--r--src/libnrtype/TextWrapper.cpp4
-rw-r--r--src/libnrtype/font-instance.h33
-rw-r--r--src/libnrtype/one-box.h2
11 files changed, 613 insertions, 291 deletions
diff --git a/src/libnrtype/FontInstance.cpp b/src/libnrtype/FontInstance.cpp
index a5572c517..f0b87efa7 100644
--- a/src/libnrtype/FontInstance.cpp
+++ b/src/libnrtype/FontInstance.cpp
@@ -21,6 +21,7 @@
#include FT_BBOX_H
#include FT_TRUETYPE_TAGS_H
#include FT_TRUETYPE_TABLES_H
+#include FT_GLYPH_H
#include <pango/pangoft2.h>
#include <2geom/pathvector.h>
#include <2geom/path-sink.h>
@@ -183,6 +184,20 @@ font_instance::font_instance(void) :
theFace(0)
{
//printf("font instance born\n");
+ _ascent = _ascent_max = 0.8;
+ _descent = _descent_max = 0.2;
+ _xheight = 0.5;
+
+ // Default baseline values, alphabetic is reference
+ _baselines[ SP_CSS_BASELINE_AUTO ] = 0.0;
+ _baselines[ SP_CSS_BASELINE_ALPHABETIC ] = 0.0;
+ _baselines[ SP_CSS_BASELINE_IDEOGRAPHIC ] = -_descent;
+ _baselines[ SP_CSS_BASELINE_HANGING ] = 0.8 * _ascent;
+ _baselines[ SP_CSS_BASELINE_MATHEMATICAL ] = 0.8 * _xheight;
+ _baselines[ SP_CSS_BASELINE_CENTRAL ] = 0.5 - _descent;
+ _baselines[ SP_CSS_BASELINE_MIDDLE ] = 0.5 * _xheight;
+ _baselines[ SP_CSS_BASELINE_TEXT_BEFORE_EDGE ] = _ascent;
+ _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = -_descent;
}
font_instance::~font_instance(void)
@@ -259,6 +274,7 @@ void font_instance::InitTheFace()
FT_Select_Charmap(theFace,ft_encoding_unicode) && FT_Select_Charmap(theFace,ft_encoding_symbol);
}
#endif
+ FindFontMetrics();
}
}
@@ -374,10 +390,10 @@ void font_instance::LoadGlyph(int glyph_id)
GLYPHMETRICS metrics;
DWORD bufferSize=GetGlyphOutline (parent->hScreenDC, glyph_id, GGO_GLYPH_INDEX | GGO_NATIVE | GGO_UNHINTED, &metrics, 0, NULL, &identity);
double scale=1.0/parent->fontSize;
- n_g.h_advance=metrics.gmCellIncX*scale;
- n_g.v_advance=otm.otmTextMetrics.tmHeight*scale;
- n_g.h_width=metrics.gmBlackBoxX*scale;
- n_g.v_width=metrics.gmBlackBoxY*scale;
+ n_g.h_advance = metrics.gmCellIncX * scale;
+ n_g.v_advance = otm.otmTextMetrics.tmHeight * scale;
+ n_g.h_width = metrics.gmBlackBoxX * scale;
+ n_g.v_width = metrics.gmBlackBoxY * scale;
if ( bufferSize == GDI_ERROR) {
// shit happened
} else if ( bufferSize == 0) {
@@ -459,7 +475,13 @@ void font_instance::LoadGlyph(int glyph_id)
n_g.v_advance=((double)theFace->glyph->metrics.vertAdvance)/((double)theFace->units_per_EM);
n_g.v_width=((double)theFace->glyph->metrics.height)/((double)theFace->units_per_EM);
} else {
- n_g.v_width=n_g.v_advance=((double)theFace->height)/((double)theFace->units_per_EM);
+ // CSS3 Writing modes dictates that if vertical font metrics are missing we must
+ // synthisize them. No method is specified. The SVG 1.1 spec suggests using the em
+ // height (which is not theFace->height as that includes leading). The em height
+ // is ascender + descender (descender positive). Note: The "Requirements for
+ // Japanese Text Layout" W3C document says that Japanese kanji should be "set
+ // solid" which implies that vertical (and horizontal) advance should be 1em.
+ n_g.v_width=n_g.v_advance= 1.0;
}
if ( theFace->glyph->format == ft_glyph_format_outline ) {
FT_Outline_Funcs ft2_outline_funcs = {
@@ -501,7 +523,7 @@ void font_instance::LoadGlyph(int glyph_id)
}
}
-bool font_instance::FontMetrics(double &ascent,double &descent,double &leading)
+bool font_instance::FontMetrics(double &ascent,double &descent,double &xheight)
{
if ( pFont == NULL ) {
return false;
@@ -510,32 +532,17 @@ bool font_instance::FontMetrics(double &ascent,double &descent,double &leading)
if ( theFace == NULL ) {
return false;
}
-#ifdef USE_PANGO_WIN32
- OUTLINETEXTMETRIC otm;
- if ( !GetOutlineTextMetrics(parent->hScreenDC,sizeof(otm),&otm) ) {
- return false;
- }
- double scale=1.0/parent->fontSize;
- ascent=fabs(otm.otmAscent*scale);
- descent=fabs(otm.otmDescent*scale);
- leading=fabs(otm.otmLineGap*scale);
- //otmSubscriptSize, otmSubscriptOffset, otmSuperscriptSize, otmSuperscriptOffset,
-#else
- if ( theFace->units_per_EM == 0 ) {
- return false; // bitmap font
- }
- ascent=fabs(((double)theFace->ascender)/((double)theFace->units_per_EM));
- descent=fabs(((double)theFace->descender)/((double)theFace->units_per_EM));
- leading=fabs(((double)theFace->height)/((double)theFace->units_per_EM));
- leading-=ascent+descent;
-#endif
+
+ ascent = _ascent;
+ descent = _descent;
+ xheight = _xheight;
+
return true;
}
-bool font_instance::FontDecoration(
- double &underline_position, double &underline_thickness,
- double &linethrough_position, double &linethrough_thickness
-){
+bool font_instance::FontDecoration( double &underline_position, double &underline_thickness,
+ double &linethrough_position, double &linethrough_thickness)
+{
if ( pFont == NULL ) {
return false;
}
@@ -662,6 +669,179 @@ double font_instance::Advance(int glyph_id,bool vertical)
return 0;
}
+// Internal function to find baselines
+void font_instance::FindFontMetrics() {
+
+ // CSS2 recommends using the OS/2 values sTypoAscender and sTypoDescender for the Typographic
+ // ascender and descender values:
+ // http://www.w3.org/TR/CSS2/visudet.html#sTypoAscender
+ // On Windows, the typographic ascender and descender are taken from the otmMacAscent and
+ // otmMacDescent values:
+ // http://microsoft.public.win32.programmer.gdi.narkive.com/LV6k4BDh/msdn-documentation-outlinetextmetrics-clarification
+ // The otmAscent and otmDescent values are the maxiumum ascent and maxiumum descent of all the
+ // glyphs in a font.
+ if ( theFace ) {
+
+#ifdef USE_PANGO_WIN32
+
+ if ( GetOutlineTextMetrics(parent->hScreenDC,sizeof(otm),&otm) ) {
+ double scale=1.0/parent->fontSize;
+ _ascent = fabs(otm.otmMacAscent * scale);
+ _descent = fabs(otm.otmMacDescent * scale);
+ _xheight = fabs(otm.otmXHeight * scale);
+ _ascent_max = fabs(otm.otmAscent * scale);
+ _descent_max = fabs(otm.otmDescent * scale);
+
+ // In CSS em size is ascent + descent... which should be 1. If not,
+ // adjust so it is.
+ double em = _ascent + _descent;
+ if( em > 0 ) {
+ _ascent /= em;
+ _descent /= em;
+ }
+
+ // May not be necessary but if OS/2 table missing or not version 2 or higher,
+ // xheight might be zero.
+ if( _xheight == 0.0 ) {
+ _xheight = 0.5;
+ }
+
+ // Baselines defined relative to alphabetic.
+ _baselines[ SP_CSS_BASELINE_IDEOGRAPHIC ] = -_descent; // Recommendation
+ _baselines[ SP_CSS_BASELINE_HANGING ] = 0.8 * _ascent; // Guess
+ _baselines[ SP_CSS_BASELINE_MATHEMATICAL ] = 0.8 * _xheight; // Guess
+ _baselines[ SP_CSS_BASELINE_CENTRAL ] = 0.5 - _descent; // Definition
+ _baselines[ SP_CSS_BASELINE_MIDDLE ] = 0.5 * _xheight; // Definition
+ _baselines[ SP_CSS_BASELINE_TEXT_BEFORE_EDGE ] = _ascent; // Definition
+ _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = -_descent; // Definition
+
+
+ MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
+ GLYPHMETRICS metrics;
+ int retval;
+
+ // Better math baseline:
+ // Try center of minus sign
+ retval = GetGlyphOutline (parent->hScreenDC, 0x2212, GGO_NATIVE | GGO_UNHINTED, &metrics, 0, NULL, &identity);
+ // If no minus sign, try hyphen
+ if( retval <= 0 )
+ retval = GetGlyphOutline (parent->hScreenDC, '-', GGO_NATIVE | GGO_UNHINTED, &metrics, 0, NULL, &identity);
+
+ if( retval > 0 ) {
+ double math = (metrics.gmptGlyphOrigin.y + 0.5 * metrics.gmBlackBoxY) * scale;
+ _baselines[ SP_CSS_BASELINE_MATHEMATICAL ] = math;
+ }
+
+ // Find hanging baseline... assume it is at top of 'म'.
+ retval = GetGlyphOutline (parent->hScreenDC, 0x092E, GGO_NATIVE | GGO_UNHINTED, &metrics, 0, NULL, &identity);
+ if( retval > 0 ) {
+ double hanging = metrics.gmptGlyphOrigin.y * scale;
+ _baselines[ SP_CSS_BASELINE_MATHEMATICAL ] = hanging;
+ }
+ }
+
+#else
+
+ if ( theFace->units_per_EM != 0 ) { // If zero then it's a bitmap font.
+
+ TT_OS2* os2 = (TT_OS2*)FT_Get_Sfnt_Table( theFace, FT_SFNT_OS2 );
+ if( os2 ) {
+ _ascent = fabs(((double)os2->sTypoAscender) / ((double)theFace->units_per_EM));
+ _descent = fabs(((double)os2->sTypoDescender)/ ((double)theFace->units_per_EM));
+ } else {
+ _ascent = fabs(((double)theFace->ascender) / ((double)theFace->units_per_EM));
+ _descent = fabs(((double)theFace->descender) / ((double)theFace->units_per_EM));
+ }
+ _ascent_max = fabs(((double)theFace->ascender) / ((double)theFace->units_per_EM));
+ _descent_max = fabs(((double)theFace->descender) / ((double)theFace->units_per_EM));
+
+ // In CSS em size is ascent + descent... which should be 1. If not,
+ // adjust so it is.
+ double em = _ascent + _descent;
+ if( em > 0 ) {
+ _ascent /= em;
+ _descent /= em;
+ }
+
+ // x-height
+ if( os2 && os2->version >= 0x0002 && os2->version != 0xffffu ) {
+ // Only os/2 version 2 and above have sxHeight, 0xffff marks "old Mac fonts" without table
+ _xheight = fabs(((double)os2->sxHeight) / ((double)theFace->units_per_EM));
+ } else {
+ // Measure 'x' height in font. Recommended option by XSL standard if no sxHeight.
+ FT_UInt index = FT_Get_Char_Index( theFace, 'x' );
+ if( index != 0 ) {
+ FT_Load_Glyph( theFace, index, FT_LOAD_NO_SCALE );
+ _xheight = (fabs)(((double)theFace->glyph->metrics.height/(double)theFace->units_per_EM));
+ } else {
+ // No 'x' in font!
+ _xheight = 0.5;
+ }
+ }
+
+ // Baselines defined relative to alphabetic.
+ _baselines[ SP_CSS_BASELINE_IDEOGRAPHIC ] = -_descent; // Recommendation
+ _baselines[ SP_CSS_BASELINE_HANGING ] = 0.8 * _ascent; // Guess
+ _baselines[ SP_CSS_BASELINE_MATHEMATICAL ] = 0.8 * _xheight; // Guess
+ _baselines[ SP_CSS_BASELINE_CENTRAL ] = 0.5 - _descent; // Definition
+ _baselines[ SP_CSS_BASELINE_MIDDLE ] = 0.5 * _xheight; // Definition
+ _baselines[ SP_CSS_BASELINE_TEXT_BEFORE_EDGE ] = _ascent; // Definition
+ _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = -_descent; // Definition
+
+ // Better math baseline:
+ // Try center of minus sign
+ FT_UInt index = FT_Get_Char_Index( theFace, 0x2212 ); //'−'
+ // If no minus sign, try hyphen
+ if( index == 0 )
+ index = FT_Get_Char_Index( theFace, '-' );
+
+ if( index != 0 ) {
+ FT_Load_Glyph( theFace, index, FT_LOAD_NO_SCALE );
+ FT_Glyph aglyph;
+ FT_Get_Glyph( theFace->glyph, &aglyph );
+ FT_BBox acbox;
+ FT_Glyph_Get_CBox( aglyph, FT_GLYPH_BBOX_UNSCALED, &acbox );
+ double math = (acbox.yMin + acbox.yMax)/2.0/(double)theFace->units_per_EM;
+ _baselines[ SP_CSS_BASELINE_MATHEMATICAL ] = math;
+ // std::cout << "Math baseline: - bbox: y_min: " << acbox.yMin
+ // << " y_max: " << acbox.yMax
+ // << " math: " << math << std::endl;
+ }
+
+ // Find hanging baseline... assume it is at top of 'म'.
+ index = FT_Get_Char_Index( theFace, 0x092E ); // 'म'
+ if( index != 0 ) {
+ FT_Load_Glyph( theFace, index, FT_LOAD_NO_SCALE );
+ FT_Glyph aglyph;
+ FT_Get_Glyph( theFace->glyph, &aglyph );
+ FT_BBox acbox;
+ FT_Glyph_Get_CBox( aglyph, FT_GLYPH_BBOX_UNSCALED, &acbox );
+ double hanging = (double)acbox.yMax/(double)theFace->units_per_EM;
+ _baselines[ SP_CSS_BASELINE_HANGING ] = hanging;
+ // std::cout << "Hanging baseline: प: " << hanging << std::endl;
+ }
+ }
+#endif
+ // const gchar *family = pango_font_description_get_family(descr);
+ // std::cout << "Font: " << (family?family:"null") << std::endl;
+ // std::cout << " ascent: " << _ascent << std::endl;
+ // std::cout << " descent: " << _descent << std::endl;
+ // std::cout << " x-height: " << _xheight << std::endl;
+ // std::cout << " max ascent: " << _ascent_max << std::endl;
+ // std::cout << " max descent: " << _descent_max << std::endl;
+ // std::cout << " Baselines:" << std::endl;
+ // std::cout << " alphabetic: " << _baselines[ SP_CSS_BASELINE_ALPHABETIC ] << std::endl;
+ // std::cout << " ideographic: " << _baselines[ SP_CSS_BASELINE_IDEOGRAPHIC ] << std::endl;
+ // std::cout << " hanging: " << _baselines[ SP_CSS_BASELINE_HANGING ] << std::endl;
+ // std::cout << " math: " << _baselines[ SP_CSS_BASELINE_MATHEMATICAL ] << std::endl;
+ // std::cout << " central: " << _baselines[ SP_CSS_BASELINE_CENTRAL ] << std::endl;
+ // std::cout << " middle: " << _baselines[ SP_CSS_BASELINE_MIDDLE ] << std::endl;
+ // std::cout << " text_before: " << _baselines[ SP_CSS_BASELINE_TEXT_BEFORE_EDGE ] << std::endl;
+ // std::cout << " text_after: " << _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] << std::endl;
+ }
+}
+
+
/*
Local Variables:
mode:c++
diff --git a/src/libnrtype/Layout-TNG-Compute.cpp b/src/libnrtype/Layout-TNG-Compute.cpp
index c4b0a5bee..2ae13efa6 100644
--- a/src/libnrtype/Layout-TNG-Compute.cpp
+++ b/src/libnrtype/Layout-TNG-Compute.cpp
@@ -23,33 +23,6 @@ namespace Text {
#define TRACE(_args) IFTRACE(g_print _args)
-// ******* enum conversion tables
-
-// These enums are probably from the SVG 1.0 era where one could interpret 'writing-mode' as setting direction.
-// SVG 1.1 makes it clear that 'direction' should be used. 'direction' has only two values 'ltr' and 'rtl'.
-// The first two values for the 'writing-mode' enum just happen to match the first two value of 'direction' so the
-// existing code worked when 'writing-mode' was changed to 'direction'.
-// static Layout::EnumConversionItem const enum_convert_spstyle_direction_to_pango_direction[] = {
-// {SP_CSS_WRITING_MODE_LR_TB, PANGO_DIRECTION_LTR},
-// {SP_CSS_WRITING_MODE_RL_TB, PANGO_DIRECTION_RTL},
-// {SP_CSS_WRITING_MODE_TB_LR, PANGO_DIRECTION_LTR}}; // this is correct
-
-// static Layout::EnumConversionItem const enum_convert_spstyle_direction_to_my_direction[] = {
-// {SP_CSS_WRITING_MODE_LR_TB, Layout::LEFT_TO_RIGHT},
-// {SP_CSS_WRITING_MODE_RL_TB, Layout::RIGHT_TO_LEFT},
-// {SP_CSS_WRITING_MODE_TB_LR, Layout::LEFT_TO_RIGHT}}; // this is correct
-
-// Proper 'direction' enums
-static Layout::EnumConversionItem const enum_convert_spstyle_direction_to_pango_direction[] = {
- {SP_CSS_DIRECTION_LTR, PANGO_DIRECTION_LTR},
- {SP_CSS_DIRECTION_RTL, PANGO_DIRECTION_RTL}};
-
-static Layout::EnumConversionItem const enum_convert_spstyle_direction_to_my_direction[] = {
- {SP_CSS_DIRECTION_LTR, Layout::LEFT_TO_RIGHT},
- {SP_CSS_DIRECTION_RTL, Layout::RIGHT_TO_LEFT}};
-
-
-
/** \brief private to Layout. Does the real work of text flowing.
This class does a standard greedy paragraph wrapping algorithm.
@@ -140,9 +113,10 @@ class Layout::Calculator
unsigned input_index; /// index into Layout::_input_stream
Glib::ustring::const_iterator input_stream_first_character;
double font_size;
- LineHeight line_height; /// This is not the CSS line-height attribute!
+ FontMetrics line_height; /// This is not the CSS line-height attribute!
double line_height_multiplier; /// calculated from the font-height css property
double baseline_shift; /// calculated from the baseline-shift css property
+ SPCSSTextOrientation text_orientation;
unsigned text_bytes;
unsigned char_index_in_para; /// the index of the first character in this span in the paragraph, for looking up char_attributes
SVGLength x, y, dx, dy, rotate; // these are reoriented copies of the <tspan> attributes. We change span when we encounter one.
@@ -220,9 +194,8 @@ class Layout::Calculator
void _buildPangoItemizationForPara(ParagraphInfo *para) const;
- static void _computeFontLineHeight(font_instance *font, double font_size,
- SPStyle const *style, LineHeight *line_height,
- double *line_height_multiplier);
+ // Returns line_height_multiplier
+ static double _computeFontLineHeight( SPStyle const *style );
unsigned _buildSpansForPara(ParagraphInfo *para) const;
@@ -270,7 +243,7 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
bool _goToNextWrapShape();
bool _findChunksForLine(ParagraphInfo const &para, UnbrokenSpanPosition *start_span_pos,
- std::vector<ChunkInfo> *chunk_info, LineHeight *line_height);
+ std::vector<ChunkInfo> *chunk_info, FontMetrics *line_height);
static inline PangoLogAttr const &_charAttributes(ParagraphInfo const &para,
UnbrokenSpanPosition const &span_pos)
@@ -282,7 +255,7 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
UnbrokenSpanPosition const &start_span_pos,
ScanlineMaker::ScanRun const &scan_run,
std::vector<ChunkInfo> *chunk_info,
- LineHeight *line_height) const;
+ FontMetrics *line_height) const;
/** computes the width of a single UnbrokenSpan (pointed to by span->start.iter_span)
and outputs its vital statistics into the other fields of \a span.
@@ -300,8 +273,11 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
span->setZero();
if (span->start.iter_span->dx._set && span->start.char_byte == 0){
- if(para.direction == RIGHT_TO_LEFT){ span->width -= span->start.iter_span->dx.computed; }
- else { span->width += span->start.iter_span->dx.computed; }
+ if(para.direction == RIGHT_TO_LEFT){
+ span->width -= span->start.iter_span->dx.computed;
+ } else {
+ span->width += span->start.iter_span->dx.computed;
+ }
}
if (span->start.iter_span->pango_item_index == -1) {
@@ -382,10 +358,22 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
double char_width = 0.0;
while (span->end_glyph_index < (unsigned)span->end.iter_span->glyph_string->num_glyphs
&& span->end.iter_span->glyph_string->log_clusters[span->end_glyph_index] <= (int)span->end.char_byte) {
- if (_block_progression == LEFT_TO_RIGHT || _block_progression == RIGHT_TO_LEFT)
- char_width += span->start.iter_span->font_size * para.pango_items[span->end.iter_span->pango_item_index].font->Advance(span->end.iter_span->glyph_string->glyphs[span->end_glyph_index].glyph, true);
- else
+ if (_block_progression == LEFT_TO_RIGHT || _block_progression == RIGHT_TO_LEFT) {
+ // Vertical text
+
+ if( text_source->style->text_orientation.computed == SP_CSS_TEXT_ORIENTATION_SIDEWAYS ||
+ (text_source->style->text_orientation.computed == SP_CSS_TEXT_ORIENTATION_MIXED &&
+ para.pango_items[span->end.iter_span->pango_item_index].item->analysis.gravity == 0) ) {
+ // Sideways orientation
+ char_width += span->start.iter_span->font_size * para.pango_items[span->end.iter_span->pango_item_index].font->Advance(span->end.iter_span->glyph_string->glyphs[span->end_glyph_index].glyph, false);
+ } else {
+ // Upright orientation
+ char_width += span->start.iter_span->font_size * para.pango_items[span->end.iter_span->pango_item_index].font->Advance(span->end.iter_span->glyph_string->glyphs[span->end_glyph_index].glyph, true);
+ }
+ } else {
+ // Horizontal text
char_width += font_size_multiplier * span->end.iter_span->glyph_string->glyphs[span->end_glyph_index].geometry.width;
+ }
span->end_glyph_index++;
}
if (char_attributes.is_cursor_position)
@@ -474,7 +462,7 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
are ready to output the final result to #_flow. This method takes its
input parameters and does that.
*/
- void _outputLine(ParagraphInfo const &para, LineHeight const &line_height, std::vector<ChunkInfo> const &chunk_info)
+ void _outputLine(ParagraphInfo const &para, FontMetrics const &line_height, std::vector<ChunkInfo> const &chunk_info)
{
TRACE(("Start _outputLine\n"));
if (chunk_info.empty()) {
@@ -486,7 +474,17 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
TRACE(("found line fit; creating output\n"));
Layout::Line new_line;
new_line.in_paragraph = _flow._paragraphs.size() - 1;
- new_line.baseline_y = _scanline_maker->yCoordinate() + line_height.ascent;
+ new_line.baseline_y = _scanline_maker->yCoordinate();
+ if( !_flow._input_wrap_shapes.empty() ) {
+ // Flowed text
+ if( _block_progression == RIGHT_TO_LEFT || _block_progression == LEFT_TO_RIGHT ) {
+ // Vertical text, use em box center as baseline
+ new_line.baseline_y += 0.5 * line_height.emSize();
+ } else {
+ new_line.baseline_y += line_height.getTypoAscent();
+ }
+ }
+
new_line.in_shape = _current_shape_index;
_flow._lines.push_back(new_line);
@@ -527,17 +525,16 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
// If <tspan> "y" attribute is set, use it (initial "y" attributes in
// <tspans> other than the first have already been stripped for <tspans>
// marked with role="line", see sp-text.cpp: SPText::_buildLayoutInput).
+ // NOTE: for vertical text, "y" is the user-space "x" value.
if( it_chunk->broken_spans.front().start.iter_span->y._set ) {
// Use set "y" attribute
new_line.baseline_y = it_chunk->broken_spans.front().start.iter_span->y.computed;
-
// Save baseline
_flow._lines.back().baseline_y = new_line.baseline_y;
- // Save new <tspan> y coordinate
- _scanline_maker->setNewYCoordinate(new_line.baseline_y - line_height.ascent);
-
+ // Set the initial y coordinate of the next line.
+ _scanline_maker->setNewYCoordinate(new_line.baseline_y);
}
// Reset relative y_offset ("dy" attribute is relative but should be reset at
@@ -556,21 +553,21 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
}
_flow._chunks.push_back(new_chunk);
- double x;
+ double current_x;
double direction_sign;
Direction previous_direction = para.direction;
double counter_directional_width_remaining = 0.0;
float glyph_rotate = 0.0;
if (para.direction == LEFT_TO_RIGHT) {
direction_sign = +1.0;
- x = 0.0;
+ current_x = 0.0;
} else {
direction_sign = -1.0;
if (para.alignment == FULL && !_flow._input_wrap_shapes.empty()){
- x = it_chunk->scanrun_width;
+ current_x = it_chunk->scanrun_width;
}
else {
- x = it_chunk->text_width;
+ current_x = it_chunk->text_width;
}
}
@@ -583,7 +580,7 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
if (it_span->start.char_byte == 0) {
// Start of an unbroken span, we might have dx, dy or rotate still to process
// (x and y are done per chunk)
- if (unbroken_span.dx._set) x += unbroken_span.dx.computed;
+ if (unbroken_span.dx._set) current_x += unbroken_span.dx.computed;
if (unbroken_span.dy._set) _y_offset += unbroken_span.dy.computed;
if (unbroken_span.rotate._set) glyph_rotate = unbroken_span.rotate.computed * (M_PI/180);
}
@@ -601,6 +598,7 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
new_span.in_input_stream_item = unbroken_span.input_index;
new_span.baseline_shift = 0.0;
new_span.block_progression = _block_progression;
+ new_span.text_orientation = unbroken_span.text_orientation;
if ((_flow._input_stream[unbroken_span.input_index]->Type() == TEXT_SOURCE) && (new_span.font = para.pango_items[unbroken_span.pango_item_index].font))
{
new_span.font->Ref();
@@ -609,12 +607,12 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
new_span.input_stream_first_character = Glib::ustring::const_iterator(unbroken_span.input_stream_first_character.base() + it_span->start.char_byte);
} else { // a control code
new_span.font = NULL;
- new_span.font_size = new_span.line_height.ascent + new_span.line_height.descent;
+ new_span.font_size = new_span.line_height.emSize();
new_span.direction = para.direction;
}
if (new_span.direction == para.direction) {
- x -= counter_directional_width_remaining;
+ current_x -= counter_directional_width_remaining;
counter_directional_width_remaining = 0.0;
} else if (new_span.direction != previous_direction) {
// measure width of spans we need to switch round
@@ -630,10 +628,10 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
}
counter_directional_width_remaining += direction_sign * (it_following_span->width + it_following_span->whitespace_count * add_to_each_whitespace);
}
- x += counter_directional_width_remaining;
+ current_x += counter_directional_width_remaining;
counter_directional_width_remaining = 0.0; // we want to go increasingly negative
}
- new_span.x_start = x;
+ new_span.x_start = current_x;
if (_flow._input_stream[unbroken_span.input_index]->Type() == TEXT_SOURCE) {
// the span is set up, push the glyphs and chars
@@ -673,10 +671,12 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
}
// create the Layout::Glyph
+ PangoGlyphInfo *unbroken_span_glyph_info = &unbroken_span.glyph_string->glyphs[glyph_index];
Layout::Glyph new_glyph;
- new_glyph.glyph = unbroken_span.glyph_string->glyphs[glyph_index].glyph;
+ new_glyph.glyph = unbroken_span_glyph_info->glyph;
new_glyph.in_character = _flow._characters.size();
new_glyph.rotation = glyph_rotate;
+ new_glyph.orientation = ORIENTATION_UPRIGHT; // Only effects vertical text
// We may have scaled font size to fit textLength; now, if
// @lengthAdjust=spacingAndGlyphs, this scaling must be only horizontal,
@@ -686,30 +686,68 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
else
new_glyph.vertical_scale = 1.0;
- /* put something like this back in when we do glyph-rotation-horizontal/vertical
- if (new_span.block_progression == LEFT_TO_RIGHT || new_span.block_progression == RIGHT_TO_LEFT) {
- new_glyph.x += new_span.line_height.ascent;
- new_glyph.y -= unbroken_span.glyph_string->glyphs[glyph_index].geometry.width * font_size_multiplier * 0.5;
- new_glyph.width = new_span.line_height.ascent + new_span.line_height.descent;
- } else */
+ // Position glyph --------------------
+ new_glyph.x = current_x + unbroken_span_glyph_info->geometry.x_offset * font_size_multiplier;
+ new_glyph.y =_y_offset;
+
+ // y-coordinate is flipped between vertical and horizontal text... delta_y is common offset but applied with opposite sign
+ double delta_y = unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier + unbroken_span.baseline_shift;
+ SPCSSBaseline dominant_baseline = _flow._blockBaseline();
if (_block_progression == LEFT_TO_RIGHT || _block_progression == RIGHT_TO_LEFT) {
- new_glyph.x = x + unbroken_span.glyph_string->glyphs[glyph_index].geometry.x_offset * font_size_multiplier + new_span.line_height.ascent;
- new_glyph.y = _y_offset -
- unbroken_span.baseline_shift +
- (unbroken_span.glyph_string->glyphs[glyph_index].geometry.y_offset -
- unbroken_span.glyph_string->glyphs[glyph_index].geometry.width * 0.5) * font_size_multiplier;
- new_glyph.width = new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span.glyph_string->glyphs[glyph_index].glyph, true);
+ // Vertical text
+
+ // Default dominant baseline is determined by overall block (i.e. <text>) 'text-orientation' value.
+ if( _flow._blockTextOrientation() != SP_CSS_TEXT_ORIENTATION_SIDEWAYS ) {
+ if( dominant_baseline == SP_CSS_BASELINE_AUTO ) dominant_baseline = SP_CSS_BASELINE_CENTRAL;
+ } else {
+ if( dominant_baseline == SP_CSS_BASELINE_AUTO ) dominant_baseline = SP_CSS_BASELINE_ALPHABETIC;
+ }
+
+ new_glyph.y += delta_y;
+
+ // TODO: Should also check 'glyph_orientation_vertical' if 'text-orientation' is unset...
+ if( new_span.text_orientation == SP_CSS_TEXT_ORIENTATION_SIDEWAYS ||
+ (new_span.text_orientation == SP_CSS_TEXT_ORIENTATION_MIXED &&
+ para.pango_items[unbroken_span.pango_item_index].item->analysis.gravity == 0) ) {
+
+ // Sideways orientation (Latin characters, CJK punctuation), 90deg rotation done at output stage. zzzzzzz
+ new_glyph.orientation = ORIENTATION_SIDEWAYS;
+
+ new_glyph.y -= new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->GetBaselines()[ dominant_baseline ];
+ new_glyph.width = new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span_glyph_info->glyph, false);
+
+ } else {
+ // Upright orientation
+
+ new_glyph.x += new_span.line_height.ascent;
+
+ // Glyph reference point is center (shift: left edge to center glyph)
+ new_glyph.y -= unbroken_span_glyph_info->geometry.width * 0.5 * font_size_multiplier;
+ new_glyph.y -= new_span.font_size * (para.pango_items[unbroken_span.pango_item_index].font->GetBaselines()[ dominant_baseline ] -
+ para.pango_items[unbroken_span.pango_item_index].font->GetBaselines()[ SP_CSS_BASELINE_CENTRAL ] );
+
+ new_glyph.width = new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span_glyph_info->glyph, true);
+ if( new_glyph.width == 0 ) {
+ new_glyph.width = unbroken_span_glyph_info->geometry.width * font_size_multiplier;
+ }
+
+ }
} else {
- new_glyph.x = x + unbroken_span.glyph_string->glyphs[glyph_index].geometry.x_offset * font_size_multiplier;
- new_glyph.y = _y_offset -
- unbroken_span.baseline_shift +
- unbroken_span.glyph_string->glyphs[glyph_index].geometry.y_offset * font_size_multiplier;
- new_glyph.width = unbroken_span.glyph_string->glyphs[glyph_index].geometry.width * font_size_multiplier;
+ // Horizontal text
+
+ if( dominant_baseline == SP_CSS_BASELINE_AUTO ) dominant_baseline = SP_CSS_BASELINE_ALPHABETIC;
+
+ new_glyph.y -= delta_y;
+ new_glyph.y += new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->GetBaselines()[ dominant_baseline ];
+
+ new_glyph.width = unbroken_span_glyph_info->geometry.width * font_size_multiplier;
if ((new_glyph.width == 0) && (para.pango_items[unbroken_span.pango_item_index].font))
- new_glyph.width = new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span.glyph_string->glyphs[glyph_index].glyph, false);
+ new_glyph.width = new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span_glyph_info->glyph, false);
// for some reason pango returns zero width for invalid glyph characters (those empty boxes), so go to freetype for the info
}
+
+
if (new_span.direction == RIGHT_TO_LEFT) {
// pango wanted to give us glyphs in visual order but we refused, so we need to work
// out where the cluster start is ourselves
@@ -718,13 +756,15 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
if (unbroken_span.glyph_string->glyphs[rtl_index].attr.is_cluster_start && rtl_index != glyph_index)
break;
if (_block_progression == LEFT_TO_RIGHT || _block_progression == RIGHT_TO_LEFT)
+ // Vertical text
cluster_width += new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span.glyph_string->glyphs[rtl_index].glyph, true);
else
+ // Horizontal text
cluster_width += font_size_multiplier * unbroken_span.glyph_string->glyphs[rtl_index].geometry.width;
}
new_glyph.x -= cluster_width;
}
- _flow._glyphs.push_back(new_glyph);
+ _flow._glyphs.push_back(new_glyph);
// create the Layout::Character(s)
double advance_width = new_glyph.width;
@@ -781,15 +821,15 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){
advance_width *= direction_sign;
if (new_span.direction != para.direction) {
counter_directional_width_remaining -= advance_width;
- x -= advance_width;
+ current_x -= advance_width;
x_in_span_last -= advance_width;
} else {
- x += advance_width;
+ current_x += advance_width;
x_in_span_last += advance_width;
}
}
} else if (_flow._input_stream[unbroken_span.input_index]->Type() == CONTROL_CODE) {
- x += static_cast<InputStreamControlCode const *>(_flow._input_stream[unbroken_span.input_index])->width;
+ current_x += static_cast<InputStreamControlCode const *>(_flow._input_stream[unbroken_span.input_index])->width;
}
new_span.x_end = new_span.x_start + x_in_span_last;
@@ -978,6 +1018,7 @@ void Layout::Calculator::_buildPangoItemizationForPara(ParagraphInfo *para) con
#if PANGO_VERSION_CHECK(1,37,1)
TRACE((" ... compiled for font features\n"));
#endif
+
Glib::ustring para_text;
PangoAttrList *attributes_list;
unsigned input_index;
@@ -999,10 +1040,10 @@ void Layout::Calculator::_buildPangoItemizationForPara(ParagraphInfo *para) con
} else if (_flow._input_stream[input_index]->Type() == TEXT_SOURCE) {
Layout::InputStreamTextSource *text_source = static_cast<Layout::InputStreamTextSource *>(_flow._input_stream[input_index]);
- // create the font_instance
- font_instance *font = text_source->styleGetFontInstance();
- if (font == NULL)
- continue; // bad news: we'll have to ignore all this text because we know of no font to render it
+ // create the font_instance
+ font_instance *font = text_source->styleGetFontInstance();
+ if (font == NULL)
+ continue; // bad news: we'll have to ignore all this text because we know of no font to render it
PangoAttribute *attribute_font_description = pango_attr_font_desc_new(font->descr);
attribute_font_description->start_index = para_text.bytes();
@@ -1028,25 +1069,22 @@ void Layout::Calculator::_buildPangoItemizationForPara(ParagraphInfo *para) con
TRACE(("whole para: \"%s\"\n", para_text.data()));
TRACE(("%d input sources used\n", input_index - para->first_input_index));
-
// do the pango_itemize()
GList *pango_items_glist = NULL;
+ para->direction = LEFT_TO_RIGHT; // CSS default
if (_flow._input_stream[para->first_input_index]->Type() == TEXT_SOURCE) {
Layout::InputStreamTextSource const *text_source = static_cast<Layout::InputStreamTextSource *>(_flow._input_stream[para->first_input_index]);
- if (text_source->style->direction.set) {
- PangoDirection pango_direction = (PangoDirection)_enum_converter(text_source->style->direction.computed, enum_convert_spstyle_direction_to_pango_direction, sizeof(enum_convert_spstyle_direction_to_pango_direction)/sizeof(enum_convert_spstyle_direction_to_pango_direction[0]));
- pango_items_glist = pango_itemize_with_base_dir(_pango_context, pango_direction, para_text.data(), 0, para_text.bytes(), attributes_list, NULL);
- para->direction = (Layout::Direction)_enum_converter(text_source->style->direction.computed, enum_convert_spstyle_direction_to_my_direction, sizeof(enum_convert_spstyle_direction_to_my_direction)/sizeof(enum_convert_spstyle_direction_to_my_direction[0]));
- }
+
+ para->direction = (text_source->style->direction.computed == SP_CSS_DIRECTION_LTR) ? LEFT_TO_RIGHT : RIGHT_TO_LEFT;
+ PangoDirection pango_direction = (text_source->style->direction.computed == SP_CSS_DIRECTION_LTR) ? PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL;
+ pango_items_glist = pango_itemize_with_base_dir(_pango_context, pango_direction, para_text.data(), 0, para_text.bytes(), attributes_list, NULL);
}
- if (pango_items_glist == NULL) { // no direction specified, guess it
- pango_items_glist = pango_itemize(_pango_context, para_text.data(), 0, para_text.bytes(), attributes_list, NULL);
- // I think according to the css spec this is wrong and we're never allowed to guess the directionality
- // of a paragraph. Need to talk to an rtl speaker.
- if (pango_items_glist == NULL || pango_items_glist->data == NULL) para->direction = LEFT_TO_RIGHT;
- else para->direction = (((PangoItem*)pango_items_glist->data)->analysis.level & 1) ? RIGHT_TO_LEFT : LEFT_TO_RIGHT;
+ if( pango_items_glist == NULL ) {
+ // Type wasn't TEXT_SOURCE or direction was not set.
+ pango_items_glist = pango_itemize(_pango_context, para_text.data(), 0, para_text.bytes(), attributes_list, NULL);
}
+
pango_attr_list_unref(attributes_list);
// convert the GList to our vector<> and make the font_instance for each PangoItem at the same time
@@ -1070,44 +1108,32 @@ void Layout::Calculator::_buildPangoItemizationForPara(ParagraphInfo *para) con
}
/**
- * Gets the ascent, descent and leading for a font and the alteration that has to be performed
- * according to the value specified by the line-height css property. The result of multiplying
- * \a line_height by \a line_height_multiplier is the inline box height as specified in css2
- * section 10.8.
+ * Finds the value of line_height_multiplier given the 'line-height' property. The result of
+ * multiplying \a l by \a line_height_multiplier is the inline box height as specified in css2
+ * section 10.8. http://www.w3.org/TR/CSS2/visudet.html#line-height
+ *
+ * The 'computed' value of 'line-height' does not have a consistent meaning. We need to find the
+ * 'used' value and divide that by the font size.
*/
-void Layout::Calculator::_computeFontLineHeight(font_instance *font, double font_size,
- SPStyle const *style, LineHeight *line_height,
- double *line_height_multiplier)
+double Layout::Calculator::_computeFontLineHeight( SPStyle const *style )
{
- if (font == NULL) {
- line_height->setZero();
- *line_height_multiplier = 1.0;
- }
- else {
- font->FontMetrics(line_height->ascent, line_height->descent, line_height->leading);
- }
- *line_height *= font_size;
-
// yet another borked SPStyle member that we're going to have to fix ourselves
+ // We shouldn't need to climb the element tree...
for ( ; ; ) {
if (style->line_height.set && !style->line_height.inherit) {
if (style->line_height.normal)
break;
switch (style->line_height.unit) {
case SP_CSS_UNIT_NONE:
- *line_height_multiplier = style->line_height.computed * font_size / line_height->total();
- return;
+ return style->line_height.computed;
case SP_CSS_UNIT_EX:
- *line_height_multiplier = style->line_height.value * 0.5 * font_size / line_height->total();
+ return style->line_height.value * 0.5;
// 0.5 is an approximation of the x-height. Fixme.
- return;
case SP_CSS_UNIT_EM:
case SP_CSS_UNIT_PERCENT:
- *line_height_multiplier = style->line_height.value * font_size / line_height->total();
- return;
+ return style->line_height.value;
default: // absolute values
- *line_height_multiplier = style->line_height.computed / line_height->total();
- return;
+ return style->line_height.computed / style->font_size.computed;
}
break;
}
@@ -1115,7 +1141,7 @@ void Layout::Calculator::_computeFontLineHeight(font_instance *font, double font
style = style->object->parent->style;
if (style == NULL) break;
}
- *line_height_multiplier = LINE_HEIGHT_NORMAL * font_size / line_height->total();
+ return (LINE_HEIGHT_NORMAL);
}
bool compareGlyphWidth(const PangoGlyphInfo &a, const PangoGlyphInfo &b)
@@ -1155,7 +1181,6 @@ unsigned Layout::Calculator::_buildSpansForPara(ParagraphInfo *para) const
new_span.input_index = input_index;
new_span.line_height.ascent = control_code->ascent * _flow.getTextLengthMultiplierDue();
new_span.line_height.descent = control_code->descent * _flow.getTextLengthMultiplierDue();
- new_span.line_height.leading = 0.0;
new_span.text_bytes = 0;
new_span.char_index_in_para = char_index_in_para;
para->unbroken_spans.push_back(new_span);
@@ -1194,11 +1219,13 @@ unsigned Layout::Calculator::_buildSpansForPara(ParagraphInfo *para) const
new_span.dy._set = false;
new_span.rotate._set = false;
if (_block_progression == TOP_TO_BOTTOM || _block_progression == BOTTOM_TO_TOP) {
+ // Horizontal text
if (text_source->x.size() > char_index_in_source) new_span.x = text_source->x[char_index_in_source];
if (text_source->y.size() > char_index_in_source) new_span.y = text_source->y[char_index_in_source];
if (text_source->dx.size() > char_index_in_source) new_span.dx = text_source->dx[char_index_in_source].computed * _flow.getTextLengthMultiplierDue();
if (text_source->dy.size() > char_index_in_source) new_span.dy = text_source->dy[char_index_in_source].computed * _flow.getTextLengthMultiplierDue();
} else {
+ // Vertical text
if (text_source->x.size() > char_index_in_source) new_span.y = text_source->x[char_index_in_source];
if (text_source->y.size() > char_index_in_source) new_span.x = text_source->y[char_index_in_source];
if (text_source->dx.size() > char_index_in_source) new_span.dy = text_source->dx[char_index_in_source].computed * _flow.getTextLengthMultiplierDue();
@@ -1231,7 +1258,7 @@ unsigned Layout::Calculator::_buildSpansForPara(ParagraphInfo *para) const
}
// now we know the length, do some final calculations and add the UnbrokenSpan to the list
- new_span.font_size = text_source->styleComputeFontSize() * _flow.getTextLengthMultiplierDue();
+ new_span.font_size = text_source->style->font_size.computed * _flow.getTextLengthMultiplierDue();
if (new_span.text_bytes) {
new_span.glyph_string = pango_glyph_string_new();
/* Some assertions intended to help diagnose bug #1277746. */
@@ -1240,6 +1267,7 @@ unsigned Layout::Calculator::_buildSpansForPara(ParagraphInfo *para) const
g_assert( span_start_byte_in_source + new_span.text_bytes <= text_source->text->bytes() );
g_assert( memchr(text_source->text->data() + span_start_byte_in_source, '\0', static_cast<size_t>(new_span.text_bytes))
== NULL );
+
/* Notes as of 4/29/13. Pango_shape is not generating English language ligatures, but it is generating
them for Hebrew (and probably other similar languages). In the case observed 3 unicode characters (a base
and 2 Mark, nonspacings) are merged into two glyphs (the base + first Mn, the 2nd Mn). All of these map
@@ -1247,6 +1275,8 @@ unsigned Layout::Calculator::_buildSpansForPara(ParagraphInfo *para) const
characters and glyphs. A big chunk of the conditional code which immediately follows this call
is there to clean up the resulting mess.
*/
+
+ // Convert characters to glyphs
pango_shape(text_source->text->data() + span_start_byte_in_source,
new_span.text_bytes,
&para->pango_items[pango_item_index].item->analysis,
@@ -1324,11 +1354,14 @@ unsigned Layout::Calculator::_buildSpansForPara(ParagraphInfo *para) const
/* glyphs[].x_offset values may be out of order within any log_clusters, apparently harmless */
}
new_span.pango_item_index = pango_item_index;
- _computeFontLineHeight(para->pango_items[pango_item_index].font, new_span.font_size, text_source->style, &new_span.line_height, &new_span.line_height_multiplier);
+ new_span.line_height_multiplier = _computeFontLineHeight( text_source->style );
+ new_span.line_height.set( para->pango_items[pango_item_index].font );
+ new_span.line_height *= new_span.font_size;
// At some point we may want to calculate baseline_shift here (to take advantage
// of otm features like superscript baseline), but for now we use style baseline_shift.
new_span.baseline_shift = text_source->style->baseline_shift.computed;
+ new_span.text_orientation = (SPCSSTextOrientation)text_source->style->text_orientation.computed;
// TODO: metrics for vertical text
TRACE(("add text span %lu \"%s\"\n", para->unbroken_spans.size(), text_source->text->raw().substr(span_start_byte_in_source, new_span.text_bytes).c_str()));
@@ -1338,11 +1371,13 @@ unsigned Layout::Calculator::_buildSpansForPara(ParagraphInfo *para) const
new_span.pango_item_index = -1;
font_instance *font = text_source->styleGetFontInstance();
if (font) {
- _computeFontLineHeight(font, new_span.font_size, text_source->style, &new_span.line_height, &new_span.line_height_multiplier);
+ new_span.line_height_multiplier = _computeFontLineHeight( text_source->style );
+ new_span.line_height.set( font );
+ new_span.line_height *= new_span.font_size;
font->Unref();
} else {
- new_span.line_height.setZero();
- new_span.line_height_multiplier = 1.0;
+ new_span.line_height *= 0.0; // Set all to zero
+ new_span.line_height_multiplier = LINE_HEIGHT_NORMAL;
}
TRACE(("add style init span %lu\n", para->unbroken_spans.size()));
}
@@ -1399,7 +1434,7 @@ bool Layout::Calculator::_goToNextWrapShape()
bool Layout::Calculator::_findChunksForLine(ParagraphInfo const &para,
UnbrokenSpanPosition *start_span_pos,
std::vector<ChunkInfo> *chunk_info,
- LineHeight *line_height)
+ FontMetrics *line_height)
{
// init the initial line_height
if (start_span_pos->iter_span == para.unbroken_spans.end()) {
@@ -1408,9 +1443,9 @@ bool Layout::Calculator::_findChunksForLine(ParagraphInfo const &para,
InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_flow._input_stream.front());
font_instance *font = text_source->styleGetFontInstance();
if (font) {
- double font_size = text_source->styleComputeFontSize();
- double multiplier;
- _computeFontLineHeight(font, font_size, text_source->style, line_height, &multiplier);
+ double multiplier = _computeFontLineHeight(text_source->style);
+ line_height->set( font );
+ *line_height *= text_source->style->font_size.computed;
font->Unref();
*line_height *= multiplier;
_scanline_maker->setNewYCoordinate(_scanline_maker->yCoordinate() - line_height->ascent);
@@ -1422,7 +1457,6 @@ bool Layout::Calculator::_findChunksForLine(ParagraphInfo const &para,
// if we're not wrapping set the line_height big and negative so we can use negative line height
line_height->ascent = -1.0e10;
line_height->descent = -1.0e10;
- line_height->leading = -1.0e10;
}
else
line_height->setZero();
@@ -1474,7 +1508,7 @@ bool Layout::Calculator::_buildChunksInScanRun(ParagraphInfo const &para,
UnbrokenSpanPosition const &start_span_pos,
ScanlineMaker::ScanRun const &scan_run,
std::vector<ChunkInfo> *chunk_info,
- LineHeight *line_height) const
+ FontMetrics *line_height) const
{
ChunkInfo new_chunk;
new_chunk.text_width = 0.0;
@@ -1511,16 +1545,17 @@ bool Layout::Calculator::_buildChunksInScanRun(ParagraphInfo const &para,
}
// see if this span is too tall to fit on the current line
- LineHeight total_height = new_span.start.iter_span->line_height;
- total_height *= new_span.start.iter_span->line_height_multiplier;
+ FontMetrics new_span_height = new_span.start.iter_span->line_height;
+ new_span_height.computeEffective( new_span.start.iter_span->line_height_multiplier );
/* floating point 80-bit/64-bit rounding problems require epsilon. See
discussion http://inkscape.gristle.org/2005-03-16.txt around 22:00 */
- if ( total_height.ascent > line_height->ascent + FLT_EPSILON
- || total_height.descent > line_height->descent + FLT_EPSILON
- || total_height.leading > line_height->leading + FLT_EPSILON) {
- line_height->max(total_height);
- if (!_scanline_maker->canExtendCurrentScanline(*line_height))
+ if ( new_span_height.ascent > line_height->ascent + FLT_EPSILON ||
+ new_span_height.descent > line_height->descent + FLT_EPSILON) {
+ // Take larger of each of the two ascents and two descents per CSS
+ line_height->max(new_span_height);
+ if (!_scanline_maker->canExtendCurrentScanline(*line_height)) {
return false;
+ }
}
bool span_fitted = _measureUnbrokenSpan(para, &new_span, &last_span_at_break, &last_span_at_emergency_break, new_chunk.scanrun_width - new_chunk.text_width);
@@ -1540,7 +1575,7 @@ bool Layout::Calculator::_buildChunksInScanRun(ParagraphInfo const &para,
TRACE(("chunk complete, used %f width (%d whitespaces, %lu brokenspans)\n", new_chunk.text_width, new_chunk.whitespace_count, new_chunk.broken_spans.size()));
chunk_info->push_back(new_chunk);
- if (scan_run.width() >= 4.0 * line_height->total() && last_span_at_break.end == start_span_pos) {
+ if (scan_run.width() >= 4.0 * line_height->emSize() && last_span_at_break.end == start_span_pos) {
/* **non-SVG spec bit**: See bug #1191102
If the user types a very long line with no spaces, the way the spec
is written at the moment means that when the length of the text
@@ -1619,14 +1654,27 @@ bool Layout::Calculator::calculate()
_flow._clearOutputObjects();
_pango_context = (font_factory::Default())->fontContext;
+
_font_factory_size_multiplier = (font_factory::Default())->fontSize;
+ // Reset gravity hint in case it was changed via previous use of 'text-orientation'
+ // (scripts take their natural gravity given base gravity).
+ pango_context_set_gravity_hint(_pango_context, PANGO_GRAVITY_HINT_NATURAL);
+
_block_progression = _flow._blockProgression();
+ if( _block_progression == RIGHT_TO_LEFT || _block_progression == LEFT_TO_RIGHT ) {
+ // Vertical text, CJK
+ pango_context_set_base_gravity(_pango_context, PANGO_GRAVITY_EAST);
+ } else {
+ // Horizontal text
+ pango_context_set_base_gravity(_pango_context, PANGO_GRAVITY_AUTO);
+ }
+
_y_offset = 0.0;
_createFirstScanlineMaker();
ParagraphInfo para;
- LineHeight line_height; // needs to be maintained across paragraphs to be able to deal with blank paras
+ FontMetrics line_height; // needs to be maintained across paragraphs to be able to deal with blank paras
for(para.first_input_index = 0 ; para.first_input_index < _flow._input_stream.size() ; ) {
// jump to the next wrap shape if this is a SHAPE_BREAK control code
if (_flow._input_stream[para.first_input_index]->Type() == CONTROL_CODE) {
@@ -1640,7 +1688,10 @@ bool Layout::Calculator::calculate()
if (_scanline_maker == NULL)
break; // we're trying to flow past the last wrap shape
- _buildPangoItemizationForPara(&para);
+ // Break things up into little pango units with unique direction, gravity, etc.
+ _buildPangoItemizationForPara(&para);
+
+ // Do shaping (convert characters to glyphs)
unsigned para_end_input_index = _buildSpansForPara(&para);
if (_flow._input_stream[para.first_input_index]->Type() == TEXT_SOURCE)
@@ -1679,7 +1730,7 @@ bool Layout::Calculator::calculate()
Layout::Span new_span;
if (_flow._spans.empty()) {
new_span.font = NULL;
- new_span.font_size = line_height.ascent + line_height.descent;
+ new_span.font_size = line_height.emSize();
new_span.line_height = line_height;
new_span.x_end = 0.0;
} else {
@@ -1755,19 +1806,16 @@ void Layout::_calculateCursorShapeForEmpty()
InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_input_stream.front());
font_instance *font = text_source->styleGetFontInstance();
- double font_size = text_source->styleComputeFontSize();
+ double font_size = text_source->style->font_size.computed;
double caret_slope_run = 0.0, caret_slope_rise = 1.0;
- LineHeight line_height;
+ FontMetrics line_height;
if (font) {
const_cast<font_instance*>(font)->FontSlope(caret_slope_run, caret_slope_rise);
- font->FontMetrics(line_height.ascent, line_height.descent, line_height.leading);
+ font->FontMetrics(line_height.ascent, line_height.descent, line_height.xheight);
line_height *= font_size;
font->Unref();
- } else {
- line_height.ascent = font_size * 0.85; // random guesses
- line_height.descent = font_size * 0.15;
- line_height.leading = 0.0;
}
+
double caret_slope = atan2(caret_slope_run, caret_slope_rise);
_empty_cursor_shape.height = font_size / cos(caret_slope);
_empty_cursor_shape.rotation = caret_slope;
@@ -1780,10 +1828,13 @@ void Layout::_calculateCursorShapeForEmpty()
ShapeScanlineMaker scanline_maker(_input_wrap_shapes.front().shape, block_progression);
std::vector<ScanlineMaker::ScanRun> scan_runs = scanline_maker.makeScanline(line_height);
if (!scan_runs.empty()) {
- if (block_progression == LEFT_TO_RIGHT || block_progression == RIGHT_TO_LEFT)
+ if (block_progression == LEFT_TO_RIGHT || block_progression == RIGHT_TO_LEFT) {
+ // Vertical text
_empty_cursor_shape.position = Geom::Point(scan_runs.front().y + font_size, scan_runs.front().x_start);
- else
+ } else {
+ // Horizontal text
_empty_cursor_shape.position = Geom::Point(scan_runs.front().x_start, scan_runs.front().y + font_size);
+ }
}
}
}
diff --git a/src/libnrtype/Layout-TNG-Input.cpp b/src/libnrtype/Layout-TNG-Input.cpp
index 77480cebe..6f5d4e5f8 100644
--- a/src/libnrtype/Layout-TNG-Input.cpp
+++ b/src/libnrtype/Layout-TNG-Input.cpp
@@ -126,52 +126,6 @@ int Layout::_enum_converter(int input, EnumConversionItem const *conversion_tabl
return conversion_table[0].output;
}
-// ***** the style format interface
-// this doesn't include all accesses to SPStyle, only the ones that are non-trivial
-
-static const float medium_font_size = 12.0; // more of a default if all else fails than anything else
-float Layout::InputStreamTextSource::styleComputeFontSize() const
-{
- return style->font_size.computed;
-
- // in case the computed value's not good enough, here's some manual code held in reserve:
- SPStyle const *this_style = style;
- float inherit_multiplier = 1.0;
-
- for ( ; ; ) {
- if (this_style->font_size.set && !this_style->font_size.inherit) {
- switch (this_style->font_size.type) {
- case SP_FONT_SIZE_LITERAL: {
- switch(this_style->font_size.literal) { // these multipliers are straight out of the CSS spec
- case SP_CSS_FONT_SIZE_XX_SMALL: return medium_font_size * inherit_multiplier * (3.0/5.0);
- case SP_CSS_FONT_SIZE_X_SMALL: return medium_font_size * inherit_multiplier * (3.0/4.0);
- case SP_CSS_FONT_SIZE_SMALL: return medium_font_size * inherit_multiplier * (8.0/9.0);
- default:
- case SP_CSS_FONT_SIZE_MEDIUM: return medium_font_size * inherit_multiplier;
- case SP_CSS_FONT_SIZE_LARGE: return medium_font_size * inherit_multiplier * (6.0/5.0);
- case SP_CSS_FONT_SIZE_X_LARGE: return medium_font_size * inherit_multiplier * (3.0/2.0);
- case SP_CSS_FONT_SIZE_XX_LARGE: return medium_font_size * inherit_multiplier * 2.0;
- case SP_CSS_FONT_SIZE_SMALLER: inherit_multiplier *= 0.84; break; //not exactly according to spec
- case SP_CSS_FONT_SIZE_LARGER: inherit_multiplier *= 1.26; break; //not exactly according to spec
- }
- break;
- }
- case SP_FONT_SIZE_PERCENTAGE: { // 'em' units should be in here, but aren't. Fix in style.cpp.
- inherit_multiplier *= this_style->font_size.value;
- break;
- }
- case SP_FONT_SIZE_LENGTH: {
- return this_style->font_size.value * inherit_multiplier;
- }
- }
- }
- if (this_style->object == NULL || this_style->object->parent == NULL) break;
- this_style = this_style->object->parent->style;
- if (this_style == NULL) break;
- }
- return medium_font_size * inherit_multiplier;
-}
-
Layout::Direction Layout::InputStreamTextSource::styleGetBlockProgression() const
{
switch( style->writing_mode.computed ) {
@@ -192,6 +146,16 @@ Layout::Direction Layout::InputStreamTextSource::styleGetBlockProgression() cons
return TOP_TO_BOTTOM;
}
+SPCSSTextOrientation Layout::InputStreamTextSource::styleGetTextOrientation() const
+{
+ return ((SPCSSTextOrientation)style->text_orientation.computed);
+}
+
+SPCSSBaseline Layout::InputStreamTextSource::styleGetDominantBaseline() const
+{
+ return ((SPCSSBaseline)style->dominant_baseline.computed);
+}
+
static Layout::Alignment text_anchor_to_alignment(unsigned anchor, Layout::Direction para_direction)
{
switch (anchor) {
diff --git a/src/libnrtype/Layout-TNG-OutIter.cpp b/src/libnrtype/Layout-TNG-OutIter.cpp
index 137fe0a0f..8c29b7dbc 100644
--- a/src/libnrtype/Layout-TNG-OutIter.cpp
+++ b/src/libnrtype/Layout-TNG-OutIter.cpp
@@ -120,7 +120,8 @@ Layout::iterator Layout::getNearestCursorPositionTo(double x, double y) const
double best_y_range = DBL_MAX;
double best_x_range = DBL_MAX;
for (chunk_index = 0 ; chunk_index < _chunks.size() ; chunk_index++) {
- LineHeight line_height = {0.0, 0.0, 0.0};
+ FontMetrics line_height;
+ line_height *= 0.0; // Set all metrics to zero.
double chunk_width = 0.0;
for ( ; span_index < _spans.size() && _spans[span_index].in_chunk == chunk_index ; span_index++) {
line_height.max(_spans[span_index].line_height);
@@ -340,8 +341,8 @@ Geom::Rect Layout::characterBoundingBox(iterator const &it, double *rotation) co
double baseline_y = _characters[char_index].line(this).baseline_y + _characters[char_index].span(this).baseline_shift;
if (_directions_are_orthogonal(_blockProgression(), TOP_TO_BOTTOM)) {
- double span_height = _spans[_characters[char_index].in_span].line_height.ascent + _spans[_characters[char_index].in_span].line_height.descent;
- top_left[Geom::Y] = top_left[Geom::X];
+ double span_height = _spans[_characters[char_index].in_span].line_height.emSize();
+ top_left[Geom::Y] = top_left[Geom::X];
top_left[Geom::X] = baseline_y - span_height * 0.5;
bottom_right[Geom::Y] = bottom_right[Geom::X];
bottom_right[Geom::X] = baseline_y + span_height * 0.5;
@@ -404,7 +405,7 @@ std::vector<Geom::Point> Layout::createSelectionShape(iterator const &it_start,
double vertical_scale = _glyphs.back().vertical_scale;
if (_directions_are_orthogonal(_blockProgression(), TOP_TO_BOTTOM)) {
- double span_height = vertical_scale * (_spans[span_index].line_height.ascent + _spans[span_index].line_height.descent);
+ double span_height = vertical_scale * _spans[span_index].line_height.emSize();
top_left[Geom::Y] = top_left[Geom::X];
top_left[Geom::X] = baseline_y - span_height * 0.5;
bottom_right[Geom::Y] = bottom_right[Geom::X];
@@ -510,17 +511,19 @@ void Layout::queryCursorShape(iterator const &it, Geom::Point &position, double
double vertical_scale = _glyphs.empty() ? 1.0 : _glyphs.back().vertical_scale;
if (_directions_are_orthogonal(_blockProgression(), TOP_TO_BOTTOM)) {
- height = vertical_scale * span->line_height.ascent + span->line_height.descent;
+ // Vertical text
+ height = vertical_scale * span->line_height.emSize();
rotation += M_PI / 2;
std::swap(position[Geom::X], position[Geom::Y]);
position[Geom::X] -= vertical_scale * sin(rotation) * height * 0.5;
position[Geom::Y] += vertical_scale * cos(rotation) * height * 0.5;
} else {
+ // Horizontal text
double caret_slope_run = 0.0, caret_slope_rise = 1.0;
if (span->font)
const_cast<font_instance*>(span->font)->FontSlope(caret_slope_run, caret_slope_rise);
double caret_slope = atan2(caret_slope_run, caret_slope_rise);
- height = vertical_scale * (span->line_height.ascent + span->line_height.descent) / cos(caret_slope);
+ height = vertical_scale * (span->line_height.emSize()) / cos(caret_slope);
rotation += caret_slope;
position[Geom::X] -= sin(rotation) * vertical_scale * span->line_height.descent;
position[Geom::Y] += cos(rotation) * vertical_scale * span->line_height.descent;
diff --git a/src/libnrtype/Layout-TNG-Output.cpp b/src/libnrtype/Layout-TNG-Output.cpp
index 6e3faf33b..526319f35 100644
--- a/src/libnrtype/Layout-TNG-Output.cpp
+++ b/src/libnrtype/Layout-TNG-Output.cpp
@@ -93,26 +93,55 @@ void Layout::_clearOutputObjects()
_path_fitted = NULL;
}
-void Layout::LineHeight::max(LineHeight const &other)
+void Layout::FontMetrics::set(font_instance *font)
{
- if (other.ascent > ascent) ascent = other.ascent;
- if (other.descent > descent) descent = other.descent;
- if (other.leading > leading) leading = other.leading;
+ if( font != NULL ) {
+ ascent = font->GetTypoAscent();
+ descent = font->GetTypoDescent();
+ xheight = font->GetXHeight();
+ ascent_max = font->GetMaxAscent();
+ descent_max = font->GetMaxDescent();
+ }
+}
+
+void Layout::FontMetrics::max(FontMetrics const &other)
+{
+ if (other.ascent > ascent ) ascent = other.ascent;
+ if (other.descent > descent ) descent = other.descent;
+ if( other.xheight > xheight ) xheight = other.xheight;
+ if( other.ascent_max > ascent_max ) ascent_max = other.ascent_max;
+ if( other.descent_max > descent_max ) descent_max = other.descent_max;
+}
+
+void Layout::FontMetrics::computeEffective( const double &line_height_multiplier ) {
+ double half_leading = 0.5 * (line_height_multiplier - 1.0) * emSize();
+ ascent += half_leading;
+ descent += half_leading;
}
void Layout::_getGlyphTransformMatrix(int glyph_index, Geom::Affine *matrix) const
{
Span const &span = _glyphs[glyph_index].span(this);
- double sin_rotation = sin(_glyphs[glyph_index].rotation);
- double cos_rotation = cos(_glyphs[glyph_index].rotation);
+ double rotation = _glyphs[glyph_index].rotation;
+ if ( (span.block_progression == LEFT_TO_RIGHT || span.block_progression == RIGHT_TO_LEFT) &&
+ _glyphs[glyph_index].orientation == ORIENTATION_SIDEWAYS ) {
+ // Vertical sideways text
+ rotation += M_PI/2.0;
+ }
+ double sin_rotation = sin(rotation);
+ double cos_rotation = cos(rotation);
(*matrix)[0] = span.font_size * cos_rotation;
(*matrix)[1] = span.font_size * sin_rotation;
(*matrix)[2] = span.font_size * sin_rotation;
(*matrix)[3] = -span.font_size * cos_rotation * (_glyphs[glyph_index].vertical_scale); // unscale vertically so the specified text height is preserved if lengthAdjust=spacingAndGlyphs
if (span.block_progression == LEFT_TO_RIGHT || span.block_progression == RIGHT_TO_LEFT) {
+ // Vertical text
+ // This effectively swaps x for y which changes handedness of coordinate system. This is a bit strange
+ // and not what one would expect but the compute code already reverses y so OK.
(*matrix)[4] = _lines[_chunks[span.in_chunk].in_line].baseline_y + _glyphs[glyph_index].y;
(*matrix)[5] = _chunks[span.in_chunk].left_x + _glyphs[glyph_index].x;
} else {
+ // Horizontal text
(*matrix)[4] = _chunks[span.in_chunk].left_x + _glyphs[glyph_index].x;
(*matrix)[5] = _lines[_chunks[span.in_chunk].in_line].baseline_y + _glyphs[glyph_index].y;
}
@@ -127,9 +156,9 @@ void Layout::show(DrawingGroup *in_arena, Geom::OptRect const &paintbox) const
InputStreamTextSource const *text_source = static_cast<InputStreamTextSource const *>(_input_stream[_spans[span_index].in_input_stream_item]);
text_source->style->text_decoration_data.tspan_width = _spans[span_index].width();
- text_source->style->text_decoration_data.ascender = _spans[span_index].line_height.getAscent();
- text_source->style->text_decoration_data.descender = _spans[span_index].line_height.getDescent();
- text_source->style->text_decoration_data.line_gap = _spans[span_index].line_height.getLeading();
+ text_source->style->text_decoration_data.ascender = _spans[span_index].line_height.getTypoAscent();
+ text_source->style->text_decoration_data.descender = _spans[span_index].line_height.getTypoDescent();
+
if(!span_index ||
(_chunks[_spans[span_index].in_chunk].in_line != _chunks[_spans[span_index-1].in_chunk].in_line)){
text_source->style->text_decoration_data.tspan_line_start = true;
@@ -170,11 +199,14 @@ void Layout::show(DrawingGroup *in_arena, Geom::OptRect const &paintbox) const
first_line_glyph = false;
phase0 = glyph_matrix.translation()[Geom::X];
}
- // save the starting coordinates for the line - these are needed for figuring out dot/dash/wave phase
+ // Save the starting coordinates for the line - these are needed for figuring out
+ // dot/dash/wave phase.
+ // Use maximum ascent and descent to ensure glpyhs that extend outside the embox
+ // are fully drawn.
(void) nr_text->addComponent(_spans[span_index].font, _glyphs[glyph_index].glyph, glyph_matrix,
_glyphs[glyph_index].width,
- _spans[span_index].line_height.getAscent(),
- _spans[span_index].line_height.getDescent(),
+ _spans[span_index].line_height.getMaxAscent(),
+ _spans[span_index].line_height.getMaxDescent(),
glyph_matrix.translation()[Geom::X] - phase0
);
}
@@ -580,7 +612,7 @@ Glib::ustring Layout::dumpAsText() const
result += Glib::ustring::compose(" font '%1' %2 %3 %4\n", sp_font_description_get_family(_spans[span_index].font->descr), _spans[span_index].font_size, style_to_text(pango_font_description_get_style(_spans[span_index].font->descr)), weight_to_text(pango_font_description_get_weight(_spans[span_index].font->descr)));
}
result += Glib::ustring::compose(" x_start = %1, x_end = %2\n", _spans[span_index].x_start, _spans[span_index].x_end)
- + Glib::ustring::compose(" line height: ascent %1, descent %2 leading %3\n", _spans[span_index].line_height.ascent, _spans[span_index].line_height.descent, _spans[span_index].line_height.leading)
+ + Glib::ustring::compose(" line height: ascent %1, descent %2\n", _spans[span_index].line_height.ascent, _spans[span_index].line_height.descent)
+ Glib::ustring::compose(" direction %1, block-progression %2\n", direction_to_text(_spans[span_index].direction), direction_to_text(_spans[span_index].block_progression))
+ " ** characters:\n";
Glib::ustring::const_iterator iter_char = _spans[span_index].input_stream_first_character;
diff --git a/src/libnrtype/Layout-TNG-Scanline-Maker.h b/src/libnrtype/Layout-TNG-Scanline-Maker.h
index d513d7cc1..da70bff89 100644
--- a/src/libnrtype/Layout-TNG-Scanline-Maker.h
+++ b/src/libnrtype/Layout-TNG-Scanline-Maker.h
@@ -47,7 +47,7 @@ public:
between calls if the new height too big to fit in the space remaining in
this shape. Returns an empty vector if there is no space left in the
current shape. */
- virtual std::vector<ScanRun> makeScanline(Layout::LineHeight const &line_height) =0;
+ virtual std::vector<ScanRun> makeScanline(Layout::FontMetrics const &line_height) =0;
/** Indicates that the caller has successfully filled the current line
and hence that the next call to makeScanline() should return lines on
@@ -71,7 +71,7 @@ public:
The metrics given here are considered to be the ones that are being
used now, and hence is the line advance height used by completeLine().
*/
- virtual bool canExtendCurrentScanline(Layout::LineHeight const &line_height) =0;
+ virtual bool canExtendCurrentScanline(Layout::FontMetrics const &line_height) =0;
};
/** \brief private to Layout. Generates infinite scanlines for when you don't want wrapping
@@ -90,7 +90,7 @@ public:
virtual ~InfiniteScanlineMaker();
/** Returns a single infinite run at the current location */
- virtual std::vector<ScanRun> makeScanline(Layout::LineHeight const &line_height);
+ virtual std::vector<ScanRun> makeScanline(Layout::FontMetrics const &line_height);
/** Increments the current y by the current line height */
virtual void completeLine();
@@ -102,11 +102,11 @@ public:
virtual void setNewYCoordinate(double new_y);
/** Always true, but has to save the new height */
- virtual bool canExtendCurrentScanline(Layout::LineHeight const &line_height);
+ virtual bool canExtendCurrentScanline(Layout::FontMetrics const &line_height);
private:
double _x, _y;
- Layout::LineHeight _current_line_height;
+ Layout::FontMetrics _current_line_height;
bool _negative_block_progression; /// if true, indicates that completeLine() should decrement rather than increment, ie block-progression is either rl or bt
};
@@ -122,7 +122,7 @@ public:
ShapeScanlineMaker(Shape const *shape, Layout::Direction block_progression);
virtual ~ShapeScanlineMaker();
- virtual std::vector<ScanRun> makeScanline(Layout::LineHeight const &line_height);
+ virtual std::vector<ScanRun> makeScanline(Layout::FontMetrics const &line_height);
virtual void completeLine();
@@ -131,7 +131,7 @@ public:
virtual void setNewYCoordinate(double new_y);
/** never true */
- virtual bool canExtendCurrentScanline(Layout::LineHeight const &line_height);
+ virtual bool canExtendCurrentScanline(Layout::FontMetrics const &line_height);
private:
/** To generate scanlines for top-to-bottom text it is easiest if we
simply rotate the given shape by a multiple of 90 degrees. This stores
diff --git a/src/libnrtype/Layout-TNG-Scanline-Makers.cpp b/src/libnrtype/Layout-TNG-Scanline-Makers.cpp
index 7144f3876..67fffd620 100644
--- a/src/libnrtype/Layout-TNG-Scanline-Makers.cpp
+++ b/src/libnrtype/Layout-TNG-Scanline-Makers.cpp
@@ -19,9 +19,7 @@ namespace Text {
Layout::InfiniteScanlineMaker::InfiniteScanlineMaker(double initial_x, double initial_y, Layout::Direction block_progression)
{
- _current_line_height.ascent = 0.0;
- _current_line_height.descent = 0.0;
- _current_line_height.leading = 0.0;
+ _current_line_height.setZero();
switch (block_progression) {
case LEFT_TO_RIGHT:
case RIGHT_TO_LEFT:
@@ -41,7 +39,7 @@ Layout::InfiniteScanlineMaker::~InfiniteScanlineMaker()
{
}
-std::vector<Layout::ScanlineMaker::ScanRun> Layout::InfiniteScanlineMaker::makeScanline(Layout::LineHeight const &line_height)
+std::vector<Layout::ScanlineMaker::ScanRun> Layout::InfiniteScanlineMaker::makeScanline(Layout::FontMetrics const &line_height)
{
std::vector<ScanRun> runs(1);
runs[0].x_start = _x;
@@ -54,12 +52,10 @@ std::vector<Layout::ScanlineMaker::ScanRun> Layout::InfiniteScanlineMaker::makeS
void Layout::InfiniteScanlineMaker::completeLine()
{
if (_negative_block_progression)
- _y -= _current_line_height.total();
+ _y -= _current_line_height.emSize();
else
- _y += _current_line_height.total();
- _current_line_height.ascent = 0.0;
- _current_line_height.descent = 0.0;
- _current_line_height.leading = 0.0;
+ _y += _current_line_height.emSize();
+ _current_line_height.setZero();
}
void Layout::InfiniteScanlineMaker::setNewYCoordinate(double new_y)
@@ -67,7 +63,7 @@ void Layout::InfiniteScanlineMaker::setNewYCoordinate(double new_y)
_y = new_y;
}
-bool Layout::InfiniteScanlineMaker::canExtendCurrentScanline(Layout::LineHeight const &line_height)
+bool Layout::InfiniteScanlineMaker::canExtendCurrentScanline(Layout::FontMetrics const &line_height)
{
_current_line_height = line_height;
return true;
@@ -111,29 +107,29 @@ Layout::ShapeScanlineMaker::~ShapeScanlineMaker()
delete _rotated_shape;
}
-std::vector<Layout::ScanlineMaker::ScanRun> Layout::ShapeScanlineMaker::makeScanline(Layout::LineHeight const &line_height)
+std::vector<Layout::ScanlineMaker::ScanRun> Layout::ShapeScanlineMaker::makeScanline(Layout::FontMetrics const &line_height)
{
- FloatLigne line_rasterization;
- FloatLigne line_decent_length_runs;
- float line_text_height = (float)(line_height.ascent + line_height.descent);
-
if (_y > _bounding_box_bottom)
return std::vector<ScanRun>();
if (_y < _bounding_box_top)
_y = _bounding_box_top;
+ FloatLigne line_rasterization;
+ FloatLigne line_decent_length_runs;
+ float line_text_height = (float)(line_height.emSize());
if (line_text_height == 0.0)
line_text_height = 0.001; // Scan() doesn't work for zero height so this will have to do
- _current_line_height = (float)line_height.total();
+ _current_line_height = (float)line_height.emSize();
// I think what's going on here is that we're moving the top of the scanline to the given position...
_rotated_shape->Scan(_rasterizer_y, _current_rasterization_point, _y, line_text_height);
// ...then actually retreiving the scanline (which alters the first two parameters)
_rotated_shape->Scan(_rasterizer_y, _current_rasterization_point, _y + line_text_height , &line_rasterization, true, line_text_height);
- // sanitise the raw rasterisation, which could have weird overlaps
+ // sanitise the raw rasterisation, which could have weird overlaps
line_rasterization.Flatten();
+ // line_rasterization.Affiche();
// cut out runs that cover less than 90% of the line
line_decent_length_runs.Over(&line_rasterization, 0.9 * line_text_height);
@@ -179,7 +175,7 @@ void Layout::ShapeScanlineMaker::setNewYCoordinate(double new_y)
// it's not an important question because <flowSpan> doesn't have a y attribute
}
-bool Layout::ShapeScanlineMaker::canExtendCurrentScanline(Layout::LineHeight const &/*line_height*/)
+bool Layout::ShapeScanlineMaker::canExtendCurrentScanline(Layout::FontMetrics const &/*line_height*/)
{
//we actually could return true if only the leading changed, but that's too much effort for something that rarely happens
return false;
diff --git a/src/libnrtype/Layout-TNG.h b/src/libnrtype/Layout-TNG.h
index 26db1fad9..97a05bde8 100644
--- a/src/libnrtype/Layout-TNG.h
+++ b/src/libnrtype/Layout-TNG.h
@@ -22,6 +22,7 @@
#include <vector>
#include <boost/optional.hpp>
#include <svg/svg-length.h>
+#include "style-enums.h"
#ifdef HAVE_CAIRO_PDF
namespace Inkscape {
@@ -157,6 +158,9 @@ public:
both the 'direction' and 'block-progression' CSS attributes. */
enum Direction {LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, BOTTOM_TO_TOP};
+ /** Used to specify orientation of glyphs in vertical text. */
+ enum Orientation {ORIENTATION_UPRIGHT, ORIENTATION_SIDEWAYS};
+
/** Display alignment for shapes. See appendWrapShape(). */
enum DisplayAlign {DISPLAY_ALIGN_BEFORE, DISPLAY_ALIGN_CENTER, DISPLAY_ALIGN_AFTER};
@@ -603,19 +607,69 @@ public:
//@}
- /// it's useful for this to be public so that ScanlineMaker can use it
- struct LineHeight {
- double ascent;
- double descent;
- double leading;
- inline double total() const {return ascent + descent + leading;}
- inline void setZero() {ascent = descent = leading = 0.0;}
- inline LineHeight& operator*=(double x) {ascent *= x; descent *= x; leading *= x; return *this;}
- void max(LineHeight const &other); /// makes this object contain the largest of all three members between this object and other
- inline double getAscent() const {return ascent; }
- inline double getDescent() const {return descent; }
- inline double getLeading() const {return leading; }
- };
+
+ /**
+ * Keep track of font metrics. Two use cases:
+ * 1. Keep track of ascent, descent, and x-height of an individual font.
+ * 2. Keep track of effective ascent and descent that includes half-leading.
+ *
+ * Note: Leading refers to the "external" leading which is added (subtracted) due to
+ * a computed value of 'line-height' that differs from 'font-size'. "Internal" leading
+ * which is specified inside a font is not used in CSS. The 'font-size' is based on
+ * the font's em size which is 'ascent' + 'descent'.
+ *
+ * This structure was renamed (and modified) from "LineHeight".
+ *
+ * It's useful for this to be public so that ScanlineMaker can use it.
+ */
+ class FontMetrics {
+
+ public:
+ FontMetrics() { reset(); }
+
+ void reset() {
+ ascent = 0.8;
+ descent = -0.2;
+ xheight = 0.5;
+ ascent_max = 0.8;
+ descent_max = 0.2;
+ }
+
+ void set( font_instance *font );
+
+ // CSS 2.1 dictates that font-size is based on em-size which is defined as ascent + descent
+ inline double emSize() const {return ascent + descent;}
+ // Alternatively name function for use 2.
+ inline double lineSize() const { return ascent + descent; }
+ inline void setZero() {ascent = descent = xheight = ascent_max = descent_max = 0.0;}
+
+ // For scaling for 'font-size'.
+ inline FontMetrics& operator*=(double x) {
+ ascent *= x; descent *= x; xheight *= x; ascent_max *= x; descent_max *= x;
+ return *this;
+ }
+
+ /// Save the larger values of ascent and descent between this and other. Needed for laying
+ /// out a line with mixed font-sizes, fonts, or line spacings.
+ void max(FontMetrics const &other);
+
+ /// Calculate the effective ascent and descent including half "leading".
+ void computeEffective( const double &line_height );
+
+ inline double getTypoAscent() const {return ascent; }
+ inline double getTypoDescent() const {return descent; }
+ inline double getXHeight() const {return xheight; }
+ inline double getMaxAscent() const {return ascent_max; }
+ inline double getMaxDescent() const {return descent_max; }
+
+ // private:
+ double ascent; // Typographic ascent.
+ double descent; // Typographic descent.
+ double xheight; // Height of 'x' measured from alphabetic baseline.
+ double ascent_max; // Maximum ascent of all glyphs in font.
+ double descent_max; // Maximum descent of all glyphs in font.
+
+ }; // End FontMetrics
/// see _enum_converter()
struct EnumConversionItem {
@@ -664,11 +718,12 @@ private:
LengthAdjust lengthAdjust;
// a few functions for some of the more complicated style accesses
- float styleComputeFontSize() const;
/// The return value must be freed with pango_font_description_free()
PangoFontDescription *styleGetFontDescription() const;
font_instance *styleGetFontInstance() const;
Direction styleGetBlockProgression() const;
+ SPCSSTextOrientation styleGetTextOrientation() const;
+ SPCSSBaseline styleGetDominantBaseline() const;
Alignment styleGetAlignment(Direction para_direction, bool try_text_align) const;
};
@@ -707,6 +762,22 @@ private:
return TOP_TO_BOTTOM;
}
+ /** The overall text-orientation of the whole flow. */
+ inline SPCSSTextOrientation _blockTextOrientation() const
+ {
+ if(!_input_stream.empty())
+ return static_cast<InputStreamTextSource*>(_input_stream.front())->styleGetTextOrientation();
+ return SP_CSS_TEXT_ORIENTATION_MIXED;
+ }
+
+ /** The overall text-orientation of the whole flow. */
+ inline SPCSSBaseline _blockBaseline() const
+ {
+ if(!_input_stream.empty())
+ return static_cast<InputStreamTextSource*>(_input_stream.front())->styleGetDominantBaseline();
+ return SP_CSS_BASELINE_AUTO;
+ }
+
/** so that LEFT_TO_RIGHT == RIGHT_TO_LEFT but != TOP_TO_BOTTOM */
static bool _directions_are_orthogonal(Direction d1, Direction d2);
@@ -748,6 +819,7 @@ private:
float x; /// relative to the start of the chunk
float y; /// relative to the current line's baseline
float rotation; /// absolute, modulo any object transforms, which we don't know about
+ Orientation orientation; /// Orientation of glyph in vertical text
float width;
float vertical_scale; /// to implement lengthAdjust="spacingAndGlyphs" that must scale glyphs only horizontally; instead we change font size and then undo that change vertically only
inline Span const & span(Layout const *l) const {return l->_spans[l->_characters[in_character].in_span];}
@@ -772,8 +844,9 @@ private:
float x_start; /// relative to the start of the chunk
float x_end; /// relative to the start of the chunk
inline float width() const {return std::abs(x_start - x_end);}
- LineHeight line_height;
+ FontMetrics line_height;
double baseline_shift; /// relative to the line's baseline
+ SPCSSTextOrientation text_orientation;
Direction direction; /// See CSS3 section 3.2. Either rtl or ltr
Direction block_progression; /// See CSS3 section 3.2. The direction in which lines go.
unsigned in_input_stream_item;
diff --git a/src/libnrtype/TextWrapper.cpp b/src/libnrtype/TextWrapper.cpp
index 380e9ba3f..124f3f7b4 100644
--- a/src/libnrtype/TextWrapper.cpp
+++ b/src/libnrtype/TextWrapper.cpp
@@ -862,7 +862,7 @@ void text_wrapper::MeasureBoxes(void)
for (int i = 0; i < nbBox; i++) {
boxes[i].ascent = 0;
boxes[i].descent = 0;
- boxes[i].leading = 0;
+ boxes[i].xheight = 0;
boxes[i].width = 0;
PangoFont *curPF = glyph_text[boxes[i].g_st].font;
@@ -870,7 +870,7 @@ void text_wrapper::MeasureBoxes(void)
PangoFontDescription *pfd = pango_font_describe(curPF);
font_instance *curF = f_src->Face(pfd);
if ( curF ) {
- curF->FontMetrics(boxes[i].ascent, boxes[i].descent, boxes[i].leading);
+ curF->FontMetrics(boxes[i].ascent, boxes[i].descent, boxes[i].xheight);
curF->Unref();
}
pango_font_description_free(pfd);
diff --git a/src/libnrtype/font-instance.h b/src/libnrtype/font-instance.h
index 5a71e353b..2fac7c19b 100644
--- a/src/libnrtype/font-instance.h
+++ b/src/libnrtype/font-instance.h
@@ -57,19 +57,31 @@ public:
// nota: all coordinates returned by these functions are on a [0..1] scale; you need to multiply
// by the fontsize to get the real sizes
+
+ // Return 2geom pathvector for glyph. Deallocated when font instance dies.
Geom::PathVector* PathVector(int glyph_id);
- // returns the 2geom-type pathvector for this glyph. no refcounting needed, it's deallocated when the font_instance dies
+
+ // Horizontal advance if 'vertical' is false, vertical advance if true.
double Advance(int glyph_id, bool vertical);
- // nominal advance of the font.
+
+ double GetTypoAscent() { return _ascent; }
+ double GetTypoDescent() { return _descent; }
+ double GetXHeight() { return _xheight; }
+ double GetMaxAscent() { return _ascent_max; }
+ double GetMaxDescent() { return _descent_max; }
+ const double* GetBaselines() { return _baselines; }
+
bool FontMetrics(double &ascent, double &descent, double &leading);
- bool FontDecoration(double &underline_position, double &underline_thickness,
- double &linethrough_position, double &linethrough_thickness);
+ bool FontDecoration(double &underline_position, double &underline_thickness,
+ double &linethrough_position, double &linethrough_thickness);
bool FontSlope(double &run, double &rise);
// for generating slanted cursors for oblique fonts
- Geom::OptRect BBox(int glyph_id);
+ Geom::OptRect BBox(int glyph_id);
private:
void FreeTheFace();
+ // Find ascent, descent, x-height, and baselines.
+ void FindFontMetrics();
// Temp: make public
public:
@@ -81,6 +93,17 @@ public:
// as long as pFont is valid, theFace is too
#endif
+private:
+
+ // Font metrics in em-box units
+ double _ascent; // Typographic ascent.
+ double _descent; // Typographic descent.
+ double _xheight; // x-height of font.
+ double _ascent_max; // Maxiumum ascent of all glyphs in font.
+ double _descent_max; // Maxiumum descent of all glyphs in font.
+
+ // Baselines
+ double _baselines[SP_CSS_BASELINE_SIZE];
};
diff --git a/src/libnrtype/one-box.h b/src/libnrtype/one-box.h
index c868cf23f..c3b36a3ce 100644
--- a/src/libnrtype/one-box.h
+++ b/src/libnrtype/one-box.h
@@ -9,7 +9,7 @@
// this time for sp-typeset
struct one_box {
int g_st, g_en; ///< First and last glyph of this word.
- double ascent, descent, leading;
+ double ascent, descent, xheight;
double width;
bool word_start, word_end;
};