Skip to content

Commit a321183

Browse files
committed
Kotlin Paint functions
1 parent d857cf7 commit a321183

File tree

9 files changed

+92
-99
lines changed

9 files changed

+92
-99
lines changed

chartLib/src/main/kotlin/info/appdev/charting/components/Legend.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import android.graphics.DashPathEffect
44
import android.graphics.Paint
55
import info.appdev.charting.utils.ColorTemplate
66
import info.appdev.charting.utils.FSize
7-
import info.appdev.charting.utils.Utils
87
import info.appdev.charting.utils.ViewPortHandler
98
import info.appdev.charting.utils.calcTextHeight
9+
import info.appdev.charting.utils.calcTextWidth
1010
import info.appdev.charting.utils.convertDpToPixel
11+
import info.appdev.charting.utils.calcTextSize
12+
import info.appdev.charting.utils.getLineHeight
13+
import info.appdev.charting.utils.getLineSpacing
1114
import java.lang.Float
1215
import kotlin.Array
1316
import kotlin.Boolean
@@ -204,7 +207,7 @@ class Legend() : ComponentBase() {
204207

205208
val label = entry.label ?: continue
206209

207-
val length = Utils.calcTextWidth(p, label).toFloat()
210+
val length = p.calcTextWidth(label).toFloat()
208211

209212
if (length > max) max = length
210213
}
@@ -376,7 +379,7 @@ class Legend() : ComponentBase() {
376379
var maxWidth = 0f
377380
var maxHeight = 0f
378381
var width = 0f
379-
val labelLineHeight = Utils.getLineHeight(labelpaint)
382+
val labelLineHeight = labelpaint.getLineHeight()
380383
var wasStacked = false
381384

382385
var i = 0
@@ -408,7 +411,7 @@ class Legend() : ComponentBase() {
408411
wasStacked = false
409412
}
410413

411-
width += Utils.calcTextWidth(labelpaint, label).toFloat()
414+
width += labelpaint.calcTextWidth(label).toFloat()
412415

413416
maxHeight += labelLineHeight + yEntrySpace
414417
} else {
@@ -426,8 +429,8 @@ class Legend() : ComponentBase() {
426429
}
427430

428431
LegendOrientation.HORIZONTAL -> {
429-
val labelLineHeight = Utils.getLineHeight(labelpaint)
430-
val labelLineSpacing = Utils.getLineSpacing(labelpaint) + yEntrySpace
432+
val labelLineHeight = labelpaint.getLineHeight()
433+
val labelLineSpacing = labelpaint.getLineSpacing() + yEntrySpace
431434
val contentWidth = viewPortHandler.contentWidth() * this.maxSizePercent
432435

433436
// Start calculating layout
@@ -463,7 +466,7 @@ class Legend() : ComponentBase() {
463466

464467
// grouped forms have null labels
465468
if (label != null) {
466-
calculatedLabelSizes.add(Utils.calcTextSize(labelpaint, label))
469+
calculatedLabelSizes.add(labelpaint.calcTextSize(label))
467470
requiredWidth += if (drawingForm) formToTextSpace + formSize else 0f
468471
requiredWidth += calculatedLabelSizes.get(i)!!.width
469472
} else {

chartLib/src/main/kotlin/info/appdev/charting/components/YAxis.kt

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package info.appdev.charting.components
22

33
import android.graphics.Color
44
import android.graphics.Paint
5-
import info.appdev.charting.utils.Utils
65
import info.appdev.charting.utils.calcTextHeight
6+
import info.appdev.charting.utils.calcTextWidth
77
import info.appdev.charting.utils.convertDpToPixel
88
import kotlin.math.abs
99
import kotlin.math.max
@@ -52,30 +52,18 @@ class YAxis : AxisBase {
5252
var isDrawZeroLineEnabled: Boolean = false
5353
protected set
5454

55-
/**
56-
* Returns true if autoscale restriction for axis min value is enabled
57-
*/
58-
/**
59-
* Sets autoscale restriction for axis min value as enabled/disabled
60-
*/
6155
/**
6256
* flag indicating that auto scale min restriction should be used
6357
*/
64-
@get:Deprecated("")
65-
@set:Deprecated("")
58+
@get:Deprecated("flag indicating that auto scale min restriction should be used")
59+
@set:Deprecated("flag indicating that auto scale min restriction should be used")
6660
var isUseAutoScaleMinRestriction: Boolean = false
6761

68-
/**
69-
* Returns true if autoscale restriction for axis max value is enabled
70-
*/
71-
/**
72-
* Sets autoscale restriction for axis max value as enabled/disabled
73-
*/
7462
/**
7563
* flag indicating that auto scale max restriction should be used
7664
*/
77-
@get:Deprecated("")
78-
@set:Deprecated("")
65+
@get:Deprecated("flag indicating that auto scale max restriction should be used")
66+
@set:Deprecated("flag indicating that auto scale max restriction should be used")
7967
var isUseAutoScaleMaxRestriction: Boolean = false
8068

8169
/**
@@ -240,7 +228,7 @@ class YAxis : AxisBase {
240228
p.textSize = mTextSize
241229

242230
val label = getLongestLabel(p)
243-
var width = Utils.calcTextWidth(p, label).toFloat() + xOffset * 2f
231+
var width = p.calcTextWidth(label).toFloat() + xOffset * 2f
244232

245233
var minWidth = this.minWidth
246234
var maxWidth = this.maxWidth

chartLib/src/main/kotlin/info/appdev/charting/renderer/HorizontalBarChartRenderer.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import info.appdev.charting.utils.Transformer
1515
import info.appdev.charting.utils.Utils
1616
import info.appdev.charting.utils.ViewPortHandler
1717
import info.appdev.charting.utils.calcTextHeight
18+
import info.appdev.charting.utils.calcTextWidth
1819
import info.appdev.charting.utils.convertDpToPixel
1920
import kotlin.math.ceil
2021
import kotlin.math.min
@@ -225,7 +226,7 @@ open class HorizontalBarChartRenderer(
225226
val valueY = barEntry.y
226227
val formattedValue = formatter.getFormattedValue(valueY, barEntry, i, viewPortHandler)
227228
// calculate the correct offset depending on the draw position of the value
228-
val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat()
229+
val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat()
229230
posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus))
230231
negOffset = ((if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus)
231232
- (buffer.buffer[j + 2] - buffer.buffer[j]))
@@ -302,7 +303,7 @@ open class HorizontalBarChartRenderer(
302303
)
303304

304305
// calculate the correct offset depending on the draw position of the value
305-
val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat()
306+
val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat()
306307
posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus))
307308
negOffset = (if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus)
308309

@@ -380,7 +381,7 @@ open class HorizontalBarChartRenderer(
380381
)
381382

382383
// calculate the correct offset depending on the draw position of the value
383-
val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat()
384+
val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat()
384385
posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus))
385386
negOffset = (if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus)
386387

chartLib/src/main/kotlin/info/appdev/charting/renderer/LegendRenderer.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import info.appdev.charting.interfaces.datasets.IBarDataSet
1717
import info.appdev.charting.interfaces.datasets.ICandleDataSet
1818
import info.appdev.charting.interfaces.datasets.IPieDataSet
1919
import info.appdev.charting.utils.ColorTemplate
20-
import info.appdev.charting.utils.Utils
2120
import info.appdev.charting.utils.ViewPortHandler
2221
import info.appdev.charting.utils.calcTextHeight
22+
import info.appdev.charting.utils.calcTextWidth
2323
import info.appdev.charting.utils.convertDpToPixel
24+
import info.appdev.charting.utils.getLineHeight
25+
import info.appdev.charting.utils.getLineSpacing
2426
import java.util.Collections
2527
import kotlin.math.min
2628

@@ -207,8 +209,8 @@ open class LegendRenderer(
207209
labelPaint.textSize = legend.textSize
208210
labelPaint.color = legend.textColor
209211

210-
val labelLineHeight = Utils.getLineHeight(labelPaint, legendFontMetrics)
211-
val labelLineSpacing = (Utils.getLineSpacing(labelPaint, legendFontMetrics)
212+
val labelLineHeight = labelPaint.getLineHeight(legendFontMetrics)
213+
val labelLineSpacing = (labelPaint.getLineSpacing(legendFontMetrics)
212214
+ legend.yEntrySpace.convertDpToPixel())
213215
val formYOffset = labelLineHeight - labelPaint.calcTextHeight("ABC") / 2f
214216

@@ -391,7 +393,7 @@ open class LegendRenderer(
391393
-formToTextSpace
392394
else if (wasStacked) posX = originPosX
393395

394-
if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= Utils.calcTextWidth(labelPaint, entry.label).toFloat()
396+
if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= labelPaint.calcTextWidth(entry.label).toFloat()
395397

396398
if (!wasStacked) {
397399
drawLabel(canvas, posX, posY + labelLineHeight, entry.label)

chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRenderer.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import info.appdev.charting.utils.Transformer
1919
import info.appdev.charting.utils.Utils
2020
import info.appdev.charting.utils.ViewPortHandler
2121
import info.appdev.charting.utils.calcTextHeight
22+
import info.appdev.charting.utils.calcTextSize
23+
import info.appdev.charting.utils.calcTextWidth
2224
import info.appdev.charting.utils.convertDpToPixel
2325
import info.appdev.charting.utils.drawXAxisValue
2426
import kotlin.math.roundToInt
@@ -71,7 +73,7 @@ open class XAxisRenderer(
7173
paintAxisLabels.typeface = xAxis.typeface
7274
paintAxisLabels.textSize = xAxis.textSize
7375

74-
val labelSize = Utils.calcTextSize(paintAxisLabels, longest)
76+
val labelSize = paintAxisLabels.calcTextSize(longest)
7577

7678
val labelWidth = labelSize.width
7779
val labelHeight = paintAxisLabels.calcTextHeight("Q").toFloat()
@@ -208,15 +210,15 @@ open class XAxisRenderer(
208210
// avoid clipping of the last
209211

210212
if (i / 2 == xAxis.entryCount - 1 && xAxis.entryCount > 1) {
211-
val width = Utils.calcTextWidth(paintAxisLabels, label).toFloat()
213+
val width = paintAxisLabels.calcTextWidth(label).toFloat()
212214

213215
if (width > viewPortHandler.offsetRight() * 2
214216
&& x + width > viewPortHandler.chartWidth
215217
) x -= width / 2
216218

217219
// avoid clipping of the first
218220
} else if (i == 0) {
219-
val width = Utils.calcTextWidth(paintAxisLabels, label).toFloat()
221+
val width = paintAxisLabels.calcTextWidth(label).toFloat()
220222
x += width / 2
221223
}
222224
}

chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRendererHorizontalBarChart.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import info.appdev.charting.utils.Transformer
1616
import info.appdev.charting.utils.Utils
1717
import info.appdev.charting.utils.ViewPortHandler
1818
import info.appdev.charting.utils.calcTextHeight
19+
import info.appdev.charting.utils.calcTextSize
1920
import info.appdev.charting.utils.convertDpToPixel
2021
import kotlin.math.roundToInt
2122

@@ -58,7 +59,7 @@ open class XAxisRendererHorizontalBarChart(
5859

5960
val longest = xAxis.longestLabel
6061

61-
val labelSize = Utils.calcTextSize(paintAxisLabels, longest)
62+
val labelSize = paintAxisLabels.calcTextSize(longest)
6263

6364
val labelWidth = (labelSize.width + xAxis.xOffset * 3.5f).toInt().toFloat()
6465
val labelHeight = labelSize.height

chartLib/src/main/kotlin/info/appdev/charting/utils/CanvasUtils.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ fun Canvas.drawXAxisValue(
113113
paint.textAlign = originalTextAlign
114114
}
115115

116+
/**
117+
* calculates the approximate width of a text, depending on a demo text
118+
* avoid repeated calls (e.g. inside drawing methods)
119+
*/
120+
fun Paint.calcTextWidth(demoText: String?): Int {
121+
return measureText(demoText).toInt()
122+
}
123+
116124
/**
117125
* Returns a recyclable FSize instance.
118126
* Represents size of a rotated rectangle by degrees.

chartLib/src/main/kotlin/info/appdev/charting/utils/Utils.kt

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package info.appdev.charting.utils
22

33
import android.content.Context
44
import android.graphics.Canvas
5-
import android.graphics.Paint
65
import android.graphics.Rect
76
import android.graphics.drawable.Drawable
87
import android.view.MotionEvent
@@ -14,7 +13,6 @@ import info.appdev.charting.utils.MPPointF.Companion.instance
1413
import java.lang.Float
1514
import kotlin.Int
1615
import kotlin.IntArray
17-
import kotlin.String
1816
import kotlin.Suppress
1917
import kotlin.intArrayOf
2018
import kotlin.math.abs
@@ -44,67 +42,6 @@ object Utils {
4442
maximumFlingVelocity = viewConfiguration.scaledMaximumFlingVelocity
4543
}
4644

47-
/**
48-
* calculates the approximate width of a text, depending on a demo text
49-
* avoid repeated calls (e.g. inside drawing methods)
50-
*/
51-
fun calcTextWidth(paint: Paint, demoText: String?): Int {
52-
return paint.measureText(demoText).toInt()
53-
}
54-
55-
private val mFontMetrics = Paint.FontMetrics()
56-
57-
fun getLineHeight(paint: Paint): kotlin.Float {
58-
return getLineHeight(paint, mFontMetrics)
59-
}
60-
61-
fun getLineHeight(paint: Paint, fontMetrics: Paint.FontMetrics): kotlin.Float {
62-
paint.getFontMetrics(fontMetrics)
63-
return fontMetrics.descent - fontMetrics.ascent
64-
}
65-
66-
fun getLineSpacing(paint: Paint): kotlin.Float {
67-
return getLineSpacing(paint, mFontMetrics)
68-
}
69-
70-
fun getLineSpacing(paint: Paint, fontMetrics: Paint.FontMetrics): kotlin.Float {
71-
paint.getFontMetrics(fontMetrics)
72-
return fontMetrics.ascent - fontMetrics.top + fontMetrics.bottom
73-
}
74-
75-
/**
76-
* Returns a recyclable FSize instance.
77-
* calculates the approximate size of a text, depending on a demo text
78-
* avoid repeated calls (e.g. inside drawing methods)
79-
*
80-
* @param paint
81-
* @param demoText
82-
* @return A Recyclable FSize instance
83-
*/
84-
fun calcTextSize(paint: Paint, demoText: String): FSize {
85-
val result = FSize.getInstance(0f, 0f)
86-
calcTextSize(paint, demoText, result)
87-
return result
88-
}
89-
90-
private val mCalcTextSizeRect = Rect()
91-
92-
/**
93-
* calculates the approximate size of a text, depending on a demo text
94-
* avoid repeated calls (e.g. inside drawing methods)
95-
*
96-
* @param paint
97-
* @param demoText
98-
* @param outputFSize An output variable, modified by the function.
99-
*/
100-
fun calcTextSize(paint: Paint, demoText: String, outputFSize: FSize) {
101-
val r = mCalcTextSizeRect
102-
r.set(0, 0, 0, 0)
103-
paint.getTextBounds(demoText, 0, demoText.length, r)
104-
outputFSize.width = r.width().toFloat()
105-
outputFSize.height = r.height().toFloat()
106-
}
107-
10845
/**
10946
* Math.pow(...) is very expensive, so avoid calling it and create it
11047
* yourself.

chartLib/src/main/kotlin/info/appdev/charting/utils/UtilsKt.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,54 @@ fun Paint.calcTextHeight(demoText: String): Int {
168168
this.getTextBounds(demoText, 0, demoText.length, r)
169169
return r.height()
170170
}
171+
172+
private val mFontMetrics = Paint.FontMetrics()
173+
174+
fun Paint.getLineHeight(): kotlin.Float {
175+
return this.getLineHeight(mFontMetrics)
176+
}
177+
178+
fun Paint.getLineHeight(fontMetrics: Paint.FontMetrics): kotlin.Float {
179+
this.getFontMetrics(fontMetrics)
180+
return fontMetrics.descent - fontMetrics.ascent
181+
}
182+
183+
fun Paint.getLineSpacing(): kotlin.Float {
184+
return this.getLineSpacing(mFontMetrics)
185+
}
186+
187+
fun Paint.getLineSpacing(fontMetrics: Paint.FontMetrics): kotlin.Float {
188+
this.getFontMetrics(fontMetrics)
189+
return fontMetrics.ascent - fontMetrics.top + fontMetrics.bottom
190+
}
191+
192+
/**
193+
* Returns a recyclable FSize instance.
194+
* calculates the approximate size of a text, depending on a demo text
195+
* avoid repeated calls (e.g. inside drawing methods)
196+
*
197+
* @param paint
198+
* @param demoText
199+
* @return A Recyclable FSize instance
200+
*/
201+
fun Paint.calcTextSize(demoText: String): FSize {
202+
val result = FSize.getInstance(0f, 0f)
203+
calcTextSize(demoText, result)
204+
return result
205+
}
206+
207+
private val mCalcTextSizeRect = Rect()
208+
209+
/**
210+
* calculates the approximate size of a text, depending on a demo text
211+
* avoid repeated calls (e.g. inside drawing methods)
212+
*
213+
* @param outputFSize An output variable, modified by the function.
214+
*/
215+
fun Paint.calcTextSize(demoText: String, outputFSize: FSize) {
216+
val r = mCalcTextSizeRect
217+
r.set(0, 0, 0, 0)
218+
this.getTextBounds(demoText, 0, demoText.length, r)
219+
outputFSize.width = r.width().toFloat()
220+
outputFSize.height = r.height().toFloat()
221+
}

0 commit comments

Comments
 (0)