comparison samples/freetype.tp @ 328:c1fad3d93861

Add getKerning to freetype module and use it in sample
author Michael Pavone <pavone@retrodev.com>
date Wed, 25 Mar 2015 00:16:37 -0700
parents 50760ba52b11
children e70f9d3f19f8
comparison
equal deleted inserted replaced
327:860075fdc2d3 328:c1fad3d93861
26 slot <- face glyphSlot 26 slot <- face glyphSlot
27 27
28 glyphs <- #[] 28 glyphs <- #[]
29 //TODO: Use a bytearray once that has an append method 29 //TODO: Use a bytearray once that has an append method
30 pixels <- #[] 30 pixels <- #[]
31 foreach: (face charmap) :char glyphIndex { 31 foreach: (face charmap) :char glyphIdx {
32 face loadGlyph: glyphIndex flags: (render or linearDesign) 32 face loadGlyph: glyphIdx flags: (render or linearDesign)
33 pixelStart <- pixels length 33 pixelStart <- pixels length
34 _width <- slot bitmapWidth 34 _width <- slot bitmapWidth
35 _height <- slot bitmapRows 35 _height <- slot bitmapRows
36 pitch <- slot bitmapPitch 36 pitch <- slot bitmapPitch
37 buffer <- slot bitmapData 37 buffer <- slot bitmapData
56 hAdvance <- slot linearHoriAdvance 56 hAdvance <- slot linearHoriAdvance
57 vAdvance <- slot linearVertAdvance 57 vAdvance <- slot linearVertAdvance
58 leftOffset <- (slot bitmapLeft) 58 leftOffset <- (slot bitmapLeft)
59 topOffset <- (slot bitmapTop) 59 topOffset <- (slot bitmapTop)
60 charCode <- char 60 charCode <- char
61 glyphIndex <- glyphIdx
61 62
62 atlasX <- -1 63 atlasX <- -1
63 atlasY <- -1 64 atlasY <- -1
64 atlasRect <- { 65 atlasRect <- {
65 sdl rect: atlasX atlasY size: width height 66 sdl rect: atlasX atlasY size: width height
262 option value: #{ 263 option value: #{
263 texture <- drawTex 264 texture <- drawTex
264 width <- aWidth 265 width <- aWidth
265 height <- aHeight 266 height <- aHeight
266 glyphs <- glyphDict 267 glyphs <- glyphDict
267 drawString:at <- :str :x y { 268 drawString:at:useKerning? <- :str :xPos yPos :kern? {
268 //pixels to font units 269 //pixels to font units
269 designPosition <- (x f64) * _pixelFactor 270 designPosition <- (xPos f64) * _pixelFactor
270 charIdx <- 0 271 charIdx <- 0
272 last <- 0u32
273 useKerning? <- kern? && (face hasKerning?)
271 274
272 while: { charIdx < (str byte_length) } do: { 275 while: { charIdx < (str byte_length) } do: {
273 //TODO: UTF-8 276 //TODO: UTF-8
274 char <- (str byte: charIdx) uint32 277 char <- (str byte: charIdx) uint32
275 glyph <- glyphs get: char else: { 278 glyph <- glyphs get: char else: {
276 glyphs get: 0u32 else: { false } 279 glyphs get: 0u32 else: { false }
277 } 280 }
278 texture copyRect: (glyph atlasRect) To: (glyph destRect: x y) 281 texture copyRect: (glyph atlasRect) To: (glyph destRect: xPos yPos)
279 282
280 designPosition <- designPosition + ((glyph hAdvance) f64) 283 designPosition <- designPosition + ((glyph hAdvance) f64)
281 x <- (designPosition / _pixelFactor + 0.5) truncInt32 284 if: charIdx > 0 && useKerning? {
285 (face getKerning: last (glyph glyphIndex) mode: ((freetype kerning) unscaled)) value: :kern {
286 designPosition <- designPosition + ((kern x) f64)
287 } none: {}
288 }
289 xPos <- (designPosition / _pixelFactor + 0.5) truncInt32
282 290
291 last <- glyph glyphIndex
283 charIdx <- charIdx + 1 292 charIdx <- charIdx + 1
284 } 293 }
285 } 294 }
286 } 295 }
287 } none: { 296 } none: {
358 (makeAtlas: renderer face 12.0 dpi (sdl r: 0u8 g: 0u8 b: 0u8)) value: :atlas { 367 (makeAtlas: renderer face 12.0 dpi (sdl r: 0u8 g: 0u8 b: 0u8)) value: :atlas {
359 continue? <- true 368 continue? <- true
360 while: { continue? } do: { 369 while: { continue? } do: {
361 renderer clear 370 renderer clear
362 if: (str length) > 0 { 371 if: (str length) > 0 {
363 atlas drawString: str at: 0 windowHeight / 2 372 atlas drawString: str at: 0 windowHeight / 3 useKerning?: true
373 atlas drawString: str at: 0 windowHeight * 2 / 3 useKerning?: false
364 } else: { 374 } else: {
365 y <- 0 375 y <- 0
366 x <- 0 376 x <- 0
367 while: { y < (atlas height) } do: { 377 while: { y < (atlas height) } do: {
368 copyWidth <- if: (atlas width) < windowWidth { atlas width } else: { windowWidth } 378 copyWidth <- if: (atlas width) < windowWidth { atlas width } else: { windowWidth }