luapower / tr0

Unicode text rendering engine in Lua
http://luapower.com/tr0
0 stars 1 forks source link

Bounding box API? #32

Open capr opened 6 years ago

capr commented 6 years ago

Not useful for now, but maybe in the future. Here's some old code for it:

function glyph_run:glyph_metrics(i)
    local glyph_index = self.info[i].codepoint
    return self.tr.rs:glyph_metrics(self.font, self.font_size, glyph_index)
end

function glyph_run:bounding_box()
    local bx, by, bw, bh = 0, 0, 0, 0
    for i = 0, glyph_count-1 do

        --glyph origin relative to the start of the run.
        local ax = i > 0 and glyph_pos[i-1].x_advance or 0
        local ay = 0
        local px = ax + glyph_pos[i].x_offset
        local py = ay - glyph_pos[i].y_offset

        --glyph run metrics, used for more precise word wrapping and alignment
        --(commented because not used in current layouting algorithm).
        local glyph_index = glyph_info[i].codepoint
        local m = self.rs:glyph_metrics(font, font_size, glyph_index)
        bx, by, bw, bh = bounding_box(bx, by, bw, bh,
            px / 64 + m.hlsb,
            py / 64 - m.htsb,
            m.w, m.h)

    end
    return bx, by, bw, bh
end