diff options
| author | Brandon Schaefer <brandontschaefer@gmail.com> | 2014-01-17 09:06:39 -0800 |
|---|---|---|
| committer | Brandon Schaefer <brandontschaefer@gmail.com> | 2014-01-17 09:06:39 -0800 |
| commit | edf6f413a23ba798fca51b2af4fdf84f72cb7264 (patch) | |
| tree | 7a540595d1f06049c34dd7506465b7bae4047858 /unity-shared | |
| parent | 4e21b07a7991bc0c09cbbc699477a137afa9fc13 (diff) | |
* Refactor the EMConverter API.
* Really all thats needed is ConvertPixels(int pixels). This takes in a pixel value, and calculates what the real pixel value should be based on the DPI and font size. * Just need to set the DPI and font size. (bzr r3627.1.1)
Diffstat (limited to 'unity-shared')
| -rw-r--r-- | unity-shared/EMConverter.cpp | 27 | ||||
| -rw-r--r-- | unity-shared/EMConverter.h | 10 |
2 files changed, 33 insertions, 4 deletions
diff --git a/unity-shared/EMConverter.cpp b/unity-shared/EMConverter.cpp index 799c99485..fa67834e8 100644 --- a/unity-shared/EMConverter.cpp +++ b/unity-shared/EMConverter.cpp @@ -22,15 +22,18 @@ namespace unity { +double const BASE_DPI = 96.0; double const DEFAULT_PPE = 10.0; double const PIXELS_PER_INCH = 72.0; EMConverter::EMConverter(int font_size, double dpi) : pixels_per_em_(DEFAULT_PPE) + , base_pixels_per_em_(DEFAULT_PPE) , dpi_(dpi) , font_size_(font_size) { UpdatePixelsPerEM(); + UpdateBasePixelsPerEM(); } void EMConverter::UpdatePixelsPerEM() @@ -41,12 +44,21 @@ void EMConverter::UpdatePixelsPerEM() pixels_per_em_ = DEFAULT_PPE; } +void EMConverter::UpdateBasePixelsPerEM() +{ + base_pixels_per_em_ = font_size_ * BASE_DPI / PIXELS_PER_INCH; + + if (base_pixels_per_em_ == 0) + base_pixels_per_em_ = DEFAULT_PPE; +} + void EMConverter::SetFontSize(int font_size) { if (font_size != font_size_) { font_size_ = font_size; UpdatePixelsPerEM(); + UpdateBasePixelsPerEM(); } } @@ -74,9 +86,20 @@ int EMConverter::EMToPixels(double em) const return (em * pixels_per_em_); } -double EMConverter::PixelsToEM(int pixels) const +double EMConverter::PixelsToBaseEM(int pixels) const +{ + return (pixels / base_pixels_per_em_); +} + +int EMConverter::ConvertPixels(int pixels) const +{ + double pixels_em = PixelsToBaseEM(pixels); + return EMToPixels(pixels_em); +} + +double EMConverter::DPIScale() const { - return (pixels / pixels_per_em_); + return dpi_ / BASE_DPI; } } // namespace unity diff --git a/unity-shared/EMConverter.h b/unity-shared/EMConverter.h index 381a50b70..739ce713a 100644 --- a/unity-shared/EMConverter.h +++ b/unity-shared/EMConverter.h @@ -34,13 +34,19 @@ public: int GetFontSize() const; double GetDPI() const; - int EMToPixels(double em) const; - double PixelsToEM(int pixels) const; + int ConvertPixels(int pixels) const; + double DPIScale() const; private: void UpdatePixelsPerEM(); + void UpdateBasePixelsPerEM(); + + int EMToPixels(double em) const; + double PixelsToBaseEM(int pixels) const; double pixels_per_em_; + double base_pixels_per_em_; + double dpi_; int font_size_; }; |
