47_Typography.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. -- Text rendering example.
  2. -- Displays text at various sizes, with checkboxes to change the rendering parameters.
  3. require "LuaScripts/Utilities/Sample"
  4. -- Tag used to find all Text elements
  5. local TEXT_TAG = "Typography_text_tag"
  6. -- Top-level container for this sample's UI
  7. local uielement = nil
  8. function Start()
  9. -- Execute the common startup for samples
  10. SampleStart()
  11. -- Enable OS cursor
  12. input.mouseVisible = true
  13. -- Load XML file containing default UI style sheet
  14. local style = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  15. -- Set the loaded style as default style
  16. ui.root.defaultStyle = style
  17. -- Create a UIElement to hold all our content
  18. -- (Don't modify the root directly, as the base Sample class uses it)
  19. uielement = UIElement:new()
  20. uielement:SetAlignment(HA_CENTER, VA_CENTER)
  21. uielement:SetLayout(LM_VERTICAL, 10, IntRect(20, 40, 20, 40))
  22. ui.root:AddChild(uielement)
  23. -- Add some sample text
  24. CreateText()
  25. -- Add a checkbox to toggle the background color.
  26. CreateCheckbox("White background", "HandleWhiteBackground")
  27. :SetChecked(false)
  28. -- Add a checkbox to toggle SRGB output conversion (if available).
  29. -- This will give more correct text output for FreeType fonts, as the FreeType rasterizer
  30. -- outputs linear coverage values rather than SRGB values. However, this feature isn't
  31. -- available on all platforms.
  32. CreateCheckbox("Graphics::SetSRGB", "HandleSRGB")
  33. :SetChecked(graphics:GetSRGB())
  34. -- Add a checkbox for the global ForceAutoHint setting. This affects character spacing.
  35. CreateCheckbox("UI::SetForceAutoHint", "HandleForceAutoHint")
  36. :SetChecked(ui:GetForceAutoHint())
  37. -- Add a drop-down menu to control the font hinting level.
  38. local levels = {
  39. "FONT_HINT_LEVEL_NONE",
  40. "FONT_HINT_LEVEL_LIGHT",
  41. "FONT_HINT_LEVEL_NORMAL"
  42. }
  43. CreateMenu("UI::SetFontHintLevel", levels, "HandleFontHintLevel")
  44. :SetSelection(ui:GetFontHintLevel())
  45. -- Add a drop-down menu to control the subpixel threshold.
  46. local thresholds = {
  47. "0",
  48. "3",
  49. "6",
  50. "9",
  51. "12",
  52. "15",
  53. "18",
  54. "21"
  55. }
  56. CreateMenu("UI::SetFontSubpixelThreshold", thresholds, "HandleFontSubpixel")
  57. :SetSelection(ui:GetFontSubpixelThreshold() / 3)
  58. -- Add a drop-down menu to control oversampling.
  59. local limits = {
  60. "1",
  61. "2",
  62. "3",
  63. "4",
  64. "5",
  65. "6",
  66. "7",
  67. "8"
  68. }
  69. CreateMenu("UI::SetFontOversampling", limits, "HandleFontOversampling")
  70. :SetSelection(ui:GetFontOversampling() - 1)
  71. -- Set the mouse mode to use in the sample
  72. SampleInitMouseMode(MM_FREE)
  73. end
  74. function CreateText()
  75. local container = UIElement:new()
  76. container:SetAlignment(HA_LEFT, VA_TOP)
  77. container:SetLayout(LM_VERTICAL)
  78. uielement:AddChild(container)
  79. local font = cache:GetResource("Font", "Fonts/BlueHighway.ttf")
  80. for size = 1, 18, 0.5 do
  81. local text = Text:new()
  82. text.text = "The quick brown fox jumps over the lazy dog (" .. size .. "pt)"
  83. text:SetFont(font, size)
  84. text:AddTag(TEXT_TAG)
  85. container:AddChild(text)
  86. end
  87. end
  88. function CreateCheckbox(label, handler)
  89. local container = UIElement:new()
  90. container:SetAlignment(HA_LEFT, VA_TOP)
  91. container:SetLayout(LM_HORIZONTAL, 8)
  92. uielement:AddChild(container)
  93. local box = CheckBox:new()
  94. container:AddChild(box)
  95. box:SetStyleAuto()
  96. local text = Text:new()
  97. container:AddChild(text)
  98. text.text = label
  99. text:SetStyleAuto()
  100. text:AddTag(TEXT_TAG)
  101. SubscribeToEvent(box, "Toggled", handler)
  102. return box
  103. end
  104. function CreateMenu(label, items, handler)
  105. local container = UIElement:new()
  106. container:SetAlignment(HA_LEFT, VA_TOP)
  107. container:SetLayout(LM_HORIZONTAL, 8)
  108. uielement:AddChild(container)
  109. local text = Text:new()
  110. container:AddChild(text)
  111. text.text = label
  112. text:SetStyleAuto()
  113. text:AddTag(TEXT_TAG)
  114. local list = DropDownList:new()
  115. container:AddChild(list)
  116. list:SetStyleAuto()
  117. for i, item in ipairs(items) do
  118. local t = Text:new()
  119. list:AddItem(t)
  120. t.text = item
  121. t:SetStyleAuto()
  122. t:SetMinWidth(t:GetRowWidth(0) + 10)
  123. t:AddTag(TEXT_TAG)
  124. end
  125. text:SetMaxWidth(text:GetRowWidth(0))
  126. SubscribeToEvent(list, "ItemSelected", handler)
  127. return list
  128. end
  129. function HandleWhiteBackground(eventType, eventData)
  130. local box = eventData["Element"]:GetPtr("CheckBox")
  131. local checked = box:IsChecked()
  132. local fg = checked and Color.BLACK or Color.WHITE
  133. local bg = checked and Color.WHITE or Color.BLACK
  134. renderer.defaultZone.fogColor = bg
  135. local elements = uielement:GetChildrenWithTag(TEXT_TAG, true)
  136. for i, element in ipairs(elements) do
  137. element.color = fg
  138. end
  139. end
  140. function HandleForceAutoHint(eventType, eventData)
  141. local box = eventData["Element"]:GetPtr("CheckBox")
  142. local checked = box:IsChecked()
  143. ui:SetForceAutoHint(checked)
  144. end
  145. function HandleSRGB(eventType, eventData)
  146. local box = eventData["Element"]:GetPtr("CheckBox")
  147. local checked = box:IsChecked()
  148. if graphics:GetSRGBWriteSupport() then
  149. graphics:SetSRGB(checked)
  150. else
  151. log:Write(LOG_WARNING, "graphics:GetSRGBWriteSupport returned false")
  152. end
  153. end
  154. function HandleFontHintLevel(eventType, eventData)
  155. local list = eventData["Element"]:GetPtr("DropDownList")
  156. local i = list:GetSelection()
  157. ui:SetFontHintLevel(i)
  158. end
  159. function HandleFontSubpixel(eventType, eventData)
  160. local list = eventData["Element"]:GetPtr("DropDownList")
  161. local i = list:GetSelection()
  162. ui:SetFontSubpixelThreshold(i * 3)
  163. end
  164. function HandleFontOversampling(eventType, eventData)
  165. local list = eventData["Element"]:GetPtr("DropDownList")
  166. local i = list:GetSelection()
  167. ui:SetFontOversampling(i + 1)
  168. end
  169. -- Create XML patch instructions for screen joystick layout specific to this sample app
  170. function GetScreenJoystickPatchString()
  171. return
  172. "<patch>" ..
  173. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
  174. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  175. " </add>" ..
  176. "</patch>"
  177. end