builder.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "github.com/g3n/engine/gui/assets/icon"
  14. "github.com/g3n/engine/math32"
  15. "github.com/g3n/engine/window"
  16. "gopkg.in/yaml.v2"
  17. )
  18. // Builder builds GUI objects from a declarative description in YAML format
  19. type Builder struct {
  20. desc map[string]*descPanel // parsed descriptions
  21. imgpath string // base path for image panels files
  22. objpath strStack // stack of object names being built (used for error messages)
  23. }
  24. // descLayout contains all layout attributes
  25. type descLayout struct {
  26. Type string // Type of the layout: HBox, VBox, Grid, Dock, others...
  27. Cols int // Number of columns for Grid layout
  28. Spacing float32 // Spacing in pixels for HBox and VBox
  29. AlignH string // HBox group alignment type
  30. AlignV string // VBox group alignment type
  31. MinHeight bool // HBox, VBox minimum height flag
  32. MinWidth bool // HBox, VBox minimum width flag
  33. ExpandH bool // Grid
  34. ExpandV bool // Grid
  35. }
  36. // descLayoutParam describes all layout parameters types
  37. type descLayoutParams struct {
  38. Expand *float32 // HBox, VBox expand factor
  39. ColSpan int // Grid layout colspan
  40. AlignH string // horizontal alignment
  41. AlignV string // vertical alignment
  42. Edge string // Dock layout edge: top,right,bottom,left,center
  43. }
  44. // descPanel describes all panel types
  45. type descPanel struct {
  46. Type string // Gui object type: Panel, Label, Edit, etc ...
  47. Name string // Optional name for identification
  48. Position string // Optional position as: x y | x,y
  49. Width *float32 // Optional width (default = 0)
  50. Height *float32 // Optional height (default = 0)
  51. AspectWidth *float32 // Optional aspectwidth (default = nil)
  52. AspectHeight *float32 // Optional aspectwidth (default = nil)
  53. Margins string // Optional margins as 1 or 4 float values
  54. Borders string // Optional borders as 1 or 4 float values
  55. BorderColor string // Optional border color as name or 3 or 4 float values
  56. Paddings string // Optional paddings as 1 or 4 float values
  57. Color string // Optional color as 1 or 4 float values
  58. Enabled *bool // All:
  59. Visible *bool // All:
  60. Renderable *bool // All:
  61. Imagefile string // For Panel, Button
  62. Layout *descLayout // Optional pointer to layout
  63. LayoutParams *descLayoutParams // Optional layout parameters
  64. Text string // Label, Button
  65. Icons string // Label
  66. BgColor string // Label
  67. FontColor string // Label
  68. FontSize *float32 // Label
  69. FontDPI *float32 // Label
  70. LineSpacing *float32 // Label
  71. PlaceHolder string // Edit
  72. MaxLength *uint // Edit
  73. Icon string // Button
  74. Group string // RadioButton
  75. Checked bool // CheckBox, RadioButton
  76. ImageLabel *descPanel // DropDown
  77. Items []*descPanel // Menu, MenuBar
  78. Shortcut string // Menu
  79. Value *float32 // Slider
  80. ScaleFactor *float32 // Slider
  81. Title string // Window
  82. Resizable string // Window resizable borders
  83. P0 *descPanel // Splitter panel 0
  84. P1 *descPanel // Splitter panel 1
  85. Split *float32 // Splitter split value
  86. parent *descPanel // used internally
  87. }
  88. const (
  89. descTypePanel = "panel"
  90. descTypeImagePanel = "imagepanel"
  91. descTypeLabel = "label"
  92. descTypeImageLabel = "imagelabel"
  93. descTypeButton = "button"
  94. descTypeCheckBox = "checkbox"
  95. descTypeRadioButton = "radiobutton"
  96. descTypeEdit = "edit"
  97. descTypeVList = "vlist"
  98. descTypeHList = "hlist"
  99. descTypeDropDown = "dropdown"
  100. descTypeHSlider = "hslider"
  101. descTypeVSlider = "vslider"
  102. descTypeHSplitter = "hsplitter"
  103. descTypeVSplitter = "vsplitter"
  104. descTypeTree = "tree"
  105. descTypeTreeNode = "node"
  106. descTypeMenuBar = "menubar"
  107. descTypeMenu = "menu"
  108. descTypeWindow = "window"
  109. descTypeHBoxLayout = "hbox"
  110. descTypeVBoxLayout = "vbox"
  111. descTypeGridLayout = "grid"
  112. descTypeDockLayout = "dock"
  113. fieldMargins = "margins"
  114. fieldBorders = "borders"
  115. fieldBorderColor = "bordercolor"
  116. fieldPaddings = "paddings"
  117. fieldColor = "color"
  118. fieldBgColor = "bgcolor"
  119. )
  120. const (
  121. aPOS = 1 << iota // attribute position
  122. aSIZE = 1 << iota // attribute size
  123. aNAME = 1 << iota // attribute name
  124. aMARGINS = 1 << iota // attribute margins widths
  125. aBORDERS = 1 << iota // attribute borders widths
  126. aBORDERCOLOR = 1 << iota // attribute border color
  127. aPADDINGS = 1 << iota // attribute paddings widths
  128. aCOLOR = 1 << iota // attribute panel bgcolor
  129. aENABLED = 1 << iota // attribute enabled for events
  130. aRENDER = 1 << iota // attribute renderable
  131. aVISIBLE = 1 << iota // attribute visible
  132. asPANEL = 0xFF // attribute set for panels
  133. asWIDGET = aPOS | aNAME | aENABLED | aVISIBLE // attribute set for widgets
  134. asBUTTON = aPOS | aSIZE | aNAME | aENABLED | aVISIBLE // attribute set for buttons
  135. )
  136. // maps align name with align parameter
  137. var mapAlignName = map[string]Align{
  138. "none": AlignNone,
  139. "left": AlignLeft,
  140. "right": AlignRight,
  141. "width": AlignWidth,
  142. "top": AlignTop,
  143. "bottom": AlignBottom,
  144. "height": AlignHeight,
  145. "center": AlignCenter,
  146. }
  147. // maps edge name (dock layout) with edge parameter
  148. var mapEdgeName = map[string]int{
  149. "top": DockTop,
  150. "right": DockRight,
  151. "bottom": DockBottom,
  152. "left": DockLeft,
  153. "center": DockCenter,
  154. }
  155. // maps resize border name (window) with parameter value
  156. var mapResizable = map[string]Resizable{
  157. "top": ResizeTop,
  158. "right": ResizeRight,
  159. "bottom": ResizeBottom,
  160. "left": ResizeLeft,
  161. "all": ResizeAll,
  162. }
  163. // NewBuilder creates and returns a pointer to a new gui Builder object
  164. func NewBuilder() *Builder {
  165. return new(Builder)
  166. }
  167. // ParseString parses a string with gui objects descriptions in YAML format
  168. // It there was a previously parsed description, it is cleared.
  169. func (b *Builder) ParseString(desc string) error {
  170. // Try assuming the description contains a single root panel
  171. var dp descPanel
  172. err := yaml.Unmarshal([]byte(desc), &dp)
  173. if err != nil {
  174. return err
  175. }
  176. if dp.Type != "" {
  177. b.desc = make(map[string]*descPanel)
  178. b.desc[""] = &dp
  179. b.setupDescTree(&dp)
  180. return nil
  181. }
  182. // Try assuming the description is a map of panels
  183. var dpm map[string]*descPanel
  184. err = yaml.Unmarshal([]byte(desc), &dpm)
  185. if err != nil {
  186. return err
  187. }
  188. b.desc = dpm
  189. for _, v := range dpm {
  190. b.setupDescTree(v)
  191. }
  192. return nil
  193. }
  194. // ParseFile parses a file with gui objects descriptions in YAML format
  195. // It there was a previously parsed description, it is cleared.
  196. func (b *Builder) ParseFile(filepath string) error {
  197. // Reads all file data
  198. f, err := os.Open(filepath)
  199. if err != nil {
  200. return err
  201. }
  202. data, err := ioutil.ReadAll(f)
  203. if err != nil {
  204. return err
  205. }
  206. err = f.Close()
  207. if err != nil {
  208. return err
  209. }
  210. // Parses file data
  211. return b.ParseString(string(data))
  212. }
  213. // Names returns a sorted list of names of top level previously parsed objects.
  214. // Only objects with defined types are returned.
  215. // If there is only a single object with no name, its name is returned
  216. // as an empty string
  217. func (b *Builder) Names() []string {
  218. var objs []string
  219. for name, pd := range b.desc {
  220. if pd.Type != "" {
  221. objs = append(objs, name)
  222. }
  223. }
  224. sort.Strings(objs)
  225. return objs
  226. }
  227. // Build builds a gui object and all its children recursively.
  228. // The specified name should be a top level name from a
  229. // from a previously parsed description
  230. // If the descriptions contains a single object with no name,
  231. // It should be specified the empty string to build this object.
  232. func (b *Builder) Build(name string) (IPanel, error) {
  233. pd, ok := b.desc[name]
  234. if !ok {
  235. return nil, fmt.Errorf("Object name:%s not found", name)
  236. }
  237. b.objpath.clear()
  238. b.objpath.push(pd.Name)
  239. return b.build(pd, nil)
  240. }
  241. // Sets the path for image panels relative image files
  242. func (b *Builder) SetImagepath(path string) {
  243. b.imgpath = path
  244. }
  245. // build builds the gui object from the specified description.
  246. // All its children are also built recursively
  247. // Returns the built object or an error
  248. func (b *Builder) build(pd *descPanel, iparent IPanel) (IPanel, error) {
  249. var err error
  250. var pan IPanel
  251. switch pd.Type {
  252. case descTypePanel:
  253. pan, err = b.buildPanel(pd)
  254. case descTypeImagePanel:
  255. pan, err = b.buildImagePanel(pd)
  256. case descTypeLabel:
  257. pan, err = b.buildLabel(pd)
  258. case descTypeImageLabel:
  259. pan, err = b.buildImageLabel(pd)
  260. case descTypeButton:
  261. pan, err = b.buildButton(pd)
  262. case descTypeCheckBox:
  263. pan, err = b.buildCheckBox(pd)
  264. case descTypeRadioButton:
  265. pan, err = b.buildRadioButton(pd)
  266. case descTypeEdit:
  267. pan, err = b.buildEdit(pd)
  268. case descTypeVList:
  269. pan, err = b.buildVList(pd)
  270. case descTypeHList:
  271. pan, err = b.buildHList(pd)
  272. case descTypeDropDown:
  273. pan, err = b.buildDropDown(pd)
  274. case descTypeHSlider:
  275. pan, err = b.buildSlider(pd, true)
  276. case descTypeVSlider:
  277. pan, err = b.buildSlider(pd, false)
  278. case descTypeHSplitter:
  279. pan, err = b.buildSplitter(pd, true)
  280. case descTypeVSplitter:
  281. pan, err = b.buildSplitter(pd, false)
  282. case descTypeTree:
  283. pan, err = b.buildTree(pd)
  284. case descTypeMenuBar:
  285. pan, err = b.buildMenu(pd, false, true)
  286. case descTypeMenu:
  287. pan, err = b.buildMenu(pd, false, false)
  288. case descTypeWindow:
  289. pan, err = b.buildWindow(pd)
  290. default:
  291. err = fmt.Errorf("Invalid panel type:%s", pd.Type)
  292. }
  293. if err != nil {
  294. return nil, err
  295. }
  296. if iparent != nil {
  297. iparent.GetPanel().Add(pan)
  298. }
  299. return pan, nil
  300. }
  301. // buildPanel builds a gui object of type: "Panel"
  302. func (b *Builder) buildPanel(dp *descPanel) (IPanel, error) {
  303. // Builds panel and set attributes
  304. width, height := b.size(dp)
  305. pan := NewPanel(width, height)
  306. err := b.setAttribs(dp, pan, asPANEL)
  307. if err != nil {
  308. return nil, err
  309. }
  310. // Builds panel children recursively
  311. for i := 0; i < len(dp.Items); i++ {
  312. item := dp.Items[i]
  313. b.objpath.push(item.Name)
  314. child, err := b.build(item, pan)
  315. b.objpath.pop()
  316. if err != nil {
  317. return nil, err
  318. }
  319. pan.Add(child)
  320. }
  321. return pan, nil
  322. }
  323. // buildImagePanel builds a gui object of type: "ImagePanel"
  324. func (b *Builder) buildImagePanel(pd *descPanel) (IPanel, error) {
  325. // Imagefile must be supplied
  326. if pd.Imagefile == "" {
  327. return nil, b.err("Imagefile", "Imagefile must be supplied")
  328. }
  329. // If path is not absolute join with user supplied image base path
  330. path := pd.Imagefile
  331. if !filepath.IsAbs(path) {
  332. path = filepath.Join(b.imgpath, path)
  333. }
  334. // Builds panel and set common attributes
  335. panel, err := NewImage(path)
  336. if err != nil {
  337. return nil, err
  338. }
  339. err = b.setAttribs(pd, panel, asPANEL)
  340. if err != nil {
  341. return nil, err
  342. }
  343. // AspectWidth and AspectHeight attributes
  344. if pd.AspectWidth != nil {
  345. panel.SetContentAspectWidth(*pd.AspectWidth)
  346. }
  347. if pd.AspectHeight != nil {
  348. panel.SetContentAspectHeight(*pd.AspectHeight)
  349. }
  350. // Builds panel children recursively
  351. for i := 0; i < len(pd.Items); i++ {
  352. item := pd.Items[i]
  353. b.objpath.push(item.Name)
  354. child, err := b.build(item, panel)
  355. b.objpath.pop()
  356. if err != nil {
  357. return nil, err
  358. }
  359. panel.Add(child)
  360. }
  361. return panel, nil
  362. }
  363. // buildLabel builds a gui object of type: "Label"
  364. func (b *Builder) buildLabel(pd *descPanel) (IPanel, error) {
  365. // Builds label with icon or text font
  366. var label *Label
  367. icons, err := b.parseIconNames("icons", pd.Icons)
  368. if err != nil {
  369. return nil, err
  370. }
  371. if icons != "" {
  372. label = NewLabel(icons, true)
  373. } else {
  374. label = NewLabel(pd.Text)
  375. }
  376. // Sets common attributes
  377. err = b.setAttribs(pd, label, asPANEL)
  378. if err != nil {
  379. return nil, err
  380. }
  381. // Set optional background color
  382. c, err := b.parseColor(fieldBgColor, pd.BgColor)
  383. if err != nil {
  384. return nil, err
  385. }
  386. if c != nil {
  387. label.SetBgColor4(c)
  388. }
  389. // Set optional font color
  390. c, err = b.parseColor("fontcolor", pd.FontColor)
  391. if err != nil {
  392. return nil, err
  393. }
  394. if c != nil {
  395. label.SetColor4(c)
  396. }
  397. // Sets optional font size
  398. if pd.FontSize != nil {
  399. label.SetFontSize(float64(*pd.FontSize))
  400. }
  401. // Sets optional font dpi
  402. if pd.FontDPI != nil {
  403. label.SetFontDPI(float64(*pd.FontDPI))
  404. }
  405. // Sets optional line spacing
  406. if pd.LineSpacing != nil {
  407. label.SetLineSpacing(float64(*pd.LineSpacing))
  408. }
  409. return label, nil
  410. }
  411. // buildImageLabel builds a gui object of type: ImageLabel
  412. func (b *Builder) buildImageLabel(pd *descPanel) (IPanel, error) {
  413. // Builds image label and set common attributes
  414. imglabel := NewImageLabel(pd.Text)
  415. err := b.setAttribs(pd, imglabel, asPANEL)
  416. if err != nil {
  417. return nil, err
  418. }
  419. // Sets optional icon(s)
  420. icons, err := b.parseIconNames("icons", pd.Icons)
  421. if err != nil {
  422. return nil, err
  423. }
  424. if icons != "" {
  425. imglabel.SetIcon(icons)
  426. }
  427. // Sets optional image from file
  428. // If path is not absolute join with user supplied image base path
  429. if pd.Imagefile != "" {
  430. path := pd.Imagefile
  431. if !filepath.IsAbs(path) {
  432. path = filepath.Join(b.imgpath, path)
  433. }
  434. err := imglabel.SetImageFromFile(path)
  435. if err != nil {
  436. return nil, err
  437. }
  438. }
  439. return imglabel, nil
  440. }
  441. // buildButton builds a gui object of type: Button
  442. func (b *Builder) buildButton(pd *descPanel) (IPanel, error) {
  443. // Builds button and set commont attributes
  444. button := NewButton(pd.Text)
  445. err := b.setAttribs(pd, button, asBUTTON)
  446. if err != nil {
  447. return nil, err
  448. }
  449. // Sets optional icon
  450. if pd.Icon != "" {
  451. cp, err := b.parseIconName("icon", pd.Icon)
  452. if err != nil {
  453. return nil, err
  454. }
  455. button.SetIcon(cp)
  456. }
  457. // Sets optional image from file
  458. // If path is not absolute join with user supplied image base path
  459. if pd.Imagefile != "" {
  460. path := pd.Imagefile
  461. if !filepath.IsAbs(path) {
  462. path = filepath.Join(b.imgpath, path)
  463. }
  464. err := button.SetImage(path)
  465. if err != nil {
  466. return nil, err
  467. }
  468. }
  469. return button, nil
  470. }
  471. // buildCheckBox builds a gui object of type: CheckBox
  472. func (b *Builder) buildCheckBox(pd *descPanel) (IPanel, error) {
  473. // Builds check box and set commont attributes
  474. cb := NewCheckBox(pd.Text)
  475. err := b.setAttribs(pd, cb, asWIDGET)
  476. if err != nil {
  477. return nil, err
  478. }
  479. cb.SetValue(pd.Checked)
  480. return cb, nil
  481. }
  482. // buildRadioButton builds a gui object of type: RadioButton
  483. func (b *Builder) buildRadioButton(pd *descPanel) (IPanel, error) {
  484. // Builds check box and set commont attributes
  485. rb := NewRadioButton(pd.Text)
  486. err := b.setAttribs(pd, rb, asWIDGET)
  487. if err != nil {
  488. return nil, err
  489. }
  490. // Sets optional radio button group
  491. if pd.Group != "" {
  492. rb.SetGroup(pd.Group)
  493. }
  494. rb.SetValue(pd.Checked)
  495. return rb, nil
  496. }
  497. // buildEdit builds a gui object of type: "Edit"
  498. func (b *Builder) buildEdit(dp *descPanel) (IPanel, error) {
  499. // Builds button and set attributes
  500. width, _ := b.size(dp)
  501. edit := NewEdit(int(width), dp.PlaceHolder)
  502. err := b.setAttribs(dp, edit, asWIDGET)
  503. if err != nil {
  504. return nil, err
  505. }
  506. edit.SetText(dp.Text)
  507. return edit, nil
  508. }
  509. // buildVList builds a gui object of type: VList
  510. func (b *Builder) buildVList(dp *descPanel) (IPanel, error) {
  511. // Builds list and set commont attributes
  512. width, height := b.size(dp)
  513. list := NewVList(width, height)
  514. err := b.setAttribs(dp, list, asWIDGET)
  515. if err != nil {
  516. return nil, err
  517. }
  518. // Builds list children
  519. for i := 0; i < len(dp.Items); i++ {
  520. item := dp.Items[i]
  521. b.objpath.push(item.Name)
  522. child, err := b.build(item, list)
  523. b.objpath.pop()
  524. if err != nil {
  525. return nil, err
  526. }
  527. list.Add(child)
  528. }
  529. return list, nil
  530. }
  531. // buildHList builds a gui object of type: VList
  532. func (b *Builder) buildHList(dp *descPanel) (IPanel, error) {
  533. // Builds list and set commont attributes
  534. width, height := b.size(dp)
  535. list := NewHList(width, height)
  536. err := b.setAttribs(dp, list, asWIDGET)
  537. if err != nil {
  538. return nil, err
  539. }
  540. // Builds list children
  541. for i := 0; i < len(dp.Items); i++ {
  542. item := dp.Items[i]
  543. b.objpath.push(item.Name)
  544. child, err := b.build(item, list)
  545. b.objpath.pop()
  546. if err != nil {
  547. return nil, err
  548. }
  549. list.Add(child)
  550. }
  551. return list, nil
  552. }
  553. // buildDropDown builds a gui object of type: DropDown
  554. func (b *Builder) buildDropDown(pd *descPanel) (IPanel, error) {
  555. // If image label attribute defined use it, otherwise
  556. // uses default value.
  557. var imglabel *ImageLabel
  558. if pd.ImageLabel != nil {
  559. pd.ImageLabel.Type = descTypeImageLabel
  560. ipan, err := b.build(pd.ImageLabel, nil)
  561. if err != nil {
  562. return nil, err
  563. }
  564. imglabel = ipan.(*ImageLabel)
  565. } else {
  566. imglabel = NewImageLabel("")
  567. }
  568. // Builds drop down and set common attributes
  569. width, _ := b.size(pd)
  570. dd := NewDropDown(width, imglabel)
  571. err := b.setAttribs(pd, dd, asWIDGET)
  572. if err != nil {
  573. return nil, err
  574. }
  575. // Builds drop down children
  576. for i := 0; i < len(pd.Items); i++ {
  577. item := pd.Items[i]
  578. item.Type = descTypeImageLabel
  579. b.objpath.push(item.Name)
  580. child, err := b.build(item, dd)
  581. b.objpath.pop()
  582. if err != nil {
  583. return nil, err
  584. }
  585. dd.Add(child.(*ImageLabel))
  586. }
  587. return dd, nil
  588. }
  589. // buildSlider builds a gui object of type: HSlider or VSlider
  590. func (b *Builder) buildSlider(pd *descPanel, horiz bool) (IPanel, error) {
  591. // Builds slider and sets its position
  592. width, height := b.size(pd)
  593. var slider *Slider
  594. if horiz {
  595. slider = NewHSlider(width, height)
  596. } else {
  597. slider = NewVSlider(width, height)
  598. }
  599. err := b.setAttribs(pd, slider, asWIDGET)
  600. if err != nil {
  601. return nil, err
  602. }
  603. // Sets optional text
  604. if pd.Text != "" {
  605. slider.SetText(pd.Text)
  606. }
  607. // Sets optional scale factor
  608. if pd.ScaleFactor != nil {
  609. slider.SetScaleFactor(*pd.ScaleFactor)
  610. }
  611. // Sets optional value
  612. if pd.Value != nil {
  613. slider.SetValue(*pd.Value)
  614. }
  615. return slider, nil
  616. }
  617. // buildSplitter builds a gui object of type: HSplitterr or VSplitter
  618. func (b *Builder) buildSplitter(pd *descPanel, horiz bool) (IPanel, error) {
  619. // Builds splitter and sets its common attributes
  620. width, height := b.size(pd)
  621. var splitter *Splitter
  622. if horiz {
  623. splitter = NewHSplitter(width, height)
  624. } else {
  625. splitter = NewVSplitter(width, height)
  626. }
  627. err := b.setAttribs(pd, splitter, asWIDGET)
  628. if err != nil {
  629. return nil, err
  630. }
  631. // Optional split value
  632. if pd.Split != nil {
  633. splitter.SetSplit(*pd.Split)
  634. }
  635. // Splitter panel 0 attributes and items
  636. if pd.P0 != nil {
  637. err := b.setAttribs(pd.P0, &splitter.P0, asPANEL)
  638. if err != nil {
  639. return nil, err
  640. }
  641. err = b.addPanelItems(pd.P0, &splitter.P0)
  642. if err != nil {
  643. return nil, err
  644. }
  645. }
  646. // Splitter panel 1 attributes and items
  647. if pd.P1 != nil {
  648. err := b.setAttribs(pd.P1, &splitter.P1, asPANEL)
  649. if err != nil {
  650. return nil, err
  651. }
  652. err = b.addPanelItems(pd.P1, &splitter.P1)
  653. if err != nil {
  654. return nil, err
  655. }
  656. }
  657. return splitter, nil
  658. }
  659. // buildTree builds a gui object of type: Tree
  660. func (b *Builder) buildTree(dp *descPanel) (IPanel, error) {
  661. // Builds tree and sets its common attributes
  662. width, height := b.size(dp)
  663. tree := NewTree(width, height)
  664. err := b.setAttribs(dp, tree, asWIDGET)
  665. if err != nil {
  666. return nil, err
  667. }
  668. // Internal function to build tree nodes recursively
  669. var buildItems func(dp *descPanel, pnode *TreeNode) error
  670. buildItems = func(dp *descPanel, pnode *TreeNode) error {
  671. for i := 0; i < len(dp.Items); i++ {
  672. item := dp.Items[i]
  673. // Item is a tree node
  674. if item.Type == "" || item.Type == descTypeTreeNode {
  675. var node *TreeNode
  676. if pnode == nil {
  677. node = tree.AddNode(item.Text)
  678. } else {
  679. node = pnode.AddNode(item.Text)
  680. }
  681. err := buildItems(item, node)
  682. if err != nil {
  683. return err
  684. }
  685. continue
  686. }
  687. // Other controls
  688. ipan, err := b.build(item, nil)
  689. if err != nil {
  690. return err
  691. }
  692. if pnode == nil {
  693. tree.Add(ipan)
  694. } else {
  695. pnode.Add(ipan)
  696. }
  697. }
  698. return nil
  699. }
  700. // Build nodes
  701. err = buildItems(dp, nil)
  702. if err != nil {
  703. return nil, err
  704. }
  705. return tree, nil
  706. }
  707. // buildMenu builds a gui object of type: Menu or MenuBar from the
  708. // specified panel descriptor.
  709. func (b *Builder) buildMenu(pd *descPanel, child, bar bool) (IPanel, error) {
  710. // Builds menu bar or menu
  711. var menu *Menu
  712. if bar {
  713. menu = NewMenuBar()
  714. } else {
  715. menu = NewMenu()
  716. }
  717. // Only sets attribs for top level menus
  718. if !child {
  719. err := b.setAttribs(pd, menu, asWIDGET)
  720. if err != nil {
  721. return nil, err
  722. }
  723. }
  724. // Builds and adds menu items
  725. for i := 0; i < len(pd.Items); i++ {
  726. item := pd.Items[i]
  727. // Item is another menu
  728. if item.Type == descTypeMenu {
  729. subm, err := b.buildMenu(item, true, false)
  730. if err != nil {
  731. return nil, err
  732. }
  733. menu.AddMenu(item.Text, subm.(*Menu))
  734. continue
  735. }
  736. // Item is a separator
  737. if item.Type == "Separator" {
  738. menu.AddSeparator()
  739. continue
  740. }
  741. // Item must be a menu option
  742. mi := menu.AddOption(item.Text)
  743. // Set item optional icon(s)
  744. icons, err := b.parseIconNames("icon", item.Icon)
  745. if err != nil {
  746. return nil, err
  747. }
  748. if icons != "" {
  749. mi.SetIcon(string(icons))
  750. }
  751. // Sets optional menu item shortcut
  752. err = b.setMenuShortcut(mi, "shortcut", item.Shortcut)
  753. if err != nil {
  754. return nil, err
  755. }
  756. }
  757. return menu, nil
  758. }
  759. // buildWindow builds a gui object of type: Window from the
  760. // specified panel descriptor.
  761. func (b *Builder) buildWindow(dp *descPanel) (IPanel, error) {
  762. // Builds window and sets its common attributes
  763. width, height := b.size(dp)
  764. win := NewWindow(width, height)
  765. err := b.setAttribs(dp, win, asWIDGET)
  766. if err != nil {
  767. return nil, err
  768. }
  769. // Title attribute
  770. win.SetTitle(dp.Title)
  771. // Parse resizable borders
  772. if dp.Resizable != "" {
  773. parts := strings.Fields(dp.Resizable)
  774. var res Resizable
  775. for _, name := range parts {
  776. v, ok := mapResizable[name]
  777. if !ok {
  778. return nil, b.err("resizable", "Invalid resizable name:"+name)
  779. }
  780. res |= v
  781. }
  782. win.SetResizable(res)
  783. }
  784. // Builds window client panel children recursively
  785. for i := 0; i < len(dp.Items); i++ {
  786. item := dp.Items[i]
  787. b.objpath.push(item.Name)
  788. child, err := b.build(item, win)
  789. b.objpath.pop()
  790. if err != nil {
  791. return nil, err
  792. }
  793. win.Add(child)
  794. }
  795. return win, nil
  796. }
  797. // addPanelItems adds the items in the panel descriptor to the specified panel
  798. func (b *Builder) addPanelItems(dp *descPanel, ipan IPanel) error {
  799. pan := ipan.GetPanel()
  800. for i := 0; i < len(dp.Items); i++ {
  801. item := dp.Items[i]
  802. b.objpath.push(item.Name)
  803. child, err := b.build(item, pan)
  804. b.objpath.pop()
  805. if err != nil {
  806. return err
  807. }
  808. pan.Add(child)
  809. }
  810. return nil
  811. }
  812. // setAttribs sets common attributes from the description to the specified panel
  813. // The attributes which are set can be specified by the specified bitmask.
  814. func (b *Builder) setAttribs(pd *descPanel, ipan IPanel, attr uint) error {
  815. panel := ipan.GetPanel()
  816. // Set optional position
  817. if attr&aPOS != 0 && pd.Position != "" {
  818. va, err := b.parseFloats("position", pd.Position, 2, 2)
  819. if va == nil || err != nil {
  820. return err
  821. }
  822. panel.SetPosition(va[0], va[1])
  823. }
  824. // Set optional size
  825. if attr&aSIZE != 0 {
  826. if pd.Width != nil {
  827. panel.SetWidth(*pd.Width)
  828. }
  829. if pd.Height != nil {
  830. panel.SetHeight(*pd.Height)
  831. }
  832. }
  833. // Set optional margin sizes
  834. if attr&aMARGINS != 0 {
  835. bs, err := b.parseBorderSizes(fieldMargins, pd.Margins)
  836. if err != nil {
  837. return err
  838. }
  839. if bs != nil {
  840. panel.SetMarginsFrom(bs)
  841. }
  842. }
  843. // Set optional border sizes
  844. if attr&aBORDERS != 0 {
  845. bs, err := b.parseBorderSizes(fieldBorders, pd.Borders)
  846. if err != nil {
  847. return err
  848. }
  849. if bs != nil {
  850. panel.SetBordersFrom(bs)
  851. }
  852. }
  853. // Set optional border color
  854. if attr&aBORDERCOLOR != 0 {
  855. c, err := b.parseColor(fieldBorderColor, pd.BorderColor)
  856. if err != nil {
  857. return err
  858. }
  859. if c != nil {
  860. panel.SetBordersColor4(c)
  861. }
  862. }
  863. // Set optional paddings sizes
  864. if attr&aPADDINGS != 0 {
  865. bs, err := b.parseBorderSizes(fieldPaddings, pd.Paddings)
  866. if err != nil {
  867. return err
  868. }
  869. if bs != nil {
  870. panel.SetPaddingsFrom(bs)
  871. }
  872. }
  873. // Set optional color
  874. if attr&aCOLOR != 0 {
  875. c, err := b.parseColor(fieldColor, pd.Color)
  876. if err != nil {
  877. return err
  878. }
  879. if c != nil {
  880. panel.SetColor4(c)
  881. }
  882. }
  883. if attr&aNAME != 0 && pd.Name != "" {
  884. panel.SetName(pd.Name)
  885. }
  886. if attr&aVISIBLE != 0 && pd.Visible != nil {
  887. panel.SetVisible(*pd.Visible)
  888. }
  889. if attr&aENABLED != 0 && pd.Enabled != nil {
  890. panel.SetEnabled(*pd.Enabled)
  891. }
  892. if attr&aRENDER != 0 && pd.Renderable != nil {
  893. panel.SetRenderable(*pd.Renderable)
  894. }
  895. err := b.setLayoutParams(pd, ipan)
  896. if err != nil {
  897. return err
  898. }
  899. return b.setLayout(pd, ipan)
  900. }
  901. // setLayoutParams sets the optional layout params attribute for specified the panel
  902. func (b *Builder) setLayoutParams(dp *descPanel, ipan IPanel) error {
  903. // If layout params not declared, nothing to do
  904. if dp.LayoutParams == nil {
  905. return nil
  906. }
  907. // Get the parent layout
  908. if dp.parent == nil {
  909. return b.err("layoutparams", "No parent defined")
  910. }
  911. playout := dp.parent.Layout
  912. if playout == nil {
  913. return b.err("layoutparams", "Parent does not have layout")
  914. }
  915. panel := ipan.GetPanel()
  916. dlp := dp.LayoutParams
  917. // HBoxLayout parameters
  918. if playout.Type == descTypeHBoxLayout {
  919. // Creates layout parameter
  920. params := HBoxLayoutParams{Expand: 0, AlignV: AlignTop}
  921. // Sets optional expand parameter
  922. if dlp.Expand != nil {
  923. params.Expand = *dlp.Expand
  924. }
  925. // Sets optional align parameter
  926. if dlp.AlignV != "" {
  927. align, ok := mapAlignName[dlp.AlignV]
  928. if !ok {
  929. return b.err("align", "Invalid align name:"+dlp.AlignV)
  930. }
  931. params.AlignV = align
  932. }
  933. panel.SetLayoutParams(&params)
  934. return nil
  935. }
  936. // VBoxLayout parameters
  937. if playout.Type == descTypeVBoxLayout {
  938. // Creates layout parameter
  939. params := VBoxLayoutParams{Expand: 0, AlignH: AlignLeft}
  940. // Sets optional expand parameter
  941. if dlp.Expand != nil {
  942. params.Expand = *dlp.Expand
  943. }
  944. // Sets optional align parameter
  945. if dlp.AlignH != "" {
  946. align, ok := mapAlignName[dlp.AlignH]
  947. if !ok {
  948. return b.err("align", "Invalid align name:"+dlp.AlignH)
  949. }
  950. params.AlignH = align
  951. }
  952. panel.SetLayoutParams(&params)
  953. return nil
  954. }
  955. // GridLayout parameters
  956. if playout.Type == descTypeGridLayout {
  957. // Creates layout parameter
  958. params := GridLayoutParams{
  959. ColSpan: 0,
  960. AlignH: AlignNone,
  961. AlignV: AlignNone,
  962. }
  963. params.ColSpan = dlp.ColSpan
  964. // Sets optional alignh parameter
  965. if dlp.AlignH != "" {
  966. align, ok := mapAlignName[dlp.AlignH]
  967. if !ok {
  968. return b.err("alignh", "Invalid align name:"+dlp.AlignH)
  969. }
  970. params.AlignH = align
  971. }
  972. // Sets optional alignv parameter
  973. if dlp.AlignV != "" {
  974. align, ok := mapAlignName[dlp.AlignV]
  975. if !ok {
  976. return b.err("alignv", "Invalid align name:"+dlp.AlignV)
  977. }
  978. params.AlignV = align
  979. }
  980. panel.SetLayoutParams(&params)
  981. return nil
  982. }
  983. // DockLayout parameters
  984. if playout.Type == descTypeDockLayout {
  985. if dlp.Edge != "" {
  986. edge, ok := mapEdgeName[dlp.Edge]
  987. if !ok {
  988. return b.err("edge", "Invalid edge name:"+dlp.Edge)
  989. }
  990. params := DockLayoutParams{Edge: edge}
  991. panel.SetLayoutParams(&params)
  992. return nil
  993. }
  994. }
  995. return b.err("layoutparams", "Invalid parent layout:"+playout.Type)
  996. }
  997. // setLayout sets the optional panel layout and layout parameters
  998. func (b *Builder) setLayout(dp *descPanel, ipan IPanel) error {
  999. // If layout types not declared, nothing to do
  1000. if dp.Layout == nil {
  1001. return nil
  1002. }
  1003. dl := dp.Layout
  1004. // HBox layout
  1005. if dl.Type == descTypeHBoxLayout {
  1006. hbl := NewHBoxLayout()
  1007. hbl.SetSpacing(dl.Spacing)
  1008. if dl.AlignH != "" {
  1009. align, ok := mapAlignName[dl.AlignH]
  1010. if !ok {
  1011. return b.err("align", "Invalid align name:"+dl.AlignV)
  1012. }
  1013. hbl.SetAlignH(align)
  1014. }
  1015. hbl.SetMinHeight(dl.MinHeight)
  1016. hbl.SetMinWidth(dl.MinWidth)
  1017. ipan.SetLayout(hbl)
  1018. return nil
  1019. }
  1020. // VBox layout
  1021. if dl.Type == descTypeVBoxLayout {
  1022. vbl := NewVBoxLayout()
  1023. vbl.SetSpacing(dl.Spacing)
  1024. if dl.AlignV != "" {
  1025. align, ok := mapAlignName[dl.AlignV]
  1026. if !ok {
  1027. return b.err("align", "Invalid align name:"+dl.AlignV)
  1028. }
  1029. vbl.SetAlignV(align)
  1030. }
  1031. vbl.SetMinHeight(dl.MinHeight)
  1032. vbl.SetMinWidth(dl.MinWidth)
  1033. ipan.SetLayout(vbl)
  1034. return nil
  1035. }
  1036. // Grid layout
  1037. if dl.Type == descTypeGridLayout {
  1038. // Number of columns
  1039. if dl.Cols == 0 {
  1040. return b.err("cols", "Invalid number of columns:"+dl.AlignH)
  1041. }
  1042. grl := NewGridLayout(dl.Cols)
  1043. // Global horizontal alignment
  1044. if dl.AlignH != "" {
  1045. alignh, ok := mapAlignName[dl.AlignH]
  1046. if !ok {
  1047. return b.err("alignh", "Invalid horizontal align:"+dl.AlignH)
  1048. }
  1049. grl.SetAlignH(alignh)
  1050. }
  1051. // Global vertical alignment
  1052. if dl.AlignV != "" {
  1053. alignv, ok := mapAlignName[dl.AlignV]
  1054. if !ok {
  1055. return b.err("alignv", "Invalid vertical align:"+dl.AlignH)
  1056. }
  1057. grl.SetAlignV(alignv)
  1058. }
  1059. // Expansion flags
  1060. grl.SetExpandH(dl.ExpandH)
  1061. grl.SetExpandV(dl.ExpandV)
  1062. ipan.SetLayout(grl)
  1063. return nil
  1064. }
  1065. // Dock layout
  1066. if dl.Type == descTypeDockLayout {
  1067. dockl := NewDockLayout()
  1068. ipan.SetLayout(dockl)
  1069. return nil
  1070. }
  1071. return b.err("layout", "Invalid layout type:"+dl.Type)
  1072. }
  1073. func (b *Builder) setMenuShortcut(mi *MenuItem, fname, field string) error {
  1074. field = strings.Trim(field, " ")
  1075. if field == "" {
  1076. return nil
  1077. }
  1078. parts := strings.Split(field, "+")
  1079. var mods window.ModifierKey
  1080. for i := 0; i < len(parts)-1; i++ {
  1081. switch parts[i] {
  1082. case "Shift":
  1083. mods |= window.ModShift
  1084. case "Ctrl":
  1085. mods |= window.ModControl
  1086. case "Alt":
  1087. mods |= window.ModAlt
  1088. default:
  1089. return b.err(fname, "Invalid shortcut:"+field)
  1090. }
  1091. }
  1092. // The last part must be a key
  1093. key := parts[len(parts)-1]
  1094. for kcode, kname := range mapKeyText {
  1095. if kname == key {
  1096. mi.SetShortcut(mods, kcode)
  1097. return nil
  1098. }
  1099. }
  1100. return b.err(fname, "Invalid shortcut:"+field)
  1101. }
  1102. // parseBorderSizes parses a string field which can contain one float value or
  1103. // float values. In the first case all borders has the same width
  1104. func (b *Builder) parseBorderSizes(fname, field string) (*BorderSizes, error) {
  1105. va, err := b.parseFloats(fname, field, 1, 4)
  1106. if va == nil || err != nil {
  1107. return nil, err
  1108. }
  1109. if len(va) == 1 {
  1110. return &BorderSizes{va[0], va[0], va[0], va[0]}, nil
  1111. }
  1112. return &BorderSizes{va[0], va[1], va[2], va[3]}, nil
  1113. }
  1114. // parseColor parses a string field which can contain a color name or
  1115. // a list of 3 or 4 float values for the color components
  1116. func (b *Builder) parseColor(fname, field string) (*math32.Color4, error) {
  1117. // Checks if field is empty
  1118. field = strings.Trim(field, " ")
  1119. if field == "" {
  1120. return nil, nil
  1121. }
  1122. // If string has 1 or 2 fields it must be a color name and optional alpha
  1123. parts := strings.Fields(field)
  1124. if len(parts) == 1 || len(parts) == 2 {
  1125. // First part must be a color name
  1126. if !math32.IsColor(parts[0]) {
  1127. return nil, b.err(fname, fmt.Sprintf("Invalid color name:%s", parts[0]))
  1128. }
  1129. c := math32.ColorName(parts[0])
  1130. c4 := math32.Color4{c.R, c.G, c.B, 1}
  1131. if len(parts) == 2 {
  1132. val, err := strconv.ParseFloat(parts[1], 32)
  1133. if err != nil {
  1134. return nil, b.err(fname, fmt.Sprintf("Invalid float32 value:%s", parts[1]))
  1135. }
  1136. c4.A = float32(val)
  1137. }
  1138. return &c4, nil
  1139. }
  1140. // Accept 3 or 4 floats values
  1141. va, err := b.parseFloats(fname, field, 3, 4)
  1142. if err != nil {
  1143. return nil, err
  1144. }
  1145. if len(va) == 3 {
  1146. return &math32.Color4{va[0], va[1], va[2], 1}, nil
  1147. }
  1148. return &math32.Color4{va[0], va[1], va[2], va[3]}, nil
  1149. }
  1150. // parseIconNames parses a string with a list of icon names or codepoints and
  1151. // returns a string with the icons codepoints encoded in UTF8
  1152. func (b *Builder) parseIconNames(fname, field string) (string, error) {
  1153. text := ""
  1154. parts := strings.Fields(field)
  1155. for i := 0; i < len(parts); i++ {
  1156. cp, err := b.parseIconName(fname, parts[i])
  1157. if err != nil {
  1158. return "", err
  1159. }
  1160. text = text + string(cp)
  1161. }
  1162. return text, nil
  1163. }
  1164. // parseIconName parses a string with an icon name or codepoint in hex
  1165. // and returns the icon codepoints value and an error
  1166. func (b *Builder) parseIconName(fname, field string) (string, error) {
  1167. // Try name first
  1168. cp := icon.Codepoint(field)
  1169. if cp != "" {
  1170. return cp, nil
  1171. }
  1172. // Try to parse as hex value
  1173. cp2, err := strconv.ParseUint(field, 16, 32)
  1174. if err != nil {
  1175. return "", b.err(fname, fmt.Sprintf("Invalid icon codepoint value/name:%v", field))
  1176. }
  1177. return string(cp2), nil
  1178. }
  1179. // parseFloats parses a string with a list of floats with the specified size
  1180. // and returns a slice. The specified size is 0 any number of floats is allowed.
  1181. // The individual values can be separated by spaces or commas
  1182. func (b *Builder) parseFloats(fname, field string, min, max int) ([]float32, error) {
  1183. // Checks if field is empty
  1184. field = strings.Trim(field, " ")
  1185. if field == "" {
  1186. return nil, nil
  1187. }
  1188. // Separate individual fields
  1189. var parts []string
  1190. if strings.Index(field, ",") < 0 {
  1191. parts = strings.Fields(field)
  1192. } else {
  1193. parts = strings.Split(field, ",")
  1194. }
  1195. if len(parts) < min || len(parts) > max {
  1196. return nil, b.err(fname, "Invalid number of float32 values")
  1197. }
  1198. // Parse each field value and appends to slice
  1199. var values []float32
  1200. for i := 0; i < len(parts); i++ {
  1201. val, err := strconv.ParseFloat(strings.Trim(parts[i], " "), 32)
  1202. if err != nil {
  1203. return nil, b.err(fname, err.Error())
  1204. }
  1205. values = append(values, float32(val))
  1206. }
  1207. return values, nil
  1208. }
  1209. // err creates and returns an error for the current object, field name and with the specified message
  1210. func (b *Builder) err(fname, msg string) error {
  1211. return fmt.Errorf("Error in object:%s field:%s -> %s", b.objpath.path(), fname, msg)
  1212. }
  1213. // setupDescTree sets the types of all description tree elements to lower case and
  1214. // sets the items "parent" attribute pointing the respective parent description
  1215. func (b *Builder) setupDescTree(dp *descPanel) {
  1216. dp.Type = strings.ToLower(dp.Type)
  1217. if dp.Layout != nil {
  1218. dp.Layout.Type = strings.ToLower(dp.Layout.Type)
  1219. }
  1220. for i := 0; i < len(dp.Items); i++ {
  1221. dp.Items[i].parent = dp
  1222. b.setupDescTree(dp.Items[i])
  1223. }
  1224. // Special case for splitter
  1225. if dp.P0 != nil {
  1226. dp.P0.parent = dp
  1227. b.setupDescTree(dp.P0)
  1228. }
  1229. if dp.P1 != nil {
  1230. dp.P1.parent = dp
  1231. b.setupDescTree(dp.P1)
  1232. }
  1233. }
  1234. // Returns the width and height attributes of the specified descriptor
  1235. // if defined or the defaul value (0).
  1236. func (b *Builder) size(dp *descPanel) (float32, float32) {
  1237. var width, height float32
  1238. if dp.Width != nil {
  1239. width = *dp.Width
  1240. }
  1241. if dp.Height != nil {
  1242. height = *dp.Height
  1243. }
  1244. return width, height
  1245. }
  1246. // strStack is a stack of strings
  1247. type strStack struct {
  1248. stack []string
  1249. }
  1250. // clear removes all elements from the stack
  1251. func (ss *strStack) clear() {
  1252. ss.stack = []string{}
  1253. }
  1254. // push pushes a string to the top of the stack
  1255. func (ss *strStack) push(v string) {
  1256. ss.stack = append(ss.stack, v)
  1257. }
  1258. // pop removes and returns the string at the top of the stack.
  1259. // Returns an empty string if the stack is empty
  1260. func (ss *strStack) pop() string {
  1261. if len(ss.stack) == 0 {
  1262. return ""
  1263. }
  1264. length := len(ss.stack)
  1265. v := ss.stack[length-1]
  1266. ss.stack = ss.stack[:length-1]
  1267. return v
  1268. }
  1269. // path returns a string composed of all the strings in the
  1270. // stack separated by a forward slash.
  1271. func (ss *strStack) path() string {
  1272. return strings.Join(ss.stack, "/")
  1273. }