Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkFontMetricsPrivTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
10#include "include/core/SkFont.h"
17#include "tests/Test.h"
19
20#include <optional>
21
22DEF_TEST(SkFontMetricsPriv_Basic, reporter) {
23 auto typeface = ToolUtils::CreateTestTypeface("monospace", SkFontStyle());
24 SkFont font{typeface};
26 auto context = spec.createScalerContext();
27 SkFontMetrics srcMetrics;
28
29 // Check that font metrics round-trip.
30 context->getFontMetrics(&srcMetrics);
31
32 SkBinaryWriteBuffer writeBuffer({});
33 SkFontMetricsPriv::Flatten(writeBuffer, srcMetrics);
34
35 auto data = writeBuffer.snapshotAsData();
36
37 SkReadBuffer readBuffer{data->data(), data->size()};
38
39 std::optional<SkFontMetrics> dstMetrics = SkFontMetricsPriv::MakeFromBuffer(readBuffer);
40
41 REPORTER_ASSERT(reporter, dstMetrics.has_value());
42 REPORTER_ASSERT(reporter, srcMetrics == dstMetrics.value());
43
44 // Check that a broken buffer is detected.
45 // Must be multiple of 4 for a valid buffer.
46 std::uint8_t brokenData[] = {1, 2, 3, 4, 5, 6, 7, 8};
47 SkReadBuffer brokenBuffer{brokenData, std::size(brokenData)};
48
49 dstMetrics = SkFontMetricsPriv::MakeFromBuffer(brokenBuffer);
50 REPORTER_ASSERT(reporter, !dstMetrics.has_value());
51}
52
53
reporter
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
static void Flatten(SkWriteBuffer &buffer, const SkFontMetrics &metrics)
static std::optional< SkFontMetrics > MakeFromBuffer(SkReadBuffer &buffer)
static SkStrikeSpec MakeWithNoDevice(const SkFont &font, const SkPaint *paint=nullptr)
std::unique_ptr< SkScalerContext > createScalerContext() const
sk_sp< SkTypeface > CreateTestTypeface(const char *name, SkFontStyle style)