|
| 1 | +/* text.go contains functions for text annotation |
| 2 | +TODO: |
| 3 | +getters and setters for |
| 4 | +- Stretch (redefine C type) |
| 5 | +- Style (redefine C type) |
| 6 | +- Resolution (two doubles) |
| 7 | +- Decoration (redefine C type) |
| 8 | +- Encoding (string) |
| 9 | +- DrawSetTextInterlineSpacing(DrawingWand *,const double), |
| 10 | +- DrawSetTextInterwordSpacing(DrawingWand *,const double), |
| 11 | +- DrawSetGravity(DrawingWand *wand,const GravityType gravity) |
| 12 | +*/ |
| 13 | + |
| 14 | +package canvas |
| 15 | + |
| 16 | +/* |
| 17 | +#cgo CFLAGS: -fopenmp -I./_include |
| 18 | +#cgo LDFLAGS: -lMagickWand -lMagickCore |
| 19 | +
|
| 20 | +#include <wand/magick_wand.h> |
| 21 | +*/ |
| 22 | +import "C" |
| 23 | + |
| 24 | +import ( |
| 25 | + "unsafe" |
| 26 | +) |
| 27 | + |
| 28 | +type Alignment uint |
| 29 | + |
| 30 | +const ( |
| 31 | + UndefinedAlign Alignment = Alignment(C.UndefinedAlign) |
| 32 | + LeftAlign = Alignment(C.LeftAlign) |
| 33 | + CenterAlign = Alignment(C.CenterAlign) |
| 34 | + RightAlign = Alignment(C.RightAlign) |
| 35 | +) |
| 36 | + |
| 37 | +// structure containing all text properties for an annotation |
| 38 | +// except the colors that are defined by FillColor and StrokeColor |
| 39 | +type TextProperties struct { |
| 40 | + Font string |
| 41 | + Family string |
| 42 | + Size float64 |
| 43 | + // Stretch C.StretchType |
| 44 | + Weight uint |
| 45 | + // Style C.StyleType |
| 46 | + // Resolution [2]C.double |
| 47 | + Alignment Alignment |
| 48 | + Antialias bool |
| 49 | + // Decoration C.DecorationType |
| 50 | + // Encoding string |
| 51 | + Kerning float64 |
| 52 | + // Interline float64 |
| 53 | + // Interword float64 |
| 54 | + UnderColor *C.PixelWand |
| 55 | +} |
| 56 | + |
| 57 | +// Returns a TextProperties structure. |
| 58 | +// Parameters: |
| 59 | +// readDefault: if false, returns an empty structure. |
| 60 | +// if true, returns a structure set with current canvas settings |
| 61 | +func (self *Canvas) NewTextProperties(readDefault bool) *TextProperties { |
| 62 | + if readDefault { |
| 63 | + cfont := C.DrawGetFont(self.drawing) |
| 64 | + defer C.free(unsafe.Pointer(cfont)) |
| 65 | + cfamily := C.DrawGetFontFamily(self.drawing) |
| 66 | + defer C.free(unsafe.Pointer(cfamily)) |
| 67 | + csize := C.DrawGetFontSize(self.drawing) |
| 68 | + cweight := C.DrawGetFontWeight(self.drawing) |
| 69 | + calignment := C.DrawGetTextAlignment(self.drawing) |
| 70 | + cantialias := C.DrawGetTextAntialias(self.drawing) |
| 71 | + ckerning := C.DrawGetTextKerning(self.drawing) |
| 72 | + antialias := cantialias == C.MagickTrue |
| 73 | + |
| 74 | + underColor := C.NewPixelWand() |
| 75 | + C.DrawGetTextUnderColor(self.drawing, underColor) |
| 76 | + return &TextProperties{ |
| 77 | + Font: C.GoString(cfont), |
| 78 | + Family: C.GoString(cfamily), |
| 79 | + Size: float64(csize), |
| 80 | + Weight: uint(cweight), |
| 81 | + Alignment: Alignment(calignment), |
| 82 | + Antialias: antialias, |
| 83 | + Kerning: float64(ckerning), |
| 84 | + UnderColor: underColor, |
| 85 | + } |
| 86 | + } |
| 87 | + return &TextProperties{ |
| 88 | + UnderColor: C.NewPixelWand(), |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Sets canvas' default TextProperties |
| 93 | +func (self *Canvas) SetTextProperties(def *TextProperties) { |
| 94 | + if def != nil { |
| 95 | + self.text = def |
| 96 | + self.SetFontFamily(def.Family) |
| 97 | + self.SetFont(def.Font, def.Size) |
| 98 | + self.SetFontWeight(def.Weight) |
| 99 | + self.SetTextAlignment(def.Alignment) |
| 100 | + self.SetTextAntialias(def.Antialias) |
| 101 | + self.SetTextKerning(def.Kerning) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Gets a copy of canvas' current TextProperties |
| 106 | +func (self *Canvas) TextProperties() *TextProperties { |
| 107 | + if self.text == nil { |
| 108 | + return nil |
| 109 | + } |
| 110 | + cpy := *self.text |
| 111 | + return &cpy |
| 112 | +} |
| 113 | + |
| 114 | +// Sets canvas' default font name |
| 115 | +func (self *Canvas) SetFontName(font string) { |
| 116 | + self.text.Font = font |
| 117 | + cfont := C.CString(font) |
| 118 | + defer C.free(unsafe.Pointer(cfont)) |
| 119 | + C.DrawSetFont(self.drawing, cfont) |
| 120 | +} |
| 121 | + |
| 122 | +// Returns canvas' current font name |
| 123 | +func (self *Canvas) FontName() string { |
| 124 | + return self.text.Font |
| 125 | +} |
| 126 | + |
| 127 | +// Sets canvas' default font family |
| 128 | +func (self *Canvas) SetFontFamily(family string) { |
| 129 | + self.text.Family = family |
| 130 | + cfamily := C.CString(family) |
| 131 | + defer C.free(unsafe.Pointer(cfamily)) |
| 132 | + C.DrawSetFontFamily(self.drawing, cfamily) |
| 133 | +} |
| 134 | + |
| 135 | +// Returns canvas' current font family |
| 136 | +func (self *Canvas) FontFamily() string { |
| 137 | + return self.text.Family |
| 138 | +} |
| 139 | + |
| 140 | +// Sets canvas' default font size |
| 141 | +func (self *Canvas) SetFontSize(size float64) { |
| 142 | + self.text.Size = size |
| 143 | + C.DrawSetFontSize(self.drawing, C.double(size)) |
| 144 | +} |
| 145 | + |
| 146 | +// Returns canvas' current font size |
| 147 | +func (self *Canvas) FontSize() float64 { |
| 148 | + return self.text.Size |
| 149 | +} |
| 150 | + |
| 151 | +// Sets canvas' default font weight |
| 152 | +func (self *Canvas) SetFontWeight(weight uint) { |
| 153 | + self.text.Weight = weight |
| 154 | + C.DrawSetFontWeight(self.drawing, C.size_t(weight)) |
| 155 | +} |
| 156 | + |
| 157 | +// Returns canvas' current font weight |
| 158 | +func (self *Canvas) FontWeight() uint { |
| 159 | + return self.text.Weight |
| 160 | +} |
| 161 | + |
| 162 | +// Sets canvas' font name and size. |
| 163 | +// If font is 0-length, the current font family is not changed |
| 164 | +// If size is <= 0, the current font size is not changed |
| 165 | +func (self *Canvas) SetFont(font string, size float64) { |
| 166 | + if len(font) > 0 { |
| 167 | + self.SetFontName(font) |
| 168 | + } |
| 169 | + if size > 0 { |
| 170 | + self.SetFontSize(size) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// Returns canvas' current font name and size |
| 175 | +func (self *Canvas) Font() (string, float64) { |
| 176 | + return self.text.Font, self.text.Size |
| 177 | +} |
| 178 | + |
| 179 | +// Sets canvas' default text alignment. Available values are: |
| 180 | +// UndefinedAlign (?), LeftAlign, CenterAlign, RightAlign |
| 181 | +func (self *Canvas) SetTextAlignment(a Alignment) { |
| 182 | + self.text.Alignment = a |
| 183 | + C.DrawSetTextAlignment(self.drawing, C.AlignType(a)) |
| 184 | +} |
| 185 | + |
| 186 | +// Returns the canvas' current text aligment |
| 187 | +func (self *Canvas) TextAlignment() Alignment { |
| 188 | + return self.text.Alignment |
| 189 | +} |
| 190 | + |
| 191 | +// Sets canvas' default text antialiasing option. |
| 192 | +func (self *Canvas) SetTextAntialias(b bool) { |
| 193 | + self.text.Antialias = b |
| 194 | + C.DrawSetTextAntialias(self.drawing, magickBoolean(b)) |
| 195 | +} |
| 196 | + |
| 197 | +// Returns the canvas' current text aligment |
| 198 | +func (self *Canvas) TextAntialias() bool { |
| 199 | + return self.text.Antialias |
| 200 | +} |
| 201 | + |
| 202 | +// Sets canvas' default text antialiasing option. |
| 203 | +func (self *Canvas) SetTextKerning(k float64) { |
| 204 | + self.text.Kerning = k |
| 205 | + C.DrawSetTextKerning(self.drawing, C.double(k)) |
| 206 | +} |
| 207 | + |
| 208 | +// Returns the canvas' current text aligment |
| 209 | +func (self *Canvas) TextKerning() float64 { |
| 210 | + return self.text.Kerning |
| 211 | +} |
| 212 | + |
| 213 | +// Draws a string at the specified coordinates and using the current canvas |
| 214 | +// Alignment. |
| 215 | +func (self *Canvas) Annotate(text string, x, y float64) { |
| 216 | + c_text := C.CString(text) |
| 217 | + defer C.free(unsafe.Pointer(c_text)) |
| 218 | + C.DrawAnnotation(self.drawing, C.double(x), C.double(y), (*C.uchar)(unsafe.Pointer(c_text))) |
| 219 | +} |
| 220 | + |
| 221 | +// Draws a string at the specified coordinates and using the specified Text Properties |
| 222 | +// Does not modify the canvas' default TextProperties |
| 223 | +func (self *Canvas) AnnotateWithProperties(text string, x, y float64, prop *TextProperties) { |
| 224 | + tmp := self.TextProperties() |
| 225 | + self.SetTextProperties(prop) |
| 226 | + self.Annotate(text, x, y) |
| 227 | + self.SetTextProperties(tmp) |
| 228 | +} |
0 commit comments