blob: bc88c817ffc8d5f5cc7efcd1fe7884ba67ae47c5 [file] [log] [blame]
Chris Craika1717272015-11-19 13:02:43 -08001/*
2 * Copyright (C) 2015 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#include "Canvas.h"
18
19#include <SkDrawFilter.h>
20
21namespace android {
22
23void Canvas::drawTextDecorations(float x, float y, float length, const SkPaint& paint) {
24 uint32_t flags;
25 SkDrawFilter* drawFilter = getDrawFilter();
26 if (drawFilter) {
27 SkPaint paintCopy(paint);
28 drawFilter->filter(&paintCopy, SkDrawFilter::kText_Type);
29 flags = paintCopy.getFlags();
30 } else {
31 flags = paint.getFlags();
32 }
33 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
34 // Same values used by Skia
35 const float kStdStrikeThru_Offset = (-6.0f / 21.0f);
36 const float kStdUnderline_Offset = (1.0f / 9.0f);
37 const float kStdUnderline_Thickness = (1.0f / 18.0f);
38
39 SkScalar left = x;
40 SkScalar right = x + length;
41 float textSize = paint.getTextSize();
42 float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
43 if (flags & SkPaint::kUnderlineText_Flag) {
44 SkScalar top = y + textSize * kStdUnderline_Offset - 0.5f * strokeWidth;
45 SkScalar bottom = y + textSize * kStdUnderline_Offset + 0.5f * strokeWidth;
46 drawRect(left, top, right, bottom, paint);
47 }
48 if (flags & SkPaint::kStrikeThruText_Flag) {
49 SkScalar top = y + textSize * kStdStrikeThru_Offset - 0.5f * strokeWidth;
50 SkScalar bottom = y + textSize * kStdStrikeThru_Offset + 0.5f * strokeWidth;
51 drawRect(left, top, right, bottom, paint);
52 }
53 }
54}
55
56} // namespace android