This commit is contained in:
Skytrias
2022-12-24 11:19:12 +01:00
parent 4ee413aa32
commit 2a94b66f4d
3 changed files with 133 additions and 158 deletions
+12 -16
View File
@@ -816,16 +816,15 @@ __getVerticalAlign :: proc(
pixelSize: i16,
) -> (res: f32) {
switch ctx.location {
case .TOPLEFT: {
case .TOPLEFT:
switch av {
case .TOP: res = font.ascender * f32(pixelSize) / 10
case .MIDDLE: res = (font.ascender + font.descender) / 2 * f32(pixelSize) / 10
case .BASELINE: res = 0
case .BOTTOM: res = font.descender * f32(pixelSize) / 10
}
}
case .BOTTOMLEFT: {
case .BOTTOMLEFT:
switch av {
case .TOP: res = -font.ascender * f32(pixelSize) / 10
case .MIDDLE: res = -(font.ascender + font.descender) / 2 * f32(pixelSize) / 10
@@ -833,7 +832,6 @@ __getVerticalAlign :: proc(
case .BOTTOM: res = -font.descender * f32(pixelSize) / 10
}
}
}
return
}
@@ -1005,7 +1003,7 @@ __getQuad :: proc(
y1 = f32(glyph.y1 - 1)
switch ctx.location {
case .TOPLEFT: {
case .TOPLEFT:
rx = math.floor(x^ + xoff)
ry = math.floor(y^ + yoff)
@@ -1018,9 +1016,8 @@ __getQuad :: proc(
quad.t0 = y0 * ctx.ith
quad.s1 = x1 * ctx.itw
quad.t1 = y1 * ctx.ith
}
case .BOTTOMLEFT: {
case .BOTTOMLEFT:
rx = math.floor(x^ + xoff)
ry = math.floor(y^ - yoff)
@@ -1034,7 +1031,6 @@ __getQuad :: proc(
quad.s1 = x1 * ctx.itw
quad.t1 = y1 * ctx.ith
}
}
x^ += f32(int(f32(glyph.xadvance) / 10 + 0.5))
}
@@ -1057,15 +1053,15 @@ TextIterInit :: proc(
y := y
switch state.ah {
case .LEFT: {}
case .CENTER: {
case .CENTER:
width := TextBounds(ctx, text, x, y, nil)
x = math.round(x - width * 0.5)
}
case .RIGHT: {
case .RIGHT:
width := TextBounds(ctx, text, x, y, nil)
x -= width
}
}
// align vertically
y = math.round(y + __getVerticalAlign(ctx, res.font, state.av, res.isize))
@@ -1185,15 +1181,15 @@ TextBounds :: proc(
advance := x - start_x
switch state.ah {
case .LEFT: {}
case .CENTER: {
case .CENTER:
minx -= advance * 0.5
maxx -= advance * 0.5
}
case .RIGHT: {
case .RIGHT:
minx -= advance
maxx -= advance
}
}
if bounds != nil {
bounds^ = { minx, miny, maxx, maxy }
+23 -44
View File
@@ -1302,30 +1302,21 @@ __appendCommands :: proc(ctx: ^Context, values: []f32) {
cmd := Commands(values[i])
switch cmd {
case .MOVE_TO, .LINE_TO: {
case .MOVE_TO, .LINE_TO:
TransformPoint(&values[i + 1], &values[i + 2], state.xform, values[i + 1], values[i + 2])
i += 3
}
case .BEZIER_TO: {
case .BEZIER_TO:
TransformPoint(&values[i + 1], &values[i + 2], state.xform, values[i + 1], values[i + 2])
TransformPoint(&values[i + 3], &values[i + 4], state.xform, values[i + 3], values[i + 4])
TransformPoint(&values[i + 5], &values[i + 6], state.xform, values[i + 5], values[i + 6])
i += 7
}
case .CLOSE: {
i += 1
}
case .WINDING: {
i += 2
}
case .CLOSE: i += 1
case .WINDING: i += 2
// default
case: {
i += 1
}
case: i += 1
}
}
@@ -1510,20 +1501,18 @@ __flattenPaths :: proc(ctx: ^Context) {
cmd := Commands(ctx.commands[i])
switch cmd {
case .MOVE_TO: {
case .MOVE_TO:
__addPath(ctx)
p := ctx.commands[i + 1:]
__addPoint(ctx, p[0], p[1], { .CORNER })
i += 3
}
case .LINE_TO: {
case .LINE_TO:
p := ctx.commands[i + 1:]
__addPoint(ctx, p[0], p[1], { .CORNER })
i += 3
}
case .BEZIER_TO: {
case .BEZIER_TO:
last := __lastPoint(ctx)
if last != nil {
@@ -1534,21 +1523,16 @@ __flattenPaths :: proc(ctx: ^Context) {
}
i += 7
}
case .CLOSE: {
case .CLOSE:
__closePath(ctx)
i += 1
}
case .WINDING: {
case .WINDING:
__pathWinding(ctx, Winding(ctx.commands[i + 1]))
i += 2
}
case: {
i += 1
}
case: i += 1
}
}
@@ -2520,7 +2504,7 @@ Ellipse :: proc(ctx: ^Context, cx, cy, rx, ry: f32) {
__cmdf(.BEZIER_TO), cx+rx*KAPPA, cy+ry, cx+rx, cy+ry*KAPPA, cx+rx, cy,
__cmdf(.BEZIER_TO), cx+rx, cy-ry*KAPPA, cx+rx*KAPPA, cy-ry, cx, cy-ry,
__cmdf(.BEZIER_TO), cx-rx*KAPPA, cy-ry, cx-rx, cy-ry*KAPPA, cx-rx, cy,
__cmdf(.CLOSE)
__cmdf(.CLOSE),
}
__appendCommands(ctx, values[:])
}
@@ -2674,8 +2658,8 @@ CreateFont :: proc(ctx: ^Context, name, filename: string) -> int {
// Creates font by loading it from the specified memory chunk.
// Returns handle to the font.
CreateFontMem :: proc(ctx: ^Context, name: string, slice: []byte) -> int {
return fontstash.AddFontMem(&ctx.fs, name, slice)
CreateFontMem :: proc(ctx: ^Context, name: string, slice: []byte, free_loaded_data: bool) -> int {
return fontstash.AddFontMem(&ctx.fs, name, slice, free_loaded_data)
}
// Finds a loaded font of specified name, and returns handle to it, or -1 if the font is not found.
@@ -2891,15 +2875,15 @@ TextIcon :: proc(ctx: ^Context, x, y: f32, codepoint: rune) -> f32 {
y := y * scale
switch fstate.ah {
case .LEFT: {}
case .CENTER: {
case .CENTER:
width := fontstash.CodepointWidth(font, codepoint, fscale)
x = math.round(x - width * 0.5)
}
case .RIGHT: {
case .RIGHT:
width := fontstash.CodepointWidth(font, codepoint, fscale)
x -= width
}
}
// align vertically
y = math.round(y + fontstash.__getVerticalAlign(fs, font, fstate.av, isize))
@@ -3190,25 +3174,21 @@ TextBreakLines :: proc(
prev_iter = iter
switch iter.codepoint {
case '\t', '\v', '\f', ' ', 0x00a0: {
case '\t', '\v', '\f', ' ', 0x00a0:
// NBSP
type = .Space
}
case '\n': {
case '\n':
type = pcodepoint == 13 ? .Space : .Newline
}
case '\r': {
case '\r':
type = pcodepoint == 10 ? .Space : .Newline
}
case 0x0085: {
case 0x0085:
// NEL
type = .Newline
}
case: {
case:
if (iter.codepoint >= 0x4E00 && iter.codepoint <= 0x9FFF) ||
(iter.codepoint >= 0x3000 && iter.codepoint <= 0x30FF) ||
(iter.codepoint >= 0xFF00 && iter.codepoint <= 0xFFEF) ||
@@ -3220,7 +3200,6 @@ TextBreakLines :: proc(
type = .Char
}
}
}
if type == .Newline {
// Always handle new lines.