code.witches.io/go/sdl2@v0.1.1/ttf/attributes.go (about) 1 package ttf 2 3 //#include <SDL2/SDL_ttf.h> 4 import "C" 5 import "unsafe" 6 7 type FontStyle int 8 9 const ( 10 StyleNormal FontStyle = C.TTF_STYLE_NORMAL 11 StyleBold FontStyle = C.TTF_STYLE_BOLD 12 StyleItalic FontStyle = C.TTF_STYLE_ITALIC 13 StyleUnderline FontStyle = C.TTF_STYLE_UNDERLINE 14 StyleStrikethrough FontStyle = C.TTF_STYLE_STRIKETHROUGH 15 ) 16 17 type FontHinting int 18 19 const ( 20 HintingNormal FontHinting = C.TTF_HINTING_NORMAL 21 HintingLight FontHinting = C.TTF_HINTING_LIGHT 22 HintingMono FontHinting = C.TTF_HINTING_MONO 23 HintingNone FontHinting = C.TTF_HINTING_NONE 24 ) 25 26 func (f *Font) Style() FontStyle { 27 return (FontStyle)(C.TTF_GetFontStyle((*C.struct__TTF_Font)(f))) 28 } 29 30 func (f *Font) WithStyle(style FontStyle) { 31 C.TTF_SetFontStyle((*C.struct__TTF_Font)(f), C.int(style)) 32 } 33 34 func (f *Font) Outline() int { 35 return int(C.TTF_GetFontOutline((*C.struct__TTF_Font)(f))) 36 } 37 38 func (f *Font) WithOutline(outline int) { 39 C.TTF_SetFontOutline((*C.struct__TTF_Font)(f), C.int(outline)) 40 } 41 42 func (f *Font) Hinting() FontHinting { 43 return (FontHinting)(C.TTF_GetFontHinting((*C.struct__TTF_Font)(f))) 44 } 45 46 func (f *Font) WithHinting(hinting FontHinting) { 47 C.TTF_SetFontHinting((*C.struct__TTF_Font)(f), C.int(hinting)) 48 } 49 50 func (f *Font) Kerning() int { 51 return int(C.TTF_GetFontKerning((*C.struct__TTF_Font)(f))) 52 } 53 54 func (f *Font) WithKerning(kerning int) { 55 C.TTF_SetFontKerning((*C.struct__TTF_Font)(f), C.int(kerning)) 56 } 57 58 func (f *Font) Height() int { 59 return int(C.TTF_FontHeight((*C.struct__TTF_Font)(f))) 60 } 61 62 func (f *Font) Ascent() int { 63 return int(C.TTF_FontAscent((*C.struct__TTF_Font)(f))) 64 } 65 66 func (f *Font) Descent() int { 67 return int(C.TTF_FontDescent((*C.struct__TTF_Font)(f))) 68 } 69 70 func (f *Font) LineSkip() int { 71 return int(C.TTF_FontLineSkip((*C.struct__TTF_Font)(f))) 72 } 73 74 func (f *Font) Faces() int { 75 return int(C.TTF_FontFaces((*C.struct__TTF_Font)(f))) 76 } 77 78 func (f *Font) FaceIsFixedWidth() bool { 79 return C.TTF_FontFaceIsFixedWidth((*C.struct__TTF_Font)(f)) != 0 80 } 81 82 func (f *Font) FaceFamilyName() string { 83 nativeName := C.TTF_FontFaceFamilyName((*C.struct__TTF_Font)(f)) 84 if nativeName == nil { 85 return "" 86 } 87 return C.GoString(nativeName) 88 } 89 90 func (f *Font) FaceStyleName() string { 91 nativeName := C.TTF_FontFaceStyleName((*C.struct__TTF_Font)(f)) 92 if nativeName == nil { 93 return "" 94 } 95 return C.GoString(nativeName) 96 } 97 98 func (f *Font) GlyphIsProvided(glyph rune) int { 99 return int(C.TTF_GlyphIsProvided((*C.struct__TTF_Font)(f), C.Uint16(glyph))) 100 } 101 102 func (f *Font) GlyphMetrics(glyph rune) (minX, maxX, minY, maxY, advance int, err error) { 103 var nativeMinX, nativeMaxX, nativeMinY, nativeMaxY, nativeAdvance C.int 104 105 if C.TTF_GlyphMetrics( 106 (*C.struct__TTF_Font)(f), 107 C.Uint16(glyph), 108 &nativeMinX, 109 &nativeMaxX, 110 &nativeMinY, 111 &nativeMaxY, 112 &nativeAdvance, 113 ) != 0 { 114 return 0, 0, 0, 0, 0, GetError() 115 } 116 117 return int(nativeMinX), int(nativeMaxX), int(nativeMinY), int(nativeMaxY), int(nativeAdvance), nil 118 } 119 120 func (f *Font) SizeText(text string) (w, h int, err error) { 121 var nativeW, nativeH C.int 122 nativeText := C.CString(text) 123 defer C.free(unsafe.Pointer(nativeText)) 124 125 if C.TTF_SizeText((*C.struct__TTF_Font)(f), nativeText, &nativeW, &nativeH) != 0 { 126 return 0, 0, GetError() 127 } 128 129 return int(nativeW), int(nativeH), nil 130 } 131 132 func (f *Font) SizeUTF8(text string) (w, h int, err error) { 133 var nativeW, nativeH C.int 134 nativeText := C.CString(text) 135 defer C.free(unsafe.Pointer(nativeText)) 136 137 if C.TTF_SizeUTF8((*C.struct__TTF_Font)(f), nativeText, &nativeW, &nativeH) != 0 { 138 return 0, 0, GetError() 139 } 140 141 return int(nativeW), int(nativeH), nil 142 } 143 144 func (f *Font) SizeUnicode(text string) (w, h int, err error) { 145 var nativeW, nativeH C.int 146 var nativeText []C.Uint16 147 148 for _, ch := range text { 149 nativeText = append(nativeText, C.Uint16(ch)) 150 } 151 152 nativeText = append(nativeText, 0) 153 154 if C.TTF_SizeUNICODE((*C.struct__TTF_Font)(f), (*C.Uint16)(unsafe.Pointer(&nativeText[0])), &nativeW, &nativeH) != 0 { 155 return 0, 0, GetError() 156 } 157 158 return int(nativeW), int(nativeH), nil 159 }