1/* 2 * Copyright (C) 2014 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#define LOG_TAG "Minikin" 18#include <cutils/log.h> 19#include <string> 20 21#include "SkPathMeasure.h" 22#include "Paint.h" 23#include "TypefaceImpl.h" 24 25#include "MinikinUtils.h" 26 27namespace android { 28 29FontStyle MinikinUtils::prepareMinikinPaint(MinikinPaint* minikinPaint, FontCollection** pFont, 30 const Paint* paint, TypefaceImpl* typeface) { 31 const TypefaceImpl* resolvedFace = TypefaceImpl_resolveDefault(typeface); 32 *pFont = resolvedFace->fFontCollection; 33 FontStyle resolved = resolvedFace->fStyle; 34 35 /* Prepare minikin FontStyle */ 36 const std::string& lang = paint->getTextLocale(); 37 FontLanguage minikinLang(lang.c_str(), lang.size()); 38 FontVariant minikinVariant = (paint->getFontVariant() == VARIANT_ELEGANT) ? VARIANT_ELEGANT 39 : VARIANT_COMPACT; 40 FontStyle minikinStyle(minikinLang, minikinVariant, resolved.getWeight(), resolved.getItalic()); 41 42 /* Prepare minikin Paint */ 43 // Note: it would be nice to handle fractional size values (it would improve smooth zoom 44 // behavior), but historically size has been treated as an int. 45 // TODO: explore whether to enable fractional sizes, possibly when linear text flag is set. 46 minikinPaint->size = (int)paint->getTextSize(); 47 minikinPaint->scaleX = paint->getTextScaleX(); 48 minikinPaint->skewX = paint->getTextSkewX(); 49 minikinPaint->letterSpacing = paint->getLetterSpacing(); 50 minikinPaint->paintFlags = MinikinFontSkia::packPaintFlags(paint); 51 minikinPaint->fontFeatureSettings = paint->getFontFeatureSettings(); 52 minikinPaint->hyphenEdit = HyphenEdit(paint->getHyphenEdit()); 53 return minikinStyle; 54} 55 56void MinikinUtils::doLayout(Layout* layout, const Paint* paint, int bidiFlags, 57 TypefaceImpl* typeface, const uint16_t* buf, size_t start, size_t count, 58 size_t bufSize) { 59 FontCollection *font; 60 MinikinPaint minikinPaint; 61 FontStyle minikinStyle = prepareMinikinPaint(&minikinPaint, &font, paint, typeface); 62 layout->setFontCollection(font); 63 layout->doLayout(buf, start, count, bufSize, bidiFlags, minikinStyle, minikinPaint); 64} 65 66float MinikinUtils::xOffsetForTextAlign(Paint* paint, const Layout& layout) { 67 switch (paint->getTextAlign()) { 68 case Paint::kCenter_Align: 69 return layout.getAdvance() * -0.5f; 70 break; 71 case Paint::kRight_Align: 72 return -layout.getAdvance(); 73 break; 74 default: 75 break; 76 } 77 return 0; 78} 79 80float MinikinUtils::hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path) { 81 float align = 0; 82 switch (paint->getTextAlign()) { 83 case Paint::kCenter_Align: 84 align = -0.5f; 85 break; 86 case Paint::kRight_Align: 87 align = -1; 88 break; 89 default: 90 return 0; 91 } 92 SkPathMeasure measure(path, false); 93 return align * (layout.getAdvance() - measure.getLength()); 94} 95 96} 97