summaryrefslogtreecommitdiff
path: root/libs/minikin/Debug.cpp
blob: 42d6d8e1d69298743ad2af6d5006a5f32d4866e1 (plain)
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
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "minikin/Debug.h"

#include <unicode/utf16.h>
#include <unicode/utf8.h>

#include <sstream>

#include "minikin/FontFileParser.h"
#include "minikin/LayoutCore.h"
#include "minikin/LocaleList.h"
#include "minikin/MinikinExtent.h"
#include "minikin/MinikinPaint.h"
#include "minikin/MinikinRect.h"
#include "minikin/Point.h"
#include "minikin/Range.h"
#include "minikin/U16StringPiece.h"

namespace minikin {

namespace debug {

std::string toUtf8(const U16StringPiece& text) {
    std::string output;
    output.resize(text.size() * 4);
    uint32_t in_i = 0;
    uint32_t out_i = 0;
    bool isError = false;

    while (in_i < text.size()) {
        uint32_t ch;
        U16_NEXT(text, in_i, text.size(), ch);
        U8_APPEND(output, out_i, output.size(), ch, isError);
        if (isError) {
            return "";
        }
    }

    output.resize(out_i);
    return output;
}

std::string toString(const Point& point) {
    std::stringstream ss;
    ss << point;
    return ss.str();
}

std::string toString(const MinikinRect& rect) {
    std::stringstream ss;
    ss << rect;
    return ss.str();
}

std::string toString(const Range& range) {
    std::stringstream ss;
    ss << range;
    return ss.str();
}

std::string toString(const MinikinExtent& extent) {
    std::stringstream ss;
    ss << extent;
    return ss.str();
}

std::string toString(const LayoutPiece& layout) {
    std::stringstream ss;
    ss << "{advance=" << layout.advance() << ", extent=" << toString(layout.extent())
       << ", glyphs=";

    for (uint32_t i = 0; i < layout.glyphCount(); ++i) {
        ss << "{id:" << layout.glyphIdAt(i) << ", pos = " << toString(layout.pointAt(i))
           << ", font = \""
           << FontFileParser(layout.fontAt(i).hbFont()).getPostScriptName().value_or("[UNKNOWN]")
           << "\"}, ";
    }
    return ss.str();
}

std::string toString(const MinikinPaint& paint) {
    std::stringstream ss;
    ss << "{size=" << paint.size << ", scaleX=" << paint.scaleX << ", skewX=" << paint.skewX
       << ", letterSpacing=" << paint.letterSpacing << ", wordSpacing=" << paint.wordSpacing
       << ", fontFlags=" << paint.fontFlags
       << ", localeList=" << getLocaleString(paint.localeListId)
       << ", fontStyle=" << paint.fontStyle << ", familyVariant=" << paint.familyVariant
       << ", fontFeatureSettings=" << paint.fontFeatureSettings
       << ", fontCollectionId=" << paint.font->getId();
    return ss.str();
}

}  // namespace debug

}  // namespace minikin