18bf80f4bSopenharmony_ci/* 28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License. 58bf80f4bSopenharmony_ci * You may obtain a copy of the License at 68bf80f4bSopenharmony_ci * 78bf80f4bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88bf80f4bSopenharmony_ci * 98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and 138bf80f4bSopenharmony_ci * limitations under the License. 148bf80f4bSopenharmony_ci */ 158bf80f4bSopenharmony_ci 168bf80f4bSopenharmony_ci#ifndef FONT_DEFS_H 178bf80f4bSopenharmony_ci#define FONT_DEFS_H 188bf80f4bSopenharmony_ci 198bf80f4bSopenharmony_ci#include <cstdint> 208bf80f4bSopenharmony_ci 218bf80f4bSopenharmony_ci#include <base/containers/unordered_map.h> 228bf80f4bSopenharmony_ci#include <base/math/matrix.h> 238bf80f4bSopenharmony_ci#include <font/namespace.h> 248bf80f4bSopenharmony_ci 258bf80f4bSopenharmony_ciFONT_BEGIN_NAMESPACE() 268bf80f4bSopenharmony_ci 278bf80f4bSopenharmony_ci#ifndef VERBOSE 288bf80f4bSopenharmony_ci#define CORE_LOG_N(...) 298bf80f4bSopenharmony_ci#endif 308bf80f4bSopenharmony_ci#ifndef CORE_LOG_N 318bf80f4bSopenharmony_ci#define CORE_LOG_N CORE_LOG_I 328bf80f4bSopenharmony_ci#endif 338bf80f4bSopenharmony_ci 348bf80f4bSopenharmony_ciclass DrawBatcher; 358bf80f4bSopenharmony_ciclass IPaint; 368bf80f4bSopenharmony_ciusing RenderDrawKey = uint64_t; 378bf80f4bSopenharmony_ci 388bf80f4bSopenharmony_cinamespace FontDefs { 398bf80f4bSopenharmony_ci// implementation specific API 408bf80f4bSopenharmony_cistruct RenderData { 418bf80f4bSopenharmony_ci float x; 428bf80f4bSopenharmony_ci float y; 438bf80f4bSopenharmony_ci BASE_NS::Math::Mat4X4 matrix; 448bf80f4bSopenharmony_ci DrawBatcher& drawBatcher; 458bf80f4bSopenharmony_ci const IPaint& paint; 468bf80f4bSopenharmony_ci RenderDrawKey renderDrawKey; 478bf80f4bSopenharmony_ci RENDER_NS::ScissorDesc scissor; 488bf80f4bSopenharmony_ci ClipData clipData; 498bf80f4bSopenharmony_ci 508bf80f4bSopenharmony_ci RenderData(const RenderData&) = delete; 518bf80f4bSopenharmony_ci RenderData& operator=(const RenderData&) = delete; 528bf80f4bSopenharmony_ci}; 538bf80f4bSopenharmony_ci 548bf80f4bSopenharmony_cistruct IRect { 558bf80f4bSopenharmony_ci uint16_t x; 568bf80f4bSopenharmony_ci uint16_t y; 578bf80f4bSopenharmony_ci uint16_t w; 588bf80f4bSopenharmony_ci uint16_t h; 598bf80f4bSopenharmony_ci}; 608bf80f4bSopenharmony_ci 618bf80f4bSopenharmony_cistruct AtlasSlot { 628bf80f4bSopenharmony_ci IRect rect; 638bf80f4bSopenharmony_ci uint16_t index; 648bf80f4bSopenharmony_ci}; 658bf80f4bSopenharmony_ci 668bf80f4bSopenharmony_cistruct Glyph { 678bf80f4bSopenharmony_ci int16_t xMin; 688bf80f4bSopenharmony_ci int16_t xMax; 698bf80f4bSopenharmony_ci int16_t yMin; 708bf80f4bSopenharmony_ci int16_t yMax; 718bf80f4bSopenharmony_ci int16_t hlsb; // left side bearing 728bf80f4bSopenharmony_ci int16_t htsb; // top side bearing 738bf80f4bSopenharmony_ci int16_t hAdv; 748bf80f4bSopenharmony_ci int16_t vlsb; // left side bearing 758bf80f4bSopenharmony_ci int16_t vtsb; // top side bearing 768bf80f4bSopenharmony_ci int16_t vAdv; 778bf80f4bSopenharmony_ci AtlasSlot atlas; 788bf80f4bSopenharmony_ci}; 798bf80f4bSopenharmony_ci 808bf80f4bSopenharmony_ciusing GlyphCache = BASE_NS::unordered_map<uint32_t, FontDefs::Glyph>; 818bf80f4bSopenharmony_ci 828bf80f4bSopenharmony_ciconstexpr uint32_t ATLAS_SIZE = 2048; 838bf80f4bSopenharmony_ci 848bf80f4bSopenharmony_ciconstexpr int ATLAS_ERROR = -1; 858bf80f4bSopenharmony_ciconstexpr int ATLAS_OK = 0; 868bf80f4bSopenharmony_ciconstexpr int ATLAS_RESET = 1; 878bf80f4bSopenharmony_ci 888bf80f4bSopenharmony_ciconstexpr uint8_t BORDER_WIDTH = 1u; 898bf80f4bSopenharmony_ciconstexpr uint8_t BORDER_WIDTH_X2 = 2u * BORDER_WIDTH; 908bf80f4bSopenharmony_ci 918bf80f4bSopenharmony_ciconstexpr const int GLYPH_FIT_THRESHOLD = 4; 928bf80f4bSopenharmony_ci 938bf80f4bSopenharmony_ciconstexpr uint32_t NUM_6 = 6; 948bf80f4bSopenharmony_ciconstexpr uint32_t NUM_32 = 32; 958bf80f4bSopenharmony_ciconstexpr uint32_t NUM_48 = 48; 968bf80f4bSopenharmony_ciconstexpr uint32_t NUM_56 = 56; 978bf80f4bSopenharmony_ci 988bf80f4bSopenharmony_ciinline uint64_t rshift_u64(uint64_t val, uint8_t bits) 998bf80f4bSopenharmony_ci{ 1008bf80f4bSopenharmony_ci return val >> bits; 1018bf80f4bSopenharmony_ci} 1028bf80f4bSopenharmony_ci 1038bf80f4bSopenharmony_ciinline uint64_t lshift_u64(uint64_t val, uint8_t bits) 1048bf80f4bSopenharmony_ci{ 1058bf80f4bSopenharmony_ci return val << bits; 1068bf80f4bSopenharmony_ci} 1078bf80f4bSopenharmony_ci 1088bf80f4bSopenharmony_ciinline uint32_t rshift_u32(uint32_t val, uint8_t bits) 1098bf80f4bSopenharmony_ci{ 1108bf80f4bSopenharmony_ci return val >> bits; 1118bf80f4bSopenharmony_ci} 1128bf80f4bSopenharmony_ci 1138bf80f4bSopenharmony_ciinline uint32_t lshift_u32(uint32_t val, uint8_t bits) 1148bf80f4bSopenharmony_ci{ 1158bf80f4bSopenharmony_ci return val << bits; 1168bf80f4bSopenharmony_ci} 1178bf80f4bSopenharmony_ci 1188bf80f4bSopenharmony_ciinline int32_t rshift_i32(int32_t val, uint8_t bits) 1198bf80f4bSopenharmony_ci{ 1208bf80f4bSopenharmony_ci return val >> bits; 1218bf80f4bSopenharmony_ci} 1228bf80f4bSopenharmony_ci 1238bf80f4bSopenharmony_ciinline int32_t lshift_i32(int32_t val, uint8_t bits) 1248bf80f4bSopenharmony_ci{ 1258bf80f4bSopenharmony_ci return val << bits; 1268bf80f4bSopenharmony_ci} 1278bf80f4bSopenharmony_ci 1288bf80f4bSopenharmony_ciinline uint16_t lshift_u16(uint16_t val, uint8_t bits) 1298bf80f4bSopenharmony_ci{ 1308bf80f4bSopenharmony_ci return uint16_t(val << bits); 1318bf80f4bSopenharmony_ci} 1328bf80f4bSopenharmony_ci 1338bf80f4bSopenharmony_ciinline uint8_t GetStrength(uint64_t glyphKey) 1348bf80f4bSopenharmony_ci{ 1358bf80f4bSopenharmony_ci return rshift_u64(glyphKey, NUM_48) & UINT8_MAX; 1368bf80f4bSopenharmony_ci} 1378bf80f4bSopenharmony_ci 1388bf80f4bSopenharmony_ciinline uint8_t GetSkewX(uint64_t glyphKey) 1398bf80f4bSopenharmony_ci{ 1408bf80f4bSopenharmony_ci return rshift_u64(glyphKey, NUM_56) & UINT8_MAX; 1418bf80f4bSopenharmony_ci} 1428bf80f4bSopenharmony_ci 1438bf80f4bSopenharmony_ciinline int32_t IntToFp26(int32_t val) 1448bf80f4bSopenharmony_ci{ 1458bf80f4bSopenharmony_ci // convert to 26.6 fractional pixels values used by freetype library 1468bf80f4bSopenharmony_ci return lshift_i32(val, NUM_6); 1478bf80f4bSopenharmony_ci} 1488bf80f4bSopenharmony_ci 1498bf80f4bSopenharmony_ciinline int32_t Fp26ToInt(int32_t val) 1508bf80f4bSopenharmony_ci{ 1518bf80f4bSopenharmony_ci // convert from 26.6 fractional pixels values used by freetype library 1528bf80f4bSopenharmony_ci return rshift_i32(val, NUM_6); 1538bf80f4bSopenharmony_ci} 1548bf80f4bSopenharmony_ci// font size in pixel 1558bf80f4bSopenharmony_ciinline uint64_t MakeGlyphKey(float size, uint32_t idx) 1568bf80f4bSopenharmony_ci{ 1578bf80f4bSopenharmony_ci return (static_cast<uint64_t>(size) << NUM_32) | idx; 1588bf80f4bSopenharmony_ci} 1598bf80f4bSopenharmony_ci 1608bf80f4bSopenharmony_ciconstexpr float FLOAT_DIV = 64.f; 1618bf80f4bSopenharmony_ci 1628bf80f4bSopenharmony_ciinline int32_t FloatToFTPos(float x) 1638bf80f4bSopenharmony_ci{ 1648bf80f4bSopenharmony_ci return static_cast<int32_t>(x * FLOAT_DIV); 1658bf80f4bSopenharmony_ci} 1668bf80f4bSopenharmony_ci 1678bf80f4bSopenharmony_ciinline float FTPosToFloat(int32_t x) 1688bf80f4bSopenharmony_ci{ 1698bf80f4bSopenharmony_ci return static_cast<float>(x) / FLOAT_DIV; 1708bf80f4bSopenharmony_ci} 1718bf80f4bSopenharmony_ci} // namespace FontDefs 1728bf80f4bSopenharmony_ci 1738bf80f4bSopenharmony_ciFONT_END_NAMESPACE() 1748bf80f4bSopenharmony_ci#endif // FONT_DEFS_H 175