1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.compose.ui.unit
18
19 import androidx.compose.runtime.Immutable
20 import androidx.compose.runtime.Stable
21 import androidx.compose.ui.geometry.Rect
22 import androidx.compose.ui.geometry.Size
23 import androidx.compose.ui.geometry.isSpecified
24 import androidx.compose.ui.unit.internal.JvmDefaultWithCompatibility
25 import androidx.compose.ui.util.fastRoundToInt
26
27 /**
28 * A density of the screen. Used for convert [Dp] to pixels.
29 *
30 * @param density The logical density of the display. This is a scaling factor for the [Dp] unit.
31 * @param fontScale Current user preference for the scaling factor for fonts.
32 */
33 @Stable
Densitynull34 fun Density(density: Float, fontScale: Float = 1f): Density = DensityImpl(density, fontScale)
35
36 private data class DensityImpl(override val density: Float, override val fontScale: Float) :
37 Density
38
39 /**
40 * A density of the screen. Used for the conversions between pixels, [Dp], [Int] and [TextUnit].
41 *
42 * @sample androidx.compose.ui.unit.samples.WithDensitySample
43 */
44 @Immutable
45 @JvmDefaultWithCompatibility
46 interface Density : FontScaling {
47
48 /** The logical density of the display. This is a scaling factor for the [Dp] unit. */
49 @Stable val density: Float
50
51 /** Convert [Dp] to pixels. Pixels are used to paint to Canvas. */
52 @Stable fun Dp.toPx(): Float = value * density
53
54 /** Convert [Dp] to [Int] by rounding */
55 @Stable
56 fun Dp.roundToPx(): Int {
57 val px = toPx()
58 return if (px.isInfinite()) Constraints.Infinity else px.fastRoundToInt()
59 }
60
61 /**
62 * Convert Sp to pixels. Pixels are used to paint to Canvas.
63 *
64 * @throws IllegalStateException if TextUnit other than SP unit is specified.
65 */
66 @Stable
67 fun TextUnit.toPx(): Float {
68 checkPrecondition(type == TextUnitType.Sp) { "Only Sp can convert to Px" }
69 return toDp().toPx()
70 }
71
72 /** Convert Sp to [Int] by rounding */
73 @Stable fun TextUnit.roundToPx(): Int = toPx().fastRoundToInt()
74
75 /** Convert an [Int] pixel value to [Dp]. */
76 @Stable fun Int.toDp(): Dp = (this / density).dp
77
78 /** Convert an [Int] pixel value to Sp. */
79 @Stable fun Int.toSp(): TextUnit = toDp().toSp()
80
81 /** Convert a [Float] pixel value to a Dp */
82 @Stable fun Float.toDp(): Dp = (this / density).dp
83
84 /** Convert a [Float] pixel value to a Sp */
85 @Stable fun Float.toSp(): TextUnit = toDp().toSp()
86
87 /** Convert a [DpRect] to a [Rect]. */
88 @Stable
89 fun DpRect.toRect(): Rect {
90 return Rect(left.toPx(), top.toPx(), right.toPx(), bottom.toPx())
91 }
92
93 /** Convert a [DpSize] to a [Size]. */
94 @Stable
95 fun DpSize.toSize(): Size =
96 if (isSpecified) {
97 Size(width.toPx(), height.toPx())
98 } else {
99 Size.Unspecified
100 }
101
102 /** Convert a [Size] to a [DpSize]. */
103 @Stable
104 fun Size.toDpSize(): DpSize =
105 if (isSpecified) {
106 DpSize(width.toDp(), height.toDp())
107 } else {
108 DpSize.Unspecified
109 }
110 }
111