comparison android/DWindows.kt @ 2547:dbd15c13f5bb

Android: Implement most of the font functions and control/widget color. Add an Android section in dwtest for picking fonts and folders. Minor update to the readme.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 14 May 2021 11:29:00 +0000
parents 897d94c20365
children a8d90e2896bc
comparison
equal deleted inserted replaced
2546:897d94c20365 2547:dbd15c13f5bb
238 ) 238 )
239 } 239 }
240 240
241 class DWRender(context: Context) : View(context) { 241 class DWRender(context: Context) : View(context) {
242 var cachedCanvas: Canvas? = null 242 var cachedCanvas: Canvas? = null
243 var typeface: Typeface? = null
244 var fontsize: Float? = null
243 245
244 override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) { 246 override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) {
245 super.onSizeChanged(width, height, oldWidth, oldHeight) 247 super.onSizeChanged(width, height, oldWidth, oldHeight)
246 // Send DW_SIGNAL_CONFIGURE 248 // Send DW_SIGNAL_CONFIGURE
247 eventHandlerInt(1, width, height, 0, 0) 249 eventHandlerInt(1, width, height, 0, 0)
684 waitOnUiThread { 686 waitOnUiThread {
685 window.setEnabled(state) 687 window.setEnabled(state)
686 } 688 }
687 } 689 }
688 690
691 fun typefaceFromFontName(fontname: String?): Typeface?
692 {
693 if(fontname != null) {
694 val bold: Boolean = fontname.contains(" Bold")
695 val italic: Boolean = fontname.contains(" Italic")
696 val font = fontname.substringAfter('.')
697 var fontFamily = font
698 var typeface: Typeface? = null
699
700 if (bold && font != null) {
701 fontFamily = font.substringBefore(" Bold")
702 } else if (italic && font != null) {
703 fontFamily = font.substringBefore(" Italic")
704 }
705
706 if (fontFamily != null) {
707 var style: Int = Typeface.NORMAL
708 if (bold && italic) {
709 style = Typeface.BOLD_ITALIC
710 } else if (bold) {
711 style = Typeface.BOLD
712 } else if (italic) {
713 style = Typeface.ITALIC
714 }
715 typeface = Typeface.create(fontFamily, style)
716 }
717 return typeface
718 }
719 return Typeface.DEFAULT
720 }
721
722 fun windowSetFont(window: View, fontname: String?) {
723 var typeface: Typeface? = typefaceFromFontName(fontname)
724 var size: Float? = null
725
726 if(fontname != null) {
727 size = fontname.substringBefore('.').toFloatOrNull()
728 }
729
730 if(typeface != null) {
731 waitOnUiThread {
732 if (window is TextView) {
733 var textview: TextView = window
734 textview.typeface = typeface
735 if(size != null) {
736 textview.textSize = size
737 }
738 } else if (window is Button) {
739 var button: Button = window
740 button.typeface = typeface
741 if(size != null) {
742 button.textSize = size
743 }
744 } else if(window is DWRender) {
745 var render: DWRender = window
746 render.typeface = typeface
747 if(size != null) {
748 render.fontsize = size
749 }
750 }
751 }
752 }
753 }
754
755 fun windowSetColor(window: View, falpha: Int, fred: Int, fgreen: Int, fblue: Int,
756 balpha: Int, bred: Int, bgreen: Int, bblue: Int) {
757
758 waitOnUiThread {
759 if (window is TextView) {
760 var textview: TextView = window
761 textview.setTextColor(Color.rgb(fred, fgreen, fblue))
762 textview.setBackgroundColor(Color.rgb(bred, bgreen, bblue))
763 } else if (window is Button) {
764 var button: Button = window
765 button.setTextColor(Color.rgb(fred, fgreen, fblue))
766 button.setBackgroundColor(Color.rgb(bred, bgreen, bblue))
767 } else if(window is LinearLayout) {
768 var box: LinearLayout = window
769 box.setBackgroundColor(Color.rgb(bred, bgreen, bblue))
770 }
771 }
772 }
773
689 fun windowSetText(window: View, text: String) { 774 fun windowSetText(window: View, text: String) {
690 waitOnUiThread { 775 waitOnUiThread {
691 if (window is TextView) { 776 if (window is TextView) {
692 var textview: TextView = window 777 var textview: TextView = window
693 textview.text = text 778 textview.text = text
2093 canvas.drawLine(x1.toFloat(), y1.toFloat(), x2.toFloat(), y2.toFloat(), paint) 2178 canvas.drawLine(x1.toFloat(), y1.toFloat(), x2.toFloat(), y2.toFloat(), paint)
2094 } 2179 }
2095 } 2180 }
2096 } 2181 }
2097 2182
2098 fun drawText(render: DWRender?, bitmap: Bitmap?, x: Int, y: Int, text:String) 2183 fun fontTextExtentsGet(render: DWRender?, bitmap: Bitmap?, text:String, typeface: Typeface?, fontsize: Int, window: View?): Long
2184 {
2185 var dimensions: Long = 0
2186
2187 waitOnUiThread {
2188 var rect = Rect()
2189
2190 if (render != null) {
2191 if (render.typeface != null) {
2192 paint.typeface = render.typeface
2193 if (render.fontsize != null && render.fontsize!! > 0F) {
2194 paint.textSize = render.fontsize!!
2195 }
2196 }
2197 } else if (bitmap != null) {
2198 if (typeface != null) {
2199 paint.typeface = typeface
2200 if (fontsize > 0) {
2201 paint.textSize = fontsize.toFloat()
2202 }
2203 } else if (window != null && window is DWRender) {
2204 val secondary: DWRender = window as DWRender
2205
2206 if (secondary.typeface != null) {
2207 paint.typeface = secondary.typeface
2208 if (secondary.fontsize != null && secondary.fontsize!! > 0F) {
2209 paint.textSize = secondary.fontsize!!
2210 }
2211 }
2212 }
2213 }
2214 paint.getTextBounds(text, 0, text.length, rect)
2215 val textheight = rect.bottom - rect.top
2216 val textwidth = rect.right - rect.left
2217 dimensions = textwidth.toLong() or (textheight.toLong() shl 32)
2218 }
2219 return dimensions
2220 }
2221
2222 fun drawText(render: DWRender?, bitmap: Bitmap?, x: Int, y: Int, text:String, typeface: Typeface?, fontsize: Int, window: View?)
2099 { 2223 {
2100 waitOnUiThread { 2224 waitOnUiThread {
2101 var canvas: Canvas? = null 2225 var canvas: Canvas? = null
2102 2226
2103 if(render != null) { 2227 if(render != null && render.cachedCanvas != null) {
2104 canvas = render.cachedCanvas 2228 canvas = render.cachedCanvas
2229 if(render.typeface != null) {
2230 paint.typeface = render.typeface
2231 if(render.fontsize != null && render.fontsize!! > 0F) {
2232 paint.textSize = render.fontsize!!
2233 }
2234 }
2105 } else if(bitmap != null) { 2235 } else if(bitmap != null) {
2106 canvas = Canvas(bitmap) 2236 canvas = Canvas(bitmap)
2237 if(typeface != null) {
2238 paint.typeface = typeface
2239 if(fontsize > 0) {
2240 paint.textSize = fontsize.toFloat()
2241 }
2242 } else if(window != null && window is DWRender) {
2243 val secondary: DWRender = window as DWRender
2244
2245 if(secondary.typeface != null) {
2246 paint.typeface = secondary.typeface
2247 if(secondary.fontsize != null && secondary.fontsize!! > 0F) {
2248 paint.textSize = secondary.fontsize!!
2249 }
2250 }
2251 }
2107 } 2252 }
2108 2253
2109 if(canvas != null) { 2254 if(canvas != null) {
2110 // Save the old color for later... 2255 // Save the old color for later...
2111 var rect = Rect() 2256 var rect = Rect()