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#include "font.h"
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <cfloat>
198bf80f4bSopenharmony_ci
208bf80f4bSopenharmony_ci#include "face_data.h"
218bf80f4bSopenharmony_ci#include "font_buffer.h"
228bf80f4bSopenharmony_ci#include "font_data.h"
238bf80f4bSopenharmony_ci
248bf80f4bSopenharmony_ciusing namespace BASE_NS;
258bf80f4bSopenharmony_ci
268bf80f4bSopenharmony_ciFONT_BEGIN_NAMESPACE()
278bf80f4bSopenharmony_ciFont::Font(std::shared_ptr<FaceData>&& faceData) : faceData_(faceData) {}
288bf80f4bSopenharmony_ci
298bf80f4bSopenharmony_civoid Font::SetSize(FontSize sizeInPt)
308bf80f4bSopenharmony_ci{
318bf80f4bSopenharmony_ci    this->fontSize_ = sizeInPt;
328bf80f4bSopenharmony_ci    this->data_ = nullptr;
338bf80f4bSopenharmony_ci}
348bf80f4bSopenharmony_ci
358bf80f4bSopenharmony_ciFontSize Font::GetSize()
368bf80f4bSopenharmony_ci{
378bf80f4bSopenharmony_ci    return fontSize_;
388bf80f4bSopenharmony_ci}
398bf80f4bSopenharmony_ci
408bf80f4bSopenharmony_civoid Font::SetDpi(uint16_t x, uint16_t y)
418bf80f4bSopenharmony_ci{
428bf80f4bSopenharmony_ci    xDpi_ = x;
438bf80f4bSopenharmony_ci    yDpi_ = y;
448bf80f4bSopenharmony_ci    this->data_ = nullptr;
458bf80f4bSopenharmony_ci}
468bf80f4bSopenharmony_civoid Font::GetDpi(uint16_t& x, uint16_t& y)
478bf80f4bSopenharmony_ci{
488bf80f4bSopenharmony_ci    x = xDpi_;
498bf80f4bSopenharmony_ci    y = yDpi_;
508bf80f4bSopenharmony_ci}
518bf80f4bSopenharmony_ci
528bf80f4bSopenharmony_ciFontData* Font::GetData()
538bf80f4bSopenharmony_ci{
548bf80f4bSopenharmony_ci    if (!data_) {
558bf80f4bSopenharmony_ci        data_ = faceData_->CreateFontData(fontSize_, xDpi_, yDpi_);
568bf80f4bSopenharmony_ci    }
578bf80f4bSopenharmony_ci    return data_;
588bf80f4bSopenharmony_ci}
598bf80f4bSopenharmony_ci
608bf80f4bSopenharmony_ciBASE_NS::array_view<uint8_t> Font::GetFontData()
618bf80f4bSopenharmony_ci{
628bf80f4bSopenharmony_ci    return faceData_->fontBuffer_->bytes_;
638bf80f4bSopenharmony_ci}
648bf80f4bSopenharmony_ci
658bf80f4bSopenharmony_civoid Font::DrawGlyphs(BASE_NS::array_view<const GlyphInfo> glyphs, const FontDefs::RenderData& renderData)
668bf80f4bSopenharmony_ci{
678bf80f4bSopenharmony_ci    GetData()->DrawGlyphs(glyphs, renderData);
688bf80f4bSopenharmony_ci}
698bf80f4bSopenharmony_ci
708bf80f4bSopenharmony_civoid Font::DrawString(const BASE_NS::string_view string, const FontDefs::RenderData& renderData)
718bf80f4bSopenharmony_ci{
728bf80f4bSopenharmony_ci    GetData()->DrawString(string, renderData);
738bf80f4bSopenharmony_ci}
748bf80f4bSopenharmony_ci
758bf80f4bSopenharmony_ciBASE_NS::Math::Vec2 Font::MeasureString(const BASE_NS::string_view string)
768bf80f4bSopenharmony_ci{
778bf80f4bSopenharmony_ci    return GetData()->MeasureString(string);
788bf80f4bSopenharmony_ci}
798bf80f4bSopenharmony_ci
808bf80f4bSopenharmony_ciFontMetrics Font::GetMetrics()
818bf80f4bSopenharmony_ci{
828bf80f4bSopenharmony_ci    return GetData()->GetMetrics();
838bf80f4bSopenharmony_ci}
848bf80f4bSopenharmony_ci
858bf80f4bSopenharmony_ciGlyphMetrics Font::GetGlyphMetrics(uint32_t glyphIndex)
868bf80f4bSopenharmony_ci{
878bf80f4bSopenharmony_ci    return GetData()->GetGlyphMetrics(glyphIndex);
888bf80f4bSopenharmony_ci}
898bf80f4bSopenharmony_ci
908bf80f4bSopenharmony_ciGlyphInfo Font::GetGlyphInfo(uint32_t glyphIndex)
918bf80f4bSopenharmony_ci{
928bf80f4bSopenharmony_ci    return GetData()->GetGlyphInfo(glyphIndex);
938bf80f4bSopenharmony_ci}
948bf80f4bSopenharmony_ci
958bf80f4bSopenharmony_ciuint32_t Font::GetGlyphIndex(uint32_t code)
968bf80f4bSopenharmony_ci{
978bf80f4bSopenharmony_ci    return faceData_->GetGlyphIndex(code);
988bf80f4bSopenharmony_ci}
998bf80f4bSopenharmony_ci
1008bf80f4bSopenharmony_ciconst CORE_NS::IInterface* Font::GetInterface(const BASE_NS::Uid& uid) const
1018bf80f4bSopenharmony_ci{
1028bf80f4bSopenharmony_ci    if (uid == CORE_NS::IInterface::UID) {
1038bf80f4bSopenharmony_ci        return this;
1048bf80f4bSopenharmony_ci    }
1058bf80f4bSopenharmony_ci    if (uid == IFont::UID) {
1068bf80f4bSopenharmony_ci        return this;
1078bf80f4bSopenharmony_ci    }
1088bf80f4bSopenharmony_ci    return nullptr;
1098bf80f4bSopenharmony_ci}
1108bf80f4bSopenharmony_ci
1118bf80f4bSopenharmony_ciCORE_NS::IInterface* Font::GetInterface(const BASE_NS::Uid& uid)
1128bf80f4bSopenharmony_ci{
1138bf80f4bSopenharmony_ci    if (uid == CORE_NS::IInterface::UID) {
1148bf80f4bSopenharmony_ci        return this;
1158bf80f4bSopenharmony_ci    }
1168bf80f4bSopenharmony_ci    if (uid == IFont::UID) {
1178bf80f4bSopenharmony_ci        return this;
1188bf80f4bSopenharmony_ci    }
1198bf80f4bSopenharmony_ci    return nullptr;
1208bf80f4bSopenharmony_ci}
1218bf80f4bSopenharmony_ci
1228bf80f4bSopenharmony_civoid Font::Ref()
1238bf80f4bSopenharmony_ci{
1248bf80f4bSopenharmony_ci    refcnt_.fetch_add(1, std::memory_order_relaxed);
1258bf80f4bSopenharmony_ci}
1268bf80f4bSopenharmony_ci
1278bf80f4bSopenharmony_civoid Font::Unref()
1288bf80f4bSopenharmony_ci{
1298bf80f4bSopenharmony_ci    if (refcnt_.fetch_sub(1, std::memory_order_release) == 1) {
1308bf80f4bSopenharmony_ci        std::atomic_thread_fence(std::memory_order_acquire);
1318bf80f4bSopenharmony_ci        delete this;
1328bf80f4bSopenharmony_ci    }
1338bf80f4bSopenharmony_ci}
1348bf80f4bSopenharmony_ci
1358bf80f4bSopenharmony_ciFONT_END_NAMESPACE()
136