1 /* 2 * Copyright (C) 2017 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 #ifndef MINIKIN_FONT_STYLE_H 18 #define MINIKIN_FONT_STYLE_H 19 20 #include <minikin/Buffer.h> 21 22 namespace minikin { 23 24 // FontStyle represents style information. 25 class FontStyle { 26 public: 27 enum class Weight : uint16_t { 28 THIN = 100, 29 EXTRA_LIGHT = 200, 30 LIGHT = 300, 31 NORMAL = 400, 32 MEDIUM = 500, 33 SEMI_BOLD = 600, 34 BOLD = 700, 35 EXTRA_BOLD = 800, 36 BLACK = 900, 37 EXTRA_BLACK = 1000, 38 }; 39 40 enum class Slant : bool { 41 ITALIC = true, 42 UPRIGHT = false, 43 }; 44 FontStyle()45 constexpr FontStyle() : FontStyle(Weight::NORMAL, Slant::UPRIGHT) {} FontStyle(Weight weight)46 constexpr explicit FontStyle(Weight weight) : FontStyle(weight, Slant::UPRIGHT) {} FontStyle(Slant slant)47 constexpr explicit FontStyle(Slant slant) : FontStyle(Weight::NORMAL, slant) {} FontStyle(Weight weight,Slant slant)48 constexpr FontStyle(Weight weight, Slant slant) 49 : FontStyle(static_cast<uint16_t>(weight), slant) {} FontStyle(uint16_t weight,Slant slant)50 constexpr FontStyle(uint16_t weight, Slant slant) : mWeight(weight), mSlant(slant) {} FontStyle(BufferReader * reader)51 explicit FontStyle(BufferReader* reader) { 52 mWeight = reader->read<uint16_t>(); 53 mSlant = static_cast<Slant>(reader->read<uint8_t>()); 54 } 55 writeTo(BufferWriter * writer)56 void writeTo(BufferWriter* writer) const { 57 writer->write<uint16_t>(mWeight); 58 writer->write<uint8_t>(static_cast<uint8_t>(mSlant)); 59 } 60 weight()61 constexpr uint16_t weight() const { return mWeight; } slant()62 constexpr Slant slant() const { return mSlant; } 63 64 constexpr bool operator==(const FontStyle& other) const { 65 return weight() == other.weight() && slant() == other.slant(); 66 } 67 identifier()68 constexpr uint32_t identifier() const { 69 return (static_cast<uint32_t>(weight()) << 16) | static_cast<uint32_t>(slant()); 70 } 71 72 private: 73 uint16_t mWeight; 74 Slant mSlant; 75 }; 76 77 } // namespace minikin 78 79 #endif // MINIKIN_FONT_STYLE_H 80