From ce697d7ebfb1e4affce10805c89445244d29388a Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Wed, 28 Oct 2015 14:40:01 +0100 Subject: Implement 'text-orientation' with user interface. Update 'writing-mode', adding 'vertical-lr'. Overhaul vertical text. Eliminate any use of "internal" leading in glyph metrics. Etc. (bzr r14430.1.1) --- src/libnrtype/FontInstance.cpp | 25 ++- src/libnrtype/Layout-TNG-Compute.cpp | 284 ++++++++++++++++----------- src/libnrtype/Layout-TNG-OutIter.cpp | 14 +- src/libnrtype/Layout-TNG-Output.cpp | 27 ++- src/libnrtype/Layout-TNG-Scanline-Maker.h | 14 +- src/libnrtype/Layout-TNG-Scanline-Makers.cpp | 32 ++- src/libnrtype/Layout-TNG.h | 51 ++++- 7 files changed, 280 insertions(+), 167 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/FontInstance.cpp b/src/libnrtype/FontInstance.cpp index a5572c517..6b2e030d1 100644 --- a/src/libnrtype/FontInstance.cpp +++ b/src/libnrtype/FontInstance.cpp @@ -374,10 +374,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 +459,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 = { @@ -529,6 +535,15 @@ bool font_instance::FontMetrics(double &ascent,double &descent,double &leading) leading=fabs(((double)theFace->height)/((double)theFace->units_per_EM)); leading-=ascent+descent; #endif + + // CSS dictates em size is ascent + descent + double em = ascent + descent; + if( em <= 0 ) { + return false; // Pathological + } + ascent /= em; + descent /= em; + leading /= em; return true; } diff --git a/src/libnrtype/Layout-TNG-Compute.cpp b/src/libnrtype/Layout-TNG-Compute.cpp index c4b0a5bee..4201d052c 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 attributes. We change span when we encounter one. @@ -221,7 +195,7 @@ class Layout::Calculator void _buildPangoItemizationForPara(ParagraphInfo *para) const; static void _computeFontLineHeight(font_instance *font, double font_size, - SPStyle const *style, LineHeight *line_height, + SPStyle const *style, FontMetrics *line_height, double *line_height_multiplier); unsigned _buildSpansForPara(ParagraphInfo *para) const; @@ -270,7 +244,7 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ bool _goToNextWrapShape(); bool _findChunksForLine(ParagraphInfo const ¶, UnbrokenSpanPosition *start_span_pos, - std::vector *chunk_info, LineHeight *line_height); + std::vector *chunk_info, FontMetrics *line_height); static inline PangoLogAttr const &_charAttributes(ParagraphInfo const ¶, UnbrokenSpanPosition const &span_pos) @@ -282,7 +256,7 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ UnbrokenSpanPosition const &start_span_pos, ScanlineMaker::ScanRun const &scan_run, std::vector *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 +274,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 +359,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 +463,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 ¶, LineHeight const &line_height, std::vector const &chunk_info) + void _outputLine(ParagraphInfo const ¶, FontMetrics const &line_height, std::vector const &chunk_info) { TRACE(("Start _outputLine\n")); if (chunk_info.empty()) { @@ -486,7 +475,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.getAscent(); + } + } + new_line.in_shape = _current_shape_index; _flow._lines.push_back(new_line); @@ -527,17 +526,16 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ // If "y" attribute is set, use it (initial "y" attributes in // other than the first have already been stripped for // 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 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 +554,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 +581,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 +599,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 +608,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 +629,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 +672,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,28 +687,52 @@ 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 */ - 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 + + // 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. + new_glyph.orientation = ORIENTATION_SIDEWAYS; + + new_glyph.x = current_x + unbroken_span_glyph_info->geometry.x_offset * font_size_multiplier; + + new_glyph.y =_y_offset + + unbroken_span.baseline_shift + + (unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier) + + 0.5 * (new_span.line_height.descent - new_span.line_height.ascent); + + 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 = current_x + unbroken_span_glyph_info->geometry.x_offset * font_size_multiplier + + new_span.line_height.ascent; + + new_glyph.y = _y_offset + // Does baseline shift have any meaning here? + unbroken_span.baseline_shift + + (unbroken_span_glyph_info->geometry.y_offset - + unbroken_span_glyph_info->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_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; + // Horizontal text + new_glyph.x = current_x + unbroken_span_glyph_info->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; + unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier; + 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) { @@ -718,8 +743,10 @@ 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; @@ -781,15 +808,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(_flow._input_stream[unbroken_span.input_index])->width; + current_x += static_cast(_flow._input_stream[unbroken_span.input_index])->width; } new_span.x_end = new_span.x_start + x_in_span_last; @@ -978,6 +1005,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 +1027,10 @@ void Layout::Calculator::_buildPangoItemizationForPara(ParagraphInfo *para) con } else if (_flow._input_stream[input_index]->Type() == TEXT_SOURCE) { Layout::InputStreamTextSource *text_source = static_cast(_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 +1056,23 @@ 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(_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])); + 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); - 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])); } } - 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,43 +1096,45 @@ 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. + * Gets the ascent and descent for a font given the 'font-size' propert and 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 */ +// THIS FUNCTION SHOULD NOT BE NECESSARY void Layout::Calculator::_computeFontLineHeight(font_instance *font, double font_size, - SPStyle const *style, LineHeight *line_height, + SPStyle const *style, FontMetrics *font_metrics, double *line_height_multiplier) { if (font == NULL) { - line_height->setZero(); + font_metrics->setZero(); *line_height_multiplier = 1.0; } else { - font->FontMetrics(line_height->ascent, line_height->descent, line_height->leading); + font->FontMetrics(font_metrics->ascent, font_metrics->descent, font_metrics->leading); } - *line_height *= font_size; + *font_metrics *= font_size; // yet another borked SPStyle member that we're going to have to fix ourselves + // To do: check if it really is still borked. 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(); + *line_height_multiplier = style->line_height.computed; return; case SP_CSS_UNIT_EX: - *line_height_multiplier = style->line_height.value * 0.5 * font_size / line_height->total(); + *line_height_multiplier = 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(); + *line_height_multiplier = style->line_height.value; return; default: // absolute values - *line_height_multiplier = style->line_height.computed / line_height->total(); + *line_height_multiplier = style->line_height.computed / font_metrics->emSize(); return; } break; @@ -1115,7 +1143,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(); + *line_height_multiplier = LINE_HEIGHT_NORMAL; } bool compareGlyphWidth(const PangoGlyphInfo &a, const PangoGlyphInfo &b) @@ -1155,7 +1183,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 +1221,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(); @@ -1240,6 +1269,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(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 +1277,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, ¶->pango_items[pango_item_index].item->analysis, @@ -1329,6 +1361,7 @@ unsigned Layout::Calculator::_buildSpansForPara(ParagraphInfo *para) const // 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())); @@ -1399,7 +1432,7 @@ bool Layout::Calculator::_goToNextWrapShape() bool Layout::Calculator::_findChunksForLine(ParagraphInfo const ¶, UnbrokenSpanPosition *start_span_pos, std::vector *chunk_info, - LineHeight *line_height) + FontMetrics *line_height) { // init the initial line_height if (start_span_pos->iter_span == para.unbroken_spans.end()) { @@ -1422,7 +1455,6 @@ bool Layout::Calculator::_findChunksForLine(ParagraphInfo const ¶, // 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 +1506,7 @@ bool Layout::Calculator::_buildChunksInScanRun(ParagraphInfo const ¶, UnbrokenSpanPosition const &start_span_pos, ScanlineMaker::ScanRun const &scan_run, std::vector *chunk_info, - LineHeight *line_height) const + FontMetrics *line_height) const { ChunkInfo new_chunk; new_chunk.text_width = 0.0; @@ -1511,16 +1543,17 @@ bool Layout::Calculator::_buildChunksInScanRun(ParagraphInfo const ¶, } // 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 +1573,7 @@ bool Layout::Calculator::_buildChunksInScanRun(ParagraphInfo const ¶, 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 +1652,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 +1686,10 @@ bool Layout::Calculator::calculate() if (_scanline_maker == NULL) break; // we're trying to flow past the last wrap shape - _buildPangoItemizationForPara(¶); + // Break things up into little pango units with unique direction, gravity, etc. + _buildPangoItemizationForPara(¶); + + // Do shaping (convert characters to glyphs) unsigned para_end_input_index = _buildSpansForPara(¶); if (_flow._input_stream[para.first_input_index]->Type() == TEXT_SOURCE) @@ -1679,7 +1728,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 { @@ -1757,7 +1806,7 @@ void Layout::_calculateCursorShapeForEmpty() font_instance *font = text_source->styleGetFontInstance(); double font_size = text_source->styleComputeFontSize(); double caret_slope_run = 0.0, caret_slope_rise = 1.0; - LineHeight line_height; + FontMetrics line_height; if (font) { const_cast(font)->FontSlope(caret_slope_run, caret_slope_rise); font->FontMetrics(line_height.ascent, line_height.descent, line_height.leading); @@ -1780,10 +1829,13 @@ void Layout::_calculateCursorShapeForEmpty() ShapeScanlineMaker scanline_maker(_input_wrap_shapes.front().shape, block_progression); std::vector 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-OutIter.cpp b/src/libnrtype/Layout-TNG-OutIter.cpp index 137fe0a0f..c7275c24e 100644 --- a/src/libnrtype/Layout-TNG-OutIter.cpp +++ b/src/libnrtype/Layout-TNG-OutIter.cpp @@ -120,7 +120,7 @@ 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 = {0.0, 0.0, 0.0}; 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 +340,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 +404,7 @@ std::vector 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 +510,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(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..0bbf266c7 100644 --- a/src/libnrtype/Layout-TNG-Output.cpp +++ b/src/libnrtype/Layout-TNG-Output.cpp @@ -93,26 +93,41 @@ void Layout::_clearOutputObjects() _path_fitted = NULL; } -void Layout::LineHeight::max(LineHeight const &other) +void Layout::FontMetrics::max(FontMetrics const &other) { if (other.ascent > ascent) ascent = other.ascent; if (other.descent > descent) descent = other.descent; - if (other.leading > leading) leading = other.leading; +} + +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; } @@ -129,7 +144,7 @@ void Layout::show(DrawingGroup *in_arena, Geom::OptRect const &paintbox) const 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(); + 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; @@ -580,7 +595,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 makeScanline(Layout::LineHeight const &line_height) =0; + virtual std::vector 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 makeScanline(Layout::LineHeight const &line_height); + virtual std::vector 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 makeScanline(Layout::LineHeight const &line_height); + virtual std::vector 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::InfiniteScanlineMaker::makeScanline(Layout::LineHeight const &line_height) +std::vector Layout::InfiniteScanlineMaker::makeScanline(Layout::FontMetrics const &line_height) { std::vector runs(1); runs[0].x_start = _x; @@ -54,12 +52,10 @@ std::vector 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::ShapeScanlineMaker::makeScanline(Layout::LineHeight const &line_height) +std::vector 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(); 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 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..66cc96d3f 100644 --- a/src/libnrtype/Layout-TNG.h +++ b/src/libnrtype/Layout-TNG.h @@ -22,6 +22,7 @@ #include #include #include +#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,18 +607,45 @@ public: //@} - /// it's useful for this to be public so that ScanlineMaker can use it - struct LineHeight { + + /** + * Keep track of font metrics. Two use cases: + * 1. Keep track of ascent and descent 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. + */ + struct FontMetrics { + 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 + double leading; // Not used... to be removed (requires change to font->FontMetrics() + + // 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 = 0.0;} + + // For scaling for 'font-size'. + inline FontMetrics& operator*=(double x) {ascent *= x; descent *= 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 getAscent() const {return ascent; } inline double getDescent() const {return descent; } - inline double getLeading() const {return leading; } }; /// see _enum_converter() @@ -748,6 +779,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 +804,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; -- cgit v1.2.3 From ead60e7c6226f24be5d11b550cea287fe2ddee11 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Tue, 3 Nov 2015 11:46:37 +0100 Subject: Vertical baseline depends on block 'text-orientation' value. (bzr r14430.1.2) --- src/libnrtype/Layout-TNG-Compute.cpp | 37 ++++++++++++++++++++++++++++-------- src/libnrtype/Layout-TNG-Input.cpp | 5 +++++ src/libnrtype/Layout-TNG.h | 9 +++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/Layout-TNG-Compute.cpp b/src/libnrtype/Layout-TNG-Compute.cpp index 4201d052c..1fb0e2d8d 100644 --- a/src/libnrtype/Layout-TNG-Compute.cpp +++ b/src/libnrtype/Layout-TNG-Compute.cpp @@ -700,10 +700,20 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ new_glyph.x = current_x + unbroken_span_glyph_info->geometry.x_offset * font_size_multiplier; - new_glyph.y =_y_offset + - unbroken_span.baseline_shift + - (unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier) - + 0.5 * (new_span.line_height.descent - new_span.line_height.ascent); + // Baseline is determined by overall block (i.e. ) 'text-orientation' value. + // (It's actually a bit more complicated but this should handle most cases.) + if( _flow._blockTextOrientation() == SP_CSS_TEXT_ORIENTATION_SIDEWAYS ) { + // Baseline is alphabetic + new_glyph.y =_y_offset + + unbroken_span.baseline_shift + + (unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier); + } else { + // Baseline is center + new_glyph.y =_y_offset + + unbroken_span.baseline_shift + + (unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier) + + 0.5 * (new_span.line_height.descent - new_span.line_height.ascent); + } new_glyph.width = new_span.font_size * para.pango_items[unbroken_span.pango_item_index].font->Advance(unbroken_span_glyph_info->glyph, false); @@ -713,10 +723,21 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ new_glyph.x = current_x + unbroken_span_glyph_info->geometry.x_offset * font_size_multiplier + new_span.line_height.ascent; - new_glyph.y = _y_offset + // Does baseline shift have any meaning here? - unbroken_span.baseline_shift + - (unbroken_span_glyph_info->geometry.y_offset - - unbroken_span_glyph_info->geometry.width * 0.5) * font_size_multiplier; + // Baseline is determined by overall block (i.e. ) 'text-orientation' value. + // (It's actually a bit more complicated but this should handle most cases.) + if( _flow._blockTextOrientation() == SP_CSS_TEXT_ORIENTATION_SIDEWAYS ) { + // Baseline is alphabetic .. sideways + new_glyph.y =_y_offset + + unbroken_span.baseline_shift + + (unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier) - + new_span.line_height.descent; + } else { + // Baseline is center + new_glyph.y = _y_offset + // Does baseline shift have any meaning here? + unbroken_span.baseline_shift + + (unbroken_span_glyph_info->geometry.y_offset - + unbroken_span_glyph_info->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_info->glyph, true); if( new_glyph.width == 0 ) { diff --git a/src/libnrtype/Layout-TNG-Input.cpp b/src/libnrtype/Layout-TNG-Input.cpp index 77480cebe..d99433adf 100644 --- a/src/libnrtype/Layout-TNG-Input.cpp +++ b/src/libnrtype/Layout-TNG-Input.cpp @@ -192,6 +192,11 @@ Layout::Direction Layout::InputStreamTextSource::styleGetBlockProgression() cons return TOP_TO_BOTTOM; } +SPCSSTextOrientation Layout::InputStreamTextSource::styleGetTextOrientation() const +{ + return ((SPCSSTextOrientation)style->text_orientation.computed); +} + static Layout::Alignment text_anchor_to_alignment(unsigned anchor, Layout::Direction para_direction) { switch (anchor) { diff --git a/src/libnrtype/Layout-TNG.h b/src/libnrtype/Layout-TNG.h index 66cc96d3f..6875fa14a 100644 --- a/src/libnrtype/Layout-TNG.h +++ b/src/libnrtype/Layout-TNG.h @@ -700,6 +700,7 @@ private: PangoFontDescription *styleGetFontDescription() const; font_instance *styleGetFontInstance() const; Direction styleGetBlockProgression() const; + SPCSSTextOrientation styleGetTextOrientation() const; Alignment styleGetAlignment(Direction para_direction, bool try_text_align) const; }; @@ -738,6 +739,14 @@ private: return TOP_TO_BOTTOM; } + /** The overall text-orientation of the whole flow. */ + inline SPCSSTextOrientation _blockTextOrientation() const + { + if(!_input_stream.empty()) + return static_cast(_input_stream.front())->styleGetTextOrientation(); + return SP_CSS_TEXT_ORIENTATION_MIXED; + } + /** so that LEFT_TO_RIGHT == RIGHT_TO_LEFT but != TOP_TO_BOTTOM */ static bool _directions_are_orthogonal(Direction d1, Direction d2); -- cgit v1.2.3 From 83dac189ff21c59be9b4f912e0d0e9690e710a4d Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Tue, 3 Nov 2015 13:19:36 +0100 Subject: Rearrange code to make handling of baseline clearer. (bzr r14430.1.3) --- src/libnrtype/Layout-TNG-Compute.cpp | 56 +++++++++++++++--------------------- 1 file changed, 23 insertions(+), 33 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/Layout-TNG-Compute.cpp b/src/libnrtype/Layout-TNG-Compute.cpp index 1fb0e2d8d..7cae1eb8a 100644 --- a/src/libnrtype/Layout-TNG-Compute.cpp +++ b/src/libnrtype/Layout-TNG-Compute.cpp @@ -687,9 +687,18 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ else new_glyph.vertical_scale = 1.0; + // 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; + if (_block_progression == LEFT_TO_RIGHT || _block_progression == RIGHT_TO_LEFT) { // Vertical text + 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 && @@ -698,45 +707,26 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ // Sideways orientation (Latin characters, CJK punctuation), 90deg rotation done at output stage. new_glyph.orientation = ORIENTATION_SIDEWAYS; - new_glyph.x = current_x + unbroken_span_glyph_info->geometry.x_offset * font_size_multiplier; - // Baseline is determined by overall block (i.e. ) 'text-orientation' value. - // (It's actually a bit more complicated but this should handle most cases.) - if( _flow._blockTextOrientation() == SP_CSS_TEXT_ORIENTATION_SIDEWAYS ) { - // Baseline is alphabetic - new_glyph.y =_y_offset + - unbroken_span.baseline_shift + - (unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier); - } else { - // Baseline is center - new_glyph.y =_y_offset + - unbroken_span.baseline_shift + - (unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier) + - 0.5 * (new_span.line_height.descent - new_span.line_height.ascent); + if( _flow._blockTextOrientation() != SP_CSS_TEXT_ORIENTATION_SIDEWAYS ) { + // Baseline is center (shift: alphabetic to center) + new_glyph.y += 0.5 * (new_span.line_height.descent - new_span.line_height.ascent); } 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 = current_x + unbroken_span_glyph_info->geometry.x_offset * font_size_multiplier + - new_span.line_height.ascent; + + new_glyph.x += new_span.line_height.ascent; // Baseline is determined by overall block (i.e. ) 'text-orientation' value. - // (It's actually a bit more complicated but this should handle most cases.) if( _flow._blockTextOrientation() == SP_CSS_TEXT_ORIENTATION_SIDEWAYS ) { - // Baseline is alphabetic .. sideways - new_glyph.y =_y_offset + - unbroken_span.baseline_shift + - (unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier) - - new_span.line_height.descent; + // Baseline is alphabetic .. sideways (shift: left edge to alphabetic) + new_glyph.y -= new_span.line_height.descent; } else { - // Baseline is center - new_glyph.y = _y_offset + // Does baseline shift have any meaning here? - unbroken_span.baseline_shift + - (unbroken_span_glyph_info->geometry.y_offset - - unbroken_span_glyph_info->geometry.width * 0.5) * font_size_multiplier; + // Baseline is center (shift: left edge to center) + new_glyph.y -= unbroken_span_glyph_info->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_info->glyph, true); @@ -747,15 +737,15 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ } } else { // Horizontal text - new_glyph.x = current_x + unbroken_span_glyph_info->geometry.x_offset * font_size_multiplier; - new_glyph.y = _y_offset - - unbroken_span.baseline_shift + - unbroken_span_glyph_info->geometry.y_offset * font_size_multiplier; + new_glyph.y -= delta_y; + 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_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 @@ -772,7 +762,7 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ } 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; -- cgit v1.2.3 From 322711ea12ec4ec3b129e3415fa1a1bd8bc7060f Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Tue, 10 Nov 2015 13:57:55 +0100 Subject: 'direction' is an inherited property, remove 'set' requirement. (bzr r14430.1.4) --- src/libnrtype/Layout-TNG-Compute.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/Layout-TNG-Compute.cpp b/src/libnrtype/Layout-TNG-Compute.cpp index 7cae1eb8a..8f7f6bca0 100644 --- a/src/libnrtype/Layout-TNG-Compute.cpp +++ b/src/libnrtype/Layout-TNG-Compute.cpp @@ -1072,11 +1072,10 @@ void Layout::Calculator::_buildPangoItemizationForPara(ParagraphInfo *para) con 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(_flow._input_stream[para->first_input_index]); - if (text_source->style->direction.set) { - 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); - } + + 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 ) { -- cgit v1.2.3 From d136162e0abe6b045ac791b8eb0e16ee51b3f7cc Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Fri, 13 Nov 2015 15:35:25 +0100 Subject: Replace leading by more useful x-height. Use OS/2 font metrics when available. Use otmMacAscent/otmMacDescent rather than otmAscent/otmDescent. Actually both should be available as they have different purposes... (bzr r14430.1.5) --- src/libnrtype/FontInstance.cpp | 66 ++++++++++++++++++++++++++++++------ src/libnrtype/Layout-TNG-Compute.cpp | 6 ++-- src/libnrtype/Layout-TNG.h | 9 ++--- src/libnrtype/TextWrapper.cpp | 4 +-- src/libnrtype/one-box.h | 2 +- 5 files changed, 66 insertions(+), 21 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/FontInstance.cpp b/src/libnrtype/FontInstance.cpp index 6b2e030d1..5853d5217 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 #include <2geom/pathvector.h> #include <2geom/path-sink.h> @@ -507,7 +508,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; @@ -516,34 +517,77 @@ bool font_instance::FontMetrics(double &ascent,double &descent,double &leading) if ( theFace == NULL ) { return false; } + + // CSS2 recommends using the OS/2 values sTypoAscender and sTypoDescender + // for the ascender and descender values: + // http://www.w3.org/TR/CSS2/visudet.html#sTypoAscender + // 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. + #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); + ascent=fabs(otm.otmMacAscent*scale); + descent=fabs(otm.otmMacDescent*scale); + xheight=fabs(otm.otmXHeight*scale); + // May not be necessary... but if OS/2 table is missing or not version 2 or higher, + // xheight might be set to 0. + if( xheight = 0.0 ) xheight = ascent/2.0; //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; + + 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)); + } + + // We must find x-height ourselves! First try OS/2 table. + 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 = ascent/2.0; + } + } #endif - // CSS dictates em size is ascent + descent + // CSS dictates em size is ascent + descent.... but this doesn't seem to be used in practice. + // The em size is what the font reports... double em = ascent + descent; if( em <= 0 ) { - return false; // Pathological + return false; // Pathological } ascent /= em; descent /= em; - leading /= em; + xheight /= em; + + // gchar* font_name = pango_font_description_to_string( descr ); + // std::cout << "Font: " << (font_name ? font_name : ("Null")) << std::endl; + // g_free( font_name ); + // std::cout << " ascent: " << ascent << " descent: " << descent + // << " x-height: " << xheight << "\n" << std::endl; + return true; } diff --git a/src/libnrtype/Layout-TNG-Compute.cpp b/src/libnrtype/Layout-TNG-Compute.cpp index 8f7f6bca0..6b5697b10 100644 --- a/src/libnrtype/Layout-TNG-Compute.cpp +++ b/src/libnrtype/Layout-TNG-Compute.cpp @@ -1121,7 +1121,7 @@ void Layout::Calculator::_computeFontLineHeight(font_instance *font, double font *line_height_multiplier = 1.0; } else { - font->FontMetrics(font_metrics->ascent, font_metrics->descent, font_metrics->leading); + font->FontMetrics(font_metrics->ascent, font_metrics->descent, font_metrics->xheight); } *font_metrics *= font_size; @@ -1819,13 +1819,13 @@ void Layout::_calculateCursorShapeForEmpty() FontMetrics line_height; if (font) { const_cast(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; + line_height.xheight = 0.0; } double caret_slope = atan2(caret_slope_run, caret_slope_rise); _empty_cursor_shape.height = font_size / cos(caret_slope); diff --git a/src/libnrtype/Layout-TNG.h b/src/libnrtype/Layout-TNG.h index 6875fa14a..c2895e05f 100644 --- a/src/libnrtype/Layout-TNG.h +++ b/src/libnrtype/Layout-TNG.h @@ -610,7 +610,7 @@ public: /** * Keep track of font metrics. Two use cases: - * 1. Keep track of ascent and descent of an individual font. + * 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 @@ -626,16 +626,16 @@ public: double ascent; double descent; - double leading; // Not used... to be removed (requires change to font->FontMetrics() + double xheight; // 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 = 0.0;} + inline void setZero() {ascent = descent = xheight = 0.0;} // For scaling for 'font-size'. - inline FontMetrics& operator*=(double x) {ascent *= x; descent *= x; return *this;} + inline FontMetrics& operator*=(double x) {ascent *= x; descent *= x; xheight *= 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. @@ -646,6 +646,7 @@ public: inline double getAscent() const {return ascent; } inline double getDescent() const {return descent; } + inline double getXheight() const {return xheight; } }; /// see _enum_converter() 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/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; }; -- cgit v1.2.3 From 81760f171e6b7bce537fd94cae3e3e1a6d82e886 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Wed, 18 Nov 2015 10:34:55 +0100 Subject: Find font metrics when font is initialized. Fill baseline table. Separately track typographic ascent/descent and maximum ascent/descent. (bzr r14430.1.7) --- src/libnrtype/FontInstance.cpp | 267 ++++++++++++++++++++++++++++++----------- src/libnrtype/font-instance.h | 33 ++++- 2 files changed, 222 insertions(+), 78 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/FontInstance.cpp b/src/libnrtype/FontInstance.cpp index 5853d5217..4578efe8f 100644 --- a/src/libnrtype/FontInstance.cpp +++ b/src/libnrtype/FontInstance.cpp @@ -184,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; + _baselines[ SP_CSS_BASELINE_MIDDLE ] = 0.5 * _xheight; + _baselines[ SP_CSS_BASELINE_TEXT_BEFORE_EDGE ] = -_descent; + _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = _ascent; } font_instance::~font_instance(void) @@ -260,6 +274,7 @@ void font_instance::InitTheFace() FT_Select_Charmap(theFace,ft_encoding_unicode) && FT_Select_Charmap(theFace,ft_encoding_symbol); } #endif + FindFontMetrics(); } } @@ -518,83 +533,16 @@ bool font_instance::FontMetrics(double &ascent,double &descent,double &xheight) return false; } - // CSS2 recommends using the OS/2 values sTypoAscender and sTypoDescender - // for the ascender and descender values: - // http://www.w3.org/TR/CSS2/visudet.html#sTypoAscender - // 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. - -#ifdef USE_PANGO_WIN32 - OUTLINETEXTMETRIC otm; - if ( !GetOutlineTextMetrics(parent->hScreenDC,sizeof(otm),&otm) ) { - return false; - } - - double scale=1.0/parent->fontSize; - ascent=fabs(otm.otmMacAscent*scale); - descent=fabs(otm.otmMacDescent*scale); - xheight=fabs(otm.otmXHeight*scale); - // May not be necessary... but if OS/2 table is missing or not version 2 or higher, - // xheight might be set to 0. - if( xheight = 0.0 ) xheight = ascent/2.0; - //otmSubscriptSize, otmSubscriptOffset, otmSuperscriptSize, otmSuperscriptOffset, -#else - if ( theFace->units_per_EM == 0 ) { - return false; // 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)); - } - - // We must find x-height ourselves! First try OS/2 table. - 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 = ascent/2.0; - } - } -#endif - - // CSS dictates em size is ascent + descent.... but this doesn't seem to be used in practice. - // The em size is what the font reports... - double em = ascent + descent; - if( em <= 0 ) { - return false; // Pathological - } - ascent /= em; - descent /= em; - xheight /= em; - - // gchar* font_name = pango_font_description_to_string( descr ); - // std::cout << "Font: " << (font_name ? font_name : ("Null")) << std::endl; - // g_free( font_name ); - // std::cout << " ascent: " << ascent << " descent: " << descent - // << " x-height: " << xheight << "\n" << std::endl; + 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; } @@ -721,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; // Definition + _baselines[ SP_CSS_BASELINE_MIDDLE ] = 0.5 * _xheight; // Definition + _baselines[ SP_CSS_BASELINE_TEXT_BEFORE_EDGE ] = -_descent; // Definition + _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = _ascent; // 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; // Definition + _baselines[ SP_CSS_BASELINE_MIDDLE ] = 0.5 * _xheight; // Definition + _baselines[ SP_CSS_BASELINE_TEXT_BEFORE_EDGE ] = -_descent; // Definition + _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = _ascent; // 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/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]; }; -- cgit v1.2.3 From 1c89d667e01b9ed8a8aa283ca7332ab61857c6bc Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Wed, 18 Nov 2015 11:17:24 +0100 Subject: Turn FontMetrics into a class. Add maximum ascent/descent variables. (bzr r14430.1.8) --- src/libnrtype/Layout-TNG-Compute.cpp | 2 +- src/libnrtype/Layout-TNG-OutIter.cpp | 3 ++- src/libnrtype/Layout-TNG-Output.cpp | 26 ++++++++++++++++----- src/libnrtype/Layout-TNG.h | 44 +++++++++++++++++++++++++++--------- 4 files changed, 56 insertions(+), 19 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/Layout-TNG-Compute.cpp b/src/libnrtype/Layout-TNG-Compute.cpp index 6b5697b10..2081b5a6d 100644 --- a/src/libnrtype/Layout-TNG-Compute.cpp +++ b/src/libnrtype/Layout-TNG-Compute.cpp @@ -482,7 +482,7 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ // Vertical text, use em box center as baseline new_line.baseline_y += 0.5 * line_height.emSize(); } else { - new_line.baseline_y += line_height.getAscent(); + new_line.baseline_y += line_height.getTypoAscent(); } } diff --git a/src/libnrtype/Layout-TNG-OutIter.cpp b/src/libnrtype/Layout-TNG-OutIter.cpp index c7275c24e..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++) { - FontMetrics 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); diff --git a/src/libnrtype/Layout-TNG-Output.cpp b/src/libnrtype/Layout-TNG-Output.cpp index 0bbf266c7..9557bc84c 100644 --- a/src/libnrtype/Layout-TNG-Output.cpp +++ b/src/libnrtype/Layout-TNG-Output.cpp @@ -93,10 +93,24 @@ void Layout::_clearOutputObjects() _path_fitted = NULL; } +void Layout::FontMetrics::set(font_instance *font) +{ + 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.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 ) { @@ -142,8 +156,8 @@ void Layout::show(DrawingGroup *in_arena, Geom::OptRect const &paintbox) const InputStreamTextSource const *text_source = static_cast(_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.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)){ @@ -188,8 +202,8 @@ void Layout::show(DrawingGroup *in_arena, Geom::OptRect const &paintbox) const // save the starting coordinates for the line - these are needed for figuring out dot/dash/wave phase (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.getTypoAscent(), + _spans[span_index].line_height.getTypoDescent(), glyph_matrix.translation()[Geom::X] - phase0 ); } diff --git a/src/libnrtype/Layout-TNG.h b/src/libnrtype/Layout-TNG.h index c2895e05f..ccf4bbe43 100644 --- a/src/libnrtype/Layout-TNG.h +++ b/src/libnrtype/Layout-TNG.h @@ -622,20 +622,32 @@ public: * * It's useful for this to be public so that ScanlineMaker can use it. */ - struct FontMetrics { - - double ascent; - double descent; - double xheight; + class FontMetrics { + public: + FontMetrics() { reset(); } + + void reset() { + ascent = 0.8; + descent = -0.2; + xheight = 0.5; + ascent_max = 0.8; + descent_max = 0.2; + } + + inline 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 = 0.0;} + 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; return *this;} + 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. @@ -644,10 +656,20 @@ public: /// Calculate the effective ascent and descent including half "leading". void computeEffective( const double &line_height ); - inline double getAscent() const {return ascent; } - inline double getDescent() const {return descent; } - inline double getXheight() const {return xheight; } - }; + 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 { -- cgit v1.2.3 From bfc8e4559285237d13c45e37240a9561c8af5264 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Wed, 18 Nov 2015 15:53:56 +0100 Subject: Code cleanup. Remove/simplify some functions. (bzr r14430.1.9) --- src/libnrtype/Layout-TNG-Compute.cpp | 70 +++++++++++++++--------------------- src/libnrtype/Layout-TNG-Input.cpp | 46 ------------------------ src/libnrtype/Layout-TNG.h | 3 +- 3 files changed, 29 insertions(+), 90 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/Layout-TNG-Compute.cpp b/src/libnrtype/Layout-TNG-Compute.cpp index 2081b5a6d..05cc638f7 100644 --- a/src/libnrtype/Layout-TNG-Compute.cpp +++ b/src/libnrtype/Layout-TNG-Compute.cpp @@ -194,9 +194,8 @@ class Layout::Calculator void _buildPangoItemizationForPara(ParagraphInfo *para) const; - static void _computeFontLineHeight(font_instance *font, double font_size, - SPStyle const *style, FontMetrics *line_height, - double *line_height_multiplier); + // Returns line_height_multiplier + static double _computeFontLineHeight( SPStyle const *style ); unsigned _buildSpansForPara(ParagraphInfo *para) const; @@ -1106,46 +1105,32 @@ void Layout::Calculator::_buildPangoItemizationForPara(ParagraphInfo *para) con } /** - * Gets the ascent and descent for a font given the 'font-size' propert and 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 + * 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. */ -// THIS FUNCTION SHOULD NOT BE NECESSARY -void Layout::Calculator::_computeFontLineHeight(font_instance *font, double font_size, - SPStyle const *style, FontMetrics *font_metrics, - double *line_height_multiplier) +double Layout::Calculator::_computeFontLineHeight( SPStyle const *style ) { - if (font == NULL) { - font_metrics->setZero(); - *line_height_multiplier = 1.0; - } - else { - font->FontMetrics(font_metrics->ascent, font_metrics->descent, font_metrics->xheight); - } - *font_metrics *= font_size; - // yet another borked SPStyle member that we're going to have to fix ourselves - // To do: check if it really is still borked. + // 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; - return; + return style->line_height.computed; case SP_CSS_UNIT_EX: - *line_height_multiplier = style->line_height.value * 0.5; + 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; - return; + return style->line_height.value; default: // absolute values - *line_height_multiplier = style->line_height.computed / font_metrics->emSize(); - return; + return style->line_height.computed / style->font_size.computed; } break; } @@ -1153,7 +1138,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; + return (LINE_HEIGHT_NORMAL); } bool compareGlyphWidth(const PangoGlyphInfo &a, const PangoGlyphInfo &b) @@ -1270,7 +1255,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. */ @@ -1366,7 +1351,9 @@ 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. @@ -1381,11 +1368,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())); } @@ -1451,9 +1440,9 @@ bool Layout::Calculator::_findChunksForLine(ParagraphInfo const ¶, InputStreamTextSource const *text_source = static_cast(_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); @@ -1814,7 +1803,7 @@ void Layout::_calculateCursorShapeForEmpty() InputStreamTextSource const *text_source = static_cast(_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; FontMetrics line_height; if (font) { @@ -1822,11 +1811,8 @@ void Layout::_calculateCursorShapeForEmpty() 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.xheight = 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; diff --git a/src/libnrtype/Layout-TNG-Input.cpp b/src/libnrtype/Layout-TNG-Input.cpp index d99433adf..b66bbb5cd 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 ) { diff --git a/src/libnrtype/Layout-TNG.h b/src/libnrtype/Layout-TNG.h index ccf4bbe43..c923cc0dc 100644 --- a/src/libnrtype/Layout-TNG.h +++ b/src/libnrtype/Layout-TNG.h @@ -635,7 +635,7 @@ public: descent_max = 0.2; } - inline void set( font_instance *font ); + 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;} @@ -718,7 +718,6 @@ 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; -- cgit v1.2.3 From 3e68cf3b33b153dd0fc7eda9a2850f448a42c76c Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Wed, 18 Nov 2015 16:05:09 +0100 Subject: Use maximum ascent and descent for glyphs to ensure that glyphs that extend outside the em-box are fully drawn. (bzr r14430.1.10) --- src/libnrtype/Layout-TNG-Output.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/Layout-TNG-Output.cpp b/src/libnrtype/Layout-TNG-Output.cpp index 9557bc84c..526319f35 100644 --- a/src/libnrtype/Layout-TNG-Output.cpp +++ b/src/libnrtype/Layout-TNG-Output.cpp @@ -199,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.getTypoAscent(), - _spans[span_index].line_height.getTypoDescent(), + _spans[span_index].line_height.getMaxAscent(), + _spans[span_index].line_height.getMaxDescent(), glyph_matrix.translation()[Geom::X] - phase0 ); } -- cgit v1.2.3 From c1e4090d4e1d4deed0ea4fde9929360b6b291ea2 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Wed, 18 Nov 2015 22:05:48 +0100 Subject: Implement rendering of 'dominant-baseline' property. (bzr r14430.1.11) --- src/libnrtype/FontInstance.cpp | 46 ++++++++++++++++++------------------ src/libnrtype/Layout-TNG-Compute.cpp | 35 ++++++++++++++------------- src/libnrtype/Layout-TNG-Input.cpp | 5 ++++ src/libnrtype/Layout-TNG.h | 9 +++++++ 4 files changed, 56 insertions(+), 39 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/FontInstance.cpp b/src/libnrtype/FontInstance.cpp index 4578efe8f..79477d431 100644 --- a/src/libnrtype/FontInstance.cpp +++ b/src/libnrtype/FontInstance.cpp @@ -194,7 +194,7 @@ font_instance::font_instance(void) : _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; + _baselines[ SP_CSS_BASELINE_CENTRAL ] = 0.5 - _descent; _baselines[ SP_CSS_BASELINE_MIDDLE ] = 0.5 * _xheight; _baselines[ SP_CSS_BASELINE_TEXT_BEFORE_EDGE ] = -_descent; _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = _ascent; @@ -710,7 +710,7 @@ void font_instance::FindFontMetrics() { _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; // Definition + _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 ] = -_descent; // Definition _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = _ascent; // Definition @@ -783,7 +783,7 @@ void font_instance::FindFontMetrics() { _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; // Definition + _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 ] = -_descent; // Definition _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = _ascent; // Definition @@ -803,9 +803,9 @@ void font_instance::FindFontMetrics() { 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; + // 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 'म'. @@ -818,26 +818,26 @@ void font_instance::FindFontMetrics() { 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; + // 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; + // 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; } } diff --git a/src/libnrtype/Layout-TNG-Compute.cpp b/src/libnrtype/Layout-TNG-Compute.cpp index 05cc638f7..2ae13efa6 100644 --- a/src/libnrtype/Layout-TNG-Compute.cpp +++ b/src/libnrtype/Layout-TNG-Compute.cpp @@ -692,10 +692,18 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ // 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) { // Vertical text + // Default dominant baseline is determined by overall block (i.e. ) '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... @@ -703,15 +711,10 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ (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. + // Sideways orientation (Latin characters, CJK punctuation), 90deg rotation done at output stage. zzzzzzz new_glyph.orientation = ORIENTATION_SIDEWAYS; - // Baseline is determined by overall block (i.e. ) 'text-orientation' value. - if( _flow._blockTextOrientation() != SP_CSS_TEXT_ORIENTATION_SIDEWAYS ) { - // Baseline is center (shift: alphabetic to center) - new_glyph.y += 0.5 * (new_span.line_height.descent - new_span.line_height.ascent); - } - + 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 { @@ -719,14 +722,10 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ new_glyph.x += new_span.line_height.ascent; - // Baseline is determined by overall block (i.e. ) 'text-orientation' value. - if( _flow._blockTextOrientation() == SP_CSS_TEXT_ORIENTATION_SIDEWAYS ) { - // Baseline is alphabetic .. sideways (shift: left edge to alphabetic) - new_glyph.y -= new_span.line_height.descent; - } else { - // Baseline is center (shift: left edge to center) - new_glyph.y -= unbroken_span_glyph_info->geometry.width * 0.5 * font_size_multiplier; - } + // 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 ) { @@ -736,8 +735,12 @@ static void dumpUnbrokenSpans(ParagraphInfo *para){ } } else { // Horizontal text - new_glyph.y -= delta_y; + 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_info->glyph, false); diff --git a/src/libnrtype/Layout-TNG-Input.cpp b/src/libnrtype/Layout-TNG-Input.cpp index b66bbb5cd..6f5d4e5f8 100644 --- a/src/libnrtype/Layout-TNG-Input.cpp +++ b/src/libnrtype/Layout-TNG-Input.cpp @@ -151,6 +151,11 @@ SPCSSTextOrientation Layout::InputStreamTextSource::styleGetTextOrientation() co 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.h b/src/libnrtype/Layout-TNG.h index c923cc0dc..97a05bde8 100644 --- a/src/libnrtype/Layout-TNG.h +++ b/src/libnrtype/Layout-TNG.h @@ -723,6 +723,7 @@ private: font_instance *styleGetFontInstance() const; Direction styleGetBlockProgression() const; SPCSSTextOrientation styleGetTextOrientation() const; + SPCSSBaseline styleGetDominantBaseline() const; Alignment styleGetAlignment(Direction para_direction, bool try_text_align) const; }; @@ -769,6 +770,14 @@ private: 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(_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); -- cgit v1.2.3 From 9966fe3579bc3d8f0bfc0d453f549119de1d6884 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Thu, 19 Nov 2015 11:33:34 +0100 Subject: Swap text-before-edge and text-after-edge baseline values. (bzr r14430.1.12) --- src/libnrtype/FontInstance.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/libnrtype') diff --git a/src/libnrtype/FontInstance.cpp b/src/libnrtype/FontInstance.cpp index 79477d431..f0b87efa7 100644 --- a/src/libnrtype/FontInstance.cpp +++ b/src/libnrtype/FontInstance.cpp @@ -196,8 +196,8 @@ font_instance::font_instance(void) : _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 ] = -_descent; - _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = _ascent; + _baselines[ SP_CSS_BASELINE_TEXT_BEFORE_EDGE ] = _ascent; + _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = -_descent; } font_instance::~font_instance(void) @@ -712,8 +712,8 @@ void font_instance::FindFontMetrics() { _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 ] = -_descent; // Definition - _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = _ascent; // 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}}; @@ -785,8 +785,8 @@ void font_instance::FindFontMetrics() { _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 ] = -_descent; // Definition - _baselines[ SP_CSS_BASELINE_TEXT_AFTER_EDGE ] = _ascent; // 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 -- cgit v1.2.3