MinikinUtils.cpp revision afbd0f1fef46ef0ddf633dfde0de724db3da1405
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 FontVariant minikinVariant = (paint->getFontVariant() == VARIANT_ELEGANT) ? VARIANT_ELEGANT 37 : VARIANT_COMPACT; 38 const uint32_t langListId = paint->getMinikinLangListId(); 39 FontStyle minikinStyle(langListId, minikinVariant, resolved.getWeight(), resolved.getItalic()); 40 41 /* Prepare minikin Paint */ 42 // Note: it would be nice to handle fractional size values (it would improve smooth zoom 43 // behavior), but historically size has been treated as an int. 44 // TODO: explore whether to enable fractional sizes, possibly when linear text flag is set. 45 minikinPaint->size = (int)paint->getTextSize(); 46 minikinPaint->scaleX = paint->getTextScaleX(); 47 minikinPaint->skewX = paint->getTextSkewX(); 48 minikinPaint->letterSpacing = paint->getLetterSpacing(); 49 minikinPaint->paintFlags = MinikinFontSkia::packPaintFlags(paint); 50 minikinPaint->fontFeatureSettings = paint->getFontFeatureSettings(); 51 minikinPaint->hyphenEdit = HyphenEdit(paint->getHyphenEdit()); 52 return minikinStyle; 53} 54 55void MinikinUtils::doLayout(Layout* layout, const Paint* paint, int bidiFlags, 56 TypefaceImpl* typeface, const uint16_t* buf, size_t start, size_t count, 57 size_t bufSize) { 58 FontCollection *font; 59 MinikinPaint minikinPaint; 60 FontStyle minikinStyle = prepareMinikinPaint(&minikinPaint, &font, paint, typeface); 61 layout->setFontCollection(font); 62 layout->doLayout(buf, start, count, bufSize, bidiFlags, minikinStyle, minikinPaint); 63} 64 65float MinikinUtils::measureText(const Paint* paint, int bidiFlags, TypefaceImpl* typeface, 66 const uint16_t* buf, size_t start, size_t count, size_t bufSize, float *advances) { 67 FontCollection *font; 68 MinikinPaint minikinPaint; 69 FontStyle minikinStyle = prepareMinikinPaint(&minikinPaint, &font, paint, typeface); 70 return Layout::measureText(buf, start, count, bufSize, bidiFlags, minikinStyle, minikinPaint, 71 font, advances); 72} 73 74bool MinikinUtils::hasVariationSelector(TypefaceImpl* typeface, uint32_t codepoint, uint32_t vs) { 75 const TypefaceImpl* resolvedFace = TypefaceImpl_resolveDefault(typeface); 76 return resolvedFace->fFontCollection->hasVariationSelector(codepoint, vs); 77} 78 79float MinikinUtils::xOffsetForTextAlign(Paint* paint, const Layout& layout) { 80 switch (paint->getTextAlign()) { 81 case Paint::kCenter_Align: 82 return layout.getAdvance() * -0.5f; 83 break; 84 case Paint::kRight_Align: 85 return -layout.getAdvance(); 86 break; 87 default: 88 break; 89 } 90 return 0; 91} 92 93float MinikinUtils::hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path) { 94 float align = 0; 95 switch (paint->getTextAlign()) { 96 case Paint::kCenter_Align: 97 align = -0.5f; 98 break; 99 case Paint::kRight_Align: 100 align = -1; 101 break; 102 default: 103 return 0; 104 } 105 SkPathMeasure measure(path, false); 106 return align * (layout.getAdvance() - measure.getLength()); 107} 108 109} 110