MinikinUtils.cpp revision 63c5c78a72a21d57913e8601cc2a1ab72a424a02
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 "minikin/Layout.h" 23#include "Paint.h" 24#include "TypefaceImpl.h" 25#include "MinikinSkia.h" 26 27#include "MinikinUtils.h" 28 29namespace android { 30 31// Do an sprintf starting at offset n, abort on overflow 32static int snprintfcat(char* buf, int off, int size, const char* format, ...) 33 __attribute__((__format__(__printf__, 4, 5))); 34static int snprintfcat(char* buf, int off, int size, const char* format, ...) { 35 va_list args; 36 va_start(args, format); 37 int n = vsnprintf(buf + off, size - off, format, args); 38 LOG_ALWAYS_FATAL_IF(n >= size - off, "String overflow in setting layout properties"); 39 va_end(args); 40 return off + n; 41} 42 43void MinikinUtils::doLayout(Layout* layout, const Paint* paint, int bidiFlags, TypefaceImpl* typeface, 44 const uint16_t* buf, size_t start, size_t count, size_t bufSize) { 45 TypefaceImpl* resolvedFace = TypefaceImpl_resolveDefault(typeface); 46 layout->setFontCollection(resolvedFace->fFontCollection); 47 FontStyle style = resolvedFace->fStyle; 48 char css[512]; 49 int off = snprintfcat(css, 0, sizeof(css), 50 "font-size: %d; font-scale-x: %f; font-skew-x: %f; -paint-flags: %d;" 51 " font-weight: %d; font-style: %s; -minikin-bidi: %d; letter-spacing: %f;", 52 (int)paint->getTextSize(), 53 paint->getTextScaleX(), 54 paint->getTextSkewX(), 55 MinikinFontSkia::packPaintFlags(paint), 56 style.getWeight() * 100, 57 style.getItalic() ? "italic" : "normal", 58 bidiFlags, 59 paint->getLetterSpacing()); 60 SkString langString = paint->getPaintOptionsAndroid().getLanguage().getTag(); 61 off = snprintfcat(css, off, sizeof(css), " lang: %s;", langString.c_str()); 62 SkPaintOptionsAndroid::FontVariant var = paint->getPaintOptionsAndroid().getFontVariant(); 63 const char* varstr = var == SkPaintOptionsAndroid::kElegant_Variant ? "elegant" : "compact"; 64 off = snprintfcat(css, off, sizeof(css), " -minikin-variant: %s;", varstr); 65 layout->doLayout(buf, start, count, bufSize, std::string(css)); 66} 67 68float MinikinUtils::xOffsetForTextAlign(Paint* paint, const Layout& layout) { 69 switch (paint->getTextAlign()) { 70 case Paint::kCenter_Align: 71 return layout.getAdvance() * -0.5f; 72 break; 73 case Paint::kRight_Align: 74 return -layout.getAdvance(); 75 break; 76 default: 77 break; 78 } 79 return 0; 80} 81 82float MinikinUtils::hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path) { 83 float align = 0; 84 switch (paint->getTextAlign()) { 85 case Paint::kCenter_Align: 86 align = -0.5f; 87 break; 88 case Paint::kRight_Align: 89 align = -1; 90 break; 91 default: 92 return 0; 93 } 94 SkPathMeasure measure(path, false); 95 return align * (layout.getAdvance() - measure.getLength()); 96} 97 98} 99