aboutsummaryrefslogtreecommitdiffhomepage
path: root/cocoa/QuartzTextStyleAttribute.h
diff options
context:
space:
mode:
authorNeil Hodgson <nyamatongwe@gmail.com>2024-08-16 17:21:53 +1000
committerNeil Hodgson <nyamatongwe@gmail.com>2024-08-16 17:21:53 +1000
commitae3b01d2b1eb8dc532f616fb2f24aac954775f45 (patch)
tree17247b5f07753ab2d81758ce64c6f677ff26b41a /cocoa/QuartzTextStyleAttribute.h
parentdcbc339899911e1a3a743de1c0c25d0c253dd39a (diff)
downloadscintilla-mirror-ae3b01d2b1eb8dc532f616fb2f24aac954775f45.tar.gz
On Cocoa implement more values of font weight and stretch.
Diffstat (limited to 'cocoa/QuartzTextStyleAttribute.h')
-rw-r--r--cocoa/QuartzTextStyleAttribute.h88
1 files changed, 55 insertions, 33 deletions
diff --git a/cocoa/QuartzTextStyleAttribute.h b/cocoa/QuartzTextStyleAttribute.h
index 22af29b4a..558daa58c 100644
--- a/cocoa/QuartzTextStyleAttribute.h
+++ b/cocoa/QuartzTextStyleAttribute.h
@@ -12,6 +12,42 @@
#ifndef QUARTZTEXTSTYLEATTRIBUTE_H
#define QUARTZTEXTSTYLEATTRIBUTE_H
+// Convert from a FontWeight value to a floating point value to pass to CoreText.
+// This was defined based on Cocoa's NSFontWeight* values, discussion by other open
+// source projects then tweaked until most values produced visibly different results.
+inline double WeightFromEnumeration(Scintilla::FontWeight weight) {
+ switch (static_cast<int>(weight)/100) {
+ case 0: return -1.0;
+ case 1: return -0.7;
+ case 2: return -0.5;
+ case 3: return -0.23;
+ case 4: return 0.0;
+ case 5: return 0.2;
+ case 6: return 0.3;
+ case 7: return 0.4;
+ case 8: return 0.6;
+ case 9: return 0.8;
+ }
+ return 0.0;
+}
+
+// Convert from a FontStretch value to a floating point value to pass to CoreText.
+// This was defined based on values used by other open source projects then tweaked
+// until most values produced reasonable results.
+inline double StretchFromEnumeration(Scintilla::FontStretch stretch) {
+ switch (stretch) {
+ case Scintilla::FontStretch::UltraCondensed: return -0.8;
+ case Scintilla::FontStretch::ExtraCondensed: return -0.3;
+ case Scintilla::FontStretch::Condensed: return -0.23;
+ case Scintilla::FontStretch::SemiCondensed: return -0.1;
+ case Scintilla::FontStretch::Normal: return 0.0;
+ case Scintilla::FontStretch::SemiExpanded: return 0.1;
+ case Scintilla::FontStretch::Expanded: return 0.2;
+ case Scintilla::FontStretch::ExtraExpanded: return 0.3;
+ case Scintilla::FontStretch::UltraExpanded: return 0.7;
+ }
+}
+
class QuartzFont {
public:
/** Create a font style from a name. */
@@ -20,42 +56,28 @@ public:
CFStringRef fontName = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingMacRoman);
assert(fontName != NULL);
- bool bold = weight > Scintilla::FontWeight::Normal;
- if (bold || italic || stretch != Scintilla::FontStretch::Normal) {
- CTFontSymbolicTraits desiredTrait = 0;
- CTFontSymbolicTraits traitMask = 0;
-
- // if bold was specified, add the trait
- if (bold) {
- desiredTrait |= kCTFontBoldTrait;
- traitMask |= kCTFontBoldTrait;
- }
+ // Specify the weight, stretch, and italics
+ DictionaryForCF traits;
+ const double weightValue = WeightFromEnumeration(weight);
+ traits.SetItem(kCTFontWeightTrait, kCFNumberCGFloatType, &weightValue);
+ const double stretchValue = StretchFromEnumeration(stretch);
+ traits.SetItem(kCTFontWidthTrait, kCFNumberCGFloatType, &stretchValue);
+ if (italic) {
+ const int italicValue = kCTFontTraitItalic;
+ traits.SetItem(kCTFontSymbolicTrait, kCFNumberIntType, &italicValue);
+ }
- // if italic was specified, add the trait
- if (italic) {
- desiredTrait |= kCTFontItalicTrait;
- traitMask |= kCTFontItalicTrait;
- }
- if (stretch < Scintilla::FontStretch::Normal) {
- desiredTrait |= kCTFontCondensedTrait;
- traitMask |= kCTFontCondensedTrait;
- } else if (stretch > Scintilla::FontStretch::Normal) {
- desiredTrait |= kCTFontExpandedTrait;
- traitMask |= kCTFontExpandedTrait;
- }
+ // create a font decriptor and then a font with that descriptor
+ DictionaryForCF attributes;
+ attributes.SetValue(kCTFontTraitsAttribute, traits.get());
+ attributes.SetValue(kCTFontNameAttribute, fontName);
- // create a font and then a copy of it with the sym traits
- CTFontRef iFont = ::CTFontCreateWithName(fontName, size, NULL);
- fontid = ::CTFontCreateCopyWithSymbolicTraits(iFont, size, NULL, desiredTrait, traitMask);
- if (fontid) {
- CFRelease(iFont);
- } else {
- // Traits failed so use base font
- fontid = iFont;
- }
- } else {
- // create the font, no traits
+ CTFontDescriptorRef desc = ::CTFontDescriptorCreateWithAttributes(attributes.get());
+ fontid = ::CTFontCreateWithFontDescriptor(desc, size, NULL);
+ CFRelease(desc);
+ if (!fontid) {
+ // Traits failed so use base font
fontid = ::CTFontCreateWithName(fontName, size, NULL);
}