summaryrefslogtreecommitdiff
path: root/src/mbgl/text/harfbuzz_shaper.cpp
blob: d234548deaa35769193a1cc252da4386d9de0ff4 (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
116
117
118
119
120
121
122
123
124
125
126
127
#include <mbgl/text/harfbuzz_shaper.hpp>

#include <harfbuzz/hb.h>
#include <harfbuzz/hb-ft.h>

#include <mutex>
#include <iostream>

// freetype2
extern "C"
{
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include FT_OUTLINE_H
}

namespace mbgl {
namespace harfbuzz {
    
RequiredGlyphs getGlyphIDs(hb_font_t* font, const std::string& u8label) {
    hb_buffer_t *hb_buffer;
    hb_buffer = hb_buffer_create();
    hb_buffer_add_utf8(hb_buffer, u8label.c_str(), -1, 0, -1);
    hb_buffer_guess_segment_properties(hb_buffer);
    
    hb_shape(font, hb_buffer, NULL, 0);
    
    /* Get glyph information and positions out of the buffer. */
    unsigned int len = hb_buffer_get_length(hb_buffer);
    hb_glyph_info_t *glyphs = hb_buffer_get_glyph_infos(hb_buffer, NULL);
    
    RequiredGlyphs requiredGlyphs;
    for (uint32_t i = 0; i < len; i++)
    {
        // "codepoint" means "glyph id" after shaping
        if (!glyphs[i].codepoint) {
            hb_buffer_destroy(hb_buffer);
            return RequiredGlyphs(); // There's an invalid glyph in here, fall back to another font
        }
        requiredGlyphs.insert(glyphs[i].codepoint);
    }
    hb_buffer_destroy(hb_buffer);
    return requiredGlyphs;
}
    
RequiredGlyphsForFont getGlyphIDs(const LocalFonts& localFonts, const std::string& u8label) {
    for (auto localFont : localFonts) {
        RequiredGlyphs glyphs = getGlyphIDs(localFont.hb_font, u8label);
        if (!glyphs.empty())
            return RequiredGlyphsForFont(localFont.id,glyphs);
    }
    return RequiredGlyphsForFont(localFonts.back().id, RequiredGlyphs()); // All tofu for this label
}

// TODO: This can share a shaping call with getGlyphIDs, although the timing is awkward because the non-Harfbuzz pathway can't do shaping until later when the glyphs have already been downloaded
    
std::vector<std::pair<char16_t,double>> getClusterWidths(hb_font_t* font, const std::u16string& u16text) {
    hb_buffer_t *hb_buffer;
    hb_buffer = hb_buffer_create ();
    
    hb_buffer_add_utf16(hb_buffer, reinterpret_cast<const uint16_t*>(u16text.c_str()), -1, 0, -1);
    hb_buffer_guess_segment_properties(hb_buffer);
    hb_buffer_set_direction(hb_buffer, HB_DIRECTION_LTR); // We set the direction LTR because by the time we get here we've already reversed RTL text with BiDi
    
    hb_shape(font, hb_buffer, NULL, 0);
    
    /* Get glyph information and positions out of the buffer. */
    unsigned int len = hb_buffer_get_length(hb_buffer);
    hb_glyph_info_t *info = hb_buffer_get_glyph_infos(hb_buffer, NULL);
    hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(hb_buffer, NULL);
    
    std::vector<std::pair<char16_t,double>> clusterWidths;
    
    // TODO: Re-write to collect all data in a single pass through info/pos...
    for (size_t i = 0; i < u16text.size(); i++) {
        double accumulatedWidth = 0;
        for (unsigned int j = 0; j < len; j++) {
            if (info[j].cluster == i) {
                accumulatedWidth += pos[j].x_advance / 64.;
            }
        }
        // NOTE: Some codepoints won't have any glyphs associated with them -- we just make those into zero-width clusters.
        clusterWidths.emplace_back(u16text[i],accumulatedWidth);
    }
    
    hb_buffer_destroy(hb_buffer);
    
    return clusterWidths;
}

void applyShaping(hb_font_t* font, const std::u16string& u16text, std::vector<PositionedGlyph>& positionedGlyphs, float& current_x, float& current_y) {

    hb_buffer_t *hb_buffer;
    hb_buffer = hb_buffer_create ();
    
    hb_buffer_add_utf16(hb_buffer, reinterpret_cast<const uint16_t*>(u16text.c_str()), -1, 0, -1);
    hb_buffer_guess_segment_properties(hb_buffer);
    hb_buffer_set_direction(hb_buffer, HB_DIRECTION_LTR); // We set the direction LTR because by the time we get here we've already reversed RTL text with BiDi
    
    hb_shape(font, hb_buffer, NULL, 0);
    
    /* Get glyph information and positions out of the buffer. */
    unsigned int len = hb_buffer_get_length(hb_buffer);
    hb_glyph_info_t *info = hb_buffer_get_glyph_infos(hb_buffer, NULL);
    hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(hb_buffer, NULL);
    
    for (unsigned int i = 0; i < len; i++)
    {
        hb_codepoint_t gid   = info[i].codepoint; // "codepoint" means "glyph id" after shaping
        //unsigned int cluster = info[i].cluster;
        double x_advance = pos[i].x_advance / 64.;
        double y_advance = pos[i].y_advance / 64.;
        double x_offset  = pos[i].x_offset / 64.;
        double y_offset  = pos[i].y_offset / 64.;
        
        
        positionedGlyphs.emplace_back(gid, current_x + x_offset, current_y + y_offset);
        current_x += x_advance;
        current_y += y_advance;
    }
    
    hb_buffer_destroy(hb_buffer);
}

}
}