1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/platform/fonts/shaping/shape_result_test_info.h"
6 
7 #include "third_party/blink/renderer/platform/fonts/font.h"
8 #include "third_party/blink/renderer/platform/fonts/shaping/shape_result_inline_headers.h"
9 
10 namespace blink {
11 
NumberOfRunsForTesting() const12 unsigned ShapeResultTestInfo::NumberOfRunsForTesting() const {
13   return runs_.size();
14 }
15 
RunInfoForTesting(unsigned run_index) const16 ShapeResult::RunInfo& ShapeResultTestInfo::RunInfoForTesting(
17     unsigned run_index) const {
18   return *runs_[run_index];
19 }
20 
RunInfoForTesting(unsigned run_index,unsigned & start_index,unsigned & num_characters,unsigned & num_glyphs,hb_script_t & script) const21 bool ShapeResultTestInfo::RunInfoForTesting(unsigned run_index,
22                                             unsigned& start_index,
23                                             unsigned& num_characters,
24                                             unsigned& num_glyphs,
25                                             hb_script_t& script) const {
26   if (run_index < runs_.size() && runs_[run_index]) {
27     start_index = runs_[run_index]->start_index_;
28     num_characters = runs_[run_index]->num_characters_;
29     num_glyphs = runs_[run_index]->glyph_data_.size();
30     script = runs_[run_index]->script_;
31     return true;
32   }
33   return false;
34 }
35 
RunInfoForTesting(unsigned run_index,unsigned & start_index,unsigned & num_glyphs,hb_script_t & script) const36 bool ShapeResultTestInfo::RunInfoForTesting(unsigned run_index,
37                                             unsigned& start_index,
38                                             unsigned& num_glyphs,
39                                             hb_script_t& script) const {
40   unsigned num_characters;
41   return RunInfoForTesting(run_index, start_index, num_characters, num_glyphs,
42                            script);
43 }
44 
GlyphForTesting(unsigned run_index,unsigned glyph_index) const45 uint16_t ShapeResultTestInfo::GlyphForTesting(unsigned run_index,
46                                               unsigned glyph_index) const {
47   return runs_[run_index]->glyph_data_[glyph_index].glyph;
48 }
49 
AdvanceForTesting(unsigned run_index,unsigned glyph_index) const50 float ShapeResultTestInfo::AdvanceForTesting(unsigned run_index,
51                                              unsigned glyph_index) const {
52   return runs_[run_index]->glyph_data_[glyph_index].advance;
53 }
54 
FontDataForTesting(unsigned run_index) const55 SimpleFontData* ShapeResultTestInfo::FontDataForTesting(
56     unsigned run_index) const {
57   return runs_[run_index]->font_data_.get();
58 }
59 
CharacterIndexesForTesting() const60 Vector<unsigned> ShapeResultTestInfo::CharacterIndexesForTesting() const {
61   Vector<unsigned> character_indexes;
62   for (const auto& run : runs_) {
63     for (const auto& glyph_data : run->glyph_data_) {
64       character_indexes.push_back(run->start_index_ +
65                                   glyph_data.character_index);
66     }
67   }
68   return character_indexes;
69 }
70 
AddGlyphInfo(void * context,unsigned character_index,Glyph glyph,FloatSize glyph_offset,float advance,bool is_horizontal,CanvasRotationInVertical rotation,const SimpleFontData * font_data)71 void AddGlyphInfo(void* context,
72                   unsigned character_index,
73                   Glyph glyph,
74                   FloatSize glyph_offset,
75                   float advance,
76                   bool is_horizontal,
77                   CanvasRotationInVertical rotation,
78                   const SimpleFontData* font_data) {
79   auto* list = static_cast<Vector<ShapeResultTestGlyphInfo>*>(context);
80   ShapeResultTestGlyphInfo glyph_info = {character_index, glyph, advance};
81   list->push_back(glyph_info);
82 }
83 
ComputeGlyphResults(const ShapeResult & result,Vector<ShapeResultTestGlyphInfo> * glyphs)84 void ComputeGlyphResults(const ShapeResult& result,
85                          Vector<ShapeResultTestGlyphInfo>* glyphs) {
86   result.ForEachGlyph(0, AddGlyphInfo, static_cast<void*>(glyphs));
87 }
88 
CompareResultGlyphs(const Vector<ShapeResultTestGlyphInfo> & test,const Vector<ShapeResultTestGlyphInfo> & reference,unsigned reference_start,unsigned num_glyphs)89 bool CompareResultGlyphs(const Vector<ShapeResultTestGlyphInfo>& test,
90                          const Vector<ShapeResultTestGlyphInfo>& reference,
91                          unsigned reference_start,
92                          unsigned num_glyphs) {
93   float advance_offset = reference[reference_start].advance;
94   bool glyphs_match = true;
95   for (unsigned i = 0; i < test.size(); i++) {
96     const auto& test_glyph = test[i];
97     const auto& reference_glyph = reference[i + reference_start];
98     if (test_glyph.character_index != reference_glyph.character_index ||
99         test_glyph.glyph != reference_glyph.glyph ||
100         test_glyph.advance != reference_glyph.advance - advance_offset) {
101       glyphs_match = false;
102       break;
103     }
104   }
105   if (!glyphs_match) {
106     fprintf(stderr, "╔══ Actual ═══════╤═══════╤═════════╗    ");
107     fprintf(stderr, "╔══ Expected ═════╤═══════╤═════════╗\n");
108     fprintf(stderr, "║ Character Index │ Glyph │ Advance ║    ");
109     fprintf(stderr, "║ Character Index │ Glyph │ Advance ║\n");
110     fprintf(stderr, "╟─────────────────┼───────┼─────────╢    ");
111     fprintf(stderr, "╟─────────────────┼───────┼─────────╢\n");
112     for (unsigned i = 0; i < test.size(); i++) {
113       const auto& test_glyph = test[i];
114       const auto& reference_glyph = reference[i + reference_start];
115 
116       if (test_glyph.character_index == reference_glyph.character_index)
117         fprintf(stderr, "║      %10u │", test_glyph.character_index);
118       else
119         fprintf(stderr, "║▶     %10u◀│", test_glyph.character_index);
120 
121       if (test_glyph.glyph == reference_glyph.glyph)
122         fprintf(stderr, "  %04X │", test_glyph.glyph);
123       else
124         fprintf(stderr, "▶ %04X◀│", test_glyph.glyph);
125 
126       if (test_glyph.advance == reference_glyph.advance)
127         fprintf(stderr, " %7.2f ║    ", test_glyph.advance);
128       else
129         fprintf(stderr, "▶%7.2f◀║    ", test_glyph.advance);
130 
131       fprintf(stderr, "║      %10u │  %04X │ %7.2f ║\n",
132               reference_glyph.character_index, reference_glyph.glyph,
133               reference_glyph.advance - advance_offset);
134     }
135     fprintf(stderr, "╚═════════════════╧═══════╧═════════╝    ");
136     fprintf(stderr, "╚═════════════════╧═══════╧═════════╝\n");
137   }
138   return glyphs_match;
139 }
140 
141 }  // namespace blink
142