1 /* 2 * Copyright (c) 2012 Google Inc. All rights reserved. 3 * Copyright (C) 2013 BlackBerry Limited. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_GLYPH_BOUNDS_ACCUMULATOR_H_ 33 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_GLYPH_BOUNDS_ACCUMULATOR_H_ 34 35 #include "third_party/blink/renderer/platform/fonts/shaping/shape_result_inline_headers.h" 36 #include "third_party/blink/renderer/platform/geometry/float_rect.h" 37 38 namespace blink { 39 40 // Helper class to accumulate glyph bounding box. 41 // 42 // Glyph positions and bounding boxes from HarfBuzz and fonts are in physical 43 // coordinate, while ShapeResult::glyph_bounding_box_ is in logical coordinate. 44 // To minimize the number of conversions, this class accumulates the bounding 45 // boxes in physical coordinate, and convert the accumulated box to logical. 46 struct GlyphBoundsAccumulator { 47 // Construct an accumulator with the logical glyph origin. GlyphBoundsAccumulatorGlyphBoundsAccumulator48 explicit GlyphBoundsAccumulator(float origin) : origin(origin) {} 49 50 // The accumulated glyph bounding box in physical coordinate, until 51 // ConvertVerticalRunToLogical(). 52 FloatRect bounds; 53 // The current origin, in logical coordinate. 54 float origin; 55 56 // Unite a glyph bounding box to |bounds|. 57 template <bool is_horizontal_run> UniteGlyphBoundsAccumulator58 void Unite(FloatRect bounds_for_glyph, 59 ShapeResult::GlyphOffset glyph_offset) { 60 if (UNLIKELY(bounds_for_glyph.IsEmpty())) 61 return; 62 63 // Glyphs are drawn at |origin + offset|. Move glyph_bounds to that point. 64 // All positions in hb_glyph_position_t are relative to the current point. 65 // https://behdad.github.io/harfbuzz/harfbuzz-Buffers.html#hb-glyph-position-t-struct 66 if (is_horizontal_run) 67 bounds_for_glyph.SetX(bounds_for_glyph.X() + origin); 68 else 69 bounds_for_glyph.SetY(bounds_for_glyph.Y() + origin); 70 bounds_for_glyph.Move(glyph_offset); 71 72 bounds.Unite(bounds_for_glyph); 73 } 74 75 // Non-template version of |Unite()|, see above. UniteGlyphBoundsAccumulator76 void Unite(bool is_horizontal_run, 77 FloatRect bounds_for_glyph, 78 ShapeResult::GlyphOffset glyph_offset) { 79 is_horizontal_run ? Unite<true>(bounds_for_glyph, glyph_offset) 80 : Unite<false>(bounds_for_glyph, glyph_offset); 81 } 82 83 // Convert vertical run glyph bounding box to logical. Horizontal runs do not 84 // need conversions because physical and logical are the same. ConvertVerticalRunToLogicalGlyphBoundsAccumulator85 void ConvertVerticalRunToLogical(const FontMetrics& font_metrics) { 86 // Convert physical glyph_bounding_box to logical. 87 bounds = bounds.TransposedRect(); 88 89 // The glyph bounding box of a vertical run uses ideographic baseline. 90 // Adjust the box Y position because the bounding box of a ShapeResult uses 91 // alphabetic baseline. 92 // See diagrams of base lines at 93 // https://drafts.csswg.org/css-writing-modes-3/#intro-baselines 94 int baseline_adjust = font_metrics.Ascent(kIdeographicBaseline) - 95 font_metrics.Ascent(kAlphabeticBaseline); 96 bounds.SetY(bounds.Y() + baseline_adjust); 97 } 98 }; 99 100 } // namespace blink 101 102 #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_GLYPH_BOUNDS_ACCUMULATOR_H_ 103