summaryrefslogtreecommitdiff
path: root/thirdparty/msdfgen/core/rasterization.cpp
blob: 9aa695a8c1dc2a26139ff26391e936ef1b1c30b6 (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
111
112
113
114
115

#include "rasterization.h"

#include <vector>
#include "arithmetics.hpp"

namespace msdfgen {

void rasterize(const BitmapRef<float, 1> &output, const Shape &shape, const Projection &projection, FillRule fillRule) {
    Scanline scanline;
    for (int y = 0; y < output.height; ++y) {
        int row = shape.inverseYAxis ? output.height-y-1 : y;
        shape.scanline(scanline, projection.unprojectY(y+.5));
        for (int x = 0; x < output.width; ++x)
            *output(x, row) = (float) scanline.filled(projection.unprojectX(x+.5), fillRule);
    }
}

void distanceSignCorrection(const BitmapRef<float, 1> &sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {
    Scanline scanline;
    for (int y = 0; y < sdf.height; ++y) {
        int row = shape.inverseYAxis ? sdf.height-y-1 : y;
        shape.scanline(scanline, projection.unprojectY(y+.5));
        for (int x = 0; x < sdf.width; ++x) {
            bool fill = scanline.filled(projection.unprojectX(x+.5), fillRule);
            float &sd = *sdf(x, row);
            if ((sd > .5f) != fill)
                sd = 1.f-sd;
        }
    }
}

template <int N>
static void multiDistanceSignCorrection(const BitmapRef<float, N> &sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {
    int w = sdf.width, h = sdf.height;
    if (!(w*h))
        return;
    Scanline scanline;
    bool ambiguous = false;
    std::vector<char> matchMap;
    matchMap.resize(w*h);
    char *match = &matchMap[0];
    for (int y = 0; y < h; ++y) {
        int row = shape.inverseYAxis ? h-y-1 : y;
        shape.scanline(scanline, projection.unprojectY(y+.5));
        for (int x = 0; x < w; ++x) {
            bool fill = scanline.filled(projection.unprojectX(x+.5), fillRule);
            float *msd = sdf(x, row);
            float sd = median(msd[0], msd[1], msd[2]);
            if (sd == .5f)
                ambiguous = true;
            else if ((sd > .5f) != fill) {
                msd[0] = 1.f-msd[0];
                msd[1] = 1.f-msd[1];
                msd[2] = 1.f-msd[2];
                *match = -1;
            } else
                *match = 1;
            if (N >= 4 && (msd[3] > .5f) != fill)
                msd[3] = 1.f-msd[3];
            ++match;
        }
    }
    // This step is necessary to avoid artifacts when whole shape is inverted
    if (ambiguous) {
        match = &matchMap[0];
        for (int y = 0; y < h; ++y) {
            int row = shape.inverseYAxis ? h-y-1 : y;
            for (int x = 0; x < w; ++x) {
                if (!*match) {
                    int neighborMatch = 0;
                    if (x > 0) neighborMatch += *(match-1);
                    if (x < w-1) neighborMatch += *(match+1);
                    if (y > 0) neighborMatch += *(match-w);
                    if (y < h-1) neighborMatch += *(match+w);
                    if (neighborMatch < 0) {
                        float *msd = sdf(x, row);
                        msd[0] = 1.f-msd[0];
                        msd[1] = 1.f-msd[1];
                        msd[2] = 1.f-msd[2];
                    }
                }
                ++match;
            }
        }
    }
}

void distanceSignCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {
    multiDistanceSignCorrection(sdf, shape, projection, fillRule);
}

void distanceSignCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const Projection &projection, FillRule fillRule) {
    multiDistanceSignCorrection(sdf, shape, projection, fillRule);
}

// Legacy API

void rasterize(const BitmapRef<float, 1> &output, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
    rasterize(output, shape, Projection(scale, translate), fillRule);
}

void distanceSignCorrection(const BitmapRef<float, 1> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
    distanceSignCorrection(sdf, shape, Projection(scale, translate), fillRule);
}

void distanceSignCorrection(const BitmapRef<float, 3> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
    distanceSignCorrection(sdf, shape, Projection(scale, translate), fillRule);
}

void distanceSignCorrection(const BitmapRef<float, 4> &sdf, const Shape &shape, const Vector2 &scale, const Vector2 &translate, FillRule fillRule) {
    distanceSignCorrection(sdf, shape, Projection(scale, translate), fillRule);
}

}