diff 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
line wrap: on
line diff
--- a/android/DWindows.kt	Thu May 13 22:52:11 2021 +0000
+++ b/android/DWindows.kt	Fri May 14 11:29:00 2021 +0000
@@ -240,6 +240,8 @@
 
 class DWRender(context: Context) : View(context) {
     var cachedCanvas: Canvas? = null
+    var typeface: Typeface? = null
+    var fontsize: Float? = null
 
     override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) {
         super.onSizeChanged(width, height, oldWidth, oldHeight)
@@ -686,6 +688,89 @@
         }
     }
 
+    fun typefaceFromFontName(fontname: String?): Typeface?
+    {
+        if(fontname != null) {
+            val bold: Boolean = fontname.contains(" Bold")
+            val italic: Boolean = fontname.contains(" Italic")
+            val font = fontname.substringAfter('.')
+            var fontFamily = font
+            var typeface: Typeface? = null
+
+            if (bold && font != null) {
+                fontFamily = font.substringBefore(" Bold")
+            } else if (italic && font != null) {
+                fontFamily = font.substringBefore(" Italic")
+            }
+
+            if (fontFamily != null) {
+                var style: Int = Typeface.NORMAL
+                if (bold && italic) {
+                    style = Typeface.BOLD_ITALIC
+                } else if (bold) {
+                    style = Typeface.BOLD
+                } else if (italic) {
+                    style = Typeface.ITALIC
+                }
+                typeface = Typeface.create(fontFamily, style)
+            }
+            return typeface
+        }
+        return Typeface.DEFAULT
+    }
+
+    fun windowSetFont(window: View, fontname: String?) {
+        var typeface: Typeface? = typefaceFromFontName(fontname)
+        var size: Float? = null
+
+        if(fontname != null) {
+            size = fontname.substringBefore('.').toFloatOrNull()
+        }
+
+        if(typeface != null) {
+            waitOnUiThread {
+                if (window is TextView) {
+                    var textview: TextView = window
+                    textview.typeface = typeface
+                    if(size != null) {
+                        textview.textSize = size
+                    }
+                } else if (window is Button) {
+                    var button: Button = window
+                    button.typeface = typeface
+                    if(size != null) {
+                        button.textSize = size
+                    }
+                } else if(window is DWRender) {
+                    var render: DWRender = window
+                    render.typeface = typeface
+                    if(size != null) {
+                        render.fontsize = size
+                    }
+                }
+            }
+        }
+    }
+
+    fun windowSetColor(window: View, falpha: Int, fred: Int, fgreen: Int, fblue: Int,
+                       balpha: Int, bred: Int, bgreen: Int, bblue: Int) {
+
+        waitOnUiThread {
+            if (window is TextView) {
+                var textview: TextView = window
+                textview.setTextColor(Color.rgb(fred, fgreen, fblue))
+                textview.setBackgroundColor(Color.rgb(bred, bgreen, bblue))
+            } else if (window is Button) {
+                var button: Button = window
+                button.setTextColor(Color.rgb(fred, fgreen, fblue))
+                button.setBackgroundColor(Color.rgb(bred, bgreen, bblue))
+            } else if(window is LinearLayout) {
+                var box: LinearLayout = window
+                box.setBackgroundColor(Color.rgb(bred, bgreen, bblue))
+            }
+        }
+    }
+
     fun windowSetText(window: View, text: String) {
         waitOnUiThread {
             if (window is TextView) {
@@ -2095,15 +2180,75 @@
         }
     }
 
-    fun drawText(render: DWRender?, bitmap: Bitmap?, x: Int, y: Int, text:String)
+    fun fontTextExtentsGet(render: DWRender?, bitmap: Bitmap?, text:String, typeface: Typeface?, fontsize: Int, window: View?): Long
+    {
+        var dimensions: Long = 0
+
+        waitOnUiThread {
+            var rect = Rect()
+
+            if (render != null) {
+                if (render.typeface != null) {
+                    paint.typeface = render.typeface
+                    if (render.fontsize != null && render.fontsize!! > 0F) {
+                        paint.textSize = render.fontsize!!
+                    }
+                }
+            } else if (bitmap != null) {
+                if (typeface != null) {
+                    paint.typeface = typeface
+                    if (fontsize > 0) {
+                        paint.textSize = fontsize.toFloat()
+                    }
+                } else if (window != null && window is DWRender) {
+                    val secondary: DWRender = window as DWRender
+
+                    if (secondary.typeface != null) {
+                        paint.typeface = secondary.typeface
+                        if (secondary.fontsize != null && secondary.fontsize!! > 0F) {
+                            paint.textSize = secondary.fontsize!!
+                        }
+                    }
+                }
+            }
+            paint.getTextBounds(text, 0, text.length, rect)
+            val textheight = rect.bottom - rect.top
+            val textwidth = rect.right - rect.left
+            dimensions = textwidth.toLong() or (textheight.toLong() shl 32)
+        }
+        return dimensions
+    }
+
+    fun drawText(render: DWRender?, bitmap: Bitmap?, x: Int, y: Int, text:String, typeface: Typeface?, fontsize: Int, window: View?)
     {
         waitOnUiThread {
             var canvas: Canvas? = null
 
-            if(render != null) {
+            if(render != null && render.cachedCanvas != null) {
                 canvas = render.cachedCanvas
+                if(render.typeface != null) {
+                    paint.typeface = render.typeface
+                    if(render.fontsize != null && render.fontsize!! > 0F) {
+                        paint.textSize = render.fontsize!!
+                    }
+                }
             } else if(bitmap != null) {
                 canvas = Canvas(bitmap)
+                if(typeface != null) {
+                    paint.typeface = typeface
+                    if(fontsize > 0) {
+                        paint.textSize = fontsize.toFloat()
+                    }
+                } else if(window != null && window is DWRender) {
+                    val secondary: DWRender = window as DWRender
+
+                    if(secondary.typeface != null) {
+                        paint.typeface = secondary.typeface
+                        if(secondary.fontsize != null && secondary.fontsize!! > 0F) {
+                            paint.textSize = secondary.fontsize!!
+                        }
+                    }
+                }
             }
 
             if(canvas != null) {