css.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package ultraviolet
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func ViewToString(view View) string {
  7. parts := []string{
  8. DisplayToString(view.Block.Display),
  9. PositionToString(view.Block.Position),
  10. FloatToString(view.Block.Float),
  11. WidthToString(view.Block.Width),
  12. HeightToString(view.Block.Height),
  13. BottomToString(view.Block.Bottom),
  14. LeftToString(view.Block.Left),
  15. TopToString(view.Block.Top),
  16. RightToString(view.Block.Right),
  17. MarginToString(view.Block.Margin),
  18. PaddingToString(view.Block.Padding),
  19. BorderThicknessToString(view.Block.Border),
  20. BorderRadiusToString(view.Block.Border),
  21. FontToString(view.Font),
  22. view.Custom,
  23. }
  24. switch view.Cursor {
  25. case CURSOR_POINTER:
  26. parts = append(parts, "cursor: pointer;")
  27. parts = append(parts, "user-select: none;")
  28. }
  29. return strings.Join(parts, "")
  30. }
  31. func DisplayToString(display int) string {
  32. switch display {
  33. case DISPLAY_BLOCK:
  34. return "display: block;"
  35. case DISPLAY_INLINE:
  36. return "display: inline-block;"
  37. }
  38. return ""
  39. }
  40. func PositionToString(position int) string {
  41. switch position {
  42. case POSITION_FIXED:
  43. return "position: fixed;"
  44. case POSITION_RELATIVE:
  45. return "position: relative;"
  46. case POSITION_ABSOLUTE:
  47. return "position: absolute;"
  48. }
  49. return ""
  50. }
  51. func FloatToString(float int) string {
  52. switch float {
  53. case FLOAT_LEFT:
  54. return "float: left;"
  55. case FLOAT_RIGHT:
  56. return "float: right;"
  57. case FLOAT_CLEAR:
  58. return "clear: both;"
  59. }
  60. return ""
  61. }
  62. func WidthToString(width int) string {
  63. if width == 0 {
  64. return ""
  65. }
  66. return fmt.Sprintf("width: %dpx;", width)
  67. }
  68. func HeightToString(height int) string {
  69. if height == 0 {
  70. return ""
  71. }
  72. return fmt.Sprintf("height: %dpx;", height)
  73. }
  74. func BottomToString(bottom int) string {
  75. if bottom == 0 {
  76. return ""
  77. }
  78. return fmt.Sprintf("bottom: %dpx;", bottom)
  79. }
  80. func LeftToString(left int) string {
  81. if left == 0 {
  82. return ""
  83. }
  84. return fmt.Sprintf("left: %dpx;", left)
  85. }
  86. func TopToString(top int) string {
  87. if top == 0 {
  88. return ""
  89. }
  90. return fmt.Sprintf("top: %dpx;", top)
  91. }
  92. func RightToString(right int) string {
  93. if right == 0 {
  94. return ""
  95. }
  96. return fmt.Sprintf("right: %dpx;", right)
  97. }
  98. func MarginToString(margin Margin) string {
  99. if margin.Top == 0 && margin.Right == 0 && margin.Bottom == 0 && margin.Left == 0 {
  100. return ""
  101. }
  102. return fmt.Sprintf(
  103. "margin: %dpx %dpx %dpx %dpx;",
  104. margin.Top,
  105. margin.Right,
  106. margin.Bottom,
  107. margin.Left,
  108. )
  109. }
  110. func PaddingToString(padding Padding) string {
  111. if padding.Top == 0 && padding.Right == 0 && padding.Bottom == 0 && padding.Left == 0 {
  112. return ""
  113. }
  114. return fmt.Sprintf(
  115. "padding: %dpx %dpx %dpx %dpx;",
  116. padding.Top,
  117. padding.Right,
  118. padding.Bottom,
  119. padding.Left,
  120. )
  121. }
  122. func BorderThicknessToString(border Border) string {
  123. color := ColorToString(border.Color)
  124. parts := []string{}
  125. if border.ThicknessTop != 0 {
  126. parts = append(
  127. parts,
  128. fmt.Sprintf("border-top: %dpx solid %s;", border.ThicknessTop, color),
  129. )
  130. }
  131. if border.ThicknessRight != 0 {
  132. parts = append(
  133. parts,
  134. fmt.Sprintf("border-right: %dpx solid %s;", border.ThicknessRight, color),
  135. )
  136. }
  137. if border.ThicknessBottom != 0 {
  138. parts = append(
  139. parts,
  140. fmt.Sprintf("border-bottom: %dpx solid %s;", border.ThicknessBottom, color),
  141. )
  142. }
  143. if border.ThicknessLeft != 0 {
  144. parts = append(
  145. parts,
  146. fmt.Sprintf("border-left: %dpx solid %s;", border.ThicknessLeft, color),
  147. )
  148. }
  149. return strings.Join(parts, "")
  150. }
  151. func BorderRadiusToString(border Border) string {
  152. if border.RadiusTopLeft == 0 && border.RadiusTopRight == 0 && border.RadiusBottomRight == 0 && border.RadiusBottomLeft == 0 {
  153. return ""
  154. }
  155. return fmt.Sprintf(
  156. "border-radius: %dpx %dpx %dpx %dpx;",
  157. border.RadiusTopLeft,
  158. border.RadiusTopRight,
  159. border.RadiusBottomRight,
  160. border.RadiusBottomLeft,
  161. )
  162. }
  163. func ColorToString(color Color) string {
  164. if color.Alpha == 0 {
  165. return ""
  166. }
  167. return fmt.Sprintf(
  168. "rgba(%d, %d, %d, %.2f)",
  169. color.Red,
  170. color.Green,
  171. color.Blue,
  172. float64(color.Alpha)/255,
  173. )
  174. }
  175. func FontToString(font Font) string {
  176. parts := []string{}
  177. if font.Face != "" {
  178. parts = append(
  179. parts,
  180. fmt.Sprintf("font-face: '%s';", font.Face),
  181. )
  182. }
  183. if font.Size != 0 {
  184. parts = append(
  185. parts,
  186. fmt.Sprintf("font-size: %dpx;", font.Size),
  187. )
  188. }
  189. if font.Bold {
  190. parts = append(
  191. parts,
  192. "font-weight: bold;",
  193. )
  194. }
  195. if font.Italic {
  196. parts = append(
  197. parts,
  198. "font-view: italic;",
  199. )
  200. }
  201. switch font.Decoration {
  202. case DECORATION_UNDERLINE:
  203. parts = append(
  204. parts,
  205. "text-decoration: underline;",
  206. )
  207. }
  208. fontColor := ColorToString(font.FontColor)
  209. if fontColor != "" {
  210. parts = append(
  211. parts,
  212. fmt.Sprintf("color: %s;", ColorToString(font.FontColor)),
  213. )
  214. }
  215. backgroundColor := ColorToString(font.BackgroundColor)
  216. if backgroundColor != "" {
  217. parts = append(
  218. parts,
  219. fmt.Sprintf("background-color: %s;", ColorToString(font.BackgroundColor)),
  220. )
  221. }
  222. return strings.Join(parts, "")
  223. }