File size: 3,159 Bytes
f61bac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, The OpenROAD Authors

#include "glyph_cache.h"
#include "gtest/gtest.h"

namespace web {
namespace {

TEST(GlyphCacheTest, TextWidthPositive)
{
  EXPECT_GT(glyphCache().textWidth("hello", 14), 0);
}

TEST(GlyphCacheTest, TextWidthEmpty)
{
  EXPECT_EQ(glyphCache().textWidth("", 14), 0);
}

TEST(GlyphCacheTest, TextHeightPositive)
{
  EXPECT_GT(glyphCache().textHeight(14), 0);
}

TEST(GlyphCacheTest, GlyphReturnsAlpha)
{
  // Every printable ASCII char should have a non-null bitmap
  // (except space which has no visible pixels).
  for (char ch = '!'; ch <= '~'; ++ch) {
    auto gi = glyphCache().glyph(14, ch);
    EXPECT_NE(gi.alpha, nullptr) << "char '" << ch << "'";
    EXPECT_GT(gi.bmp_width, 0) << "char '" << ch << "'";
    EXPECT_GT(gi.bmp_height, 0) << "char '" << ch << "'";
    EXPECT_GT(gi.advance, 0) << "char '" << ch << "'";
  }
}

TEST(GlyphCacheTest, SpaceHasNoPixels)
{
  auto gi = glyphCache().glyph(14, ' ');
  EXPECT_EQ(gi.alpha, nullptr);
  EXPECT_GT(gi.advance, 0);  // space still has an advance
}

TEST(GlyphCacheTest, GlyphOutOfRange)
{
  auto gi = glyphCache().glyph(14, '\n');
  EXPECT_EQ(gi.alpha, nullptr);

  auto gi2 = glyphCache().glyph(14, '\x7f');  // DEL
  EXPECT_EQ(gi2.alpha, nullptr);
}

TEST(GlyphCacheTest, AntiAliasedPixels)
{
  // A rendered glyph should contain alpha values between 1 and 254
  // (anti-aliased edges), not just binary 0/255.
  auto gi = glyphCache().glyph(20, 'O');
  ASSERT_NE(gi.alpha, nullptr);
  bool found_intermediate = false;
  for (int i = 0; i < gi.bmp_width * gi.bmp_height; ++i) {
    if (gi.alpha[i] > 0 && gi.alpha[i] < 255) {
      found_intermediate = true;
      break;
    }
  }
  EXPECT_TRUE(found_intermediate) << "No anti-aliased pixels found in 'O'";
}

TEST(GlyphCacheTest, ProportionalWidths)
{
  // DejaVu Sans is proportional — 'W' should be wider than 'i'.
  const int w_width = glyphCache().textWidth("W", 14);
  const int i_width = glyphCache().textWidth("i", 14);
  EXPECT_GT(w_width, i_width);
}

TEST(GlyphCacheTest, DifferentSizes)
{
  EXPECT_LT(glyphCache().cellHeight(10), glyphCache().cellHeight(20));
}

TEST(GlyphCacheTest, CasePreserved)
{
  // 'a' and 'A' should produce different bitmaps.
  auto lower = glyphCache().glyph(14, 'a');
  auto upper = glyphCache().glyph(14, 'A');
  ASSERT_NE(lower.alpha, nullptr);
  ASSERT_NE(upper.alpha, nullptr);
  // At minimum, the bitmap dimensions or content should differ.
  bool differ = (lower.bmp_width != upper.bmp_width)
                || (lower.bmp_height != upper.bmp_height);
  if (!differ) {
    // Same dimensions — compare content.
    for (int i = 0; i < lower.bmp_width * lower.bmp_height; ++i) {
      if (lower.alpha[i] != upper.alpha[i]) {
        differ = true;
        break;
      }
    }
  }
  EXPECT_TRUE(differ) << "'a' and 'A' glyphs should differ";
}

TEST(GlyphCacheTest, TextWidthIncludesAllChars)
{
  // Width of "ab" should be greater than width of "a".
  const int w1 = glyphCache().textWidth("a", 14);
  const int w2 = glyphCache().textWidth("ab", 14);
  EXPECT_GT(w2, w1);
}

}  // namespace
}  // namespace web