Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 19 | #include "DisplayListCanvas.h" |
| 20 | #include "RecordingCanvas.h" |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 21 | #include <SkDrawFilter.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 25 | Canvas* Canvas::create_recording_canvas(int width, int height) { |
| 26 | #if HWUI_NEW_OPS |
| 27 | return new uirenderer::RecordingCanvas(width, height); |
| 28 | #else |
| 29 | return new uirenderer::DisplayListCanvas(width, height); |
| 30 | #endif |
| 31 | } |
| 32 | |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 33 | void Canvas::drawTextDecorations(float x, float y, float length, const SkPaint& paint) { |
| 34 | uint32_t flags; |
| 35 | SkDrawFilter* drawFilter = getDrawFilter(); |
| 36 | if (drawFilter) { |
| 37 | SkPaint paintCopy(paint); |
| 38 | drawFilter->filter(&paintCopy, SkDrawFilter::kText_Type); |
| 39 | flags = paintCopy.getFlags(); |
| 40 | } else { |
| 41 | flags = paint.getFlags(); |
| 42 | } |
| 43 | if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { |
| 44 | // Same values used by Skia |
| 45 | const float kStdStrikeThru_Offset = (-6.0f / 21.0f); |
| 46 | const float kStdUnderline_Offset = (1.0f / 9.0f); |
| 47 | const float kStdUnderline_Thickness = (1.0f / 18.0f); |
| 48 | |
| 49 | SkScalar left = x; |
| 50 | SkScalar right = x + length; |
| 51 | float textSize = paint.getTextSize(); |
| 52 | float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); |
| 53 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 54 | SkScalar top = y + textSize * kStdUnderline_Offset - 0.5f * strokeWidth; |
| 55 | SkScalar bottom = y + textSize * kStdUnderline_Offset + 0.5f * strokeWidth; |
| 56 | drawRect(left, top, right, bottom, paint); |
| 57 | } |
| 58 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 59 | SkScalar top = y + textSize * kStdStrikeThru_Offset - 0.5f * strokeWidth; |
| 60 | SkScalar bottom = y + textSize * kStdStrikeThru_Offset + 0.5f * strokeWidth; |
| 61 | drawRect(left, top, right, bottom, paint); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } // namespace android |