huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
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
116
117
118
119
120
121
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
 
#include "SkGlyph.h"
 
#include "SkArenaAlloc.h"
#include "SkMakeUnique.h"
#include "SkScalerContext.h"
 
void SkGlyph::toMask(SkMask* mask) const {
    SkASSERT(mask);
 
    mask->fImage = (uint8_t*)fImage;
    mask->fBounds.set(fLeft, fTop, fLeft + fWidth, fTop + fHeight);
    mask->fRowBytes = this->rowBytes();
    mask->fFormat = static_cast<SkMask::Format>(fMaskFormat);
}
 
void SkGlyph::zeroMetrics() {
    fAdvanceX = 0;
    fAdvanceY = 0;
    fWidth    = 0;
    fHeight   = 0;
    fTop      = 0;
    fLeft     = 0;
}
 
static size_t bits_to_bytes(size_t bits) {
    return (bits + 7) >> 3;
}
 
static size_t format_alignment(SkMask::Format format) {
    switch (format) {
        case SkMask::kBW_Format:
        case SkMask::kA8_Format:
        case SkMask::k3D_Format:
        case SkMask::kSDF_Format:
            return alignof(uint8_t);
        case SkMask::kARGB32_Format:
            return alignof(uint32_t);
        case SkMask::kLCD16_Format:
            return alignof(uint16_t);
        default:
            SK_ABORT("Unknown mask format.");
            break;
    }
    return 0;
}
 
static size_t format_rowbytes(int width, SkMask::Format format) {
    return format == SkMask::kBW_Format ? bits_to_bytes(width)
                                        : width * format_alignment(format);
}
 
size_t SkGlyph::formatAlignment() const {
    auto format = static_cast<SkMask::Format>(fMaskFormat);
    return format_alignment(format);
}
 
size_t SkGlyph::allocImage(SkArenaAlloc* alloc) {
    auto size = this->computeImageSize();
    auto format = static_cast<SkMask::Format>(fMaskFormat);
    fImage = alloc->makeBytesAlignedTo(size, format_alignment(format));
 
    return size;
}
 
size_t SkGlyph::rowBytes() const {
    return format_rowbytes(fWidth, (SkMask::Format)fMaskFormat);
}
 
size_t SkGlyph::rowBytesUsingFormat(SkMask::Format format) const {
    return format_rowbytes(fWidth, format);
}
 
size_t SkGlyph::computeImageSize() const {
    size_t size = this->rowBytes() * fHeight;
 
    if (fMaskFormat == SkMask::k3D_Format) {
        size *= 3;
    }
 
    return size;
}
 
size_t SkGlyph::copyImageData(const SkGlyph& from, SkArenaAlloc* alloc) {
    fMaskFormat = from.fMaskFormat;
    fWidth = from.fWidth;
    fHeight = from.fHeight;
    fLeft = from.fLeft;
    fTop = from.fTop;
    fForceBW = from.fForceBW;
 
    if (from.fImage != nullptr) {
        auto imageSize = this->allocImage(alloc);
        SkASSERT(imageSize == from.computeImageSize());
 
        memcpy(fImage, from.fImage, imageSize);
        return imageSize;
    }
 
    return 0u;
}
 
SkPath* SkGlyph::addPath(SkScalerContext* scalerContext, SkArenaAlloc* alloc) {
    if (!this->isEmpty()) {
        if (fPathData == nullptr) {
            fPathData = alloc->make<SkGlyph::PathData>();
            if (scalerContext->getPath(this->getPackedID(), &fPathData->fPath)) {
                fPathData->fPath.updateBoundsCache();
                fPathData->fPath.getGenerationID();
                fPathData->fHasPath = true;
            }
        }
    }
    return this->path();
}